From 4fa9ee612c2c06f34f1c44265334f86fcbf24cf5 Mon Sep 17 00:00:00 2001
From: Erik Troan <ewt@redhat.com>
Date: Tue, 10 Mar 1998 15:07:31 +0000
Subject: added ipcalc

---
 src/Makefile |  5 ++++-
 src/ipcalc.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 73 insertions(+), 1 deletion(-)
 create mode 100644 src/ipcalc.c

(limited to 'src')

diff --git a/src/Makefile b/src/Makefile
index b3324ef6..e16e7b37 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -1,6 +1,6 @@
 CFLAGS+=-Wall
 
-PROGS=usernetctl doexec netreport testd usleep
+PROGS=usernetctl doexec netreport testd usleep ipcalc
 
 all:	$(PROGS)
 
@@ -13,6 +13,7 @@ install:
 	install -o 0 -g 0 -s -m 755 usleep $(ROOT)/bin/usleep
 	install -o 0 -g 0 -s -m 4755 usernetctl $(ROOT)/usr/sbin/usernetctl
 	install -o 0 -g 0 -s -m 2755 netreport $(ROOT)/sbin/netreport
+	install -o 0 -g 0 -s -m 2755 ipcalc $(ROOT)/sbin/ipcalc
 	install -m 644 doexec.1 $(ROOT)/usr/man/man1
 	install -m 644 netreport.1 $(ROOT)/usr/man/man1
 	install -m 644 usleep.1 $(ROOT)/usr/man/man1
@@ -24,3 +25,5 @@ install-test:
 	install -o 0 -g 0 -s -m 755 testd $(ROOT)/usr/sbin/testd
 	install -o 0 -g 0 -m 755 testdinit $(ROOT)/etc/rc.d/init.d/testd
 
+ipcalc: ipcalc.o
+	$(CC) $(LDFLAGS) -o $@ $< -lpopt
diff --git a/src/ipcalc.c b/src/ipcalc.c
new file mode 100644
index 00000000..5640d8c3
--- /dev/null
+++ b/src/ipcalc.c
@@ -0,0 +1,69 @@
+#include <popt.h>
+#include <stdio.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+typedef unsigned int int32;
+
+int main(int argc, char ** argv) {
+    int showBroadcast = 0, showNetwork = 0;
+    int rc;
+    poptContext optCon;
+    char * ipStr, * netmaskStr, * chptr;
+    int32 ip, netmask, network, broadcast;
+    struct poptOption optionsTable[] = {
+	    { "broadcast", '\0', 0, &showBroadcast, 0 },
+	    { "network", '\0', 0, &showNetwork, 0 },
+	    { NULL, '\0', 0, 0, 0 },
+    };
+
+    optCon = poptGetContext("ipcalc", argc, argv, optionsTable,0);
+    poptReadDefaultConfig(optCon, 1);
+
+    if ((rc = poptGetNextOpt(optCon)) < -1) {
+	fprintf(stderr, "ipcalc: bad argument %s: %s\n", 
+		poptBadOption(optCon, POPT_BADOPTION_NOALIAS), 
+		poptStrerror(rc));
+	return 1;
+    }
+
+    if (!(ipStr = poptGetArg(optCon))) {
+	fprintf(stderr, "ipcalc: ip address expected\n");
+	return 1;
+    }
+
+    if (!(netmaskStr = poptGetArg(optCon))) {
+	fprintf(stderr, "ipcalc: netmask expected\n");
+	return 1;
+    }
+
+    if ((chptr = poptGetArg(optCon))) {
+	fprintf(stderr, "ipcalc: unexpected argument: %s\n", chptr);
+	return 1;
+    }
+
+    poptFreeContext(optCon);
+
+    if (!inet_aton(ipStr, (struct in_addr *) &ip)) {
+	fprintf(stderr, "ipcalc: bad ip address: %s\n", ipStr);
+	return 1;
+    }
+
+    if (!inet_aton(netmaskStr, (struct in_addr *) &netmask)) {
+	fprintf(stderr, "ipcalc: bad netmask: %s\n", netmaskStr);
+	return 1;
+    }
+
+    if (showBroadcast) {
+	broadcast = (ip & netmask) | ~netmask;
+	printf("BROADCAST=%s\n", inet_ntoa(*((struct in_addr *) &broadcast)));
+    }
+
+    if (showNetwork) {
+	network = ip & netmask;
+	printf("NETWORK=%s\n", inet_ntoa(*((struct in_addr *) &network)));
+    }
+
+    return 0;
+}
-- 
cgit v1.2.1