[PATCH] ping: add -p to specify data pattern

Florian Fainelli florian at alphacore.org
Tue Jun 17 00:16:25 UTC 2014


From: Florian Fainelli <f.fainelli at gmail.com>

iputils-ping supports a feature allowing to change the ping payload
pattern. This is useful as a quick check for verifying that nothing is
corrupting data.

bloat-o-metter repots the following delta:

function                                             old     new   delta
.rodata                                           150675  150753     +78
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/0 up/down: 78/0)               Total: 78
bytes

Signed-off-by: Florian Fainelli <f.fainelli at gmail.com>
---
 networking/ping.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

diff --git a/networking/ping.c b/networking/ping.c
index 5e4771f5a03f..55f1171473cd 100644
--- a/networking/ping.c
+++ b/networking/ping.c
@@ -102,6 +102,7 @@
 //usage:     "\n			(can exit earlier with -c CNT)"
 //usage:     "\n	-q		Quiet, only displays output at start"
 //usage:     "\n			and when finished"
+//usage:     "\n	-p		Pattern to use for the ping payload (default: 0x00)"
 //usage:
 //usage:# define ping6_trivial_usage
 //usage:       "[OPTIONS] HOST"
@@ -112,6 +113,7 @@
 //usage:     "\n	-I IFACE/IP	Use interface or IP address as source"
 //usage:     "\n	-q		Quiet, only displays output at start"
 //usage:     "\n			and when finished"
+//usage:     "\n	-p		Pattern to use for the ping payload (default: 0x00)"
 //usage:
 //usage:#endif
 //usage:
@@ -330,7 +332,7 @@ static int common_ping_main(sa_family_t af, char **argv)
 
 /* Full(er) version */
 
-#define OPT_STRING ("qvc:s:t:w:W:I:n4" IF_PING6("6"))
+#define OPT_STRING ("qvc:s:t:w:W:I:n4" IF_PING6("6") "p:")
 enum {
 	OPT_QUIET = 1 << 0,
 	OPT_VERBOSE = 1 << 1,
@@ -343,6 +345,7 @@ enum {
 	/*OPT_n = 1 << 8, - ignored */
 	OPT_IPV4 = 1 << 9,
 	OPT_IPV6 = (1 << 10) * ENABLE_PING6,
+	OPT_p = 1 << 11,
 };
 
 
@@ -365,6 +368,7 @@ struct globals {
 	void *snd_packet; /* [datalen + ipv4/ipv6_const] */
 	const char *hostname;
 	const char *dotted;
+	int pattern;
 	union {
 		struct sockaddr sa;
 		struct sockaddr_in sin;
@@ -392,6 +396,7 @@ struct globals {
 #define dotted       (G.dotted      )
 #define pingaddr     (G.pingaddr    )
 #define rcvd_tbl     (G.rcvd_tbl    )
+#define pattern	     (G.pattern	    )
 void BUG_ping_globals_too_big(void);
 #define INIT_G() do { \
 	if (sizeof(G) > COMMON_BUFSIZE) \
@@ -485,7 +490,7 @@ static void sendping4(int junk UNUSED_PARAM)
 {
 	struct icmp *pkt = G.snd_packet;
 
-	//memset(pkt, 0, datalen + ICMP_MINLEN + 4); - G.snd_packet was xzalloced
+	memset(pkt, pattern, datalen + ICMP_MINLEN + 4);
 	pkt->icmp_type = ICMP_ECHO;
 	/*pkt->icmp_code = 0;*/
 	pkt->icmp_cksum = 0; /* cksum is calculated with this field set to 0 */
@@ -508,7 +513,7 @@ static void sendping6(int junk UNUSED_PARAM)
 {
 	struct icmp6_hdr *pkt = G.snd_packet;
 
-	//memset(pkt, 0, datalen + sizeof(struct icmp6_hdr) + 4);
+	memset(pkt, pattern, datalen + sizeof(struct icmp6_hdr) + 4);
 	pkt->icmp6_type = ICMP6_ECHO_REQUEST;
 	/*pkt->icmp6_code = 0;*/
 	/*pkt->icmp6_cksum = 0;*/
@@ -850,13 +855,13 @@ static void ping(len_and_sockaddr *lsa)
 static int common_ping_main(int opt, char **argv)
 {
 	len_and_sockaddr *lsa;
-	char *str_s;
+	char *str_s, *str_p;
 
 	INIT_G();
 
 	/* 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+:t+:w+:W+";
-	opt |= getopt32(argv, OPT_STRING, &pingcount, &str_s, &opt_ttl, &deadline, &timeout, &str_I);
+	opt |= getopt32(argv, OPT_STRING, &pingcount, &str_s, &opt_ttl, &deadline, &timeout, &str_I, &str_p);
 	if (opt & OPT_s)
 		datalen = xatou16(str_s); // -s
 	if (opt & OPT_I) { // -I
@@ -867,6 +872,10 @@ static int common_ping_main(int opt, char **argv)
 			str_I = NULL; /* don't try to bind to device later */
 		}
 	}
+	if (opt & OPT_p)
+		if (sscanf(str_p, "%2x", &pattern) != 1)
+			bb_error_msg_and_die("Invalid pattern specified: %s", str_p);
+
 	myid = (uint16_t) getpid();
 	hostname = argv[optind];
 #if ENABLE_PING6
-- 
1.9.1



More information about the busybox mailing list