#define DNS_RCODE_REFUSED 5 /* query refused */
/* dns flags masks */
+#define DNS_FLAG_RESPONSE 0x8000 /* mask for response (QR) flag */
#define DNS_FLAG_TRUNCATED 0x0200 /* mask for truncated flag */
#define DNS_FLAG_REPLYCODE 0x000F /* mask for reply code */
--- /dev/null
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+# DNS fixture answering with or without the QR (response) bit.
+
+import argparse
+import socket
+import struct
+
+TEST_ADDRESS = "192.0.2.123"
+
+
+def parse_question(query):
+ offset = 12
+ labels = []
+ while query[offset]:
+ length = query[offset]
+ offset += 1
+ labels.append(query[offset : offset + length].decode())
+ offset += length
+ offset += 1
+ return ".".join(labels), query[12 : offset + 4]
+
+
+def main():
+ parser = argparse.ArgumentParser()
+ parser.add_argument("--port", type=int, required=True)
+ args = parser.parse_args()
+
+ udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
+ udp.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
+ udp.bind(("127.0.0.1", args.port))
+ print("READY", flush=True)
+
+ while True:
+ query, peer = udp.recvfrom(65535)
+ name, question = parse_question(query)
+ # same answer in both cases, only the QR bit differs
+ flags = 0x0180 if name == "no-qr.test" else 0x8180
+ header = query[:2] + struct.pack("!HHHHH", flags, 1, 1, 0, 0)
+ address = socket.inet_aton(TEST_ADDRESS)
+ answer = b"\xc0\x0c" + struct.pack("!HHIH", 1, 1, 30, len(address)) + address
+ udp.sendto(header + question + answer, peer)
+
+
+if __name__ == "__main__":
+ main()
--- /dev/null
+varnishtest "DNS messages without the QR bit are not responses"
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+#REGTEST_TYPE=devel
+
+feature ignore_unknown_macro
+feature cmd "python3 --version"
+
+# Reserve a random local port, then release its TCP listener so the Python
+# fixture can bind UDP to it.
+server s10 {} -start
+server s10 -break
+
+process p1 "python3 ${testdir}/response_flag.py --port ${s10_port}" -start
+process p1 -expect-text 0 0 "READY"
+
+haproxy h1 -conf {
+ defaults
+ mode http
+ timeout connect "${HAPROXY_TEST_TIMEOUT-5s}"
+ timeout client "${HAPROXY_TEST_TIMEOUT-5s}"
+ timeout server "${HAPROXY_TEST_TIMEOUT-5s}"
+
+ resolvers dns
+ nameserver dns udp@127.0.0.1:${s10_port}
+ timeout resolve 30s
+ timeout retry 200ms
+ resolve_retries 1
+
+ frontend fe
+ bind "fd@${fe}"
+ http-request do-resolve(txn.ip,dns,ipv4) hdr(host)
+ http-request return status 200 hdr x-ip %[var(txn.ip)] if { var(txn.ip) -m found }
+ http-request return status 503
+} -start
+
+client c1 -connect ${h1_fe_sock} {
+ txreq -hdr "Host: qr.test"
+ rxresp
+ expect resp.status == 200
+ expect resp.http.x-ip == "192.0.2.123"
+
+ txreq -hdr "Host: no-qr.test"
+ rxresp
+ expect resp.status == 503
+} -run
+
+haproxy h1 -cli {
+ send "show resolvers dns"
+ expect ~ "invalid: +[1-9][0-9]*"
+}
flags = reader[0] * 256 + reader[1];
+ /* a message without the QR bit is a query, not a response */
+ if (!(flags & DNS_FLAG_RESPONSE))
+ goto invalid_resp;
+
if ((flags & DNS_FLAG_REPLYCODE) != DNS_RCODE_NO_ERROR) {
if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_NX_DOMAIN) {
cause = RSLV_RESP_NX_DOMAIN;