ipv6 parsing code (formerly dummied-out)
[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]==':') {
40                 s+=2;
41                 brk=0;
42         }
43
44         for (i=0; ; i++, s+=j+1) {
45                 if (s[0]==':' && brk<0) {
46                         brk=i;
47                         j=0;
48                         ip[i]=0;
49                         if (!s[1]) break;
50                         continue;
51                 }
52                 if (hexval(s[0])<0) return -1;
53                 while (s[0]=='0' && s[1]=='0') s++;
54                 for (v=j=0; j<5 && (d=hexval(s[j]))>=0; j++)
55                         v=16*v+d;
56                 if (v > 65535) return -1;
57                 ip[i] = v;
58                 if (!s[j]) {
59                         if (brk<0 && i!=7) return -1;
60                         break;
61                 }
62                 if (i<7) {
63                         if (s[j]==':') continue;
64                         if (s[j]!='.') return -1;
65                         need_v4=1;
66                         i++;
67                         break;
68                 }
69                 return -1;
70         }
71         if (brk>=0) {
72                 memmove(ip+brk+7-i, ip+brk, 2*(i+1-brk));
73                 for (j=0; j<7-i; j++) ip[brk+j] = 0;
74         }
75         for (j=0; j<8; j++) {
76                 *a++ = ip[j]>>8;
77                 *a++ = ip[j];
78         }
79         if (need_v4 &&inet_pton(AF_INET, (void *)s, a-4) <= 0) return -1;
80         return 1;
81 }