add ttl error when verbose is active
This commit is contained in:
@ -6,7 +6,7 @@
|
|||||||
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
|
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/04/26 19:54:25 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);
|
printf("--- %s ping statistics ---\n", host);
|
||||||
percent = ((double)(stats->sent - stats->received) / stats->sent) * 100.0;
|
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("%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)
|
int ping_host(t_settings *set, char *host)
|
||||||
@ -88,10 +89,10 @@ int ping_host(t_settings *set, char *host)
|
|||||||
if(tmp)
|
if(tmp)
|
||||||
waitlist_add(&wl, tmp);
|
waitlist_add(&wl, tmp);
|
||||||
usleep(100);
|
usleep(100);
|
||||||
receive_icmp(set, info, &wl, &stats);
|
receive_icmp(set, &wl, &stats);
|
||||||
}
|
}
|
||||||
while(wl && !g_stop && timediff(&set->last_send_time) < set->linger)
|
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);
|
ping_end_print(set, host, &stats);
|
||||||
freeaddrinfo(info);
|
freeaddrinfo(info);
|
||||||
waitlist_free(wl);
|
waitlist_free(wl);
|
||||||
|
92
srcs/icmp.c
92
srcs/icmp.c
@ -6,7 +6,7 @@
|
|||||||
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
|
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/04/24 22:49:22 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"
|
#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);
|
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;
|
struct in_addr addr = { .s_addr = ip };
|
||||||
t_icmp_ip_reply res;
|
return(inet_ntoa(addr));
|
||||||
uint16_t checksum;
|
}
|
||||||
|
|
||||||
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))
|
ip_str = convert_ip(res->header.saddr);
|
||||||
return;
|
(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 )
|
if(len == (size_t)-1 )
|
||||||
return;
|
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 ;
|
return ;
|
||||||
checksum = res.icmp.checksum;
|
elem = waitlist_remove(wl, res, res->icmp.type == 11);
|
||||||
res.icmp.checksum = 0;
|
if(!elem)
|
||||||
if(calc_checksum(&res.icmp, sizeof(t_icmp_echo)) != checksum)
|
return;
|
||||||
return ;
|
if(res->icmp.type == 0)
|
||||||
res.icmp.sequence = htons(res.icmp.sequence);
|
show_ping_res_line(elem, set, res->icmp.sequence, stats, res->header.ttl);
|
||||||
waitlist_remove(wl, res.icmp.sequence, set, stats, res.header.ttl);
|
else if (res->icmp.type == 11 && set->verbose)
|
||||||
|
ttl_exceeded(res, set, len);
|
||||||
|
free(elem);
|
||||||
}
|
}
|
||||||
|
@ -107,11 +107,11 @@ void show_help(t_settings *set, char *name);
|
|||||||
void send_pings(t_settings *set);
|
void send_pings(t_settings *set);
|
||||||
uint16_t calc_checksum(void *ptr, size_t len);
|
uint16_t calc_checksum(void *ptr, size_t len);
|
||||||
double timediff(struct timeval *from);
|
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 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);
|
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 *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 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
|
#endif
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
|
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/05/01 17:38:58 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 *prev;
|
||||||
t_waitlist *cur;
|
t_waitlist *cur;
|
||||||
|
|
||||||
prev = 0;
|
prev = 0;
|
||||||
cur = *lst;
|
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;
|
prev = cur;
|
||||||
cur = cur->next;
|
cur = cur->next;
|
||||||
}
|
}
|
||||||
if(!cur)
|
if(!cur)
|
||||||
return ;
|
return (0);
|
||||||
if(!prev)
|
if(!prev)
|
||||||
*lst = cur->next;
|
*lst = cur->next;
|
||||||
else
|
else
|
||||||
prev->next = cur->next;
|
prev->next = cur->next;
|
||||||
show_ping_res_line(cur, set, seq, stats, ttl);
|
return(cur);
|
||||||
free(cur);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user