summaryrefslogtreecommitdiffhomepage
path: root/dhcpv4/option_vivc.go
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 /dhcpv4/option_vivc.go
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 'dhcpv4/option_vivc.go')
-rw-r--r--dhcpv4/option_vivc.go62
1 files changed, 38 insertions, 24 deletions
diff --git a/dhcpv4/option_vivc.go b/dhcpv4/option_vivc.go
index b6efab9..509ba80 100644
--- a/dhcpv4/option_vivc.go
+++ b/dhcpv4/option_vivc.go
@@ -10,39 +10,53 @@ import (
// VIVCIdentifier implements the vendor-identifying vendor class option
// described by RFC 3925.
type VIVCIdentifier struct {
+ // EntID is the enterprise ID.
EntID uint32
Data []byte
}
-// OptVIVC represents the DHCP message type option.
-type OptVIVC struct {
- Identifiers []VIVCIdentifier
+// OptVIVC returns a new vendor-identifying vendor class option.
+//
+// The option is described by RFC 3925.
+func OptVIVC(identifiers ...VIVCIdentifier) Option {
+ return Option{
+ Code: OptionVendorIdentifyingVendorClass,
+ Value: VIVCIdentifiers(identifiers),
+ }
}
-// ParseOptVIVC contructs an OptVIVC tsruct from a sequence of bytes and returns
-// it, or an error.
-func ParseOptVIVC(data []byte) (*OptVIVC, error) {
- buf := uio.NewBigEndianBuffer(data)
+// GetVIVC returns the vendor-identifying vendor class option in o if present.
+func GetVIVC(o Options) VIVCIdentifiers {
+ v := o.Get(OptionVendorIdentifyingVendorClass)
+ if v == nil {
+ return nil
+ }
+ var ids VIVCIdentifiers
+ if err := ids.FromBytes(v); err != nil {
+ return nil
+ }
+ return ids
+}
+
+// VIVCIdentifiers implements encoding and decoding methods for a DHCP option
+// described in RFC 3925.
+type VIVCIdentifiers []VIVCIdentifier
- var ids []VIVCIdentifier
+// FromBytes parses data into ids per RFC 3925.
+func (ids *VIVCIdentifiers) FromBytes(data []byte) error {
+ buf := uio.NewBigEndianBuffer(data)
for buf.Has(5) {
entID := buf.Read32()
idLen := int(buf.Read8())
- ids = append(ids, VIVCIdentifier{EntID: entID, Data: buf.CopyN(idLen)})
+ *ids = append(*ids, VIVCIdentifier{EntID: entID, Data: buf.CopyN(idLen)})
}
-
- return &OptVIVC{Identifiers: ids}, buf.FinError()
-}
-
-// Code returns the option code.
-func (o *OptVIVC) Code() OptionCode {
- return OptionVendorIdentifyingVendorClass
+ return buf.FinError()
}
// ToBytes returns a serialized stream of bytes for this option.
-func (o *OptVIVC) ToBytes() []byte {
+func (ids VIVCIdentifiers) ToBytes() []byte {
buf := uio.NewBigEndianBuffer(nil)
- for _, id := range o.Identifiers {
+ for _, id := range ids {
buf.Write32(id.EntID)
buf.Write8(uint8(len(id.Data)))
buf.WriteBytes(id.Data)
@@ -51,13 +65,13 @@ func (o *OptVIVC) ToBytes() []byte {
}
// String returns a human-readable string for this option.
-func (o *OptVIVC) String() string {
+func (ids VIVCIdentifiers) String() string {
+ if len(ids) == 0 {
+ return ""
+ }
buf := bytes.Buffer{}
- fmt.Fprintf(&buf, "Vendor-Identifying Vendor Class ->")
-
- for _, id := range o.Identifiers {
+ for _, id := range ids {
fmt.Fprintf(&buf, " %d:'%s',", id.EntID, id.Data)
}
-
- return buf.String()[:buf.Len()-1]
+ return buf.String()[1 : buf.Len()-1]
}