pasring start
This commit is contained in:
2
Makefile
2
Makefile
@ -1,6 +1,6 @@
|
|||||||
NAME = ft_ping
|
NAME = ft_ping
|
||||||
CC=cc
|
CC=cc
|
||||||
FLAGS=-Werror -Wextra -Wall -g
|
FLAGS=-Werror -Wextra -Wall -g -Wno-unused-result
|
||||||
|
|
||||||
SRCS_DIR = srcs
|
SRCS_DIR = srcs
|
||||||
OBJS_DIR = .objs
|
OBJS_DIR = .objs
|
||||||
|
@ -6,24 +6,60 @@
|
|||||||
/* 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/24 22:50:08 by tomoron ### ########.fr */
|
/* Updated: 2025/04/25 19:29:48 by tomoron ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "includes/ft_ping.h"
|
#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)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
(void)argc;
|
t_settings settings;
|
||||||
(void)argv;
|
|
||||||
t_icmp_echo packet;
|
|
||||||
int fd;
|
|
||||||
|
|
||||||
packet = prepare_icmp_echo(0, 1);
|
settings = parse_args(argc, argv);
|
||||||
fd = open("test", O_WRONLY | O_CREAT, 0700);
|
if(settings.stop || settings.err)
|
||||||
write(fd, &packet, sizeof(t_icmp_echo));
|
return((settings.err != 0) * 64);
|
||||||
printf("checksum : %x\n", packet.checksum);
|
|
||||||
|
|
||||||
return(0);
|
return(0);
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
|
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/04/24 22:49:22 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"
|
#include "includes/ft_ping.h"
|
||||||
@ -16,7 +16,6 @@ static void set_packet_data(uint8_t *packet_data)
|
|||||||
int i;
|
int i;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
|
|
||||||
while(i < PACKET_DATA_LENGTH)
|
while(i < PACKET_DATA_LENGTH)
|
||||||
{
|
{
|
||||||
packet_data[i] = i;
|
packet_data[i] = i;
|
||||||
@ -57,8 +56,6 @@ t_icmp_echo prepare_icmp_echo(uint16_t sequence, uint16_t identifier)
|
|||||||
packet.sequence = htons(sequence);
|
packet.sequence = htons(sequence);
|
||||||
set_packet_data(packet.data);
|
set_packet_data(packet.data);
|
||||||
gettimeofday(&packet.time, 0);
|
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 = 0;
|
||||||
packet.checksum = calc_checksum(&packet, sizeof(t_icmp_echo));
|
packet.checksum = calc_checksum(&packet, sizeof(t_icmp_echo));
|
||||||
|
@ -13,12 +13,22 @@
|
|||||||
|
|
||||||
#define PACKET_DATA_LENGTH 40
|
#define PACKET_DATA_LENGTH 40
|
||||||
|
|
||||||
|
#define HELP_MESSAGE " [OPTION...] HOST ...\n\
|
||||||
|
Send ICMP ECHO_REQUEST packets to network hosts.\n"
|
||||||
|
|
||||||
|
|
||||||
typedef struct s_settings
|
typedef struct s_settings
|
||||||
{
|
{
|
||||||
char *host;
|
char *host;
|
||||||
int count;
|
int count; // -c (--count)
|
||||||
int ttl;
|
int timeout; // -w (--timeout)
|
||||||
|
short flood; // -f (--flood)
|
||||||
|
short no_resolve; //-n (--numeric)
|
||||||
|
int ttl; // --ttl
|
||||||
|
|
||||||
|
short stop;
|
||||||
|
short err;
|
||||||
|
|
||||||
} t_settings;
|
} t_settings;
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user