summaryrefslogtreecommitdiffhomepage
path: root/dhcpv6/option_remoteid.go
diff options
context:
space:
mode:
Diffstat (limited to 'dhcpv6/option_remoteid.go')
-rw-r--r--dhcpv6/option_remoteid.go36
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()
}