/
name.go
349 lines (308 loc) · 6.82 KB
/
name.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package dnsmsg
import (
"bytes"
"strconv"
"github.com/IrineSistiana/mosproxy/internal/pool"
)
// Name is a reusable buffer from bytespool.
type Name []byte
func ReleaseName(n Name) {
pool.ReleaseBuf(pool.Buffer(n))
}
// Always returns 1~255.
func (n Name) PackLen() int {
l := len(n)
if l > 254 {
l = 254 // invalid length, let packName() fail
}
return l + 1
}
// copied and from dnsmessage.Name.pack.
// Note: compression map is valid when name is not changed.
func (n Name) pack(msg []byte, off int, compression map[string]uint16) (int, error) {
var unsafeStr string // lazy init
scanner := NewNameScanner(n)
for scanner.Scan() {
seg := scanner.Label()
labelStart := scanner.LabelOff()
// We can only compress domain suffixes starting with a new
// segment. A pointer is two bytes with the two most significant
// bits set to 1 to indicate that it is a pointer.
if compression != nil {
if ptr, ok := compression[string(n[labelStart:])]; ok {
// Hit. Emit a pointer instead of the rest of
// the domain.
return packNamePtr(msg, off, [2]byte{byte(ptr>>8 | 0xC0), byte(ptr)})
}
// Miss. Add the suffix to the compression table if the
// offset can be stored in the available 14 bits.
newPtr := off
if newPtr <= int(^uint16(0)>>2) {
if len(unsafeStr) == 0 {
unsafeStr = bytes2StrUnsafe(n)
}
compression[unsafeStr[labelStart:]] = uint16(newPtr)
}
}
var err error
off, err = packByte(msg, off, byte(len(seg)))
if err != nil {
return off, err
}
off, err = packBytes(msg, off, seg)
if err != nil {
return off, err
}
}
if err := scanner.Err(); err != nil {
return off, err
}
return packByte(msg, off, 0)
}
func ToLowerName(n []byte) error {
scanner := NewNameScanner(n)
for scanner.Scan() {
asciiToLower(scanner.Label())
}
return scanner.Err()
}
// Convert n from wire format to common readable format.
// Root domain will be '.' .
// Labels will be split by '.' .
// No '.' at the end of the name.
// Unprintable characters will be escaped as "\DDD".
// '.' and '\' will be "\.", "\\".
// If n is invalid, returns nil, err.
func ToReadable(n []byte) (pool.Buffer, error) {
if len(n) == 0 {
b := pool.GetBuf(1)
b[0] = '.'
return b, nil
}
b := pool.GetBuf(1024)[:0]
scanner := NewNameScanner(n)
started := false
for scanner.Scan() {
if started {
b = append(b, '.')
}
started = true
b = appendEscapedLabel(b, scanner.Label())
}
if err := scanner.Err(); err != nil {
pool.ReleaseBuf(b)
return nil, err
}
return b, nil
}
func appendEscapedLabel(dst []byte, label []byte) []byte {
for _, b := range label {
if isPrintableLabelChar(b) {
dst = append(dst, b)
} else {
switch b {
case '.':
dst = append(dst, "\\."...)
case '\\':
dst = append(dst, "\\\\"...)
default:
dst = strconv.AppendUint(dst, uint64(b), 10)
}
}
}
return dst
}
type NameBuilder struct {
buf [254]byte
l uint8
}
func (b *NameBuilder) AppendLabel(s []byte) error {
l := len(s)
if l == 0 {
return errZeroSegLen
}
if l > 63 {
return errSegTooLong
}
labelStart := int(b.l)
labelEnd := labelStart + 1 + l
if labelEnd > 253 {
return errNameTooLong
}
b.buf[labelStart] = byte(l)
copy(b.buf[labelStart+1:], s)
b.l = uint8(labelEnd)
return nil
}
func (b *NameBuilder) Reset() {
b.l = 0
}
// Empty s or "." will be the root domain.
// Both FQDN/non-FQDN are OK.
//
// Note: escaping ("\.", "\DDD" etc.) is not supported yet and
// will be parsed as part of the label. Which is not as expected.
// TODO: Support escaping or return an error.
func (b *NameBuilder) ParseReadable(s []byte) error {
b.Reset()
if s[len(s)-1] == '.' {
s = s[:len(s)-1]
}
if len(s) == 0 {
return nil
}
off := 0
for off < len(s) {
i := bytes.IndexByte(s[off:], '.')
var label []byte
if i > 0 {
label = s[off : off+i]
} else {
label = s[off:]
}
err := b.AppendLabel(label)
if err != nil {
return err
}
off += len(label) + 1
}
return nil
}
func (b *NameBuilder) Parse(labels [][]byte) error {
b.Reset()
if len(labels) == 0 {
return nil
}
for _, label := range labels {
err := b.AppendLabel(label)
if err != nil {
return err
}
}
return nil
}
func (b *NameBuilder) Data() []byte {
return b.buf[:b.l]
}
func (b *NameBuilder) ToName() Name {
buf := pool.GetBuf(int(b.l))
copy(buf, b.buf[:])
return Name(buf)
}
// copied and modified from dnsmessage
// unpack unpacks a domain name.
func (n *NameBuilder) unpack(msg []byte, off int) (int, error) {
// currOff is the current working offset.
currOff := off
// newOff is the offset where the next record will start. Pointers lead
// to data that belongs to other names and thus doesn't count towards to
// the usage of this name.
newOff := off
// ptr is the number of pointers followed.
var ptr int
// Name is a slice representation of the name data.
name := n.buf[:0]
Loop:
for {
if currOff >= len(msg) {
return off, errBaseLen
}
c := int(msg[currOff])
currOff++
switch c & 0xC0 {
case 0x00: // String segment
if c == 0x00 {
// A zero length signals the end of the name.
break Loop
}
endOff := currOff + c
if endOff > len(msg) {
return off, errCalcLen
}
if len(name)+1+c+1 > 255 {
return off, errNameTooLong
}
name = append(name, byte(c))
name = append(name, msg[currOff:endOff]...)
currOff = endOff
case 0xC0: // Pointer
if currOff >= len(msg) {
return off, errInvalidPtr
}
c1 := msg[currOff]
currOff++
if ptr == 0 {
newOff = currOff
}
// Don't follow too many pointers, maybe there's a loop.
if ptr++; ptr > 10 {
return off, errTooManyPtr
}
currOff = (c^0xC0)<<8 | int(c1)
default:
// Prefixes 0x80 and 0x40 are reserved.
return off, errReserved
}
}
if ptr == 0 {
newOff = currOff
}
n.l = uint8(len(name))
return newOff, nil
}
func unpackName(msg []byte, off int) (Name, int, error) {
var n NameBuilder
off, err := n.unpack(msg, off)
if err != nil {
return nil, off, err
}
return n.ToName(), off, nil
}
type NameScanner struct {
n []byte
off int
err error
label []byte
labelOff int
}
func NewNameScanner(n []byte) NameScanner {
return NameScanner{n: n}
}
func (s *NameScanner) Scan() bool {
s.label = nil
if len(s.n) > 254 {
s.err = errNameTooLong
return false
}
if s.off > len(s.n)-1 {
return false
}
labelLen := int(s.n[s.off])
if labelLen == 0 {
s.err = errZeroSegLen
return false
}
if labelLen > 63 {
s.err = errInvalidLabelLen
return false
}
labelStart := s.off + 1
labelEnd := labelStart + labelLen
if labelEnd > len(s.n) {
s.err = errInvalidLabelLen
return false
}
s.label = s.n[labelStart:labelEnd]
s.labelOff = labelStart
s.off = labelEnd
return true
}
func (s *NameScanner) Label() []byte {
return s.label
}
func (s *NameScanner) LabelOff() int {
return s.labelOff
}
func (s *NameScanner) Err() error {
return s.err
}