public inbox for gwml@vger.gnuweeb.org
 help / color / mirror / Atom feed
* [PATCH gwproxy v1 0/2] Introduce IPv6 support for dns parser program
@ 2025-07-28  5:14 ReYuki
  2025-07-28  5:14 ` [PATCH gwproxy v1 1/2] add unit test ReYuki
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: ReYuki @ 2025-07-28  5:14 UTC (permalink / raw)
  To: GNU/Weeb Mailing List; +Cc: ReYuki

From: ReYuki <me@reyuki.site>

Introduce IPv6 support for dns parser program

ReYuki (2):
  add unit test
  add support for AAAA record type

 Makefile         |   7 +++
 dnsclient.c      |   7 ++-
 gwdnsparserlib.c | 128 +++++++++++++++++++++++++++++++++++++++++++++--
 gwdnsparserlib.h |  23 +++++++--
 4 files changed, 155 insertions(+), 10 deletions(-)


base-commit: ba66fa0a7bc0f5585615f867f4f738e55833a088
-- 
ReYuki


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH gwproxy v1 1/2] add unit test
  2025-07-28  5:14 [PATCH gwproxy v1 0/2] Introduce IPv6 support for dns parser program ReYuki
@ 2025-07-28  5:14 ` ReYuki
  2025-07-28  5:14 ` [PATCH gwproxy v1 2/2] add support for AAAA record type ReYuki
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 6+ messages in thread
From: ReYuki @ 2025-07-28  5:14 UTC (permalink / raw)
  To: GNU/Weeb Mailing List; +Cc: ReYuki

From: ReYuki <me@reyuki.site>

---
 Makefile         |   7 +++
 gwdnsparserlib.c | 114 ++++++++++++++++++++++++++++++++++++++++++++++-
 gwdnsparserlib.h |  21 ++++++---
 3 files changed, 135 insertions(+), 7 deletions(-)

diff --git a/Makefile b/Makefile
index 9a03526..031695a 100644
--- a/Makefile
+++ b/Makefile
@@ -6,6 +6,7 @@ INOTIFYSRC	:= test_inotify.c linux.c
 DNSCLIENTSRC	:= dnsclient.c general.c gwdnsparserlib.c
 GWSOCKS5LIBSRC	:= gwsocks5lib.c linux.c general.c
 GWDNSLIBSRC	:= gwdnslib.c linux.c
+PARSERLIBSRC	:= gwdnsparserlib.c
 
 OBJS1		:= $(patsubst %.c,$(BUILDDIR)/%.o,$(GWPROXYSRC))
 OBJS2		:= $(patsubst %.c,$(BUILDDIR)/%.o,$(CONVERTERSRC))
@@ -13,6 +14,7 @@ OBJS3		:= $(patsubst %.c,$(BUILDDIR)/%.o,$(INOTIFYSRC))
 OBJS5		:= $(patsubst %.c,$(BUILDDIR)/%.o,$(DNSCLIENTSRC))
 OBJS6		:= $(patsubst %.c,$(BUILDDIR)/%.o,$(GWSOCKS5LIBSRC))
 OBJS7		:= $(patsubst %.c,$(BUILDDIR)/%.o,$(GWDNSLIBSRC))
+OBJS8		:= $(patsubst %.c,$(BUILDDIR)/%.o,$(PARSERLIBSRC))
 
 TARGET1		:= $(BUILDDIR)/gwproxy
 TARGET2		:= $(BUILDDIR)/ip_converter
@@ -20,6 +22,7 @@ TARGET3		:= $(BUILDDIR)/test_inotify
 TARGET5		:= $(BUILDDIR)/dnsclient
 TARGET6		:= $(BUILDDIR)/gwsocks5lib
 TARGET7		:= $(BUILDDIR)/gwdnslib
+TARGET8		:= $(BUILDDIR)/gwdnsparserlib
 
 CC		:= gcc
 CFLAGS		:= -Wmaybe-uninitialized -Wall -Wextra -Os -g3
@@ -44,6 +47,8 @@ $(TARGET6): CFLAGS += -DRUNTEST  -DENABLE_DUMP
 $(TARGET6): $(OBJS6)
 $(TARGET7): CFLAGS += -DRUNTEST
 $(TARGET7): $(OBJS7)
+$(TARGET8): CFLAGS += -DRUNTEST
+$(TARGET8): $(OBJS8)
 
 # test without log: make CFLAGS="-DENABLE_LOG=false" -B test_conventional
 test_conventional: $(TARGET1)
@@ -55,6 +60,8 @@ test_dnsclient: $(TARGET5)
 	$< 1.1.1.1 53 google.com github.com facebook.com
 test_gwsocks5lib: $(TARGET6)
 	$<
+test_gwdnsparserlib: $(TARGET8)
+	$<
 
 .PHONY: clean
 clean:
diff --git a/gwdnsparserlib.c b/gwdnsparserlib.c
index bf19c87..aa883d9 100644
--- a/gwdnsparserlib.c
+++ b/gwdnsparserlib.c
@@ -135,8 +135,6 @@ int serialize_answ(uint16_t txid, uint8_t *in, size_t in_len, gwdns_answ_data *o
 		memcpy(&rdlength, in, sizeof(rdlength));
 		rdlength = ntohs(rdlength);
 		item->rdlength = rdlength;
-		/* TODO: for now it's always assume the size is 4 bytes long (IPv4) */
-		assert(rdlength == 4);
 		in += 2;
 
 		item->rdata = malloc(rdlength);
@@ -194,3 +192,115 @@ ssize_t construct_question(gwdns_question_part *question)
 
 	return required_len;
 }
+
+#ifdef RUNTEST
+
+void test_simulate_ipv4query(void)
+{
+	char buff[UDP_MSG_LIMIT];
+	gwdns_query_pkt *send_pkt;
+	uint8_t recv_pkt[] = {
+		/* Header (12 bytes) */
+		0x00, 0x00,		/* transaction ID - STUB! */
+		0x81, 0x80,		/* Flags: QR=1, AA=0, RD=1, RA=1, RCODE=0 */
+		0x00, 0x01,		/* QDCOUNT = 1 */
+		0x00, 0x06,		/* ANCOUNT = 6 */
+		0x00, 0x00,		/* NSCOUNT = 0 */
+		0x00, 0x00,		/* ARCOUNT = 0 */
+		
+		/* Question Section */
+		/* Pointer label compression may be used in answers */
+		0x06, 'g','o','o','g','l','e',
+		0x03, 'c','o','m',
+		0x00,			/* Terminate name */
+		0x00, 0x01,		/* QTYPE = A */
+		0x00, 0x01,		/* QCLASS = IN */
+
+		/* Answer Section (6 records) */
+		/* Each Answer record: name pointer, type, class, ttl, rdlength, rdata */
+		/* First Answer */
+		0xC0, 0x0C,		/* Name: pointer to offset 0x0C (start of question name) */
+		0x00, 0x01,		/* TYPE = A */
+		0x00, 0x01,		/* CLASS = IN */
+		0x00, 0x00, 0x08, 0x62, /* TTL = 0x00000862 = 2146 sec */
+		0x00, 0x04,		/* RDLENGTH = 4 */
+		0x4A, 0x7D, 0x18, 0x71,	/* RDATA = 74.125.24.113 */
+
+		/* Second Answer */
+		0xC0, 0x0C,
+		0x00, 0x01,
+		0x00, 0x01,
+		0x00, 0x00, 0x08, 0x62,
+		0x00, 0x04,
+		0x4A, 0x7D, 0x18, 0x65, /* 74.125.24.101 */
+
+		/* Third Answer */
+		0xC0, 0x0C,
+		0x00, 0x01,
+		0x00, 0x01,
+		0x00, 0x00, 0x08, 0x62,
+		0x00, 0x04,
+		0x4A, 0x7D, 0x18, 0x8B, /* 74.125.24.139 */
+
+		/* Fourth Answer */
+		0xC0, 0x0C,
+		0x00, 0x01,
+		0x00, 0x01,
+		0x00, 0x00, 0x08, 0x62,
+		0x00, 0x04,
+		0x4A, 0x7D, 0x18, 0x8A, /* 74.125.24.138 */
+
+		/* Fifth Answer */
+		0xC0, 0x0C,
+		0x00, 0x01,
+		0x00, 0x01,
+		0x00, 0x00, 0x08, 0x62,
+		0x00, 0x04,
+		0x4A, 0x7D, 0x18, 0x64, /* 74.125.24.100 */
+
+		/* Sixth Answer */
+		0xC0, 0x0C,
+		0x00, 0x01,
+		0x00, 0x01,
+		0x00, 0x00, 0x08, 0x62,
+		0x00, 0x04,
+		0x4A, 0x7D, 0x18, 0x66, /* 74.125.24.102 */
+	};
+	gwdns_answ_data d;
+	char first_label[] = "google";
+	char second_label[] = "com";
+
+	memset(&d, 0, sizeof(d));
+	gwdns_question_part q = {
+		.domain = "google.com",
+		.dst_buffer = (uint8_t *)buff,
+		.dst_len = sizeof(buff)
+	};
+	assert(construct_question(&q) > 0);
+
+	assert(buff[12] == 6);
+	assert(!memcmp(&buff[13], first_label, 6));
+
+	assert(buff[13 + 6] == 3);
+	assert(!memcmp(&buff[13 + 6 + 1], second_label, 3));
+
+	// fill the STUB
+	memcpy(recv_pkt, buff, 2);
+
+	send_pkt = (void *)buff;
+	assert(!serialize_answ(send_pkt->hdr.id, recv_pkt, sizeof(recv_pkt), &d));
+}
+
+void run_all_tests(void)
+{
+	test_simulate_ipv4query();
+	fprintf(stderr, "all tests passed!\n");
+}
+
+int main(void)
+{
+	run_all_tests();
+	return 0;
+}
+
+#endif
diff --git a/gwdnsparserlib.h b/gwdnsparserlib.h
index 3513442..d24e414 100644
--- a/gwdnsparserlib.h
+++ b/gwdnsparserlib.h
@@ -181,6 +181,17 @@ typedef struct {
 	size_t domain_nr;
 } gwdns_resolv_hint;
 
+/*
+* Construct question packet
+*
+* @param	prepared question
+* @return	length of bytes written into dst_buffer on success, or a negative integer on failure.
+*
+* possible error are:
+* - ENAMETOOLONG	domain name in question.name is too long.
+* - ENOBUFS		length in question.dst_len is not sufficient.
+* - EINVAL		malformed question.name
+*/
 ssize_t construct_question(gwdns_question_part *question);
 
 /*
@@ -192,10 +203,10 @@ ssize_t construct_question(gwdns_question_part *question);
 * @return	zero on success or a negative number on failure
 *
 * possible error are:
-* -EAGAIN in buffer is not sufficient, no bytes are processed, need more data.
-* -EINVAL the content of in buffer is not valid.
-* -ENOMEM failed to allocate dynamic memory.
-* -ENODATA the packet didn't contain any answers.
-* -EPROTO the DNS server can't understand your question
+* -EAGAIN	in buffer is not sufficient, no bytes are processed, need more data.
+* -EINVAL	the content of in buffer is not valid.
+* -ENOMEM	failed to allocate dynamic memory.
+* -ENODATA	the packet didn't contain any answers.
+* -EPROTO	the DNS server can't understand your question
 */
 int serialize_answ(uint16_t txid, uint8_t *in, size_t in_len, gwdns_answ_data *out);
\ No newline at end of file
-- 
ReYuki


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH gwproxy v1 2/2] add support for AAAA record type
  2025-07-28  5:14 [PATCH gwproxy v1 0/2] Introduce IPv6 support for dns parser program ReYuki
  2025-07-28  5:14 ` [PATCH gwproxy v1 1/2] add unit test ReYuki
