Skip to content

Commit

Permalink
New socket + autocleaning
Browse files Browse the repository at this point in the history
- add new socket manager
  - add some function to set option on socket
- add atexit for parsing and socket
  • Loading branch information
Pixailz committed Aug 13, 2024
1 parent cdfeb07 commit 6b93f45
Show file tree
Hide file tree
Showing 26 changed files with 415 additions and 57 deletions.
3 changes: 2 additions & 1 deletion inc/libft_network.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
/* By: brda-sil <brda-sil@students.42angouleme +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/09 00:48:48 by brda-sil #+# #+# */
/* Updated: 2024/05/26 01:22:11 by brda-sil ### ########.fr */
/* Updated: 2024/08/13 20:46:37 by brda-sil ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef LIBFT_NETWORK_H
# define LIBFT_NETWORK_H

# include "libft_network/socket.h"
# include "libft_network/ipv4.h"
# include "libft_network/packet.h"

Expand Down
7 changes: 5 additions & 2 deletions inc/libft_network/ipv4.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: brda-sil <brda-sil@students.42angouleme +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/09 00:48:48 by brda-sil #+# #+# */
/* Updated: 2024/05/28 23:39:37 by brda-sil ### ########.fr */
/* Updated: 2024/08/12 16:50:48 by brda-sil ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -64,7 +64,7 @@
// # define DNS_CLASS_CH 3
// # define DNS_CLASS_HS 4

# define DNS_ADDR 0x08080808
# define DNS_DEFAULT_ADDR 0x08080808
# define DNS_DST_PORT 53
# define DNS_SRC_PORT 0x1234
# define DNS_TTL 64
Expand Down Expand Up @@ -148,6 +148,9 @@ void ft_ntop(t_bin pf, t_int4 ip, char *ip_str);
// network/ipv4/ft_putip_fd.c
t_size ft_putip_fd(t_int4 n, int fd);

// network/ipv4/get_nameserver.c
t_int4 get_dns_nameserver(void);

// network/ipv4/htoi4_socket/init_packet.c
t_packet ft_htoi4_init_packet(char *domain);

Expand Down
67 changes: 67 additions & 0 deletions inc/libft_network/socket.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* socket.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: brda-sil <brda-sil@students.42angouleme +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/09 00:48:48 by brda-sil #+# #+# */
/* Updated: 2024/08/13 21:27:15 by brda-sil ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef LIBFT_NETWORK_SOCKET_H
# define LIBFT_NETWORK_SOCKET_H

/* ########################################################################## */
/* REQUIREMENTS */
/* ############ */

# include "libft_define.h"

/* ########################################################################## */

/* ########################################################################## */
/* CONFIG */
/* ###### */

# define FT_SOCKET_MAX 0x80

/* ########################################################################## */

/* ########################################################################## */
/* STRUCT */
/* ###### */
typedef t_int32 t_socket_id;

/* ########################################################################## */


/* ########################################################################## */
/* FILES */
/* ##### */

// network/socket/ft_socket.c
int ft_socket(int domain, int type, int protocol);

// network/socket/ft_socket_get.c
int ft_socket_get(t_socket_id index);
int *ft_socket_get_new();
t_socket_id ft_socket_get_index(int sock);

// network/socket/ft_socket_singletone.c
int *ft_socket_sing_get();
void ft_socket_sing_free();

// network/socket/setsockopt/bind_interface.c
t_bool ft_setsockopt_bind_interface(int sock, char *interface);

// network/socket/setsockopt/ipheader.c
t_bool ft_setsockopt_ipheader(int sock);

// network/socket/setsockopt/timeout.c
t_bool ft_setsockopt_timeout(int sock, int timeout);

/* ########################################################################## */

#endif /* LIBFT_NETWORK_SOCKET_H */
6 changes: 6 additions & 0 deletions mk/config.mk
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@ MULTIPLE := 1
# dependecies
# PRINT := 1
endif
ifeq ($(findstring net_pack,$(MAKECMDGOALS)),net_sock)
NET_SOCK := 1
MULTIPLE := 1
# dependecies
# PRINT := 1
endif
ifeq ($(findstring error,$(MAKECMDGOALS)),error)
ERROR := 1
MULTIPLE := 1
Expand Down
14 changes: 13 additions & 1 deletion mk/srcs.mk
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ SRC_NET_IPV4 := network/ipv4/ft_getip_str.c \
network/ipv4/ft_ntohs.c \
network/ipv4/ft_ntop.c \
network/ipv4/ft_putip_fd.c \
network/ipv4/get_nameserver.c \
network/ipv4/htoi4_socket/init_packet.c \
network/ipv4/htoi4_socket/init_socket.c \
network/ipv4/htoi4_socket/main.c \
Expand Down Expand Up @@ -225,7 +226,15 @@ SRC_NET_PACK := network/packet/checksum.c \
network/packet/udp/get.c \
network/packet/udp/print.c

