Skip to contentSkip to conversation

patch for iputils ping & ping6: print gettimeofday timestamp, print no reply

PATCH Ole Bjørn Hessen 1 participant 1 message Jun 26, 2009

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

Hi,

sometimes I need to ping an IP address for a long time to get to know how stable a connection is. It'd be nice if output from ping could be configured to be more grep'ble and time stamped.

This small patch adds some functionality to print timestamp before printfs (option -D) and also to print out "no reply yet" (option -N) if no packet was received before next packet is sent.

I sent the patch original to Noah Meyerhans at debian, but he suggested to send the patch to YOSHIFUJI Hideaki) via the [email protected] mailinglist instead.

Kind regards,

Ole Bjørn Hessen
Telenor

doc/ping.sgml renamed from iputils_20071127.orig/doc/ping.sgml +13 -0
diff -r -u ../iputils_20071127.orig/doc/ping.sgml ./doc/ping.sgml
@@ -160,6 +160,19 @@
   </para></listitem>
  </varlistentry>
  <varlistentry>
+  <term><option/-D/</term>
+  <listitem><para>
+Print timestamp (unix time + microseconds as in gettimeofday) before
+each line.
+  </para></listitem>
+ </varlistentry>
+ <varlistentry>
+  <term><option/-N/</term>
+  <listitem><para>
+Print no reply yet if no packet was received before sending next packet.
+  </para></listitem>
+ </varlistentry>
+ <varlistentry>
   <term><option>-Q <replaceable/tos/</option></term>
   <listitem><para>
 Set Quality of Service -related bits in ICMP datagrams.  
ping6.c renamed from iputils_20071127.orig/ping6.c +16 -1
diff -r -u ../iputils_20071127.orig/ping6.c ./ping6.c
@@ -672,6 +672,11 @@
 		if (options & F_FLOOD) {
 			write(STDOUT_FILENO, "\bE", 2);
 		} else {
+			if (options & F_PTIMEOFDAY) {
+				struct timeval recv_time;
+				gettimeofday(&recv_time, NULL);
+				printf("%lu.%06lu ", (unsigned long)recv_time.tv_sec, (unsigned long)recv_time.tv_usec);
+			}
 			printf("From %s icmp_seq=%u ", pr_addr(&sin6->sin6_addr), ntohs(icmph.icmp6_seq));
 			pr_icmph(e->ee_type, e->ee_code, e->ee_info);
 			putchar('\n');
@@ -821,11 +826,21 @@
 				write(STDOUT_FILENO, "\bE", 2);
 				return 0;
 			}
+			if (options & F_PTIMEOFDAY) {
+				struct timeval recv_time;
+				gettimeofday(&recv_time, NULL);
+				printf("%lu.%06lu ", (unsigned long)recv_time.tv_sec, (unsigned long)recv_time.tv_usec);
+			}
 			printf("From %s: icmp_seq=%u ", pr_addr(&from->sin6_addr), ntohs(icmph1->icmp6_seq));
 		} else {
 			/* We've got something other than an ECHOREPLY */
 			if (!(options & F_VERBOSE) || uid)
 				return 1;
+			if (options & F_PTIMEOFDAY) {
+				struct timeval recv_time;
+				gettimeofday(&recv_time, NULL);
+				printf("%lu.%06lu ", (unsigned long)recv_time.tv_sec, (unsigned long)recv_time.tv_usec);
+			}
 			printf("From %s: ", pr_addr(&from->sin6_addr));
 		}
 		pr_icmph(icmph->icmp6_type, icmph->icmp6_code, ntohl(icmph->icmp6_mtu));
@@ -971,7 +986,7 @@
 void usage(void)
 {
 	fprintf(stderr,
-"Usage: ping6 [-LUdfnqrvVaA] [-c count] [-i interval] [-w deadline]\n"
+"Usage: ping6 [-LUdfnqrvVaADN] [-c count] [-i interval] [-w deadline]\n"
 "             [-p pattern] [-s packetsize] [-t ttl] [-I interface]\n"
 "             [-M mtu discovery hint] [-S sndbuf]\n"
 "             [-F flow label] [-Q traffic class] [hop1 ...] destination\n");
ping.c renamed from iputils_20071127.orig/ping.c +16 -1
diff -r -u ../iputils_20071127.orig/ping.c ./ping.c
@@ -595,6 +595,11 @@
 		if (options & F_FLOOD) {
 			write(STDOUT_FILENO, "\bE", 2);
 		} else {
+			if (options & F_PTIMEOFDAY) {
+				struct timeval recv_time;
+				gettimeofday(&recv_time, NULL);
+				printf("%lu.%06lu ", (unsigned long)recv_time.tv_sec, (unsigned long)recv_time.tv_usec);
+			}
 			printf("From %s icmp_seq=%u ", pr_addr(sin->sin_addr.s_addr), ntohs(icmph.un.echo.sequence));
 			pr_icmph(e->ee_type, e->ee_code, e->ee_info, NULL);
 			fflush(stdout);
@@ -761,6 +766,11 @@
 						write(STDOUT_FILENO, "\bE", 2);
 					return !error_pkt;
 				}
+				if (options & F_PTIMEOFDAY) {
+					struct timeval recv_time;
+					gettimeofday(&recv_time, NULL);
+					printf("%lu.%06lu ", (unsigned long)recv_time.tv_sec, (unsigned long)recv_time.tv_usec);
+				}
 				printf("From %s: icmp_seq=%u ",
 				       pr_addr(from->sin_addr.s_addr),
 				       ntohs(icp1->un.echo.sequence));
@@ -782,6 +792,11 @@
 		}
 		if (!(options & F_VERBOSE) || uid)
 			return 0;
