[patch] shrink arping a bit

Bernhard Fischer rep.nop at aon.at
Thu Mar 30 16:57:08 UTC 2006


Hi,

   text    data     bss     dec     hex filename
   3627       4     120    3751     ea7 networking/arping.o.oorig
   3592       4     120    3716     e84 networking/arping.o

The attached patchlet doesn't use bb_getopt_ulflags yet and i don't plan
to convert it during the next couple of days, so if anyone wants to
help..

Apparently we have yet another arping() impl in udhcpd which could be
dropped unless udhcp is built in standalone-mode

It's irritating that our arping wants the interface specified as capital
'I' while the big version which ships with my desktop uses a lowercase
'i'. I'd like to change ours to lowercase too.

Objections or comments?
-------------- next part --------------
Index: networking/arping.c
===================================================================
--- networking/arping.c	(revision 14706)
+++ networking/arping.c	(working copy)
@@ -1,10 +1,8 @@
+/* vi:set ts=4:*/
 /*
  * arping.c - Ping hosts by ARP requests/replies
  *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version
- * 2 of the License, or (at your option) any later version.
+ * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  *
  * Author:	Alexey Kuznetsov <kuznet at ms2.inr.ac.ru>
  * Busybox port: Nick Fedchik <nick at fedchik.org.ua>
@@ -26,8 +24,6 @@
 
 #include "busybox.h"
 
-#define APPLET_NAME "arping"
-
 static struct in_addr src;
 static struct in_addr dst;
 static struct sockaddr_ll me;
@@ -69,7 +65,7 @@
 {
 	int err;
 	struct timeval now;
-	unsigned char buf[256];
+	RESERVE_CONFIG_UBUFFER(buf, 256);
 	struct arphdr *ah = (struct arphdr *) buf;
 	unsigned char *p = (unsigned char *) (ah + 1);
 
@@ -103,6 +99,7 @@
 		if (!unicasting)
 			brd_sent++;
 	}
+	RELEASE_CONFIG_BUFFER(buf);
 	return err;
 }
 
@@ -153,13 +150,10 @@
 
 static int recv_pack(unsigned char *buf, int len, struct sockaddr_ll *FROM)
 {
-	struct timeval tv;
 	struct arphdr *ah = (struct arphdr *) buf;
 	unsigned char *p = (unsigned char *) (ah + 1);
 	struct in_addr src_ip, dst_ip;
 
-	gettimeofday(&tv, NULL);
-
 	/* Filter out wild packets */
 	if (FROM->sll_pkttype != PACKET_HOST &&
 		FROM->sll_pkttype != PACKET_BROADCAST &&
@@ -217,7 +211,10 @@
 	}
 	if (!quiet) {
 		int s_printed = 0;
+		struct timeval tv;
 
+		gettimeofday(&tv, NULL);
+
 		printf("%s ",
 			   FROM->sll_pkttype == PACKET_HOST ? "Unicast" : "Broadcast");
 		printf("%s from ",
@@ -234,6 +231,7 @@
 			printf("[%s]",
 				   ether_ntoa((struct ether_addr *) p + ah->ar_hln + 4));
 		}
+
 		if (last.tv_sec) {
 			long usecs = (tv.tv_sec - last.tv_sec) * 1000000 +
 				tv.tv_usec - last.tv_usec;
@@ -274,6 +272,7 @@
 	socket_errno = errno;
 
 	setuid(uid);
+	bb_default_error_retval = 2;
 
 	while ((ch = getopt(argc, argv, "h?bfDUAqc:w:s:I:")) != EOF) {
 		switch (ch) {
@@ -304,9 +303,8 @@
 			if (optarg == NULL)
 				bb_show_usage();
 			if (bb_strlen(optarg) > IF_NAMESIZE) {
-				bb_error_msg("Interface name `%s' must be less than %d", optarg,
-						  IF_NAMESIZE);
-				exit(2);
+				bb_error_msg_and_die("Interface name `%s' must be less than %d",
+								optarg, IF_NAMESIZE);
 			}
 			device = optarg;
 			break;
@@ -330,9 +328,8 @@
 
 	target = *argv;
 
-
 	if (s < 0) {
-		bb_error_msg("socket");
+		bb_perror_msg("socket");
 		exit(socket_errno);
 	}
 
@@ -342,18 +339,15 @@
 		memset(&ifr, 0, sizeof(ifr));
 		strncpy(ifr.ifr_name, device, IFNAMSIZ - 1);
 		if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
-			bb_error_msg("Interface %s not found", device);
-			exit(2);
+			bb_error_msg_and_die("Interface %s not found", device);
 		}
 		ifindex = ifr.ifr_ifindex;
 
 		if (ioctl(s, SIOCGIFFLAGS, (char *) &ifr)) {
-			bb_error_msg("SIOCGIFFLAGS");
-			exit(2);
+			bb_error_msg_and_die("SIOCGIFFLAGS");
 		}
 		if (!(ifr.ifr_flags & IFF_UP)) {
-			bb_error_msg("Interface %s is down", device);
-			exit(2);
+			bb_error_msg_and_die("Interface %s is down", device);
 		}
 		if (ifr.ifr_flags & (IFF_NOARP | IFF_LOOPBACK)) {
 			bb_error_msg("Interface %s is not ARPable", device);
@@ -366,15 +360,13 @@
 
 		hp = gethostbyname2(target, AF_INET);
 		if (!hp) {
-			bb_error_msg("invalid or unknown target %s", target);
-			exit(2);
+			bb_error_msg_and_die("invalid or unknown target %s", target);
 		}
 		memcpy(&dst, hp->h_addr, 4);
 	}
 
 	if (source && !inet_aton(source, &src)) {
-		bb_error_msg("invalid source address %s", source);
-		exit(2);
+		bb_error_msg_and_die("invalid source address %s", source);
 	}
 
 	if (!dad && unsolicited && src.s_addr == 0)
@@ -385,8 +377,7 @@
 		int probe_fd = socket(AF_INET, SOCK_DGRAM, 0);
 
 		if (probe_fd < 0) {
-			bb_error_msg("socket");
-			exit(2);
+			bb_error_msg_and_die("socket");
 		}
 		if (device) {
 			if (setsockopt
@@ -399,8 +390,7 @@
 		if (src.s_addr) {
 			saddr.sin_addr = src;
 			if (bind(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr)) == -1) {
-				bb_error_msg("bind");
-				exit(2);
+				bb_error_msg_and_die("bind");
 			}
 		} else if (!dad) {
 			int on = 1;
@@ -412,16 +402,14 @@
 			if (setsockopt
 				(probe_fd, SOL_SOCKET, SO_DONTROUTE, (char *) &on,
 				 sizeof(on)) == -1)
-				perror("WARNING: setsockopt(SO_DONTROUTE)");
+				bb_perror_msg("WARNING: setsockopt(SO_DONTROUTE)");
 			if (connect(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr))
 				== -1) {
-				bb_error_msg("connect");
-				exit(2);
+				bb_error_msg_and_die("connect");
 			}
 			if (getsockname(probe_fd, (struct sockaddr *) &saddr, &alen) ==
 				-1) {
-				bb_error_msg("getsockname");
-				exit(2);
+				bb_error_msg_and_die("getsockname");
 			}
 			src = saddr.sin_addr;
 		}
@@ -432,16 +420,14 @@
 	me.sll_ifindex = ifindex;
 	me.sll_protocol = htons(ETH_P_ARP);
 	if (bind(s, (struct sockaddr *) &me, sizeof(me)) == -1) {
-		bb_error_msg("bind");
-		exit(2);
+		bb_error_msg_and_die("bind");
 	}
 
 	{
 		socklen_t alen = sizeof(me);
 
 		if (getsockname(s, (struct sockaddr *) &me, &alen) == -1) {
-			bb_error_msg("getsockname");
-			exit(2);
+			bb_error_msg_and_die("getsockname");
 		}
 	}
 	if (me.sll_halen == 0) {
@@ -458,8 +444,7 @@
 	}
 
 	if (!src.s_addr && !dad) {
-		bb_error_msg("no src address in the non-DAD mode");
-		exit(2);
+		bb_error_msg_and_die("no src address in the non-DAD mode");
 	}
 
 	{


More information about the busybox mailing list