SRC_NET := $(SRC_NET_IPV4) $(SRC_NET_PACK)
#### SOCKET
SRC_NET_SOCK := network/socket/ft_socket.c \
network/socket/ft_socket_get.c \
network/socket/ft_socket_singletone.c \
network/socket/setsockopt/bind_interface.c \
network/socket/setsockopt/ipheader.c \
network/socket/setsockopt/timeout.c

SRC_NET := $(SRC_NET_IPV4) $(SRC_NET_PACK) $(SRC_NET_SOCK)

### ERROR
SRC_UNI_TEST := unit_test/ft_assert.c
Expand Down Expand Up @@ -300,6 +309,9 @@ endif
ifeq ($(NET_PACK),1)
SRC_C += $(SRC_NET_PACK)
endif
ifeq ($(NET_SOCK),1)
SRC_C += $(SRC_NET_SOCK)
endif
ifeq ($(UNIT_TEST),1)
SRC_C += $(SRC_UNI_TEST)
endif
Expand Down
2 changes: 1 addition & 1 deletion rsc/helper
25 changes: 23 additions & 2 deletions src/input/ft_get_next_line.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: brda-sil <brda-sil@students.42angouleme +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/05 01:18:23 by brda-sil #+# #+# */
/* Updated: 2022/12/28 12:47:38 by brda-sil ### ########.fr */
/* Updated: 2024/08/12 16:03:20 by brda-sil ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -70,12 +70,33 @@ static void get_line(char **line, char **buff)
push_buff(buff, len_line);
}

static void flush_buffer(char **buff)
{
int counter;

counter = 0;
while (counter < MAX_FD)
{
if (buff[counter])
{
free(buff[counter]);
buff[counter] = FT_NULL;
}
counter++;
}
}

char *ft_get_next_line(int fd)
{
static char *buff[MAX_FD] = {FT_NULL};
char *line;

if (fd < 0 || BUFFER_SIZE < 1)
if (fd == -2)
{
flush_buffer(buff);
return (FT_NULL);
}
else if (fd < 0 || BUFFER_SIZE < 1)
return (FT_NULL);
if (buff[fd] == FT_NULL)
buff[fd] = (char *)ft_calloc(sizeof(char), BUFFER_SIZE + 1);
Expand Down
51 changes: 51 additions & 0 deletions src/network/ipv4/get_nameserver.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_nameserver.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: brda-sil <brda-sil@students.42angouleme +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/12 15:06:04 by brda-sil #+# #+# */
/* Updated: 2024/08/13 10:10:19 by brda-sil ### ########.fr */
/* */
/* ************************************************************************** */

#include "libft_network.h"
#include "libft_input.h"
#include <fcntl.h>

t_int4 get_dns_nameserver(void)
{
int fd;
char* line;
char** splitted;
t_int4 ip;

fd = open("/etc/resolv.conf", O_RDONLY);
if (fd < 0)
return (0);
while (TRUE)
{
line = ft_get_next_line(fd);
if (!line)
{
free(line);
break ;
}
else if (ft_strncmp(line, "nameserver", 10))
{
free(line);
continue ;
}
break;
}
ft_get_next_line(-2);
close(fd);
splitted = ft_split(line, ' ');
free(line);
splitted[1][ft_strlen(splitted[1]) - 1] = '\0';
ip = ft_ipstr(splitted[1]);
ft_free_char_pp(splitted);
ip = ip? ip: DNS_DEFAULT_ADDR;
return (ft_htonl(ip));
}
4 changes: 2 additions & 2 deletions src/network/ipv4/htoi4_socket/init_packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: brda-sil <brda-sil@students.42angouleme +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/26 00:03:48 by brda-sil #+# #+# */
/* Updated: 2024/05/27 11:35:29 by brda-sil ### ########.fr */
/* Updated: 2024/08/12 16:39:59 by brda-sil ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -27,7 +27,7 @@ t_packet ft_htoi4_init_packet(char *domain)
pkt_ip->fragment_off = ft_htons(ft_pkt_fragment_offset(IPHDR_F_DONT_FRAG, 0));
pkt_ip->ttl = DNS_TTL;
pkt_ip->protocol = IPPROTO_UDP;
pkt_ip->dst_addr = ft_htonl(DNS_ADDR);
pkt_ip->dst_addr = get_dns_nameserver();
pkt_udp = ft_pkt_get_udp(&pkt);
pkt_udp->src_port = ft_htons(DNS_SRC_PORT);
pkt_udp->dst_port = ft_htons(DNS_DST_PORT);
Expand Down
14 changes: 5 additions & 9 deletions src/network/ipv4/htoi4_socket/init_socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,28 @@
/* By: brda-sil <brda-sil@students.42angouleme +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/26 00:00:53 by brda-sil #+# #+# */
/* Updated: 2024/05/26 01:23:49 by brda-sil ### ########.fr */
/* Updated: 2024/08/13 21:23:28 by brda-sil ### ########.fr */
/* */
/* ************************************************************************** */

