From 5e6e8baddaa29b866abe0b865e0c66c9190ec2f7 Mon Sep 17 00:00:00 2001 From: Andrea Barberio Date: Sat, 19 Jan 2019 11:55:16 +0000 Subject: dhcpv6: added Duid.Equal --- dhcpv6/duid.go | 24 +++++++++++++++++++++++- dhcpv6/duid_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/dhcpv6/duid.go b/dhcpv6/duid.go index c9e38b2..2ae8e60 100644 --- a/dhcpv6/duid.go +++ b/dhcpv6/duid.go @@ -1,6 +1,7 @@ package dhcpv6 import ( + "bytes" "encoding/binary" "fmt" "net" @@ -8,6 +9,7 @@ import ( "github.com/insomniacslk/dhcp/iana" ) +// DuidType is the DUID type as defined in rfc3315. type DuidType uint16 // DUID types @@ -18,6 +20,7 @@ const ( DUID_UUID DuidType = 4 ) +// DuidTypeToString maps a DuidType to a name. var DuidTypeToString = map[DuidType]string{ DUID_LL: "DUID-LL", DUID_LLT: "DUID-LLT", @@ -32,10 +35,11 @@ func (d DuidType) String() string { return "Unknown" } +// Duid is a DHCP Unique Identifier. type Duid struct { Type DuidType HwType iana.HWType // for DUID-LLT and DUID-LL. Ignored otherwise. RFC 826 - Time uint32 // for DUID-LLT. Ignored otherwise + Time uint32 // for DUID-LLT. Ignored otherwise LinkLayerAddr net.HardwareAddr EnterpriseNumber uint32 // for DUID-EN. Ignored otherwise EnterpriseIdentifier []byte // for DUID-EN. Ignored otherwise @@ -43,6 +47,7 @@ type Duid struct { Opaque []byte // for unknown DUIDs } +// Length returns the DUID length in bytes. func (d *Duid) Length() int { if d.Type == DUID_LLT { return 8 + len(d.LinkLayerAddr) @@ -57,6 +62,22 @@ func (d *Duid) Length() int { } } +// Equal compares two Duid objects. +func (d Duid) Equal(o Duid) bool { + if d.Type != o.Type || + d.HwType != o.HwType || + d.Time != o.Time || + !bytes.Equal(d.LinkLayerAddr, o.LinkLayerAddr) || + d.EnterpriseNumber != o.EnterpriseNumber || + !bytes.Equal(d.EnterpriseIdentifier, o.EnterpriseIdentifier) || + !bytes.Equal(d.Uuid, o.Uuid) || + !bytes.Equal(d.Opaque, o.Opaque) { + return false + } + return true +} + +// ToBytes serializes a Duid object. func (d *Duid) ToBytes() []byte { if d.Type == DUID_LLT { buf := make([]byte, 8) @@ -98,6 +119,7 @@ func (d *Duid) String() string { return fmt.Sprintf("DUID{type=%v hwtype=%v hwaddr=%v}", d.Type.String(), d.HwType.String(), hwaddr) } +// DuidFromBytes parses a Duid from a byte slice. func DuidFromBytes(data []byte) (*Duid, error) { if len(data) < 2 { return nil, fmt.Errorf("Invalid DUID: shorter than 2 bytes") diff --git a/dhcpv6/duid_test.go b/dhcpv6/duid_test.go index 840ab4a..5efa0e1 100644 --- a/dhcpv6/duid_test.go +++ b/dhcpv6/duid_test.go @@ -118,3 +118,31 @@ func TestOpaqueDuid(t *testing.T) { t.Fatalf("ToBytes: unexpected result: got %x, want %x", got, want) } } + +func TestDuidEqual(t *testing.T) { + d := Duid{ + Type: DUID_LL, + HwType: iana.HWTypeEthernet, + LinkLayerAddr: net.HardwareAddr{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}, + } + o := Duid{ + Type: DUID_LL, + HwType: iana.HWTypeEthernet, + LinkLayerAddr: net.HardwareAddr{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}, + } + require.True(t, d.Equal(o)) +} + +func TestDuidEqualNotEqual(t *testing.T) { + d := Duid{ + Type: DUID_LL, + HwType: iana.HWTypeEthernet, + LinkLayerAddr: net.HardwareAddr{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}, + } + o := Duid{ + Type: DUID_LL, + HwType: iana.HWTypeEthernet, + LinkLayerAddr: net.HardwareAddr{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0x00}, + } + require.False(t, d.Equal(o)) +} -- cgit v1.2.3