From dddc024f6468b73d054fc05d95ee676478ac778b Mon Sep 17 00:00:00 2001 From: mykola2312 <49044616+mykola2312@users.noreply.github.com> Date: Wed, 29 Jan 2025 06:17:14 +0200 Subject: [PATCH] implement IPv6 in DNS --- node/lux_dns.go | 43 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/node/lux_dns.go b/node/lux_dns.go index 4ac9d64..4dcd8c6 100644 --- a/node/lux_dns.go +++ b/node/lux_dns.go @@ -9,7 +9,7 @@ import ( "strings" ) -const LUX_DNS_TTL uint32 = 60 * 5 // 5 minutes +const LUX_DNS_TTL uint32 = 60 * 1 // 1 minute type LuxDnsServer struct { node *LuxNode @@ -75,6 +75,9 @@ func (entry *dnsEntry) ParseDomainInfo() luxDomainInfo { } } +const dnsTypeA = 1 +const dnsTypeAAAA = 28 + func (sv *LuxDnsServer) HandleRequest(req []byte) []byte { id := NO.Uint16(req[0:2]) flags := NO.Uint16(req[2:4]) @@ -155,6 +158,10 @@ func (sv *LuxDnsServer) HandleRequest(req []byte) []byte { // answer questions for _, entry := range entries { + if entry.dnsType != dnsTypeA && entry.dnsType != dnsTypeAAAA { + continue // only A and AAAA are supported + } + info := entry.ParseDomainInfo() if info.tld != "lux" { goto refused @@ -178,7 +185,17 @@ func (sv *LuxDnsServer) HandleRequest(req []byte) []byte { goto not_found } - addr = wan.(*host.LuxOptionWAN).Addr4 + if entry.dnsType == dnsTypeA { + // IPv4 + addr = wan.(*host.LuxOptionWAN).Addr4 + } else { + // IPv6 + addr = wan.(*host.LuxOptionWAN).Addr6 + } + + if addr.IsUnspecified() { + goto not_found + } default: // look for netif opt, ok := state.Options[host.LuxOptionTypeNetIf] @@ -193,9 +210,25 @@ func (sv *LuxDnsServer) HandleRequest(req []byte) []byte { ipFound := false for _, netAddr := range netif.Addrs { - if netAddr.Type == host.LuxNetAddrType4 { - addr = netAddr.Addr - ipFound = true + if entry.dnsType == dnsTypeA { + if netAddr.Type == host.LuxNetAddrType4 { + addr = netAddr.Addr + ipFound = true + break + } + } else { + // look either for IPv6 global unicast or local unicast. + // but we prefer global over local unicast + if netAddr.Type == host.LuxNetAddrType6GUA { + addr = netAddr.Addr + ipFound = true + break + } else if netAddr.Type == host.LuxNetAddrType6ULA { + addr = netAddr.Addr + ipFound = true + // we dont break here since next addr may be global unicast, + // which we prefer over ula + } } } if !ipFound {