72 lines
2.0 KiB
C
72 lines
2.0 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* main.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2025/04/24 00:03:56 by tomoron #+# #+# */
|
|
/* Updated: 2025/04/30 16:48:45 by tomoron ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "includes/ft_ping.h"
|
|
|
|
void show_help(t_settings *set, char *name)
|
|
{
|
|
set->stop = 1;
|
|
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);
|
|
settings.socket = init_socket(argv[0]);
|
|
settings.id = get_id();
|
|
settings.name = argv[0];
|
|
if(settings.stop || settings.err || settings.socket == -1)
|
|
{
|
|
free_hosts(settings.hosts);
|
|
return((settings.err != 0) * 64);
|
|
}
|
|
if(!settings.hosts)
|
|
{
|
|
fprintf(stderr, "%s: missing host operand\n", argv[0]);
|
|
return(64);
|
|
}
|
|
send_pings(&settings);
|
|
free_hosts(settings.hosts);
|
|
return(0);
|
|
}
|