mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Qingtao Cao <qingtao.cao.au@gmail.com>
To: dalias@aerifal.cx, musl@lists.openwall.com
Cc: qingtao.cao.au@gmail.com, Qingtao Cao <qingtao.cao@digi.com>
Subject: [musl] [PATCH 1/1] musl/resolver: serialise querying name servers
Date: Sun, 23 Oct 2022 21:18:01 +1000	[thread overview]
Message-ID: <20221023111801.515290-1-qingtao.cao@digi.com> (raw)

Querying all configured DNS servers in parallel boost performance but makes
it impossible to prefer private DNS servers to public ones. Serialise DNS
servers access from the top of the nameserver specified in resolv.conf and
only move on to the next, or less preferred DNS server when the current one
either timed out or replied NOERROR and NXDOMAIN for all queries

When querying the next or less perferred DNS servers, only unsuccessfully
resolved host names were queried. When replies are received, they won't replace
a perviously successful answer. This ensures successful answers from higher
prioritised DNS servers won't be overridden

This eliminates a racy condition when different DNS servers replies differently
to a query, the last received one wins

Last but not least, using a local array to save the currently received reply to
simplify the logic to re-shuffle answers[next] to the corresponding answers[i]

Signed-off-by: Qingtao Cao <qingtao.cao@digi.com>
---
 src/network/res_msend.c | 100 +++++++++++++++++++++++++++-------------
 1 file changed, 68 insertions(+), 32 deletions(-)

diff --git a/src/network/res_msend.c b/src/network/res_msend.c
index 9adaea13..130ac4fe 100644
--- a/src/network/res_msend.c
+++ b/src/network/res_msend.c
@@ -10,6 +10,7 @@
 #include <unistd.h>
 #include <errno.h>
 #include <pthread.h>
+#include <arpa/nameser.h>
 #include "stdio_impl.h"
 #include "syscall.h"
 #include "lookup.h"
