Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix i4toh #2

Merged
merged 2 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 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 <[email protected] +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/09 00:48:48 by brda-sil #+# #+# */
/* Updated: 2024/08/12 16:50:48 by brda-sil ### ########.fr */
/* Updated: 2024/08/14 16:35:13 by brda-sil ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -46,7 +46,7 @@
# endif // NI_MAXHOST

# define HTOI4_USE_SOCKET 1
# define I4TOH_USE_SOCKET 0
# define I4TOH_USE_SOCKET 1

# define DNS_TYPE_A 1
// # define DNS_TYPE_NS 2
Expand Down Expand Up @@ -122,6 +122,7 @@ 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);
char **ft_i4tohs(t_int4 ip);

// network/ipv4/ft_ipstr.c
int ft_ip_check_part(char *splitted);
Expand Down Expand Up @@ -174,9 +175,10 @@ int ft_i4toh_init_socket(void);

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

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

// network/ipv4/i4toh_socket/send_packet.c
int ft_i4toh_send_packet(int socket, t_packet pkt);
Expand Down
103 changes: 79 additions & 24 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 <[email protected] +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/09 00:48:48 by brda-sil #+# #+# */
/* Updated: 2024/05/28 22:04:49 by brda-sil ### ########.fr */
/* Updated: 2024/08/14 11:51:31 by brda-sil ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -173,6 +173,23 @@ typedef struct __attribute__((__packed__)) s_icmphdr_time_exceed
} t_icmphdr_time_exceed;

// DNS

typedef enum e_dns_rcode {
DNS_RCODE_NO_ERROR = 0,
DNS_RCODE_FORMAT_ERROR = 1,
DNS_RCODE_SERVER_FAILURE = 2,
DNS_RCODE_NAME_ERROR = 3,
DNS_RCODE_NOT_IMPLEMENTED = 4,
DNS_RCODE_REFUSED = 5,
} t_dns_rcode;

typedef enum e_dns_rtype {
DNS_RTYPE_QUESTION = 0,
DNS_RTYPE_ANSWER = 1,
DNS_RTYPE_AUTHORITY = 2,
DNS_RTYPE_ADDITIONAL = 3,
} t_dns_rtype;

//HEADER
typedef struct __attribute__((__packed__)) s_dnshdr
{
Expand All @@ -185,7 +202,7 @@ typedef struct __attribute__((__packed__)) s_dnshdr
t_uint8 opcode:4;
t_uint8 is_response:1;

t_uint8 response_code:4;
t_dns_rcode response_code:4;
t_uint8 reserved:3;
t_uint8 recursion_available:1;

Expand All @@ -203,7 +220,7 @@ typedef struct __attribute__((__packed__)) s_dnshdr
# endif // __BYTE_ORDER == __LITTLE_ENDIAN

t_uint16 question_count;
t_uint16 response_count;
t_uint16 answer_count;
t_uint16 authority_count;
t_uint16 additional_count;

Expand All @@ -217,6 +234,28 @@ typedef struct s_dnsq
t_uint16 class;
} t_dnsq;

// DNS RECORD
typedef struct s_dnsr_name
{
t_uint16 offset;
t_dns_rtype rsc_type;
t_uint16 type;
t_uint16 class;
t_uint16 ttl;
t_uint16 rlength;
char *name;
char *rdata;
struct s_dnsr_name *next;
} t_dnsr_name;

typedef struct s_dnsr_struct
{
unsigned char *data;
t_dnshdr *header;
t_dnsr_name *names;
t_uint16 offset;
} t_dnsr_struct;

typedef struct s_dnsr
{
t_uint16 type;
Expand All @@ -228,7 +267,6 @@ typedef struct s_dnsr

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


/* ########################################################################## */
/* FILES */
/* ##### */
Expand All @@ -248,26 +286,13 @@ void ft_pkt_dnsq_fill_ptr(
t_uint16 class
);

