diff --git a/Makefile b/Makefile index 171ab70..5f374da 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ NAME = ft_ping CC=cc -FLAGS=-Werror -Wextra -Wall -g +FLAGS=-Werror -Wextra -Wall -g -Wno-unused-result SRCS_DIR = srcs OBJS_DIR = .objs diff --git a/srcs/ft_ping.c b/srcs/ft_ping.c index ae8e65b..cf7cbcd 100644 --- a/srcs/ft_ping.c +++ b/srcs/ft_ping.c @@ -6,24 +6,60 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/24 00:03:56 by tomoron #+# #+# */ -/* Updated: 2025/04/24 22:50:08 by tomoron ### ########.fr */ +/* Updated: 2025/04/25 19:29:48 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ #include "includes/ft_ping.h" +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)); +} + +void parse_opt(int *i, t_settings *set, int argc, char **argv) +{ + (void)argc; + if(!strcmp(argv[*i], "-?") || !strcmp(argv[*i], "--help")) + show_help(set, *argv); + if(!strcmp(argv[*i], "-c") || !strcmp(argv[*i], "--count")) + get_setting(i, &set->count, argc, argv); + if(!strcmp(argv[*i], "-w") || !strcmp(argv[*i], "--timeout")) + get_setting(i, &set->timeout, argc, argv); + if(!strcmp(argv[*i], "-f") || !strcmp(argv[*i], "--flood")) + set->flood = 1; + if(!strcmp(argv[*i], "-n") || !strcmp(argv[*i], "--numeric")) + set->no_resolve = 1; + + (*i)++; +} + +t_settings parse_args(int argc, char **argv) +{ + int i; + t_settings res; + + i = 1; + bzero(&res, sizeof(t_settings)); + while(i < argc) + { + if(*argv[i] == '-') + parse_opt(&i, &res, argc, argv); + + } + return(res); +} int main(int argc, char **argv) { - (void)argc; - (void)argv; - t_icmp_echo packet; - int fd; + t_settings settings; - packet = prepare_icmp_echo(0, 1); - fd = open("test", O_WRONLY | O_CREAT, 0700); - write(fd, &packet, sizeof(t_icmp_echo)); - printf("checksum : %x\n", packet.checksum); + settings = parse_args(argc, argv); + if(settings.stop || settings.err) + return((settings.err != 0) * 64); return(0); } diff --git a/srcs/icmp.c b/srcs/icmp.c index fa09e49..a054af6 100644 --- a/srcs/icmp.c +++ b/srcs/icmp.c @@ -6,7 +6,7 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/24 22:49:22 by tomoron #+# #+# */ -/* Updated: 2025/04/24 22:52:09 by tomoron ### ########.fr */ +/* Updated: 2025/04/25 19:14:34 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ #include "includes/ft_ping.h" @@ -16,7 +16,6 @@ static void set_packet_data(uint8_t *packet_data) int i; i = 0; - while(i < PACKET_DATA_LENGTH) { packet_data[i] = i; @@ -57,8 +56,6 @@ t_icmp_echo prepare_icmp_echo(uint16_t sequence, uint16_t identifier) packet.sequence = htons(sequence); set_packet_data(packet.data); gettimeofday(&packet.time, 0); - packet.time.tv_sec = bswap_64(packet.time.tv_sec); - packet.time.tv_usec = bswap_64(packet.time.tv_usec); packet.checksum = 0; packet.checksum = calc_checksum(&packet, sizeof(t_icmp_echo)); diff --git a/srcs/includes/ft_ping.h b/srcs/includes/ft_ping.h index 4b82f87..67eb2a0 100644 --- a/srcs/includes/ft_ping.h +++ b/srcs/includes/ft_ping.h @@ -13,12 +13,22 @@ #define PACKET_DATA_LENGTH 40 +#define HELP_MESSAGE " [OPTION...] HOST ...\n\ +Send ICMP ECHO_REQUEST packets to network hosts.\n" + typedef struct s_settings { char *host; - int count; - int ttl; + int count; // -c (--count) + int timeout; // -w (--timeout) + short flood; // -f (--flood) + short no_resolve; //-n (--numeric) + int ttl; // --ttl + + short stop; + short err; + } t_settings;