diff options
author | Pablo Mazzini <pmazzini@gmail.com> | 2018-07-08 11:20:32 +0100 |
---|---|---|
committer | insomniac <insomniacslk@users.noreply.github.com> | 2018-07-08 11:20:32 +0100 |
commit | f15362a37221d3c20f1844dfdf6151a265123608 (patch) | |
tree | b0a4dc6efd2813275977328a039349fa4e4713aa /dhcpv6 | |
parent | 5062bfae598f68ed7c57b3c14259d1a1ca6476fa (diff) |
add DelOption to OptIANA (#77)
Diffstat (limited to 'dhcpv6')
-rw-r--r-- | dhcpv6/dhcpv6.go | 10 | ||||
-rw-r--r-- | dhcpv6/option_nontemporaryaddress.go | 5 | ||||
-rw-r--r-- | dhcpv6/option_nontemporaryaddress_test.go | 19 |
3 files changed, 34 insertions, 0 deletions
diff --git a/dhcpv6/dhcpv6.go b/dhcpv6/dhcpv6.go index 7a53b90..d8427ea 100644 --- a/dhcpv6/dhcpv6.go +++ b/dhcpv6/dhcpv6.go @@ -117,6 +117,16 @@ func getOption(options []Option, code OptionCode) Option { return opts[0] } +func delOption(options []Option, code OptionCode) []Option { + newOpts := make([]Option, 0, len(options)) + for _, opt := range options { + if opt.Code() != code { + newOpts = append(newOpts, opt) + } + } + return newOpts +} + // DecapsulateRelay extracts the content of a relay message. It does not recurse // if there are nested relay messages. Returns the original packet if is not not // a relay message diff --git a/dhcpv6/option_nontemporaryaddress.go b/dhcpv6/option_nontemporaryaddress.go index cefca64..9f91b18 100644 --- a/dhcpv6/option_nontemporaryaddress.go +++ b/dhcpv6/option_nontemporaryaddress.go @@ -45,6 +45,11 @@ func (op *OptIANA) String() string { op.IaId, op.T1, op.T2, op.Options) } +// DelOption will remove all the options that match a Option code. +func (op *OptIANA) DelOption(code OptionCode) { + op.Options = delOption(op.Options, code) +} + // build an OptIANA structure from a sequence of bytes. // The input data does not include option code and length bytes. func ParseOptIANA(data []byte) (*OptIANA, error) { diff --git a/dhcpv6/option_nontemporaryaddress_test.go b/dhcpv6/option_nontemporaryaddress_test.go index 4d87122..a9e7087 100644 --- a/dhcpv6/option_nontemporaryaddress_test.go +++ b/dhcpv6/option_nontemporaryaddress_test.go @@ -39,6 +39,25 @@ func TestOptIANAParseOptIANAInvalidOptions(t *testing.T) { require.Error(t, err) } +func TestOptIANADelOption(t *testing.T) { + optiana1 := OptIANA{} + optiana2 := OptIANA{} + optiaaddr := OptIAAddress{} + optsc := OptStatusCode{} + + optiana1.Options = append(optiana1.Options, &optsc) + optiana1.Options = append(optiana1.Options, &optiaaddr) + optiana1.Options = append(optiana1.Options, &optiaaddr) + optiana1.DelOption(OPTION_IAADDR) + require.Equal(t, optiana1.Options, []Option{&optsc}) + + optiana2.Options = append(optiana2.Options, &optiaaddr) + optiana2.Options = append(optiana2.Options, &optsc) + optiana2.Options = append(optiana2.Options, &optiaaddr) + optiana2.DelOption(OPTION_IAADDR) + require.Equal(t, optiana2.Options, []Option{&optsc}) +} + func TestOptIANAToBytes(t *testing.T) { opt := OptIANA{ IaId: [4]byte{1, 2, 3, 4}, |