icmp echo can be sent and reply is sent but not yet handled
This commit is contained in:
42
srcs/main.c
42
srcs/main.c
@ -1,12 +1,12 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_ping.c :+: :+: :+: */
|
||||
/* main.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/24 00:03:56 by tomoron #+# #+# */
|
||||
/* Updated: 2025/04/26 17:08:50 by tomoron ### ########.fr */
|
||||
/* Updated: 2025/04/29 18:39:49 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -15,18 +15,46 @@
|
||||
void show_help(t_settings *set, char *name)
|
||||
{
|
||||
set->stop = 1;
|
||||
write(1, "Usage: ", 7);
|
||||
write(1, name, strlen(name));
|
||||
write(1, HELP_MESSAGE, sizeof(HELP_MESSAGE));
|
||||
printf("Usage: %s %s", name, HELP_MESSAGE);
|
||||
}
|
||||
|
||||
int init_socket(char *name)
|
||||
{
|
||||
int res;
|
||||
|
||||
res = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
|
||||
if(res == -1)
|
||||
{
|
||||
if(errno == EPERM)
|
||||
fprintf(stderr, "%s: socket creation permission denied, make sure you are runing as root\n", name);
|
||||
else
|
||||
fprintf(stderr, "%s: can't initialize socket\n", name);
|
||||
return(-1);
|
||||
}
|
||||
fcntl(res, F_SETFL, O_NONBLOCK);
|
||||
return(res);
|
||||
}
|
||||
|
||||
uint16_t get_id(void)
|
||||
{
|
||||
struct timeval time;
|
||||
uint16_t res;
|
||||
|
||||
gettimeofday(&time,0);
|
||||
res = (uint8_t)time.tv_sec;
|
||||
res |= (uint8_t)time.tv_usec << 8;
|
||||
return(res);
|
||||
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
t_settings settings;
|
||||
|
||||
settings = parse_args(argc, argv);
|
||||
if(settings.stop || settings.err)
|
||||
settings.socket = init_socket(argv[0]);
|
||||
settings.id = get_id();
|
||||
if(settings.stop || settings.err || settings.socket == -1)
|
||||
{
|
||||
free_hosts(settings.hosts);
|
||||
return((settings.err != 0) * 64);
|
||||
@ -36,7 +64,7 @@ int main(int argc, char **argv)
|
||||
fprintf(stderr, "%s: missing host operand\n", argv[0]);
|
||||
return(64);
|
||||
}
|
||||
send_pings(settings);
|
||||
send_pings(&settings);
|
||||
free_hosts(settings.hosts);
|
||||
return(0);
|
||||
}
|
||||
|
Reference in New Issue
Block a user