diff options
Diffstat (limited to 'dhcpv4')
-rw-r--r-- | dhcpv4/dhcpv4.go | 7 | ||||
-rw-r--r-- | dhcpv4/modifiers.go | 7 | ||||
-rw-r--r-- | dhcpv4/modifiers_test.go | 11 | ||||
-rw-r--r-- | dhcpv4/options.go | 5 |
4 files changed, 30 insertions, 0 deletions
diff --git a/dhcpv4/dhcpv4.go b/dhcpv4/dhcpv4.go index b894fbb..900bb22 100644 --- a/dhcpv4/dhcpv4.go +++ b/dhcpv4/dhcpv4.go @@ -382,6 +382,13 @@ func (d *DHCPv4) GetOneOption(code OptionCode) []byte { return d.Options.Get(code) } +// DeleteOption deletes an existing option with the given option code. +func (d *DHCPv4) DeleteOption(code OptionCode) { + if d.Options != nil { + d.Options.Del(code) + } +} + // UpdateOption replaces an existing option with the same option code with the // given one, adding it if not already present. func (d *DHCPv4) UpdateOption(opt Option) { diff --git a/dhcpv4/modifiers.go b/dhcpv4/modifiers.go index 0ab35bc..68da298 100644 --- a/dhcpv4/modifiers.go +++ b/dhcpv4/modifiers.go @@ -99,6 +99,13 @@ func WithOption(opt Option) Modifier { } } +// WithoutOption removes the DHCPv4 option with the given code +func WithoutOption(code OptionCode) Modifier { + return func(d *DHCPv4) { + d.DeleteOption(code) + } +} + // WithUserClass adds a user class option to the packet. // The rfc parameter allows you to specify if the userclass should be // rfc compliant or not. More details in issue #113 diff --git a/dhcpv4/modifiers_test.go b/dhcpv4/modifiers_test.go index 274b409..57d8ff0 100644 --- a/dhcpv4/modifiers_test.go +++ b/dhcpv4/modifiers_test.go @@ -44,6 +44,17 @@ func TestWithOptionModifier(t *testing.T) { require.Equal(t, "slackware.it", dnOpt) } +func TestWithoutOptionModifier(t *testing.T) { + d, err := New( + WithOption(OptDomainName("slackware.it")), + WithoutOption(OptionDomainName), + ) + require.NoError(t, err) + + require.False(t, d.Options.Has(OptionDomainName)) + require.Equal(t, "", d.DomainName()) +} + func TestUserClassModifier(t *testing.T) { d, err := New(WithUserClass("linuxboot", false)) require.NoError(t, err) diff --git a/dhcpv4/options.go b/dhcpv4/options.go index fdc79ae..9d404b4 100644 --- a/dhcpv4/options.go +++ b/dhcpv4/options.go @@ -81,6 +81,11 @@ func (o Options) Has(opcode OptionCode) bool { return ok } +// Del deletes the option matching the option code. +func (o Options) Del(opcode OptionCode) { + delete(o, opcode.Code()) +} + // Update updates the existing options with the passed option, adding it // at the end if not present already func (o Options) Update(option Option) { |