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_remoteid.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_remoteid.go')
-rw-r--r-- | dhcpv6/option_remoteid.go | 36 |
1 files changed, 18 insertions, 18 deletions
diff --git a/dhcpv6/option_remoteid.go b/dhcpv6/option_remoteid.go index 9d249a7..6b1831d 100644 --- a/dhcpv6/option_remoteid.go +++ b/dhcpv6/option_remoteid.go @@ -1,13 +1,14 @@ package dhcpv6 -// This module defines the OptRemoteId structure. -// https://www.ietf.org/rfc/rfc4649.txt - import ( - "encoding/binary" "fmt" + + "github.com/u-root/u-root/pkg/uio" ) +// OptRemoteId implemens the Remote ID option. +// +// https://www.ietf.org/rfc/rfc4649.txt type OptRemoteId struct { enterpriseNumber uint32 remoteId []byte @@ -17,13 +18,14 @@ func (op *OptRemoteId) Code() OptionCode { return OptionRemoteID } +// ToBytes serializes this option to a byte stream. func (op *OptRemoteId) ToBytes() []byte { - buf := make([]byte, 8) - binary.BigEndian.PutUint16(buf[0:2], uint16(OptionRemoteID)) - binary.BigEndian.PutUint16(buf[2:4], uint16(op.Length())) - binary.BigEndian.PutUint32(buf[4:8], uint32(op.enterpriseNumber)) - buf = append(buf, op.remoteId...) - return buf + buf := uio.NewBigEndianBuffer(nil) + buf.Write16(uint16(OptionRemoteID)) + buf.Write16(uint16(op.Length())) + buf.Write32(uint32(op.enterpriseNumber)) + buf.WriteBytes(op.remoteId) + return buf.Data() } func (op *OptRemoteId) EnterpriseNumber() uint32 { @@ -52,14 +54,12 @@ func (op *OptRemoteId) String() string { ) } -// build an OptRemoteId structure from a sequence of bytes. +// ParseOptRemoteId builds an OptRemoteId structure from a sequence of bytes. // The input data does not include option code and length bytes. func ParseOptRemoteId(data []byte) (*OptRemoteId, error) { - opt := OptRemoteId{} - if len(data) < 4 { - return nil, fmt.Errorf("Invalid remote id data length. Expected at least 4 bytes, got %v", len(data)) - } - opt.enterpriseNumber = binary.BigEndian.Uint32(data[:4]) - opt.remoteId = append([]byte(nil), data[4:]...) - return &opt, nil + var opt OptRemoteId + buf := uio.NewBigEndianBuffer(data) + opt.enterpriseNumber = buf.Read32() + opt.remoteId = buf.ReadAll() + return &opt, buf.FinError() } |