diff options
author | Chris Koch <chrisko@google.com> | 2019-12-28 02:24:53 -0800 |
---|---|---|
committer | insomniac <insomniacslk@users.noreply.github.com> | 2020-03-05 15:51:55 +0000 |
commit | 20d19b1f0dd7e6c43af491199d8bd121d5c8a0c8 (patch) | |
tree | 9c1c3f34ed1399c1ae6a2531604e5caaf7eb86cd | |
parent | ee59cc7ad87ad6705c3f4b9f9a73b6a1f690a330 (diff) |
v6: clean up option NII
Signed-off-by: Chris Koch <chrisko@google.com>
-rw-r--r-- | dhcpv6/option_nii.go | 95 | ||||
-rw-r--r-- | dhcpv6/option_nii_test.go | 29 | ||||
-rw-r--r-- | dhcpv6/options.go | 4 |
3 files changed, 58 insertions, 70 deletions
diff --git a/dhcpv6/option_nii.go b/dhcpv6/option_nii.go index bc47e8c..01d38d4 100644 --- a/dhcpv6/option_nii.go +++ b/dhcpv6/option_nii.go @@ -6,17 +6,27 @@ import ( "github.com/u-root/u-root/pkg/uio" ) +// NetworkInterfaceType is the NIC type as defined by RFC 4578 Section 2.2 +type NetworkInterfaceType uint8 + // see rfc4578 const ( - NII_LANDESK_NOPXE = 0 - NII_PXE_GEN_I = 1 - NII_PXE_GEN_II = 2 - NII_UNDI_NOEFI = 3 - NII_UNDI_EFI_GEN_I = 4 - NII_UNDI_EFI_GEN_II = 5 + NII_LANDESK_NOPXE NetworkInterfaceType = 0 + NII_PXE_GEN_I NetworkInterfaceType = 1 + NII_PXE_GEN_II NetworkInterfaceType = 2 + NII_UNDI_NOEFI NetworkInterfaceType = 3 + NII_UNDI_EFI_GEN_I NetworkInterfaceType = 4 + NII_UNDI_EFI_GEN_II NetworkInterfaceType = 5 ) -var niiToStringMap = map[uint8]string{ +func (nit NetworkInterfaceType) String() string { + if s, ok := niiToStringMap[nit]; ok { + return s + } + return fmt.Sprintf("NetworkInterfaceType(%d, unknown)", nit) +} + +var niiToStringMap = map[NetworkInterfaceType]string{ NII_LANDESK_NOPXE: "LANDesk service agent boot ROMs. No PXE", NII_PXE_GEN_I: "First gen. PXE boot ROMs", NII_PXE_GEN_II: "Second gen. PXE boot ROMs", @@ -25,66 +35,39 @@ var niiToStringMap = map[uint8]string{ NII_UNDI_EFI_GEN_II: "UNDI 32/64 bit. UEFI runtime 2nd gen", } -// OptNetworkInterfaceId implements the NIC ID option for network booting as +// OptNetworkInterfaceID implements the NIC ID option for network booting as // defined by RFC 4578 Section 2.2 and RFC 5970 Section 3.4. -type OptNetworkInterfaceId struct { - type_ uint8 - major, minor uint8 // revision number +type OptNetworkInterfaceID struct { + Typ NetworkInterfaceType + + // Revision number + Major, Minor uint8 } -func (op *OptNetworkInterfaceId) Code() OptionCode { +// Code implements Option.Code. +func (*OptNetworkInterfaceID) Code() OptionCode { return OptionNII } -func (op *OptNetworkInterfaceId) ToBytes() []byte { +// ToBytes implements Option.ToBytes. +func (op *OptNetworkInterfaceID) ToBytes() []byte { buf := uio.NewBigEndianBuffer(nil) - buf.Write8(op.type_) - buf.Write8(op.major) - buf.Write8(op.minor) + buf.Write8(uint8(op.Typ)) + buf.Write8(op.Major) + buf.Write8(op.Minor) return buf.Data() } -func (op *OptNetworkInterfaceId) Type() uint8 { - return op.type_ -} - -func (op *OptNetworkInterfaceId) SetType(type_ uint8) { - op.type_ = type_ -} - -func (op *OptNetworkInterfaceId) Major() uint8 { - return op.major -} - -func (op *OptNetworkInterfaceId) SetMajor(major uint8) { - op.major = major -} - -func (op *OptNetworkInterfaceId) Minor() uint8 { - return op.minor -} - -func (op *OptNetworkInterfaceId) SetMinor(minor uint8) { - op.minor = minor -} - -func (op *OptNetworkInterfaceId) String() string { - typeName, ok := niiToStringMap[op.type_] - if !ok { - typeName = "Unknown" - } - return fmt.Sprintf("OptNetworkInterfaceId{type=%v, revision=%v.%v}", - typeName, op.major, op.minor, - ) +func (op *OptNetworkInterfaceID) String() string { + return fmt.Sprintf("NetworkInterfaceID: %s (Revision %d.%d)", op.Typ, op.Major, op.Minor) } -// build an OptNetworkInterfaceId structure from a sequence of bytes. -// The input data does not include option code and length bytes. -func ParseOptNetworkInterfaceId(data []byte) (*OptNetworkInterfaceId, error) { +// FromBytes builds an OptNetworkInterfaceID structure from a sequence of +// bytes. The input data does not include option code and length bytes. +func (op *OptNetworkInterfaceID) FromBytes(data []byte) error { buf := uio.NewBigEndianBuffer(data) - var opt OptNetworkInterfaceId - opt.type_ = buf.Read8() - opt.major = buf.Read8() - opt.minor = buf.Read8() - return &opt, buf.FinError() + op.Typ = NetworkInterfaceType(buf.Read8()) + op.Major = buf.Read8() + op.Minor = buf.Read8() + return buf.FinError() } diff --git a/dhcpv6/option_nii_test.go b/dhcpv6/option_nii_test.go index a355122..6e3170c 100644 --- a/dhcpv6/option_nii_test.go +++ b/dhcpv6/option_nii_test.go @@ -12,12 +12,13 @@ func TestOptNetworkInterfaceIdParse(t *testing.T) { 3, // major revision 20, // minor revision } - opt, err := ParseOptNetworkInterfaceId(expected) + var opt OptNetworkInterfaceID + err := opt.FromBytes(expected) require.NoError(t, err, "ParseOptNetworkInterfaceId() should not return an error with correct bytes") require.Equal(t, OptionNII, opt.Code(), OptionNII, "Code() should return 62 for OptNetworkInterfaceId") - require.Equal(t, uint8(1), opt.Type(), "Type() should return 1 for UNDI") - require.Equal(t, uint8(3), opt.Major(), "Major() should return 1 for UNDI") - require.Equal(t, uint8(20), opt.Minor(), "Minor() should return 1 for UNDI") + require.Equal(t, NetworkInterfaceType(1), opt.Typ, "Typ should return 1 for UNDI") + require.Equal(t, uint8(3), opt.Major, "Major should return 1 for UNDI") + require.Equal(t, uint8(20), opt.Minor, "Minor should return 1 for UNDI") } func TestOptNetworkInterfaceIdToBytes(t *testing.T) { @@ -26,10 +27,10 @@ func TestOptNetworkInterfaceIdToBytes(t *testing.T) { 3, // major revision 20, // minor revision } - opt := OptNetworkInterfaceId{} - opt.SetType(1) - opt.SetMajor(3) - opt.SetMinor(20) + var opt OptNetworkInterfaceID + opt.Typ = NetworkInterfaceType(1) + opt.Major = 3 + opt.Minor = 20 require.Equal(t, expected, opt.ToBytes()) } @@ -38,7 +39,8 @@ func TestOptNetworkInterfaceIdTooShort(t *testing.T) { 1, // type (UNDI) // missing major/minor revision bytes } - _, err := ParseOptNetworkInterfaceId(buf) + var opt OptNetworkInterfaceID + err := opt.FromBytes(buf) require.Error(t, err, "ParseOptNetworkInterfaceId() should return an error on truncated options") } @@ -48,18 +50,19 @@ func TestOptNetworkInterfaceIdString(t *testing.T) { 3, // major revision 20, // minor revision } - opt, err := ParseOptNetworkInterfaceId(buf) + var opt OptNetworkInterfaceID + err := opt.FromBytes(buf) require.NoError(t, err) require.Contains( t, opt.String(), - "type=First gen. PXE boot ROMs, revision=3.20", + "First gen. PXE boot ROMs (Revision 3.20)", "String() should contain the type and revision", ) - opt.SetType(200) + opt.Typ = NetworkInterfaceType(200) require.Contains( t, opt.String(), - "type=Unknown", + "NetworkInterfaceType(200, unknown)", "String() should contain unknown for an unknown type", ) } diff --git a/dhcpv6/options.go b/dhcpv6/options.go index f773c7f..617761f 100644 --- a/dhcpv6/options.go +++ b/dhcpv6/options.go @@ -82,7 +82,9 @@ func ParseOption(code OptionCode, optData []byte) (Option, error) { case OptionClientArchType: opt, err = parseOptClientArchType(optData) case OptionNII: - opt, err = ParseOptNetworkInterfaceId(optData) + var o OptNetworkInterfaceID + err = o.FromBytes(optData) + opt = &o case Option4RD: opt, err = ParseOpt4RD(optData) case Option4RDMapRule: |