From 20d5929543acb718bb633e094783f266a8c83a07 Mon Sep 17 00:00:00 2001 From: maoyingming Date: Fri, 5 Jul 2024 20:24:33 +0800 Subject: [PATCH] rtt: Fix extra 1 second delay in RTT calculation Fix the issue in the rtt() function where it incorrectly adds an extra 1 second delay to the RTT calculation. To ensure the acquisition of atomic seconds and microseconds values, use gettimeofday() instead of time() and get_usec(). Reviewed-by: lishuo02 Signed-off-by: maoyingming --- binding.c | 11 ++++++----- getusec.c | 4 +++- rtt.c | 12 ++++++------ sendicmp.c | 21 +++++++++++++++------ sendtcp.c | 4 +++- sendudp.c | 4 +++- 6 files changed, 36 insertions(+), 20 deletions(-) diff --git a/binding.c b/binding.c index 338da17..487c4fe 100644 --- a/binding.c +++ b/binding.c @@ -20,8 +20,8 @@ void inc_destparm(int sid) { - static long sec = 0; - static long usec = 0; + static struct timeval tv = {0, 0}; + struct timeval tmptv; int *p; int errno_save = errno; @@ -38,7 +38,9 @@ void inc_destparm(int sid) return; } - if ( (time(NULL) == sec) && ((get_usec() - usec) < 200000) ) { + gettimeofday(&tmptv, NULL); + if ( (tmptv.tv_sec == tv.tv_sec) && + ((tmptv.tv_usec - tv.tv_usec) < 200000) ) { if (*p > 0) (*p)-=2; if (*p < 0) @@ -50,8 +52,7 @@ void inc_destparm(int sid) printf("%d: ", *p); fflush(stdout); - sec = time(NULL); - usec = get_usec(); + gettimeofday(&tv, NULL); signal(SIGTSTP, inc_destparm); errno = errno_save; } diff --git a/getusec.c b/getusec.c index fd344ee..d9a7736 100644 --- a/getusec.c +++ b/getusec.c @@ -13,12 +13,14 @@ #include #include +/* A signed 32-bit integer can represent a maximum of 35 minutes + * when measured in microseconds. */ time_t get_usec(void) { struct timeval tmptv; gettimeofday(&tmptv, NULL); - return tmptv.tv_usec; + return ((tmptv.tv_sec % 1800) * 1000000 + tmptv.tv_usec); } time_t milliseconds(void) diff --git a/rtt.c b/rtt.c index e06343d..ee6276e 100644 --- a/rtt.c +++ b/rtt.c @@ -32,6 +32,7 @@ int rtt(int *seqp, int recvport, float *ms_delay) { long sec_delay = 0, usec_delay = 0; int i, tablepos = -1, status; + struct timeval tmptv; if (*seqp != 0) { for (i = 0; i < TABLESIZE; i++) @@ -54,10 +55,9 @@ int rtt(int *seqp, int recvport, float *ms_delay) status = delaytable[tablepos].status; delaytable[tablepos].status = S_RECV; - sec_delay = time(NULL) - delaytable[tablepos].sec; - usec_delay = get_usec() - delaytable[tablepos].usec; - if (sec_delay == 0 && usec_delay < 0) - usec_delay += 1000000; + gettimeofday(&tmptv, NULL); + sec_delay = tmptv.tv_sec - delaytable[tablepos].sec; + usec_delay = tmptv.tv_usec - delaytable[tablepos].usec; *ms_delay = (sec_delay * 1000) + ((float)usec_delay / 1000); minavgmax(*ms_delay); @@ -74,11 +74,11 @@ int rtt(int *seqp, int recvport, float *ms_delay) printf("\n\nSANITY CHECK in rtt.c FAILED!\n"); printf("- seqnum = %d\n", *seqp); printf("- status = %d\n", status); - printf("- get_usec() = %ld\n", (long int) get_usec()); + printf("- usec = %ld\n", tmptv.tv_usec); printf("- delaytable.usec = %ld\n", (long int) delaytable[tablepos].usec); printf("- usec_delay = %ld\n", usec_delay); - printf("- time(NULL) = %ld\n", (long int) time(NULL)); + printf("- sec = %ld\n", tmptv.tv_sec); printf("- delaytable.sec = %ld\n", (long int) delaytable[tablepos].sec); printf("- sec_delay = %ld\n", sec_delay); diff --git a/sendicmp.c b/sendicmp.c index 7efb274..fae8633 100644 --- a/sendicmp.c +++ b/sendicmp.c @@ -67,6 +67,7 @@ void send_icmp_echo(void) { char *packet, *data; struct myicmphdr *icmp; + struct timeval tmptv; packet = malloc(ICMPHDR_SIZE + data_size); if (packet == NULL) { @@ -96,8 +97,10 @@ void send_icmp_echo(void) icmp->checksum = icmp_cksum; /* adds this pkt in delaytable */ - if (opt_icmptype == ICMP_ECHO) - delaytable_add(_icmp_seq, 0, time(NULL), get_usec(), S_SENT); + if (opt_icmptype == ICMP_ECHO) { + gettimeofday(&tmptv, NULL); + delaytable_add(_icmp_seq, 0, tmptv.tv_sec, tmptv.tv_usec, S_SENT); + } /* send packet */ send_ip_handler(packet, ICMPHDR_SIZE + data_size); @@ -111,6 +114,7 @@ void send_icmp_timestamp(void) char *packet; struct myicmphdr *icmp; struct icmp_tstamp_data *tstamp_data; + struct timeval tmptv; packet = malloc(ICMPHDR_SIZE + sizeof(struct icmp_tstamp_data)); if (packet == NULL) { @@ -140,8 +144,10 @@ void send_icmp_timestamp(void) icmp->checksum = icmp_cksum; /* adds this pkt in delaytable */ - if (opt_icmptype == ICMP_TIMESTAMP) - delaytable_add(_icmp_seq, 0, time(NULL), get_usec(), S_SENT); + if (opt_icmptype == ICMP_TIMESTAMP) { + gettimeofday(&tmptv, NULL); + delaytable_add(_icmp_seq, 0, tmptv.tv_sec, tmptv.tv_usec, S_SENT); + } /* send packet */ send_ip_handler(packet, ICMPHDR_SIZE + sizeof(struct icmp_tstamp_data)); @@ -154,6 +160,7 @@ void send_icmp_address(void) { char *packet; struct myicmphdr *icmp; + struct timeval tmptv; packet = malloc(ICMPHDR_SIZE + 4); if (packet == NULL) { @@ -180,8 +187,10 @@ void send_icmp_address(void) icmp->checksum = icmp_cksum; /* adds this pkt in delaytable */ - if (opt_icmptype == ICMP_TIMESTAMP) - delaytable_add(_icmp_seq, 0, time(NULL), get_usec(), S_SENT); + if (opt_icmptype == ICMP_TIMESTAMP) { + gettimeofday(&tmptv, NULL); + delaytable_add(_icmp_seq, 0, tmptv.tv_sec, tmptv.tv_usec, S_SENT); + } /* send packet */ send_ip_handler(packet, ICMPHDR_SIZE + 4); diff --git a/sendtcp.c b/sendtcp.c index 6765ad4..de6a6e1 100644 --- a/sendtcp.c +++ b/sendtcp.c @@ -29,6 +29,7 @@ void send_tcp(void) struct mytcphdr *tcp; struct pseudohdr *pseudoheader; unsigned char *tstamp; + struct timeval tmptv; if (opt_tcp_timestamp) tcp_opt_size = 12; @@ -86,7 +87,8 @@ void send_tcp(void) #endif /* adds this pkt in delaytable */ - delaytable_add(sequence, src_port, time(NULL), get_usec(), S_SENT); + gettimeofday(&tmptv, NULL); + delaytable_add(sequence, src_port, tmptv.tv_sec, tmptv.tv_usec, S_SENT); /* send packet */ send_ip_handler(packet+PSEUDOHDR_SIZE, packet_size); diff --git a/sendudp.c b/sendudp.c index 6da7cd2..b991473 100644 --- a/sendudp.c +++ b/sendudp.c @@ -29,6 +29,7 @@ void send_udp(void) char *packet, *data; struct myudphdr *udp; struct pseudohdr *pseudoheader; + struct timeval tmptv; packet_size = UDPHDR_SIZE + data_size; packet = malloc(PSEUDOHDR_SIZE + packet_size); @@ -65,7 +66,8 @@ void send_udp(void) #endif /* adds this pkt in delaytable */ - delaytable_add(sequence, src_port, time(NULL), get_usec(), S_SENT); + gettimeofday(&tmptv, NULL); + delaytable_add(sequence, src_port, tmptv.tv_sec, tmptv.tv_usec, S_SENT); /* send packet */ send_ip_handler(packet+PSEUDOHDR_SIZE, packet_size);