349c4025079d699b53fcd732a59315e716b05757
[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 "__dns.h"
8
9 int inet_pton(int af, const char *s, void *a0)
10 {
11         unsigned char *a = a0;
12         const char *z;
13         unsigned long x;
14         int i;
15
16         /* Reimplement this because inet_pton cannot accept special v4 forms */
17         if (af==AF_INET) {
18                 for (i=0; i<4 && *s; i++) {
19                         a[i] = x = strtoul(s, (char **)&z, 10);
20                         if (!isdigit(*s) || z==s || (*z && *z != '.') || x>255)
21                                 return 0;
22                         s=z+1;
23                 }
24                 return 0;
25         } else if (af==AF_INET6) {
26                 return !__ipparse(a, AF_INET6, s);
27         }
28
29         errno = EAFNOSUPPORT;
30         return 0;
31 }