blob: 06870b545dca4a066aae6b80beac69346b54b468 (
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
44
45
46
47
48
49
50
51
52
53
54
|
package dhcpv6
import (
"fmt"
"strings"
)
func LabelsFromBytes(buf []byte) ([]string, error) {
var (
pos = 0
domains = make([]string, 0)
label = ""
)
for {
if pos >= len(buf) {
return domains, nil
}
length := int(buf[pos])
pos++
if length == 0 {
domains = append(domains, label)
label = ""
}
if len(buf)-pos < length {
return nil, fmt.Errorf("DomainNamesFromBytes: invalid short label length")
}
if label != "" {
label += "."
}
label += string(buf[pos : pos+length])
pos += length
}
return domains, nil
}
func LabelToBytes(label string) []byte {
var encodedLabel []byte
if len(label) == 0 {
return []byte{0}
}
for _, part := range strings.Split(label, ".") {
encodedLabel = append(encodedLabel, byte(len(part)))
encodedLabel = append(encodedLabel, []byte(part)...)
}
return append(encodedLabel, 0)
}
func LabelsToBytes(labels []string) []byte {
var encodedLabels []byte
for _, label := range labels {
encodedLabels = append(encodedLabels, LabelToBytes(label)...)
}
return encodedLabels
}
|