Debian Codegraph @

dnsutil/compat.go

raw
1 package dnsutil
2
3 import "codeberg.org/miekg/dns"
4
5 // SetQuestion set the question section in the message m.
6 // It generates an ID and sets the RecursionDesired (RD) bit to true. If c is given it's used as the class,
7 // otherwise it default to [dns.ClassINET]
8 // If the type t isn't known to this library, nil is returned. Also see [dns.NewMsg] and [Question].
9 func SetQuestion(m *dns.Msg, z string, t uint16, c ...uint16) *dns.Msg {
10 m.ID = dns.ID()
11 m.RecursionDesired = true
12 var rr dns.RR
13 newFn, ok := dns.TypeToRR[t]
14 if !ok {
15 return nil
16 }
17 rr = newFn()
18 rr.Header().Name = z
19 rr.Header().Class = dns.ClassINET
20 if len(c) > 0 {
21 rr.Header().Class = c[0]
22 }
23
24 m.Question = []dns.RR{rr}
25 return m
26 }
27
28 // Question returns the question name and the type from the message m. Also see [Question].
29 func Question(m *dns.Msg) (z string, t uint16) {
30 z = m.Question[0].Header().Name
31 t = dns.RRToType(m.Question[0])
32 return z, t
33 }
34
35 // SetReply creates a reply message from r. It copies the ID, opcode, rcode and question, r's Data buffer is not copied.
36 // In the header the RecursionDesired, CheckingDisabled and Security bit are copied, the Z bits are cleared.
37 // All other sections are resliced to length zero. Also see [SetQuestion].
38 func SetReply(m, r *dns.Msg) *dns.Msg {
39 m.ID = r.ID
40 m.Response = true
41 m.Opcode = r.Opcode
42 if m.Opcode == dns.OpcodeQuery {
43 m.RecursionDesired = r.RecursionDesired
44 m.CheckingDisabled = r.CheckingDisabled
45 m.Security = r.Security
46 }
47 m.Z = 0 // RFC 6891, section 6.1.4: a sender zeroes these.
48 m.Rcode = dns.RcodeSuccess
49 m.Question = r.Question
50 m.Reset()
51 return m
52 }
53
54 // IsRRset reports whether a set of RRs is a valid RRset as defined by RFC 2181.
55 // This means the RRs need to have the same type, name, and class. Duplicate RRs are not detected.
56 // See [codeberg.org/miekg/dns.RRset] if you need to sort an RRset.
57 func IsRRset(rrset []dns.RR) bool {
58 if len(rrset) == 0 {
59 return false
60 }
61 basetype := dns.RRToType(rrset[0])
62 for j, rr := range rrset[1:] {
63 if dns.RRToType(rr) != basetype {
64 return false
65 }
66 if rrset[j+1].Header().Class != rrset[0].Header().Class {
67 return false
68 }
69 if !dns.EqualName(rrset[j+1].Header().Name, rrset[0].Header().Name) {
70 return false
71 }
72 }
73 return true
74 }