fix ping loss, add ttl from ip header compile .h file to detect changes

This commit is contained in:
2025-05-22 15:30:34 +02:00
parent b96652f9a4
commit 94085f5bb2
6 changed files with 44 additions and 31 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/01 20:19:41 by tomoron ### ########.fr */
/* Updated: 2025/05/22 15:26:40 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include "includes/ft_ping.h"
@ -87,20 +87,21 @@ t_waitlist *send_icmp(t_settings *set, struct addrinfo *host, uint16_t *seq, str
void receive_icmp(t_settings *set, struct addrinfo *info, t_waitlist **wl, t_stats *stats)
{
size_t len;
char buf[256];
t_icmp_echo *res;
t_icmp_ip_reply res;
uint16_t checksum;
len = recvfrom(set->socket, buf, sizeof(t_icmp_echo) + 20, 0, info->ai_addr, &info->ai_addrlen);
res = (void*)(buf + 20);
if(len == (size_t)-1 || len < 84)
len = recvfrom(set->socket, &res, sizeof(t_icmp_ip_reply), 0, info->ai_addr, &info->ai_addrlen);
if(len < sizeof(t_icmp_ip_reply))
return;
if(res->type != 0 && res->identifier != set->id)
if(len == (size_t)-1 )
return;
if(res.icmp.type != 0 && res.icmp.identifier != set->id)
return ;
checksum = res->checksum;
res->checksum = 0;
if(calc_checksum(res, sizeof(t_icmp_echo)) != checksum)
checksum = res.icmp.checksum;
res.icmp.checksum = 0;
if(calc_checksum(&res.icmp, sizeof(t_icmp_echo)) != checksum)
return ;
res->sequence = htons(res->sequence);
waitlist_remove(wl, res->sequence, set, stats);
res.icmp.sequence = htons(res.icmp.sequence);
waitlist_remove(wl, res.icmp.sequence, set, stats, res.header.ttl);
}