xref: /haiku/src/system/libnetwork/musl/network/inet_aton.c (revision cbf71a819fd6ceaa0d813b5956060516cf0d842a)
1*cbf71a81SAugustin Cavalier #include <features.h>
2*cbf71a81SAugustin Cavalier #include <ctype.h>
3*cbf71a81SAugustin Cavalier #include <sys/socket.h>
4*cbf71a81SAugustin Cavalier #include <netinet/in.h>
5*cbf71a81SAugustin Cavalier #include <arpa/inet.h>
6*cbf71a81SAugustin Cavalier #include <stdlib.h>
7*cbf71a81SAugustin Cavalier 
__inet_aton(const char * s0,struct in_addr * dest)8*cbf71a81SAugustin Cavalier int __inet_aton(const char *s0, struct in_addr *dest)
9*cbf71a81SAugustin Cavalier {
10*cbf71a81SAugustin Cavalier 	const char *s = s0;
11*cbf71a81SAugustin Cavalier 	unsigned char *d = (void *)dest;
12*cbf71a81SAugustin Cavalier 	unsigned long a[4] = { 0 };
13*cbf71a81SAugustin Cavalier 	char *z;
14*cbf71a81SAugustin Cavalier 	int i;
15*cbf71a81SAugustin Cavalier 
16*cbf71a81SAugustin Cavalier 	for (i=0; i<4; i++) {
17*cbf71a81SAugustin Cavalier 		a[i] = strtoul(s, &z, 0);
18*cbf71a81SAugustin Cavalier 		if (z==s || (*z && *z != '.') || !isdigit(*s))
19*cbf71a81SAugustin Cavalier 			return 0;
20*cbf71a81SAugustin Cavalier 		if (!*z) break;
21*cbf71a81SAugustin Cavalier 		s=z+1;
22*cbf71a81SAugustin Cavalier 	}
23*cbf71a81SAugustin Cavalier 	if (i==4) return 0;
24*cbf71a81SAugustin Cavalier 	switch (i) {
25*cbf71a81SAugustin Cavalier 	case 0:
26*cbf71a81SAugustin Cavalier 		a[1] = a[0] & 0xffffff;
27*cbf71a81SAugustin Cavalier 		a[0] >>= 24;
28*cbf71a81SAugustin Cavalier 	case 1:
29*cbf71a81SAugustin Cavalier 		a[2] = a[1] & 0xffff;
30*cbf71a81SAugustin Cavalier 		a[1] >>= 16;
31*cbf71a81SAugustin Cavalier 	case 2:
32*cbf71a81SAugustin Cavalier 		a[3] = a[2] & 0xff;
33*cbf71a81SAugustin Cavalier 		a[2] >>= 8;
34*cbf71a81SAugustin Cavalier 	}
35*cbf71a81SAugustin Cavalier 	for (i=0; i<4; i++) {
36*cbf71a81SAugustin Cavalier 		if (a[i] > 255) return 0;
37*cbf71a81SAugustin Cavalier 		d[i] = a[i];
38*cbf71a81SAugustin Cavalier 	}
39*cbf71a81SAugustin Cavalier 	return 1;
40*cbf71a81SAugustin Cavalier }
41*cbf71a81SAugustin Cavalier 
42*cbf71a81SAugustin Cavalier weak_alias(__inet_aton, inet_aton);
43