diff options
author | Brandon Bennett <bennetb@gmail.com> | 2018-05-11 14:09:24 -0600 |
---|---|---|
committer | insomniac <insomniacslk@users.noreply.github.com> | 2018-05-11 21:09:24 +0100 |
commit | d7cd0594274352de80d86a3bf32b487dfde63629 (patch) | |
tree | 11e49ad04a68378dc8e689c26098f7b4ef8f53c1 /dhcpv4/dhcpv4.go | |
parent | eab1c08cbc637f5d370bf84da1feba8673fb7cc3 (diff) |
add dhcpv4.GetOption and dhcpv4.GetSingleOption methods (#63)
GetOption and GetOneOption are convenience methods for getting a DHCPv4
option from its Option Code.
GetOption returns a list of options that match since the DHCP RFC allows
for an option to be present multiple times with its values appended
together. (Note: I am not sure I've ever seen this done and it's not clear if
how option parsing would work for more complex values. Is appending
done at the byte level? )
GetOneOption will return the first found option that matches a code.
Diffstat (limited to 'dhcpv4/dhcpv4.go')
-rw-r--r-- | dhcpv4/dhcpv4.go | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/dhcpv4/dhcpv4.go b/dhcpv4/dhcpv4.go index 3deda86..226c077 100644 --- a/dhcpv4/dhcpv4.go +++ b/dhcpv4/dhcpv4.go @@ -511,6 +511,31 @@ func (d *DHCPv4) Options() []Option { return d.options } +// GetOption will attempt to get all options that match a DHCPv4 option +// from its OptionCode. If the option was not found it will return an +// empty list. +func (d *DHCPv4) GetOption(code OptionCode) []Option { + opts := []Option{} + for _, opt := range d.Options() { + if opt.Code() == code { + opts = append(opts, opt) + } + } + return opts +} + +// GetOneOption will attempt to get an option that match a Option code. +// If there are multiple options with the same OptionCode it will only return +// the first one found. If no matching option is found nil will be returned. +func (d *DHCPv4) GetOneOption(code OptionCode) Option { + for _, opt := range d.Options() { + if opt.Code() == code { + return opt + } + } + return nil +} + // StrippedOptions works like Options, but it does not return anything after the // End option. func (d *DHCPv4) StrippedOptions() []Option { |