From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_ADSP_CUSTOM_MED, DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FROM,MAILING_LIST_MULTI, RCVD_IN_MSPIKE_H2 autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 21165 invoked from network); 23 Oct 2022 11:18:33 -0000 Received: from second.openwall.net (193.110.157.125) by inbox.vuxu.org with ESMTPUTF8; 23 Oct 2022 11:18:33 -0000 Received: (qmail 31829 invoked by uid 550); 23 Oct 2022 11:18:28 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Reply-To: musl@lists.openwall.com Received: (qmail 31788 invoked from network); 23 Oct 2022 11:18:27 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=GPopMuEdJr/dAFbrPlu3wRuGvDM2GvVW2N9ixVDupy0=; b=WLz6HEqpbepx30Qc1uaeetELkTNhzDIFgms09wSrkYPWT2LWuGeLjTbtV79RDVtzVI GtSTuUCaITdV7wC/6KS4Jrr4GWOsjbYkTpL+HL61MJTHAnyw7vEmiQooGB6yW6r88snK CvDpaAj5oFzp6OEGL0v0ybZIQrtojDttPlFHi12eOwqzYX4GY8FqMIoQ8dKJzI9cFYPG 5rY/80OqKtjoFoEa6fD1IL1vmR+YiIV/ghjGpUoIr4hJiqECGIhk2nc/tXsJCfVNi5ZW Is2AbDB4PHLoqQhoTltEqQ3WjbPIZBuxawaYhae8VAJDtRMMPHGKXn4Y4l4zDucrxMlV LVJA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=GPopMuEdJr/dAFbrPlu3wRuGvDM2GvVW2N9ixVDupy0=; b=5ZM5O8IOmXuvN5wM785uxdZQvHy9a+AxOYGG8/rl2o6p4zVmiS+QatJkH9Up0Xo0El ve6j49nNy3kmsBRzJ3sXwUIqeGaa24Fp1UIbA4xVb3/aI04Y9S4fxAqKnEIbAjeXDaGv AGRV7DrkjiFAfSKQzzs4r6BSe7Ub68pKibDm7Hky3xvC8MjLlmG0K1qsQ65zR3ysQyO5 gzw/qvBtiUzHgU/OTcMCwbWjrRiyBsLHYQ74kZwXjlyRl/9bC/a+E9YkkYnQRZBqCCrd VqRaWccsrh8G5jzyxXlnVyVKKUSePgT4pXikTkbe/Z1jommUxKt1z7vwEEGRhLmzfU69 04/A== X-Gm-Message-State: ACrzQf0aqynNTGu29xWekYxyZPbCzemPSmCPZ3hwwnSicJr22cBEFM3D dY/WbY7nuE3Frb6N58edeb0= X-Google-Smtp-Source: AMsMyM4u/6vBQxXb37Z8I7KqpHbxVonIm5lmg+MqNT0QdZQidQOOoluoPInOodu9fRXgoc97vYuKIg== X-Received: by 2002:a17:902:f707:b0:184:e44f:88cc with SMTP id h7-20020a170902f70700b00184e44f88ccmr28278210plo.42.1666523895278; Sun, 23 Oct 2022 04:18:15 -0700 (PDT) From: Qingtao Cao X-Google-Original-From: Qingtao Cao To: dalias@aerifal.cx, musl@lists.openwall.com Cc: qingtao.cao.au@gmail.com, Qingtao Cao Date: Sun, 23 Oct 2022 21:18:01 +1000 Message-Id: <20221023111801.515290-1-qingtao.cao@digi.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [musl] [PATCH 1/1] musl/resolver: serialise querying name servers 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 --- 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 #include #include +#include #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= 0) { /* Ignore non-identifiable packets */ if (rlen < 4) continue; /* Ignore replies from addresses we didn't send to */ - for (j=0; jnns) { + 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