-
Notifications
You must be signed in to change notification settings - Fork 12
/
record.go
186 lines (169 loc) · 4.72 KB
/
record.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package fastdns
import (
"net"
)
// AppendSRVRecord appends the SRV records to dst and returns the resulting dst.
func AppendSRVRecord(dst []byte, req *Message, ttl uint32, srvs []net.SRV) []byte {
// SRV Records
for _, srv := range srvs {
length := 8 + len(srv.Target)
// fixed size array for avoid bounds check
answer := [...]byte{
// NAME
0xc0, 0x0c,
// TYPE
0x00, byte(TypeSRV),
// CLASS
byte(req.Question.Class >> 8), byte(req.Question.Class),
// TTL
byte(ttl >> 24), byte(ttl >> 16), byte(ttl >> 8), byte(ttl),
// RDLENGTH
byte(length >> 8), byte(length),
// PRIOVRITY
byte(srv.Priority >> 8), byte(srv.Priority),
// WEIGHT
byte(srv.Weight >> 8), byte(srv.Weight),
// PORT
byte(srv.Port >> 8), byte(srv.Port),
}
dst = append(dst, answer[:]...)
// RDATA
dst = EncodeDomain(dst, srv.Target)
}
return dst
}
// AppendNSRecord appends the NS records to dst and returns the resulting dst.
func AppendNSRecord(dst []byte, req *Message, ttl uint32, nameservers []net.NS) []byte {
// NS Records
for _, ns := range nameservers {
// fixed size array for avoid bounds check
answer := [...]byte{
// NAME
0xc0, 0x0c,
// TYPE
0x00, byte(TypeNS),
// CLASS
byte(req.Question.Class >> 8), byte(req.Question.Class),
// TTL
byte(ttl >> 24), byte(ttl >> 16), byte(ttl >> 8), byte(ttl),
// RDLENGTH
0x00, byte(len(ns.Host) + 2),
}
dst = append(dst, answer[:]...)
// RDATA
dst = EncodeDomain(dst, ns.Host)
}
return dst
}
// AppendSOARecord appends the SOA records to dst and returns the resulting dst.
func AppendSOARecord(dst []byte, req *Message, ttl uint32, mname, rname net.NS, serial, refresh, retry, expire, minimum uint32) []byte {
length := 2 + len(mname.Host) + 2 + len(rname.Host) + 4 + 4 + 4 + 4 + 4
// fixed size array for avoid bounds check
answer := [...]byte{
// NAME
0xc0, 0x0c,
// TYPE
0x00, byte(TypeSOA),
// CLASS
byte(req.Question.Class >> 8), byte(req.Question.Class),
// TTL
byte(ttl >> 24), byte(ttl >> 16), byte(ttl >> 8), byte(ttl),
// RDLENGTH
byte(length >> 8), byte(length),
}
dst = append(dst, answer[:]...)
// MNAME
dst = EncodeDomain(dst, mname.Host)
// RNAME
dst = EncodeDomain(dst, rname.Host)
section := [...]byte{
// SERIAL
byte(serial >> 24), byte(serial >> 16), byte(serial >> 8), byte(serial),
// REFRESH
byte(refresh >> 24), byte(refresh >> 16), byte(refresh >> 8), byte(refresh),
// RETRY
byte(retry >> 24), byte(retry >> 16), byte(retry >> 8), byte(retry),
// EXPIRE
byte(expire >> 24), byte(expire >> 16), byte(expire >> 8), byte(expire),
// MINIMUM
byte(minimum >> 24), byte(minimum >> 16), byte(minimum >> 8), byte(minimum),
}
dst = append(dst, section[:]...)
return dst
}
// AppendMXRecord appends the MX records to dst and returns the resulting dst.
func AppendMXRecord(dst []byte, req *Message, ttl uint32, mxs []net.MX) []byte {
// MX Records
for _, mx := range mxs {
length := 4 + len(mx.Host)
// fixed size array for avoid bounds check
answer := [...]byte{
// NAME
0xc0, 0x0c,
// TYPE
0x00, byte(TypeMX),
// CLASS
byte(req.Question.Class >> 8), byte(req.Question.Class),
// TTL
byte(ttl >> 24), byte(ttl >> 16), byte(ttl >> 8), byte(ttl),
// RDLENGTH
byte(length >> 8), byte(length),
// PRIOVRITY
byte(mx.Pref >> 8), byte(mx.Pref),
}
dst = append(dst, answer[:]...)
// RDATA
dst = EncodeDomain(dst, mx.Host)
}
return dst
}
// AppendPTRRecord appends the PTR records to dst and returns the resulting dst.
func AppendPTRRecord(dst []byte, req *Message, ttl uint32, ptr string) []byte {
// fixed size array for avoid bounds check
answer := [...]byte{
// NAME
0xc0, 0x0c,
// TYPE
0x00, byte(TypePTR),
// CLASS
byte(req.Question.Class >> 8), byte(req.Question.Class),
// TTL
byte(ttl >> 24), byte(ttl >> 16), byte(ttl >> 8), byte(ttl),
// RDLENGTH
00, byte(2 + len(ptr)),
}
dst = append(dst, answer[:]...)
// PTR
dst = EncodeDomain(dst, ptr)
return dst
}
// AppendTXTRecord appends the TXT records to dst and returns the resulting dst.
func AppendTXTRecord(dst []byte, req *Message, ttl uint32, txt string) []byte {
length := len(txt) + (len(txt)+0xff)/0x100
// fixed size array for avoid bounds check
answer := [...]byte{
// NAME
0xc0, 0x0c,
// TYPE
0x00, byte(TypeTXT),
// CLASS
byte(req.Question.Class >> 8), byte(req.Question.Class),
// TTL
byte(ttl >> 24), byte(ttl >> 16), byte(ttl >> 8), byte(ttl),
// RDLENGTH
byte(length >> 8), byte(length),
}
dst = append(dst, answer[:]...)
for len(txt) > 0xff {
// TXT Length
dst = append(dst, 0xff)
// TXT
dst = append(dst, txt[:0xff]...)
txt = txt[0xff:]
}
// TXT Length
dst = append(dst, byte(len(txt)))
// TXT
dst = append(dst, txt...)
return dst
}