diff options
Diffstat (limited to 'dhcpv6')
42 files changed, 71 insertions, 192 deletions
diff --git a/dhcpv6/dhcpv6_test.go b/dhcpv6/dhcpv6_test.go index d5a29aa..81d7ad1 100644 --- a/dhcpv6/dhcpv6_test.go +++ b/dhcpv6/dhcpv6_test.go @@ -140,10 +140,9 @@ func TestAddOption(t *testing.T) { func TestToBytes(t *testing.T) { d := DHCPv6Message{} d.SetMessage(MessageTypeSolicit) - xid := TransactionID{0xa, 0xb, 0xc} - d.SetTransactionID(xid) - opt := OptionGeneric{OptionCode: 0, OptionData: []byte{}} - d.AddOption(&opt) + d.SetTransactionID(TransactionID{0xa, 0xb, 0xc}) + d.AddOption(&OptionGeneric{OptionCode: 0, OptionData: []byte{}}) + bytes := d.ToBytes() expected := []byte{01, 0xa, 0xb, 0xc, 0x00, 0x00, 0x00, 0x00} require.Equal(t, expected, bytes) diff --git a/dhcpv6/dhcpv6message.go b/dhcpv6/dhcpv6message.go index 078f72a..43d9f50 100644 --- a/dhcpv6/dhcpv6message.go +++ b/dhcpv6/dhcpv6message.go @@ -314,9 +314,7 @@ func (d *DHCPv6Message) ToBytes() []byte { buf := uio.NewBigEndianBuffer(nil) buf.Write8(uint8(d.messageType)) buf.WriteBytes(d.transactionID[:]) - for _, opt := range d.options { - buf.WriteBytes(opt.ToBytes()) - } + buf.WriteBytes(d.options.ToBytes()) return buf.Data() } diff --git a/dhcpv6/dhcpv6relay.go b/dhcpv6/dhcpv6relay.go index e2024f4..337bda6 100644 --- a/dhcpv6/dhcpv6relay.go +++ b/dhcpv6/dhcpv6relay.go @@ -59,9 +59,7 @@ func (r *DHCPv6Relay) ToBytes() []byte { buf.Write8(byte(r.hopCount)) buf.WriteBytes(r.linkAddr.To16()) buf.WriteBytes(r.peerAddr.To16()) - for _, opt := range r.options { - buf.WriteBytes(opt.ToBytes()) - } + buf.WriteBytes(r.options.ToBytes()) return buf.Data() } diff --git a/dhcpv6/option_archtype.go b/dhcpv6/option_archtype.go index b3d36a4..afb22b1 100644 --- a/dhcpv6/option_archtype.go +++ b/dhcpv6/option_archtype.go @@ -23,8 +23,6 @@ func (op *OptClientArchType) Code() OptionCode { // ToBytes marshals the client arch type as defined by RFC 5970. func (op *OptClientArchType) ToBytes() []byte { buf := uio.NewBigEndianBuffer(nil) - buf.Write16(uint16(OptionClientArchType)) - buf.Write16(uint16(op.Length())) for _, at := range op.ArchTypes { buf.Write16(uint16(at)) } diff --git a/dhcpv6/option_archtype_test.go b/dhcpv6/option_archtype_test.go index abf872d..cf9f432 100644 --- a/dhcpv6/option_archtype_test.go +++ b/dhcpv6/option_archtype_test.go @@ -26,14 +26,9 @@ func TestOptClientArchTypeParseAndToBytes(t *testing.T) { data := []byte{ 0, 8, // EFI_XSCALE } - expected := []byte{ - 0, 61, // OptionClientArchType - 0, 2, // length - 0, 8, // EFI_XSCALE - } opt, err := ParseOptClientArchType(data) require.NoError(t, err) - require.Equal(t, expected, opt.ToBytes()) + require.Equal(t, data, opt.ToBytes()) } func TestOptClientArchType(t *testing.T) { diff --git a/dhcpv6/option_bootfileurl.go b/dhcpv6/option_bootfileurl.go index e0ad63e..d34f5ec 100644 --- a/dhcpv6/option_bootfileurl.go +++ b/dhcpv6/option_bootfileurl.go @@ -2,8 +2,6 @@ package dhcpv6 import ( "fmt" - - "github.com/u-root/u-root/pkg/uio" ) // OptBootFileURL implements the OptionBootfileURL option @@ -21,11 +19,7 @@ func (op *OptBootFileURL) Code() OptionCode { // ToBytes serializes the option and returns it as a sequence of bytes func (op *OptBootFileURL) ToBytes() []byte { - buf := uio.NewBigEndianBuffer(nil) - buf.Write16(uint16(OptionBootfileURL)) - buf.Write16(uint16(len(op.BootFileURL))) - buf.WriteBytes(op.BootFileURL) - return buf.Data() + return op.BootFileURL } // Length returns the option length in bytes diff --git a/dhcpv6/option_bootfileurl_test.go b/dhcpv6/option_bootfileurl_test.go index f93dc47..97d3163 100644 --- a/dhcpv6/option_bootfileurl_test.go +++ b/dhcpv6/option_bootfileurl_test.go @@ -24,13 +24,11 @@ func TestOptBootFileURL(t *testing.T) { func TestOptBootFileURLToBytes(t *testing.T) { urlString := []byte("https://insomniac.slackware.it") - expected := []byte{00, 59, 00, byte(len(urlString))} - expected = append(expected, urlString...) opt := OptBootFileURL{ BootFileURL: urlString, } toBytes := opt.ToBytes() - if !bytes.Equal(toBytes, expected) { - t.Fatalf("Invalid ToBytes result. Expected %v, got %v", expected, toBytes) + if !bytes.Equal(toBytes, urlString) { + t.Fatalf("Invalid ToBytes result. Expected %v, got %v", urlString, toBytes) } } diff --git a/dhcpv6/option_clientid.go b/dhcpv6/option_clientid.go index 92ceb24..d0d31d6 100644 --- a/dhcpv6/option_clientid.go +++ b/dhcpv6/option_clientid.go @@ -2,8 +2,6 @@ package dhcpv6 import ( "fmt" - - "github.com/u-root/u-root/pkg/uio" ) // OptClientId represents a Client ID option @@ -20,11 +18,7 @@ func (op *OptClientId) Code() OptionCode { // ToBytes marshals the Client ID option as defined by RFC 3315, Section 22.2. func (op *OptClientId) ToBytes() []byte { - buf := uio.NewBigEndianBuffer(nil) - buf.Write16(uint16(OptionClientID)) - buf.Write16(uint16(op.Length())) - buf.WriteBytes(op.Cid.ToBytes()) - return buf.Data() + return op.Cid.ToBytes() } func (op *OptClientId) Length() int { diff --git a/dhcpv6/option_clientid_test.go b/dhcpv6/option_clientid_test.go index 5e95d1a..b147e57 100644 --- a/dhcpv6/option_clientid_test.go +++ b/dhcpv6/option_clientid_test.go @@ -30,8 +30,6 @@ func TestOptClientIdToBytes(t *testing.T) { }, } expected := []byte{ - 0, 1, // OptionClientID - 0, 10, // length 0, 3, // DUID_LL 0, 1, // hwtype ethernet 5, 4, 3, 2, 1, 0, // hw addr @@ -45,13 +43,9 @@ func TestOptClientIdDecodeEncode(t *testing.T) { 0, 1, // hwtype ethernet 5, 4, 3, 2, 1, 0, // hw addr } - expected := append([]byte{ - 0, 1, // OptionClientID - 0, 10, // length - }, data...) opt, err := ParseOptClientId(data) require.NoError(t, err) - require.Equal(t, expected, opt.ToBytes()) + require.Equal(t, data, opt.ToBytes()) } func TestOptionClientId(t *testing.T) { diff --git a/dhcpv6/option_dnsrecursivenameserver.go b/dhcpv6/option_dnsrecursivenameserver.go index 9018c4d..cd78c8c 100644 --- a/dhcpv6/option_dnsrecursivenameserver.go +++ b/dhcpv6/option_dnsrecursivenameserver.go @@ -23,8 +23,6 @@ func (op *OptDNSRecursiveNameServer) Code() OptionCode { // ToBytes returns the option serialized to bytes. func (op *OptDNSRecursiveNameServer) ToBytes() []byte { buf := uio.NewBigEndianBuffer(nil) - buf.Write16(uint16(OptionDNSRecursiveNameServer)) - buf.Write16(uint16(op.Length())) for _, ns := range op.NameServers { buf.WriteBytes(ns.To16()) } diff --git a/dhcpv6/option_dnsrecursivenameserver_test.go b/dhcpv6/option_dnsrecursivenameserver_test.go index 1a56797..436cf2f 100644 --- a/dhcpv6/option_dnsrecursivenameserver_test.go +++ b/dhcpv6/option_dnsrecursivenameserver_test.go @@ -26,11 +26,7 @@ func TestOptDNSRecursiveNameServerToBytes(t *testing.T) { ns1 := net.ParseIP("2a03:2880:fffe:c:face:b00c:0:35") ns2 := net.ParseIP("2001:4860:4860::8888") nameservers := []net.IP{ns1, ns2} - expected := []byte{ - 0, 23, // OptionDNSRecursiveNameServer - 0, 32, // length - } - expected = append(expected, []byte(ns1)...) + expected := append([]byte{}, []byte(ns1)...) expected = append(expected, []byte(ns2)...) opt := OptDNSRecursiveNameServer{NameServers: nameservers} require.Equal(t, expected, opt.ToBytes()) diff --git a/dhcpv6/option_domainsearchlist.go b/dhcpv6/option_domainsearchlist.go index 404a109..21d7c23 100644 --- a/dhcpv6/option_domainsearchlist.go +++ b/dhcpv6/option_domainsearchlist.go @@ -4,7 +4,6 @@ import ( "fmt" "github.com/insomniacslk/dhcp/rfc1035label" - "github.com/u-root/u-root/pkg/uio" ) // OptDomainSearchList list implements a OptionDomainSearchList option @@ -21,11 +20,7 @@ func (op *OptDomainSearchList) Code() OptionCode { // ToBytes marshals this option to bytes. func (op *OptDomainSearchList) ToBytes() []byte { - buf := uio.NewBigEndianBuffer(nil) - buf.Write16(uint16(OptionDomainSearchList)) - buf.Write16(uint16(op.Length())) - buf.WriteBytes(op.DomainSearchList.ToBytes()) - return buf.Data() + return op.DomainSearchList.ToBytes() } func (op *OptDomainSearchList) Length() int { diff --git a/dhcpv6/option_domainsearchlist_test.go b/dhcpv6/option_domainsearchlist_test.go index 0b4b6b0..67c053a 100644 --- a/dhcpv6/option_domainsearchlist_test.go +++ b/dhcpv6/option_domainsearchlist_test.go @@ -24,8 +24,6 @@ func TestParseOptDomainSearchList(t *testing.T) { func TestOptDomainSearchListToBytes(t *testing.T) { expected := []byte{ - 0, 24, // OptionDomainSearchList - 0, 33, // length 7, 'e', 'x', 'a', 'm', 'p', 'l', 'e', 3, 'c', 'o', 'm', 0, 6, 's', 'u', 'b', 'n', 'e', 't', 7, 'e', 'x', 'a', 'm', 'p', 'l', 'e', 3, 'o', 'r', 'g', 0, } diff --git a/dhcpv6/option_elapsedtime.go b/dhcpv6/option_elapsedtime.go index cca5a5d..862cf28 100644 --- a/dhcpv6/option_elapsedtime.go +++ b/dhcpv6/option_elapsedtime.go @@ -21,8 +21,6 @@ func (op *OptElapsedTime) Code() OptionCode { // ToBytes marshals this option to bytes. func (op *OptElapsedTime) ToBytes() []byte { buf := uio.NewBigEndianBuffer(nil) - buf.Write16(uint16(OptionElapsedTime)) - buf.Write16(2) buf.Write16(uint16(op.ElapsedTime)) return buf.Data() } diff --git a/dhcpv6/option_elapsedtime_test.go b/dhcpv6/option_elapsedtime_test.go index d0d8e3f..a7ca85c 100644 --- a/dhcpv6/option_elapsedtime_test.go +++ b/dhcpv6/option_elapsedtime_test.go @@ -22,7 +22,7 @@ func TestOptElapsedTime(t *testing.T) { func TestOptElapsedTimeToBytes(t *testing.T) { opt := OptElapsedTime{} - expected := []byte{0, 8, 0, 2, 0, 0} + expected := []byte{0, 0} if toBytes := opt.ToBytes(); !bytes.Equal(expected, toBytes) { t.Fatalf("Invalid ToBytes output. Expected %v, got %v", expected, toBytes) } diff --git a/dhcpv6/option_iaaddress.go b/dhcpv6/option_iaaddress.go index 096e50c..11c8a00 100644 --- a/dhcpv6/option_iaaddress.go +++ b/dhcpv6/option_iaaddress.go @@ -25,9 +25,7 @@ func (op *OptIAAddress) Code() OptionCode { // ToBytes serializes the option and returns it as a sequence of bytes func (op *OptIAAddress) ToBytes() []byte { - buf := uio.NewBigEndianBuffer(make([]byte, 0, 28)) - buf.Write16(uint16(OptionIAAddr)) - buf.Write16(uint16(op.Length())) + buf := uio.NewBigEndianBuffer(nil) buf.WriteBytes(op.IPv6Addr.To16()) buf.Write32(op.PreferredLifetime) buf.Write32(op.ValidLifetime) diff --git a/dhcpv6/option_iaaddress_test.go b/dhcpv6/option_iaaddress_test.go index dc6298d..6a16e7f 100644 --- a/dhcpv6/option_iaaddress_test.go +++ b/dhcpv6/option_iaaddress_test.go @@ -44,13 +44,8 @@ func TestOptIAAddressParseInvalidBrokenOptions(t *testing.T) { } func TestOptIAAddressToBytes(t *testing.T) { - expected := []byte{ - 0, 5, // OptionIAAddr - 0, 30, // length - } ipBytes := []byte{0x24, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15} - expected = append(expected, ipBytes...) - expected = append(expected, []byte{ + expected := append(ipBytes, []byte{ 0xa, 0xb, 0xc, 0xd, // preferred lifetime 0xe, 0xf, 0x1, 0x2, // valid lifetime 0, 8, 0, 2, 0xaa, 0xbb, // options diff --git a/dhcpv6/option_iaprefix.go b/dhcpv6/option_iaprefix.go index cad1a0c..58cdadc 100644 --- a/dhcpv6/option_iaprefix.go +++ b/dhcpv6/option_iaprefix.go @@ -26,15 +26,11 @@ func (op *OptIAPrefix) Code() OptionCode { // ToBytes marshals this option according to RFC 3633, Section 10. func (op *OptIAPrefix) ToBytes() []byte { buf := uio.NewBigEndianBuffer(nil) - buf.Write16(uint16(OptionIAPrefix)) - buf.Write16(uint16(op.Length())) buf.Write32(op.PreferredLifetime) buf.Write32(op.ValidLifetime) buf.Write8(op.prefixLength) buf.WriteBytes(op.ipv6Prefix.To16()) - for _, opt := range op.Options { - buf.WriteBytes(opt.ToBytes()) - } + buf.WriteBytes(op.Options.ToBytes()) return buf.Data() } diff --git a/dhcpv6/option_iaprefix_test.go b/dhcpv6/option_iaprefix_test.go index 2c4dea4..48b410d 100644 --- a/dhcpv6/option_iaprefix_test.go +++ b/dhcpv6/option_iaprefix_test.go @@ -10,11 +10,11 @@ import ( func TestOptIAPrefix(t *testing.T) { buf := []byte{ - 0xaa, 0xbb, 0xcc, 0xdd, // preferredLifetime - 0xee, 0xff, 0x00, 0x11, // validLifetime + 0xaa, 0xbb, 0xcc, 0xdd, // preferredLifetime + 0xee, 0xff, 0x00, 0x11, // validLifetime 36, // prefixLength 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, // ipv6Prefix - 0, 8, 0, 2, 0xaa, 0xbb, // options + 0, 8, 0, 2, 0xaa, 0xbb, // options } opt, err := ParseOptIAPrefix(buf) if err != nil { @@ -39,24 +39,22 @@ func TestOptIAPrefix(t *testing.T) { func TestOptIAPrefixToBytes(t *testing.T) { buf := []byte{ - 0xaa, 0xbb, 0xcc, 0xdd, // preferredLifetime - 0xee, 0xff, 0x00, 0x11, // validLifetime + 0xaa, 0xbb, 0xcc, 0xdd, // preferredLifetime + 0xee, 0xff, 0x00, 0x11, // validLifetime 36, // prefixLength 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // ipv6Prefix - 0, 8, 0, 2, 0xaa, 0xbb, // options + 0, 8, 0, 2, 0xaa, 0xbb, // options } - expected := []byte{00, 26, 00, byte(len(buf))} - expected = append(expected, buf...) opt := OptIAPrefix{ PreferredLifetime: 0xaabbccdd, - ValidLifetime: 0xeeff0011, - prefixLength: 36, - ipv6Prefix: net.IPv6zero, + ValidLifetime: 0xeeff0011, + prefixLength: 36, + ipv6Prefix: net.IPv6zero, } opt.Options = append(opt.Options, &OptElapsedTime{ElapsedTime: 0xaabb}) toBytes := opt.ToBytes() - if !bytes.Equal(toBytes, expected) { - t.Fatalf("Invalid ToBytes result. Expected %v, got %v", expected, toBytes) + if !bytes.Equal(toBytes, buf) { + t.Fatalf("Invalid ToBytes result. Expected %v, got %v", buf, toBytes) } } @@ -64,8 +62,8 @@ func TestOptIAPrefixParseInvalidTooShort(t *testing.T) { buf := []byte{ 0xaa, 0xbb, 0xcc, 0xdd, // preferredLifetime 0xee, 0xff, 0x00, 0x11, // validLifetime - 36, // prefixLength - 0, 0, 0, 0, 0, 0, 0, // truncated ipv6Prefix + 36, // prefixLength + 0, 0, 0, 0, 0, 0, 0, // truncated ipv6Prefix } if opt, err := ParseOptIAPrefix(buf); err == nil { t.Fatalf("ParseOptIAPrefix: Expected error on truncated option, got %v", opt) @@ -74,9 +72,9 @@ func TestOptIAPrefixParseInvalidTooShort(t *testing.T) { func TestOptIAPrefixString(t *testing.T) { buf := []byte{ - 0xaa, 0xbb, 0xcc, 0xdd, // preferredLifetime - 0xee, 0xff, 0x00, 0x11, // validLifetime - 36, // prefixLength + 0xaa, 0xbb, 0xcc, 0xdd, // preferredLifetime + 0xee, 0xff, 0x00, 0x11, // validLifetime + 36, // prefixLength 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // ipv6Prefix } opt, err := ParseOptIAPrefix(buf) diff --git a/dhcpv6/option_interfaceid.go b/dhcpv6/option_interfaceid.go index 000be5d..5937170 100644 --- a/dhcpv6/option_interfaceid.go +++ b/dhcpv6/option_interfaceid.go @@ -2,8 +2,6 @@ package dhcpv6 import ( "fmt" - - "github.com/u-root/u-root/pkg/uio" ) // OptInterfaceId implements the interface-id option as defined by RFC 3315, @@ -20,11 +18,7 @@ func (op *OptInterfaceId) Code() OptionCode { } func (op *OptInterfaceId) ToBytes() []byte { - buf := uio.NewBigEndianBuffer(nil) - buf.Write16(uint16(OptionInterfaceID)) - buf.Write16(uint16(len(op.interfaceId))) - buf.WriteBytes(op.interfaceId) - return buf.Data() + return op.interfaceId } func (op *OptInterfaceId) InterfaceID() []byte { diff --git a/dhcpv6/option_interfaceid_test.go b/dhcpv6/option_interfaceid_test.go index b8c6b42..ec9e80d 100644 --- a/dhcpv6/option_interfaceid_test.go +++ b/dhcpv6/option_interfaceid_test.go @@ -23,13 +23,11 @@ func TestOptInterfaceId(t *testing.T) { func TestOptInterfaceIdToBytes(t *testing.T) { interfaceId := []byte("DSLAM01 eth2/1/01/21") - expected := []byte{00, 18, 00, byte(len(interfaceId))} - expected = append(expected, interfaceId...) opt := OptInterfaceId{} opt.SetInterfaceID(interfaceId) toBytes := opt.ToBytes() - if !bytes.Equal(toBytes, expected) { - t.Fatalf("Invalid ToBytes result. Expected %v, got %v", expected, toBytes) + if !bytes.Equal(toBytes, interfaceId) { + t.Fatalf("Invalid ToBytes result. Expected %v, got %v", interfaceId, toBytes) } } diff --git a/dhcpv6/option_nii.go b/dhcpv6/option_nii.go index 723c57a..ca86c22 100644 --- a/dhcpv6/option_nii.go +++ b/dhcpv6/option_nii.go @@ -38,8 +38,6 @@ func (op *OptNetworkInterfaceId) Code() OptionCode { func (op *OptNetworkInterfaceId) ToBytes() []byte { buf := uio.NewBigEndianBuffer(nil) - buf.Write16(uint16(OptionNII)) - buf.Write16(uint16(op.Length())) buf.Write8(op.type_) buf.Write8(op.major) buf.Write8(op.minor) diff --git a/dhcpv6/option_nii_test.go b/dhcpv6/option_nii_test.go index 190c0e3..ba9ed19 100644 --- a/dhcpv6/option_nii_test.go +++ b/dhcpv6/option_nii_test.go @@ -8,9 +8,9 @@ import ( func TestOptNetworkInterfaceIdParse(t *testing.T) { expected := []byte{ - 1, // type (UNDI) - 3, // major revision - 20, // minor revision + 1, // type (UNDI) + 3, // major revision + 20, // minor revision } opt, err := ParseOptNetworkInterfaceId(expected) require.NoError(t, err, "ParseOptNetworkInterfaceId() should not return an error with correct bytes") @@ -23,11 +23,9 @@ func TestOptNetworkInterfaceIdParse(t *testing.T) { func TestOptNetworkInterfaceIdToBytes(t *testing.T) { expected := []byte{ - 0, 62, // OptNetworkInterfaceId - 0, 3, // length - 1, // type (UNDI) - 3, // major revision - 20, // minor revision + 1, // type (UNDI) + 3, // major revision + 20, // minor revision } opt := OptNetworkInterfaceId{} opt.SetType(1) @@ -38,9 +36,9 @@ func TestOptNetworkInterfaceIdToBytes(t *testing.T) { func TestOptNetworkInterfaceIdTooShort(t *testing.T) { buf := []byte{ - 0, 62, // OptNetworkInterfaceId - 0, 3, // length - 1, // type (UNDI) + 0, 62, // OptNetworkInterfaceId + 0, 3, // length + 1, // type (UNDI) // missing major/minor revision bytes } _, err := ParseOptNetworkInterfaceId(buf) @@ -49,9 +47,9 @@ func TestOptNetworkInterfaceIdTooShort(t *testing.T) { func TestOptNetworkInterfaceIdString(t *testing.T) { buf := []byte{ - 1, // type (UNDI) - 3, // major revision - 20, // minor revision + 1, // type (UNDI) + 3, // major revision + 20, // minor revision } opt, err := ParseOptNetworkInterfaceId(buf) require.NoError(t, err) diff --git a/dhcpv6/option_nontemporaryaddress.go b/dhcpv6/option_nontemporaryaddress.go index 7e8bbaf..8515438 100644 --- a/dhcpv6/option_nontemporaryaddress.go +++ b/dhcpv6/option_nontemporaryaddress.go @@ -25,8 +25,6 @@ func (op *OptIANA) Code() OptionCode { // ToBytes serializes IANA to DHCPv6 bytes. func (op *OptIANA) ToBytes() []byte { 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) diff --git a/dhcpv6/option_nontemporaryaddress_test.go b/dhcpv6/option_nontemporaryaddress_test.go index a42601d..40aae53 100644 --- a/dhcpv6/option_nontemporaryaddress_test.go +++ b/dhcpv6/option_nontemporaryaddress_test.go @@ -99,8 +99,6 @@ func TestOptIANAToBytes(t *testing.T) { }, } expected := []byte{ - 0, 3, // OptionIANA - 0, 18, // length 1, 2, 3, 4, // IA ID 0, 0, 0x30, 0x39, // T1 = 12345 0, 0, 0xd4, 0x31, // T2 = 54321 diff --git a/dhcpv6/option_prefixdelegation.go b/dhcpv6/option_prefixdelegation.go index ee46876..eb5deb7 100644 --- a/dhcpv6/option_prefixdelegation.go +++ b/dhcpv6/option_prefixdelegation.go @@ -23,8 +23,6 @@ func (op *OptIAForPrefixDelegation) Code() OptionCode { // ToBytes serializes the option and returns it as a sequence of bytes func (op *OptIAForPrefixDelegation) ToBytes() []byte { buf := uio.NewBigEndianBuffer(nil) - buf.Write16(uint16(OptionIAPD)) - buf.Write16(uint16(op.Length())) buf.WriteBytes(op.IaId[:]) buf.Write32(op.T1) buf.Write32(op.T2) diff --git a/dhcpv6/option_prefixdelegation_test.go b/dhcpv6/option_prefixdelegation_test.go index 93044fe..377ca3d 100644 --- a/dhcpv6/option_prefixdelegation_test.go +++ b/dhcpv6/option_prefixdelegation_test.go @@ -15,7 +15,7 @@ func TestOptIAForPrefixDelegationParseOptIAForPrefixDelegation(t *testing.T) { 0, 26, 0, 25, // 26 = IAPrefix Option, 25 = length 0xaa, 0xbb, 0xcc, 0xdd, // IAPrefix preferredLifetime 0xee, 0xff, 0x00, 0x11, // IAPrefix validLifetime - 36, // IAPrefix prefixLength + 36, // IAPrefix prefixLength 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, // IAPrefix ipv6Prefix } opt, err := ParseOptIAForPrefixDelegation(data) @@ -45,7 +45,7 @@ func TestOptIAForPrefixDelegationParseOptIAForPrefixDelegationInvalidOptions(t * 0, 26, 0, 25, // 26 = IAPrefix Option, 25 = length 0xaa, 0xbb, 0xcc, 0xdd, // IAPrefix preferredLifetime 0xee, 0xff, 0x00, 0x11, // IAPrefix validLifetime - 36, // IAPrefix prefixLength + 36, // IAPrefix prefixLength 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // IAPrefix ipv6Prefix missing last byte } _, err := ParseOptIAForPrefixDelegation(data) @@ -54,8 +54,8 @@ func TestOptIAForPrefixDelegationParseOptIAForPrefixDelegationInvalidOptions(t * func TestOptIAForPrefixDelegationGetOneOption(t *testing.T) { buf := []byte{ - 0xaa, 0xbb, 0xcc, 0xdd, // preferredLifetime - 0xee, 0xff, 0x00, 0x11, // validLifetime + 0xaa, 0xbb, 0xcc, 0xdd, // preferredLifetime + 0xee, 0xff, 0x00, 0x11, // validLifetime 36, // prefixLength 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, // ipv6Prefix } @@ -70,8 +70,8 @@ func TestOptIAForPrefixDelegationGetOneOption(t *testing.T) { func TestOptIAForPrefixDelegationGetOneOptionMissingOpt(t *testing.T) { buf := []byte{ - 0xaa, 0xbb, 0xcc, 0xdd, // preferredLifetime - 0xee, 0xff, 0x00, 0x11, // validLifetime + 0xaa, 0xbb, 0xcc, 0xdd, // preferredLifetime + 0xee, 0xff, 0x00, 0x11, // validLifetime 36, // prefixLength 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, // ipv6Prefix } @@ -119,15 +119,13 @@ func TestOptIAForPrefixDelegationToBytes(t *testing.T) { opt.Options = append(opt.Options, &oaddr) expected := []byte{ - 0, 25, // OptionIAPD - 0, 41, // length 1, 2, 3, 4, // IA ID 0, 0, 0x30, 0x39, // T1 = 12345 0, 0, 0xd4, 0x31, // T2 = 54321 0, 26, 0, 25, // 26 = IAPrefix Option, 25 = length 0xaa, 0xbb, 0xcc, 0xdd, // IAPrefix preferredLifetime 0xee, 0xff, 0x00, 0x11, // IAPrefix validLifetime - 36, // IAPrefix prefixLength + 36, // IAPrefix prefixLength 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, // IAPrefix ipv6Prefix } require.Equal(t, expected, opt.ToBytes()) @@ -141,7 +139,7 @@ func TestOptIAForPrefixDelegationString(t *testing.T) { 0, 26, 0, 25, // 26 = IAPrefix Option, 25 = length 0xaa, 0xbb, 0xcc, 0xdd, // IAPrefix preferredLifetime 0xee, 0xff, 0x00, 0x11, // IAPrefix validLifetime - 36, // IAPrefix prefixLength + 36, // IAPrefix prefixLength 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, // IAPrefix ipv6Prefix } opt, err := ParseOptIAForPrefixDelegation(data) diff --git a/dhcpv6/option_relaymsg.go b/dhcpv6/option_relaymsg.go index 09589f6..c262dde 100644 --- a/dhcpv6/option_relaymsg.go +++ b/dhcpv6/option_relaymsg.go @@ -5,8 +5,6 @@ package dhcpv6 import ( "fmt" - - "github.com/u-root/u-root/pkg/uio" ) type OptRelayMsg struct { @@ -18,11 +16,7 @@ func (op *OptRelayMsg) Code() OptionCode { } func (op *OptRelayMsg) ToBytes() []byte { - buf := uio.NewBigEndianBuffer(nil) - buf.Write16(uint16(OptionRelayMsg)) - buf.Write16(uint16(op.Length())) - buf.WriteBytes(op.relayMessage.ToBytes()) - return buf.Data() + return op.relayMessage.ToBytes() } func (op *OptRelayMsg) RelayMessage() DHCPv6 { diff --git a/dhcpv6/option_remoteid.go b/dhcpv6/option_remoteid.go index 6b1831d..84a1a57 100644 --- a/dhcpv6/option_remoteid.go +++ b/dhcpv6/option_remoteid.go @@ -21,8 +21,6 @@ func (op *OptRemoteId) Code() OptionCode { // ToBytes serializes this option to a byte stream. func (op *OptRemoteId) ToBytes() []byte { 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() diff --git a/dhcpv6/option_remoteid_test.go b/dhcpv6/option_remoteid_test.go index bc13d43..51566e3 100644 --- a/dhcpv6/option_remoteid_test.go +++ b/dhcpv6/option_remoteid_test.go @@ -28,8 +28,7 @@ func TestOptRemoteId(t *testing.T) { func TestOptRemoteIdToBytes(t *testing.T) { remoteId := []byte("DSLAM01 eth2/1/01/21") - expected := []byte{00, 37, 00, byte(len(remoteId) + 4), 00, 00, 00, 00} - expected = append(expected, remoteId...) + expected := append([]byte{0, 0, 0, 0}, remoteId...) opt := OptRemoteId{ remoteId: remoteId, } diff --git a/dhcpv6/option_requestedoption.go b/dhcpv6/option_requestedoption.go index 7f345f4..d72e2ed 100644 --- a/dhcpv6/option_requestedoption.go +++ b/dhcpv6/option_requestedoption.go @@ -21,8 +21,6 @@ func (op *OptRequestedOption) Code() OptionCode { func (op *OptRequestedOption) ToBytes() []byte { buf := uio.NewBigEndianBuffer(nil) - buf.Write16(uint16(OptionORO)) - buf.Write16(uint16(op.Length())) for _, ro := range op.requestedOptions { buf.Write16(uint16(ro)) } diff --git a/dhcpv6/option_serverid.go b/dhcpv6/option_serverid.go index 4b1a828..7202428 100644 --- a/dhcpv6/option_serverid.go +++ b/dhcpv6/option_serverid.go @@ -2,8 +2,6 @@ package dhcpv6 import ( "fmt" - - "github.com/u-root/u-root/pkg/uio" ) // OptServerId represents a Server ID option @@ -20,11 +18,7 @@ func (op *OptServerId) Code() OptionCode { // ToBytes serializes this option. func (op *OptServerId) ToBytes() []byte { - buf := uio.NewBigEndianBuffer(nil) - buf.Write16(uint16(OptionServerID)) - buf.Write16(uint16(op.Length())) - buf.WriteBytes(op.Sid.ToBytes()) - return buf.Data() + return op.Sid.ToBytes() } func (op *OptServerId) Length() int { diff --git a/dhcpv6/option_serverid_test.go b/dhcpv6/option_serverid_test.go index 72b1e33..2cd37a3 100644 --- a/dhcpv6/option_serverid_test.go +++ b/dhcpv6/option_serverid_test.go @@ -30,8 +30,6 @@ func TestOptServerIdToBytes(t *testing.T) { }, } expected := []byte{ - 0, 2, // OptionServerID - 0, 10, // length 0, 3, // DUID_LL 0, 1, // hwtype ethernet 5, 4, 3, 2, 1, 0, // hw addr @@ -45,13 +43,9 @@ func TestOptServerIdDecodeEncode(t *testing.T) { 0, 1, // hwtype ethernet 5, 4, 3, 2, 1, 0, // hw addr } - expected := append([]byte{ - 0, 2, // OptionServerID - 0, 10, // length - }, data...) opt, err := ParseOptServerId(data) require.NoError(t, err) - require.Equal(t, expected, opt.ToBytes()) + require.Equal(t, data, opt.ToBytes()) } func TestOptionServerId(t *testing.T) { diff --git a/dhcpv6/option_statuscode.go b/dhcpv6/option_statuscode.go index a368a09..dad4ff6 100644 --- a/dhcpv6/option_statuscode.go +++ b/dhcpv6/option_statuscode.go @@ -24,8 +24,6 @@ func (op *OptStatusCode) Code() OptionCode { // ToBytes serializes the option and returns it as a sequence of bytes func (op *OptStatusCode) ToBytes() []byte { buf := uio.NewBigEndianBuffer(nil) - buf.Write16(uint16(OptionStatusCode)) - buf.Write16(uint16(op.Length())) buf.Write16(uint16(op.StatusCode)) buf.WriteBytes(op.StatusMessage) return buf.Data() diff --git a/dhcpv6/option_statuscode_test.go b/dhcpv6/option_statuscode_test.go index a2cce0f..cf43ab7 100644 --- a/dhcpv6/option_statuscode_test.go +++ b/dhcpv6/option_statuscode_test.go @@ -20,8 +20,6 @@ func TestParseOptStatusCode(t *testing.T) { func TestOptStatusCodeToBytes(t *testing.T) { expected := []byte{ - 0, 13, // OptionStatusCode - 0, 9, // length 0, 0, // StatusSuccess 's', 'u', 'c', 'c', 'e', 's', 's', } diff --git a/dhcpv6/option_userclass.go b/dhcpv6/option_userclass.go index 15f3512..75e1d3b 100644 --- a/dhcpv6/option_userclass.go +++ b/dhcpv6/option_userclass.go @@ -23,8 +23,6 @@ func (op *OptUserClass) Code() OptionCode { // ToBytes serializes the option and returns it as a sequence of bytes func (op *OptUserClass) ToBytes() []byte { buf := uio.NewBigEndianBuffer(nil) - buf.Write16(uint16(OptionUserClass)) - buf.Write16(uint16(op.Length())) for _, uc := range op.UserClasses { buf.Write16(uint16(len(uc))) buf.WriteBytes(uc) diff --git a/dhcpv6/option_userclass_test.go b/dhcpv6/option_userclass_test.go index 8a03319..16fae72 100644 --- a/dhcpv6/option_userclass_test.go +++ b/dhcpv6/option_userclass_test.go @@ -40,8 +40,6 @@ func TestOptUserClassToBytes(t *testing.T) { } data := opt.ToBytes() expected := []byte{ - 0, 15, // OptionUserClass - 0, 11, // length 0, 9, 'l', 'i', 'n', 'u', 'x', 'b', 'o', 'o', 't', } require.Equal(t, expected, data) @@ -56,8 +54,6 @@ func TestOptUserClassToBytesMultiple(t *testing.T) { } data := opt.ToBytes() expected := []byte{ - 0, 15, // OptionUserClass - 0, 17, // length 0, 9, 'l', 'i', 'n', 'u', 'x', 'b', 'o', 'o', 't', 0, 4, 't', 'e', 's', 't', } diff --git a/dhcpv6/option_vendor_opts.go b/dhcpv6/option_vendor_opts.go index 027e7c3..3deeab9 100644 --- a/dhcpv6/option_vendor_opts.go +++ b/dhcpv6/option_vendor_opts.go @@ -23,8 +23,6 @@ func (op *OptVendorOpts) Code() OptionCode { // ToBytes serializes the option and returns it as a sequence of bytes func (op *OptVendorOpts) ToBytes() []byte { buf := uio.NewBigEndianBuffer(nil) - buf.Write16(uint16(OptionVendorOpts)) - buf.Write16(uint16(op.Length())) buf.Write32(uint32(op.EnterpriseNumber)) buf.WriteData(op.VendorOpts.ToBytes()) return buf.Data() diff --git a/dhcpv6/option_vendor_opts_test.go b/dhcpv6/option_vendor_opts_test.go index cce4650..043995d 100644 --- a/dhcpv6/option_vendor_opts_test.go +++ b/dhcpv6/option_vendor_opts_test.go @@ -33,13 +33,11 @@ func TestOptVendorOptsToBytes(t *testing.T) { var opts []Option opts = append(opts, &OptionGeneric{OptionCode: 1, OptionData: optData}) - var expected []byte - expected = append(expected, []byte{0, 17, // VendorOption Code 17 - 0, byte(len(optData) + 8), // Length of optionData + 4 (code & length of sub-option) + 4 for EnterpriseNumber Length + expected := append([]byte{ 0, 0, 0, 0, // EnterpriseNumber 0, 1, // Sub-Option code from vendor - 0, byte(len(optData))}...) // Length of optionData only - expected = append(expected, optData...) + 0, byte(len(optData)), // Length of optionData only + }, optData...) opt := OptVendorOpts{ EnterpriseNumber: 0000, diff --git a/dhcpv6/option_vendorclass.go b/dhcpv6/option_vendorclass.go index b7204e7..67fa15a 100644 --- a/dhcpv6/option_vendorclass.go +++ b/dhcpv6/option_vendorclass.go @@ -22,8 +22,6 @@ func (op *OptVendorClass) Code() OptionCode { // ToBytes serializes the option and returns it as a sequence of bytes func (op *OptVendorClass) ToBytes() []byte { buf := uio.NewBigEndianBuffer(nil) - buf.Write16(uint16(OptionVendorClass)) - buf.Write16(uint16(op.Length())) buf.Write32(uint32(op.EnterpriseNumber)) for _, data := range op.Data { buf.Write16(uint16(len(data))) diff --git a/dhcpv6/option_vendorclass_test.go b/dhcpv6/option_vendorclass_test.go index 7774b25..685b8b0 100644 --- a/dhcpv6/option_vendorclass_test.go +++ b/dhcpv6/option_vendorclass_test.go @@ -31,8 +31,6 @@ func TestOptVendorClassToBytes(t *testing.T) { } data := opt.ToBytes() expected := []byte{ - 0, 16, // OptionVendorClass - 0, 22, // length 0xaa, 0xbb, 0xcc, 0xdd, // EnterpriseNumber 0, 10, 'H', 'T', 'T', 'P', 'C', 'l', 'i', 'e', 'n', 't', 0, 4, 't', 'e', 's', 't', diff --git a/dhcpv6/options.go b/dhcpv6/options.go index 59742de..6c403f5 100644 --- a/dhcpv6/options.go +++ b/dhcpv6/options.go @@ -24,11 +24,7 @@ func (og *OptionGeneric) Code() OptionCode { } func (og *OptionGeneric) ToBytes() []byte { - buf := uio.NewBigEndianBuffer(nil) - buf.Write16(uint16(og.OptionCode)) - buf.Write16(uint16(len(og.OptionData))) - buf.WriteBytes(og.OptionData) - return buf.Data() + return og.OptionData } func (og *OptionGeneric) String() string { @@ -152,11 +148,15 @@ func (o *Options) Update(option Option) { // ToBytes marshals all options to bytes. func (o Options) ToBytes() []byte { - var buf []byte + buf := uio.NewBigEndianBuffer(nil) for _, opt := range o { - buf = append(buf, opt.ToBytes()...) + buf.Write16(uint16(opt.Code())) + + val := opt.ToBytes() + buf.Write16(uint16(len(val))) + buf.WriteBytes(val) } - return buf + return buf.Data() } // FromBytes reads data into o and returns an error if the options are not a |