add ttl error when verbose is active

This commit is contained in:
2025-06-03 00:43:19 +02:00
parent c2a00ab4b3
commit 71ec3ea4d2
4 changed files with 93 additions and 29 deletions

View File

@ -6,7 +6,7 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}