[PATCH] networking: code shrink

Bartosz Golaszewski bartekgola at gmail.com
Mon Jul 15 20:21:47 UTC 2013


Fixes the following TODO:

  in_ether duplicated in network/{interface,ifconfig}.c

Moves in_ether to libbb/in_ether.c and makes interface.c call it instead
of ether_input.

Signed-off-by: Bartosz Golaszewski <bartekgola at gmail.com>
---
 TODO                   |    2 --
 include/libbb.h        |    1 +
 libbb/Kbuild.src       |    4 ++++
 libbb/in_ether.c       |   49 ++++++++++++++++++++++++++++++++++++++++++
 networking/ifconfig.c  |   43 -------------------------------------
 networking/interface.c |   55 +-----------------------------------------------
 6 files changed, 55 insertions(+), 99 deletions(-)
 create mode 100644 libbb/in_ether.c

diff --git a/TODO b/TODO
index b66a1c1..8d0850c 100644
--- a/TODO
+++ b/TODO
@@ -229,8 +229,6 @@ Minor stuff:
   See grep -r strtod
   Alot of duplication that wants cleanup.
 ---
-  in_ether duplicated in network/{interface,ifconfig}.c
----
   unify progress_meter. wget, flash_eraseall, pipe_progress, fbsplash, setfiles.
 ---
   support start-stop-daemon -d <chdir-path>
diff --git a/include/libbb.h b/include/libbb.h
index f22c125..a1cf9f0 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -1140,6 +1140,7 @@ struct hwtype {
 };
 extern smallint interface_opt_a;
 int display_interfaces(char *ifname) FAST_FUNC;
+int FAST_FUNC in_ether(const char *bufp, struct sockaddr *sap);
 #if ENABLE_FEATURE_HWIB
 int in_ib(const char *bufp, struct sockaddr *sap) FAST_FUNC;
 #else
diff --git a/libbb/Kbuild.src b/libbb/Kbuild.src
index c5d86c0..88fa023 100644
--- a/libbb/Kbuild.src
+++ b/libbb/Kbuild.src
@@ -133,6 +133,10 @@ lib-$(CONFIG_TCPSVD) += udp_io.o
 lib-$(CONFIG_UDPSVD) += udp_io.o
 lib-$(CONFIG_TRACEROUTE) += udp_io.o
 
+lib-$(CONFIG_IFCONFIG) += in_ether.o
+lib-$(CONFIG_IFENSLAVE) += in_ether.o
+lib-$(CONFIG_ARP) += in_ether.o
+
 lib-$(CONFIG_LOSETUP) += loop.o
 lib-$(CONFIG_FEATURE_MOUNT_LOOP) += loop.o
 
diff --git a/libbb/in_ether.c b/libbb/in_ether.c
new file mode 100644
index 0000000..fc0eb14
--- /dev/null
+++ b/libbb/in_ether.c
@@ -0,0 +1,49 @@
+/* vi: set sw=4 ts=4: */
+/*
+ * Utility routines.
+ */
+
+#include "libbb.h"
+#include <net/if_arp.h>
+#include <net/ethernet.h>
+
+/* Input an Ethernet address and convert to binary. */
+int FAST_FUNC in_ether(const char *bufp, struct sockaddr *sap)
+{
+	char *ptr;
+	int i, j;
+	unsigned char val;
+	unsigned char c;
+
+	sap->sa_family = ARPHRD_ETHER;
+	ptr = (char *) sap->sa_data;
+
+	i = 0;
+	do {
+		j = val = 0;
+
+		/* We might get a semicolon here - not required. */
+		if (i && (*bufp == ':')) {
+			bufp++;
+		}
+
+		do {
+			c = *bufp;
+			if (((unsigned char)(c - '0')) <= 9) {
+				c -= '0';
+			} else if ((unsigned char)((c|0x20) - 'a') <= 5) {
+				c = (unsigned char)((c|0x20) - 'a') + 10;
+			} else if (j && (c == ':' || c == 0)) {
+				break;
+			} else {
+				return -1;
+			}
+			++bufp;
+			val <<= 4;
+			val += c;
+		} while (++j < 2);
+		*ptr++ = val;
+	} while (++i < ETH_ALEN);
+
+	return *bufp; /* Error if we don't end at end of string. */
+}
diff --git a/networking/ifconfig.c b/networking/ifconfig.c
index 782374b..999305a 100644
--- a/networking/ifconfig.c
+++ b/networking/ifconfig.c
@@ -265,49 +265,6 @@ static const struct options OptArray[] = {
 	{ NULL,          0,             ARG_HOSTNAME,    (IFF_UP | IFF_RUNNING) }
 };
 
