-
Notifications
You must be signed in to change notification settings - Fork 0
/
txt.go
65 lines (56 loc) · 1.36 KB
/
txt.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
package dns
import (
"bytes"
"encoding/binary"
"fmt"
)
// Txt implements interface RData
type Txt struct {
Length uint16
Data []string
}
// Parse implements TXT parsing for interface RData
func (t *Txt) Parse(buf *bytes.Buffer, _ int, _ *Domains) error {
var read uint
for {
length, err := buf.ReadByte()
if err != nil {
return err
}
if uint(length)+read > uint(t.Length) {
return fmt.Errorf(
"txt record part length too long: %d > %d\nstrings read: %+v",
uint(length)+read, t.Length, t.Data,
)
}
t.Data = append(t.Data, string(buf.Next(int(length))))
read = read + uint(length) + 1
if read == uint(t.Length) {
break
}
}
return nil
}
// Build implements TXT building for interface RData
func (t *Txt) Build(buf *bytes.Buffer, _ *Domains) error {
for _, part := range t.Data {
partLen := uint8(len(part))
if err := binary.Write(buf, binary.BigEndian, partLen); err != nil {
return err
}
buf.WriteString(part)
}
return nil
}
// PreBuild step, building name and adding full record
func (t *Txt) PreBuild(_ *Record, _ *Domains) (int, error) {
writeLength := 0
for _, s := range t.Data {
writeLength = writeLength + len(s) + 1 // Add 1 for the length indicator
}
return writeLength, nil
}
// TransformName adds service/proto/name fields of server record
func (t *Txt) TransformName(name string) string {
return name
}