show ping response lines and stats lines
This commit is contained in:
131
srcs/ft_ping.c
131
srcs/ft_ping.c
@ -6,7 +6,7 @@
|
||||
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/26 19:54:25 by tomoron #+# #+# */
|
||||
/* Updated: 2025/04/30 17:59:21 by tomoron ### ########.fr */
|
||||
/* Updated: 2025/05/02 01:04:29 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -38,131 +38,54 @@ struct addrinfo *resolve_ip(char *host, t_settings *settings)
|
||||
return(ret);
|
||||
}
|
||||
|
||||
double timediff(struct timeval *from)
|
||||
void ping_start_print(t_settings *set, char *host)
|
||||
{
|
||||
struct timeval now;
|
||||
float diff;
|
||||
|
||||
gettimeofday(&now, 0);
|
||||
diff = ((double)now.tv_sec - from->tv_sec);
|
||||
diff += (((double)now.tv_usec / 1000000) - ((double)from->tv_usec / 1000000));
|
||||
return(diff);
|
||||
printf("PING %s (%s): 56 data bytes\n", host, set->current_ip);
|
||||
}
|
||||
|
||||
t_waitlist *send_icmp(t_settings *set, struct addrinfo *host, uint16_t *seq, struct timeval *last)
|
||||
void ping_end_print(t_settings *set, char *host, 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);
|
||||
(*seq)++;
|
||||
ret = malloc(sizeof(t_waitlist));
|
||||
if(!ret)
|
||||
return(0);
|
||||
ret->time = *last;
|
||||
ret->seq = *seq;
|
||||
ret->next = 0;
|
||||
return(ret);
|
||||
}
|
||||
int percent;
|
||||
double stddev;
|
||||
|
||||
void waitlist_add(t_waitlist **waitlist, t_waitlist *elem)
|
||||
{
|
||||
t_waitlist *cur;
|
||||
|
||||
cur = *waitlist;
|
||||
|
||||
while(cur && cur->next)
|
||||
cur = cur->next;
|
||||
if(cur)
|
||||
cur->next = elem;
|
||||
else
|
||||
*waitlist = elem;
|
||||
}
|
||||
|
||||
void waitlist_free(t_waitlist *lst)
|
||||
{
|
||||
t_waitlist *tmp;
|
||||
|
||||
while(lst)
|
||||
{
|
||||
tmp = lst->next;
|
||||
free(lst);
|
||||
lst = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
void waitlist_remove(t_waitlist **lst, uint16_t seq)
|
||||
{
|
||||
t_waitlist *cur;
|
||||
t_waitlist *prev;
|
||||
|
||||
if(!*lst)
|
||||
return ;
|
||||
cur = *lst;
|
||||
prev = 0;
|
||||
while(cur && cur->seq != seq)
|
||||
{
|
||||
prev = cur;
|
||||
cur = cur->next;
|
||||
}
|
||||
if(!prev)
|
||||
return ;
|
||||
if(!prev && *lst)
|
||||
*lst = (*lst)->next;
|
||||
if(cur)
|
||||
prev->next = cur->next;
|
||||
free(cur);
|
||||
}
|
||||
|
||||
void receive_icmp(t_settings *set, struct addrinfo *info, t_waitlist **wl)
|
||||
{
|
||||
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)
|
||||
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 ;
|
||||
waitlist_remove(wl, res->sequence);
|
||||
(void)set;
|
||||
stddev = sqrt(stats->sqr_diff / (stats->received - 1));
|
||||
printf("--- %s ping statistics ---\n", host);
|
||||
percent = ((double)stats->sent / 100) * stats->received;
|
||||
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);
|
||||
}
|
||||
|
||||
int ping_host(t_settings *set, char *host)
|
||||
{
|
||||
uint16_t seq;
|
||||
struct addrinfo *info;
|
||||
struct timeval last_sent;
|
||||
t_waitlist *wl;
|
||||
t_waitlist *tmp;
|
||||
struct addrinfo *info;
|
||||
struct timeval last_sent;
|
||||
t_waitlist *wl;
|
||||
t_waitlist *tmp;
|
||||
t_stats stats;
|
||||
|
||||
seq = 0;
|
||||
memset(&stats, 0, sizeof(t_stats));
|
||||
info = resolve_ip(host, set);
|
||||
if(!info)
|
||||
return(0);
|
||||
last_sent.tv_sec = 0;
|
||||
last_sent.tv_usec = 0;
|
||||
wl = 0;
|
||||
while(seq < set->count || set->count == -1)
|
||||
inet_ntop(info->ai_family, (void *)&((struct sockaddr_in *)(info->ai_addr))->sin_addr, set->current_ip, sizeof(set->current_ip));
|
||||
ping_start_print(set, host);
|
||||
while((seq < set->count || set->count == -1) && !g_stop)
|
||||
{
|
||||
tmp = send_icmp(set, info, &seq, &last_sent);
|
||||
tmp = send_icmp(set, info, &seq, &last_sent, &stats);
|
||||
if(tmp)
|
||||
waitlist_add(&wl, tmp);
|
||||
usleep(1000);
|
||||
receive_icmp(set, info, &wl);
|
||||
usleep(100);
|
||||
receive_icmp(set, info, &wl, &stats);
|
||||
}
|
||||
while(wl)
|
||||
receive_icmp(set, info, &wl);
|
||||
while(wl && !g_stop)
|
||||
receive_icmp(set, info, &wl, &stats);
|
||||
ping_end_print(set, host, &stats);
|
||||
freeaddrinfo(info);
|
||||
waitlist_free(wl);
|
||||
return(1);
|
||||
|
Reference in New Issue
Block a user