-#if ENABLE_FEATURE_IFCONFIG_HW
-/* Input an Ethernet address and convert to binary. */
-static int in_ether(const char *bufp, struct sockaddr *sap)
-{
-	char *ptr;
-	int i, j;
-	unsigned char val;
-	unsigned char c;
-
-	sap->sa_family = ARPHRD_ETHER;
-	ptr = (char *) sap->sa_data;
-
-	i = 0;
-	do {
-		j = val = 0;
-
-		/* We might get a semicolon here - not required. */
-		if (i && (*bufp == ':')) {
-			bufp++;
-		}
-
-		do {
-			c = *bufp;
-			if (((unsigned char)(c - '0')) <= 9) {
-				c -= '0';
-			} else if ((unsigned char)((c|0x20) - 'a') <= 5) {
-				c = (unsigned char)((c|0x20) - 'a') + 10;
-			} else if (j && (c == ':' || c == 0)) {
-				break;
-			} else {
-				return -1;
-			}
-			++bufp;
-			val <<= 4;
-			val += c;
-		} while (++j < 2);
-		*ptr++ = val;
-	} while (++i < ETH_ALEN);
-
-	return *bufp; /* Error if we don't end at end of string. */
-}
-#endif
-
 int ifconfig_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int ifconfig_main(int argc UNUSED_PARAM, char **argv)
 {
diff --git a/networking/interface.c b/networking/interface.c
index 9ae8b3f..3dc5b36 100644
--- a/networking/interface.c
+++ b/networking/interface.c
@@ -722,68 +722,15 @@ static char* FAST_FUNC ether_print(unsigned char *ptr)
 	return buff;
 }
 
-static int FAST_FUNC ether_input(const char *bufp, struct sockaddr *sap);
-
 static const struct hwtype ether_hwtype = {
 	.name  = "ether",
 	.title = "Ethernet",
 	.type  = ARPHRD_ETHER,
 	.alen  = ETH_ALEN,
 	.print = ether_print,
-	.input = ether_input
+	.input = in_ether
 };
 
-static unsigned hexchar2int(char c)
-{
-	if (isdigit(c))
-		return c - '0';
-	c &= ~0x20; /* a -> A */
-	if ((unsigned)(c - 'A') <= 5)
-		return c - ('A' - 10);
-	return ~0U;
-}
-
-/* Input an Ethernet address and convert to binary. */
-static int FAST_FUNC ether_input(const char *bufp, struct sockaddr *sap)
-{
-	unsigned char *ptr;
-	char c;
-	int i;
-	unsigned val;
-
-	sap->sa_family = ether_hwtype.type;
-	ptr = (unsigned char*) sap->sa_data;
-
-	i = 0;
-	while ((*bufp != '\0') && (i < ETH_ALEN)) {
-		val = hexchar2int(*bufp++) * 0x10;
-		if (val > 0xff) {
-			errno = EINVAL;
-			return -1;
-		}
-		c = *bufp;
-		if (c == ':' || c == 0)
-			val >>= 4;
-		else {
-			val |= hexchar2int(c);
-			if (val > 0xff) {
-				errno = EINVAL;
-				return -1;
-			}
-		}
-		if (c != 0)
-			bufp++;
-		*ptr++ = (unsigned char) val;
-		i++;
-
-		/* We might get a semicolon here - not required. */
-		if (*bufp == ':') {
-			bufp++;
-		}
-	}
-	return 0;
-}
-
 static const struct hwtype ppp_hwtype = {
 	.name =		"ppp",
 	.title =	"Point-to-Point Protocol",
-- 
1.7.10.4



More information about the busybox mailing list