Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 11 Oct 2023 18:01:08 GMT
From:      Mark Johnston <markj@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org
Subject:   git: b86e4812cce8 - main - ping: pr_iph() improvements
Message-ID:  <202310111801.39BI18eL053900@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch main has been updated by markj:

URL: https://cgit.FreeBSD.org/src/commit/?id=b86e4812cce88920cb592bd3b8571572373dbb9c

commit b86e4812cce88920cb592bd3b8571572373dbb9c
Author:     Jose Luis Duran <jlduran@gmail.com>
AuthorDate: 2023-04-13 15:30:44 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2023-10-11 17:48:28 +0000

    ping: pr_iph() improvements
    
    Very early on, the Src/Dst IP addresses were printed in hex notation
    (%08x), which will always be 8-characters wide.  It was later changed to
    use a dot-decimal notation.  Depending on the IP address length, the Src
    and Dst headers may require a different padding.  Use the source and
    destination IP lengths as padding for the headers.
    
    Also, print an Opts (options) header, if there are options present.  It
    has been abbreviated to Opts to match the length of the previous Data
    header, removed in ef9e6dc7eebe9830511602904d3ef5218d964080.
    
    Print the header info such that no trailing spaces are produced.  As
    some git workflows may automatically trim them, and make the tests fail
    (see 25b86f8559c2e7076daff56933217e95cd4398d4).
    
    Before
    
        Vr HL TOS  Len   ID Flg  off TTL Pro  cks      Src      Dst
         4  f  00 007c 0001   0 0000  40  01 d868 192.0.2.1  192.0.2.2␣
    
    After
    
        Vr HL TOS  Len   ID Flg  off TTL Pro  cks       Src       Dst
         4  f  00 007c 0001   0 0000  40  01 d868 192.0.2.1 192.0.2.2
    
    And with options:
    
    Before
    
        Vr HL TOS  Len   ID Flg  off TTL Pro  cks      Src      Dst
         4  f  00 007c 0001   0 0000  40  01 d868 192.0.2.1  192.0.2.2 01...
    
    After
    
        Vr HL TOS  Len   ID Flg  off TTL Pro  cks       Src       Dst Opts
         4  f  00 007c 0001   0 0000  40  01 d868 192.0.2.1 192.0.2.2 01...
    
    Reviewed by:    markj
    MFC after:      1 week
    Pull Request:   https://github.com/freebsd/freebsd-src/pull/863
    Differential Revision:  https://reviews.freebsd.org/D39561
---
 sbin/ping/ping.c             | 25 +++++++++++++++++--------
 sbin/ping/tests/test_ping.py | 12 ++++++------
 2 files changed, 23 insertions(+), 14 deletions(-)

diff --git a/sbin/ping/ping.c b/sbin/ping/ping.c
index fcc27d34ee54..3b8663772e87 100644
--- a/sbin/ping/ping.c
+++ b/sbin/ping/ping.c
@@ -1561,13 +1561,21 @@ pr_icmph(struct icmp *icp, struct ip *oip, const u_char *const oicmp_raw)
 static void
 pr_iph(struct ip *ip, const u_char *cp)
 {
-	struct in_addr ina;
+	struct in_addr dst_ina, src_ina;
 	int hlen;
 
 	hlen = ip->ip_hl << 2;
 	cp = cp + sizeof(struct ip);		/* point to options */
 
-	(void)printf("Vr HL TOS  Len   ID Flg  off TTL Pro  cks      Src      Dst\n");
+	memcpy(&src_ina, &ip->ip_src.s_addr, sizeof(src_ina));
+	memcpy(&dst_ina, &ip->ip_dst.s_addr, sizeof(dst_ina));
+
+	(void)printf("Vr HL TOS  Len   ID Flg  off TTL Pro  cks %*s %*s",
+	    (int)strlen(inet_ntoa(src_ina)), "Src",
+	    (int)strlen(inet_ntoa(dst_ina)), "Dst");
+	if (hlen > (int)sizeof(struct ip))
+		(void)printf(" Opts");
+	(void)putchar('\n');
 	(void)printf(" %1x  %1x  %02x %04x %04x",
 	    ip->ip_v, ip->ip_hl, ip->ip_tos, ntohs(ip->ip_len),
 	    ntohs(ip->ip_id));
@@ -1576,13 +1584,14 @@ pr_iph(struct ip *ip, const u_char *cp)
 	    ntohs(ip->ip_off) & 0x1fff);
 	(void)printf("  %02x  %02x %04x", ip->ip_ttl, ip->ip_p,
 							    ntohs(ip->ip_sum));
