receive of icmp work, needs statistics and missing settings
This commit is contained in:
2
Makefile
2
Makefile
@ -28,6 +28,6 @@ clean:
|
|||||||
rm -rf $(OBJS_DIR)
|
rm -rf $(OBJS_DIR)
|
||||||
|
|
||||||
fclean: clean
|
fclean: clean
|
||||||
rm $(NAME)
|
rm -f $(NAME)
|
||||||
|
|
||||||
.PHONY: clean fclean
|
.PHONY: clean fclean
|
||||||
|
@ -6,13 +6,13 @@
|
|||||||
/* 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/04/30 01:57:10 by tomoron ### ########.fr */
|
/* Updated: 2025/04/30 17:59:21 by tomoron ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "includes/ft_ping.h"
|
#include "includes/ft_ping.h"
|
||||||
|
|
||||||
struct addrinfo *resolve_ip(char *host)
|
struct addrinfo *resolve_ip(char *host, t_settings *settings)
|
||||||
{
|
{
|
||||||
struct addrinfo hints, *result;
|
struct addrinfo hints, *result;
|
||||||
int s;
|
int s;
|
||||||
@ -25,7 +25,7 @@ struct addrinfo *resolve_ip(char *host)
|
|||||||
|
|
||||||
s = getaddrinfo(host, NULL, &hints, &result);
|
s = getaddrinfo(host, NULL, &hints, &result);
|
||||||
if (s != 0) {
|
if (s != 0) {
|
||||||
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
|
fprintf(stderr, "%s: %s\n",settings->name, gai_strerror(s));
|
||||||
return(ret);
|
return(ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -49,22 +49,22 @@ double timediff(struct timeval *from)
|
|||||||
return(diff);
|
return(diff);
|
||||||
}
|
}
|
||||||
|
|
||||||
t_waitlist *send_icmp(t_settings *set, struct addrinfo *host, int seq, struct timeval *last)
|
t_waitlist *send_icmp(t_settings *set, struct addrinfo *host, uint16_t *seq, struct timeval *last)
|
||||||
{
|
{
|
||||||
t_icmp_echo packet;
|
t_icmp_echo packet;
|
||||||
t_waitlist *ret;
|
t_waitlist *ret;
|
||||||
|
|
||||||
if(timediff(last) < set->interval)
|
if(timediff(last) < set->interval)
|
||||||
return (0);
|
return (0);
|
||||||
printf("send packet\n");
|
packet = prepare_icmp_echo(*seq, set->id);
|
||||||
packet = prepare_icmp_echo(seq, set->id);
|
|
||||||
sendto(set->socket, &packet, sizeof(t_icmp_echo), 0, host->ai_addr, host->ai_addrlen);
|
sendto(set->socket, &packet, sizeof(t_icmp_echo), 0, host->ai_addr, host->ai_addrlen);
|
||||||
gettimeofday(last, 0);
|
gettimeofday(last, 0);
|
||||||
|
(*seq)++;
|
||||||
ret = malloc(sizeof(t_waitlist));
|
ret = malloc(sizeof(t_waitlist));
|
||||||
if(!ret)
|
if(!ret)
|
||||||
return(0);
|
return(0);
|
||||||
ret->time = *last;
|
ret->time = *last;
|
||||||
ret->seq = seq;
|
ret->seq = *seq;
|
||||||
ret->next = 0;
|
ret->next = 0;
|
||||||
return(ret);
|
return(ret);
|
||||||
}
|
}
|
||||||
@ -83,7 +83,7 @@ void waitlist_add(t_waitlist **waitlist, t_waitlist *elem)
|
|||||||
*waitlist = elem;
|
*waitlist = elem;
|
||||||
}
|
}
|
||||||
|
|
||||||
void whitelist_free(t_waitlist *lst)
|
void waitlist_free(t_waitlist *lst)
|
||||||
{
|
{
|
||||||
t_waitlist *tmp;
|
t_waitlist *tmp;
|
||||||
|
|
||||||
@ -95,24 +95,47 @@ void whitelist_free(t_waitlist *lst)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
void receive_icmp(t_settings *set, struct addrinfo *info, t_waitlist **wl)
|
||||||
{
|
{
|
||||||
size_t len;
|
size_t len;
|
||||||
t_icmp_echo packet;
|
char buf[256];
|
||||||
t_waitlist *tmp;
|
t_icmp_echo *res;
|
||||||
|
uint16_t checksum;
|
||||||
|
|
||||||
len = recvfrom(set->socket, &packet, sizeof(t_icmp_echo), 0, info->ai_addr, &info->ai_addrlen);
|
len = recvfrom(set->socket, buf, sizeof(t_icmp_echo) + 20, 0, info->ai_addr, &info->ai_addrlen);
|
||||||
if(len == -1 || !len)
|
res = (void*)(buf + 20);
|
||||||
|
if(len == (size_t)-1 || !len)
|
||||||
return;
|
return;
|
||||||
tmp = *wl;
|
if(res->type != 0 && res->identifier != set->id)
|
||||||
while(tmp && tmp->seq != packet.sequence)
|
return ;
|
||||||
tmp = tmp->next;
|
checksum = res->checksum;
|
||||||
if(tmp)
|
res->checksum = 0;
|
||||||
{
|
if(calc_checksum(res, sizeof(t_icmp_echo)) != checksum)
|
||||||
free(tmp);
|
return ;
|
||||||
}
|
waitlist_remove(wl, res->sequence);
|
||||||
(void)len;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int ping_host(t_settings *set, char *host)
|
int ping_host(t_settings *set, char *host)
|
||||||
@ -124,7 +147,7 @@ int ping_host(t_settings *set, char *host)
|
|||||||
t_waitlist *tmp;
|
t_waitlist *tmp;
|
||||||
|
|
||||||
seq = 0;
|
seq = 0;
|
||||||
info = resolve_ip(host);
|
info = resolve_ip(host, set);
|
||||||
if(!info)
|
if(!info)
|
||||||
return(0);
|
return(0);
|
||||||
last_sent.tv_sec = 0;
|
last_sent.tv_sec = 0;
|
||||||
@ -132,7 +155,7 @@ int ping_host(t_settings *set, char *host)
|
|||||||
wl = 0;
|
wl = 0;
|
||||||
while(seq < set->count || set->count == -1)
|
while(seq < set->count || set->count == -1)
|
||||||
{
|
{
|
||||||
tmp = send_icmp(set, info, seq, &last_sent);
|
tmp = send_icmp(set, info, &seq, &last_sent);
|
||||||
if(tmp)
|
if(tmp)
|
||||||
waitlist_add(&wl, tmp);
|
waitlist_add(&wl, tmp);
|
||||||
usleep(1000);
|
usleep(1000);
|
||||||
@ -141,7 +164,7 @@ int ping_host(t_settings *set, char *host)
|
|||||||
while(wl)
|
while(wl)
|
||||||
receive_icmp(set, info, &wl);
|
receive_icmp(set, info, &wl);
|
||||||
freeaddrinfo(info);
|
freeaddrinfo(info);
|
||||||
whitelist_free(wl);
|
waitlist_free(wl);
|
||||||
return(1);
|
return(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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/04/29 19:09:11 by tomoron ### ########.fr */
|
/* Updated: 2025/04/30 17:53:19 by tomoron ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
#include "includes/ft_ping.h"
|
#include "includes/ft_ping.h"
|
||||||
@ -23,7 +23,7 @@ static void set_packet_data(uint8_t *packet_data)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint16_t calc_checksum(void *ptr, size_t len)
|
uint16_t calc_checksum(void *ptr, size_t len)
|
||||||
{
|
{
|
||||||
uint32_t res;
|
uint32_t res;
|
||||||
uint16_t *data;
|
uint16_t *data;
|
||||||
|
@ -56,6 +56,7 @@ typedef struct s_settings
|
|||||||
uint16_t id;
|
uint16_t id;
|
||||||
short stop;
|
short stop;
|
||||||
short err;
|
short err;
|
||||||
|
char *name;
|
||||||
|
|
||||||
} t_settings;
|
} t_settings;
|
||||||
|
|
||||||
@ -77,5 +78,6 @@ void free_hosts(t_host_list *list);
|
|||||||
t_settings parse_args(int argc, char **argv);
|
t_settings parse_args(int argc, char **argv);
|
||||||
void show_help(t_settings *set, char *name);
|
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);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
|
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/04/24 00:03:56 by tomoron #+# #+# */
|
/* Created: 2025/04/24 00:03:56 by tomoron #+# #+# */
|
||||||
/* Updated: 2025/04/29 18:39:49 by tomoron ### ########.fr */
|
/* Updated: 2025/04/30 16:48:45 by tomoron ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -54,6 +54,7 @@ int main(int argc, char **argv)
|
|||||||
settings = parse_args(argc, argv);
|
settings = parse_args(argc, argv);
|
||||||
settings.socket = init_socket(argv[0]);
|
settings.socket = init_socket(argv[0]);
|
||||||
settings.id = get_id();
|
settings.id = get_id();
|
||||||
|
settings.name = argv[0];
|
||||||
if(settings.stop || settings.err || settings.socket == -1)
|
if(settings.stop || settings.err || settings.socket == -1)
|
||||||
{
|
{
|
||||||
free_hosts(settings.hosts);
|
free_hosts(settings.hosts);
|
||||||
|
Reference in New Issue
Block a user