Skip to content

Commit

Permalink
Syncing with ft_traceroute
Browse files Browse the repository at this point in the history
- added i4toh from socket (but need some debugging)
- refac dns part
- refac ft_getip_str
  • Loading branch information
Pixailz committed May 28, 2024
1 parent 4e58959 commit 79b3a58
Show file tree
Hide file tree
Showing 22 changed files with 591 additions and 155 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ jobs:
test_src: [
"ato.c",
"check/is_str.c",
"dns.c",
"get_next_line.c",
"hton.c",
"htoi4.c",
Expand Down Expand Up @@ -100,11 +99,11 @@ jobs:
run: git submodule update --remote --recursive

- name: 🔧 Launch, with sudo, ${{ matrix.test_src }}
if: contains(fromJson('["dns.c"]'), matrix.test_src)
if: contains(fromJson('["htoi4.c", "i4toh.c"]'), matrix.test_src)
run: sudo make CI_TARGET=${{ matrix.test_src }} ci-run

- name: 🔧 Launch ${{ matrix.test_src }}
if: ${{ !contains(fromJson('["dns.c"]'), matrix.test_src) }}
if: ${{ !contains(fromJson('["htoi4.c", "i4toh.c"]'), matrix.test_src) }}
run: make CI_TARGET=${{ matrix.test_src }} ci-run

norminette:
Expand Down
35 changes: 29 additions & 6 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/26 16:59:00 by brda-sil ### ########.fr */
/* Updated: 2024/05/28 23:39:37 by brda-sil ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -41,13 +41,18 @@
/* CONFIG */
/* ###### */

# ifndef NI_MAXHOST
# define NI_MAXHOST 0x401
# endif // NI_MAXHOST

# define HTOI4_USE_SOCKET 1
# define I4TOH_USE_SOCKET 0

# define DNS_TYPE_A 1
// # define DNS_TYPE_NS 2
# define DNS_TYPE_CNAME 5
// # define DNS_TYPE_SOA 6
// # define DNS_TYPE_PTR 12
# define DNS_TYPE_PTR 12
// # define DNS_TYPE_MX 15
// # define DNS_TYPE_TXT 16
// # define DNS_TYPE_AAAA 28
Expand Down Expand Up @@ -101,6 +106,9 @@ typedef struct s_ip
/* FILES */
/* ##### */

// network/ipv4/ft_getip_str.c
char *ft_getip_str(t_int4 n);

// network/ipv4/ft_htoi4.c
t_int4 ft_htoi4_getaddrinfo(char *hostname, char *service);
t_int4 ft_htoi4(char *hostname, char *service);
Expand All @@ -111,6 +119,10 @@ t_uint32 ft_htonl(t_uint32 h);
// network/ipv4/ft_htons.c
t_uint16 ft_htons(t_uint16 h);

// network/ipv4/ft_i4toh.c
char *ft_i4toh_getnameinfo(t_int4 ip);
char *ft_i4toh(t_int4 ip);

// network/ipv4/ft_ipstr.c
int ft_ip_check_part(char *splitted);
char **ft_ip_check(char *str);
Expand All @@ -135,10 +147,6 @@ 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);
char *ft_getip_str(t_int4 n);

// network/ipv4/htoi4_socket/get_a_record.c
t_int4 ft_htoi4_get_a_record(t_packet *pkt);

// network/ipv4/htoi4_socket/init_packet.c
t_packet ft_htoi4_init_packet(char *domain);
Expand All @@ -155,6 +163,21 @@ t_int4 ft_htoi4_recv_packet(int sock);
// network/ipv4/htoi4_socket/send_packet.c
int ft_htoi4_send_packet(int socket, t_packet pkt);

// network/ipv4/i4toh_socket/init_packet.c
t_packet ft_i4toh_init_packet(t_int4 ip);

// network/ipv4/i4toh_socket/init_socket.c
int ft_i4toh_init_socket(void);

// network/ipv4/i4toh_socket/main.c
char *ft_i4toh_socket(t_int4 ip);

// network/ipv4/i4toh_socket/recv_packet.c
char *ft_i4toh_recv_packet(int sock);

// network/ipv4/i4toh_socket/send_packet.c
int ft_i4toh_send_packet(int socket, t_packet pkt);

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

