diff options
author | Pablo Mazzini <pmazzini@gmail.com> | 2018-08-03 12:22:05 +0200 |
---|---|---|
committer | Pablo Mazzini <pmazzini@gmail.com> | 2018-08-10 15:25:49 +0200 |
commit | 27e10b9e7f76b99dbdc0d8fd779b18ef322ed92c (patch) | |
tree | 5bfd9b0d94b0e387893519620cec34e5f3a62783 | |
parent | 5207d76712250f33111c546d9ace98336d616bfc (diff) |
add IsRequested
-rw-r--r-- | dhcpv4/utils.go | 14 | ||||
-rw-r--r-- | dhcpv4/utils_test.go | 17 | ||||
-rw-r--r-- | dhcpv6/utils.go | 17 | ||||
-rw-r--r-- | dhcpv6/utils_test.go | 11 |
4 files changed, 55 insertions, 4 deletions
diff --git a/dhcpv4/utils.go b/dhcpv4/utils.go new file mode 100644 index 0000000..d95f586 --- /dev/null +++ b/dhcpv4/utils.go @@ -0,0 +1,14 @@ +package dhcpv4 + +// IsRequested function takes a DHCPv4 message and an OptionCode, and returns +// true if that option is within the requested options of the DHCPv6 message. +func IsRequested(pkt *DHCPv4, requested OptionCode) bool { + for _, optprl := range pkt.GetOption(OptionParameterRequestList) { + for _, o := range optprl.(*OptParameterRequestList).RequestedOpts { + if o == requested { + return true + } + } + } + return false +} diff --git a/dhcpv4/utils_test.go b/dhcpv4/utils_test.go new file mode 100644 index 0000000..d722466 --- /dev/null +++ b/dhcpv4/utils_test.go @@ -0,0 +1,17 @@ +package dhcpv4 + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestIsRequetsed(t *testing.T) { + pkt, err := New() + require.NoError(t, err) + require.False(t, IsRequested(pkt, OptionDomainNameServer)) + + optprl := OptParameterRequestList{RequestedOpts: []OptionCode{OptionDomainNameServer}} + pkt.AddOption(&optprl) + require.True(t, IsRequested(pkt, OptionDomainNameServer)) +} diff --git a/dhcpv6/utils.go b/dhcpv6/utils.go index 1681661..d1715da 100644 --- a/dhcpv6/utils.go +++ b/dhcpv6/utils.go @@ -11,16 +11,25 @@ import ( // if the "boot file" option is included in the packet, which is useful for // ADVERTISE/REPLY packet. func IsNetboot(msg DHCPv6) bool { + if IsRequested(msg, OptionBootfileURL) { + return true + } + if optbf := msg.GetOneOption(OptionBootfileURL); optbf != nil { + return true + } + return false +} + +// IsRequested function takes a DHCPv6 message and an OptionCode, and returns +// true if that option is within the requested options of the DHCPv6 message. +func IsRequested(msg DHCPv6, requested OptionCode) bool { for _, optoro := range msg.GetOption(OptionORO) { for _, o := range optoro.(*OptRequestedOption).RequestedOptions() { - if o == OptionBootfileURL { + if o == requested { return true } } } - if optbf := msg.GetOneOption(OptionBootfileURL); optbf != nil { - return true - } return false } diff --git a/dhcpv6/utils_test.go b/dhcpv6/utils_test.go index f3b53f0..779d55c 100644 --- a/dhcpv6/utils_test.go +++ b/dhcpv6/utils_test.go @@ -22,6 +22,17 @@ func TestIsNetboot(t *testing.T) { require.True(t, IsNetboot(&msg3)) } +func TestIsRequetsed(t *testing.T) { + msg1 := DHCPv6Message{} + require.False(t, IsRequested(&msg1, OptionDNSRecursiveNameServer)) + + msg2 := DHCPv6Message{} + optro := OptRequestedOption{} + optro.AddRequestedOption(OptionDNSRecursiveNameServer) + msg2.AddOption(&optro) + require.True(t, IsRequested(&msg2, OptionDNSRecursiveNameServer)) +} + func TestIsUsingUEFIArchTypeTrue(t *testing.T) { msg := DHCPv6Message{} opt := OptClientArchType{ArchType: EFI_BC} |