summaryrefslogtreecommitdiff
path: root/internal/dns/rr_txt.go
blob: f225afa441880e4a7e9745dc784f7c0b0cf8dc95 (plain)
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
package dns

import "fmt"

type TXT struct {
	Txt []string
}

func (t *TXT) Type() uint16 {
	return TypeTXT
}

func (t *TXT) Pack(p *packer) error {
	for _, s := range t.Txt {
		if len(s) > 255 {
			return fmt.Errorf("dns: txt string > 255 octets")
		}
		p.buf = append(p.buf, byte(len(s)))
		p.buf = append(p.buf, s...)
	}
	return nil
}

func (t *TXT) Unpack(u *unpacker, length uint16) error {
	end := u.off + int(length)
	for u.off < end {
		if u.off >= len(u.buf) {
			return fmt.Errorf("dns: short txt")
		}
		l := int(u.buf[u.off])
		u.off++
		if u.off+l > end {
			return fmt.Errorf("dns: txt string overflow")
		}
		t.Txt = append(t.Txt, string(u.buf[u.off:u.off+l]))
		u.off += l
	}
	return nil
}

func (t *TXT) String() string {
	return fmt.Sprintf("%v", t.Txt)
}