svn commit: trunk/busybox: include networking

vda at busybox.net vda at busybox.net
Mon Apr 7 00:46:29 UTC 2008


Author: vda
Date: 2008-04-06 17:46:29 -0700 (Sun, 06 Apr 2008)
New Revision: 21658

Log:
ping: add -w, -W support (James Simmons <jsimmons AT infradead.org>)

function                                             old     new   delta
print_stats_and_exit                                   -     282    +282
sendping_tail                                        151     231     +80
packed_usage                                       23976   24054     +78
ping_main                                            401     412     +11
arm_ioctl                                             13      20      +7
pingstats                                            259       -    -259
------------------------------------------------------------------------------
(add/remove: 1/1 grow/shrink: 4/0 up/down: 458/-259)          Total: 199 bytes



Modified:
   trunk/busybox/include/usage.h
   trunk/busybox/networking/ping.c


Changeset:
Modified: trunk/busybox/include/usage.h
===================================================================
--- trunk/busybox/include/usage.h	2008-04-06 19:59:21 UTC (rev 21657)
+++ trunk/busybox/include/usage.h	2008-04-07 00:46:29 UTC (rev 21658)
@@ -2935,6 +2935,10 @@
      "\n	-c CNT		Send only CNT pings" \
      "\n	-s SIZE		Send SIZE data bytes in packets (default=56)" \
      "\n	-I iface/IP	Use interface or IP address as source" \
+     "\n	-W timeout	Seconds to wait for the first response (default:10)" \
+     "\n			(after all -c CNT packets are sent)" \
+     "\n	-w deadline	Seconds until ping exits (default:infinite)" \
+     "\n			(can exit earlier with -c CNT)" \
      "\n	-q		Quiet, only displays output at start" \
      "\n			and when finished" \
 

Modified: trunk/busybox/networking/ping.c
===================================================================
--- trunk/busybox/networking/ping.c	2008-04-06 19:59:21 UTC (rev 21657)
+++ trunk/busybox/networking/ping.c	2008-04-07 00:46:29 UTC (rev 21658)
@@ -224,15 +224,17 @@
 
 /* full(er) version */
 
-#define OPT_STRING ("qvc:s:I:4" USE_PING6("6"))
+#define OPT_STRING ("qvc:s:w:W:I:4" USE_PING6("6"))
 enum {
 	OPT_QUIET = 1 << 0,
 	OPT_VERBOSE = 1 << 1,
 	OPT_c = 1 << 2,
 	OPT_s = 1 << 3,
-	OPT_I = 1 << 4,
-	OPT_IPV4 = 1 << 5,
-	OPT_IPV6 = (1 << 6) * ENABLE_PING6,
+	OPT_w = 1 << 4,
+	OPT_W = 1 << 5,
+	OPT_I = 1 << 6,
+	OPT_IPV4 = 1 << 7,
+	OPT_IPV6 = (1 << 8) * ENABLE_PING6,
 };
 
 
@@ -246,6 +248,9 @@
 	uint16_t myid;
 	unsigned tmin, tmax; /* in us */
 	unsigned long long tsum; /* in us, sum of all times */
+	unsigned deadline;
+	unsigned timeout;
+	unsigned total_secs;
 	const char *hostname;
 	const char *dotted;
 	union {
@@ -271,6 +276,9 @@
 #define tmin         (G.tmin        )
 #define tmax         (G.tmax        )
 #define tsum         (G.tsum        )
+#define deadline     (G.deadline    )
+#define timeout      (G.timeout     )
+#define total_secs   (G.total_secs  )
 #define hostname     (G.hostname    )
 #define dotted       (G.dotted      )
 #define pingaddr     (G.pingaddr    )
@@ -280,6 +288,8 @@
 	if (sizeof(G) > COMMON_BUFSIZE) \
 		BUG_ping_globals_too_big(); \
 	pingsock = -1; \
+	datalen = DEFDATALEN; \
+	timeout = MAXWAIT; \
 	tmin = UINT_MAX; \
 } while (0)
 
@@ -292,7 +302,8 @@
 
 /**************************************************************************/
 
-static void pingstats(int junk ATTRIBUTE_UNUSED)
+static void print_stats_and_exit(int junk) ATTRIBUTE_NORETURN;
+static void print_stats_and_exit(int junk ATTRIBUTE_UNUSED)
 {
 	signal(SIGINT, SIG_IGN);
 
@@ -311,7 +322,8 @@
 			tavg / 1000, tavg % 1000,
 			tmax / 1000, tmax % 1000);
 	}
