about summary refs log tree commit diff
path: root/src/network
diff options
context:
space:
mode:
authorRich Felker <dalias@aerifal.cx>2013-06-25 21:35:49 -0400
committerRich Felker <dalias@aerifal.cx>2013-06-25 21:35:49 -0400
commite40f48a421a9176e3e298b5bac75f0355b219e58 (patch)
tree23255d3b92b410810a16eb5fd5853e3fe62bf05e /src/network
parent83966b369d2fa105a58a35830f75662e18273965 (diff)
downloadmusl-e40f48a421a9176e3e298b5bac75f0355b219e58.tar.gz
musl-e40f48a421a9176e3e298b5bac75f0355b219e58.tar.xz
musl-e40f48a421a9176e3e298b5bac75f0355b219e58.zip
implement inet_lnaof, inet_netof, and inet_makeaddr
also move all legacy inet_* functions into a single file to avoid
wasting object file and compile time overhead on them.

the added functions are legacy interfaces for working with classful
ipv4 network addresses. they have no modern usefulness whatsoever, but
some programs unconditionally use them anyway, and they're tiny.
Diffstat (limited to 'src/network')
-rw-r--r--src/network/inet_addr.c11
-rw-r--r--src/network/inet_aton.c7
-rw-r--r--src/network/inet_legacy.c55
-rw-r--r--src/network/inet_network.c11
-rw-r--r--src/network/inet_ntoa.c10
5 files changed, 55 insertions, 39 deletions
diff --git a/src/network/inet_addr.c b/src/network/inet_addr.c
deleted file mode 100644
index 84137281..00000000
--- a/src/network/inet_addr.c
+++ /dev/null
@@ -1,11 +0,0 @@
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#include "__dns.h"
-
-in_addr_t inet_addr(const char *p)
-{
-	struct sockaddr_in sin;
-	if (__ipparse(&sin, AF_INET, p)) return -1;
-	return sin.sin_addr.s_addr;
-}
diff --git a/src/network/inet_aton.c b/src/network/inet_aton.c
deleted file mode 100644
index ea4ee165..00000000
--- a/src/network/inet_aton.c
+++ /dev/null
@@ -1,7 +0,0 @@
-#include <sys/socket.h>
-#include <arpa/inet.h>
-
-int inet_aton(const char *cp, struct in_addr *inp)
-{
-	return inet_pton(AF_INET, cp, (void *)inp) > 0;
-}
diff --git a/src/network/inet_legacy.c b/src/network/inet_legacy.c
new file mode 100644
index 00000000..e802557b
--- /dev/null
+++ b/src/network/inet_legacy.c
@@ -0,0 +1,55 @@
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <stdio.h>
+#include "__dns.h"
+
+in_addr_t inet_addr(const char *p)
+{
+	struct sockaddr_in sin;
+	if (__ipparse(&sin, AF_INET, p)) return -1;
+	return sin.sin_addr.s_addr;
+}
+
+in_addr_t inet_network(const char *p)
+{
+	return ntohl(inet_addr(p));
+}
+
+int inet_aton(const char *cp, struct in_addr *inp)
+{
+	return inet_pton(AF_INET, cp, (void *)inp) > 0;
+}
+
+char *inet_ntoa(struct in_addr in)
+{
+	static char buf[16];
+	unsigned char *a = (void *)&in;
+	snprintf(buf, sizeof buf, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
+	return buf;
+}
+
+struct in_addr inet_makeaddr(int net, int host)
+{
+	uint32_t n = net, h = host;
+	if (n < 256) h |= n<<24;
+	else if (n < 65536) h |= n<<16;
+	else h |= n<<8;
+	return (struct in_addr){ h };
+}
+
+in_addr_t inet_lnaof(struct in_addr in)
+{
+	uint32_t h = in.s_addr;
+	if (h>>24 < 128) return h & 0xffffff;
+	if (h>>24 < 192) return h & 0xffff;
+	return h & 0xff;
+}
+
+in_addr_t inet_netof(struct in_addr in)
+{
+	uint32_t h = in.s_addr;
+	if (h>>24 < 128) return h >> 24;
+	if (h>>24 < 192) return h >> 16;
+	return h >> 8;
+}
diff --git a/src/network/inet_network.c b/src/network/inet_network.c
deleted file mode 100644
index ae60d7f6..00000000
--- a/src/network/inet_network.c
+++ /dev/null
@@ -1,11 +0,0 @@
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#include "__dns.h"
-
-in_addr_t inet_network(const char *p)
-{
-        struct sockaddr_in sin;
-        if (__ipparse(&sin, AF_INET, p)) return -1;
-        return ntohl(sin.sin_addr.s_addr);
-}
diff --git a/src/network/inet_ntoa.c b/src/network/inet_ntoa.c
deleted file mode 100644
index 71411e0b..00000000
--- a/src/network/inet_ntoa.c
+++ /dev/null
@@ -1,10 +0,0 @@
-#include <arpa/inet.h>
-#include <stdio.h>
-
-char *inet_ntoa(struct in_addr in)
-{
-	static char buf[16];
-	unsigned char *a = (void *)&in;
-	snprintf(buf, sizeof buf, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
-	return buf;
-}