implement IPv6 in DNS
This commit is contained in:
parent
7bc2093c42
commit
dddc024f64
1 changed files with 38 additions and 5 deletions
|
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if entry.dnsType == dnsTypeA {
|
||||||
|
// IPv4
|
||||||
addr = wan.(*host.LuxOptionWAN).Addr4
|
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 entry.dnsType == dnsTypeA {
|
||||||
if netAddr.Type == host.LuxNetAddrType4 {
|
if netAddr.Type == host.LuxNetAddrType4 {
|
||||||
addr = netAddr.Addr
|
addr = netAddr.Addr
|
||||||
ipFound = true
|
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 {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue