1 #include <sys/socket.h>
7 static int hexval(unsigned c)
9 if (c-'0'<10) return c-'0';
11 if (c-'a'<6) return c-'a'+10;
15 int inet_pton(int af, const char *restrict s, void *restrict a0)
18 unsigned char *a = a0;
19 int i, j, v, d, brk=-1, need_v4=0;
23 for (v=j=0; j<3 && isdigit(s[j]); j++)
25 if (j==0 || (j>1 && s[0]=='0') || v>255) return 0;
27 if (s[j]==0 && i==3) return 1;
28 if (s[j]!='.') return 0;
32 } else if (af!=AF_INET6) {
37 if (*s==':' && *++s!=':') return 0;
40 if (s[0]==':' && brk<0) {
46 for (v=j=0; j<4 && (d=hexval(s[j]))>=0; j++)
50 if (!s[j] && (brk>=0 || i==7)) break;
53 if (s[j]!='.' || (i<6 && brk<0)) return 0;
61 memmove(ip+brk+7-i, ip+brk, 2*(i+1-brk));
62 for (j=0; j<7-i; j++) ip[brk+j] = 0;
68 if (need_v4 && inet_pton(AF_INET, (void *)s, a-4) <= 0) return 0;