|
|
@@ -29,9 +29,9 @@ import (
|
|
|
type ClassicNameServer struct {
|
|
|
sync.RWMutex
|
|
|
name string
|
|
|
- address *net.Destination
|
|
|
- ips map[string]*record
|
|
|
- requests map[uint16]*dnsRequest
|
|
|
+ address net.Destination
|
|
|
+ ips map[string]record
|
|
|
+ requests map[uint16]dnsRequest
|
|
|
pub *pubsub.Service
|
|
|
udpServer *udp.Dispatcher
|
|
|
cleanup *task.Periodic
|
|
|
@@ -46,9 +46,9 @@ func NewClassicNameServer(address net.Destination, dispatcher routing.Dispatcher
|
|
|
}
|
|
|
|
|
|
s := &ClassicNameServer{
|
|
|
- address: &address,
|
|
|
- ips: make(map[string]*record),
|
|
|
- requests: make(map[uint16]*dnsRequest),
|
|
|
+ address: address,
|
|
|
+ ips: make(map[string]record),
|
|
|
+ requests: make(map[uint16]dnsRequest),
|
|
|
pub: pubsub.NewService(),
|
|
|
name: strings.ToUpper(address.String()),
|
|
|
}
|
|
|
@@ -85,7 +85,6 @@ func (s *ClassicNameServer) Cleanup() error {
|
|
|
}
|
|
|
|
|
|
if record.A == nil && record.AAAA == nil {
|
|
|
- newError(s.name, " cleanup ", domain).AtDebug().WriteToLog()
|
|
|
delete(s.ips, domain)
|
|
|
} else {
|
|
|
s.ips[domain] = record
|
|
|
@@ -93,7 +92,7 @@ func (s *ClassicNameServer) Cleanup() error {
|
|
|
}
|
|
|
|
|
|
if len(s.ips) == 0 {
|
|
|
- s.ips = make(map[string]*record)
|
|
|
+ s.ips = make(map[string]record)
|
|
|
}
|
|
|
|
|
|
for id, req := range s.requests {
|
|
|
@@ -103,7 +102,7 @@ func (s *ClassicNameServer) Cleanup() error {
|
|
|
}
|
|
|
|
|
|
if len(s.requests) == 0 {
|
|
|
- s.requests = make(map[uint16]*dnsRequest)
|
|
|
+ s.requests = make(map[uint16]dnsRequest)
|
|
|
}
|
|
|
|
|
|
return nil
|
|
|
@@ -141,17 +140,15 @@ func (s *ClassicNameServer) HandleResponse(ctx context.Context, packet *udp_prot
|
|
|
elapsed := time.Since(req.start)
|
|
|
newError(s.name, " got answer: ", req.domain, " ", req.reqType, " -> ", ipRec.IP, " ", elapsed).AtInfo().WriteToLog()
|
|
|
if len(req.domain) > 0 && (rec.A != nil || rec.AAAA != nil) {
|
|
|
- s.updateIP(req.domain, &rec)
|
|
|
+ s.updateIP(req.domain, rec)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func (s *ClassicNameServer) updateIP(domain string, newRec *record) {
|
|
|
+func (s *ClassicNameServer) updateIP(domain string, newRec record) {
|
|
|
s.Lock()
|
|
|
|
|
|
- rec, found := s.ips[domain]
|
|
|
- if !found {
|
|
|
- rec = &record{}
|
|
|
- }
|
|
|
+ newError(s.name, " updating IP records for domain:", domain).AtDebug().WriteToLog()
|
|
|
+ rec := s.ips[domain]
|
|
|
|
|
|
updated := false
|
|
|
if isNewer(rec.A, newRec.A) {
|
|
|
@@ -164,7 +161,6 @@ func (s *ClassicNameServer) updateIP(domain string, newRec *record) {
|
|
|
}
|
|
|
|
|
|
if updated {
|
|
|
- newError(s.name, " updating IP records for domain:", domain).AtDebug().WriteToLog()
|
|
|
s.ips[domain] = rec
|
|
|
}
|
|
|
if newRec.A != nil {
|
|
|
@@ -187,7 +183,7 @@ func (s *ClassicNameServer) addPendingRequest(req *dnsRequest) {
|
|
|
|
|
|
id := req.msg.ID
|
|
|
req.expire = time.Now().Add(time.Second * 8)
|
|
|
- s.requests[id] = req
|
|
|
+ s.requests[id] = *req
|
|
|
}
|
|
|
|
|
|
func (s *ClassicNameServer) sendQuery(ctx context.Context, domain string, clientIP net.IP, option dns_feature.IPOption) {
|
|
|
@@ -205,7 +201,7 @@ func (s *ClassicNameServer) sendQuery(ctx context.Context, domain string, client
|
|
|
udpCtx = session.ContextWithContent(udpCtx, &session.Content{
|
|
|
Protocol: "dns",
|
|
|
})
|
|
|
- s.udpServer.Dispatch(udpCtx, *s.address, b)
|
|
|
+ s.udpServer.Dispatch(udpCtx, s.address, b)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -218,30 +214,30 @@ func (s *ClassicNameServer) findIPsForDomain(domain string, option dns_feature.I
|
|
|
return nil, errRecordNotFound
|
|
|
}
|
|
|
|
|
|
- var err4 error
|
|
|
- var err6 error
|
|
|
var ips []net.Address
|
|
|
- var ip6 []net.Address
|
|
|
-
|
|
|
- switch {
|
|
|
- case option.IPv4Enable:
|
|
|
- ips, err4 = record.A.getIPs()
|
|
|
- fallthrough
|
|
|
- case option.IPv6Enable:
|
|
|
- ip6, err6 = record.AAAA.getIPs()
|
|
|
- ips = append(ips, ip6...)
|
|
|
+ var lastErr error
|
|
|
+ if option.IPv4Enable {
|
|
|
+ a, err := record.A.getIPs()
|
|
|
+ if err != nil {
|
|
|
+ lastErr = err
|
|
|
+ }
|
|
|
+ ips = append(ips, a...)
|
|
|
}
|
|
|
|
|
|
- if len(ips) > 0 {
|
|
|
- return toNetIP(ips)
|
|
|
+ if option.IPv6Enable {
|
|
|
+ aaaa, err := record.AAAA.getIPs()
|
|
|
+ if err != nil {
|
|
|
+ lastErr = err
|
|
|
+ }
|
|
|
+ ips = append(ips, aaaa...)
|
|
|
}
|
|
|
|
|
|
- if err4 != nil {
|
|
|
- return nil, err4
|
|
|
+ if len(ips) > 0 {
|
|
|
+ return toNetIP(ips)
|
|
|
}
|
|
|
|
|
|
- if err6 != nil {
|
|
|
- return nil, err6
|
|
|
+ if lastErr != nil {
|
|
|
+ return nil, lastErr
|
|
|
}
|
|
|
|
|
|
return nil, dns_feature.ErrEmptyResponse
|