From e80868b1650e14ae167a15e08ff9083e9e7462b3 Mon Sep 17 00:00:00 2001 From: Sergey Poznyakoff Date: Tue, 3 Mar 2020 16:48:13 +0200 Subject: Bugfixes. * pinger.c (icmp_generic_decode): Convert seq and id to hostorder prior to return. (send_echo): Keep hostping structure locked between sendto and update of the xmit_count. This fixes race condition: if a reply arrived in a time window between sendto and hostping_lock, the statistics would be committed prior to increasing xmit_count, producing inaccurate results (number of packets transmitted less that the number of packets received in the absense of duplicates). --- src/pinger.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/pinger.c b/src/pinger.c index 80d4dc0..4ab54f3 100644 --- a/src/pinger.c +++ b/src/pinger.c @@ -1085,10 +1085,11 @@ icmp_generic_decode(unsigned char * buffer, size_t bufsize, cksum = icmp->icmp_cksum; icmp->icmp_cksum = 0; icmp->icmp_cksum = icmp_cksum((unsigned char *) icmp, bufsize - hlen); - if (icmp->icmp_cksum != cksum) - return 1; icmp->icmp_seq = ntohs(icmp->icmp_seq); icmp->icmp_id = ntohs(icmp->icmp_id); + + if (icmp->icmp_cksum != cksum) + return 1; return 0; } @@ -1123,21 +1124,21 @@ send_echo(HOSTPING *host, unsigned char *ping_buffer) seqno); icmp_generic_encode(ping_buffer, buflen, ICMP_ECHO, ping_ident, seqno); + hostping_lock(host); n = sendto(ping_fd, (char *) ping_buffer, buflen, 0, host->addr, host->addrlen); if (n < 0) { error("%s: sendto: %s", host->name, strerror(errno)); } else { - hostping_lock(host); if (host->xmit_count == 0) host->start_tv = host->xmit_tv; host->xmit_count++; - hostping_unlock(host); xmit_total++; if (n != buflen) error("ping: wrote %s %zu chars, ret=%ld\n", host->name, buflen, n); } + hostping_unlock(host); } static void hostping_commit(HOSTPING *host); -- cgit v1.2.1