#include "libft_network.h"

int ft_htoi4_init_socket(void)
{
int sock;
struct timeval tv;
int sock;

sock = socket(PF_INET, SOCK_RAW, IPPROTO_UDP);
sock = ft_socket(PF_INET, SOCK_RAW, IPPROTO_UDP);
if (sock == -1)
{
ft_perr("ft_htoi4_init_socket: Permission denied\n");
return (sock);
}
tv.tv_usec = DNS_TIMEOUT % A_SEC;
tv.tv_sec = DNS_TIMEOUT / A_SEC;
if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) == -1)
if (ft_setsockopt_timeout(sock, DNS_TIMEOUT))
{
ft_perr("ft_htoi4_init_socket: SO_RCVTIMEO\n");
return (sock);
}
if (setsockopt(sock, IPPROTO_IP, IP_HDRINCL, (int [1]){1}, \
sizeof(int)) == -1)
if (ft_setsockopt_ipheader(sock))
{
ft_perr("ft_htoi4_init_socket: IP_HDRINCL\n");
return (sock);
Expand Down
15 changes: 6 additions & 9 deletions src/network/ipv4/htoi4_socket/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,25 @@
/* By: brda-sil <brda-sil@students.42angouleme +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/25 23:56:34 by brda-sil #+# #+# */
/* Updated: 2024/05/26 02:07:58 by brda-sil ### ########.fr */
/* Updated: 2024/08/13 20:50:26 by brda-sil ### ########.fr */
/* */
/* ************************************************************************** */

#include "libft_network.h"

t_int4 ft_htoi4_socket(char *domain)
{
t_int4 ip;
int sock;
t_packet pkt;
t_int4 ip;
static int sock = -2;
t_packet pkt;

sock = ft_htoi4_init_socket();
if (sock == -2)
sock = ft_htoi4_init_socket();
if (sock == -1)
return (0);

pkt = ft_htoi4_init_packet(domain);

if (ft_htoi4_send_packet(sock, pkt))
return (0);
ip = ft_htoi4_recv_packet(sock);
if (sock != -1)
close(sock);
return (ip);
}
4 changes: 2 additions & 2 deletions src/network/ipv4/htoi4_socket/recv_packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: brda-sil <brda-sil@students.42angouleme +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/26 00:22:20 by brda-sil #+# #+# */
/* Updated: 2024/05/28 23:50:07 by brda-sil ### ########.fr */
/* Updated: 2024/08/13 09:40:12 by brda-sil ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -38,7 +38,7 @@ static t_bool recv_reply(int sock, t_packet *pong_pkt)
PACK_MAX_LEN, // LEN
0, // FLAG
FT_NULL, // SOCK ADDR FROM
0 // SOCK ADDR FROM LEN
FT_NULL // SOCK ADDR FROM LEN
);
return (ret == -1);
}
Expand Down
6 changes: 4 additions & 2 deletions src/network/ipv4/htoi4_socket/send_packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: brda-sil <brda-sil@students.42angouleme +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/26 00:18:13 by brda-sil #+# #+# */
/* Updated: 2024/05/26 01:23:43 by brda-sil ### ########.fr */
/* Updated: 2024/08/12 16:43:43 by brda-sil ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -16,8 +16,10 @@ int ft_htoi4_send_packet(int socket, t_packet pkt)
{
long ret;
struct sockaddr dst;
t_iphdr *pkt_ip;

dst = ft_ltoaddr(DNS_ADDR);
pkt_ip = ft_pkt_get_ip(&pkt);
dst = ft_ltoaddr(pkt_ip->dst_addr);
ret = sendto(
socket,
pkt.data,
Expand Down
Loading

0 comments on commit 6b93f45

Please sign in to comment.