Skip to content

Commit

Permalink
✨ Add ft_swap, ft_ntohs implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
lumenthi committed Nov 25, 2022
1 parent f925a60 commit 539816b
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 8 deletions.
2 changes: 2 additions & 0 deletions headers/malcolm.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef MALCOLM_H
# define MALCOLM_H

#include "libft.h"

#include <sys/types.h>
#include <sys/socket.h>
#include <linux/if_ether.h>
Expand Down
5 changes: 3 additions & 2 deletions libft/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# By: lumenthi <[email protected]> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# 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 #
# #
# **************************************************************************** #

Expand Down Expand Up @@ -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))
Expand Down
28 changes: 28 additions & 0 deletions libft/ft_swap.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_swap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lumenthi <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}
4 changes: 3 additions & 1 deletion libft/libft.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: lumenthi <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -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
15 changes: 10 additions & 5 deletions sources/main.c
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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 */
Expand All @@ -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 */
Expand All @@ -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");
Expand Down

0 comments on commit 539816b

Please sign in to comment.