@@ -12,6 +12,7 @@ import (
1212 "errors"
1313 "fmt"
1414 "hash/fnv"
15+ "math"
1516 "net"
1617 "net/netip"
1718 "strconv"
@@ -173,9 +174,19 @@ func (a expaddr) get(s xaddrstatus) (out []netip.Addr) {
173174 return
174175}
175176
177+ // go.dev/play/p/t24GYIQERsp
178+ func expaddrDobSorter (a , b expaddr ) int {
179+ if a .after (b ) {
180+ return - 1
181+ } else if b .after (a ) {
182+ return 1
183+ }
184+ return 0
185+ }
186+
176187type xips struct {
177188 // protects pri, aux
178- pmu * sync.RWMutex
189+ pmu sync.RWMutex
179190 // resolved expaddr(v6 or v4) by tid; may be nil
180191 pri map [string ]expaddr
181192 // resolved expaddr(v6 or v4) by secondary tid for a uid; may be nil
@@ -227,7 +238,6 @@ func NewXips(tid, uid string, pri, sec []netip.Addr, ttl time.Time) *xips {
227238 }
228239
229240 x := & xips {
230- pmu : new (sync.RWMutex ),
231241 pri : make (map [string ]expaddr ),
232242 aux : make (map [string ]expaddr ), // sec may be nil
233243 }
@@ -291,17 +301,7 @@ func (p *xips) allips(t xaddrtyp, s xaddrstatus) (out []netip.Addr) {
291301 const all = 0
292302 addrs := append ([]expaddr {}, vals (g , all )... )
293303
294- // go.dev/play/p/t24GYIQERsp
295- sorted := core .Sort (addrs , func (a , b expaddr ) int {
296- if a .after (b ) {
297- return - 1
298- } else if b .after (a ) {
299- return 1
300- }
301- return 0
302- })
303-
304- for _ , v := range sorted {
304+ for _ , v := range core .Sort (addrs , expaddrDobSorter ) {
305305 out = append (out , v .get (s )... )
306306 }
307307 return
@@ -535,7 +535,7 @@ func (a expdomains) sizes() (alive, tot int) {
535535 if a .domains == nil {
536536 return
537537 }
538- return len (a .get (xalive )), len (a .domains )
538+ return len (a .get (xalive )), len (a .domains ) // == len(a.get(xall))
539539}
540540
541541func (a expdomains ) get (s xaddrstatus ) (out []string ) {
@@ -569,6 +569,15 @@ func (a expdomains) after(b expdomains) bool {
569569 return a .dob .After (b .dob )
570570}
571571
572+ func expdomainsDobSorter (a , b expdomains ) int {
573+ if a .after (b ) { // a is born after b (ie, a is younger)
574+ return - 1
575+ } else if b .after (a ) { // b is younger
576+ return 1
577+ }
578+ return 0
579+ }
580+
572581// xdomains tracks domains per tid+uid; there is no secondary (aux) store.
573582type xdomains struct {
574583 pmu * sync.RWMutex
@@ -613,6 +622,7 @@ func (p *xdomains) domainsFor(tid, uid string, forIP netip.Addr, s xaddrstatus)
613622 uid = core .UNKNOWN_UID_STR
614623 }
615624
625+ doms := make ([]expdomains , 0 )
616626 ttls := []time.Time {}
617627 key := tid + uid
618628
@@ -624,7 +634,11 @@ func (p *xdomains) domainsFor(tid, uid string, forIP netip.Addr, s xaddrstatus)
624634 if ! strings .HasSuffix (k , uid ) {
625635 continue
626636 }
637+ doms = append (doms , v )
638+ }
639+ for _ , v := range core .Sort (doms , expdomainsDobSorter ) {
627640 if settings .Debug {
641+ // for debugging: may have expired ttls
628642 ttls = append (ttls , v .ttl )
629643 }
630644 out = append (out , v .get (s )... )
@@ -922,18 +936,18 @@ func (t *dnsgateway) fromInternalCache(tid, uid string, q *dns.Msg, typ iptype)
922936 } else if aaaa && len (cached6s ) > 0 {
923937 cachedips = cached6s
924938 }
925-
939+ cachehit := len ( cachedips ) > 0
926940 ttlnegative := false
927941 ttl := int64 (until / time .Second )
928- if ttl <= 0 {
942+ if ttl < 0 {
929943 ttl = int64 (ttl8s / time .Second )
930944 ttlnegative = true
931945 }
932946
933- logeif (ttlnegative )("alg: response for %s by %s[%s] (q4? %t / q6? %t) realip; in cache? %v [ttl: %s / -ve? %t / until: %s] (or stale? %v)" ,
934- domain , tid , uid , a , aaaa , cachedips , core .FmtSecs (ttl ), ttlnegative , core .FmtPeriod (until ), stale )
947+ logeif (ttlnegative && cachehit )("alg: response for %s by %s[%s] (q4? %t / q6? %t) realip; in cache? %v [ttl: %s / -ve? %t / hit ? %t / until: %s] (or stale? %v)" ,
948+ domain , tid , uid , a , aaaa , cachedips , core .FmtSecs (ttl ), ttlnegative , cachehit , core .FmtPeriod (until ), stale )
935949
936- if len ( cachedips ) <= 0 {
950+ if ! cachehit {
937951 return nil , errNilCacheResponse
938952 }
939953 return xdns .AQuadAForQueryTTL (q , uint32 (ttl ), cachedips ... )
@@ -1937,6 +1951,7 @@ func (t *dnsgateway) ptrLocked(maybeAlg netip.Addr, uid, tid string, useptr bool
19371951func (t * dnsgateway ) resolvLocked (domain string , typ iptype , tid , uid string ) (ip4s , ip6s , staleips []netip.Addr , until time.Duration , targets []string ) {
19381952 partkey4 := domain + key4
19391953 partkey6 := domain + key6
1954+ until = time .Duration (math .MaxInt64 )
19401955
19411956 ip4s = make ([]netip.Addr , 0 )
19421957 ip6s = make ([]netip.Addr , 0 )
0 commit comments