show ping response lines and stats lines

This commit is contained in:
2025-05-02 01:17:18 +02:00
parent b4afb26076
commit e3a2901370
8 changed files with 233 additions and 117 deletions

View File

@ -6,7 +6,7 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/24 22:49:22 by tomoron #+# #+# */
/* Updated: 2025/04/30 17:53:19 by tomoron ### ########.fr */
/* Updated: 2025/05/01 20:19:41 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include "includes/ft_ping.h"
@ -62,3 +62,45 @@ t_icmp_echo prepare_icmp_echo(uint16_t sequence, uint16_t identifier)
return(packet);
}
t_waitlist *send_icmp(t_settings *set, struct addrinfo *host, uint16_t *seq, struct timeval *last, t_stats *stats)
{
t_icmp_echo packet;
t_waitlist *ret;
if(timediff(last) < set->interval)
return (0);
packet = prepare_icmp_echo(*seq, set->id);
sendto(set->socket, &packet, sizeof(t_icmp_echo), 0, host->ai_addr, host->ai_addrlen);
gettimeofday(last, 0);
ret = malloc(sizeof(t_waitlist));
if(!ret)
return(0);
ret->time = *last;
ret->seq = *seq;
ret->next = 0;
stats->sent++;
(*seq)++;
return(ret);
}
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;
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)
return;
if(res->type != 0 && res->identifier != set->id)
return ;
checksum = res->checksum;
res->checksum = 0;
if(calc_checksum(res, sizeof(t_icmp_echo)) != checksum)
return ;
res->sequence = htons(res->sequence);
waitlist_remove(wl, res->sequence, set, stats);
}