fix bug in ipv6 parsing that prevented parsing a lone "::"
[musl] / src / network / inet_pton.c
1 #include <sys/socket.h>
2 #include <netdb.h>
3 #include <arpa/inet.h>
4 #include <stdlib.h>
5 #include <ctype.h>
6 #include <errno.h>
7 #include <string.h>
8
9 static int hexval(unsigned c)
10 {
11         if (c-'0'<10) return c-'0';
12         c |= 32;
13         if (c-'a'<6) return c-'a'+10;
14         return -1;
15 }
16
17 int inet_pton(int af, const char *s, void *a0)
18 {
19         uint16_t ip[8];
20         unsigned char *a = a0;
21         const char *z;
22         unsigned long x;
23         int i, j, v, d, brk=-1, need_v4=0;
24
25         /* Reimplement this because inet_pton cannot accept special v4 forms */
26         if (af==AF_INET) {
27                 for (i=0; i<4 && *s; i++) {
28                         a[i] = x = strtoul(s, (char **)&z, 10);
29                         if (!isdigit(*s) || z==s || (*z && *z != '.') || x>255)
30                                 return 0;
31                         s=z+1;
32                 }
33                 return 1;
34         } else if (af!=AF_INET6) {
35                 errno = EAFNOSUPPORT;
36                 return -1;
37         }
38
39         if (s[0]==':' && s[1]==':') s++;
40
41         for (i=0; ; i++, s+=j+1) {
42                 if (s[0]==':' && brk<0) {
43                         brk=i;
44                         j=0;
45                         ip[i]=0;
46                         if (!s[1]) break;
47                         continue;
48                 }
49                 if (hexval(s[0])<0) return -1;
50                 while (s[0]=='0' && s[1]=='0') s++;
51                 for (v=j=0; j<5 && (d=hexval(s[j]))>=0; j++)
52                         v=16*v+d;
53                 if (v > 65535) return -1;
54                 ip[i] = v;
55                 if (!s[j]) {
56                         if (brk<0 && i!=7) return -1;
57                         break;
58                 }
59                 if (i<7) {
60                         if (s[j]==':') continue;
61                         if (s[j]!='.') return -1;
62                         need_v4=1;
63                         i++;
64                         break;
65                 }
66                 return -1;
67         }
68         if (brk>=0) {
69                 memmove(ip+brk+7-i, ip+brk, 2*(i+1-brk));
70                 for (j=0; j<7-i; j++) ip[brk+j] = 0;
71         }
72         for (j=0; j<8; j++) {
73                 *a++ = ip[j]>>8;
74                 *a++ = ip[j];
75         }
76         if (need_v4 &&inet_pton(AF_INET, (void *)s, a-4) <= 0) return -1;
77         return 1;
78 }