diff options
author | Andrea Barberio <insomniac@slackware.it> | 2017-12-07 23:17:53 +0000 |
---|---|---|
committer | Andrea Barberio <insomniac@slackware.it> | 2017-12-07 23:17:53 +0000 |
commit | 55d9d52d0a25d3826c5109f0bf101448322ec565 (patch) | |
tree | b953d5ad611eb4f6e6e5021d3f72fe258e1780da /dhcpv6/option_rfc1035label.go | |
parent | d38c49539b87fb57fec7ff62f787304e4f0eccee (diff) |
Refactored options into the dhcpv6 package to resolve circular imports. Sadly.
Diffstat (limited to 'dhcpv6/option_rfc1035label.go')
-rw-r--r-- | dhcpv6/option_rfc1035label.go | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/dhcpv6/option_rfc1035label.go b/dhcpv6/option_rfc1035label.go new file mode 100644 index 0000000..06870b5 --- /dev/null +++ b/dhcpv6/option_rfc1035label.go @@ -0,0 +1,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 +} |