From 539816b0704ccddaaba1f392370533fc9d4310a4 Mon Sep 17 00:00:00 2001 From: lumenthi Date: Fri, 25 Nov 2022 17:54:27 +0100 Subject: [PATCH] :sparkles: Add ft_swap, ft_ntohs implementation --- headers/malcolm.h | 2 ++ libft/Makefile | 5 +++-- libft/ft_swap.c | 28 ++++++++++++++++++++++++++++ libft/libft.h | 4 +++- sources/main.c | 15 ++++++++++----- 5 files changed, 46 insertions(+), 8 deletions(-) create mode 100644 libft/ft_swap.c diff --git a/headers/malcolm.h b/headers/malcolm.h index 37c2a73..207149c 100644 --- a/headers/malcolm.h +++ b/headers/malcolm.h @@ -1,6 +1,8 @@ #ifndef MALCOLM_H # define MALCOLM_H +#include "libft.h" + #include #include #include diff --git a/libft/Makefile b/libft/Makefile index 9c11081..5dbb20e 100644 --- a/libft/Makefile +++ b/libft/Makefile @@ -6,7 +6,7 @@ # By: lumenthi +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2018/01/04 10:37:11 by lumenthi #+# #+# # -# Updated: 2022/10/11 11:10:04 by lumenthi ### ########.fr # +# Updated: 2022/11/25 17:41:20 by lumenthi ### ########.fr # # # # **************************************************************************** # @@ -117,7 +117,8 @@ SRCS = ft_atoi.c \ ft_strjoin_free.c \ ft_rmchar.c \ time_utils.c \ - ft_abs.c + ft_abs.c \ + ft_swap.c OBJDIR = objs OBJS = $(addprefix $(OBJDIR)/, $(SRCS:.c=.o)) diff --git a/libft/ft_swap.c b/libft/ft_swap.c new file mode 100644 index 0000000..76184a4 --- /dev/null +++ b/libft/ft_swap.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_swap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: lumenthi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/10 12:01:16 by lumenthi #+# #+# */ +/* Updated: 2022/11/25 17:45:11 by lumenthi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +uint16_t swap_uint16(uint16_t nb) +{ + nb = (nb << 8) | (nb >> 8); + return (nb); +} + +uint32_t swap_uint32(uint32_t nb) +{ + nb = ((nb & 0x000000FF) << 24 | + (nb & 0x0000FF00) << 8 | + (nb & 0x00FF0000) >> 8 | + (nb & 0xFF000000) >> 24); + return (nb); +} diff --git a/libft/libft.h b/libft/libft.h index 7790094..9f4bf7b 100644 --- a/libft/libft.h +++ b/libft/libft.h @@ -6,7 +6,7 @@ /* By: lumenthi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/11/11 10:50:56 by lumenthi #+# #+# */ -/* Updated: 2022/10/11 11:10:36 by lumenthi ### ########.fr */ +/* Updated: 2022/11/25 17:45:30 by lumenthi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -121,5 +121,7 @@ uint64_t get_time(void); int ft_abs(int nb); long ft_labs(long nb); long long ft_llabs(long long nb); +uint16_t swap_uint16(uint16_t nb); +uint32_t swap_uint32(uint32_t nb); #endif diff --git a/sources/main.c b/sources/main.c index 63296cf..c09f4c0 100644 --- a/sources/main.c +++ b/sources/main.c @@ -1,5 +1,10 @@ #include "malcolm.h" +uint16_t ft_ntohs(uint16_t netshort) +{ + return swap_uint16(netshort); +} + void print_ip(uint8_t *ip_address) { int i = 0; @@ -30,11 +35,11 @@ void debug_arp(struct arp_hdr *arp) /* Type informations */ printf("Hardware type: %s\n", - (ntohs(arp->hrd) == HARDWARE_ETHERNET) ? "Ethernet" : "Unknown"); + (ft_ntohs(arp->hrd) == HARDWARE_ETHERNET) ? "Ethernet" : "Unknown"); printf("Protocol type: %s\n", - (ntohs(arp->pro) == ETH_P_IP) ? "IPv4" : "Unknown"); + (ft_ntohs(arp->pro) == ETH_P_IP) ? "IPv4" : "Unknown"); printf("Operation: %s\n", - (ntohs(arp->op) == ARP_REQUEST) ? "ARP Request" : "ARP Reply"); + (ft_ntohs(arp->op) == ARP_REQUEST) ? "ARP Request" : "ARP Reply"); /* Addresses informations */ /* Sender */ @@ -60,7 +65,7 @@ void debug_eth(struct ethernet_hdr *ethernet) /* Type */ printf("Ethernet type: %s\n", - (ntohs(ethernet->type) == ETH_P_ARP) ? "ARP" : "Other"); + (ft_ntohs(ethernet->type) == ETH_P_ARP) ? "ARP" : "Other"); /* Addresses informations */ /* Sender */ @@ -83,7 +88,7 @@ void debug_packet(char *buffer) ethernet = (struct ethernet_hdr *)buffer; arp = (struct arp_hdr *)(buffer + sizeof(struct ethernet_hdr)); - type = ntohs(ethernet->type); + type = ft_ntohs(ethernet->type); if (type == ETH_P_ARP) { printf("\n");