Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rtt: Fix extra 1 second delay in RTT calculation #70

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions binding.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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)
Expand All @@ -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;
}
4 changes: 3 additions & 1 deletion getusec.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
#include <sys/time.h>
#include <stdlib.h>

/* 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)
Expand Down
12 changes: 6 additions & 6 deletions rtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -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++)
Expand All @@ -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);
Expand All @@ -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);
Expand Down
21 changes: 15 additions & 6 deletions sendicmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand All @@ -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) {
Expand Down Expand Up @@ -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));
Expand All @@ -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) {
Expand All @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion sendtcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion sendudp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down