internal/clientinfo: map ::1 to the right host MAC address

So queries originating from host using ::1 as source will be recognized
properly, and treated the same as other queries from host itself.
This commit is contained in:
Cuong Manh Le
2024-04-19 14:32:09 +07:00
committed by Cuong Manh Le
parent b5cf0e2b31
commit dd9f2465be
4 changed files with 21 additions and 10 deletions
+1 -5
View File
@@ -820,11 +820,7 @@ func (p *prog) getClientInfo(remoteIP string, msg *dns.Msg) *ctrld.ClientInfo {
// If this is a query from self, but ci.IP is not loopback IP, // If this is a query from self, but ci.IP is not loopback IP,
// try using hostname mapping for lookback IP if presents. // try using hostname mapping for lookback IP if presents.
if ci.Self { if ci.Self {
loopbackIP := "127.0.0.1" if name := p.ciTable.LocalHostname(isV6); name != "" {
if isV6 {
loopbackIP = "::1"
}
if name := p.ciTable.LookupHostname(loopbackIP, ""); name != "" {
ci.Hostname = name ci.Hostname = name
} }
} }
+13
View File
@@ -14,6 +14,11 @@ import (
"github.com/Control-D-Inc/ctrld/internal/controld" "github.com/Control-D-Inc/ctrld/internal/controld"
) )
const (
ipV4Loopback = "127.0.0.1"
ipv6Loopback = "::1"
)
// IpResolver is the interface for retrieving IP from Mac. // IpResolver is the interface for retrieving IP from Mac.
type IpResolver interface { type IpResolver interface {
fmt.Stringer fmt.Stringer
@@ -322,6 +327,14 @@ func (t *Table) LookupRFC1918IPv4(mac string) string {
return "" return ""
} }
// LocalHostname returns the localhost hostname associated with loopback IP.
func (t *Table) LocalHostname(v6 bool) string {
if v6 {
return t.LookupHostname(ipv6Loopback, "")
}
return t.LookupHostname(ipV4Loopback, "")
}
type macEntry struct { type macEntry struct {
mac string mac string
src string src string
+6 -4
View File
@@ -353,8 +353,8 @@ func (d *dhcp) addSelf() {
return return
} }
hostname = normalizeHostname(hostname) hostname = normalizeHostname(hostname)
d.ip2name.Store("127.0.0.1", hostname) d.ip2name.Store(ipV4Loopback, hostname)
d.ip2name.Store("::1", hostname) d.ip2name.Store(ipv6Loopback, hostname)
found := false found := false
interfaces.ForeachInterface(func(i interfaces.Interface, prefixes []netip.Prefix) { interfaces.ForeachInterface(func(i interfaces.Interface, prefixes []netip.Prefix) {
mac := i.HardwareAddr.String() mac := i.HardwareAddr.String()
@@ -375,15 +375,17 @@ func (d *dhcp) addSelf() {
d.mac.Store(ip.String(), mac) d.mac.Store(ip.String(), mac)
d.ip.Store(mac, ip.String()) d.ip.Store(mac, ip.String())
if ip.To4() != nil { if ip.To4() != nil {
d.mac.Store("127.0.0.1", mac) d.mac.Store(ipV4Loopback, mac)
} else { } else {
d.mac.Store("::1", mac) d.mac.Store(ipv6Loopback, mac)
} }
d.mac2name.Store(mac, hostname) d.mac2name.Store(mac, hostname)
d.ip2name.Store(ip.String(), hostname) d.ip2name.Store(ip.String(), hostname)
// If we have self IP set, and this IP is it, use this IP only. // If we have self IP set, and this IP is it, use this IP only.
if ip.String() == d.selfIP { if ip.String() == d.selfIP {
found = true found = true
d.mac.Store(ipV4Loopback, mac)
d.mac.Store(ipv6Loopback, mac)
} }
} }
}) })
+1 -1
View File
@@ -95,7 +95,7 @@ func (hf *hostsFile) LookupHostnameByIP(ip string) string {
hf.mu.Lock() hf.mu.Lock()
defer hf.mu.Unlock() defer hf.mu.Unlock()
if names := hf.m[ip]; len(names) > 0 { if names := hf.m[ip]; len(names) > 0 {
isLoopback := ip == "127.0.0.1" || ip == "::1" isLoopback := ip == ipV4Loopback || ip == ipv6Loopback
for _, hostname := range names { for _, hostname := range names {
name := normalizeHostname(hostname) name := normalizeHostname(hostname)
// Ignoring ipv4/ipv6 loopback entry. // Ignoring ipv4/ipv6 loopback entry.