@@ -41,11 +42,12 @@ int __res_msend_rc(int nqueries, const unsigned char *const *queries,
 	int nns = 0;
 	int family = AF_INET;
 	int rlen;
-	int next;
-	int i, j;
+	int i;
 	int cs;
 	struct pollfd pfd;
 	unsigned long t0, t1, t2;
+	int ns_index, rcode, all_done, unanswered;
+	unsigned char answer[512];
 
 	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
 
@@ -115,19 +117,27 @@ int __res_msend_rc(int nqueries, const unsigned char *const *queries,
 	pfd.fd = fd;
 	pfd.events = POLLIN;
 	retry_interval = timeout / attempts;
-	next = 0;
+
+	ns_index = 0; /* Start from the top name server */
+
+next_ns:
 	t0 = t2 = mtime();
 	t1 = t2 - retry_interval;
 
+	unanswered = 0;
+
 	for (; t2-t0 < timeout; t2=mtime()) {
 		if (t2-t1 >= retry_interval) {
-			/* Query all configured namservers in parallel */
-			for (i=0; i<nqueries; i++)
-				if (!alens[i])
-					for (j=0; j<nns; j++)
-						sendto(fd, queries[i],
-							qlens[i], MSG_NOSIGNAL,
-							(void *)&ns[j], sl);
+			for (i=0; i<nqueries; i++) {
+				/* Only query unresolved or negative resolved host names */
+				rcode = answers[i][3] & 15;
+				if (!alens[i] || rcode != NOERROR) {
+					sendto(fd, queries[i],
+						qlens[i], MSG_NOSIGNAL,
+						(void *)&ns[ns_index], sl);
+					unanswered++;
+				}
+			}
 			t1 = t2;
 			servfail_retry = 2 * nqueries;
 		}
@@ -135,51 +145,77 @@ int __res_msend_rc(int nqueries, const unsigned char *const *queries,
 		/* Wait for a response, or until time to retry */
 		if (poll(&pfd, 1, t1+retry_interval-t2) <= 0) continue;
 
-		while ((rlen = recvfrom(fd, answers[next], asize, 0,
+		while ((rlen = recvfrom(fd, answer, 512, 0,
 		  (void *)&sa, (socklen_t[1]){sl})) >= 0) {
 
 			/* Ignore non-identifiable packets */
 			if (rlen < 4) continue;
 
 			/* Ignore replies from addresses we didn't send to */
-			for (j=0; j<nns && memcmp(ns+j, &sa, sl); j++);
-			if (j==nns) continue;
+			if (memcmp(ns+ns_index, &sa, sl))
+				continue;
 
 			/* Find which query this answer goes with, if any */
-			for (i=next; i<nqueries && (
-				answers[next][0] != queries[i][0] ||
-				answers[next][1] != queries[i][1] ); i++);
+			for (i=0; i<nqueries && (
+				answer[0] != queries[i][0] ||
+				answer[1] != queries[i][1] ); i++);
 			if (i==nqueries) continue;
-			if (alens[i]) continue;
+
+			rcode = answers[i][3] & 15;
+			if (alens[i] && rcode == NOERROR) {
+				/* Do not override successful answers from a more prioritied server */
+				continue;
+			}
 
 			/* Only accept positive or negative responses;
 			 * retry immediately on server failure, and ignore
 			 * all other codes such as refusal. */
-			switch (answers[next][3] & 15) {
-			case 0:
-			case 3:
-				break;
-			case 2:
+			rcode = answer[3] & 15;
+			switch (rcode) {
+			case NOERROR:
+			case NXDOMAIN:
+				alens[i] = rlen;
+				memcpy(answers[i], answer, rlen);
+				unanswered--;
+				continue;
+			case SERVFAIL:
 				if (servfail_retry && servfail_retry--)
 					sendto(fd, queries[i],
 						qlens[i], MSG_NOSIGNAL,
-						(void *)&ns[j], sl);
+						(void *)&ns[ns_index], sl);
 			default:
 				continue;
 			}
+		}
 
-			/* Store answer in the right slot, or update next
-			 * available temp slot if it's already in place. */
-			alens[i] = rlen;
-			if (i == next)
-				for (; next<nqueries && alens[next]; next++);
-			else
-				memcpy(answers[i], answers[next], rlen);
+		/* Check if all sent queries have got NOERROR or NXDOMAIN replies from this server,
+		 * no need to retry it anymore even when not timed out yet. When the unanswered counter
+		 * drops to zero, any remaining NXDOMAIN replies were sure sent from this server */
+		if (!unanswered) {
+			all_done = 1;
+			for (i = 0; i < nqueries; i++) {
+				rcode = answers[i][3] & 15;
+				if (!(alens[i] && ((rcode == NOERROR) || (rcode == NXDOMAIN)))) {
+					all_done = 0;
+					break;
+				}
+			}
 
-			if (next == nqueries) goto out;
+			if (all_done)
+				break;
 		}
 	}
-out:
+
+	/* Check if any queries is unresolved or unsuccessful (regardless of failure code)
+	 * try the next DNS server if available */
+	if (++ns_index < conf->nns) {
+		for (i = 0; i < nqueries; i++) {
+			rcode = answers[i][3] & 15;
+			if (!alens[i] || rcode != NOERROR)
+				goto next_ns;
+		}
+	}
+
 	pthread_cleanup_pop(1);
 
 	return 0;
-- 
2.34.1


             reply	other threads:[~2022-10-23 11:18 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-23 11:18 Qingtao Cao [this message]
2022-10-23 15:32 ` Rich Felker
2022-10-24  1:06   ` Qingtao Cao

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=20221023111801.515290-1-qingtao.cao@digi.com \
    --to=qingtao.cao.au@gmail.com \
    --cc=dalias@aerifal.cx \
    --cc=musl@lists.openwall.com \
    --cc=qingtao.cao@digi.com \
    /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.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).