Debian Codegraph @

cmd/atomdns/handlers/dbfile/zone/walk.go

raw
1 package zone
2
3 import (
4 "codeberg.org/miekg/dns"
5 "codeberg.org/miekg/dns/cmd/atomdns/internal/dnszone"
6 "codeberg.org/miekg/dns/dnsutil"
7 )
8
9 // Walk walks the zone and calls fn on each element found, as long as f returns true the walk is continued.
10 // The order of the walk is ascending order: from apex to longest child.
11 func (z *Zone) Walk(fn func(*dnszone.Node) bool) { z.Tree.Scan(fn) }
12
13 // AuthoritativeWalk walks the the zone, but keeps track of authoritative names and call fn auth a boolean
14 // indicating is the name is considered that.
15 func (z *Zone) AuthoritativeWalk(fn func(*dnszone.Node, bool) bool) {
16 delegated := map[string]struct{}{}
17
18 z.Walk(func(n *dnszone.Node) bool {
19 if len(n.Name) > len(z.Origin()) { // apex also has NSes, if we add those the entire zone would be delegated
20 for _, rr := range n.RRs {
21 if _, ok := rr.(*dns.NS); ok {
22 delegated[n.Name] = struct{}{}
23 break
24 }
25 }
26 }
27 auth, end := true, false
28 i, j := 0, 0
29 for ; !end; j, end = dnsutil.Next(n.Name, i) {
30 if len(n.Name[j:]) < len(z.Origin()) {
31 break
32 }
33 if _, ok := delegated[n.Name[j:]]; ok {
34 // If we have zone cut records, which is NSEC, DS and DELEG, this is authoritative data.
35 // This must be signed, but NOT the NSs in there.
36 auth = false
37 if zonecut(n) {
38 auth = true
39 }
40
41 break
42 }
43 i++
44 }
45
46 return fn(n, auth)
47 })
48 }
49
50 // zonecut returns true if all RR in n are needed for a zone cut. That is: is there authoritative data in this
51 // zonecut that we still have to sign like DS, NSEC.
52 func zonecut(n *dnszone.Node) bool {
53 i := 0
54 for _, rr := range n.RRs {
55 switch t := dns.RRToType(rr); t {
56 case dns.TypeNS:
57 i++
58 case dns.TypeDS:
59 i++
60 case dns.TypeDELEG:
61 i++
62 case dns.TypeNSEC, dns.TypeNSEC3:
63 i++
64 }
65 }
66 return i == len(n.RRs)
67 }