public inbox for gwml@vger.gnuweeb.org
 help / color / mirror / Atom feed
From: Ahmad Gani <reyuki@gnuweeb.org>
To: Ammar Faizi <ammarfaizi2@gnuweeb.org>
Cc: Ahmad Gani <reyuki@gnuweeb.org>,
	Alviro Iskandar Setiawan <alviro.iskandar@gnuweeb.org>,
	GNU/Weeb Mailing List <gwml@vger.gnuweeb.org>
Subject: [PATCH gwproxy v6 11/11] dns: Add fallback mechanism for raw DNS
Date: Thu, 28 Aug 2025 21:34:33 +0700	[thread overview]
Message-ID: <20250828143444.540247-12-reyuki@gnuweeb.org> (raw)
In-Reply-To: <20250828143444.540247-1-reyuki@gnuweeb.org>

If *_PREFER_* is used in the restyp, the raw DNS backend will attempt to
retry DNS query with different address family.

Signed-off-by: Ahmad Gani <reyuki@gnuweeb.org>
---
 src/gwproxy/dns.c      | 75 ++++++++++++++++++++++++++++++++----------
 src/gwproxy/ev/epoll.c | 40 ++++++++++++++--------
 2 files changed, 84 insertions(+), 31 deletions(-)

diff --git a/src/gwproxy/dns.c b/src/gwproxy/dns.c
index 1eb0d9a67229..9f563e083fd2 100644
--- a/src/gwproxy/dns.c
+++ b/src/gwproxy/dns.c
@@ -264,11 +264,36 @@ int gwp_dns_process(struct gwp_dns_ctx *ctx, struct gwp_dns_entry *e)
 		return (int)r;
 
 	r = gwdns_parse_query(e->txid, e->service, buff, r, &ai);
-	if (r)
+	if (r) {
+		if (r == -ENODATA) {
+			uint16_t txid;
+			int af;
+
+			/* Fallback to other family if this one yield no results */
+			switch (ctx->cfg.restyp) {
+			case GWP_DNS_RESTYP_PREFER_IPV4:
+				af = AF_INET6;
+				break;
+			case GWP_DNS_RESTYP_PREFER_IPV6:
+				af = AF_INET;
+				break;
+			default:
+				goto exit_free_ai;
+				break;
+			}
+
+			txid = (uint16_t)rand();
+			r = gwdns_build_query(txid, e->name, af, e->payload, sizeof(e->payload));
+			if (r > 0) {
+				e->txid = txid;
+				e->payloadlen = (int)r;
+				r = -EAGAIN;
+			}
+		}
 		goto exit_free_ai;
+	}
 
 	e->addr = ai->ai_addr;
-	// gwp_dns_find_preferred_addr(ctx, ai, e->name, &e->addr, ctx->cfg.restyp);
 
 exit_free_ai:
 	gwdns_free_parsed_query(ai);