-	memcpy(&ina, &ip->ip_src.s_addr, sizeof ina);
-	(void)printf(" %s ", inet_ntoa(ina));
-	memcpy(&ina, &ip->ip_dst.s_addr, sizeof ina);
-	(void)printf(" %s ", inet_ntoa(ina));
+	(void)printf(" %s", inet_ntoa(src_ina));
+	(void)printf(" %s", inet_ntoa(dst_ina));
 	/* dump any option bytes */
-	while (hlen-- > (int)sizeof(struct ip)) {
-		(void)printf("%02x", *cp++);
+	if (hlen > (int)sizeof(struct ip)) {
+		(void)printf(" ");
+		while (hlen-- > (int)sizeof(struct ip)) {
+			(void)printf("%02x", *cp++);
+		}
 	}
 	(void)putchar('\n');
 }
diff --git a/sbin/ping/tests/test_ping.py b/sbin/ping/tests/test_ping.py
index 545e680fbc9f..645940a62cb6 100644
--- a/sbin/ping/tests/test_ping.py
+++ b/sbin/ping/tests/test_ping.py
@@ -1244,8 +1244,8 @@ round-trip min/avg/max/stddev = /// ms
                 "stdout": """\
 PING 192.0.2.2 (192.0.2.2): 56 data bytes
 132 bytes from 192.0.2.2: Destination Host Unreachable
-Vr HL TOS  Len   ID Flg  off TTL Pro  cks      Src      Dst
- 4  f  00 007c 0001   0 0000  40  01 d868 192.0.2.1  192.0.2.2 01010101010101010101010101010101010101010101010101010101010101010101010101010101
+Vr HL TOS  Len   ID Flg  off TTL Pro  cks       Src       Dst Opts
+ 4  f  00 007c 0001   0 0000  40  01 d868 192.0.2.1 192.0.2.2 01010101010101010101010101010101010101010101010101010101010101010101010101010101
 
 
 --- 192.0.2.2 ping statistics ---
@@ -1269,8 +1269,8 @@ Vr HL TOS  Len   ID Flg  off TTL Pro  cks      Src      Dst
                 "stdout": """\
 PING 192.0.2.2 (192.0.2.2): 56 data bytes
 92 bytes from 192.0.2.2: Destination Host Unreachable
-Vr HL TOS  Len   ID Flg  off TTL Pro  cks      Src      Dst
- 4  5  00 0054 0001   2 0000  40  01 b6a4 192.0.2.1  192.0.2.2 
+Vr HL TOS  Len   ID Flg  off TTL Pro  cks       Src       Dst
+ 4  5  00 0054 0001   2 0000  40  01 b6a4 192.0.2.1 192.0.2.2
 
 
 --- 192.0.2.2 ping statistics ---
@@ -1342,8 +1342,8 @@ ping: quoted data too short (28 bytes) from 192.0.2.2
                 "stdout": """\
 PING 192.0.2.2 (192.0.2.2): 56 data bytes
 92 bytes from 192.0.2.2: Destination Host Unreachable
-Vr HL TOS  Len   ID Flg  off TTL Pro  cks      Src      Dst
- 4  5  00 0054 0001   0 0000  40  01 f6a4 192.0.2.1  192.0.2.2 
+Vr HL TOS  Len   ID Flg  off TTL Pro  cks       Src       Dst
+ 4  5  00 0054 0001   0 0000  40  01 f6a4 192.0.2.1 192.0.2.2
 
 
 --- 192.0.2.2 ping statistics ---



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202310111801.39BI18eL053900>