Debian Codegraph @

cmd/atomdns/internal/dnszone/retrieve.go

raw
1 // Package implement a DNS zone, held in a binary tree. Each RR(set) that gets inserted will need to create
2 // any empty non-terminals (ENT) it possesses. I.e. inserting www.example.org into example.org is easy, but when
3 // www.a.b.c.example.org inserts we need to make sure that 'c.example.org', 'b.c.example.org' and
4 // 'a.b.c.example.org' also exist and are ENTs (have no actual RRs). For deleted the opposite must happen. As
5 // an example from RFC 4592, the record: sub.*.example. TXT "this is not a wildcard" is a fun one. As this
6 // means the '*.example' ENT exists meaning that bogus.example. gets a NODATA response instead of NXDOMAIN.
7 //
8 // Doing this on insert sucks a bit, but makes the lookup code much more simple (and correct), which is more
9 // important for a DNS server.
10 package dnszone
11
12 import (
13 "codeberg.org/miekg/dns"
14 "codeberg.org/miekg/dns/dnsutil"
15 )
16
17 // Retrieve looks up the qname and qtype in the Zone z. It returns a message with the RRs (if found) in the
18 // correct places. In case of NXDOMAIN or NODATA response the message will also contain the correct
19 // information. The optional Restart is used to generate the correct CNAME chains.
20 // When calling Retrieve for the first time re should be nil. The returned message has been copied from m and
21 // shares its buffer.
22 func Retrieve(z Interface, m *dns.Msg, re *Restart) *dns.Msg {
23 // If here, we are guaranteed that this zone has the correct origin and the qname falls in this zone.
24 // so we should be able to Prev to the first label that should fall in this zone.
25 r := m.Copy()
26 dnsutil.SetReply(r, m)
27 r.Authoritative = true
28
29 // We have 2 loops, the Search loop and then a "found" loop. The search loop lookups up the correct
30 // record set from the zone. The second loop (in z.Msg) then creates a message with the correct RRs in the sections.
31 // This might involve even more zone lookups for cname and glue records. The returned message can be written to the client.
32 qname := r.Question[0].Header().Name
33
34 // Doing apex queries separate simplifies the loop below as we can not have delegation, wildcards, etc.
35 if z.Labels() == dnsutil.Labels(qname) {
36 return MsgFound(z, r, z.Apex(), hintAnswer, re)
37 }
38
39 sosynthesis := &Node{} // source of synthesize
40 hint := hintAnswer
41 encloser := z.Apex()
42
43 // Go back in the qname to where it gets interesting for this zone, skip z.Labels() from the right.
44 var i, start = len(qname), false
45 for range z.Labels() {
46 i, start = dnsutil.Prev(qname, i)
47 }
48
49 // Even if start is true (reached begining of the name), we still have to check that name, hence the latter
50 // cehck for start.
51 Search:
52 for i, start = dnsutil.Prev(qname, i); ; i, start = dnsutil.Prev(qname, i) {
53 node, ok := z.Get(qname[i:])
54 if ok {
55 encloser = node
56 // Check for delegation, thus NS and (later) DELEG records. If this set contain NS records we have a delegation.
57 for _, rr := range node.RRs {
58 if _, ok := rr.(*dns.NS); ok {
59 hint = hintDelegation
60 break Search
61 }
62 }
63
64 } else {
65 // Skip a label to the right again and replace with '*', this should work by definition. If we
66 // find a wildcard label here we keep track of what we found, but we need to search below to see
67 // if there is a more specific match.
68 j, _ := dnsutil.Next(qname[i:], 0)
69 node, ok := z.Get("*." + qname[i+j:])
70 if ok {
71 sosynthesis = node
72 hint = hintWildcard
73 break Search
74 }
75 }
76
77 if start {
78 break
79 }
80 }
81
82 if hint == hintWildcard {
83 return Synthesize(z, r, sosynthesis, encloser, re)
84 }
85
86 return MsgFound(z, r, encloser, hint, re)
87 }