diff options
author | Chris Gorham <gorhamc90@gmail.com> | 2018-11-07 23:35:11 +0000 |
---|---|---|
committer | insomniac <insomniacslk@users.noreply.github.com> | 2018-11-07 23:35:11 +0000 |
commit | 7af404c171f0b1e63112b579e246dd3dc77e56ce (patch) | |
tree | 8ad3db2846e704ce37f77e0530aa822fc781deec /dhcpv6 | |
parent | c572359d4d841c1b9d3eadd06c2b05e6085897b2 (diff) |
Adding module for DHCPv6 Vendor Options (Opt 17) (#130)
Diffstat (limited to 'dhcpv6')
-rw-r--r-- | dhcpv6/option_vendor_opts.go | 117 | ||||
-rw-r--r-- | dhcpv6/option_vendor_opts_test.go | 101 | ||||
-rw-r--r-- | dhcpv6/options.go | 13 |
3 files changed, 230 insertions, 1 deletions
diff --git a/dhcpv6/option_vendor_opts.go b/dhcpv6/option_vendor_opts.go new file mode 100644 index 0000000..a2281e3 --- /dev/null +++ b/dhcpv6/option_vendor_opts.go @@ -0,0 +1,117 @@ +package dhcpv6 + +/* + This module defines the OptVendorOpts structure. + https://tools.ietf.org/html/rfc3315#section-22.17 + + Option 17 + 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | OPTION_VENDOR_OPTS | option-len | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | enterprise-number | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + . . + . option-data (sub-options) . + . . + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + + Sub-Option + 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | opt-code | option-len | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + . . + . option-data . + . . + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +*/ + +import ( + "encoding/binary" + "errors" + "fmt" +) + +// OptVendorOpts represents a DHCPv6 Status Code option +type OptVendorOpts struct { + EnterpriseNumber uint32 + VendorOpts []Option +} + +// Code returns the option code +func (op *OptVendorOpts) Code() OptionCode { + return OptionVendorOpts +} + +// ToBytes serializes the option and returns it as a sequence of bytes +func (op *OptVendorOpts) ToBytes() []byte { + buf := make([]byte, 8) + binary.BigEndian.PutUint16(buf[0:2], uint16(OptionVendorOpts)) + binary.BigEndian.PutUint16(buf[2:4], uint16(op.Length())) + binary.BigEndian.PutUint32(buf[4:8], uint32(op.EnterpriseNumber)) + for _, opt := range op.VendorOpts { + buf = append(buf, opt.ToBytes()...) + } + return buf +} + +// Length returns the option length +func (op *OptVendorOpts) Length() int { + l := 4 // 4 bytes for Enterprise Number + for _, opt := range op.VendorOpts { + l += 4 + opt.Length() // 4 bytes for Code and Length from Vendor + } + return l +} + +// String returns a string representation of the VendorOpts data +func (op *OptVendorOpts) String() string { + return fmt.Sprintf("OptVendorOpts{enterprisenum=%v, vendorOpts=%v}", + op.EnterpriseNumber, op.VendorOpts, + ) +} + +// ParseOptVendorOpts builds an OptVendorOpts structure from a sequence of bytes. +// The input data does not include option code and length bytes. +func ParseOptVendorOpts(data []byte) (*OptVendorOpts, error) { + opt := OptVendorOpts{} + if len(data) < 4 { + return nil, fmt.Errorf("Invalid vendor opts data length. Expected at least 4 bytes, got %v", len(data)) + } + opt.EnterpriseNumber = binary.BigEndian.Uint32(data[:4]) + + var err error + opt.VendorOpts, err = OptionsFromBytesWithParser(data[4:], vendParseOption) + if err != nil { + return nil, err + } + return &opt, nil +} + +// vendParseOption builds a GenericOption from a slice of bytes +// We cannot use the exisitng ParseOption function in options.go because the +// sub-options include codes specific to each vendor. There are overlaps in these +// codes with RFC standard codes. +func vendParseOption(dataStart []byte) (Option, error) { + // Parse a sequence of bytes as a single DHCPv6 option. + // Returns the option structure, or an error if any. + + if len(dataStart) < 4 { + return nil, fmt.Errorf("Invalid DHCPv6 vendor option: less than 4 bytes") + } + code := OptionCode(binary.BigEndian.Uint16(dataStart[:2])) + length := int(binary.BigEndian.Uint16(dataStart[2:4])) + if len(dataStart) < length+4 { + return nil, fmt.Errorf("Invalid option length for vendor option %v. Declared %v, actual %v", + code, length, len(dataStart)-4, + ) + } + + optData := dataStart[4 : 4+length] + if len(optData) < 1 { + return nil, errors.New("vendParseOption: at least one vendor options data is required") + } + + return &OptionGeneric{OptionCode: code, OptionData: optData}, nil +} diff --git a/dhcpv6/option_vendor_opts_test.go b/dhcpv6/option_vendor_opts_test.go new file mode 100644 index 0000000..8e62506 --- /dev/null +++ b/dhcpv6/option_vendor_opts_test.go @@ -0,0 +1,101 @@ +package dhcpv6 + +import ( + "bytes" + "fmt" + "reflect" + "testing" +) + +func TestOptVendorOpts(t *testing.T) { + optData := []byte("Arista;DCS-7304;01.00;HSH14425148") + expected := []byte{0xaa, 0xbb, 0xcc, 0xdd} + expected = append(expected, []byte{0, 1, //code + 0, byte(len(optData)), //length + }...) + expected = append(expected, optData...) + expectedOpts := OptVendorOpts{} + var vendorOpts []Option + expectedOpts.VendorOpts = append(vendorOpts, &OptionGeneric{OptionCode: 1, OptionData: optData}) + opt, err := ParseOptVendorOpts(expected) + if err != nil { + t.Fatal(err) + } + + if optLen := opt.Length(); optLen != len(expected) { + t.Fatalf("Invalid length. Expected %v, got %v", len(expected), optLen) + } + if en := opt.EnterpriseNumber; en != 0xaabbccdd { + t.Fatalf("Invalid Enterprise Number. Expected 0xaabbccdd, got %v", en) + } + if !reflect.DeepEqual(opt.VendorOpts, expectedOpts.VendorOpts) { + t.Fatalf("Invalid Vendor Option Data. Expected %v, got %v", expected, expectedOpts.VendorOpts) + } + + shortData := make([]byte, 1) + opt, err = ParseOptVendorOpts(shortData) + if err == nil { + t.Fatalf("Short data (<4 bytes) did not cause an error when it should have") + } + +} + +func TestOptVendorOptsToBytes(t *testing.T) { + optData := []byte("Arista;DCS-7304;01.00;HSH14425148") + 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 + 0, 0, 0, 0, // EnterpriseNumber + 0, 1, // Sub-Option code from vendor + 0, byte(len(optData))}...) // Length of optionData only + expected = append(expected, optData...) + + opt := OptVendorOpts{ + EnterpriseNumber: 0000, + VendorOpts: opts, + } + toBytes := opt.ToBytes() + if !bytes.Equal(toBytes, expected) { + t.Fatalf("Invalid ToBytes result. Expected %v, got %v", expected, toBytes) + } +} + +func TestVendParseOption(t *testing.T) { + var buf []byte + buf = append(buf, []byte{00, 1, 00, 33}...) + buf = append(buf, []byte("Arista;DCS-7304;01.00;HSH14425148")...) + + expected := &OptionGeneric{OptionCode: 1, OptionData: []byte("Arista;DCS-7304;01.00;HSH14425148")} + opt, err := vendParseOption(buf) + if err != nil { + fmt.Println(err) + } + if !reflect.DeepEqual(opt, expected) { + t.Fatalf("Invalid Vendor Parse Option result. Expected %v, got %v", expected, opt) + } + + + shortData := make([]byte, 1) // data length too small + opt, err = vendParseOption(shortData) + if err == nil { + t.Fatalf("Short data (<4 bytes) did not cause an error when it should have") + } + + shortData = []byte{0, 0, 0, 0} // missing actual vendor data. + opt, err = vendParseOption(shortData) + if err == nil { + t.Fatalf("Missing VendorData option. An error should have been returned but wasn't") + } + + shortData = []byte{0, 0, + 0, 4, // declared length + 0} // data starts here, length of 1 + opt, err = vendParseOption(shortData) + if err == nil { + t.Fatalf("Declared length does not match actual data length. An error should have been returned but wasn't") + } + +} diff --git a/dhcpv6/options.go b/dhcpv6/options.go index 5c4f321..8508b08 100644 --- a/dhcpv6/options.go +++ b/dhcpv6/options.go @@ -88,6 +88,8 @@ func ParseOption(dataStart []byte) (Option, error) { opt, err = ParseOptUserClass(optData) case OptionVendorClass: opt, err = ParseOptVendorClass(optData) + case OptionVendorOpts: + opt, err = ParseOptVendorOpts(optData) case OptionInterfaceID: opt, err = ParseOptInterfaceId(optData) case OptionDNSRecursiveNameServer: @@ -120,6 +122,15 @@ func ParseOption(dataStart []byte) (Option, error) { } func OptionsFromBytes(data []byte) ([]Option, error) { + return OptionsFromBytesWithParser(data, ParseOption) +} + +// OptionParser is a function signature for option parsing +type OptionParser func(data []byte) (Option, error) + +// OptionsFromBytesWithParser parses Options from byte sequences using the +// parsing function that is passed in as a paremeter +func OptionsFromBytesWithParser(data []byte, parser OptionParser) ([]Option, error) { // Parse a sequence of bytes until the end and build a list of options from // it. Returns an error if any invalid option or length is found. options := make([]Option, 0, 10) @@ -140,7 +151,7 @@ func OptionsFromBytes(data []byte) ([]Option, error) { // this should never happen return nil, fmt.Errorf("Error: reading past the end of options") } - opt, err := ParseOption(data[idx:]) + opt, err := parser(data[idx:]) if err != nil { return nil, err } |