diff options
author | Dennis Marttinen <twelho@welho.tech> | 2022-07-14 14:52:58 +0300 |
---|---|---|
committer | Dennis Marttinen <twelho@welho.tech> | 2022-07-15 22:02:52 +0300 |
commit | 0405526172c87e4fc75325c9a195834a8fb4ba94 (patch) | |
tree | bdee0c60c57077e38c0d6c89dd4b78eb7d7e9edb | |
parent | 1ca156eafb9f20f7884eddc2cf610bade5dfb560 (diff) |
dhcpv4: enable deletion of options and add modifier for it
Signed-off-by: Dennis Marttinen <twelho@welho.tech>
-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) { |