-	exit(nreceived == 0); /* (nreceived == 0) is true (1) -- 'failure' */
+	/* if condition is true, exit with 1 -- 'failure' */
+	exit(nreceived == 0 || (deadline && nreceived < pingcount));
 }
 
 static void sendping_tail(void (*sp)(int), const void *pkt, int size_pkt)
@@ -327,13 +339,30 @@
 	if (sz != size_pkt)
 		bb_error_msg_and_die(bb_msg_write_error);
 
-	signal(SIGALRM, sp);
-	if (pingcount == 0 || ntransmitted < pingcount) { /* schedule next in 1s */
+	if (pingcount == 0 || deadline || ntransmitted < pingcount) {
+		/* Didn't send all pings yet - schedule next in 1s */
+		signal(SIGALRM, sp);
+		if (deadline) {
+			total_secs += PINGINTERVAL;
+			if (total_secs >= deadline)
+				signal(SIGALRM, print_stats_and_exit);
+		}
 		alarm(PINGINTERVAL);
-	} else { /* done, wait for the last ping to come back */
-		/* todo, don't necessarily need to wait so long... */
-		signal(SIGALRM, pingstats);
-		alarm(MAXWAIT);
+	} else { /* -c NN, and all NN are sent (and no deadline) */
+		/* Wait for the last ping to come back.
+		 * -W timeout: wait for a response in seconds.
+		 * Affects only timeout in absense of any responses,
+		 * otherwise ping waits for two RTTs. */
+		unsigned expire = timeout;
+
+		if (nreceived) {
+			/* approx. 2*tmax, in seconds (2 RTT) */
+			expire = tmax / (512*1024);
+			if (expire == 0)
+				expire = 1;
+		}
+		signal(SIGALRM, print_stats_and_exit);
+		alarm(expire);
 	}
 }
 
@@ -549,7 +578,7 @@
 	sockopt = 48 * 1024; /* explain why 48k? */
 	setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, &sockopt, sizeof(sockopt));
 
-	signal(SIGINT, pingstats);
+	signal(SIGINT, print_stats_and_exit);
 
 	/* start the ping's going ... */
 	sendping4(0);
@@ -568,7 +597,7 @@
 			continue;
 		}
 		unpack4(packet, c, &from);
-		if (pingcount > 0 && nreceived >= pingcount)
+		if (pingcount && nreceived >= pingcount)
 			break;
 	}
 }
@@ -624,7 +653,7 @@
 	if (if_index)
 		pingaddr.sin6.sin6_scope_id = if_index;
 
-	signal(SIGINT, pingstats);
+	signal(SIGINT, print_stats_and_exit);
 
 	/* start the ping's going ... */
 	sendping6(0);
@@ -659,7 +688,7 @@
 			}
 		}
 		unpack6(packet, c, /*&from,*/ hoplimit);
-		if (pingcount > 0 && nreceived >= pingcount)
+		if (pingcount && nreceived >= pingcount)
 			break;
 	}
 }
@@ -691,11 +720,9 @@
 
 	INIT_G();
 
-	datalen = DEFDATALEN;
-
-	/* exactly one argument needed, -v and -q don't mix */
-	opt_complementary = "=1:q--v:v--q";
-	getopt32(argv, OPT_STRING, &opt_c, &opt_s, &opt_I);
+	/* exactly one argument needed; -v and -q don't mix; -w NUM, -W NUM */
+	opt_complementary = "=1:q--v:v--q:w+:W+";
+	getopt32(argv, OPT_STRING, &opt_c, &opt_s, &deadline, &timeout, &opt_I);
 	if (option_mask32 & OPT_c)
 		pingcount = xatoul(opt_c); // -c
 	if (option_mask32 & OPT_s)
@@ -726,8 +753,8 @@
 
 	dotted = xmalloc_sockaddr2dotted_noport(&lsa->u.sa);
 	ping(lsa);
-	pingstats(0);
-	return EXIT_SUCCESS;
+	print_stats_and_exit(0);
+	/*return EXIT_SUCCESS;*/
 }
 #endif /* FEATURE_FANCY_PING */
 




More information about the busybox-cvs mailing list