diff options
author | Pablo Mazzini <pmazzini@gmail.com> | 2018-07-31 11:02:38 +0200 |
---|---|---|
committer | Pablo Mazzini <pmazzini@gmail.com> | 2018-07-31 11:02:38 +0200 |
commit | 4100f801b9bb7036e0b201c2bf42fa6030119cc5 (patch) | |
tree | 89a80210793af185073c9173f157f3b627fa39d1 /dhcpv4/option_rfc1035label.go | |
parent | c6894ea160d82b4a326ebab94faaebd090192a7b (diff) |
add OptDomainSearch
Diffstat (limited to 'dhcpv4/option_rfc1035label.go')
-rw-r--r-- | dhcpv4/option_rfc1035label.go | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/dhcpv4/option_rfc1035label.go b/dhcpv4/option_rfc1035label.go new file mode 100644 index 0000000..b78d8da --- /dev/null +++ b/dhcpv4/option_rfc1035label.go @@ -0,0 +1,53 @@ +package dhcpv4 + +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 + } +} + +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 +} |