From fa3b341744e9bdcbf0b930886cc214943cba50d6 Mon Sep 17 00:00:00 2001 From: "K. Lange" Date: Thu, 5 Jan 2023 23:21:17 +0900 Subject: [PATCH] ping: use gettimeofday instead of relying on clock ticks --- apps/ping.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/apps/ping.c b/apps/ping.c index 2641fb05..cff924fe 100644 --- a/apps/ping.c +++ b/apps/ping.c @@ -16,7 +16,7 @@ #include #include #include -#include +#include #include #include #include @@ -32,6 +32,12 @@ struct ICMP_Header { uint8_t payload[]; }; +static unsigned long clocktime(void) { + struct timeval tv; + gettimeofday(&tv,NULL); + return tv.tv_sec * 1000000 + tv.tv_usec; +} + static uint16_t icmp_checksum(char * payload, size_t len) { uint32_t sum = 0; uint16_t * s = (uint16_t *)payload; @@ -98,7 +104,7 @@ int main(int argc, char * argv[]) { ping->checksum = htons(icmp_checksum((void*)ping, BYTES_TO_SEND)); /* Send it and wait */ - clock_t sent_at = times(NULL); + unsigned long sent_at = clocktime(); if (sendto(sock, (void*)ping, BYTES_TO_SEND, 0, (struct sockaddr*)&dest, sizeof(struct sockaddr_in)) < 0) { fprintf(stderr, "sendto: %s\n", strerror(errno)); } @@ -115,7 +121,7 @@ int main(int argc, char * argv[]) { struct sockaddr_in source; socklen_t source_size = sizeof(struct sockaddr_in); ssize_t len = recvfrom(sock, data, 4096, 0, (struct sockaddr*)&source, &source_size); - clock_t rcvd_at = times(NULL); + unsigned long rcvd_at = clocktime(); if (len > 0) { /* Is it actually a PING response ? */