de5b75c1ae4deb23fa0fd08b55904015ff40e8c9
[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(in_addr_t n, in_addr_t h)
20 {
21         if (n < 256) h |= n<<24;
22         else if (n < 65536) h |= n<<16;
23         else h |= n<<8;
24         return (struct in_addr){ h };
25 }
26
27 in_addr_t inet_lnaof(struct in_addr in)
28 {
29         uint32_t h = in.s_addr;
30         if (h>>24 < 128) return h & 0xffffff;
31         if (h>>24 < 192) return h & 0xffff;
32         return h & 0xff;
33 }
34
35 in_addr_t inet_netof(struct in_addr in)
36 {
37         uint32_t h = in.s_addr;
38         if (h>>24 < 128) return h >> 24;
39         if (h>>24 < 192) return h >> 16;
40         return h >> 8;
41 }