make timeout work, add setcap makefile rule
This commit is contained in:
3
Makefile
3
Makefile
@ -34,6 +34,9 @@ $(OBJS_DIR)/%.o: $(SRCS_DIR)/%.c
|
||||
$(OBJS_DIR)/%.pch: $(INCLUDES_DIR)/%.h
|
||||
$(CC) $(FLAGS) -c $< -o $@
|
||||
|
||||
setcap: $(NAME)
|
||||
sudo setcap cap_net_raw+ep $(NAME)
|
||||
|
||||
clean:
|
||||
rm -rf $(OBJS_DIR)
|
||||
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/26 19:54:25 by tomoron #+# #+# */
|
||||
/* Updated: 2025/08/22 15:45:44 by tomoron ### ########.fr */
|
||||
/* Updated: 2025/08/24 19:44:50 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -67,11 +67,18 @@ void ping_end_print(t_settings *set, char *host, t_stats *stats)
|
||||
printf("round-trip min/avg/max/stddev = %.3f/%.3f/%.3f/%.3f ms\n", stats->min, stats->avg, stats->max, stddev);
|
||||
}
|
||||
|
||||
__attribute__((always_inline))
|
||||
int timeout_check(t_settings *set)
|
||||
{
|
||||
if (set->timeout == -1)
|
||||
return (1);
|
||||
return (timediff(&set->start_time) - 0.001 <= set->timeout);
|
||||
}
|
||||
|
||||
int ping_host(t_settings *set, char *host)
|
||||
{
|
||||
uint16_t seq;
|
||||
struct addrinfo *info;
|
||||
struct timeval last_sent;
|
||||
t_waitlist *wl;
|
||||
t_waitlist *tmp;
|
||||
t_stats stats;
|
||||
@ -81,20 +88,20 @@ int ping_host(t_settings *set, char *host)
|
||||
info = resolve_ip(host, set);
|
||||
if(!info)
|
||||
return(0);
|
||||
last_sent.tv_sec = 0;
|
||||
last_sent.tv_usec = 0;
|
||||
wl = 0;
|
||||
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);
|
||||
ping_start_print(set, host);
|
||||
while ((seq < set->count || set->count == 0) && !g_stop)
|
||||
{
|
||||
tmp = send_icmp(set, info, &seq, &last_sent, &stats);
|
||||
tmp = send_icmp(set, info, &seq, &stats);
|
||||
if(tmp)
|
||||
waitlist_add(&wl, tmp);
|
||||
usleep(100);
|
||||
if (!timeout_check(set))
|
||||
break ;
|
||||
usleep(10);
|
||||
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 && timeout_check(set))
|
||||
receive_icmp(set, &wl, &stats);
|
||||
ping_end_print(set, host, &stats);
|
||||
freeaddrinfo(info);
|
||||
@ -107,6 +114,7 @@ void send_pings(t_settings *set)
|
||||
t_host_list *cur;
|
||||
|
||||
cur = set->hosts;
|
||||
gettimeofday(&set->start_time, 0);
|
||||
if(set->setTtl)
|
||||
setsockopt(set->socket, IPPROTO_IP, IP_TTL, &set->ttl, sizeof(set->ttl));
|
||||
while(cur)
|
||||
|
10
srcs/icmp.c
10
srcs/icmp.c
@ -6,7 +6,7 @@
|
||||
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/24 22:49:22 by tomoron #+# #+# */
|
||||
/* Updated: 2025/08/13 16:35:29 by tomoron ### ########.fr */
|
||||
/* Updated: 2025/08/24 17:08:44 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "includes/ft_ping.h"
|
||||
@ -63,14 +63,15 @@ 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_waitlist *send_icmp(t_settings *set, struct addrinfo *host, uint16_t *seq, t_stats *stats)
|
||||
{
|
||||
t_icmp_echo packet;
|
||||
t_waitlist *ret;
|
||||
size_t len;
|
||||
|
||||
if(timediff(last) < set->interval)
|
||||
if(timediff(&set->last_send_time) < set->interval)
|
||||
return (0);
|
||||
gettimeofday(&set->last_send_time, 0);
|
||||
packet = prepare_icmp_echo(*seq, set->id);
|
||||
len = sendto(set->socket, &packet, sizeof(t_icmp_echo), MSG_DONTWAIT, host->ai_addr, host->ai_addrlen);
|
||||
if(len == (size_t)-1)
|
||||
@ -79,11 +80,10 @@ t_waitlist *send_icmp(t_settings *set, struct addrinfo *host, uint16_t *seq, str
|
||||
set->err = 1;
|
||||
g_stop = 1;
|
||||
}
|
||||
gettimeofday(last, 0);
|
||||
ret = malloc(sizeof(t_waitlist));
|
||||
if(!ret)
|
||||
return(0);
|
||||
ret->time = *last;
|
||||
ret->time = set->last_send_time;
|
||||
ret->seq = *seq;
|
||||
ret->next = 0;
|
||||
stats->sent++;
|
||||
|
@ -53,7 +53,7 @@ typedef struct s_settings
|
||||
{
|
||||
t_host_list *hosts;
|
||||
int count; // -c (--count)
|
||||
int timeout; // -w (--timeout)
|
||||
double timeout; // -w (--timeout)
|
||||
double interval; //-i (--interval)
|
||||
int linger; //-W (--linger)
|
||||
int ttl; // --ttl
|
||||
@ -66,6 +66,7 @@ typedef struct s_settings
|
||||
short err;
|
||||
char *name;
|
||||
struct timeval last_send_time;
|
||||
struct timeval start_time;
|
||||
|
||||
char current_ip[INET_ADDRSTRLEN];
|
||||
} t_settings;
|
||||
@ -111,7 +112,7 @@ uint16_t calc_checksum(void *ptr, size_t len);
|
||||
double timediff(struct timeval *from);
|
||||
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);
|
||||
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, 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 receive_icmp(t_settings *set, t_waitlist **wl, t_stats *stats);
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/26 17:01:53 by tomoron #+# #+# */
|
||||
/* Updated: 2025/08/20 17:10:02 by tomoron ### ########.fr */
|
||||
/* Updated: 2025/08/24 16:43:35 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "includes/ft_ping.h"
|
||||
|
Reference in New Issue
Block a user