fix regression in inet_aton due to misinterpretation of __ipparse return
[musl] / src / network / inet_legacy.c
1 #include <sys/socket.h>
2 #include <netinet/in.h>
3 #include <arpa/inet.h>
4 #include "__dns.h"
5
6 in_addr_t inet_network(const char *p)
7 {
8         return ntohl(inet_addr(p));
9 }
10
11 int inet_aton(const char *cp, struct in_addr *inp)
12 {
13         struct sockaddr_in sin;
14         if (__ipparse(&sin, AF_INET, cp) < 0) return 0;
15         *inp = sin.sin_addr;
16         return 1;
17 }
18
19 struct in_addr inet_makeaddr(int net, int host)
20 {
21         uint32_t n = net, h = host;
22         if (n < 256) h |= n<<24;
23         else if (n < 65536) h |= n<<16;
24         else h |= n<<8;
25         return (struct in_addr){ h };
26 }
27
28 in_addr_t inet_lnaof(struct in_addr in)
29 {
30         uint32_t h = in.s_addr;
31         if (h>>24 < 128) return h & 0xffffff;
32         if (h>>24 < 192) return h & 0xffff;
33         return h & 0xff;
34 }
35
36 in_addr_t inet_netof(struct in_addr in)
37 {
38         uint32_t h = in.s_addr;
39         if (h>>24 < 128) return h >> 24;
40         if (h>>24 < 192) return h >> 16;
41         return h >> 8;
42 }