diff --git a/srcs/ft_ping.c b/srcs/ft_ping.c index 7195c29..3332e24 100644 --- a/srcs/ft_ping.c +++ b/srcs/ft_ping.c @@ -6,7 +6,7 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/26 19:54:25 by tomoron #+# #+# */ -/* Updated: 2025/06/01 22:09:27 by tomoron ### ########.fr */ +/* Updated: 2025/06/02 22:10:42 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -60,7 +60,8 @@ void ping_end_print(t_settings *set, char *host, t_stats *stats) printf("--- %s ping statistics ---\n", host); percent = ((double)(stats->sent - stats->received) / stats->sent) * 100.0; printf("%lu packets transmitted, %lu packets received, %d%% packet loss\n", stats->sent, stats->received, percent); - printf("round-trip min/avg/max/stddev = %.3f/%.3f/%.3f/%.3f ms\n", stats->min, stats->avg, stats->max, stddev); + if(stats->received) + printf("round-trip min/avg/max/stddev = %.3f/%.3f/%.3f/%.3f ms\n", stats->min, stats->avg, stats->max, stddev); } int ping_host(t_settings *set, char *host) @@ -88,10 +89,10 @@ int ping_host(t_settings *set, char *host) if(tmp) waitlist_add(&wl, tmp); usleep(100); - receive_icmp(set, info, &wl, &stats); + receive_icmp(set, &wl, &stats); } while(wl && !g_stop && timediff(&set->last_send_time) < set->linger) - receive_icmp(set, info, &wl, &stats); + receive_icmp(set, &wl, &stats); ping_end_print(set, host, &stats); freeaddrinfo(info); waitlist_free(wl); diff --git a/srcs/icmp.c b/srcs/icmp.c index 463db30..9336bd3 100644 --- a/srcs/icmp.c +++ b/srcs/icmp.c @@ -6,7 +6,7 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/24 22:49:22 by tomoron #+# #+# */ -/* Updated: 2025/05/22 22:19:36 by tomoron ### ########.fr */ +/* Updated: 2025/06/03 00:40:54 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ #include "includes/ft_ping.h" @@ -92,24 +92,86 @@ t_waitlist *send_icmp(t_settings *set, struct addrinfo *host, uint16_t *seq, str return(ret); } -void receive_icmp(t_settings *set, struct addrinfo *info, t_waitlist **wl, t_stats *stats) +char *convert_ip(uint32_t ip) { - size_t len; - t_icmp_ip_reply res; - uint16_t checksum; + struct in_addr addr = { .s_addr = ip }; + return(inet_ntoa(addr)); +} - len = recvfrom(set->socket, &res, sizeof(t_icmp_ip_reply), 0, info->ai_addr, &info->ai_addrlen); +void ttl_exceeded(t_icmp_ip_reply *res,t_settings *settings, size_t len) +{ + char *ip_str; + t_icmp_ip_reply *sent; - if(len < sizeof(t_icmp_ip_reply)) - return; + ip_str = convert_ip(res->header.saddr); + (void) settings; + sent = (void *)res + sizeof(struct iphdr) + 8; + if(htons(sent->icmp.identifier) != settings->id) + { + printf("invalid identifier\n"); + return ; + } +//ICMP: type 8, code 0, size 64, id 0x01cd, seq 0x0000 + printf("%zu bytes from %s: Time to live exceeded\n", len, ip_str); + printf("IP Hdr Dump:\n"); + for (size_t i = 0; i < sizeof(struct iphdr); i += 2) + { + printf(" %04x", *(uint16_t *)(((void *)(&sent->header)) + i)); + } + printf("\nVr HL TOS Len ID Flg off TTL Pro cks Src Dst Data\n"); + printf(" %d %d %02x %04x %04x %01x %04x %02x %02x %02x %s",\ + sent->header.version, sent->header.ihl, sent->header.tos,\ + htons(sent->header.tot_len), htons(sent->header.id), sent->header.frag_off, sent->header.frag_off,\ + sent->header.ttl, sent->header.protocol, htons(sent->header.check),\ + convert_ip(sent->header.saddr)); + printf(" %s\nICMP: ", convert_ip(sent->header.daddr)); + printf("type %d, code %d, size %zu, id 0x%04x, seq 0x%04x\n", sent->icmp.type, sent->icmp.code, sent->header.tot_len - sizeof(struct iphdr), sent->icmp.identifier, sent->icmp.sequence); + +} + +void receive_icmp(t_settings *set, t_waitlist **wl, t_stats *stats) +{ + size_t len; + t_icmp_ip_reply *res; + char buffer[1024]; + uint16_t checksum; + t_waitlist *elem; + + len = recvfrom(set->socket, &buffer, 1024, 0, 0, 0); + + res = (void *)buffer; if(len == (size_t)-1 ) return; - if(res.icmp.type != 0 && res.icmp.identifier != set->id) + if(len < sizeof(struct iphdr) + 8) + return; + if(res->header.protocol != 1) + return ; + if(htons(res->header.tot_len) != len) + return; + if(res->icmp.type == 11) + { + if(len < (sizeof(struct iphdr) + 8) * 2) + return; + } + else if(res->icmp.type == 0) + { + if(len < sizeof(t_icmp_ip_reply)) + return; + if(htons(res->icmp.identifier) != set->id) + return; + } + else + return; + checksum = res->icmp.checksum; + res->icmp.checksum = 0; + if(calc_checksum(&res->icmp, len - sizeof(struct iphdr)) != checksum) return ; - checksum = res.icmp.checksum; - res.icmp.checksum = 0; - if(calc_checksum(&res.icmp, sizeof(t_icmp_echo)) != checksum) - return ; - res.icmp.sequence = htons(res.icmp.sequence); - waitlist_remove(wl, res.icmp.sequence, set, stats, res.header.ttl); + elem = waitlist_remove(wl, res, res->icmp.type == 11); + if(!elem) + return; + if(res->icmp.type == 0) + show_ping_res_line(elem, set, res->icmp.sequence, stats, res->header.ttl); + else if (res->icmp.type == 11 && set->verbose) + ttl_exceeded(res, set, len); + free(elem); } diff --git a/srcs/includes/ft_ping.h b/srcs/includes/ft_ping.h index 8f24fc0..442b3c5 100644 --- a/srcs/includes/ft_ping.h +++ b/srcs/includes/ft_ping.h @@ -107,11 +107,11 @@ void show_help(t_settings *set, char *name); void send_pings(t_settings *set); uint16_t calc_checksum(void *ptr, size_t len); double timediff(struct timeval *from); -void waitlist_remove(t_waitlist **lst, uint16_t seq, t_settings *set, t_stats *stats, uint8_t ttl); void show_ping_res_line(t_waitlist *cur, t_settings *set, uint16_t seq, t_stats *stats, uint8_t ttl); void waitlist_free(t_waitlist *lst); t_waitlist *send_icmp(t_settings *set, struct addrinfo *host, uint16_t *seq, struct timeval *last, t_stats *stats); +t_waitlist *waitlist_remove(t_waitlist **lst, t_icmp_ip_reply *res, int is_ttl_exceeded); void waitlist_add(t_waitlist **waitlist, t_waitlist *elem); -void receive_icmp(t_settings *set, struct addrinfo *info, t_waitlist **wl, t_stats *stats); +void receive_icmp(t_settings *set, t_waitlist **wl, t_stats *stats); #endif diff --git a/srcs/waitlist_utils.c b/srcs/waitlist_utils.c index 8fcb3b5..d731194 100644 --- a/srcs/waitlist_utils.c +++ b/srcs/waitlist_utils.c @@ -6,7 +6,7 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/05/01 17:38:58 by tomoron #+# #+# */ -/* Updated: 2025/05/22 15:25:38 by tomoron ### ########.fr */ +/* Updated: 2025/06/03 00:27:30 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -38,26 +38,27 @@ void waitlist_free(t_waitlist *lst) } } -void waitlist_remove(t_waitlist **lst, uint16_t seq, t_settings *set, t_stats *stats, uint8_t ttl) +t_waitlist *waitlist_remove(t_waitlist **lst, t_icmp_ip_reply *res, int is_ttl_exceeded) { t_waitlist *prev; t_waitlist *cur; prev = 0; cur = *lst; - while(cur && cur->seq != seq) + if(is_ttl_exceeded) + res = (void *)res + sizeof(struct iphdr) + 8; + res->icmp.sequence = htons(res->icmp.sequence); + while(cur && cur->seq != res->icmp.sequence) { - printf("%d, %d\n", cur->seq, seq); + printf("%d, %d\n", cur->seq, res->icmp.sequence); prev = cur; cur = cur->next; } if(!cur) - return ; + return (0); if(!prev) *lst = cur->next; else prev->next = cur->next; - show_ping_res_line(cur, set, seq, stats, ttl); - free(cur); + return(cur); } -