From 936f75494165fea4a3e3561e7d9a1eed1c256801 Mon Sep 17 00:00:00 2001 From: Jolan Rathelot Date: Tue, 1 Apr 2025 07:33:51 -0400 Subject: [PATCH] added min/max calcs --- includes/main.h | 5 +++++ srcs/main.c | 53 ++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 49 insertions(+), 9 deletions(-) diff --git a/includes/main.h b/includes/main.h index c65cf60..6e1333c 100644 --- a/includes/main.h +++ b/includes/main.h @@ -28,4 +28,9 @@ struct ping_pkt { char msg[64 - sizeof(struct icmphdr)]; }; +struct ping_delta { + int ms_top; + int ms_bottom; +}; + #endif //MAIN_H diff --git a/srcs/main.c b/srcs/main.c index 87a0877..f86b404 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -108,11 +108,45 @@ void sig_handler(int sig) { } } +struct ping_delta get_min(struct ping_delta a, struct ping_delta b) { + if (a.ms_top < b.ms_top) { + return a; + } else if (b.ms_top < a.ms_top) { + return b; + } else { + if (a.ms_bottom < b.ms_bottom) { + return a; + } + return b; + } +} + +struct ping_delta get_max(struct ping_delta a, struct ping_delta b) { + if (a.ms_top > b.ms_top) { + return a; + } else if (b.ms_top > a.ms_top) { + return b; + } else { + if (a.ms_bottom > b.ms_bottom) { + return a; + } + return b; + } +} + int main(int argc, char** argv) { struct arguments arguments; int timeout = 3000; struct timespec recvt; struct pollfd fds[1]; + struct ping_delta min; + struct ping_delta max; + + min.ms_bottom = INT32_MAX; + min.ms_top = INT32_MAX; + + max.ms_bottom = 0; + max.ms_top = 0; arguments.verbose = 0; arguments.ttl = 64; @@ -153,7 +187,7 @@ int main(int argc, char** argv) { send_ping(); - while (true) { + while (!stop) { int ret = poll(fds, 1, timeout); if (ret > 0) { if (fds[0].revents & POLLIN) { @@ -171,13 +205,18 @@ int main(int argc, char** argv) { struct ip* ip_header = (struct ip*)buf; struct icmphdr* icmp_reply = (struct icmphdr*)(buf + (ip_header->ip_hl << 2)); - int rtt_n = + struct ping_delta tmp; + + tmp.ms_top = ((recvt.tv_sec * 1000 + recvt.tv_nsec / 1000000) - (sendt.tv_sec * 1000 + sendt.tv_nsec / 1000000)); - int rtt_r = ((recvt.tv_nsec - sendt.tv_nsec) % 1000000 / 1000); + tmp.ms_bottom = ((recvt.tv_nsec - sendt.tv_nsec) % 1000000 / 1000); printf("%zd bytes from %s: icmp_seq=%d ttl=%d time=%d.%d ms \n", bytes_received, arguments.args[0], - icmp_reply->un.echo.sequence, ip_header->ip_ttl, rtt_n, rtt_r); + icmp_reply->un.echo.sequence, ip_header->ip_ttl, tmp.ms_top, tmp.ms_bottom); recvd++; + + max = get_max(max, tmp); + min = get_min(min, tmp); } } else if (ret == 0) { printf("No reply received within the timeout period\n"); @@ -186,14 +225,10 @@ int main(int argc, char** argv) { } pause(); - - if (stop) { - break; - } } printf("---- %s ping statistics ----\n", arguments.args[0]); printf("%zu packets transmitted, %zu packets received, %zu%% packet loss\n", xmit, recvd, (xmit - recvd) / xmit * 100); - // printf("round-trip min/avg/max/stddev = %f/%f/%f/%f ms\n"); + printf("round-trip min/avg/max/stddev = %d.%d/%d.%d/%%.3f/%%.3f ms\n", min.ms_top, min.ms_bottom, max.ms_top, max.ms_bottom); close(sock); } \ No newline at end of file