From 604885cbef0d240ea91e1d6ce1810ef33bb6df18 Mon Sep 17 00:00:00 2001 From: Xiang Xiao Date: Thu, 23 Aug 2018 06:47:33 -0600 Subject: [PATCH] apps/system/ping and ping6: Send ID and seqno in network byte order. --- system/ping/ping.c | 22 +++++++++++----------- system/ping6/ping6.c | 18 +++++++++--------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/system/ping/ping.c b/system/ping/ping.c index b99c5af45..595245e09 100644 --- a/system/ping/ping.c +++ b/system/ping/ping.c @@ -202,7 +202,7 @@ static void icmp_ping(FAR struct ping_info_s *info) memset(&outhdr, 0, sizeof(struct icmp_hdr_s)); outhdr.type = ICMP_ECHO_REQUEST; - outhdr.id = ping_newid(); + outhdr.id = htons(ping_newid()); outhdr.seqno = 0; printf("PING %u.%u.%u.%u %d bytes of data\n", @@ -240,7 +240,7 @@ static void icmp_ping(FAR struct ping_info_s *info) if (nsent < 0) { fprintf(stderr, "ERROR: sendto failed at seqno %u: %d\n", - outhdr.seqno, errno); + ntohs(outhdr.seqno), errno); return; } else if (nsent != outsize) @@ -276,7 +276,7 @@ static void icmp_ping(FAR struct ping_info_s *info) (info->dest.s_addr >> 8 ) & 0xff, (info->dest.s_addr >> 16) & 0xff, (info->dest.s_addr >> 24) & 0xff, - outhdr.seqno, info->delay); + ntohs(outhdr.seqno), info->delay); continue; } @@ -307,15 +307,15 @@ static void icmp_ping(FAR struct ping_info_s *info) fprintf(stderr, "WARNING: Ignoring ICMP reply with ID %u. " "Expected %u\n", - inhdr->id, outhdr.id); + ntohs(inhdr->id), ntohs(outhdr.id)); retry = true; } - else if (inhdr->seqno > outhdr.seqno) + else if (ntohs(inhdr->seqno) > ntohs(outhdr.seqno)) { fprintf(stderr, "WARNING: Ignoring ICMP reply to sequence %u. " "Expected <= &u\n", - inhdr->seqno, outhdr.seqno); + ntohs(inhdr->seqno), ntohs(outhdr.seqno)); retry = true; } else @@ -323,7 +323,7 @@ static void icmp_ping(FAR struct ping_info_s *info) bool verified = true; int32_t pktdelay = elapsed; - if (inhdr->seqno < outhdr.seqno) + if (ntohs(inhdr->seqno) < ntohs(outhdr.seqno)) { fprintf(stderr, "WARNING: Received after timeout\n"); pktdelay += info->delay; @@ -336,7 +336,7 @@ static void icmp_ping(FAR struct ping_info_s *info) (info->dest.s_addr >> 8 ) & 0xff, (info->dest.s_addr >> 16) & 0xff, (info->dest.s_addr >> 24) & 0xff, - inhdr->seqno, pktdelay); + ntohs(inhdr->seqno), pktdelay); /* Verify the payload data */ @@ -407,7 +407,7 @@ static void icmp_ping(FAR struct ping_info_s *info) (void)nanosleep(&rqt, NULL); } - outhdr.seqno++; + outhdr.seqno = htons(ntohs(outhdr.seqno) + 1); } } @@ -422,8 +422,8 @@ static void show_usage(FAR const char *progname, int exitcode) printf("\nUsage: %s [-c ] [-i ] \n", progname); printf(" %s -h\n", progname); printf("\nWhere:\n"); - printf(" is either an IPv6 address or the name of the remote host\n"); - printf(" that is requested the ICMPv6 ECHO reply.\n"); + printf(" is either an IPv4 address or the name of the remote host\n"); + printf(" that is requested the ICMPv4 ECHO reply.\n"); #else printf("\nUsage: %s [-c ] [-i ] \n", progname); printf(" %s -h\n", progname); diff --git a/system/ping6/ping6.c b/system/ping6/ping6.c index ddf2a294b..1f89001c3 100644 --- a/system/ping6/ping6.c +++ b/system/ping6/ping6.c @@ -208,7 +208,7 @@ static void icmpv6_ping(FAR struct ping6_info_s *info) memset(&outhdr, 0, SIZEOF_ICMPV6_ECHO_REQUEST_S(0)); outhdr.type = ICMPv6_ECHO_REQUEST; - outhdr.id = ping6_newid(); + outhdr.id = htons(ping6_newid()); outhdr.seqno = 0; (void)inet_ntop(AF_INET6, info->dest.s6_addr16, info->strbuffer, @@ -244,7 +244,7 @@ static void icmpv6_ping(FAR struct ping6_info_s *info) if (nsent < 0) { fprintf(stderr, "ERROR: sendto failed at seqno %u: %d\n", - outhdr.seqno, errno); + ntohs(outhdr.seqno), errno); return; } else if (nsent != outsize) @@ -278,7 +278,7 @@ static void icmpv6_ping(FAR struct ping6_info_s *info) (void)inet_ntop(AF_INET6, info->dest.s6_addr16, info->strbuffer, INET6_ADDRSTRLEN); printf("No response from %s: icmp_seq=%u time=%u ms\n", - info->strbuffer, outhdr.seqno, info->delay); + info->strbuffer, ntohs(outhdr.seqno), info->delay); continue; } @@ -310,15 +310,15 @@ static void icmpv6_ping(FAR struct ping6_info_s *info) fprintf(stderr, "WARNING: Ignoring ICMP reply with ID %u. " "Expected %u\n", - inhdr->id, outhdr.id); + ntohs(inhdr->id), ntohs(outhdr.id)); retry = true; } - else if (inhdr->seqno > outhdr.seqno) + else if (ntohs(inhdr->seqno) > ntohs(outhdr.seqno)) { fprintf(stderr, "WARNING: Ignoring ICMP reply to sequence %u. " "Expected <= &u\n", - inhdr->seqno, outhdr.seqno); + ntohs(inhdr->seqno), ntohs(outhdr.seqno)); retry = true; } else @@ -326,7 +326,7 @@ static void icmpv6_ping(FAR struct ping6_info_s *info) bool verified = true; int32_t pktdelay = elapsed; - if (inhdr->seqno < outhdr.seqno) + if (ntohs(inhdr->seqno) < ntohs(outhdr.seqno)) { fprintf(stderr, "WARNING: Received after timeout\n"); pktdelay += info->delay; @@ -337,7 +337,7 @@ static void icmpv6_ping(FAR struct ping6_info_s *info) info->strbuffer, INET6_ADDRSTRLEN); printf("%ld bytes from %s icmp_seq=%u time=%u ms\n", nrecvd - SIZEOF_ICMPV6_ECHO_REPLY_S(0), - info->strbuffer, inhdr->seqno, pktdelay); + info->strbuffer, ntohs(inhdr->seqno), pktdelay); /* Verify the payload data */ @@ -408,7 +408,7 @@ static void icmpv6_ping(FAR struct ping6_info_s *info) (void)nanosleep(&rqt, NULL); } - outhdr.seqno++; + outhdr.seqno = htons(ntohs(outhdr.seqno) + 1); } }