diff options
-rw-r--r-- | dhcpv4/dhcpv4.go | 13 | ||||
-rw-r--r-- | dhcpv4/dhcpv4_test.go | 10 | ||||
-rw-r--r-- | dhcpv6/dhcpv6.go | 54 | ||||
-rw-r--r-- | dhcpv6/dhcpv6_test.go | 47 | ||||
-rw-r--r-- | dhcpv6/dhcpv6message.go | 27 | ||||
-rw-r--r-- | dhcpv6/dhcpv6message_test.go | 34 | ||||
-rw-r--r-- | dhcpv6/utils.go | 77 | ||||
-rw-r--r-- | dhcpv6/utils_test.go | 69 |
8 files changed, 185 insertions, 146 deletions
diff --git a/dhcpv4/dhcpv4.go b/dhcpv4/dhcpv4.go index 5005375..ad6e319 100644 --- a/dhcpv4/dhcpv4.go +++ b/dhcpv4/dhcpv4.go @@ -682,6 +682,19 @@ func (d *DHCPv4) ValidateOptions() { } } +// IsOptionRequested returns true if that option is within the requested +// options of the DHCPv4 message. +func (d *DHCPv4) IsOptionRequested(requested OptionCode) bool { + for _, optprl := range d.GetOption(OptionParameterRequestList) { + for _, o := range optprl.(*OptParameterRequestList).RequestedOpts { + if o == requested { + return true + } + } + } + return false +} + // ToBytes encodes a DHCPv4 structure into a sequence of bytes in its wire // format. func (d *DHCPv4) ToBytes() []byte { diff --git a/dhcpv4/dhcpv4_test.go b/dhcpv4/dhcpv4_test.go index 6e3394c..eaa9266 100644 --- a/dhcpv4/dhcpv4_test.go +++ b/dhcpv4/dhcpv4_test.go @@ -401,6 +401,16 @@ func TestDHCPv4MessageTypeDiscovery(t *testing.T) { require.Equal(t, MessageTypeDiscover, *m.MessageType()) } +func TestIsOptionRequested(t *testing.T) { + pkt, err := New() + require.NoError(t, err) + require.False(t, pkt.IsOptionRequested(OptionDomainNameServer)) + + optprl := OptParameterRequestList{RequestedOpts: []OptionCode{OptionDomainNameServer}} + pkt.AddOption(&optprl) + require.True(t, pkt.IsOptionRequested(OptionDomainNameServer)) +} + // TODO // test broadcast/unicast flags // test Options setter/getter diff --git a/dhcpv6/dhcpv6.go b/dhcpv6/dhcpv6.go index 0dabca5..d82ed4a 100644 --- a/dhcpv6/dhcpv6.go +++ b/dhcpv6/dhcpv6.go @@ -1,8 +1,10 @@ package dhcpv6 import ( + "errors" "fmt" "net" + "strings" ) type DHCPv6 interface { @@ -199,3 +201,55 @@ func EncapsulateRelay(d DHCPv6, mType MessageType, linkAddr, peerAddr net.IP) (D outer.AddOption(&orm) return &outer, nil } + +// IsUsingUEFI function takes a DHCPv6 message and returns true if +// the machine trying to netboot is using UEFI of false if it is not. +func IsUsingUEFI(msg DHCPv6) bool { + // RFC 4578 says: + // As of the writing of this document, the following pre-boot + // architecture types have been requested. + // Type Architecture Name + // ---- ----------------- + // 0 Intel x86PC + // 1 NEC/PC98 + // 2 EFI Itanium + // 3 DEC Alpha + // 4 Arc x86 + // 5 Intel Lean Client + // 6 EFI IA32 + // 7 EFI BC + // 8 EFI Xscale + // 9 EFI x86-64 + if opt := msg.GetOneOption(OptionClientArchType); opt != nil { + optat := opt.(*OptClientArchType) + // TODO investigate if other types are appropriate + if optat.ArchType == EFI_BC || optat.ArchType == EFI_X86_64 { + return true + } + } + if opt := msg.GetOneOption(OptionUserClass); opt != nil { + optuc := opt.(*OptUserClass) + for _, uc := range optuc.UserClasses { + if strings.Contains(string(uc), "EFI") { + return true + } + } + } + return false +} + +// GetTransactionID returns a transactionID of a message or its inner message +// in case of relay +func GetTransactionID(packet DHCPv6) (uint32, error) { + if message, ok := packet.(*DHCPv6Message); ok { + return message.TransactionID(), nil + } + if relay, ok := packet.(*DHCPv6Relay); ok { + message, err := relay.GetInnerMessage() + if err != nil { + return 0, err + } + return GetTransactionID(message) + } + return 0, errors.New("Invalid DHCPv6 packet") +} diff --git a/dhcpv6/dhcpv6_test.go b/dhcpv6/dhcpv6_test.go index 4a44e0e..812d284 100644 --- a/dhcpv6/dhcpv6_test.go +++ b/dhcpv6/dhcpv6_test.go @@ -215,5 +215,52 @@ func TestNewMessageTypeSolicitWithCID(t *testing.T) { require.Equal(t, len(opts), 2) } + +func TestIsUsingUEFIArchTypeTrue(t *testing.T) { + msg := DHCPv6Message{} + opt := OptClientArchType{ArchType: EFI_BC} + msg.AddOption(&opt) + require.True(t, IsUsingUEFI(&msg)) +} + +func TestIsUsingUEFIArchTypeFalse(t *testing.T) { + msg := DHCPv6Message{} + opt := OptClientArchType{ArchType: INTEL_X86PC} + msg.AddOption(&opt) + require.False(t, IsUsingUEFI(&msg)) +} + +func TestIsUsingUEFIUserClassTrue(t *testing.T) { + msg := DHCPv6Message{} + opt := OptUserClass{UserClasses: [][]byte{[]byte("ipxeUEFI")}} + msg.AddOption(&opt) + require.True(t, IsUsingUEFI(&msg)) +} + +func TestIsUsingUEFIUserClassFalse(t *testing.T) { + msg := DHCPv6Message{} + opt := OptUserClass{UserClasses: [][]byte{[]byte("ipxeLegacy")}} + msg.AddOption(&opt) + require.False(t, IsUsingUEFI(&msg)) +} + +func TestGetTransactionIDMessage(t *testing.T) { + message, err := NewMessage() + require.NoError(t, err) + transactionID, err := GetTransactionID(message) + require.NoError(t, err) + require.Equal(t, transactionID, message.(*DHCPv6Message).TransactionID()) +} + +func TestGetTransactionIDRelay(t *testing.T) { + message, err := NewMessage() + require.NoError(t, err) + relay, err := EncapsulateRelay(message, MessageTypeRelayForward, nil, nil) + require.NoError(t, err) + transactionID, err := GetTransactionID(relay) + require.NoError(t, err) + require.Equal(t, transactionID, message.(*DHCPv6Message).TransactionID()) +} + // TODO test NewMessageTypeSolicit // test String and Summary diff --git a/dhcpv6/dhcpv6message.go b/dhcpv6/dhcpv6message.go index e601932..7ee00ad 100644 --- a/dhcpv6/dhcpv6message.go +++ b/dhcpv6/dhcpv6message.go @@ -286,6 +286,33 @@ func (d *DHCPv6Message) UpdateOption(option Option) { d.AddOption(option) } +// IsNetboot returns true if the machine is trying to netboot. It checks if +// "boot file" is one of the requested options, which is useful for +// SOLICIT/REQUEST packet types, it also checks if the "boot file" option is +// included in the packet, which is useful for ADVERTISE/REPLY packet. +func (d *DHCPv6Message) IsNetboot() bool { + if d.IsOptionRequested(OptionBootfileURL) { + return true + } + if optbf := d.GetOneOption(OptionBootfileURL); optbf != nil { + return true + } + return false +} + +// IsOptionRequested takes an OptionCode and returns true if that option is +// within the requested options of the DHCPv6 message. +func (d *DHCPv6Message) IsOptionRequested(requested OptionCode) bool { + for _, optoro := range d.GetOption(OptionORO) { + for _, o := range optoro.(*OptRequestedOption).RequestedOptions() { + if o == requested { + return true + } + } + } + return false +} + func (d *DHCPv6Message) String() string { return fmt.Sprintf("DHCPv6Message(messageType=%v transactionID=0x%06x, %d options)", d.MessageTypeToString(), d.TransactionID(), len(d.options), diff --git a/dhcpv6/dhcpv6message_test.go b/dhcpv6/dhcpv6message_test.go new file mode 100644 index 0000000..5c92a7b --- /dev/null +++ b/dhcpv6/dhcpv6message_test.go @@ -0,0 +1,34 @@ +package dhcpv6 + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestIsNetboot(t *testing.T) { + msg1 := DHCPv6Message{} + require.False(t, msg1.IsNetboot()) + + msg2 := DHCPv6Message{} + optro := OptRequestedOption{} + optro.AddRequestedOption(OptionBootfileURL) + msg2.AddOption(&optro) + require.True(t, msg2.IsNetboot()) + + msg3 := DHCPv6Message{} + optbf := OptBootFileURL{} + msg3.AddOption(&optbf) + require.True(t, msg3.IsNetboot()) +} + +func TestIsOptionRequested(t *testing.T) { + msg1 := DHCPv6Message{} + require.False(t, msg1.IsOptionRequested(OptionDNSRecursiveNameServer)) + + msg2 := DHCPv6Message{} + optro := OptRequestedOption{} + optro.AddRequestedOption(OptionDNSRecursiveNameServer) + msg2.AddOption(&optro) + require.True(t, msg2.IsOptionRequested(OptionDNSRecursiveNameServer)) +} diff --git a/dhcpv6/utils.go b/dhcpv6/utils.go deleted file mode 100644 index 1681661..0000000 --- a/dhcpv6/utils.go +++ /dev/null @@ -1,77 +0,0 @@ -package dhcpv6 - -import ( - "errors" - "strings" -) - -// IsNetboot function takes a DHCPv6 message and returns true if the machine -// is trying to netboot. It checks if "boot file" is one of the requested -// options, which is useful for SOLICIT/REQUEST packet types, it also checks -// if the "boot file" option is included in the packet, which is useful for -// ADVERTISE/REPLY packet. -func IsNetboot(msg DHCPv6) bool { - for _, optoro := range msg.GetOption(OptionORO) { - for _, o := range optoro.(*OptRequestedOption).RequestedOptions() { - if o == OptionBootfileURL { - return true - } - } - } - if optbf := msg.GetOneOption(OptionBootfileURL); optbf != nil { - return true - } - return false -} - -// IsUsingUEFI function takes a DHCPv6 message and returns true if -// the machine trying to netboot is using UEFI of false if it is not. -func IsUsingUEFI(msg DHCPv6) bool { - // RFC 4578 says: - // As of the writing of this document, the following pre-boot - // architecture types have been requested. - // Type Architecture Name - // ---- ----------------- - // 0 Intel x86PC - // 1 NEC/PC98 - // 2 EFI Itanium - // 3 DEC Alpha - // 4 Arc x86 - // 5 Intel Lean Client - // 6 EFI IA32 - // 7 EFI BC - // 8 EFI Xscale - // 9 EFI x86-64 - if opt := msg.GetOneOption(OptionClientArchType); opt != nil { - optat := opt.(*OptClientArchType) - // TODO investigate if other types are appropriate - if optat.ArchType == EFI_BC || optat.ArchType == EFI_X86_64 { - return true - } - } - if opt := msg.GetOneOption(OptionUserClass); opt != nil { - optuc := opt.(*OptUserClass) - for _, uc := range optuc.UserClasses { - if strings.Contains(string(uc), "EFI") { - return true - } - } - } - return false -} - -// GetTransactionID returns a transactionID of a message or its inner message -// in case of relay -func GetTransactionID(packet DHCPv6) (uint32, error) { - if message, ok := packet.(*DHCPv6Message); ok { - return message.TransactionID(), nil - } - if relay, ok := packet.(*DHCPv6Relay); ok { - message, err := relay.GetInnerMessage() - if err != nil { - return 0, err - } - return GetTransactionID(message) - } - return 0, errors.New("Invalid DHCPv6 packet") -} diff --git a/dhcpv6/utils_test.go b/dhcpv6/utils_test.go deleted file mode 100644 index f3b53f0..0000000 --- a/dhcpv6/utils_test.go +++ /dev/null @@ -1,69 +0,0 @@ -package dhcpv6 - -import ( - "testing" - - "github.com/stretchr/testify/require" -) - -func TestIsNetboot(t *testing.T) { - msg1 := DHCPv6Message{} - require.False(t, IsNetboot(&msg1)) - - msg2 := DHCPv6Message{} - optro := OptRequestedOption{} - optro.AddRequestedOption(OptionBootfileURL) - msg2.AddOption(&optro) - require.True(t, IsNetboot(&msg2)) - - msg3 := DHCPv6Message{} - optbf := OptBootFileURL{} - msg3.AddOption(&optbf) - require.True(t, IsNetboot(&msg3)) -} - -func TestIsUsingUEFIArchTypeTrue(t *testing.T) { - msg := DHCPv6Message{} - opt := OptClientArchType{ArchType: EFI_BC} - msg.AddOption(&opt) - require.True(t, IsUsingUEFI(&msg)) -} - -func TestIsUsingUEFIArchTypeFalse(t *testing.T) { - msg := DHCPv6Message{} - opt := OptClientArchType{ArchType: INTEL_X86PC} - msg.AddOption(&opt) - require.False(t, IsUsingUEFI(&msg)) -} - -func TestIsUsingUEFIUserClassTrue(t *testing.T) { - msg := DHCPv6Message{} - opt := OptUserClass{UserClasses: [][]byte{[]byte("ipxeUEFI")}} - msg.AddOption(&opt) - require.True(t, IsUsingUEFI(&msg)) -} - -func TestIsUsingUEFIUserClassFalse(t *testing.T) { - msg := DHCPv6Message{} - opt := OptUserClass{UserClasses: [][]byte{[]byte("ipxeLegacy")}} - msg.AddOption(&opt) - require.False(t, IsUsingUEFI(&msg)) -} - -func TestGetTransactionIDMessage(t *testing.T) { - message, err := NewMessage() - require.NoError(t, err) - transactionID, err := GetTransactionID(message) - require.NoError(t, err) - require.Equal(t, transactionID, message.(*DHCPv6Message).TransactionID()) -} - -func TestGetTransactionIDRelay(t *testing.T) { - message, err := NewMessage() - require.NoError(t, err) - relay, err := EncapsulateRelay(message, MessageTypeRelayForward, nil, nil) - require.NoError(t, err) - transactionID, err := GetTransactionID(relay) - require.NoError(t, err) - require.Equal(t, transactionID, message.(*DHCPv6Message).TransactionID()) -} |