diff options
author | Pablo Mazzini <pmazzini@gmail.com> | 2018-08-12 16:17:11 +0200 |
---|---|---|
committer | Pablo Mazzini <pmazzini@gmail.com> | 2018-08-13 11:52:28 +0200 |
commit | 72d70d68dfcbdbe112d9f82ead6d987898417f95 (patch) | |
tree | e36f9cfab0213ff4e43335966c57e7f73c0682bf /dhcpv6 | |
parent | b773f618967811e235b334516b0c40563c3a1464 (diff) |
fix OptClientArchType
Diffstat (limited to 'dhcpv6')
-rw-r--r-- | dhcpv6/modifiers.go | 6 | ||||
-rw-r--r-- | dhcpv6/option_archtype.go | 62 | ||||
-rw-r--r-- | dhcpv6/option_archtype_test.go | 5 |
3 files changed, 28 insertions, 45 deletions
diff --git a/dhcpv6/modifiers.go b/dhcpv6/modifiers.go index 32d11ac..6cd66db 100644 --- a/dhcpv6/modifiers.go +++ b/dhcpv6/modifiers.go @@ -2,6 +2,8 @@ package dhcpv6 import ( "log" + + "github.com/insomniacslk/dhcp/iana" ) // WithClientID adds a client ID option to a DHCPv6 packet @@ -53,9 +55,9 @@ func WithUserClass(uc []byte) Modifier { } // WithArchType adds an arch type option to the packet -func WithArchType(at ArchType) Modifier { +func WithArchType(at iana.ArchType) Modifier { return func(d DHCPv6) DHCPv6 { - ao := OptClientArchType{ArchType: at} + ao := OptClientArchType{ArchTypes: []iana.ArchType{at}} d.AddOption(&ao) return d } diff --git a/dhcpv6/option_archtype.go b/dhcpv6/option_archtype.go index a1b4a9b..231eddd 100644 --- a/dhcpv6/option_archtype.go +++ b/dhcpv6/option_archtype.go @@ -6,42 +6,14 @@ package dhcpv6 import ( "encoding/binary" "fmt" -) - -//ArchType encodes an architecture type in an uint16 -type ArchType uint16 + "strings" -// see rfc4578 -const ( - INTEL_X86PC ArchType = 0 - NEC_PC98 ArchType = 1 - EFI_ITANIUM ArchType = 2 - DEC_ALPHA ArchType = 3 - ARC_X86 ArchType = 4 - INTEL_LEAN_CLIENT ArchType = 5 - EFI_IA32 ArchType = 6 - EFI_BC ArchType = 7 - EFI_XSCALE ArchType = 8 - EFI_X86_64 ArchType = 9 + "github.com/insomniacslk/dhcp/iana" ) -// ArchTypeToStringMap maps an ArchType to a mnemonic name -var ArchTypeToStringMap = map[ArchType]string{ - INTEL_X86PC: "Intel x86PC", - NEC_PC98: "NEC/PC98", - EFI_ITANIUM: "EFI Itanium", - DEC_ALPHA: "DEC Alpha", - ARC_X86: "Arc x86", - INTEL_LEAN_CLIENT: "Intel Lean Client", - EFI_IA32: "EFI IA32", - EFI_BC: "EFI BC", - EFI_XSCALE: "EFI Xscale", - EFI_X86_64: "EFI x86-64", -} - // OptClientArchType represents an option CLIENT_ARCH_TYPE type OptClientArchType struct { - ArchType ArchType + ArchTypes []iana.ArchType } func (op *OptClientArchType) Code() OptionCode { @@ -49,23 +21,28 @@ func (op *OptClientArchType) Code() OptionCode { } func (op *OptClientArchType) ToBytes() []byte { - buf := make([]byte, 6) + buf := make([]byte, 4) binary.BigEndian.PutUint16(buf[0:2], uint16(OptionClientArchType)) binary.BigEndian.PutUint16(buf[2:4], uint16(op.Length())) - binary.BigEndian.PutUint16(buf[4:6], uint16(op.ArchType)) + u16 := make([]byte, 2) + for _, at := range op.ArchTypes { + binary.BigEndian.PutUint16(u16, uint16(at)) + buf = append(buf, u16...) + } return buf } func (op *OptClientArchType) Length() int { - return 2 + return 2*len(op.ArchTypes) } func (op *OptClientArchType) String() string { - name, ok := ArchTypeToStringMap[op.ArchType] - if !ok { - name = "Unknown" + atStrings := make([]string, 0) + for _, at := range op.ArchTypes { + name := iana.ArchTypeToString(at) + atStrings = append(atStrings, name) } - return fmt.Sprintf("OptClientArchType{archtype=%v}", name) + return fmt.Sprintf("OptClientArchType{archtype=%v}", strings.Join(atStrings, ", ")) } // ParseOptClientArchType builds an OptClientArchType structure from @@ -73,9 +50,12 @@ func (op *OptClientArchType) String() string { // length bytes. func ParseOptClientArchType(data []byte) (*OptClientArchType, error) { opt := OptClientArchType{} - if len(data) != 2 { - return nil, fmt.Errorf("Invalid arch type data length. Expected 2 bytes, got %v", len(data)) + if len(data) == 0 || len(data)%2 != 0 { + return nil, fmt.Errorf("Invalid arch type data length. Expected multiple of 2 larger than 2, got %v", len(data)) + } + for idx := 0; idx < len(data); idx += 2 { + b := data[idx : idx+2] + opt.ArchTypes = append(opt.ArchTypes, iana.ArchType(binary.BigEndian.Uint16(b))) } - opt.ArchType = ArchType(binary.BigEndian.Uint16(data)) return &opt, nil } diff --git a/dhcpv6/option_archtype_test.go b/dhcpv6/option_archtype_test.go index 748c8c5..1848e55 100644 --- a/dhcpv6/option_archtype_test.go +++ b/dhcpv6/option_archtype_test.go @@ -3,6 +3,7 @@ package dhcpv6 import ( "testing" + "github.com/insomniacslk/dhcp/iana" "github.com/stretchr/testify/require" ) @@ -12,7 +13,7 @@ func TestParseOptClientArchType(t *testing.T) { } opt, err := ParseOptClientArchType(data) require.NoError(t, err) - require.Equal(t, opt.ArchType, EFI_IA32) + require.Equal(t, opt.ArchTypes[0], iana.EFI_IA32) } func TestParseOptClientArchTypeInvalid(t *testing.T) { @@ -37,7 +38,7 @@ func TestOptClientArchTypeParseAndToBytes(t *testing.T) { func TestOptClientArchType(t *testing.T) { opt := OptClientArchType{ - ArchType: EFI_ITANIUM, + ArchTypes: []iana.ArchType{iana.EFI_ITANIUM}, } require.Equal(t, opt.Length(), 2) require.Equal(t, opt.Code(), OptionClientArchType) |