@ 2025-07-28  5:14 ` ReYuki
  2025-07-28  5:19 ` [PATCH gwproxy v1 0/2] Introduce IPv6 support for dns parser program reyuki
       [not found] ` <CAADvAgoHZujWqiFWqjKXWHMtcJxvCBHZhdEp9Ysryy-8H6tkgg@mail.gmail.com>
  3 siblings, 0 replies; 6+ messages in thread
From: ReYuki @ 2025-07-28  5:14 UTC (permalink / raw)
  To: GNU/Weeb Mailing List; +Cc: ReYuki

From: ReYuki <me@reyuki.site>

https://datatracker.ietf.org/doc/html/rfc3596
2.1 AAAA record type

   The AAAA resource record type is a record specific to the Internet
   class that stores a single IPv6 address.

   The IANA assigned value of the type is 28 (decimal).

the following is a sample from dig command (strace -x -s 512  dig A google.com):
// dns request query packet
// header section
\xfd\x11 // id
\x01\x20 // flags
\x00\x01 // qdcount
\x00\x00 // ancount
\x00\x00 // nscount
\x00\x01 // arcount

// question section
// name
\x06\x67\x6f\x6f\x67\x6c\x65
\x03\x63\x6f\x6d
\x00\x00 // type
\x1c\x00 // class

// additional resource section
\x01\x00\x00\x29\x04\xd0\x00\x00\x00\x00\x00\x0c\x00\x0a\x00\x08\x21\x06\x68\xe2\x80\xc2\xe8\xb8

// dns response answer packet
// header section
\xfd\x11 // id
\x81\x80 // flags
\x00\x01 // qdcount
\x00\x04 // ancount
\x00\x00 // nscount
\x00\x01 // arcount

// question section
\x06\x67\x6f\x6f\x67\x6c\x65
\x03\x63\x6f\x6d
\x00
\x00\x1c // type
\x00\x01 // class

// (1) answer section
\xc0\x0c // name
\x00\x1c // type
\x00\x01 // class
\x00\x00\x06\xd1 // ttl
\x00\x10 // rdlength
\x24\x04\x68\x00\x40\x03\x0c\x0f\x00\x00\x00\x00\x00\x00\x00\x8a // rdata

// (2) answer section
\xc0\x0c
\x00\x1c
\x00\x01
\x00\x00\x06\xd1
\x00\x10
\x24\x04\x68\x00\x40\x03\x0c\x0f\x00\x00\x00\x00\x00\x00\x00\x65

// (3) answer section
\xc0\x0c
\x00\x1c
\x00\x01
\x00\x00\x06\xd1
\x00\x10
\x24\x04\x68\x00\x40\x03\x0c\x0f\x00\x00\x00\x00\x00\x00\x00\x66

// (4) answer section
\xc0\x0c
\x00\x1c
\x00\x01
\x00\x00\x06\xd1
\x00\x10
\x24\x04\x68\x00\x40\x03\x0c\x0f\x00\x00\x00\x00\x00\x00\x00\x64

// additional resource section
\x00\x00\x29\x10\x00\x00\x00\x00\x00\x00\x00
---
 dnsclient.c      |  7 +++++--
 gwdnsparserlib.c | 14 +++++++++++++-
 gwdnsparserlib.h |  4 +++-
 3 files changed, 21 insertions(+), 4 deletions(-)

diff --git a/dnsclient.c b/dnsclient.c
index 4d96b18..6f2e66c 100644
--- a/dnsclient.c
+++ b/dnsclient.c
@@ -83,6 +83,7 @@ static int send_query(struct io_uring *ring, int sockfd, char *domain, gwdns_que
 	q.domain = domain;
 	q.dst_buffer = buff->question;
 	q.dst_len = UDP_MSG_LIMIT;
+	q.type = TYPE_AAAA;
 	payload_len = construct_question(&q);
 	if (payload_len < 0)
 		return -1;
@@ -161,13 +162,15 @@ static int resolv_addr(char **domains, gwdns_resolv_hint *hint, gwdns_resolv_ctx
 		ctx->sqe_nr--;
 		printf("cqe->res=%d cqe->user_data=%llx\n", cqe->res, CLEAR_ID(cqe->user_data));
 		if (EXTRACT_ID(cqe->user_data) == ID) {
-			char ipstr[INET_ADDRSTRLEN];
+			char ipstr[INET6_ADDRSTRLEN];
 			gwdns_answ_data d;
 
 			cqe->user_data = CLEAR_ID(cqe->user_data);
 			ret = serialize_answ((uint16_t)cqe->user_data, (uint8_t *)buff[idx].answr, cqe->res, &d);
 			for (size_t i = 0; i < d.hdr.ancount; i++) {
-				inet_ntop(AF_INET, d.rr_answ[i]->rdata, ipstr, sizeof(ipstr));
+				gwdns_serialized_answ *p = d.rr_answ[i];
+				int sa_family = p->rr_type == TYPE_AAAA ? AF_INET6 : AF_INET;
+				inet_ntop(sa_family, p->rdata, ipstr, sizeof(ipstr));
 				printf("%s\n", ipstr);
 			}
 			
diff --git a/gwdnsparserlib.c b/gwdnsparserlib.c
index aa883d9..022ccc2 100644
--- a/gwdnsparserlib.c
+++ b/gwdnsparserlib.c
@@ -1,3 +1,5 @@
+#define _DEFAULT_SOURCE
+#include <endian.h>
 #include "gwdnsparserlib.h"
 
 static ssize_t construct_qname(uint8_t *dst, size_t dst_len, const char *qname)
@@ -126,14 +128,21 @@ int serialize_answ(uint16_t txid, uint8_t *in, size_t in_len, gwdns_answ_data *o
 		in += 2; // NAME
 
 		memcpy(&item->rr_type, in, 2);
+		item->rr_type = ntohs(item->rr_type);
 		in += 2; // TYPE
 		memcpy(&item->rr_class, in, 2);
+		item->rr_class = ntohs(item->rr_class);
 		in += 2; // CLASS
 		memcpy(&item->ttl, in, 4);
+		item->ttl = be32toh(item->ttl);
 		in += 4; // TTL
 
 		memcpy(&rdlength, in, sizeof(rdlength));
 		rdlength = ntohs(rdlength);
+		if (item->rr_type == TYPE_AAAA && rdlength != sizeof(struct in6_addr))
+			return -EINVAL;
+		if (item->rr_type == TYPE_A && rdlength != sizeof(struct in_addr))
+			return -EINVAL;
 		item->rdlength = rdlength;
 		in += 2;
 
@@ -153,6 +162,9 @@ ssize_t construct_question(gwdns_question_part *question)
 	size_t required_len;
 	ssize_t bw;
 
+	if (question->type != TYPE_AAAA && question->type != TYPE_A)
+		return -EINVAL;
+
 	hdr = &pkt.hdr;
 	/*
 	* the memset implicitly set opcode to query
@@ -177,7 +189,7 @@ ssize_t construct_question(gwdns_question_part *question)
 		return bw;
 
 	pkt.body[bw++] = 0x0;
-	qtype = htons(TYPE_A);
+	qtype = htons(question->type);
 	qclass = htons(CLASS_IN);
 	memcpy(&pkt.body[bw], &qtype, 2);
 	bw += 2;
diff --git a/gwdnsparserlib.h b/gwdnsparserlib.h
index d24e414..c5948d0 100644
--- a/gwdnsparserlib.h
+++ b/gwdnsparserlib.h
@@ -91,6 +91,7 @@ typedef enum {
 	TYPE_MINFO	= 14,	// mailbox or mail list information
 	TYPE_MX		= 15,	// mail exchange
 	TYPE_TXT	= 16,	// text strings
+	TYPE_AAAA	= 28,	// text strings
 	QTYPE_AXFR	= 252,	// A request for a transfer of an entire zone
 	QTYPE_MAILB	= 253,	// A request for mailbox-related records (MB, MG or MR)
 	QTYPE_ALL	= 255	// A request for all records
@@ -119,6 +120,7 @@ typedef struct {
 
 typedef struct {
 	uint8_t *dst_buffer;
+	uint16_t type;
 	size_t dst_len;
 	char *domain;
 } gwdns_question_part;
@@ -190,7 +192,7 @@ typedef struct {
 * possible error are:
 * - ENAMETOOLONG	domain name in question.name is too long.
 * - ENOBUFS		length in question.dst_len is not sufficient.
-* - EINVAL		malformed question.name
+* - EINVAL		malformed or unsupported value in question data
 */
 ssize_t construct_question(gwdns_question_part *question);
 
-- 
ReYuki


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH gwproxy v1 0/2] Introduce IPv6 support for dns parser program
  2025-07-28  5:14 [PATCH gwproxy v1 0/2] Introduce IPv6 support for dns parser program ReYuki
  2025-07-28  5:14 ` [PATCH gwproxy v1 1/2] add unit test ReYuki
  2025-07-28  5:14 ` [PATCH gwproxy v1 2/2] add support for AAAA record type ReYuki
@ 2025-07-28  5:19 ` reyuki
       [not found] ` <CAADvAgoHZujWqiFWqjKXWHMtcJxvCBHZhdEp9Ysryy-8H6tkgg@mail.gmail.com>
  3 siblings, 0 replies; 6+ messages in thread
From: reyuki @ 2025-07-28  5:19 UTC (permalink / raw)
  To: GNU/Weeb Mailing List

[-- Attachment #1: Type: text/html, Size: 596 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH gwproxy v1 0/2] Introduce IPv6 support for dns parser program
       [not found] ` <CAADvAgoHZujWqiFWqjKXWHMtcJxvCBHZhdEp9Ysryy-8H6tkgg@mail.gmail.com>
