initial check-in, version 0.5.0
[musl] / src / network / __ipparse.c
1 #include <string.h>
2 #include <stdlib.h>
3 #include <sys/socket.h>
4 #include <netinet/in.h>
5 #include "__dns.h"
6 #include <stdio.h>
7
8 int __ipparse(void *dest, int family, const char *s)
9 {
10         unsigned char *d = dest;
11         unsigned long a[16] = { 0 };
12         const char *z;
13         int i;
14
15         if (family == AF_INET6) goto not_v4;
16
17         for (i=0; i<4 && *s; i++) {
18                 a[i] = strtoul(s, (char **)&z, 0);
19                 if (z==s || (*z && *z != '.')) goto not_v4;
20                 s=z+1;
21         }
22         switch (i) {
23         case 0:
24                 a[1] = a[0] & 0xffffff;
25                 a[0] >>= 24;
26         case 1:
27                 a[2] = a[1] & 0xffff;
28                 a[1] >>= 16;
29         case 2:
30                 a[3] = a[2] & 0xff;
31                 a[2] >>= 8;
32         }
33         ((struct sockaddr_in *)d)->sin_family = AF_INET;
34         d = (void *)&((struct sockaddr_in *)d)->sin_addr;
35         for (i=0; i<4; i++) d[i] = a[i];
36         return 0;
37
38 not_v4:
39         return -1;
40 }