fix ping loss, add ttl from ip header compile .h file to detect changes
This commit is contained in:
10
Makefile
10
Makefile
@ -1,8 +1,9 @@
|
|||||||
NAME = ft_ping
|
NAME = ft_ping
|
||||||
CC=cc
|
CC=cc
|
||||||
FLAGS=-Werror -Wextra -Wall -g -Wno-unused-result
|
FLAGS=-Werror -Wextra -Wall -g -Wno-unused-result -Qunused-arguments
|
||||||
|
|
||||||
SRCS_DIR = srcs
|
SRCS_DIR = srcs
|
||||||
|
INCLUDES_DIR = srcs/includes
|
||||||
OBJS_DIR = .objs
|
OBJS_DIR = .objs
|
||||||
|
|
||||||
SRCS = main.c\
|
SRCS = main.c\
|
||||||
@ -13,7 +14,10 @@ SRCS = main.c\
|
|||||||
stats.c\
|
stats.c\
|
||||||
waitlist_utils.c
|
waitlist_utils.c
|
||||||
|
|
||||||
OBJS = $(addprefix $(OBJS_DIR)/,$(SRCS:.c=.o))
|
INCLUDES = ft_ping.h
|
||||||
|
|
||||||
|
OBJS = $(addprefix $(OBJS_DIR)/,$(SRCS:.c=.o))\
|
||||||
|
$(addprefix $(OBJS_DIR)/,$(INCLUDES:.h=.pch))
|
||||||
|
|
||||||
all: $(NAME)
|
all: $(NAME)
|
||||||
|
|
||||||
@ -25,6 +29,8 @@ $(OBJS_DIR):
|
|||||||
|
|
||||||
$(OBJS_DIR)/%.o: $(SRCS_DIR)/%.c
|
$(OBJS_DIR)/%.o: $(SRCS_DIR)/%.c
|
||||||
$(CC) $(FLAGS) -c $< -o $@
|
$(CC) $(FLAGS) -c $< -o $@
|
||||||
|
$(OBJS_DIR)/%.pch: $(INCLUDES_DIR)/%.h
|
||||||
|
$(CC) $(FLAGS) -c $< -o $@
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -rf $(OBJS_DIR)
|
rm -rf $(OBJS_DIR)
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
|
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/04/26 19:54:25 by tomoron #+# #+# */
|
/* Created: 2025/04/26 19:54:25 by tomoron #+# #+# */
|
||||||
/* Updated: 2025/05/02 23:55:50 by tomoron ### ########.fr */
|
/* Updated: 2025/05/22 15:24:43 by tomoron ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -53,7 +53,7 @@ void ping_end_print(t_settings *set, char *host, t_stats *stats)
|
|||||||
if(isnan(stddev) || isinf(stddev))
|
if(isnan(stddev) || isinf(stddev))
|
||||||
stddev = 0.0f;
|
stddev = 0.0f;
|
||||||
printf("--- %s ping statistics ---\n", host);
|
printf("--- %s ping statistics ---\n", host);
|
||||||
percent = ((double)stats->sent / 100) * stats->received;
|
percent = ((double)(stats->sent - stats->received) / stats->sent) * 100.0;
|
||||||
printf("%lu packets transmitted, %lu packets received, %d%% packet loss\n", stats->sent, stats->received, percent);
|
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);
|
printf("round-trip min/avg/max/stddev = %.3f/%.3f/%.3f/%.3f ms\n", stats->min, stats->avg, stats->max, stddev);
|
||||||
}
|
}
|
||||||
|
25
srcs/icmp.c
25
srcs/icmp.c
@ -6,7 +6,7 @@
|
|||||||
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
|
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/04/24 22:49:22 by tomoron #+# #+# */
|
/* 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"
|
#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)
|
void receive_icmp(t_settings *set, struct addrinfo *info, t_waitlist **wl, t_stats *stats)
|
||||||
{
|
{
|
||||||
size_t len;
|
size_t len;
|
||||||
char buf[256];
|
t_icmp_ip_reply res;
|
||||||
t_icmp_echo *res;
|
|
||||||
uint16_t checksum;
|
uint16_t checksum;
|
||||||
|
|
||||||
len = recvfrom(set->socket, buf, sizeof(t_icmp_echo) + 20, 0, info->ai_addr, &info->ai_addrlen);
|
len = recvfrom(set->socket, &res, sizeof(t_icmp_ip_reply), 0, info->ai_addr, &info->ai_addrlen);
|
||||||
res = (void*)(buf + 20);
|
|
||||||
if(len == (size_t)-1 || len < 84)
|
if(len < sizeof(t_icmp_ip_reply))
|
||||||
return;
|
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 ;
|
return ;
|
||||||
checksum = res->checksum;
|
checksum = res.icmp.checksum;
|
||||||
res->checksum = 0;
|
res.icmp.checksum = 0;
|
||||||
if(calc_checksum(res, sizeof(t_icmp_echo)) != checksum)
|
if(calc_checksum(&res.icmp, sizeof(t_icmp_echo)) != checksum)
|
||||||
return ;
|
return ;
|
||||||
res->sequence = htons(res->sequence);
|
res.icmp.sequence = htons(res.icmp.sequence);
|
||||||
waitlist_remove(wl, res->sequence, set, stats);
|
waitlist_remove(wl, res.icmp.sequence, set, stats, res.header.ttl);
|
||||||
}
|
}
|
||||||
|
@ -15,6 +15,7 @@
|
|||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <sys/param.h>
|
#include <sys/param.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
#include <netinet/ip.h>
|
||||||
|
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
|
||||||
@ -23,13 +24,13 @@
|
|||||||
#define HELP_MESSAGE " [OPTION...] HOST ...\n\
|
#define HELP_MESSAGE " [OPTION...] HOST ...\n\
|
||||||
Send ICMP ECHO_REQUEST packets to network hosts.\n\
|
Send ICMP ECHO_REQUEST packets to network hosts.\n\
|
||||||
\n\
|
\n\
|
||||||
-c, --count=N stop after sending NUMBER packets\n\
|
-c, --count=N Stop after sending NUMBER packets\n\
|
||||||
--ttl=N specify N as time-to-live\n\
|
--ttl=N Specify N as time-to-live\n\
|
||||||
-w, --timeout=N stop after N seconds\n\
|
-i, --interval=N Wait N seconds between sending each packet\n\
|
||||||
-i, --interval=N wait N seconds between sending each packet\n\
|
-w, --timeout=N Stop after N seconds\n\
|
||||||
-W, --linger=N number of seconds to wait for response\n\
|
-W, --linger=N Number of seconds to wait for response\n\
|
||||||
\n\
|
\n\
|
||||||
-?, --help give this help list\n"
|
-?, --help Give this help list\n"
|
||||||
|
|
||||||
extern int g_stop;
|
extern int g_stop;
|
||||||
|
|
||||||
@ -63,7 +64,6 @@ typedef struct s_settings
|
|||||||
char *name;
|
char *name;
|
||||||
|
|
||||||
char current_ip[INET_ADDRSTRLEN];
|
char current_ip[INET_ADDRSTRLEN];
|
||||||
|
|
||||||
} t_settings;
|
} t_settings;
|
||||||
|
|
||||||
|
|
||||||
@ -78,6 +78,12 @@ typedef struct s_icmp_echo
|
|||||||
uint8_t data[40];
|
uint8_t data[40];
|
||||||
} t_icmp_echo;
|
} t_icmp_echo;
|
||||||
|
|
||||||
|
typedef struct __attribute__((__packed__))
|
||||||
|
{
|
||||||
|
struct iphdr header;
|
||||||
|
t_icmp_echo icmp;
|
||||||
|
} t_icmp_ip_reply;
|
||||||
|
|
||||||
typedef struct s_stats
|
typedef struct s_stats
|
||||||
{
|
{
|
||||||
size_t sent;
|
size_t sent;
|
||||||
@ -99,8 +105,8 @@ void show_help(t_settings *set, char *name);
|
|||||||
void send_pings(t_settings *set);
|
void send_pings(t_settings *set);
|
||||||
uint16_t calc_checksum(void *ptr, size_t len);
|
uint16_t calc_checksum(void *ptr, size_t len);
|
||||||
double timediff(struct timeval *from);
|
double timediff(struct timeval *from);
|
||||||
void waitlist_remove(t_waitlist **lst, uint16_t seq, t_settings *set, t_stats *stats);
|
void waitlist_remove(t_waitlist **lst, uint16_t seq, t_settings *set, t_stats *stats, uint8_t ttl);
|
||||||
void show_ping_res_line(t_waitlist *cur, t_settings *set, uint16_t seq, t_stats *stats);
|
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);
|
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, struct timeval *last, t_stats *stats);
|
||||||
void waitlist_add(t_waitlist **waitlist, t_waitlist *elem);
|
void waitlist_add(t_waitlist **waitlist, t_waitlist *elem);
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
|
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/05/01 17:41:42 by tomoron #+# #+# */
|
/* Created: 2025/05/01 17:41:42 by tomoron #+# #+# */
|
||||||
/* Updated: 2025/05/02 23:53:28 by tomoron ### ########.fr */
|
/* Updated: 2025/05/22 15:28:03 by tomoron ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -28,12 +28,12 @@ void update_stats(t_stats *stats, double time)
|
|||||||
stats->prev = time;
|
stats->prev = time;
|
||||||
}
|
}
|
||||||
|
|
||||||
void show_ping_res_line(t_waitlist *cur, t_settings *set, uint16_t seq, t_stats *stats)
|
void show_ping_res_line(t_waitlist *cur, t_settings *set, uint16_t seq, t_stats *stats, uint8_t ttl)
|
||||||
{
|
{
|
||||||
double diff;
|
double diff;
|
||||||
|
|
||||||
diff = timediff(&cur->time) * 1000;
|
diff = timediff(&cur->time) * 1000;
|
||||||
update_stats(stats, diff);
|
update_stats(stats, diff);
|
||||||
printf("%d bytes from %s: icmp_seq=%d ttl=??? time=%.3f ms\n", 64, set->current_ip, seq, diff);
|
printf("%d bytes from %s: icmp_seq=%d ttl=%u time=%.3f ms\n", 64, set->current_ip, seq, ttl, diff);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
|
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/05/01 17:38:58 by tomoron #+# #+# */
|
/* Created: 2025/05/01 17:38:58 by tomoron #+# #+# */
|
||||||
/* Updated: 2025/05/02 01:06:21 by tomoron ### ########.fr */
|
/* Updated: 2025/05/22 15:25:38 by tomoron ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -38,7 +38,7 @@ void waitlist_free(t_waitlist *lst)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void waitlist_remove(t_waitlist **lst, uint16_t seq, t_settings *set, t_stats *stats)
|
void waitlist_remove(t_waitlist **lst, uint16_t seq, t_settings *set, t_stats *stats, uint8_t ttl)
|
||||||
{
|
{
|
||||||
t_waitlist *prev;
|
t_waitlist *prev;
|
||||||
t_waitlist *cur;
|
t_waitlist *cur;
|
||||||
@ -57,7 +57,7 @@ void waitlist_remove(t_waitlist **lst, uint16_t seq, t_settings *set, t_stats *s
|
|||||||
*lst = cur->next;
|
*lst = cur->next;
|
||||||
else
|
else
|
||||||
prev->next = cur->next;
|
prev->next = cur->next;
|
||||||
show_ping_res_line(cur, set, seq, stats);
|
show_ping_res_line(cur, set, seq, stats, ttl);
|
||||||
free(cur);
|
free(cur);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user