diff options
Diffstat (limited to 'dhcpv6/option_nontemporaryaddress.go')
-rw-r--r-- | dhcpv6/option_nontemporaryaddress.go | 50 |
1 files changed, 25 insertions, 25 deletions
diff --git a/dhcpv6/option_nontemporaryaddress.go b/dhcpv6/option_nontemporaryaddress.go index 94f79c0..7e8bbaf 100644 --- a/dhcpv6/option_nontemporaryaddress.go +++ b/dhcpv6/option_nontemporaryaddress.go @@ -1,13 +1,16 @@ package dhcpv6 -// This module defines the OptIANA structure. -// https://www.ietf.org/rfc/rfc3633.txt - import ( - "encoding/binary" "fmt" + + "github.com/u-root/u-root/pkg/uio" ) +// OptIANA implements the identity association for non-temporary addresses +// option. +// +// This module defines the OptIANA structure. +// https://www.ietf.org/rfc/rfc3633.txt type OptIANA struct { IaId [4]byte T1 uint32 @@ -19,17 +22,16 @@ func (op *OptIANA) Code() OptionCode { return OptionIANA } +// ToBytes serializes IANA to DHCPv6 bytes. func (op *OptIANA) ToBytes() []byte { - buf := make([]byte, 16) - binary.BigEndian.PutUint16(buf[0:2], uint16(OptionIANA)) - binary.BigEndian.PutUint16(buf[2:4], uint16(op.Length())) - copy(buf[4:8], op.IaId[:]) - binary.BigEndian.PutUint32(buf[8:12], op.T1) - binary.BigEndian.PutUint32(buf[12:16], op.T2) - for _, opt := range op.Options { - buf = append(buf, opt.ToBytes()...) - } - return buf + buf := uio.NewBigEndianBuffer(nil) + buf.Write16(uint16(OptionIANA)) + buf.Write16(uint16(op.Length())) + buf.WriteBytes(op.IaId[:]) + buf.Write32(op.T1) + buf.Write32(op.T2) + buf.WriteBytes(op.Options.ToBytes()) + return buf.Data() } func (op *OptIANA) Length() int { @@ -61,18 +63,16 @@ func (op *OptIANA) DelOption(code OptionCode) { op.Options.Del(code) } -// build an OptIANA structure from a sequence of bytes. -// The input data does not include option code and length bytes. +// ParseOptIANA builds an OptIANA structure from a sequence of bytes. The +// input data does not include option code and length bytes. func ParseOptIANA(data []byte) (*OptIANA, error) { - opt := OptIANA{} - if len(data) < 12 { - return nil, fmt.Errorf("Invalid IA for Non-temporary Addresses data length. Expected at least 12 bytes, got %v", len(data)) - } - copy(opt.IaId[:], data[:4]) - opt.T1 = binary.BigEndian.Uint32(data[4:8]) - opt.T2 = binary.BigEndian.Uint32(data[8:12]) - if err := opt.Options.FromBytes(data[12:]); err != nil { + var opt OptIANA + buf := uio.NewBigEndianBuffer(data) + buf.ReadBytes(opt.IaId[:]) + opt.T1 = buf.Read32() + opt.T2 = buf.Read32() + if err := opt.Options.FromBytes(buf.ReadAll()); err != nil { return nil, err } - return &opt, nil + return &opt, buf.FinError() } |