Hi, maybe someone else is also interested in this.

I was testing multicast routing (pimd) on a server today and needed a
very simple multicast generator.  Calling BusyBox "ping 225.1.2.3" sends
out a neat UDP multicast stream of one packet per second, simple enough
for my purposes.  However, for multicast ICMP_ECHO defaults to a TTL of
1, whereas unicast gets 64.  Hence this little patch.

Regards
 /Joachim

This patch adds -t TTL support to ping.  Please note, there is no boundary
checking for the TTL value, even though only 0-255 are supported.

Signed-off-by: Joachim Nilsson <[email protected]>
---
 include/usage.src.h |    1 +
 networking/ping.c   |   30 ++++++++++++++++++++----------
 2 files changed, 21 insertions(+), 10 deletions(-)

diff -ru busybox-1.17.3.orig/include/usage.src.h busybox-1.17.3/include/usage.src.h
--- busybox-1.17.3.orig/include/usage.src.h	2010-10-09 21:57:13.000000000 +0200
+++ busybox-1.17.3/include/usage.src.h	2010-11-20 23:54:37.471734001 +0100
@@ -3172,6 +3172,7 @@
      "\n	-4, -6		Force IP or IPv6 name resolution" \
      "\n	-c CNT		Send only CNT pings" \
      "\n	-s SIZE		Send SIZE data bytes in packets (default:56)" \
+     "\n	-t TTL		Set the IP Time to Live, (default:64, 1 for multicast)" \
      "\n	-I IFACE/IP	Use interface or IP address as source" \
      "\n	-W SEC		Seconds to wait for the first response (default:10)" \
      "\n			(after all -c CNT packets are sent)" \
diff -ru busybox-1.17.3.orig/networking/ping.c busybox-1.17.3/networking/ping.c
--- busybox-1.17.3.orig/networking/ping.c	2010-10-09 21:57:14.000000000 +0200
+++ busybox-1.17.3/networking/ping.c	2010-11-20 23:55:18.380784002 +0100
@@ -41,6 +41,7 @@
 
 enum {
 	DEFDATALEN = 56,
+	DEFMCTTL = 1,
 	MAXIPLEN = 60,
 	MAXICMPLEN = 76,
 	MAX_DUP_CHK = (8 * 128),
@@ -223,17 +224,18 @@
 
 /* Full(er) version */
 
-#define OPT_STRING ("qvc:s:w:W:I:4" IF_PING6("6"))
+#define OPT_STRING ("qvc:s:t:w:W:I:4" IF_PING6("6"))
 enum {
 	OPT_QUIET = 1 << 0,
 	OPT_VERBOSE = 1 << 1,
 	OPT_c = 1 << 2,
 	OPT_s = 1 << 3,
-	OPT_w = 1 << 4,
-	OPT_W = 1 << 5,
-	OPT_I = 1 << 6,
-	OPT_IPV4 = 1 << 7,
-	OPT_IPV6 = (1 << 8) * ENABLE_PING6,
+	OPT_t = 1 << 4,
+	OPT_w = 1 << 5,
+	OPT_W = 1 << 6,
+	OPT_I = 1 << 7,
+	OPT_IPV4 = 1 << 8,
+	OPT_IPV6 = (1 << 9) * ENABLE_PING6,
 };
 
 
@@ -244,6 +246,7 @@
 	len_and_sockaddr *source_lsa;
 	unsigned datalen;
 	unsigned pingcount; /* must be int-sized */
+	unsigned char mcttl; /* multicast ttl */
 	unsigned long ntransmitted, nreceived, nrepeats;
 	uint16_t myid;
 	unsigned tmin, tmax; /* in us */
@@ -275,6 +278,7 @@
 #define nreceived    (G.nreceived   )
 #define nrepeats     (G.nrepeats    )
 #define pingcount    (G.pingcount   )
+#define mcttl        (G.mcttl       )
 #define myid         (G.myid        )
 #define tmin         (G.tmin        )
 #define tmax         (G.tmax        )
@@ -292,6 +296,7 @@
 		BUG_ping_globals_too_big(); \
 	pingsock = -1; \
 	datalen = DEFDATALEN; \
+	mcttl = DEFMCTTL; \
 	timeout = MAXWAIT; \
 	tmin = UINT_MAX; \
 } while (0)
@@ -340,7 +345,7 @@
 	 * it doesn't matter */
 	sz = xsendto(pingsock, pkt, size_pkt, &pingaddr.sa, sizeof(pingaddr));
 	if (sz != size_pkt)
-		bb_error_msg_and_die(bb_msg_write_error);
+		bb_error_msg_and_die("%s", bb_msg_write_error);
 
 	if (pingcount == 0 || deadline || ntransmitted < pingcount) {
 		/* Didn't send all pings yet - schedule next in 1s */
@@ -586,6 +591,9 @@
 	sockopt = (datalen * 2) + 7 * 1024; /* giving it a bit of extra room */
 	setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, &sockopt, sizeof(sockopt));
 
+	if (mcttl > DEFMCTTL)
+		setsockopt(pingsock, IPPROTO_IP, IP_MULTICAST_TTL, &mcttl, sizeof(mcttl));
+
 	signal(SIGINT, print_stats_and_exit);
 
 	/* start the ping's going ... */
@@ -731,15 +739,17 @@
 static int common_ping_main(int opt, char **argv)
 {
 	len_and_sockaddr *lsa;
-	char *str_s;
+	char *str_s, *str_t;
 
 	INIT_G();
 
-	/* exactly one argument needed; -v and -q don't mix; -c NUM, -w NUM, -W NUM */
+	/* exactly one argument needed; -v and -q don't mix; -c NUM, -t NUM, -w NUM, -W NUM */
 	opt_complementary = "=1:q--v:v--q:c+:w+:W+";
-	opt |= getopt32(argv, OPT_STRING, &pingcount, &str_s, &deadline, &timeout, &str_I);
+	opt |= getopt32(argv, OPT_STRING, &pingcount, &str_s, &str_t, &deadline, &timeout, &str_I);
 	if (opt & OPT_s)
 		datalen = xatou16(str_s); // -s
+	if (opt & OPT_t)
+		mcttl = xatou16(str_t);   // -t
 	if (opt & OPT_I) { // -I
 		if_index = if_nametoindex(str_I);
 		if (!if_index) {

_______________________________________________
busybox mailing list
[email protected]
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to