diff --git a/Makefile b/Makefile index 03a2493..7a6d15a 100644 --- a/Makefile +++ b/Makefile @@ -8,7 +8,8 @@ OBJS_DIR = .objs SRCS = main.c\ icmp.c\ parsing.c\ - utils.c + utils.c\ + ft_ping.c OBJS = $(addprefix $(OBJS_DIR)/,$(SRCS:.c=.o)) diff --git a/srcs/ft_ping.c b/srcs/ft_ping.c new file mode 100644 index 0000000..dfade7a --- /dev/null +++ b/srcs/ft_ping.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ping.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/04/26 19:54:25 by tomoron #+# #+# */ +/* Updated: 2025/04/29 01:43:38 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "includes/ft_ping.h" + +void send_icmp(t_settings set, char *host) +{ + (void)set; + (void)host; + printf("would send to %s\n", host); +} + +void ping_host(t_settings set, char *host) +{ + int i; + + i = 0; + while(i < set.count || set.count == -1) + { + send_icmp(set, host); + usleep(set.interval * 1000 * 1000); + i++; + } +} + +void send_pings(t_settings set) +{ + t_host_list *cur; + + cur = set.hosts; + while(cur) + { + ping_host(set, cur->host); + cur = cur->next; + } +} diff --git a/srcs/includes/ft_ping.h b/srcs/includes/ft_ping.h index dab31c1..0368020 100644 --- a/srcs/includes/ft_ping.h +++ b/srcs/includes/ft_ping.h @@ -38,7 +38,7 @@ typedef struct s_settings t_host_list *hosts; int count; // -c (--count) int timeout; // -w (--timeout) - short flood; // -f (--flood) + double interval; //-i (--interval) short no_resolve; //-n (--numeric) int ttl; // --ttl short setTtl; // is ttl set @@ -65,5 +65,6 @@ int add_host(t_settings *res, char *host); void free_hosts(t_host_list *list); t_settings parse_args(int argc, char **argv); void show_help(t_settings *set, char *name); +void send_pings(t_settings set); #endif diff --git a/srcs/parsing.c b/srcs/parsing.c index 3e39576..8c5e8ee 100644 --- a/srcs/parsing.c +++ b/srcs/parsing.c @@ -6,7 +6,7 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/26 17:01:53 by tomoron #+# #+# */ -/* Updated: 2025/04/26 17:08:01 by tomoron ### ########.fr */ +/* Updated: 2025/04/29 02:08:34 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ #include "includes/ft_ping.h" @@ -24,10 +24,9 @@ char *get_value_in_arg(char *arg, int end) return(res); } -int get_setting(int *i, int argc, char **argv, int end) +char *get_setting(int *i, int argc, char **argv, int end, t_settings *set) { char *value; - char *tmp; value = 0; if(argv[*i][0] == '-' && argv[*i][1] == '-') @@ -37,14 +36,27 @@ int get_setting(int *i, int argc, char **argv, int end) if(!value) { fprintf(stderr, "%s: option '%s' requires an option\n", argv[0], argv[*i]); + set->err = 1; return(0); } + return(value); +} + +int get_set_int(int *i, int argc, char **argv, int end, t_settings *set) +{ + char *value; + char *tmp; + + value = get_setting(i, argc, argv, end, set); + if(!value) + return(0); tmp = value; while(*tmp) { if(!isdigit(*tmp)) { fprintf(stderr, "%s: invalid value (`%s' near `%s')\n", argv[0], value, tmp); + set->err = 1; return(0); } tmp++; @@ -52,21 +64,47 @@ int get_setting(int *i, int argc, char **argv, int end) return(atoi(value)); } +double get_set_float(int *i, int argc, char **argv, int end, t_settings *set) +{ + char *value; + char *tmp; + int point; + + value = get_setting(i, argc, argv, end, set); + if(!value) + return(0); + tmp = value; + point = 0; + while(*tmp) + { + if((!isdigit(*tmp) && *tmp != '.') || (*tmp == '.' && point)) + { + fprintf(stderr, "%s: invalid value (`%s' near `%s')\n", argv[0], value, tmp); + set->err = 1; + return(0); + } + if(*tmp == '.') + point = 1; + tmp++; + } + return(atof(value)); +} + void parse_opt(int *i, t_settings *set, int argc, char **argv) { if(!strcmp(argv[*i], "-?") || !strcmp(argv[*i], "--help")) show_help(set, *argv); else if(!strcmp(argv[*i], "-c") || !strncmp(argv[*i], "--count", 7)) - set->count = get_setting(i, argc, argv, 7); + set->count = get_set_int(i, argc, argv, 7, set); else if(!strcmp(argv[*i], "-w") || !strncmp(argv[*i], "--timeout", 9)) - set->timeout = get_setting(i, argc, argv, 9); + set->timeout = get_set_int(i, argc, argv, 9, set); + else if(!strcmp(argv[*i], "-i") || !strncmp(argv[*i], "--interval", 10)) + set->interval = get_set_float(i, argc, argv, 10, set); else if(!strncmp(argv[*i], "--ttl", 5)) { - set->ttl = get_setting(i, argc, argv, 5); + set->ttl = get_set_int(i, argc, argv, 5, set); set->setTtl = 1; } - else if(!strcmp(argv[*i], "-f") || !strcmp(argv[*i], "--flood")) - set->flood = 1; else if(!strcmp(argv[*i], "-n") || !strcmp(argv[*i], "--numeric")) set->no_resolve = 1; else @@ -103,6 +141,7 @@ t_settings parse_args(int argc, char **argv) bzero(&res, sizeof(t_settings)); res.count = -1; res.timeout = -1; + res.interval = 1; while(i < argc) { if(*argv[i] == '-') @@ -117,7 +156,8 @@ t_settings parse_args(int argc, char **argv) } i++; } - check_values(&res, argv[0]); + if(!res.err) + res.err = !check_values(&res, argv[0]); return(res); }