#endif /* LIBFT_NETWORK_IPV4_H */
19 changes: 16 additions & 3 deletions inc/libft_network/packet.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/26 02:08:11 by brda-sil ### ########.fr */
/* Updated: 2024/05/28 22:04:49 by brda-sil ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -237,10 +237,14 @@ typedef struct s_dnsr
t_uint16 ft_pkt_checksum(char *data, t_size size);

// network/packet/dns/dnsq_fill.c
void ft_pkt_dnsq_fill(
void ft_pkt_dnsq_fill_a(
t_packet *pack,
char *domain,
t_uint16 type,
t_uint16 class
);
void ft_pkt_dnsq_fill_ptr(
t_packet *pack,
t_int4 ip,
t_uint16 class
);

Expand All @@ -261,6 +265,9 @@ int ft_pkt_dnsr_get(
t_dnshdr *ft_pkt_get_dns(t_packet *packet);
unsigned char *ft_pkt_get_dns_data(t_packet *pack);

// network/packet/dns/get_a_record.c
t_int4 ft_dns_get_a_record(t_packet *pkt);

// network/packet/dns/get_domain_fmt.c
void ft_dns_get_domain_fmt_loop(
char *ret,
Expand All @@ -269,6 +276,12 @@ void ft_dns_get_domain_fmt_loop(
);
char *ft_dns_get_domain_fmt(char *domain);

// network/packet/dns/get_ptr_record.c
char *ft_dns_get_ptr_record(t_packet *pkt);

// network/packet/dns/ip_to_domain.c
char *ft_dns_ip_to_domain(t_int4 ip);

// network/packet/dns/name_get.c
int ft_pkt_get_dnsr_name(
unsigned char *data,
Expand Down
15 changes: 12 additions & 3 deletions mk/srcs.mk
Original file line number Diff line number Diff line change
Expand Up @@ -175,22 +175,28 @@ SRC_LNX := linux/ft_getgid.c \
### NETWORK

#### IPV4
SRC_NET_IPV4 := network/ipv4/ft_htoi4.c \
SRC_NET_IPV4 := network/ipv4/ft_getip_str.c \
network/ipv4/ft_htoi4.c \
network/ipv4/ft_htonl.c \
network/ipv4/ft_htons.c \
network/ipv4/ft_i4toh.c \
network/ipv4/ft_ipstr.c \
network/ipv4/ft_is_goodhost.c \
network/ipv4/ft_ltoaddr.c \
network/ipv4/ft_ntohl.c \
network/ipv4/ft_ntohs.c \
network/ipv4/ft_ntop.c \
network/ipv4/ft_putip_fd.c \
network/ipv4/htoi4_socket/get_a_record.c \
network/ipv4/htoi4_socket/init_packet.c \
network/ipv4/htoi4_socket/init_socket.c \
network/ipv4/htoi4_socket/main.c \
network/ipv4/htoi4_socket/recv_packet.c \
network/ipv4/htoi4_socket/send_packet.c
network/ipv4/htoi4_socket/send_packet.c \
network/ipv4/i4toh_socket/init_packet.c \
network/ipv4/i4toh_socket/init_socket.c \
network/ipv4/i4toh_socket/main.c \
network/ipv4/i4toh_socket/recv_packet.c \
network/ipv4/i4toh_socket/send_packet.c

#### PACK
SRC_NET_PACK := network/packet/checksum.c \
Expand All @@ -199,7 +205,10 @@ SRC_NET_PACK := network/packet/checksum.c \
network/packet/dns/dnsq_len.c \
network/packet/dns/dnsr_get.c \
network/packet/dns/get.c \
network/packet/dns/get_a_record.c \
network/packet/dns/get_domain_fmt.c \
network/packet/dns/get_ptr_record.c \
network/packet/dns/ip_to_domain.c \
network/packet/dns/name_get.c \
network/packet/dns/print.c \
network/packet/ft_packet_get.c \
Expand Down
45 changes: 45 additions & 0 deletions src/network/ipv4/ft_getip_str.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_getip_str.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: brda-sil <brda-sil@students.42angouleme +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/28 14:53:53 by brda-sil #+# #+# */
/* Updated: 2024/05/28 14:54:06 by brda-sil ### ########.fr */
/* */
/* ************************************************************************** */

#include "libft_network.h"

static t_size set_part(char *ptr, t_int4 n, t_size i)
{
char *tmp_n;
t_size len_tmp_n;

tmp_n = ft_itoa(ft_int4_dcomp(n, i));
len_tmp_n = ft_strlen(tmp_n);
ft_memcpy(ptr, tmp_n, len_tmp_n);
if (i)
{
ptr[len_tmp_n] = '.';
len_tmp_n++;
}
free(tmp_n);
return (len_tmp_n);
}

