1 #include <sys/socket.h>
8 static int hexval(unsigned c)
10 if (c-'0'<10) return c-'0';
12 if (c-'a'<6) return c-'a'+10;
16 int inet_pton(int af, const char *restrict s, void *restrict a0)
19 unsigned char *a = a0;
20 int i, j, v, d, brk=-1, need_v4=0;
24 for (v=j=0; j<3 && isdigit(s[j]); j++)
26 if (j==0 || (j>1 && s[0]=='0') || v>255) return 0;
28 if (s[j]==0 && i==3) return 1;
29 if (s[j]!='.') return 0;
33 } else if (af!=AF_INET6) {
38 if (*s==':' && *++s!=':') return 0;
41 if (s[0]==':' && brk<0) {
47 for (v=j=0; j<4 && (d=hexval(s[j]))>=0; j++)
49 if (j==0 || v > 65535) return 0;
51 if (!s[j] && (brk>=0 || i==7)) break;
54 if (s[j]!='.' || (i<6 && brk<0)) return 0;
62 memmove(ip+brk+7-i, ip+brk, 2*(i+1-brk));
63 for (j=0; j<7-i; j++) ip[brk+j] = 0;
69 if (need_v4 && inet_pton(AF_INET, (void *)s, a-4) <= 0) return 0;