check ttl value and reverse list order

This commit is contained in:
2025-04-25 23:26:08 +02:00
parent 149ab9e725
commit 621b9fc4ee
2 changed files with 43 additions and 5 deletions

View File

@ -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/25 23:05:43 by tomoron ### ########.fr */ /* Updated: 2025/04/25 23:25:43 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -70,7 +70,10 @@ void parse_opt(int *i, t_settings *set, int argc, char **argv)
else if(!strcmp(argv[*i], "-w") || !strncmp(argv[*i], "--timeout", 9)) else if(!strcmp(argv[*i], "-w") || !strncmp(argv[*i], "--timeout", 9))
set->timeout = get_setting(i, argc, argv, 9); set->timeout = get_setting(i, argc, argv, 9);
else if(!strncmp(argv[*i], "--ttl", 5)) else if(!strncmp(argv[*i], "--ttl", 5))
{
set->ttl = get_setting(i, argc, argv, 5); set->ttl = get_setting(i, argc, argv, 5);
set->setTtl = 1;
}
else if(!strcmp(argv[*i], "-f") || !strcmp(argv[*i], "--flood")) else if(!strcmp(argv[*i], "-f") || !strcmp(argv[*i], "--flood"))
set->flood = 1; set->flood = 1;
else if(!strcmp(argv[*i], "-n") || !strcmp(argv[*i], "--numeric")) else if(!strcmp(argv[*i], "-n") || !strcmp(argv[*i], "--numeric"))
@ -80,18 +83,24 @@ void parse_opt(int *i, t_settings *set, int argc, char **argv)
fprintf(stderr, "%s: unrecognized option '%s'\n", argv[0], argv[*i]); fprintf(stderr, "%s: unrecognized option '%s'\n", argv[0], argv[*i]);
set->err = 1; set->err = 1;
} }
(*i)++;
} }
int add_host(t_settings *res, char *host) int add_host(t_settings *res, char *host)
{ {
t_host_list *new; t_host_list *new;
t_host_list *tmp;
new = malloc(sizeof(t_host_list)); new = malloc(sizeof(t_host_list));
if(!new) if(!new)
return(0); return(0);
new->host = host; new->host = host;
new->next = res->hosts; new->next = 0;
tmp = res->hosts;
while(tmp && tmp->next)
tmp = tmp->next;
if(tmp)
tmp->next = new;
else
res->hosts = new; res->hosts = new;
return(1); return(1);
} }
@ -102,12 +111,37 @@ void free_hosts(t_host_list *list)
while(list) while(list)
{ {
printf("free : %s\n", list->host);
tmp = list->next; tmp = list->next;
free(list); free(list);
list = tmp; list = tmp;
} }
} }
void init_defaults(t_settings *set)
{
set->count = -1;
set->timeout = -1;
}
int check_values(t_settings *set, char *name)
{
if(set->setTtl)
{
if(set->ttl == 0)
{
fprintf(stderr, "%s: option value too small: %d\n", name, set->ttl);
return(0);
}
else if (set->ttl >= 256)
{
fprintf(stderr, "%s: option value too big: %d\n", name, set->ttl);
return(0);
}
}
return(1);
}
t_settings parse_args(int argc, char **argv) t_settings parse_args(int argc, char **argv)
{ {
int i; int i;
@ -115,6 +149,7 @@ t_settings parse_args(int argc, char **argv)
i = 1; i = 1;
bzero(&res, sizeof(t_settings)); bzero(&res, sizeof(t_settings));
init_defaults(&res);
while(i < argc) while(i < argc)
{ {
if(*argv[i] == '-') if(*argv[i] == '-')
@ -127,7 +162,9 @@ t_settings parse_args(int argc, char **argv)
return(res); return(res);
} }
} }
i++;
} }
check_values(&res, argv[0]);
return(res); return(res);
} }
@ -146,6 +183,6 @@ int main(int argc, char **argv)
fprintf(stderr, "%s: missing host operand\n", argv[0]); fprintf(stderr, "%s: missing host operand\n", argv[0]);
return(64); return(64);
} }
free_hosts(settings.hosts);
return(0); return(0);
} }

View File

@ -41,6 +41,7 @@ typedef struct s_settings
short flood; // -f (--flood) short flood; // -f (--flood)
short no_resolve; //-n (--numeric) short no_resolve; //-n (--numeric)
int ttl; // --ttl int ttl; // --ttl
short setTtl; // is ttl set
short stop; short stop;
short err; short err;