implement IPv6 in DNS

This commit is contained in:
mykola2312 2025-01-29 06:17:14 +02:00
parent 7bc2093c42
commit dddc024f64

View file

@ -9,7 +9,7 @@ import (
"strings" "strings"
) )
const LUX_DNS_TTL uint32 = 60 * 5 // 5 minutes const LUX_DNS_TTL uint32 = 60 * 1 // 1 minute
type LuxDnsServer struct { type LuxDnsServer struct {
node *LuxNode node *LuxNode
@ -75,6 +75,9 @@ func (entry *dnsEntry) ParseDomainInfo() luxDomainInfo {
} }
} }
const dnsTypeA = 1
const dnsTypeAAAA = 28
func (sv *LuxDnsServer) HandleRequest(req []byte) []byte { func (sv *LuxDnsServer) HandleRequest(req []byte) []byte {
id := NO.Uint16(req[0:2]) id := NO.Uint16(req[0:2])
flags := NO.Uint16(req[2:4]) flags := NO.Uint16(req[2:4])
@ -155,6 +158,10 @@ func (sv *LuxDnsServer) HandleRequest(req []byte) []byte {
// answer questions // answer questions
for _, entry := range entries { for _, entry := range entries {
if entry.dnsType != dnsTypeA && entry.dnsType != dnsTypeAAAA {
continue // only A and AAAA are supported
}
info := entry.ParseDomainInfo() info := entry.ParseDomainInfo()
if info.tld != "lux" { if info.tld != "lux" {
goto refused goto refused
@ -178,7 +185,17 @@ func (sv *LuxDnsServer) HandleRequest(req []byte) []byte {
goto not_found 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: default:
// look for netif // look for netif
opt, ok := state.Options[host.LuxOptionTypeNetIf] opt, ok := state.Options[host.LuxOptionTypeNetIf]
@ -193,9 +210,25 @@ func (sv *LuxDnsServer) HandleRequest(req []byte) []byte {
ipFound := false ipFound := false
for _, netAddr := range netif.Addrs { for _, netAddr := range netif.Addrs {
if netAddr.Type == host.LuxNetAddrType4 { if entry.dnsType == dnsTypeA {
addr = netAddr.Addr if netAddr.Type == host.LuxNetAddrType4 {
ipFound = true 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 { if !ipFound {