forked from folbricht/routedns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.go
93 lines (82 loc) · 1.99 KB
/
message.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package rdns
import (
"strconv"
"github.com/miekg/dns"
)
// Return the query name from a DNS query.
func qName(q *dns.Msg) string {
if len(q.Question) == 0 {
return ""
}
return q.Question[0].Name
}
// Returns the string representation of the query type.
func qType(q *dns.Msg) string {
if len(q.Question) == 0 {
return ""
}
return dns.TypeToString[q.Question[0].Qtype]
}
// Return the result code name from a DNS response.
func rCode(r *dns.Msg) string {
if result, ok := dns.RcodeToString[r.Rcode]; ok {
return result
}
return strconv.Itoa(r.Rcode)
}
// Returns a NXDOMAIN answer for a query.
func nxdomain(q *dns.Msg) *dns.Msg {
return responseWithCode(q, dns.RcodeNameError)
}
// Returns a SERVFAIL answer for a query.
func servfail(q *dns.Msg) *dns.Msg {
return responseWithCode(q, dns.RcodeServerFailure)
}
// Returns a REFUSED answer for a query.
func refused(q *dns.Msg) *dns.Msg {
return responseWithCode(q, dns.RcodeRefused)
}
// Build a response for a query with the given responce code.
func responseWithCode(q *dns.Msg, rcode int) *dns.Msg {
a := new(dns.Msg)
a.SetRcode(q, rcode)
return a
}
// Answers a PTR query with a name
func ptr(q *dns.Msg, names []string) *dns.Msg {
a := new(dns.Msg)
a.SetReply(q)
answer := make([]dns.RR, 0, len(names))
for _, name := range names {
rr := &dns.PTR{
Hdr: dns.RR_Header{
Name: q.Question[0].Name,
Rrtype: dns.TypePTR,
Class: dns.ClassINET,
Ttl: 3600,
},
Ptr: dns.Fqdn(name),
}
answer = append(answer, rr)
}
a.Answer = answer
return a
}
// Changes the UDP size in the EDNS0 record and returns a
// copy of the query. Adds an OPT record if there isn't one
// already. If size is 0, the original query is returned.
func setUDPSize(q *dns.Msg, size uint16) *dns.Msg {
if size == 0 {
return q
}
copy := q.Copy()
// Set the EDNS0 size. Adds an OPT record if there isn't
// one already
edns0 := copy.IsEdns0()
if edns0 != nil {
edns0.SetUDPSize(size)
} else {
q.SetEdns0(size, false)
}
return copy
}