From 7db547d71164aca16653988599ed21e274aa8dcb Mon Sep 17 00:00:00 2001 From: Jolan Rathelot Date: Tue, 1 Apr 2025 07:53:06 -0400 Subject: [PATCH] added avg, using floats instead of fixed points for min/max/avg/stddev --- Makefile | 2 +- includes/main.h | 5 ----- srcs/main.c | 60 +++++++++++++------------------------------------ 3 files changed, 17 insertions(+), 50 deletions(-) diff --git a/Makefile b/Makefile index b8f79b9..931b725 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ # Project Meta Settings NAME = ft_ping -RELEASE_TYPE = Debug +RELEASE_TYPE = Release CURRENT_DIR = ${CURDIR} # Project Build Output Settings diff --git a/includes/main.h b/includes/main.h index 6e1333c..c65cf60 100644 --- a/includes/main.h +++ b/includes/main.h @@ -28,9 +28,4 @@ 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 f86b404..6589b6c 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -25,7 +25,7 @@ static error_t parse_opt(int key, char* arg, struct argp_state* state) { break; case ARG_TTL: arguments->ttl = strtoul(arg, NULL, 10); - __attribute__((fallthrough)); + break; case ARGP_KEY_ARG: if (state->arg_num >= 2) argp_usage(state); @@ -108,45 +108,18 @@ 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; + float min; + float max; + float avg; - min.ms_bottom = INT32_MAX; - min.ms_top = INT32_MAX; - - max.ms_bottom = 0; - max.ms_top = 0; + min = INT32_MAX; + max = 0; + avg = 0; arguments.verbose = 0; arguments.ttl = 64; @@ -205,18 +178,17 @@ 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)); - struct ping_delta tmp; - - tmp.ms_top = + float tmp = ((recvt.tv_sec * 1000 + recvt.tv_nsec / 1000000) - (sendt.tv_sec * 1000 + sendt.tv_nsec / 1000000)); - tmp.ms_bottom = ((recvt.tv_nsec - sendt.tv_nsec) % 1000000 / 1000); + tmp += ((float) ((recvt.tv_nsec - sendt.tv_nsec) % 1000000) / 1000000.0); - 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, tmp.ms_top, tmp.ms_bottom); + printf("%zd bytes from %s: icmp_seq=%d ttl=%d time=%.3f ms \n", bytes_received, arguments.args[0], + icmp_reply->un.echo.sequence, ip_header->ip_ttl, tmp); + + min = (((min) < (tmp)) ? (min) : (tmp)); + max = (((max) > (tmp)) ? (max) : (tmp)); + avg += tmp; recvd++; - - max = get_max(max, tmp); - min = get_min(min, tmp); } } else if (ret == 0) { printf("No reply received within the timeout period\n"); @@ -229,6 +201,6 @@ int main(int argc, char** argv) { 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 = %d.%d/%d.%d/%%.3f/%%.3f ms\n", min.ms_top, min.ms_bottom, max.ms_top, max.ms_bottom); + printf("round-trip min/avg/max/stddev = %.3f/%.3f/%.3f/%%.3f ms\n", min, max, avg / recvd); close(sock); } \ No newline at end of file