[BusyBox] [ PATCH ] ipcalc applet

Jordan Crouse jordanc at censoft.com
Wed Oct 9 10:04:03 UTC 2002


Continuing with the "no value added" patch parade - here is a patch that adds a Busybox applet for the 'ipcalc' utility (usually distributed as part of the Redhat distro).  I use it on thin clients to verify network information which is entered by users.  This applet saves me about 20k on our final ROM image, so it is useful for me, but I don't know how everyone else feels.

jordanc:busybox.devel >size ipcalc.o
   text	   data	    bss	    dec	    hex	filename
    556	     64	      0	    620	    26c	ipcalc.o

Anyway, enjoy,
Jordan

diff -urN --exclude CVS --exclude docs busybox.stable/Config.h busybox.devel/Config.h
--- busybox.stable/Config.h	Tue Sep 17 16:04:28 2002
+++ busybox.devel/Config.h	Wed Oct  9 09:55:02 2002
@@ -55,6 +55,7 @@
 //#define BB_HUSH
 #define BB_ID
 //#define BB_IFCONFIG
+//#define BB_IPCALC
 #define BB_INIT
 //#define BB_INSMOD
 #define BB_KILL
diff -urN --exclude CVS --exclude docs busybox.stable/applets.h busybox.devel/applets.h
--- busybox.stable/applets.h	Tue Sep 17 16:04:28 2002
+++ busybox.devel/applets.h	Thu Oct  3 10:49:08 2002
@@ -203,6 +203,9 @@
 #ifdef BB_INSMOD
 	APPLET(insmod, insmod_main, _BB_DIR_SBIN)
 #endif
+#ifdef BB_IPCALC
+	APPLET(ipcalc, ipcalc_main, _BB_DIR_BIN)
+#endif
 #ifdef BB_KILL
 	APPLET(kill, kill_main, _BB_DIR_BIN)
 #endif
diff -urN --exclude CVS --exclude docs busybox.stable/ipcalc.c busybox.devel/ipcalc.c
--- busybox.stable/ipcalc.c	Wed Dec 31 17:00:00 1969
+++ busybox.devel/ipcalc.c	Thu Oct  3 10:48:13 2002
@@ -0,0 +1,102 @@
+/* vi: set sw=4 ts=4: */
+/*
+ * Mini ipcalc implementation for busybox
+ *
+ * By Jordan Crouse (jordan at cosmicpenguin.net)
+ *
+ * This is a complete reimplentation of the ipcalc program
+ * from Redhat.  I didn't look at their source code, but there
+ * is no denying that this is a loving reimplementation
+ */
+
+#include <stdio.h>
+#include <getopt.h>
+#include <arpa/inet.h>
+
+#include "busybox.h"
+
+static unsigned long get_netmask(unsigned long ipaddr) {
+		if (ipaddr & 0xC0) return 0x00FFFFFF; /* Class C */
+		if (ipaddr & 0x10) return 0x0000FFFF; /* Class B */
+		return 0x000000FF;  /* Class A */
+}
+
+#define NETMASK   0x01
+#define BROADCAST 0x02
+#define NETWORK   0x04
+
+int ipcalc_main(int argc, char **argv) {
+
+  unsigned char mode = 0;
+
+  unsigned long netmask = 0;
+  unsigned long broadcast = 0;
+  unsigned long network = 0;
+  unsigned long ipaddr = 0;
+  
+  int opt = 0;
+
+#define LONG_HEADER 1
+  
+  struct option long_options[] = {
+    { "netmask", 0, NULL, 'n' },
+    { "broadcast", 0, NULL, 'b' },
+    { "network", 0, NULL, 'w' },
+    { 0, 0, 0 ,0 }
+  };
+  
+  while ((opt = getopt_long(argc, argv, "nbw", long_options, 0)) != EOF) {
+    if (opt == 'n') mode |= NETMASK;
+     else if (opt == 'b') mode |= BROADCAST;
+    else if (opt == 'w') mode |= NETWORK;
+    else {
+			show_usage();
+	}
+  }
+  
+  if (mode & (BROADCAST | NETWORK)) {
+		  if (argc - optind > 2) show_usage();
+  }
+  else {
+		  if (argc - optind != 1) show_usage();
+   }
+  
+  ipaddr = inet_addr(argv[optind]);
+  if (argc - optind == 2) netmask = inet_addr(argv[optind + 1]);
+  
+  if (mode & NETMASK || !netmask) netmask = get_netmask(ipaddr);
+
+  for(opt = 0; opt < 4; opt++) {
+    unsigned char ip = (ipaddr >> (opt * 8)) & 0xFF;
+    unsigned char nm = (netmask >> (opt * 8)) & 0xFF;
+
+    if (mode & NETWORK) 
+			network |= (ip & nm) << (opt * 8);
+    
+    if (mode & BROADCAST) 
+			broadcast |= ((ip | (~nm & 0xFF)) << (opt * 8));
+  }
+
+  if (mode & NETMASK) 
+		  printf("NETMASK=%s\n", inet_ntoa((*(struct in_addr *) &netmask)));
+
+  if (mode & BROADCAST) 
+		  printf("BROADCAST=%s\n", inet_ntoa((*(struct in_addr *) &broadcast)));
+
+  if (mode & NETWORK) 
+		  printf("NETWORK=%s\n", inet_ntoa((*(struct in_addr *) &network)));
+  
+  return 0;
+}
+      
+/*
+Local Variables:
+c-file-style: "linux"
+c-basic-offset: 4
+tab-width: 4
+End:
+*/
+
+	 
+
+
diff -urN --exclude CVS --exclude docs busybox.stable/usage.h busybox.devel/usage.h
--- busybox.stable/usage.h	Mon Sep 30 14:59:58 2002
+++ busybox.devel/usage.h	Thu Oct  3 10:48:48 2002
@@ -818,6 +818,16 @@
 	"\t-L\tLock to prevent simultaneous loads of a module\n" \
 	"\t-x\tdo not export externs"
 
+#define ipcalc_trivial_usage \
+        "[-broadcast] [-netmask] [-network] ipaddr <netmask>"
+
+#define ipcalc_full_usage \
+        "Calculate IP network settings from a IP address\n\n" \
+        "Options:\n" \
+        "\t-broadcast\tDisplay calculated broadcast address.\n" \
+        "\t-netmask\tDisplay default netmask for IP.\n" \
+        "\t-network\tDisplay calculated network address." 
+
 #define kill_trivial_usage \
 	"[-signal] process-id [process-id ...]"
 #define kill_full_usage \





More information about the busybox mailing list