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 ef9e6dc7ee.

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 25b86f8559).

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
This commit is contained in:
Jose Luis Duran 2023-04-13 15:30:44 +00:00 committed by Mark Johnston
parent 8db2c5802a
commit b86e4812cc
2 changed files with 23 additions and 14 deletions

View file

@ -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');
}

View file

@ -1244,8 +1244,8 @@ def test_ping_46(self, expected):
"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 @@ def test_ping_46(self, expected):
"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 @@ def test_ping_46(self, expected):
"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 ---