diff options
author | Christopher Koch <c@chrisko.ch> | 2019-01-20 04:47:08 +0000 |
---|---|---|
committer | insomniac <insomniacslk@users.noreply.github.com> | 2019-01-26 23:34:26 +0000 |
commit | 3478513076477d0f19eaeaf441f29949a9f6bc92 (patch) | |
tree | 4d621abff002cef480901a2ef015620472293db3 /dhcpv6/option_clientid.go | |
parent | e62883f5b5683ae2259ad01b7ffbe20671deb6b2 (diff) |
dhcpv6: easier option parsing
- move option parsing to uio buffer library.
- move option code and length reading into FromBytes rather than
implementing it in each OptionParser.
Diffstat (limited to 'dhcpv6/option_clientid.go')
-rw-r--r-- | dhcpv6/option_clientid.go | 26 |
1 files changed, 12 insertions, 14 deletions
diff --git a/dhcpv6/option_clientid.go b/dhcpv6/option_clientid.go index fc68ae7..92ceb24 100644 --- a/dhcpv6/option_clientid.go +++ b/dhcpv6/option_clientid.go @@ -1,14 +1,15 @@ package dhcpv6 -// This module defines the OptClientId and DUID structures. -// https://www.ietf.org/rfc/rfc3315.txt - import ( - "encoding/binary" "fmt" + + "github.com/u-root/u-root/pkg/uio" ) // OptClientId represents a Client ID option +// +// This module defines the OptClientId and DUID structures. +// https://www.ietf.org/rfc/rfc3315.txt type OptClientId struct { Cid Duid } @@ -17,12 +18,13 @@ func (op *OptClientId) Code() OptionCode { return OptionClientID } +// ToBytes marshals the Client ID option as defined by RFC 3315, Section 22.2. func (op *OptClientId) ToBytes() []byte { - buf := make([]byte, 4) - binary.BigEndian.PutUint16(buf[0:2], uint16(OptionClientID)) - binary.BigEndian.PutUint16(buf[2:4], uint16(op.Length())) - buf = append(buf, op.Cid.ToBytes()...) - return buf + buf := uio.NewBigEndianBuffer(nil) + buf.Write16(uint16(OptionClientID)) + buf.Write16(uint16(op.Length())) + buf.WriteBytes(op.Cid.ToBytes()) + return buf.Data() } func (op *OptClientId) Length() int { @@ -37,11 +39,7 @@ func (op *OptClientId) String() string { // of bytes. The input data does not include option code and length // bytes. func ParseOptClientId(data []byte) (*OptClientId, error) { - if len(data) < 2 { - // at least the DUID type is necessary to continue - return nil, fmt.Errorf("Invalid OptClientId data: shorter than 2 bytes") - } - opt := OptClientId{} + var opt OptClientId cid, err := DuidFromBytes(data) if err != nil { return nil, err |