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. All other sections are
37 // 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.Rcode = dns.RcodeSuccess
48 m.Question = r.Question
49 m.Reset()
50 return m
51 }
52
53 // IsRRset reports whether a set of RRs is a valid RRset as defined by RFC 2181.
54 // This means the RRs need to have the same type, name, and class. Duplicate RRs are not detected.
55 // See [codeberg.org/miekg/dns.RRset] if you need to sort an RRset.
56 func IsRRset(rrset []dns.RR) bool {
57 if len(rrset) == 0 {
58 return false
59 }
60 basetype := dns.RRToType(rrset[0])
61 for j, rr := range rrset[1:] {
62 if dns.RRToType(rr) != basetype {
63 return false
64 }
65 if rrset[j+1].Header().Class != rrset[0].Header().Class {
66 return false
67 }
68 if !dns.EqualName(rrset[j+1].Header().Name, rrset[0].Header().Name) {
69 return false
70 }
71 }
72 return true
73 }