move icmp packet prepartion to file icmp.c

This commit is contained in:
2025-04-24 22:54:03 +02:00
parent 01df0d6cb0
commit e622d9d175
4 changed files with 79 additions and 61 deletions

View File

@ -5,14 +5,15 @@ FLAGS=-Werror -Wextra -Wall -g
SRCS_DIR = srcs SRCS_DIR = srcs
OBJS_DIR = .objs OBJS_DIR = .objs
SRCS = ft_ping.c SRCS = ft_ping.c\
icmp.c
OBJS = $(addprefix $(OBJS_DIR)/,$(SRCS:.c=.o)) OBJS = $(addprefix $(OBJS_DIR)/,$(SRCS:.c=.o))
all: $(NAME) all: $(NAME)
$(NAME): $(OBJS) $(NAME): $(OBJS)
$(CC) $(FLAGS) $< -o $@ $(CC) $(FLAGS) $^ -o $@
$(OBJS_DIR): $(OBJS_DIR):
mkdir $(OBJS_DIR) mkdir $(OBJS_DIR)

View File

@ -6,70 +6,12 @@
/* 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:45:16 by tomoron ### ########.fr */ /* Updated: 2025/04/24 22:50:08 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "includes/ft_ping.h" #include "includes/ft_ping.h"
#include <fcntl.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <byteswap.h>
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) int main(int argc, char **argv)
{ {

67
srcs/icmp.c Normal file
View File

@ -0,0 +1,67 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* icmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* 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 */
/* */
/* ************************************************************************** */
#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);
}

View File

@ -5,9 +5,15 @@
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
#include <sys/time.h> #include <sys/time.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <byteswap.h>
#include <fcntl.h>
#define PACKET_DATA_LENGTH 40 #define PACKET_DATA_LENGTH 40
typedef struct s_settings typedef struct s_settings
{ {
char *host; char *host;
@ -27,4 +33,6 @@ typedef struct s_icmp_echo
uint8_t data[40]; uint8_t data[40];
} t_icmp_echo; } t_icmp_echo;
t_icmp_echo prepare_icmp_echo(uint16_t sequence, uint16_t identifier);
#endif #endif