pasring start

This commit is contained in:
2025-04-25 19:35:48 +02:00
parent e622d9d175
commit 77668368ab
4 changed files with 59 additions and 16 deletions

View File

@ -6,24 +6,60 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View File

@ -6,7 +6,7 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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));

View File

@ -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;