summaryrefslogtreecommitdiffhomepage
path: root/rfc1035label
diff options
context:
space:
mode:
authorChristopher Koch <chrisko@google.com>2018-12-29 14:48:10 -0800
committerinsomniac <insomniacslk@users.noreply.github.com>2019-01-24 08:05:49 +0000
commitc90ab10024ada840e24bb028a3405961e8e4c26a (patch)
tree9b8af0c1b80ee6efc112921f9a14b92d6c73f8eb /rfc1035label
parent2be5cae32d33f01ddecf6f167a9c0e5290e6d58f (diff)
dhcpv4: nicer API for option parsing.
From: r := d.GetOneOption(OptionRouter).(*OptRouter).Routers d.UpdateOption(&OptRouter{Routers: []net.IP{net.IP{192, 168, 0, 1}}}) To: r := GetRouter(d.Options) d.UpdateOption(OptRouter(net.IP{192, 168, 0, 1}, ...))
Diffstat (limited to 'rfc1035label')
-rw-r--r--rfc1035label/label.go30
1 files changed, 22 insertions, 8 deletions
diff --git a/rfc1035label/label.go b/rfc1035label/label.go
index 5093de8..5a67d7c 100644
--- a/rfc1035label/label.go
+++ b/rfc1035label/label.go
@@ -2,13 +2,14 @@ package rfc1035label
import (
"errors"
+ "fmt"
"strings"
)
+// Labels represents RFC1035 labels
+//
// This implements RFC 1035 labels, including compression.
// https://tools.ietf.org/html/rfc1035#section-4.1.4
-
-// Labels represents RFC1035 labels
type Labels struct {
// original contains the original bytes if the object was parsed from a byte
// sequence, or nil otherwise. The `original` field is necessary to deal
@@ -33,6 +34,11 @@ func same(a, b []string) bool {
return true
}
+// String prints labels.
+func (l *Labels) String() string {
+ return fmt.Sprintf("%v", l.Labels)
+}
+
// ToBytes returns a byte sequence representing the labels. If the original
// sequence is modified, the labels are parsed again, otherwise the original
// byte sequence is returned.
@@ -62,17 +68,25 @@ func NewLabels() *Labels {
}
}
+// FromBytes reads labels from a bytes stream according to RFC 1035.
+func (l *Labels) FromBytes(data []byte) error {
+ labs, err := labelsFromBytes(data)
+ if err != nil {
+ return err
+ }
+ l.original = data
+ l.Labels = labs
+ return nil
+}
+
// FromBytes returns a Labels object from the given byte sequence, or an error if
// any.
func FromBytes(data []byte) (*Labels, error) {
- lab := NewLabels()
- l, err := labelsFromBytes(data)
- if err != nil {
+ var l Labels
+ if err := l.FromBytes(data); err != nil {
return nil, err
}
- lab.original = data
- lab.Labels = l
- return lab, nil
+ return &l, nil
}
// fromBytes decodes a serialized stream and returns a list of labels