From 9e2016eff54f871a71d9f99e7bd6f36987323c80 Mon Sep 17 00:00:00 2001 From: Namonay Date: Mon, 16 Sep 2024 20:45:09 +0200 Subject: [PATCH] print at end of program --- ft_ping.h | 13 +------------ main.c | 23 +++++++++++++++++++---- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/ft_ping.h b/ft_ping.h index 8f0a409..88b5bf6 100644 --- a/ft_ping.h +++ b/ft_ping.h @@ -1,18 +1,14 @@ #pragma once -#include -#include #include #include #include -#include +#include #include #include #include -#include -#include #include #include #include @@ -25,10 +21,3 @@ struct icmp_header uint16_t id; uint16_t seq; }; - -struct t_socket -{ - struct sockaddr_in dst, src; - int fd; - int len; -}; diff --git a/main.c b/main.c index 50fe2e8..d8b3f2c 100644 --- a/main.c +++ b/main.c @@ -1,5 +1,10 @@ #include "ft_ping.h" +int n_packet_sent = 0; +int n_packet_recv = 0; +char *ip; +char loop = 1; + uint16_t calculate_checksum(uint16_t *data, int len) { uint32_t checksum = 0; @@ -76,15 +81,23 @@ char *get_ip_by_hostname(char *hostname) return (inet_ntoa(*addr_list[0])); } +void handler(int code) +{ + (void)code; + loop = 0; +} + +void init_signal() +{ + signal(SIGINT, handler); +} + int main(int argc, char **argv) { int sock; struct sockaddr_in dst; int seq = 1; - char *ip; double start; - int n_packet_sent = 0; - int n_packet_recv = 0; if (argc != 2 || argv[1] == NULL || argv[1][0] == 0) { @@ -105,7 +118,7 @@ int main(int argc, char **argv) fprintf(stderr, "ERROR : socket() failed\n"); return (0); } - while (1) + while (loop) { start = get_timestamp(); ft_ping(sock, seq, dst); @@ -113,6 +126,8 @@ int main(int argc, char **argv) seq++; sleep(1); } + printf("--- %s ping statistics ---\n", ip); + printf("%d packed transmitted %d received, %5.1f%% packet loss", n_packet_sent, n_packet_recv, (double)(n_packet_sent / n_packet_sent) * 100); close(sock); return (0); }