fix: allow to configure both ipv4 and ipv6 for a given host
docker / docker (push) Successful in 2m37s
package / apt (push) Successful in 58s

This commit is contained in:
tchivert committed 2026-10-19 16:01:01 +02:00
1 parent 2cc8bc6b1b
commit 42d73bf190
3 files changed
+62 -37

No files matched your search

+3 -3
View File
@@ -460,7 +460,7 @@ func TestApplyEDNSCountsServerCookieValidation(t *testing.T) {
func TestHandleQueryCookieFirstAndRepeat(t *testing.T) {
resetMaps()
qcache.Flush()
hostsMap["cookie.test"] = "192.0.2.10"
hostsMap["cookie.test"] = []string{"192.0.2.10"}
clientCookie1 := []byte{0x6d, 0x36, 0x72, 0xdf, 0xf3, 0xc4, 0x63, 0x75}
clientCookie2 := []byte{0xde, 0xad, 0xbe, 0xef, 0x00, 0x11, 0x22, 0x33}
@@ -594,7 +594,7 @@ func TestHandleQueryCookieCacheHitNXDOMAIN(t *testing.T) {
func TestHandleQueryCookieAbsentOnCacheHit(t *testing.T) {
resetMaps()
qcache.Flush()
hostsMap["plain.test"] = "192.0.2.10"
hostsMap["plain.test"] = []string{"192.0.2.10"}
// Prime the cache with a cookie-carrying entry.
clientCookie := []byte{0x6d, 0x36, 0x72, 0xdf, 0xf3, 0xc4, 0x63, 0x75}
@@ -686,7 +686,7 @@ func newProbeQuery(clientCookie, serverCookie []byte) *dns.Msg {
func TestHandleQueryMalformedCookie(t *testing.T) {
resetMaps()
qcache.Flush()
hostsMap["cookie.test"] = "192.0.2.10"
hostsMap["cookie.test"] = []string{"192.0.2.10"}
// RFC 7873 section 5.2.2: valid COOKIE option lengths are 8 and 16 to
// 40 inclusive; anything else must be answered with FORMERR.
+33 -20
View File
@@ -60,7 +60,7 @@ var (
blocklistMap = make(map[string]bool)
zonesMap = make(map[string]string)
hostsMap = make(map[string]string)
hostsMap = make(map[string][]string)
up = promauto.NewGauge(prometheus.GaugeOpts{
Name: "rdns_up",
@@ -126,11 +126,24 @@ func getZoneNS(qdomain string) string {
return "."
}
func getHost(qn string) string {
if ip, exists := hostsMap[qn]; exists {
return ip
func getHost(qn, qtype string) []net.IP {
ips, exists := hostsMap[qn]
if !exists {
return nil
}
return ""
var out []net.IP
for _, s := range ips {
ip := net.ParseIP(s)
if ip == nil {
continue
}
if qtype == "A" && ip.To4() != nil {
out = append(out, ip.To4())
} else if qtype == "AAAA" && ip.To4() == nil {
out = append(out, ip)
}
}
return out
}
func doDot(s string) string {
@@ -400,13 +413,12 @@ func handleQuery(w dns.ResponseWriter, q *dns.Msg) {
return
}
if hostIP := getHost(qname); hostIP != "" {
ip := net.ParseIP(hostIP)
if (qtype == "A" && ip != nil && ip.To4() != nil) || (qtype == "AAAA" && ip != nil && ip.To4() == nil) {
r = new(dns.Msg)
rcode = dns.RcodeSuccess
r.SetRcode(q, rcode)
r.MsgHdr.RecursionAvailable = true
if hostIPs := getHost(qname, qtype); len(hostIPs) > 0 {
r = new(dns.Msg)
rcode = dns.RcodeSuccess
r.SetRcode(q, rcode)
r.MsgHdr.RecursionAvailable = true
for _, ip := range hostIPs {
if qtype == "A" {
r.Answer = append(r.Answer, &dns.A{
Hdr: dns.RR_Header{Name: doDot(qname), Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 300},
@@ -418,14 +430,14 @@ func handleQuery(w dns.ResponseWriter, q *dns.Msg) {
AAAA: ip,
})
}
qcache.Set(key, r.Copy(), cacheTTL())
if *logs {
log.Println(client, qname, qclass, qtype, getRcode(rcode))
}
writeResponse(w, q, r, client)
responses.WithLabelValues(qtype, getRcode(rcode)).Inc()
return
}
qcache.Set(key, r.Copy(), cacheTTL())
if *logs {
log.Println(client, qname, qclass, qtype, getRcode(rcode))
}
writeResponse(w, q, r, client)
responses.WithLabelValues(qtype, getRcode(rcode)).Inc()
return
}
if blocklistMap[qname] {
@@ -573,7 +585,8 @@ func loadHosts(path string) int {
parts := strings.Fields(line)
if len(parts) >= 2 && net.ParseIP(parts[0]) != nil {
for i := 1; i < len(parts); i++ {
hostsMap[strings.ToLower(parts[i])] = parts[0]
name := strings.ToLower(parts[i])
hostsMap[name] = append(hostsMap[name], parts[0])
count++
}
}
+26 -14
View File
@@ -165,7 +165,7 @@ func TestLoadZones(t *testing.T) {
func TestLoadHosts(t *testing.T) {
path := filepath.Join(t.TempDir(), "hosts")
content := "# comment\n192.168.1.10 Example.TEST other.test\nnot-an-ip broken.test\n2001:db8::1 v6.test\n"
content := "# comment\n192.168.1.10 Example.TEST other.test\nnot-an-ip broken.test\n2001:db8::1 v6.test\n2001:db8::2 dual.test\n192.168.1.20 dual.test\n"
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
t.Fatal(err)
}
@@ -173,17 +173,23 @@ func TestLoadHosts(t *testing.T) {
resetMaps()
count := loadHosts(path)
if count != 3 {
t.Errorf("count = %d, want 3", count)
if count != 5 {
t.Errorf("count = %d, want 5", count)
}
if ip := getHost("example.test"); ip != "192.168.1.10" {
t.Errorf("getHost(example.test) = %q, want 192.168.1.10", ip)
if ips := getHost("example.test", "A"); len(ips) != 1 || !ips[0].Equal(net.ParseIP("192.168.1.10")) {
t.Errorf("getHost(example.test, A) = %v, want [192.168.1.10]", ips)
}
if ip := getHost("broken.test"); ip != "" {
t.Errorf("getHost(broken.test) = %q, want empty (invalid IP must be skipped)", ip)
if ips := getHost("broken.test", "A"); len(ips) != 0 {
t.Errorf("getHost(broken.test, A) = %v, want empty (invalid IP must be skipped)", ips)
}
if ip := getHost("v6.test"); ip != "2001:db8::1" {
t.Errorf("getHost(v6.test) = %q, want 2001:db8::1", ip)
if ips := getHost("v6.test", "AAAA"); len(ips) != 1 || !ips[0].Equal(net.ParseIP("2001:db8::1")) {
t.Errorf("getHost(v6.test, AAAA) = %v, want [2001:db8::1]", ips)
}
if ips := getHost("dual.test", "A"); len(ips) != 1 || !ips[0].Equal(net.ParseIP("192.168.1.20")) {
t.Errorf("getHost(dual.test, A) = %v, want [192.168.1.20]", ips)
}
if ips := getHost("dual.test", "AAAA"); len(ips) != 1 || !ips[0].Equal(net.ParseIP("2001:db8::2")) {
t.Errorf("getHost(dual.test, AAAA) = %v, want [2001:db8::2]", ips)
}
}
@@ -201,8 +207,7 @@ func TestClientIP(t *testing.T) {
func TestHandleQueryHostsAnswer(t *testing.T) {
resetMaps()
qcache.Flush()
hostsMap["example.test"] = "192.168.1.10"
blocklistMap["example.test"] = true // ensures a fallthrough is observable
hostsMap["example.test"] = []string{"192.168.1.10", "2001:db8::10"}
// A query matches the IPv4 entry
w := &mockWriter{addr: &net.UDPAddr{IP: net.ParseIP("192.0.2.10"), Port: 5300}}
@@ -222,14 +227,21 @@ func TestHandleQueryHostsAnswer(t *testing.T) {
t.Errorf("A = %v, want 192.168.1.10", a.A)
}
// AAAA query does not match an IPv4 entry and must fall through (blocked here)
// AAAA query matches the IPv6 entry even though an IPv4 entry exists
w = &mockWriter{addr: &net.UDPAddr{IP: net.ParseIP("192.0.2.10"), Port: 5300}}
handleQuery(w, newQuery("example.test", dns.TypeAAAA, dns.ClassINET))
if w.msg == nil {
t.Fatal("no response written")
}
if w.msg.Rcode != dns.RcodeRefused {
t.Errorf("rcode = %d, want REFUSED (fallthrough to blocklist)", w.msg.Rcode)
if len(w.msg.Answer) != 1 {
t.Fatalf("answers = %d, want 1", len(w.msg.Answer))
}
aaaa, ok := w.msg.Answer[0].(*dns.AAAA)
if !ok {
t.Fatalf("answer type = %T, want *dns.AAAA", w.msg.Answer[0])
}
if !aaaa.AAAA.Equal(net.ParseIP("2001:db8::10")) {
t.Errorf("AAAA = %v, want 2001:db8::10", aaaa.AAAA)
}
}