diff options
Diffstat (limited to 'dhcpv4/option_vivc.go')
-rw-r--r-- | dhcpv4/option_vivc.go | 62 |
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] } |