-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- add new socket manager - add some function to set option on socket - add atexit for parsing and socket
- Loading branch information
Showing
26 changed files
with
415 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,13 +6,14 @@ | |
/* By: brda-sil <[email protected] +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* 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" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
/* By: brda-sil <[email protected] +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* 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 */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -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 | ||
|
@@ -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); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* socket.h :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: brda-sil <[email protected] +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* 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 */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
/* By: brda-sil <[email protected] +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* 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 */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -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); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* get_nameserver.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: brda-sil <[email protected] +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* 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)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
/* By: brda-sil <[email protected] +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* 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 */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -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); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,32 +6,28 @@ | |
/* By: brda-sil <[email protected] +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* 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); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,28 +6,25 @@ | |
/* By: brda-sil <[email protected] +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* 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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
/* By: brda-sil <[email protected] +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* 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 */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -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); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
/* By: brda-sil <[email protected] +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* 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 */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -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, | ||
|
Oops, something went wrong.