1 #include <sys/socket.h>
9 static int hexval(unsigned c)
11 if (c-'0'<10) return c-'0';
13 if (c-'a'<6) return c-'a'+10;
17 int inet_pton(int af, const char *restrict s, void *restrict a0)
20 unsigned char *a = a0;
23 int i, j, v, d, brk=-1, need_v4=0;
25 /* Reimplement this because inet_pton cannot accept special v4 forms */
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)
34 } else if (af!=AF_INET6) {
39 if (s[0]==':' && s[1]==':') s++;
41 for (i=0; ; i++, s+=j+1) {
42 if (s[0]==':' && brk<0) {
49 if (hexval(s[0])<0) return 0;
50 while (s[0]=='0' && s[1]=='0') s++;
51 for (v=j=0; j<5 && (d=hexval(s[j]))>=0; j++)
53 if (v > 65535) return 0;
56 if (brk<0 && i!=7) return 0;
60 if (s[j]==':') continue;
61 if (s[j]!='.') return 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;
76 if (need_v4 && inet_pton(AF_INET, (void *)s, a-4) <= 0) return 0;