@@ -909,6 +934,7 @@ struct gwp_dns_entry *gwp_dns_queue(struct gwp_dns_ctx *ctx,
 #ifdef CONFIG_RAW_DNS
 	uint16_t txid;
 	ssize_t r;
+	int af;
 #endif
 
 	e = malloc(sizeof(*e));
@@ -917,25 +943,38 @@ struct gwp_dns_entry *gwp_dns_queue(struct gwp_dns_ctx *ctx,
 
 	if (ctx->cfg.use_raw_dns) {
 #ifdef CONFIG_RAW_DNS
-	if (ctx->nr_entries == ctx->entry_cap && realloc_entries(ctx))
-		return NULL;
+		if (ctx->nr_entries == ctx->entry_cap && realloc_entries(ctx))
+			return NULL;
+
+		r = __sys_socket(ctx->ns_addr.sa.sa_family, SOCK_DGRAM | SOCK_NONBLOCK, 0);
+		if (r < 0)
+			goto out_free_e;
+		e->udp_fd = (int)r;
+
+		switch (ctx->cfg.restyp) {
+		case GWP_DNS_RESTYP_PREFER_IPV4:
+		case GWP_DNS_RESTYP_IPV4_ONLY:
+			af = AF_INET;
+			break;
+		case GWP_DNS_RESTYP_PREFER_IPV6:
+		case GWP_DNS_RESTYP_IPV6_ONLY:
+			af = AF_INET6;
+			break;
+		default:
+			assert(0);
+			break;
+		}
 
-	r = __sys_socket(ctx->ns_addr.sa.sa_family, SOCK_DGRAM | SOCK_NONBLOCK, 0);
-	if (r < 0)
-		goto out_free_e;
-	e->udp_fd = (int)r;
-
-	txid = (uint16_t)rand();
-	// TODO(reyuki): avoid hard-coded AF_INET and use restyp instead
-	r = gwdns_build_query(txid, name, AF_INET, e->payload, sizeof(e->payload));
-	if (r < 0)
-		goto out_free_e;
-	e->payloadlen = (int)r;
+		txid = (uint16_t)rand();
+		r = gwdns_build_query(txid, name, af, e->payload, sizeof(e->payload));
+		if (r < 0)
+			goto out_free_e;
+		e->payloadlen = (int)r;
 #endif
 	} else {
-	e->ev_fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
-	if (e->ev_fd < 0)
-		goto out_free_e;
+		e->ev_fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
+		if (e->ev_fd < 0)
+			goto out_free_e;
 	}
 
 	/*
diff --git a/src/gwproxy/ev/epoll.c b/src/gwproxy/ev/epoll.c
index cd8fd8752473..935f7717f926 100644
--- a/src/gwproxy/ev/epoll.c
+++ b/src/gwproxy/ev/epoll.c
@@ -778,6 +778,27 @@ static int handle_connect(struct gwp_wrk *w, struct gwp_conn_pair *gcp)
 	return 0;
 }
 
+#ifdef CONFIG_RAW_DNS
+static int arm_poll_for_raw_dns_query(struct gwp_wrk *w,
+					struct gwp_conn_pair *gcp)
+{
+	struct gwp_dns_entry *gde = gcp->gde;
+	struct gwp_dns_ctx *dctx;
+	struct gwp_sockaddr addr;
+	uint8_t addrlen;
+	ssize_t r;
+
+	dctx = w->ctx->dns;
+	cp_nsaddr(dctx, &addr, &addrlen);
+	r = __sys_sendto(
+		gde->udp_fd, gde->payload, gde->payloadlen, MSG_NOSIGNAL,
+		&addr.sa, addrlen
+	);
+
+	return (int)r;
+}
+#endif
+
 static int arm_poll_for_dns_query(struct gwp_wrk *w,
 					struct gwp_conn_pair *gcp)
 {
@@ -794,18 +815,7 @@ static int arm_poll_for_dns_query(struct gwp_wrk *w,
 
 	if (w->ctx->cfg.use_raw_dns) {
 #ifdef CONFIG_RAW_DNS
-	struct gwp_dns_ctx *dctx;
-	struct gwp_sockaddr addr;
-	uint8_t addrlen;
-
-	dctx = w->ctx->dns;
-	cp_nsaddr(dctx, &addr, &addrlen);
-	r = __sys_sendto(
-		gde->udp_fd, gde->payload, gde->payloadlen, MSG_NOSIGNAL,
-		&addr.sa, addrlen
-	);
-	if (unlikely(r < 0))
-		return (int)r;
+	arm_poll_for_raw_dns_query(w, gcp);
 
 	r = __sys_epoll_ctl(w->ep_fd, EPOLL_CTL_ADD, gde->udp_fd, &ev);
 	if (unlikely(r))
@@ -853,7 +863,11 @@ static int handle_ev_dns_query(struct gwp_wrk *w, struct gwp_conn_pair *gcp)
 	if (w->ctx->cfg.use_raw_dns) {
 #ifdef CONFIG_RAW_DNS
 		r = gwp_dns_process(w->ctx->dns, gde);
-		if (r)
+		if (r == -EAGAIN) {
+			pr_dbg(&w->ctx->lh, "DNS Fallback\n");
+			arm_poll_for_raw_dns_query(w, gcp);
+			return 0;
+		} else if (r)
 			gde->res = r;
 #endif
 	} else {
-- 
Ahmad Gani


  parent reply	other threads:[~2025-08-28 14:37 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-28 14:34 [PATCH gwproxy v6 00/11] Initial work on integration of DNS parser lib in gwproxy Ahmad Gani
2025-08-28 14:34 ` [PATCH gwproxy v6 01/11] gwproxy: Fix syntax error inside assertion Ahmad Gani
2025-08-28 14:34 ` [PATCH gwproxy v6 02/11] gwproxy: Fix socks5 failure on debug mode Ahmad Gani
2025-08-28 14:34 ` [PATCH gwproxy v6 03/11] dnsparser: Add dns parser code Ahmad Gani
2025-08-28 14:34 ` [PATCH gwproxy v6 04/11] dnsparser: remove unused constant Ahmad Gani
2025-08-28 14:34 ` [PATCH gwproxy v6 05/11] dnsparser: Ignore CNAME if any Ahmad Gani
2025-08-28 14:34 ` [PATCH gwproxy v6 06/11] dns: Remove code block related to the usage of glibc's getaddrinfo_a function Ahmad Gani
2025-08-28 14:34 ` [PATCH gwproxy v6 07/11] dns: refactor dns.c to integrate the dns parser Ahmad Gani
2025-08-28 14:34 ` [PATCH gwproxy v6 08/11] dns: revert removed DNS code and disable raw DNS by default Ahmad Gani
2025-08-28 14:34 ` [PATCH gwproxy v6 09/11] test: revert DNS test-case Ahmad Gani
2025-08-28 14:34 ` [PATCH gwproxy v6 10/11] gwproxy: Add DNS server option Ahmad Gani
2025-08-28 14:34 ` Ahmad Gani [this message]
2025-08-28 21:52 ` [PATCH gwproxy v6 00/11] Initial work on integration of DNS parser lib in gwproxy Ammar Faizi
2025-08-28 22:23 ` Ammar Faizi
2025-08-29  0:54   ` Ahmad Gani
2025-08-29  1:59     ` Ammar Faizi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250828143444.540247-12-reyuki@gnuweeb.org \
    --to=reyuki@gnuweeb.org \
    --cc=alviro.iskandar@gnuweeb.org \
    --cc=ammarfaizi2@gnuweeb.org \
    --cc=gwml@vger.gnuweeb.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox