summaryrefslogtreecommitdiff
path: root/source/n/net-tools/ipmask.c
diff options
context:
space:
mode:
authorPatrick J Volkerding <volkerdi@slackware.com>2009-08-26 10:00:38 -0500
committerEric Hameleers <alien@slackware.com>2018-05-31 22:41:17 +0200
commit5a12e7c134274dba706667107d10d231517d3e05 (patch)
tree55718d5acb710fde798d9f38d0bbaf594ed4b296 /source/n/net-tools/ipmask.c
downloadcurrent-5a12e7c134274dba706667107d10d231517d3e05.tar.gz
Slackware 13.0slackware-13.0
Wed Aug 26 10:00:38 CDT 2009 Slackware 13.0 x86_64 is released as stable! Thanks to everyone who helped make this release possible -- see the RELEASE_NOTES for the credits. The ISOs are off to the replicator. This time it will be a 6 CD-ROM 32-bit set and a dual-sided 32-bit/64-bit x86/x86_64 DVD. We're taking pre-orders now at store.slackware.com. Please consider picking up a copy to help support the project. Once again, thanks to the entire Slackware community for all the help testing and fixing things and offering suggestions during this development cycle. As always, have fun and enjoy! -P.
Diffstat (limited to 'source/n/net-tools/ipmask.c')
-rw-r--r--source/n/net-tools/ipmask.c87
1 files changed, 87 insertions, 0 deletions
diff --git a/source/n/net-tools/ipmask.c b/source/n/net-tools/ipmask.c
new file mode 100644
index 00000000..6be37c31
--- /dev/null
+++ b/source/n/net-tools/ipmask.c
@@ -0,0 +1,87 @@
+/* ipmask.c
+ *
+ * Given argv[1] as a decimal netmask and argv[2] as a decimal IP address,
+ * print the resulting broadcast and network addresses to stdout. This is
+ * potentially useful in scripts which need the broadcast address and the
+ * network address but want to ask the user as few questions as possible.
+ *
+ * Copyright 1994 by David Niemi. Written in about 30 minutes on 13 Aug.
+ * The author places no restrictions on the use of this program, provided
+ * that this copyright is preserved in any derived source code.
+ *
+ * Typical compilation command for Linux:
+ * cc ipmask.c -Wall -O -m486 -N -o ipmask -s
+ */
+
+#define MYNAME "ipmask"
+
+#include <stdio.h>
+
+void Usage(void) {
+ fprintf (stderr,
+ "USAGE: %s <decimal netmask> <decimal IP address>\n",
+ MYNAME);
+}
+
+int main(int argc, char *argv[])
+{
+unsigned long netmask, ipaddr, netaddr, broadcast;
+int in[4], j;
+unsigned char bc[4],na[4];
+
+ if (3 != argc) {
+ Usage();
+ exit(1);
+ }
+
+ /* Check netmask */
+ if (4 != sscanf(argv[1],"%d.%d.%d.%d", &in[0],&in[1],&in[2],&in[3])) {
+ fprintf (stderr,"Invalid netmask \"%s\".\n", argv[1]);
+ Usage();
+ exit(1);
+ }
+ for (j=0; j<4; ++j) {
+ if (in[j]<0 || in[j]>255) {
+ fprintf (stderr,
+ "Invalid octet %d in netmask \"%s\".\n",
+ j+1, argv[1]);
+ Usage();
+ exit(1);
+ }
+ }
+ netmask = in[3] + 256 * (in[2] + 256 * (in[1] + 256 * in[0]));
+
+ /* Check IP address */
+ if (4 != sscanf(argv[2],"%d.%d.%d.%d", &in[0],&in[1],&in[2],&in[3])) {
+ fprintf (stderr,"Invalid IP address \"%s\".\n", argv[2]);
+ Usage();
+ exit(1);
+ }
+ for (j=0; j<4; ++j) {
+ if (in[j]<0 || in[j]>255) {
+ fprintf (stderr,
+ "Invalid octet %d in IP address \"%s\".\n",
+ j+1, argv[1]);
+ Usage();
+ exit(1);
+ }
+ }
+ ipaddr = in[3] + 256 * (in[2] + 256 * (in[1] + 256 * in[0]));
+
+ broadcast = ipaddr | (~ netmask);
+ bc[0] = broadcast / 256 / 256 / 256;
+ bc[1] = (broadcast / 256 / 256) % 256;
+ bc[2] = (broadcast / 256) % 256;
+ bc[3] = broadcast % 256;
+
+ netaddr = ipaddr & netmask;
+ na[0] = netaddr / 256 / 256 / 256;
+ na[1] = (netaddr / 256 / 256) % 256;
+ na[2] = (netaddr / 256) % 256;
+ na[3] = netaddr % 256;
+
+ printf ("%d.%d.%d.%d %d.%d.%d.%d\n",
+ bc[0], bc[1], bc[2], bc[3], na[0], na[1], na[2], na[3]);
+
+ exit(0);
+}