From e622d9d175a323e5b624d395a632487af929ffe4 Mon Sep 17 00:00:00 2001 From: tomoron Date: Thu, 24 Apr 2025 22:54:03 +0200 Subject: [PATCH] move icmp packet prepartion to file icmp.c --- Makefile | 5 +-- srcs/ft_ping.c | 60 +----------------------------------- srcs/icmp.c | 67 +++++++++++++++++++++++++++++++++++++++++ srcs/includes/ft_ping.h | 8 +++++ 4 files changed, 79 insertions(+), 61 deletions(-) create mode 100644 srcs/icmp.c diff --git a/Makefile b/Makefile index e83fee5..171ab70 100644 --- a/Makefile +++ b/Makefile @@ -5,14 +5,15 @@ FLAGS=-Werror -Wextra -Wall -g SRCS_DIR = srcs OBJS_DIR = .objs -SRCS = ft_ping.c +SRCS = ft_ping.c\ + icmp.c OBJS = $(addprefix $(OBJS_DIR)/,$(SRCS:.c=.o)) all: $(NAME) $(NAME): $(OBJS) - $(CC) $(FLAGS) $< -o $@ + $(CC) $(FLAGS) $^ -o $@ $(OBJS_DIR): mkdir $(OBJS_DIR) diff --git a/srcs/ft_ping.c b/srcs/ft_ping.c index 3a5194a..ae8e65b 100644 --- a/srcs/ft_ping.c +++ b/srcs/ft_ping.c @@ -6,70 +6,12 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/24 00:03:56 by tomoron #+# #+# */ -/* Updated: 2025/04/24 22:45:16 by tomoron ### ########.fr */ +/* Updated: 2025/04/24 22:50:08 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ #include "includes/ft_ping.h" -#include -#include -#include -#include -void set_packet_data(uint8_t *packet_data) -{ - int i; - - i = 0; - - while(i < PACKET_DATA_LENGTH) - { - packet_data[i] = i; - i++; - } -} - -uint16_t calc_checksum(void *ptr, size_t len) -{ - uint32_t res; - uint16_t *data; - - data = ptr; - res = 0; - while (len > 1) - { - res += *data; - data++; - len -= 2; - } - if (len) - res += *(uint8_t *)data; - - while (res >> 16) { - res = (res & 0xFFFF) + (res >> 16); - } - - return(htons(~res)); -} - -t_icmp_echo prepare_icmp_echo(uint16_t sequence, uint16_t identifier) -{ - t_icmp_echo packet; - - packet.type = 8; - packet.code = 0; - packet.identifier = htons(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)); - - return(packet); -} int main(int argc, char **argv) { diff --git a/srcs/icmp.c b/srcs/icmp.c new file mode 100644 index 0000000..fa09e49 --- /dev/null +++ b/srcs/icmp.c @@ -0,0 +1,67 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* icmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/04/24 22:49:22 by tomoron #+# #+# */ +/* Updated: 2025/04/24 22:52:09 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "includes/ft_ping.h" + +static void set_packet_data(uint8_t *packet_data) +{ + int i; + + i = 0; + + while(i < PACKET_DATA_LENGTH) + { + packet_data[i] = i; + i++; + } +} + +static uint16_t calc_checksum(void *ptr, size_t len) +{ + uint32_t res; + uint16_t *data; + + data = ptr; + res = 0; + while (len > 1) + { + res += *data; + data++; + len -= 2; + } + if (len) + res += *(uint8_t *)data; + + while (res >> 16) { + res = (res & 0xFFFF) + (res >> 16); + } + + return(htons(~res)); +} + +t_icmp_echo prepare_icmp_echo(uint16_t sequence, uint16_t identifier) +{ + t_icmp_echo packet; + + packet.type = 8; + packet.code = 0; + packet.identifier = htons(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)); + + return(packet); +} diff --git a/srcs/includes/ft_ping.h b/srcs/includes/ft_ping.h index ecac8f6..4b82f87 100644 --- a/srcs/includes/ft_ping.h +++ b/srcs/includes/ft_ping.h @@ -5,9 +5,15 @@ #include #include #include +#include +#include +#include + +#include #define PACKET_DATA_LENGTH 40 + typedef struct s_settings { char *host; @@ -27,4 +33,6 @@ typedef struct s_icmp_echo uint8_t data[40]; } t_icmp_echo; +t_icmp_echo prepare_icmp_echo(uint16_t sequence, uint16_t identifier); + #endif