+		if (options & F_PTIMEOFDAY) {
+			struct timeval recv_time;
+			gettimeofday(&recv_time, NULL);
+			printf("%lu.%06lu ", (unsigned long)recv_time.tv_sec, (unsigned long)recv_time.tv_usec);
+		}
 		printf("From %s: ", pr_addr(from->sin_addr.s_addr));
 		if (csfailed) {
 			printf("(BAD CHECKSUM)\n");
@@ -1214,7 +1229,7 @@
 void usage(void)
 {
 	fprintf(stderr,
-"Usage: ping [-LRUbdfnqrvVaA] [-c count] [-i interval] [-w deadline]\n"
+"Usage: ping [-LRUbdfnqrvVaADN] [-c count] [-i interval] [-w deadline]\n"
 "            [-p pattern] [-s packetsize] [-t ttl] [-I interface or address]\n"
 "            [-M mtu discovery hint] [-S sndbuf]\n"
 "            [ -T timestamp option ] [ -Q tos ] [hop1 ...] destination\n");
ping_common.c renamed from iputils_20071127.orig/ping_common.c +23 -0
diff -r -u ../iputils_20071127.orig/ping_common.c ./ping_common.c
@@ -114,6 +114,9 @@
 	case 'd':
 		options |= F_SO_DEBUG;
 		break;
+	case 'D':
+		options |= F_PTIMEOFDAY;
+		break;
 	case 'f':
 		options |= F_FLOOD;
 		setbuf(stdout, (char *)NULL);
@@ -171,6 +174,9 @@
 	case 'n':
 		options |= F_NUMERIC;
 		break;
+	case 'N':
+		options |= F_NOREPLY;
+		break;
 	case 'p':		/* fill buffer with user pattern */
 		options |= F_PINGFILLED;
 		fill(optarg);
@@ -316,6 +322,18 @@
 		tokens = ntokens - interval;
 	}
 
+	if (options & F_NOREPLY) {
+		if (ntransmitted > 0 && !TST(ntransmitted % mx_dup_ck)) {
+			if (options & F_PTIMEOFDAY) {
+				struct timeval recv_time;
+				gettimeofday(&recv_time, NULL);	
+				printf("%lu.%06lu ", (unsigned long)recv_time.tv_sec, (unsigned long)recv_time.tv_usec);
+			}
+			printf("no answer yet for icmp_seq=%lu\n", (ntransmitted % mx_dup_ck));
+			fflush(stdout);
+		}
+	}
+
 resend:
 	i = send_probe();
 
@@ -714,6 +732,11 @@
 	} else {
 		int i;
 		__u8 *cp, *dp;
+		if (options & F_PTIMEOFDAY) {
+			struct timeval recv_time;
+			gettimeofday(&recv_time, NULL);
+			printf("%lu.%06lu ", (unsigned long)recv_time.tv_sec, (unsigned long)recv_time.tv_usec);
+		}
 		printf("%d bytes from %s: icmp_seq=%u", cc, from, seq);
 
 		if (hops >= 0)
ping_common.h renamed from iputils_20071127.orig/ping_common.h +5 -2
diff -r -u ../iputils_20071127.orig/ping_common.h ./ping_common.h
@@ -60,6 +60,8 @@
 #define	F_STRICTSOURCE	0x8000
 #define F_NOLOOP	0x10000
 #define F_TTL		0x20000
+#define F_PTIMEOFDAY	0x40000
+#define F_NOREPLY	0x80000
 
 /*
  * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum
@@ -118,9 +120,10 @@
 case 'f': case 'i': case 'w': case 'l': \
 case 'S': case 'n': case 'p': case 'q': \
 case 'r': case 's': case 'v': case 'L': \
-case 't': case 'A': case 'W': case 'B':
+case 't': case 'A': case 'W': case 'B': \
+case 'D': case 'N':
 
-#define COMMON_OPTSTR "h?VQ:I:M:aUc:dfi:w:l:S:np:qrs:vLt:AW:B"
+#define COMMON_OPTSTR "h?VQ:I:M:aUc:dfi:w:l:S:np:qrs:vLt:AW:BDN"
 
 
 /*