diff options
author | Chris Koch <chrisko@google.com> | 2019-12-28 03:31:44 -0800 |
---|---|---|
committer | Chris K <c@chrisko.ch> | 2020-03-09 15:38:59 -0700 |
commit | a3af88f6782eba884ef5216b987bf4e2a96ff033 (patch) | |
tree | ae4402ecfd66c1fdcbbf8f3175cb90718de3dc29 | |
parent | 817408d8cc264703a709f21d90ebe62c10a9b87c (diff) |
v6: getter for InterfaceID on RelayOptions
Signed-off-by: Chris Koch <chrisko@google.com>
-rw-r--r-- | dhcpv6/dhcpv6relay.go | 12 | ||||
-rw-r--r-- | dhcpv6/dhcpv6relay_test.go | 2 | ||||
-rw-r--r-- | dhcpv6/option_interfaceid.go | 37 | ||||
-rw-r--r-- | dhcpv6/option_interfaceid_test.go | 23 | ||||
-rw-r--r-- | dhcpv6/options.go | 2 |
5 files changed, 37 insertions, 39 deletions
diff --git a/dhcpv6/dhcpv6relay.go b/dhcpv6/dhcpv6relay.go index 359e6cd..04a3b94 100644 --- a/dhcpv6/dhcpv6relay.go +++ b/dhcpv6/dhcpv6relay.go @@ -30,6 +30,18 @@ func (ro RelayOptions) RelayMessage() DHCPv6 { return nil } +// InterfaceID returns the interface ID of this relay message. +func (ro RelayOptions) InterfaceID() []byte { + opt := ro.Options.GetOne(OptionInterfaceID) + if opt == nil { + return nil + } + if iid, ok := opt.(*optInterfaceID); ok { + return iid.ID + } + return nil +} + // RelayMessage is a DHCPv6 relay agent message as defined by RFC 3315 Section // 7. type RelayMessage struct { diff --git a/dhcpv6/dhcpv6relay_test.go b/dhcpv6/dhcpv6relay_test.go index 3e37e63..9bfe2e2 100644 --- a/dhcpv6/dhcpv6relay_test.go +++ b/dhcpv6/dhcpv6relay_test.go @@ -90,7 +90,7 @@ func TestNewRelayRepFromRelayForw(t *testing.T) { rf.MessageType = MessageTypeRelayForward rf.PeerAddr = net.IPv6linklocalallrouters rf.LinkAddr = net.IPv6interfacelocalallnodes - rf.AddOption(&OptInterfaceId{}) + rf.AddOption(OptInterfaceID(nil)) rf.AddOption(&OptRemoteId{}) // create the inner message diff --git a/dhcpv6/option_interfaceid.go b/dhcpv6/option_interfaceid.go index 5ed2fba..ce85714 100644 --- a/dhcpv6/option_interfaceid.go +++ b/dhcpv6/option_interfaceid.go @@ -4,39 +4,32 @@ import ( "fmt" ) -// OptInterfaceId implements the interface-id option as defined by RFC 3315, +// OptInterfaceID returns an interface id option as defined by RFC 3315, // Section 22.18. -// -// This module defines the OptInterfaceId structure. -// https://www.ietf.org/rfc/rfc3315.txt -type OptInterfaceId struct { - interfaceId []byte +func OptInterfaceID(id []byte) Option { + return &optInterfaceID{ID: id} } -func (op *OptInterfaceId) Code() OptionCode { - return OptionInterfaceID -} - -func (op *OptInterfaceId) ToBytes() []byte { - return op.interfaceId +type optInterfaceID struct { + ID []byte } -func (op *OptInterfaceId) InterfaceID() []byte { - return op.interfaceId +func (*optInterfaceID) Code() OptionCode { + return OptionInterfaceID } -func (op *OptInterfaceId) SetInterfaceID(interfaceId []byte) { - op.interfaceId = append([]byte(nil), interfaceId...) +func (op *optInterfaceID) ToBytes() []byte { + return op.ID } -func (op *OptInterfaceId) String() string { - return fmt.Sprintf("OptInterfaceId{interfaceid=%v}", op.interfaceId) +func (op *optInterfaceID) String() string { + return fmt.Sprintf("InterfaceID: %v", op.ID) } -// build an OptInterfaceId structure from a sequence of bytes. +// build an optInterfaceID structure from a sequence of bytes. // The input data does not include option code and length bytes. -func ParseOptInterfaceId(data []byte) (*OptInterfaceId, error) { - var opt OptInterfaceId - opt.interfaceId = append([]byte(nil), data...) +func parseOptInterfaceID(data []byte) (*optInterfaceID, error) { + var opt optInterfaceID + opt.ID = append([]byte(nil), data...) return &opt, nil } diff --git a/dhcpv6/option_interfaceid_test.go b/dhcpv6/option_interfaceid_test.go index df2a762..45c1799 100644 --- a/dhcpv6/option_interfaceid_test.go +++ b/dhcpv6/option_interfaceid_test.go @@ -7,31 +7,24 @@ import ( "github.com/stretchr/testify/require" ) -func TestOptInterfaceId(t *testing.T) { +func TestParseOptInterfaceID(t *testing.T) { expected := []byte("DSLAM01 eth2/1/01/21") - opt, err := ParseOptInterfaceId(expected) + opt, err := parseOptInterfaceID(expected) if err != nil { t.Fatal(err) } - if url := opt.InterfaceID(); !bytes.Equal(url, expected) { + if url := opt.ID; !bytes.Equal(url, expected) { t.Fatalf("Invalid Interface ID. Expected %v, got %v", expected, url) } } -func TestOptInterfaceIdToBytes(t *testing.T) { - interfaceId := []byte("DSLAM01 eth2/1/01/21") - opt := OptInterfaceId{} - opt.SetInterfaceID(interfaceId) - toBytes := opt.ToBytes() - if !bytes.Equal(toBytes, interfaceId) { - t.Fatalf("Invalid ToBytes result. Expected %v, got %v", interfaceId, toBytes) +func TestOptInterfaceID(t *testing.T) { + want := []byte("DSLAM01 eth2/1/01/21") + opt := OptInterfaceID(want) + if got := opt.ToBytes(); !bytes.Equal(got, want) { + t.Fatalf("%s.ToBytes() = %v, want %v", opt, got, want) } -} -func TestOptInterfaceIdString(t *testing.T) { - interfaceId := []byte("DSLAM01 eth2/1/01/21") - opt := OptInterfaceId{} - opt.SetInterfaceID(interfaceId) require.Contains( t, opt.String(), diff --git a/dhcpv6/options.go b/dhcpv6/options.go index d0bde7c..f91bcc4 100644 --- a/dhcpv6/options.go +++ b/dhcpv6/options.go @@ -64,7 +64,7 @@ func ParseOption(code OptionCode, optData []byte) (Option, error) { case OptionVendorOpts: opt, err = ParseOptVendorOpts(optData) case OptionInterfaceID: - opt, err = ParseOptInterfaceId(optData) + opt, err = parseOptInterfaceID(optData) case OptionDNSRecursiveNameServer: opt, err = parseOptDNS(optData) case OptionDomainSearchList: |