summaryrefslogtreecommitdiffhomepage
path: root/dhcpv4/bsdp
diff options
context:
space:
mode:
authorChristopher Koch <chrisko@google.com>2018-12-29 09:16:15 -0800
committerinsomniac <insomniacslk@users.noreply.github.com>2019-01-11 19:38:21 +0000
commit9492662dae0651fd4d6698d35b58ade7300e149e (patch)
tree77f53ebd6dfded05880c322005909b97a1cf140c /dhcpv4/bsdp
parent512011c2eb80a7c0316405ef7aaae6e0b5b09b1c (diff)
dhcpv4: simplify marshaling options to binary.
- Consolidate writing the option code and length to Options.Marshal rather than doing it in each individual option. - Use uio in marshaling code.
Diffstat (limited to 'dhcpv4/bsdp')
-rw-r--r--dhcpv4/bsdp/boot_image.go27
-rw-r--r--dhcpv4/bsdp/boot_image_test.go12
-rw-r--r--dhcpv4/bsdp/bsdp.go4
-rw-r--r--dhcpv4/bsdp/bsdp_option_boot_image_list.go7
-rw-r--r--dhcpv4/bsdp/bsdp_option_boot_image_list_test.go2
-rw-r--r--dhcpv4/bsdp/bsdp_option_default_boot_image_id.go3
-rw-r--r--dhcpv4/bsdp/bsdp_option_default_boot_image_id_test.go6
-rw-r--r--dhcpv4/bsdp/bsdp_option_generic.go2
-rw-r--r--dhcpv4/bsdp/bsdp_option_generic_test.go2
-rw-r--r--dhcpv4/bsdp/bsdp_option_machine_name.go2
-rw-r--r--dhcpv4/bsdp/bsdp_option_machine_name_test.go2
-rw-r--r--dhcpv4/bsdp/bsdp_option_message_type.go2
-rw-r--r--dhcpv4/bsdp/bsdp_option_message_type_test.go2
-rw-r--r--dhcpv4/bsdp/bsdp_option_reply_port.go7
-rw-r--r--dhcpv4/bsdp/bsdp_option_reply_port_test.go2
-rw-r--r--dhcpv4/bsdp/bsdp_option_selected_boot_image_id.go3
-rw-r--r--dhcpv4/bsdp/bsdp_option_selected_boot_image_id_test.go9
-rw-r--r--dhcpv4/bsdp/bsdp_option_server_identifier.go3
-rw-r--r--dhcpv4/bsdp/bsdp_option_server_identifier_test.go2
-rw-r--r--dhcpv4/bsdp/bsdp_option_server_priority.go7
-rw-r--r--dhcpv4/bsdp/bsdp_option_server_priority_test.go2
-rw-r--r--dhcpv4/bsdp/bsdp_option_version.go2
-rw-r--r--dhcpv4/bsdp/bsdp_option_version_test.go2
-rw-r--r--dhcpv4/bsdp/option_vendor_specific_information.go9
-rw-r--r--dhcpv4/bsdp/option_vendor_specific_information_test.go2
25 files changed, 53 insertions, 70 deletions
diff --git a/dhcpv4/bsdp/boot_image.go b/dhcpv4/bsdp/boot_image.go
index fa9b1a6..954dcb6 100644
--- a/dhcpv4/bsdp/boot_image.go
+++ b/dhcpv4/bsdp/boot_image.go
@@ -1,7 +1,6 @@
package bsdp
import (
- "encoding/binary"
"fmt"
"github.com/u-root/u-root/pkg/uio"
@@ -36,15 +35,16 @@ type BootImageID struct {
Index uint16
}
-// ToBytes serializes a BootImageID to network-order bytes.
-func (b BootImageID) ToBytes() []byte {
- bytes := make([]byte, 4)
+// Marshal writes the binary representation to buf.
+func (b BootImageID) Marshal(buf *uio.Lexer) {
+ var byte0 byte
if b.IsInstall {
- bytes[0] |= 0x80
+ byte0 |= 0x80
}
- bytes[0] |= byte(b.ImageType)
- binary.BigEndian.PutUint16(bytes[2:], b.Index)
- return bytes
+ byte0 |= byte(b.ImageType)
+ buf.Write8(byte0)
+ buf.Write8(byte(0))
+ buf.Write16(b.Index)
}
// String converts a BootImageID to a human-readable representation.
@@ -78,12 +78,11 @@ type BootImage struct {
Name string
}
-// ToBytes converts a BootImage to a slice of bytes.
-func (b *BootImage) ToBytes() []byte {
- bytes := b.ID.ToBytes()
- bytes = append(bytes, byte(len(b.Name)))
- bytes = append(bytes, []byte(b.Name)...)
- return bytes
+// Marshal write a BootImage to buf.
+func (b BootImage) Marshal(buf *uio.Lexer) {
+ b.ID.Marshal(buf)
+ buf.Write8(uint8(len(b.Name)))
+ buf.WriteBytes([]byte(b.Name))
}
// String converts a BootImage to a human-readable representation.
diff --git a/dhcpv4/bsdp/boot_image_test.go b/dhcpv4/bsdp/boot_image_test.go
index d8e3aeb..996b8a0 100644
--- a/dhcpv4/bsdp/boot_image_test.go
+++ b/dhcpv4/bsdp/boot_image_test.go
@@ -13,12 +13,12 @@ func TestBootImageIDToBytes(t *testing.T) {
ImageType: BootImageTypeMacOSX,
Index: 0x1000,
}
- actual := b.ToBytes()
+ actual := uio.ToBigEndian(b)
expected := []byte{0x81, 0, 0x10, 0}
require.Equal(t, expected, actual)
b.IsInstall = false
- actual = b.ToBytes()
+ actual = uio.ToBigEndian(b)
expected = []byte{0x01, 0, 0x10, 0}
require.Equal(t, expected, actual)
}
@@ -30,7 +30,7 @@ func TestBootImageIDFromBytes(t *testing.T) {
Index: 0x1000,
}
var newBootImage BootImageID
- require.NoError(t, uio.FromBigEndian(&newBootImage, b.ToBytes()))
+ require.NoError(t, uio.FromBigEndian(&newBootImage, uio.ToBigEndian(b)))
require.Equal(t, b, newBootImage)
b = BootImageID{
@@ -38,7 +38,7 @@ func TestBootImageIDFromBytes(t *testing.T) {
ImageType: BootImageTypeMacOSX,
Index: 0x1011,
}
- require.NoError(t, uio.FromBigEndian(&newBootImage, b.ToBytes()))
+ require.NoError(t, uio.FromBigEndian(&newBootImage, uio.ToBigEndian(b)))
require.Equal(t, b, newBootImage)
}
@@ -70,7 +70,7 @@ func TestBootImageToBytes(t *testing.T) {
6, // len(Name)
98, 115, 100, 112, 45, 49, // byte-encoding of Name
}
- actual := b.ToBytes()
+ actual := uio.ToBigEndian(b)
require.Equal(t, expected, actual)
b = BootImage{
@@ -86,7 +86,7 @@ func TestBootImageToBytes(t *testing.T) {
7, // len(Name)
98, 115, 100, 112, 45, 50, 49, // byte-encoding of Name
}
- actual = b.ToBytes()
+ actual = uio.ToBigEndian(b)
require.Equal(t, expected, actual)
}
diff --git a/dhcpv4/bsdp/bsdp.go b/dhcpv4/bsdp/bsdp.go
index 6bc0cfd..12f3c06 100644
--- a/dhcpv4/bsdp/bsdp.go
+++ b/dhcpv4/bsdp/bsdp.go
@@ -36,7 +36,7 @@ func ParseBootImageListFromAck(ack dhcpv4.DHCPv4) ([]BootImage, error) {
if opt == nil {
return nil, errors.New("ParseBootImageListFromAck: could not find vendor-specific option")
}
- vendorOpt, err := ParseOptVendorSpecificInformation(opt.ToBytes()[2:])
+ vendorOpt, err := ParseOptVendorSpecificInformation(opt.ToBytes())
if err != nil {
return nil, err
}
@@ -60,7 +60,7 @@ func MessageTypeFromPacket(packet *dhcpv4.DHCPv4) *MessageType {
err error
)
for _, opt := range packet.GetOption(dhcpv4.OptionVendorSpecificInformation) {
- if vendorOpts, err = ParseOptVendorSpecificInformation(opt.ToBytes()[2:]); err == nil {
+ if vendorOpts, err = ParseOptVendorSpecificInformation(opt.ToBytes()); err == nil {
if o := vendorOpts.GetOneOption(OptionMessageType); o != nil {
if optMessageType, ok := o.(*OptMessageType); ok {
return &optMessageType.Type
diff --git a/dhcpv4/bsdp/bsdp_option_boot_image_list.go b/dhcpv4/bsdp/bsdp_option_boot_image_list.go
index d018655..ae2e59e 100644
--- a/dhcpv4/bsdp/bsdp_option_boot_image_list.go
+++ b/dhcpv4/bsdp/bsdp_option_boot_image_list.go
@@ -37,12 +37,11 @@ func (o *OptBootImageList) Code() dhcpv4.OptionCode {
// ToBytes returns a serialized stream of bytes for this option.
func (o *OptBootImageList) ToBytes() []byte {
- bs := make([]byte, 0, 2+o.Length())
- bs = append(bs, []byte{byte(o.Code()), byte(o.Length())}...)
+ buf := uio.NewBigEndianBuffer(nil)
for _, image := range o.Images {
- bs = append(bs, image.ToBytes()...)
+ image.Marshal(buf)
}
- return bs
+ return buf.Data()
}
// String returns a human-readable string for this option.
diff --git a/dhcpv4/bsdp/bsdp_option_boot_image_list_test.go b/dhcpv4/bsdp/bsdp_option_boot_image_list_test.go
index 0819d64..8e9c27f 100644
--- a/dhcpv4/bsdp/bsdp_option_boot_image_list_test.go
+++ b/dhcpv4/bsdp/bsdp_option_boot_image_list_test.go
@@ -29,8 +29,6 @@ func TestOptBootImageListInterfaceMethods(t *testing.T) {
require.Equal(t, OptionBootImageList, o.Code(), "Code")
require.Equal(t, 22, o.Length(), "Length")
expectedBytes := []byte{
- 9, // code
- 22, // length
// boot image 1
0x1, 0x0, 0x03, 0xe9, // ID
6, // name length
diff --git a/dhcpv4/bsdp/bsdp_option_default_boot_image_id.go b/dhcpv4/bsdp/bsdp_option_default_boot_image_id.go
index 52f7780..d75c883 100644
--- a/dhcpv4/bsdp/bsdp_option_default_boot_image_id.go
+++ b/dhcpv4/bsdp/bsdp_option_default_boot_image_id.go
@@ -33,8 +33,7 @@ func (o *OptDefaultBootImageID) Code() dhcpv4.OptionCode {
// ToBytes returns a serialized stream of bytes for this option.
func (o *OptDefaultBootImageID) ToBytes() []byte {
- serializedID := o.ID.ToBytes()
- return append([]byte{byte(o.Code()), byte(len(serializedID))}, serializedID...)
+ return uio.ToBigEndian(o.ID)
}
// String returns a human-readable string for this option.
diff --git a/dhcpv4/bsdp/bsdp_option_default_boot_image_id_test.go b/dhcpv4/bsdp/bsdp_option_default_boot_image_id_test.go
index ad29c30..eb63457 100644
--- a/dhcpv4/bsdp/bsdp_option_default_boot_image_id_test.go
+++ b/dhcpv4/bsdp/bsdp_option_default_boot_image_id_test.go
@@ -4,6 +4,7 @@ import (
"testing"
"github.com/stretchr/testify/require"
+ "github.com/u-root/u-root/pkg/uio"
)
func TestOptDefaultBootImageIDInterfaceMethods(t *testing.T) {
@@ -11,13 +12,12 @@ func TestOptDefaultBootImageIDInterfaceMethods(t *testing.T) {
o := OptDefaultBootImageID{b}
require.Equal(t, OptionDefaultBootImageID, o.Code(), "Code")
require.Equal(t, 4, o.Length(), "Length")
- expectedBytes := []byte{byte(OptionDefaultBootImageID), 4}
- require.Equal(t, append(expectedBytes, b.ToBytes()...), o.ToBytes(), "ToBytes")
+ require.Equal(t, uio.ToBigEndian(b), o.ToBytes(), "ToBytes")
}
func TestParseOptDefaultBootImageID(t *testing.T) {
b := BootImageID{IsInstall: true, ImageType: BootImageTypeMacOSX, Index: 1001}
- o, err := ParseOptDefaultBootImageID(b.ToBytes())
+ o, err := ParseOptDefaultBootImageID(uio.ToBigEndian(b))
require.NoError(t, err)
require.Equal(t, &OptDefaultBootImageID{b}, o)
diff --git a/dhcpv4/bsdp/bsdp_option_generic.go b/dhcpv4/bsdp/bsdp_option_generic.go
index 6702e9c..cfcffb8 100644
--- a/dhcpv4/bsdp/bsdp_option_generic.go
+++ b/dhcpv4/bsdp/bsdp_option_generic.go
@@ -27,7 +27,7 @@ func (o OptGeneric) Code() dhcpv4.OptionCode {
// ToBytes returns a serialized generic option as a slice of bytes.
func (o OptGeneric) ToBytes() []byte {
- return append([]byte{byte(o.Code()), byte(o.Length())}, o.Data...)
+ return o.Data
}
// String returns a human-readable representation of a generic option.
diff --git a/dhcpv4/bsdp/bsdp_option_generic_test.go b/dhcpv4/bsdp/bsdp_option_generic_test.go
index eae77e1..131af9f 100644
--- a/dhcpv4/bsdp/bsdp_option_generic_test.go
+++ b/dhcpv4/bsdp/bsdp_option_generic_test.go
@@ -36,7 +36,7 @@ func TestOptGenericToBytes(t *testing.T) {
Data: []byte{192, 168, 0, 1},
}
serialized := o.ToBytes()
- expected := []byte{3, 4, 192, 168, 0, 1}
+ expected := []byte{192, 168, 0, 1}
require.Equal(t, expected, serialized)
}
diff --git a/dhcpv4/bsdp/bsdp_option_machine_name.go b/dhcpv4/bsdp/bsdp_option_machine_name.go
index cffba2e..ef38921 100644
--- a/dhcpv4/bsdp/bsdp_option_machine_name.go
+++ b/dhcpv4/bsdp/bsdp_option_machine_name.go
@@ -25,7 +25,7 @@ func (o *OptMachineName) Code() dhcpv4.OptionCode {
// ToBytes returns a serialized stream of bytes for this option.
func (o *OptMachineName) ToBytes() []byte {
- return append([]byte{byte(o.Code()), byte(o.Length())}, []byte(o.Name)...)
+ return []byte(o.Name)
}
// String returns a human-readable string for this option.
diff --git a/dhcpv4/bsdp/bsdp_option_machine_name_test.go b/dhcpv4/bsdp/bsdp_option_machine_name_test.go
index 712bc49..c06ae7b 100644
--- a/dhcpv4/bsdp/bsdp_option_machine_name_test.go
+++ b/dhcpv4/bsdp/bsdp_option_machine_name_test.go
@@ -10,7 +10,7 @@ func TestOptMachineNameInterfaceMethods(t *testing.T) {
o := OptMachineName{"somebox"}
require.Equal(t, OptionMachineName, o.Code(), "Code")
require.Equal(t, 7, o.Length(), "Length")
- expectedBytes := []byte{130, 7, 's', 'o', 'm', 'e', 'b', 'o', 'x'}
+ expectedBytes := []byte{'s', 'o', 'm', 'e', 'b', 'o', 'x'}
require.Equal(t, expectedBytes, o.ToBytes(), "ToBytes")
}
diff --git a/dhcpv4/bsdp/bsdp_option_message_type.go b/dhcpv4/bsdp/bsdp_option_message_type.go
index 8c3c3d4..c427dd1 100644
--- a/dhcpv4/bsdp/bsdp_option_message_type.go
+++ b/dhcpv4/bsdp/bsdp_option_message_type.go
@@ -53,7 +53,7 @@ func (o *OptMessageType) Code() dhcpv4.OptionCode {
// ToBytes returns a serialized stream of bytes for this option.
func (o *OptMessageType) ToBytes() []byte {
- return []byte{byte(o.Code()), 1, byte(o.Type)}
+ return []byte{byte(o.Type)}
}
// String returns a human-readable string for this option.
diff --git a/dhcpv4/bsdp/bsdp_option_message_type_test.go b/dhcpv4/bsdp/bsdp_option_message_type_test.go
index 41652be..5dcda6c 100644
--- a/dhcpv4/bsdp/bsdp_option_message_type_test.go
+++ b/dhcpv4/bsdp/bsdp_option_message_type_test.go
@@ -10,7 +10,7 @@ func TestOptMessageTypeInterfaceMethods(t *testing.T) {
o := OptMessageType{MessageTypeList}
require.Equal(t, OptionMessageType, o.Code(), "Code")
require.Equal(t, 1, o.Length(), "Length")
- require.Equal(t, []byte{1, 1, 1}, o.ToBytes(), "ToBytes")
+ require.Equal(t, []byte{1}, o.ToBytes(), "ToBytes")
}
func TestParseOptMessageType(t *testing.T) {
diff --git a/dhcpv4/bsdp/bsdp_option_reply_port.go b/dhcpv4/bsdp/bsdp_option_reply_port.go
index da5e9c4..39277c3 100644
--- a/dhcpv4/bsdp/bsdp_option_reply_port.go
+++ b/dhcpv4/bsdp/bsdp_option_reply_port.go
@@ -1,7 +1,6 @@
package bsdp
import (
- "encoding/binary"
"fmt"
"github.com/insomniacslk/dhcp/dhcpv4"
@@ -32,9 +31,9 @@ func (o *OptReplyPort) Code() dhcpv4.OptionCode {
// ToBytes returns a serialized stream of bytes for this option.
func (o *OptReplyPort) ToBytes() []byte {
- serialized := make([]byte, 2)
- binary.BigEndian.PutUint16(serialized, o.Port)
- return append([]byte{byte(o.Code()), 2}, serialized...)
+ buf := uio.NewBigEndianBuffer(nil)
+ buf.Write16(o.Port)
+ return buf.Data()
}
// String returns a human-readable string for this option.
diff --git a/dhcpv4/bsdp/bsdp_option_reply_port_test.go b/dhcpv4/bsdp/bsdp_option_reply_port_test.go
index 719bbc8..413c977 100644
--- a/dhcpv4/bsdp/bsdp_option_reply_port_test.go
+++ b/dhcpv4/bsdp/bsdp_option_reply_port_test.go
@@ -10,7 +10,7 @@ func TestOptReplyPortInterfaceMethods(t *testing.T) {
o := OptReplyPort{1234}
require.Equal(t, OptionReplyPort, o.Code(), "Code")
require.Equal(t, 2, o.Length(), "Length")
- require.Equal(t, []byte{byte(OptionReplyPort), 2, 4, 210}, o.ToBytes(), "ToBytes")
+ require.Equal(t, []byte{4, 210}, o.ToBytes(), "ToBytes")
}
func TestParseOptReplyPort(t *testing.T) {
diff --git a/dhcpv4/bsdp/bsdp_option_selected_boot_image_id.go b/dhcpv4/bsdp/bsdp_option_selected_boot_image_id.go
index 52b6eab..9079346 100644
--- a/dhcpv4/bsdp/bsdp_option_selected_boot_image_id.go
+++ b/dhcpv4/bsdp/bsdp_option_selected_boot_image_id.go
@@ -33,8 +33,7 @@ func (o *OptSelectedBootImageID) Code() dhcpv4.OptionCode {
// ToBytes returns a serialized stream of bytes for this option.
func (o *OptSelectedBootImageID) ToBytes() []byte {
- serializedID := o.ID.ToBytes()
- return append([]byte{byte(o.Code()), byte(len(serializedID))}, serializedID...)
+ return uio.ToBigEndian(o.ID)
}
// String returns a human-readable string for this option.
diff --git a/dhcpv4/bsdp/bsdp_option_selected_boot_image_id_test.go b/dhcpv4/bsdp/bsdp_option_selected_boot_image_id_test.go
index a55fd9f..d9bf2df 100644
--- a/dhcpv4/bsdp/bsdp_option_selected_boot_image_id_test.go
+++ b/dhcpv4/bsdp/bsdp_option_selected_boot_image_id_test.go
@@ -4,6 +4,7 @@ import (
"testing"
"github.com/stretchr/testify/require"
+ "github.com/u-root/u-root/pkg/uio"
)
func TestOptSelectedBootImageIDInterfaceMethods(t *testing.T) {
@@ -11,19 +12,17 @@ func TestOptSelectedBootImageIDInterfaceMethods(t *testing.T) {
o := OptSelectedBootImageID{b}
require.Equal(t, OptionSelectedBootImageID, o.Code(), "Code")
require.Equal(t, 4, o.Length(), "Length")
- expectedBytes := []byte{byte(OptionSelectedBootImageID), 4}
- require.Equal(t, append(expectedBytes, b.ToBytes()...), o.ToBytes(), "ToBytes")
+ require.Equal(t, uio.ToBigEndian(b), o.ToBytes(), "ToBytes")
}
func TestParseOptSelectedBootImageID(t *testing.T) {
b := BootImageID{IsInstall: true, ImageType: BootImageTypeMacOSX, Index: 1001}
- data := b.ToBytes()
- o, err := ParseOptSelectedBootImageID(data)
+ o, err := ParseOptSelectedBootImageID(uio.ToBigEndian(b))
require.NoError(t, err)
require.Equal(t, &OptSelectedBootImageID{b}, o)
// Short byte stream
- data = []byte{}
+ data := []byte{}
_, err = ParseOptSelectedBootImageID(data)
require.Error(t, err, "should get error from short byte stream")
diff --git a/dhcpv4/bsdp/bsdp_option_server_identifier.go b/dhcpv4/bsdp/bsdp_option_server_identifier.go
index 26ec37a..704d4f2 100644
--- a/dhcpv4/bsdp/bsdp_option_server_identifier.go
+++ b/dhcpv4/bsdp/bsdp_option_server_identifier.go
@@ -27,8 +27,7 @@ func (o *OptServerIdentifier) Code() dhcpv4.OptionCode {
// ToBytes returns a serialized stream of bytes for this option.
func (o *OptServerIdentifier) ToBytes() []byte {
- ret := []byte{byte(o.Code()), byte(o.Length())}
- return append(ret, o.ServerID.To4()...)
+ return o.ServerID.To4()
}
// String returns a human-readable string.
diff --git a/dhcpv4/bsdp/bsdp_option_server_identifier_test.go b/dhcpv4/bsdp/bsdp_option_server_identifier_test.go
index d832c40..8c4555e 100644
--- a/dhcpv4/bsdp/bsdp_option_server_identifier_test.go
+++ b/dhcpv4/bsdp/bsdp_option_server_identifier_test.go
@@ -11,7 +11,7 @@ func TestOptServerIdentifierInterfaceMethods(t *testing.T) {
ip := net.IP{192, 168, 0, 1}
o := OptServerIdentifier{ServerID: ip}
require.Equal(t, OptionServerIdentifier, o.Code(), "Code")
- expectedBytes := []byte{3, 4, 192, 168, 0, 1}
+ expectedBytes := []byte{192, 168, 0, 1}
require.Equal(t, expectedBytes, o.ToBytes(), "ToBytes")
require.Equal(t, 4, o.Length(), "Length")
require.Equal(t, "BSDP Server Identifier -> 192.168.0.1", o.String(), "String")
diff --git a/dhcpv4/bsdp/bsdp_option_server_priority.go b/dhcpv4/bsdp/bsdp_option_server_priority.go
index 66bfa44..b362f01 100644
--- a/dhcpv4/bsdp/bsdp_option_server_priority.go
+++ b/dhcpv4/bsdp/bsdp_option_server_priority.go
@@ -1,7 +1,6 @@
package bsdp
import (
- "encoding/binary"
"fmt"
"github.com/insomniacslk/dhcp/dhcpv4"
@@ -27,9 +26,9 @@ func (o *OptServerPriority) Code() dhcpv4.OptionCode {
// ToBytes returns a serialized stream of bytes for this option.
func (o *OptServerPriority) ToBytes() []byte {
- serialized := make([]byte, 2)
- binary.BigEndian.PutUint16(serialized, uint16(o.Priority))
- return append([]byte{byte(o.Code()), byte(o.Length())}, serialized...)
+ buf := uio.NewBigEndianBuffer(nil)
+ buf.Write16(o.Priority)
+ return buf.Data()
}
// String returns a human-readable string.
diff --git a/dhcpv4/bsdp/bsdp_option_server_priority_test.go b/dhcpv4/bsdp/bsdp_option_server_priority_test.go
index cbcef1d..91a21b6 100644
--- a/dhcpv4/bsdp/bsdp_option_server_priority_test.go
+++ b/dhcpv4/bsdp/bsdp_option_server_priority_test.go
@@ -9,7 +9,7 @@ import (
func TestOptServerPriorityInterfaceMethods(t *testing.T) {
o := OptServerPriority{Priority: 100}
require.Equal(t, OptionServerPriority, o.Code(), "Code")
- require.Equal(t, []byte{4, 2, 0, 100}, o.ToBytes(), "ToBytes")
+ require.Equal(t, []byte{0, 100}, o.ToBytes(), "ToBytes")
require.Equal(t, 2, o.Length(), "Length")
require.Equal(t, "BSDP Server Priority -> 100", o.String(), "String")
}
diff --git a/dhcpv4/bsdp/bsdp_option_version.go b/dhcpv4/bsdp/bsdp_option_version.go
index 38158d7..9314cbe 100644
--- a/dhcpv4/bsdp/bsdp_option_version.go
+++ b/dhcpv4/bsdp/bsdp_option_version.go
@@ -34,7 +34,7 @@ func (o *OptVersion) Code() dhcpv4.OptionCode {
// ToBytes returns a serialized stream of bytes for this option.
func (o *OptVersion) ToBytes() []byte {
- return append([]byte{byte(o.Code()), 2}, o.Version...)
+ return o.Version
}
// String returns a human-readable string for this option.
diff --git a/dhcpv4/bsdp/bsdp_option_version_test.go b/dhcpv4/bsdp/bsdp_option_version_test.go
index d28f243..a74ea0e 100644
--- a/dhcpv4/bsdp/bsdp_option_version_test.go
+++ b/dhcpv4/bsdp/bsdp_option_version_test.go
@@ -10,7 +10,7 @@ func TestOptVersionInterfaceMethods(t *testing.T) {
o := OptVersion{Version1_1}
require.Equal(t, OptionVersion, o.Code(), "Code")
require.Equal(t, 2, o.Length(), "Length")
- require.Equal(t, []byte{2, 2, 1, 1}, o.ToBytes(), "ToBytes")
+ require.Equal(t, []byte{1, 1}, o.ToBytes(), "ToBytes")
}
func TestParseOptVersion(t *testing.T) {
diff --git a/dhcpv4/bsdp/option_vendor_specific_information.go b/dhcpv4/bsdp/option_vendor_specific_information.go
index 4219e40..2207059 100644
--- a/dhcpv4/bsdp/option_vendor_specific_information.go
+++ b/dhcpv4/bsdp/option_vendor_specific_information.go
@@ -4,6 +4,7 @@ import (
"strings"
"github.com/insomniacslk/dhcp/dhcpv4"
+ "github.com/u-root/u-root/pkg/uio"
)
// OptVendorSpecificInformation encapsulates the BSDP-specific options used for
@@ -64,13 +65,7 @@ func (o *OptVendorSpecificInformation) Code() dhcpv4.OptionCode {
// ToBytes returns a serialized stream of bytes for this option.
func (o *OptVendorSpecificInformation) ToBytes() []byte {
- bs := []byte{byte(o.Code()), byte(o.Length())}
-
- // Append data section
- for _, opt := range o.Options {
- bs = append(bs, opt.ToBytes()...)
- }
- return bs
+ return uio.ToBigEndian(o.Options)
}
// String returns a human-readable string for this option.
diff --git a/dhcpv4/bsdp/option_vendor_specific_information_test.go b/dhcpv4/bsdp/option_vendor_specific_information_test.go
index 689797b..e62589e 100644
--- a/dhcpv4/bsdp/option_vendor_specific_information_test.go
+++ b/dhcpv4/bsdp/option_vendor_specific_information_test.go
@@ -15,8 +15,6 @@ func TestOptVendorSpecificInformationInterfaceMethods(t *testing.T) {
require.Equal(t, 2+messageTypeOpt.Length()+2+versionOpt.Length(), o.Length(), "Length")
expectedBytes := []byte{
- 43, // code
- 7, // length
1, 1, 1, // List option
2, 2, 1, 1, // Version option
}