From b4b66a2bb94e351fc29b691eaff95c0bb90f9cc6 Mon Sep 17 00:00:00 2001 From: adjoly Date: Sun, 17 Aug 2025 23:17:00 +0200 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=F0=9F=94=A8=E3=80=8D=20fix:=20fixed?= =?UTF-8?q?=20some=20things.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/ping.h | 1 + src/main.c | 1 + src/ping/send_ping.c | 59 ++++++++++++++++++++++++-------------------- src/utils.c | 6 +++-- 4 files changed, 38 insertions(+), 29 deletions(-) diff --git a/includes/ping.h b/includes/ping.h index a7f31ac..9c105d3 100644 --- a/includes/ping.h +++ b/includes/ping.h @@ -17,6 +17,7 @@ extern int rx_count; extern char *address; extern double *times; extern int sock; +extern char **hosts; extern bool verbose; typedef struct { diff --git a/src/main.c b/src/main.c index dea86c1..c669320 100644 --- a/src/main.c +++ b/src/main.c @@ -11,6 +11,7 @@ double *times = NULL; int rx_count = 0; int tx_count = 0; bool verbose = DEFAULT_VERBOSE; +char **hosts; void init_args_t(args_t *args) { OPT_WHILE { diff --git a/src/ping/send_ping.c b/src/ping/send_ping.c index b196dc4..b5d5df1 100644 --- a/src/ping/send_ping.c +++ b/src/ping/send_ping.c @@ -28,11 +28,15 @@ void sigint(int sig) { close(sock); print_stats(); + // free(times); + // free(hosts); + exit(EXIT_SUCCESS); } options_t init_opt(args_t *args) { options_t opt; + hosts = args->hosts; verbose = args->opts[VERBOSE] != false ? true : DEFAULT_VERBOSE; opt.count = args->arg[COUNT] != -1 ? args->arg[COUNT] : DEFAULT_COUNT; if (args->arg[SIZE] > sizeof(struct icmphdr) || args->arg[SIZE] == -1) { @@ -84,12 +88,16 @@ int send_ping(int socket, struct sockaddr_in *dest, options_t opt) { process_icmp(recvbuf, bytes, &addr, seq, &pkt_start, &pkt_end); if (check_for_count(opt)) { print_stats(); + free(times); + times = NULL; return EXIT_SUCCESS; } sleep(opt.interval); if (check_for_timeout(loop_start, opt)) { print_stats(); + free(times); + times = NULL; return EXIT_SUCCESS; } } @@ -100,35 +108,32 @@ int ping(args_t *args) { int ret; options_t opt = init_opt(args); signal(SIGINT, &sigint); - for (int i = 0; args->hosts[i] != NULL; i++) { - address = args->hosts[i]; - struct sockaddr_in addr; - struct addrinfo hints, *res; + address = args->hosts[0]; + struct sockaddr_in addr; + struct addrinfo hints, *res; - memset(&hints, 0, sizeof(hints)); - hints.ai_family = AF_INET; - hints.ai_socktype = SOCK_RAW; - hints.ai_protocol = IPPROTO_ICMP; + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_INET; + hints.ai_socktype = SOCK_RAW; + hints.ai_protocol = IPPROTO_ICMP; - int status = getaddrinfo(address, NULL, &hints, &res); - if (status != 0) { - fprintf(stderr, "Bad address: %s\n", address); - ret = EXIT_FAILURE; - continue; - } - - memcpy(&addr, res->ai_addr, res->ai_addrlen); - freeaddrinfo(res); - - sock = init_socket(opt); - if (sock < 0) { - break; - } - - printf("PING %s (%s): %d data bytes\n", address, - inet_ntoa(addr.sin_addr), opt.size); - - ret = send_ping(sock, &addr, opt); + int status = getaddrinfo(address, NULL, &hints, &res); + if (status != 0) { + fprintf(stderr, "Bad address: %s\n", address); + ret = EXIT_FAILURE; } + + memcpy(&addr, res->ai_addr, res->ai_addrlen); + freeaddrinfo(res); + + sock = init_socket(opt); + if (sock < 0) { + return -1; + } + + printf("PING %s (%s): %d data bytes\n", address, inet_ntoa(addr.sin_addr), + opt.size); + + ret = send_ping(sock, &addr, opt); return ret; } diff --git a/src/utils.c b/src/utils.c index bbb2727..a592b2c 100644 --- a/src/utils.c +++ b/src/utils.c @@ -61,8 +61,9 @@ bool check_for_timeout(struct timeval start, options_t opt) { double span = (end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec) / 1000000.0; - if (span > opt.timeout) + if (span > opt.timeout) { return true; + } return false; } @@ -77,7 +78,8 @@ void print_stats(void) { bool check_for_count(options_t opt) { if (opt.count == -1) return false; - if (tx_count >= opt.count) + if (tx_count >= opt.count) { return true; + } return false; }