// network/packet/dns/dnsq_get.c
int ft_pkt_dnsq_get(unsigned char *data, t_dnsq *dnsq);

// network/packet/dns/dnsq_len.c
t_size ft_pkt_dnsq_len(t_packet pack);

// network/packet/dns/dnsr_get.c
int ft_pkt_dnsr_get(
unsigned char *data,
t_uint16 rc,
t_dnsr *dnsr
);

// network/packet/dns/get.c
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 @@ -276,19 +301,49 @@ 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/get_record/extract_answer.c
t_uint16 get_answer_nb(t_dnsr_name *name, t_uint16 type);
char **ft_dns_extract_answer_ptr(t_dnsr_name *name);
t_int4 ft_dns_extract_answer_a(t_dnsr_name *name);

// network/packet/dns/ip_to_domain.c
char *ft_dns_ip_to_domain(t_int4 ip);
// network/packet/dns/get_record/get_answer.c
void get_dns_answer_name(t_dnsr_struct *dnsr);
void print_dns_answer_name(t_dnsr_name *name);
t_bool get_dns_anrecord(t_dnsr_struct *dnsr, t_uint16 count);

// network/packet/dns/name_get.c
int ft_pkt_get_dnsr_name(
// network/packet/dns/get_record/get_label.c
int ft_dns_getlabel_ptr(
unsigned char *data,
t_uint32 offset,
char **name,
t_uint32 max_len
);
int ft_dns_getlabel_a(
unsigned char *data,
t_uint32 offset,
char **name
);

// network/packet/dns/print.c
// network/packet/dns/get_record/get_question.c
void get_dns_question_name(t_dnsr_struct *dnsr);
void print_dns_question_name(t_dnsr_name *name);
t_bool get_dns_qdrecord(t_dnsr_struct *dnsr, t_uint16 count);

// network/packet/dns/get_record/main.c
void append_name(t_dnsr_name *name, t_dnsr_struct **dnsr);
t_uint16 get_dns_offset(t_dnsr_struct *dnsr);
char *get_name_by_offset(t_dnsr_struct *dnsr);
t_uint16 get_dnsr_type(t_dnsr_struct *dnsr);
t_uint16 get_dnsr_class(t_dnsr_struct *dnsr);
t_uint16 get_dnsr_ttl(t_dnsr_struct *dnsr);
t_uint16 get_dnsr_rlength(t_dnsr_struct *dnsr);
t_dnsr_name *get_last_name(t_dnsr_struct *dnsr);
void ft_free_dnsr(t_dnsr_struct *dnsr);
char **ft_dns_get_record_ptr(t_packet *pkt);
t_int4 ft_dns_get_record_a(t_packet *pkt);

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

// network/packet/ft_packet_get.c
t_packet ft_pkt_get(void);
Expand Down
11 changes: 5 additions & 6 deletions mk/srcs.mk
Original file line number Diff line number Diff line change
Expand Up @@ -203,16 +203,15 @@ SRC_NET_IPV4 := network/ipv4/ft_getip_str.c \
#### PACK
SRC_NET_PACK := network/packet/checksum.c \
network/packet/dns/dnsq_fill.c \
network/packet/dns/dnsq_get.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/get_record/extract_answer.c \
network/packet/dns/get_record/get_answer.c \
network/packet/dns/get_record/get_label.c \
network/packet/dns/get_record/get_question.c \
network/packet/dns/get_record/main.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 \
network/packet/icmp/checksum.c \
network/packet/icmp/get.c \
Expand Down
10 changes: 9 additions & 1 deletion src/network/ipv4/ft_i4toh.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: brda-sil <[email protected] +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/14 05:54:10 by brda-sil #+# #+# */
/* Updated: 2024/05/28 21:12:27 by brda-sil ### ########.fr */
/* Updated: 2024/08/14 15:59:01 by brda-sil ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -36,3 +36,11 @@ char *ft_i4toh(t_int4 ip)
retv = ft_i4toh_getnameinfo(ip);
return (retv);
}

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

retv = ft_i4tohs_socket(ip);
return (retv);
}
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 <[email protected] +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/26 00:22:20 by brda-sil #+# #+# */
/* Updated: 2024/08/13 09:40:12 by brda-sil ### ########.fr */
/* Updated: 2024/08/14 15:51:01 by brda-sil ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -55,5 +55,5 @@ t_int4 ft_htoi4_recv_packet(int sock)
if (check_reply(&pkt))
break;
}
return (ft_dns_get_a_record(&pkt));
return (ft_dns_get_record_a(&pkt));
}
31 changes: 28 additions & 3 deletions src/network/ipv4/i4toh_socket/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: brda-sil <[email protected] +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/25 23:56:34 by brda-sil #+# #+# */
/* Updated: 2024/08/13 20:52:45 by brda-sil ### ########.fr */
/* Updated: 2024/08/14 16:40:14 by brda-sil ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -16,6 +16,7 @@ char *ft_i4toh_socket(t_int4 ip)
{
static int sock = -2;
t_packet pkt;
char **names;
char *name;

if (sock == -2)
Expand All @@ -25,9 +26,33 @@ char *ft_i4toh_socket(t_int4 ip)

pkt = ft_i4toh_init_packet(ip);

// ft_pkt_print_raw(1, (char *)pkt.data, PACK_LEN_IP + PACK_LEN_UDP + PACK_LEN_DNS + ft_pkt_dnsq_len(pkt));
if (ft_i4toh_send_packet(sock, pkt))
return (0);
name = ft_i4toh_recv_packet(sock);
names = ft_i4toh_recv_packet(sock);
name = FT_NULL;
if (names && names[0])
{
name = ft_strdup(names[0]);
ft_free_char_pp(names);
}
return (name);
}

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

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

pkt = ft_i4toh_init_packet(ip);

if (ft_i4toh_send_packet(sock, pkt))
return (0);
names = ft_i4toh_recv_packet(sock);
return (names);
}
22 changes: 14 additions & 8 deletions src/network/ipv4/i4toh_socket/recv_packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: brda-sil <[email protected] +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/26 00:22:20 by brda-sil #+# #+# */
/* Updated: 2024/05/28 16:03:34 by brda-sil ### ########.fr */
/* Updated: 2024/08/14 15:46:30 by brda-sil ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -15,16 +15,18 @@
static t_uint32 check_reply(t_packet *pkt)
{
t_udphdr *pack_udp;
t_dnshdr *pack_dns;
t_uint16 src_port;
t_uint16 dst_port;

pack_udp = ft_pkt_get_udp(pkt);
pack_dns = ft_pkt_get_dns(pkt);
src_port = ft_htons(pack_udp->src_port);
dst_port = ft_htons(pack_udp->dst_port);
if (dst_port == DNS_SRC_PORT && src_port == DNS_DST_PORT)
{
if (dst_port != DNS_SRC_PORT || src_port != DNS_DST_PORT)
return (1);
}
if (pack_dns->response_code != DNS_RCODE_NO_ERROR)
return (2);
return (0);
}

Expand All @@ -43,19 +45,23 @@ static t_bool recv_reply(int sock, t_packet *pong_pkt)
return (ret == -1);
}

char *ft_i4toh_recv_packet(int sock)
char **ft_i4toh_recv_packet(int sock)
{
t_packet pkt;
t_uint32 retv;

while (TRUE)
{
pkt = ft_pkt_get();
if (recv_reply(sock, &pkt))
{
return 0;
return FT_NULL;
}
if (check_reply(&pkt))
retv = check_reply(&pkt);
if (!retv)
break;
else if (retv == 2)
return FT_NULL;
}
return (ft_dns_get_ptr_record(&pkt));
return (ft_dns_get_record_ptr(&pkt));
}
Loading
Loading