@ 2025-07-28  5:22   ` reyuki
       [not found]   ` <CAADvAgpRnL_GtvaHrM7JY4T_-qm9EZ0Lcg_dwSPQ+w60_esnmQ@mail.gmail.com>
  1 sibling, 0 replies; 6+ messages in thread
From: reyuki @ 2025-07-28  5:22 UTC (permalink / raw)
  To: GNU/Weeb Mailing List

[-- Attachment #1: Type: text/html, Size: 712 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH gwproxy v1 0/2] Introduce IPv6 support for dns parser program
       [not found]   ` <CAADvAgpRnL_GtvaHrM7JY4T_-qm9EZ0Lcg_dwSPQ+w60_esnmQ@mail.gmail.com>
@ 2025-07-28  5:26     ` Re Yuki
  0 siblings, 0 replies; 6+ messages in thread
From: Re Yuki @ 2025-07-28  5:26 UTC (permalink / raw)
  To: GNU/Weeb Mailing List

On Mon, Jul 28, 2025 at 12:22 PM reyuki <me@reyuki.site> wrote:
>
> On Mon, Jul 28, 2025 at 12:19 PM reyuki <me@reyuki.site> wrote:
> >
> > On Mon, Jul 28, 2025 at 12:15 PM ReYuki <reyuki@gnuweeb.org> wrote:
> > >
> > > From: ReYuki <me@reyuki.site>
> > >
> > > Introduce IPv6 support for dns parser program
> >
> > This is just a test to learn the git send-email feature, anyone can
> > ignore this email.
>
> test plain text

test again.

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2025-07-28  5:26 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-07-28  5:14 [PATCH gwproxy v1 0/2] Introduce IPv6 support for dns parser program ReYuki
2025-07-28  5:14 ` [PATCH gwproxy v1 1/2] add unit test ReYuki
2025-07-28  5:14 ` [PATCH gwproxy v1 2/2] add support for AAAA record type ReYuki
2025-07-28  5:19 ` [PATCH gwproxy v1 0/2] Introduce IPv6 support for dns parser program reyuki
     [not found] ` <CAADvAgoHZujWqiFWqjKXWHMtcJxvCBHZhdEp9Ysryy-8H6tkgg@mail.gmail.com>
2025-07-28  5:22   ` reyuki
     [not found]   ` <CAADvAgpRnL_GtvaHrM7JY4T_-qm9EZ0Lcg_dwSPQ+w60_esnmQ@mail.gmail.com>
2025-07-28  5:26     ` Re Yuki

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox