diff options
Diffstat (limited to 'dhcpv4/bsdp')
-rw-r--r-- | dhcpv4/bsdp/boot_image.go | 10 | ||||
-rw-r--r-- | dhcpv4/bsdp/bsdp.go | 6 | ||||
-rw-r--r-- | dhcpv4/bsdp/bsdp_option_boot_image_list.go | 13 | ||||
-rw-r--r-- | dhcpv4/bsdp/bsdp_option_message_type.go | 13 | ||||
-rw-r--r-- | dhcpv4/bsdp/bsdp_option_misc.go | 33 | ||||
-rw-r--r-- | dhcpv4/bsdp/bsdp_option_misc_test.go | 36 | ||||
-rw-r--r-- | dhcpv4/bsdp/bsdp_test.go | 6 | ||||
-rw-r--r-- | dhcpv4/bsdp/option_vendor_specific_information.go | 70 |
8 files changed, 99 insertions, 88 deletions
diff --git a/dhcpv4/bsdp/boot_image.go b/dhcpv4/bsdp/boot_image.go index 58b5167..e494da4 100644 --- a/dhcpv4/bsdp/boot_image.go +++ b/dhcpv4/bsdp/boot_image.go @@ -129,18 +129,8 @@ func OptDefaultBootImageID(b BootImageID) dhcpv4.Option { return dhcpv4.Option{Code: OptionDefaultBootImageID, Value: b} } -// GetDefaultBootImageID returns the default boot image ID contained in o. -func GetDefaultBootImageID(o dhcpv4.Options) *BootImageID { - return getBootImageID(OptionDefaultBootImageID, o) -} - // OptSelectedBootImageID returns a new selected boot image ID option as per // BSDP. func OptSelectedBootImageID(b BootImageID) dhcpv4.Option { return dhcpv4.Option{Code: OptionSelectedBootImageID, Value: b} } - -// GetSelectedBootImageID returns the selected boot image ID contained in o. -func GetSelectedBootImageID(o dhcpv4.Options) *BootImageID { - return getBootImageID(OptionSelectedBootImageID, o) -} diff --git a/dhcpv4/bsdp/bsdp.go b/dhcpv4/bsdp/bsdp.go index 9bcc15d..c9f7c26 100644 --- a/dhcpv4/bsdp/bsdp.go +++ b/dhcpv4/bsdp/bsdp.go @@ -35,7 +35,7 @@ func ParseBootImageListFromAck(ack *dhcpv4.DHCPv4) ([]BootImage, error) { if vendorOpts == nil { return nil, errors.New("ParseBootImageListFromAck: could not find vendor-specific option") } - return GetBootImageList(vendorOpts.Options), nil + return vendorOpts.BootImageList(), nil } func needsReplyPort(replyPort uint16) bool { @@ -50,7 +50,7 @@ func MessageTypeFromPacket(packet *dhcpv4.DHCPv4) MessageType { if vendorOpts == nil { return MessageTypeNone } - return GetMessageType(vendorOpts.Options) + return vendorOpts.MessageType() } // Packet is a BSDP packet wrapper around a DHCPv4 packet in order to print the @@ -156,7 +156,7 @@ func InformSelectForAck(ack *Packet, replyPort uint16, selectedImage BootImage) } // Find server IP address - serverIP := dhcpv4.GetServerIdentifier(ack.Options) + serverIP := ack.ServerIdentifier() if serverIP.To4() == nil { return nil, fmt.Errorf("could not parse server identifier from ACK") } diff --git a/dhcpv4/bsdp/bsdp_option_boot_image_list.go b/dhcpv4/bsdp/bsdp_option_boot_image_list.go index ebbbd2d..1f99196 100644 --- a/dhcpv4/bsdp/bsdp_option_boot_image_list.go +++ b/dhcpv4/bsdp/bsdp_option_boot_image_list.go @@ -51,16 +51,3 @@ func OptBootImageList(b ...BootImage) dhcpv4.Option { Value: BootImageList(b), } } - -// GetBootImageList returns the BSDP boot image list. -func GetBootImageList(o dhcpv4.Options) BootImageList { - v := o.Get(OptionBootImageList) - if v == nil { - return nil - } - var bil BootImageList - if err := bil.FromBytes(v); err != nil { - return nil - } - return bil -} diff --git a/dhcpv4/bsdp/bsdp_option_message_type.go b/dhcpv4/bsdp/bsdp_option_message_type.go index cb0c5cf..02bf243 100644 --- a/dhcpv4/bsdp/bsdp_option_message_type.go +++ b/dhcpv4/bsdp/bsdp_option_message_type.go @@ -55,16 +55,3 @@ func OptMessageType(mt MessageType) dhcpv4.Option { Value: mt, } } - -// GetMessageType returns the BSDP Message Type in o. -func GetMessageType(o dhcpv4.Options) MessageType { - v := o.Get(OptionMessageType) - if v == nil { - return MessageTypeNone - } - var m MessageType - if err := m.FromBytes(v); err != nil { - return MessageTypeNone - } - return m -} diff --git a/dhcpv4/bsdp/bsdp_option_misc.go b/dhcpv4/bsdp/bsdp_option_misc.go index 2d3a7bf..e3193f0 100644 --- a/dhcpv4/bsdp/bsdp_option_misc.go +++ b/dhcpv4/bsdp/bsdp_option_misc.go @@ -18,31 +18,16 @@ func OptReplyPort(port uint16) dhcpv4.Option { return dhcpv4.Option{Code: OptionReplyPort, Value: dhcpv4.Uint16(port)} } -// GetReplyPort returns the BSDP reply port in o, if present. -func GetReplyPort(o dhcpv4.Options) (uint16, error) { - return dhcpv4.GetUint16(OptionReplyPort, o) -} - // OptServerPriority returns a new BSDP server priority option. func OptServerPriority(prio uint16) dhcpv4.Option { return dhcpv4.Option{Code: OptionServerPriority, Value: dhcpv4.Uint16(prio)} } -// GetServerPriority returns the BSDP server priority in o if present. -func GetServerPriority(o dhcpv4.Options) (uint16, error) { - return dhcpv4.GetUint16(OptionServerPriority, o) -} - // OptMachineName returns a BSDP Machine Name option. func OptMachineName(name string) dhcpv4.Option { return dhcpv4.Option{Code: OptionMachineName, Value: dhcpv4.String(name)} } -// GetMachineName finds and parses the BSDP Machine Name option from o. -func GetMachineName(o dhcpv4.Options) string { - return dhcpv4.GetString(OptionMachineName, o) -} - // Version is the BSDP protocol version. Can be one of 1.0 or 1.1. type Version [2]byte @@ -75,24 +60,6 @@ func OptVersion(version Version) dhcpv4.Option { return dhcpv4.Option{Code: OptionVersion, Value: version} } -// GetVersion returns the BSDP version in o if present. -func GetVersion(o dhcpv4.Options) (Version, error) { - v := o.Get(OptionVersion) - if v == nil { - return Version{0, 0}, fmt.Errorf("version not found") - } - var ver Version - if err := ver.FromBytes(v); err != nil { - return Version{0, 0}, err - } - return ver, nil -} - -// GetServerIdentifier returns the BSDP Server Identifier value in o. -func GetServerIdentifier(o dhcpv4.Options) net.IP { - return dhcpv4.GetIP(OptionServerIdentifier, o) -} - // OptServerIdentifier returns a new BSDP Server Identifier option. func OptServerIdentifier(ip net.IP) dhcpv4.Option { return dhcpv4.Option{Code: OptionServerIdentifier, Value: dhcpv4.IP(ip)} diff --git a/dhcpv4/bsdp/bsdp_option_misc_test.go b/dhcpv4/bsdp/bsdp_option_misc_test.go index dfa81b5..44e2813 100644 --- a/dhcpv4/bsdp/bsdp_option_misc_test.go +++ b/dhcpv4/bsdp/bsdp_option_misc_test.go @@ -17,11 +17,12 @@ func TestOptReplyPort(t *testing.T) { func TestGetReplyPort(t *testing.T) { o := VendorOptions{dhcpv4.OptionsFromList(OptReplyPort(1234))} - port, err := GetReplyPort(o.Options) + port, err := o.ReplyPort() require.NoError(t, err) require.Equal(t, uint16(1234), port) - port, err = GetReplyPort(dhcpv4.Options{}) + o = VendorOptions{dhcpv4.Options{}} + port, err = o.ReplyPort() require.Error(t, err, "no reply port present") } @@ -34,11 +35,12 @@ func TestOptServerPriority(t *testing.T) { func TestGetServerPriority(t *testing.T) { o := VendorOptions{dhcpv4.OptionsFromList(OptServerPriority(1234))} - prio, err := GetServerPriority(o.Options) + prio, err := o.ServerPriority() require.NoError(t, err) require.Equal(t, uint16(1234), prio) - prio, err = GetServerPriority(dhcpv4.Options{}) + o = VendorOptions{dhcpv4.Options{}} + prio, err = o.ServerPriority() require.Error(t, err, "no server prio present") } @@ -51,8 +53,10 @@ func TestOptMachineName(t *testing.T) { func TestGetMachineName(t *testing.T) { o := VendorOptions{dhcpv4.OptionsFromList(OptMachineName("foo"))} - require.Equal(t, "foo", GetMachineName(o.Options)) - require.Equal(t, "", GetMachineName(dhcpv4.Options{})) + require.Equal(t, "foo", o.MachineName()) + + o = VendorOptions{dhcpv4.Options{}} + require.Equal(t, "", o.MachineName()) } func TestOptVersion(t *testing.T) { @@ -64,20 +68,24 @@ func TestOptVersion(t *testing.T) { func TestGetVersion(t *testing.T) { o := VendorOptions{dhcpv4.OptionsFromList(OptVersion(Version1_1))} - ver, err := GetVersion(o.Options) + ver, err := o.Version() require.NoError(t, err) require.Equal(t, ver, Version1_1) - ver, err = GetVersion(dhcpv4.Options{}) + o = VendorOptions{dhcpv4.Options{}} + ver, err = o.Version() require.Error(t, err, "no version present") - ver, err = GetVersion(dhcpv4.Options{OptionVersion.Code(): []byte{}}) + o = VendorOptions{dhcpv4.Options{OptionVersion.Code(): []byte{}}} + ver, err = o.Version() require.Error(t, err, "empty version field") - ver, err = GetVersion(dhcpv4.Options{OptionVersion.Code(): []byte{1}}) + o = VendorOptions{dhcpv4.Options{OptionVersion.Code(): []byte{1}}} + ver, err = o.Version() require.Error(t, err, "version option too short") - ver, err = GetVersion(dhcpv4.Options{OptionVersion.Code(): []byte{1, 2, 3}}) + o = VendorOptions{dhcpv4.Options{OptionVersion.Code(): []byte{1, 2, 3}}} + ver, err = o.Version() require.Error(t, err, "version option too long") } @@ -90,6 +98,8 @@ func TestOptServerIdentifier(t *testing.T) { func TestGetServerIdentifier(t *testing.T) { o := VendorOptions{dhcpv4.OptionsFromList(OptServerIdentifier(net.IP{1, 1, 1, 1}))} - require.Equal(t, net.IP{1, 1, 1, 1}, GetServerIdentifier(o.Options)) - require.Equal(t, net.IP(nil), GetServerIdentifier(dhcpv4.Options{})) + require.Equal(t, net.IP{1, 1, 1, 1}, o.ServerIdentifier()) + + o = VendorOptions{dhcpv4.Options{}} + require.Nil(t, o.ServerIdentifier()) } diff --git a/dhcpv4/bsdp/bsdp_test.go b/dhcpv4/bsdp/bsdp_test.go index e0378c2..05cd85c 100644 --- a/dhcpv4/bsdp/bsdp_test.go +++ b/dhcpv4/bsdp/bsdp_test.go @@ -75,7 +75,7 @@ func TestNewInformList_NoReplyPort(t *testing.T) { require.True(t, vendorOpts.Has(OptionMessageType)) require.True(t, vendorOpts.Has(OptionVersion)) - mt := GetMessageType(vendorOpts.Options) + mt := vendorOpts.MessageType() require.Equal(t, MessageTypeList, mt) } @@ -96,7 +96,7 @@ func TestNewInformList_ReplyPort(t *testing.T) { vendorOpts := GetVendorOptions(m.Options) require.True(t, vendorOpts.Options.Has(OptionReplyPort)) - port, err := GetReplyPort(vendorOpts.Options) + port, err := vendorOpts.ReplyPort() require.NoError(t, err) require.Equal(t, replyPort, port) } @@ -139,7 +139,7 @@ func TestInformSelectForAck_Broadcast(t *testing.T) { require.True(t, m.Options.Has(dhcpv4.OptionClassIdentifier)) require.True(t, m.Options.Has(dhcpv4.OptionParameterRequestList)) require.True(t, m.Options.Has(dhcpv4.OptionDHCPMessageType)) - mt := dhcpv4.GetMessageType(m.Options) + mt := m.MessageType() require.Equal(t, dhcpv4.MessageTypeInform, mt) // Validate vendor opts. diff --git a/dhcpv4/bsdp/option_vendor_specific_information.go b/dhcpv4/bsdp/option_vendor_specific_information.go index 4e107e1..abdd9a5 100644 --- a/dhcpv4/bsdp/option_vendor_specific_information.go +++ b/dhcpv4/bsdp/option_vendor_specific_information.go @@ -2,6 +2,7 @@ package bsdp import ( "fmt" + "net" "github.com/insomniacslk/dhcp/dhcpv4" ) @@ -23,6 +24,75 @@ func (v *VendorOptions) FromBytes(data []byte) error { return v.Options.FromBytes(data) } +// DefaultBootImageID returns the default boot image ID in v. +func (v VendorOptions) DefaultBootImageID() *BootImageID { + return getBootImageID(OptionDefaultBootImageID, v.Options) +} + +// SelectedBootImageID returns the selected boot image ID in v. +func (v VendorOptions) SelectedBootImageID() *BootImageID { + return getBootImageID(OptionSelectedBootImageID, v.Options) +} + +// BootImageList returns the BSDP boot image list in v. +func (v VendorOptions) BootImageList() BootImageList { + val := v.Options.Get(OptionBootImageList) + if val == nil { + return nil + } + var bil BootImageList + if err := bil.FromBytes(val); err != nil { + return nil + } + return bil +} + +// MessageType returns the BSDP Message Type in v. +func (v VendorOptions) MessageType() MessageType { + val := v.Options.Get(OptionMessageType) + if val == nil { + return MessageTypeNone + } + var m MessageType + if err := m.FromBytes(val); err != nil { + return MessageTypeNone + } + return m +} + +// GetVersion returns the BSDP version in v if present. +func (v VendorOptions) Version() (Version, error) { + val := v.Options.Get(OptionVersion) + if val == nil { + return Version{0, 0}, fmt.Errorf("version not found") + } + var ver Version + if err := ver.FromBytes(val); err != nil { + return Version{0, 0}, err + } + return ver, nil +} + +// GetServerIdentifier returns the BSDP Server Identifier value in v if present. +func (v VendorOptions) ServerIdentifier() net.IP { + return dhcpv4.GetIP(OptionServerIdentifier, v.Options) +} + +// GetReplyPort returns the BSDP reply port in v if present. +func (v VendorOptions) ReplyPort() (uint16, error) { + return dhcpv4.GetUint16(OptionReplyPort, v.Options) +} + +// GetServerPriority returns the BSDP server priority in v if present. +func (v VendorOptions) ServerPriority() (uint16, error) { + return dhcpv4.GetUint16(OptionServerPriority, v.Options) +} + +// GetMachineName finds and parses the BSDP Machine Name option from v. +func (v VendorOptions) MachineName() string { + return dhcpv4.GetString(OptionMachineName, v.Options) +} + // OptVendorOptions returns the BSDP Vendor Specific Info in o. func OptVendorOptions(o ...dhcpv4.Option) dhcpv4.Option { return dhcpv4.Option{ |