char *ft_getip_str(t_int4 n)
{
char *ptr;
t_size index;

ptr = (char *)ft_calloc(16, sizeof(char));

index = set_part(ptr, n, 3);
index += set_part(ptr + index, n, 2);
index += set_part(ptr + index, n, 1);
index += set_part(ptr + index, n, 0);

return ptr;
}
38 changes: 38 additions & 0 deletions src/network/ipv4/ft_i4toh.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_i4toh.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: brda-sil <brda-sil@students.42angouleme +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/14 05:54:10 by brda-sil #+# #+# */
/* Updated: 2024/05/28 21:12:27 by brda-sil ### ########.fr */
/* */
/* ************************************************************************** */

#include "libft_network.h"

char *ft_i4toh_getnameinfo(t_int4 ip)
{
struct sockaddr_in sa;
char hostname[NI_MAXHOST];

ft_bzero(&sa, sizeof(struct sockaddr_in));
ft_bzero(&hostname, NI_MAXHOST);
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = ip;
if (getnameinfo((struct sockaddr *)&sa, sizeof(sa), hostname, NI_MAXHOST, NULL, 0, 0) != 0)
return (NULL);
return (ft_strdup(hostname));
}

char *ft_i4toh(t_int4 ip)
{
char *retv;

if (I4TOH_USE_SOCKET)
retv = ft_i4toh_socket(ip);
else
retv = ft_i4toh_getnameinfo(ip);
return (retv);
}
43 changes: 1 addition & 42 deletions src/network/ipv4/ft_putip_fd.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/11/07 03:36:06 by brda-sil #+# #+# */
/* Updated: 2024/05/24 00:39:25 by brda-sil ### ########.fr */
/* Updated: 2024/05/28 14:53:30 by brda-sil ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -25,44 +25,3 @@ t_size ft_putip_fd(t_int4 n, int fd)
printed = ft_putunbr_fd(ft_int4_dcomp(n, 0), fd);
return (printed);
}

// static set_part()

char *ft_getip_str(t_int4 n)
{
char *ptr;
char *tmp_n;
t_size index;
t_size len_tmp_n;

ptr = (char *)ft_calloc(15, sizeof(char));

tmp_n = ft_itoa(ft_int4_dcomp(n, 3));
len_tmp_n = ft_strlen(tmp_n);
ft_memcpy(ptr, tmp_n, len_tmp_n);
free(tmp_n);
index = len_tmp_n;
ptr[index++] = '.';

tmp_n = ft_itoa(ft_int4_dcomp(n, 2));
len_tmp_n = ft_strlen(tmp_n);
ft_memcpy(ptr + index, tmp_n, len_tmp_n);
free(tmp_n);
index += len_tmp_n;
ptr[index++] = '.';

tmp_n = ft_itoa(ft_int4_dcomp(n, 1));
len_tmp_n = ft_strlen(tmp_n);
ft_memcpy(ptr + index, tmp_n, len_tmp_n);
free(tmp_n);
index += len_tmp_n;
ptr[index++] = '.';

tmp_n = ft_itoa(ft_int4_dcomp(n, 0));
len_tmp_n = ft_strlen(tmp_n);
ft_memcpy(ptr + index, tmp_n, len_tmp_n);
free(tmp_n);
index += len_tmp_n;

return ptr;
}
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/26 01:21:08 by brda-sil ### ########.fr */
/* Updated: 2024/05/27 11:35:29 by brda-sil ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -35,7 +35,7 @@ t_packet ft_htoi4_init_packet(char *domain)
pkt_dns->identification = ft_htons(421);
pkt_dns->recursion_desired = 1;
pkt_dns->question_count = ft_htons(1);
ft_pkt_dnsq_fill(&pkt, domain, DNS_TYPE_A, DNS_CLASS_IN);
ft_pkt_dnsq_fill_a(&pkt, domain, DNS_CLASS_IN);
pkt_ip->total_len = ft_htons(PACK_LEN_IP + PACK_LEN_UDP + PACK_LEN_DNS + ft_pkt_dnsq_len(pkt));
pkt_udp->length = ft_htons(PACK_LEN_UDP + PACK_LEN_DNS + ft_pkt_dnsq_len(pkt));
return (pkt);
Expand Down
7 changes: 2 additions & 5 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/26 01:56:42 by brda-sil ### ########.fr */
/* Updated: 2024/05/28 23:50:07 by brda-sil ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -51,12 +51,9 @@ t_int4 ft_htoi4_recv_packet(int sock)
{
pkt = ft_pkt_get();
if (recv_reply(sock, &pkt))
{
ft_perr("TIMEOUT\n");
return 0;
}
if (check_reply(&pkt))
break;
}
return (ft_htoi4_get_a_record(&pkt));
return (ft_dns_get_a_record(&pkt));
}
Loading

0 comments on commit 79b3a58

Please sign in to comment.