net/dns: ensure multiple hosts with the same IP address are combined into a single HostEntry

This ensures that each line has a unique IP address.

Fixes #11939

Signed-off-by: Aaron Klotz <aaron@tailscale.com>
agottardo-patch-1
Aaron Klotz 5 months ago
parent 0d6e71df70
commit d7a4f9d31c

@ -17,6 +17,7 @@ import (
"sync/atomic" "sync/atomic"
"time" "time"
xmaps "golang.org/x/exp/maps"
"tailscale.com/control/controlknobs" "tailscale.com/control/controlknobs"
"tailscale.com/health" "tailscale.com/health"
"tailscale.com/net/dns/resolver" "tailscale.com/net/dns/resolver"
@ -122,6 +123,7 @@ func (m *Manager) Set(cfg Config) error {
// The returned list is sorted by the first hostname in each entry. // The returned list is sorted by the first hostname in each entry.
func compileHostEntries(cfg Config) (hosts []*HostEntry) { func compileHostEntries(cfg Config) (hosts []*HostEntry) {
didLabel := make(map[string]bool, len(cfg.Hosts)) didLabel := make(map[string]bool, len(cfg.Hosts))
hostsMap := make(map[netip.Addr]*HostEntry, len(cfg.Hosts))
for _, sd := range cfg.SearchDomains { for _, sd := range cfg.SearchDomains {
for h, ips := range cfg.Hosts { for h, ips := range cfg.Hosts {
if !sd.Contains(h) || h.NumLabels() != (sd.NumLabels()+1) { if !sd.Contains(h) || h.NumLabels() != (sd.NumLabels()+1) {
@ -136,15 +138,23 @@ func compileHostEntries(cfg Config) (hosts []*HostEntry) {
if cfg.OnlyIPv6 && ip.Is4() { if cfg.OnlyIPv6 && ip.Is4() {
continue continue
} }
hosts = append(hosts, &HostEntry{ if e := hostsMap[ip]; e != nil {
Addr: ip, e.Hosts = append(e.Hosts, ipHosts...)
Hosts: ipHosts, } else {
}) hostsMap[ip] = &HostEntry{
Addr: ip,
Hosts: ipHosts,
}
}
// Only add IPv4 or IPv6 per host, like we do in the resolver. // Only add IPv4 or IPv6 per host, like we do in the resolver.
break break
} }
} }
} }
if len(hostsMap) == 0 {
return nil
}
hosts = xmaps.Values(hostsMap)
slices.SortFunc(hosts, func(a, b *HostEntry) int { slices.SortFunc(hosts, func(a, b *HostEntry) int {
if len(a.Hosts) == 0 && len(b.Hosts) == 0 { if len(a.Hosts) == 0 && len(b.Hosts) == 0 {
return 0 return 0

@ -87,8 +87,7 @@ func TestCompileHostEntries(t *testing.T) {
{Addr: netip.MustParseAddr("1.1.1.1"), Hosts: []string{"a.foo.ts.net.", "a"}}, {Addr: netip.MustParseAddr("1.1.1.1"), Hosts: []string{"a.foo.ts.net.", "a"}},
{Addr: netip.MustParseAddr("1.1.1.2"), Hosts: []string{"b.foo.ts.net.", "b"}}, {Addr: netip.MustParseAddr("1.1.1.2"), Hosts: []string{"b.foo.ts.net.", "b"}},
{Addr: netip.MustParseAddr("1.1.1.3"), Hosts: []string{"c.foo.ts.net.", "c"}}, {Addr: netip.MustParseAddr("1.1.1.3"), Hosts: []string{"c.foo.ts.net.", "c"}},
{Addr: netip.MustParseAddr("1.1.1.4"), Hosts: []string{"d.foo.beta.tailscale.net."}}, {Addr: netip.MustParseAddr("1.1.1.4"), Hosts: []string{"d.foo.ts.net.", "d", "d.foo.beta.tailscale.net."}},
{Addr: netip.MustParseAddr("1.1.1.4"), Hosts: []string{"d.foo.ts.net.", "d"}},
{Addr: netip.MustParseAddr("1.1.1.5"), Hosts: []string{"e.foo.beta.tailscale.net.", "e"}}, {Addr: netip.MustParseAddr("1.1.1.5"), Hosts: []string{"e.foo.beta.tailscale.net.", "e"}},
}, },
}, },
@ -103,8 +102,7 @@ func TestCompileHostEntries(t *testing.T) {
SearchDomains: []dnsname.FQDN{"foo.ts.net.", "foo.beta.tailscale.net."}, SearchDomains: []dnsname.FQDN{"foo.ts.net.", "foo.beta.tailscale.net."},
}, },
want: []*HostEntry{ want: []*HostEntry{
{Addr: netip.MustParseAddr("1.1.1.5"), Hosts: []string{"e.foo.beta.tailscale.net."}}, {Addr: netip.MustParseAddr("1.1.1.5"), Hosts: []string{"e.foo.ts.net.", "e", "e.foo.beta.tailscale.net."}},
{Addr: netip.MustParseAddr("1.1.1.5"), Hosts: []string{"e.foo.ts.net.", "e"}},
}, },
}, },
{ {
@ -120,8 +118,7 @@ func TestCompileHostEntries(t *testing.T) {
SearchDomains: []dnsname.FQDN{"foo.ts.net.", "foo.beta.tailscale.net."}, SearchDomains: []dnsname.FQDN{"foo.ts.net.", "foo.beta.tailscale.net."},
}, },
want: []*HostEntry{ want: []*HostEntry{
{Addr: netip.MustParseAddr("1.1.1.4"), Hosts: []string{"d.foo.beta.tailscale.net."}}, {Addr: netip.MustParseAddr("1.1.1.4"), Hosts: []string{"d.foo.ts.net.", "d", "d.foo.beta.tailscale.net."}},
{Addr: netip.MustParseAddr("1.1.1.4"), Hosts: []string{"d.foo.ts.net.", "d"}},
}, },
}, },
{ {
@ -139,8 +136,7 @@ func TestCompileHostEntries(t *testing.T) {
want: []*HostEntry{ want: []*HostEntry{
{Addr: netip.MustParseAddr("1.1.1.2"), Hosts: []string{"h1.foo.beta.tailscale.net."}}, {Addr: netip.MustParseAddr("1.1.1.2"), Hosts: []string{"h1.foo.beta.tailscale.net."}},
{Addr: netip.MustParseAddr("1.1.1.3"), Hosts: []string{"h1.foo.ts.net.", "h1"}}, {Addr: netip.MustParseAddr("1.1.1.3"), Hosts: []string{"h1.foo.ts.net.", "h1"}},
{Addr: netip.MustParseAddr("1.1.1.1"), Hosts: []string{"h2.foo.beta.tailscale.net."}}, {Addr: netip.MustParseAddr("1.1.1.1"), Hosts: []string{"h2.foo.ts.net.", "h2", "h2.foo.beta.tailscale.net."}},
{Addr: netip.MustParseAddr("1.1.1.1"), Hosts: []string{"h2.foo.ts.net.", "h2"}},
}, },
}, },
} }

Loading…
Cancel
Save