From d0c840766fab2867fd98faaf53e048c6df8a756b Mon Sep 17 00:00:00 2001 From: Vladimir Dubrovin <3proxy@3proxy.ru> Date: Sat, 21 Nov 2026 13:58:55 +0300 Subject: [PATCH] resolve: a name server answering with an error asks the next one (#735) A reply with no answers and an error code - FORMERR, SERVFAIL, NOTIMP, REFUSED - ended the resolution as if the name had no address, and the servers after the one that answered were never asked, so one broken server made every name it broke fail. Such an answer is passed over for the next server now; a reply without data, and one that says the name is not there, still end the search. --- man/3proxy.cfg.5 | 5 +- src/resolve.c | 8 ++- tests/cases/nserver_failover.py | 88 +++++++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 tests/cases/nserver_failover.py diff --git a/man/3proxy.cfg.5 b/man/3proxy.cfg.5 index 18ae995..042bab5 100644 --- a/man/3proxy.cfg.5 +++ b/man/3proxy.cfg.5 @@ -610,7 +610,10 @@ Use \fBauthcache\fR to speedup authentication. RADIUS feature is currently exper .BR nserver \fI\fR[:\fIport\fR][/\fItcp\fR] .br - Nameserver to use for name resolutions; up to 3, asked in order. + Nameserver to use for name resolutions; up to 3, asked in order; a +server that answers with an error (FORMERR, SERVFAIL, REFUSED and the +like) is passed over for the next one, while a name answered with no +data, or as not there, ends the search. Optional port number may be specified. If optional /tcp is added to IP address, name resolution is performed over TCP. diff --git a/src/resolve.c b/src/resolve.c index 19d1fcd..ebca292 100644 --- a/src/resolve.c +++ b/src/resolve.c @@ -177,7 +177,13 @@ static uint32_t udpresolvec(struct extparam *c, int af, unsigned char * name, un continue; /* question does not match the query */ } if((na = buf[7] + (((unsigned short)buf[6])<<8)) < 1) { - return 0; + switch(buf[3] & 0x0F){ + case 0: /* the name has no address */ + case 3: /* the name is not there */ + return 0; + default: /* the server failed: ask the next one */ + continue; + } } for(k = 13; k= 12: + self.sock.sendto(self.reply(data), addr) + + def reply(self, query): + if self.rcode: + return (query[:2] + struct.pack("!HHHHH", 0x8180 | self.rcode, 1, 0, 0, 0) + + query[12:]) + name = b"" + pos = 12 + while query[pos]: + name += query[pos + 1:pos + 1 + query[pos]] + b"." + pos += query[pos] + 1 + name = name[:-1] + answer = self.records.get(name) + if not answer: + return (query[:2] + struct.pack("!HHHHH", 0x8183, 1, 0, 0, 0) + + query[12:]) + return (query[:2] + struct.pack("!HHHHH", 0x8180, 1, 1, 0, 0) + query[12:] + + b"\xc0\x0c" + struct.pack("!HHIH", 1, 1, 60, 4) + + socket.inet_aton(answer)) + + def start(self): + self.thread.start() + return self.port + + +def run(t): + broken = DnsServer(1, {}).start() + good = DnsServer(0, {b"here.test": "127.0.0.1"}).start() + + # the destination the resolved name points at: a one-page origin + origin = t.free_port() + page = t.raw_server(origin, b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\n" + b"\r\nok", close_after=True) + + proxy = t.free_port() + t.start("nsfailover", f""" + log + flush + nserver 127.0.0.1:{broken} + nserver 127.0.0.1:{good} + auth iponly + allow * + proxy -p{proxy} + """, ports=[origin, proxy]) + + url = f"http://here.test:{origin}/" + t.eq(200, t.http(url, proxy=f"127.0.0.1:{proxy}").status, + "a name one server breaks is resolved by the next one") + + r = t.http(f"http://gone.test:{origin}/", proxy=f"127.0.0.1:{proxy}") + t.ne(200, r.status if r else 0, + "a name the last server says is not there does not resolve") + + page()