diff options
-rw-r--r-- | dhcpv6/modifiers.go | 16 | ||||
-rw-r--r-- | dhcpv6/modifiers_test.go | 16 | ||||
-rw-r--r-- | dhcpv6/option_requestedoption.go | 5 |
3 files changed, 37 insertions, 0 deletions
diff --git a/dhcpv6/modifiers.go b/dhcpv6/modifiers.go index 6cd66db..cbd2231 100644 --- a/dhcpv6/modifiers.go +++ b/dhcpv6/modifiers.go @@ -62,3 +62,19 @@ func WithArchType(at iana.ArchType) Modifier { return d } } + +// WithRequestedOptions adds requested options to the packet +func WithRequestedOptions(optionCodes ...OptionCode) Modifier { + return func(d DHCPv6) DHCPv6 { + opt := d.GetOneOption(OptionORO) + if opt == nil { + opt = &OptRequestedOption{} + } + oro := opt.(*OptRequestedOption) + for _, optionCode := range optionCodes { + oro.AddRequestedOption(optionCode) + } + d.UpdateOption(oro) + return d + } +} diff --git a/dhcpv6/modifiers_test.go b/dhcpv6/modifiers_test.go index ab21367..9953f8b 100644 --- a/dhcpv6/modifiers_test.go +++ b/dhcpv6/modifiers_test.go @@ -35,3 +35,19 @@ func TestWithServerID(t *testing.T) { sid := opt.(*OptServerId) require.Equal(t, sid.Sid, duid) } + +func TestWithRequestedOptions(t *testing.T) { + // Check if ORO is created when no ORO present + m, err := NewMessage(WithRequestedOptions(OptionClientID)) + require.NoError(t, err) + opt := m.GetOneOption(OptionORO) + require.NotNil(t, opt) + oro := opt.(*OptRequestedOption) + require.ElementsMatch(t, oro.RequestedOptions(), []OptionCode{OptionClientID}) + // Check if already set options are preserved + m = WithRequestedOptions(OptionServerID)(m) + opt = m.GetOneOption(OptionORO) + require.NotNil(t, opt) + oro = opt.(*OptRequestedOption) + require.ElementsMatch(t, oro.RequestedOptions(), []OptionCode{OptionClientID, OptionServerID}) +} diff --git a/dhcpv6/option_requestedoption.go b/dhcpv6/option_requestedoption.go index a55c533..9da9373 100644 --- a/dhcpv6/option_requestedoption.go +++ b/dhcpv6/option_requestedoption.go @@ -37,6 +37,11 @@ func (op *OptRequestedOption) SetRequestedOptions(opts []OptionCode) { } func (op *OptRequestedOption) AddRequestedOption(opt OptionCode) { + for _, requestedOption := range op.requestedOptions { + if opt == requestedOption { + fmt.Printf("Warning: option %s is already set, appending duplicate", OptionCodeToString[opt]) + } + } op.requestedOptions = append(op.requestedOptions, opt) } |