diff options
author | Christopher Koch <c@chrisko.ch> | 2019-01-20 21:13:30 +0000 |
---|---|---|
committer | insomniac <insomniacslk@users.noreply.github.com> | 2019-01-24 08:05:49 +0000 |
commit | a4a666c6477431358a1d6ed7b556398ed690ab5c (patch) | |
tree | d6b4c0e7a753db223d7d8b09569c4d028cff0f24 /dhcpv4 | |
parent | c90ab10024ada840e24bb028a3405961e8e4c26a (diff) |
dhcpv4: getters instead of getters
From:
r := GetRouter(d.Options)
To:
r := d.Router()
Diffstat (limited to 'dhcpv4')
36 files changed, 449 insertions, 434 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{ diff --git a/dhcpv4/dhcpv4.go b/dhcpv4/dhcpv4.go index d94c58a..5f6943b 100644 --- a/dhcpv4/dhcpv4.go +++ b/dhcpv4/dhcpv4.go @@ -9,7 +9,7 @@ // p.UpdateOption(dhcpv4.OptServerIdentifier(net.IP{192, 110, 110, 110})) // // // Retrieve the DHCP Message Type option. -// m := dhcpv4.GetMessageType(p.Options) +// m := p.MessageType() // // bytesOnTheWire := p.ToBytes() // longSummary := p.Summary() @@ -21,8 +21,10 @@ import ( "fmt" "net" "strings" + "time" "github.com/insomniacslk/dhcp/iana" + "github.com/insomniacslk/dhcp/rfc1035label" "github.com/u-root/u-root/pkg/uio" ) @@ -218,7 +220,7 @@ func NewInform(hwaddr net.HardwareAddr, localIP net.IP, modifiers ...Modifier) ( // NewRequestFromOffer builds a DHCPv4 request from an offer. func NewRequestFromOffer(offer *DHCPv4, modifiers ...Modifier) (*DHCPv4, error) { // find server IP address - serverIP := GetServerIdentifier(offer.Options) + serverIP := offer.ServerIdentifier() if serverIP == nil { return nil, errors.New("Missing Server IP Address in DHCP Offer") } @@ -350,12 +352,6 @@ func (d *DHCPv4) UpdateOption(opt Option) { d.Options.Update(opt) } -// MessageType returns the message type, trying to extract it from the -// OptMessageType option. It returns nil if the message type cannot be extracted -func (d *DHCPv4) MessageType() MessageType { - return GetMessageType(d.Options) -} - // String implements fmt.Stringer. func (d *DHCPv4) String() string { return fmt.Sprintf("DHCPv4(opcode=%s xid=%s hwtype=%s hwaddr=%s)", @@ -408,7 +404,7 @@ func (d *DHCPv4) Summary() string { // IsOptionRequested returns true if that option is within the requested // options of the DHCPv4 message. func (d *DHCPv4) IsOptionRequested(requested OptionCode) bool { - for _, o := range GetParameterRequestList(d.Options) { + for _, o := range d.ParameterRequestList() { if o == requested { return true } @@ -470,3 +466,224 @@ func (d *DHCPv4) ToBytes() []byte { return buf.Data() } + +// GetBroadcastAddress returns the DHCPv4 Broadcast Address value in d. +// +// The broadcast address option is described in RFC 2132, Section 5.3. +func (d *DHCPv4) BroadcastAddress() net.IP { + return GetIP(OptionBroadcastAddress, d.Options) +} + +// RequestedIPAddress returns the DHCPv4 Requested IP Address value in d. +// +// The requested IP address option is described by RFC 2132, Section 9.1. +func (d *DHCPv4) RequestedIPAddress() net.IP { + return GetIP(OptionRequestedIPAddress, d.Options) +} + +// ServerIdentifier returns the DHCPv4 Server Identifier value in d. +// +// The server identifier option is described by RFC 2132, Section 9.7. +func (d *DHCPv4) ServerIdentifier() net.IP { + return GetIP(OptionServerIdentifier, d.Options) +} + +// Router parses the DHCPv4 Router option if present. +// +// The Router option is described by RFC 2132, Section 3.5. +func (d *DHCPv4) Router() []net.IP { + return GetIPs(OptionRouter, d.Options) +} + +// NTPServers parses the DHCPv4 NTP Servers option if present. +// +// The NTP servers option is described by RFC 2132, Section 8.3. +func (d *DHCPv4) NTPServers() []net.IP { + return GetIPs(OptionNTPServers, d.Options) +} + +// DNS parses the DHCPv4 Domain Name Server option if present. +// +// The DNS server option is described by RFC 2132, Section 3.8. +func (d *DHCPv4) DNS() []net.IP { + return GetIPs(OptionDomainNameServer, d.Options) +} + +// DomainName parses the DHCPv4 Domain Name option if present. +// +// The Domain Name option is described by RFC 2132, Section 3.17. +func (d *DHCPv4) DomainName() string { + return GetString(OptionDomainName, d.Options) +} + +// HostName parses the DHCPv4 Host Name option if present. +// +// The Host Name option is described by RFC 2132, Section 3.14. +func (d *DHCPv4) HostName() string { + return GetString(OptionHostName, d.Options) +} + +// RootPath parses the DHCPv4 Root Path option if present. +// +// The Root Path option is described by RFC 2132, Section 3.19. +func (d *DHCPv4) RootPath() string { + return GetString(OptionRootPath, d.Options) +} + +// BootFileNameOption parses the DHCPv4 Bootfile Name option if present. +// +// The Bootfile Name option is described by RFC 2132, Section 9.5. +func (d *DHCPv4) BootFileNameOption() string { + return GetString(OptionBootfileName, d.Options) +} + +// TFTPServerName parses the DHCPv4 TFTP Server Name option if present. +// +// The TFTP Server Name option is described by RFC 2132, Section 9.4. +func (d *DHCPv4) TFTPServerName() string { + return GetString(OptionTFTPServerName, d.Options) +} + +// ClassIdentifier parses the DHCPv4 Class Identifier option if present. +// +// The Vendor Class Identifier option is described by RFC 2132, Section 9.13. +func (d *DHCPv4) ClassIdentifier() string { + return GetString(OptionClassIdentifier, d.Options) +} + +// ClientArch returns the Client System Architecture Type option. +func (d *DHCPv4) ClientArch() []iana.Arch { + v := d.Options.Get(OptionClientSystemArchitectureType) + if v == nil { + return nil + } + var archs iana.Archs + if err := archs.FromBytes(v); err != nil { + return nil + } + return archs +} + +// DomainSearch returns the domain search list if present. +// +// The domain search option is described by RFC 3397, Section 2. +func (d *DHCPv4) DomainSearch() *rfc1035label.Labels { + v := d.Options.Get(OptionDNSDomainSearchList) + if v == nil { + return nil + } + labels, err := rfc1035label.FromBytes(v) + if err != nil { + return nil + } + return labels +} + +// IPAddressLeaseTime returns the IP address lease time or the given +// default duration if not present. +// +// The IP address lease time option is described by RFC 2132, Section 9.2. +func (d *DHCPv4) IPAddressLeaseTime(def time.Duration) time.Duration { + v := d.Options.Get(OptionIPAddressLeaseTime) + if v == nil { + return def + } + var dur Duration + if err := dur.FromBytes(v); err != nil { + return def + } + return time.Duration(dur) +} + +// MaxMessageSize returns the DHCP Maximum Message Size if present. +// +// The Maximum DHCP Message Size option is described by RFC 2132, Section 9.10. +func (d *DHCPv4) MaxMessageSize() (uint16, error) { + return GetUint16(OptionMaximumDHCPMessageSize, d.Options) +} + +// MessageType returns the DHCPv4 Message Type option. +func (d *DHCPv4) MessageType() MessageType { + v := d.Options.Get(OptionDHCPMessageType) + if v == nil { + return MessageTypeNone + } + var m MessageType + if err := m.FromBytes(v); err != nil { + return MessageTypeNone + } + return m +} + +// ParameterRequestList returns the DHCPv4 Parameter Request List. +// +// The parameter request list option is described by RFC 2132, Section 9.8. +func (d *DHCPv4) ParameterRequestList() OptionCodeList { + v := d.Options.Get(OptionParameterRequestList) + if v == nil { + return nil + } + var codes OptionCodeList + if err := codes.FromBytes(v); err != nil { + return nil + } + return codes +} + +// RelayAgentInfo returns options embedded by the relay agent. +// +// The relay agent info option is described by RFC 3046. +func (d *DHCPv4) RelayAgentInfo() *RelayOptions { + v := d.Options.Get(OptionRelayAgentInformation) + if v == nil { + return nil + } + var relayOptions RelayOptions + if err := relayOptions.FromBytes(v); err != nil { + return nil + } + return &relayOptions +} + +// SubnetMask returns a subnet mask option contained if present. +// +// The subnet mask option is described by RFC 2132, Section 3.3. +func (d *DHCPv4) SubnetMask() net.IPMask { + v := d.Options.Get(OptionSubnetMask) + if v == nil { + return nil + } + var im IPMask + if err := im.FromBytes(v); err != nil { + return nil + } + return net.IPMask(im) +} + +// UserClass returns the user class if present. +// +// The user class information option is defined by RFC 3004. +func (d *DHCPv4) UserClass() *UserClass { + v := d.Options.Get(OptionUserClassInformation) + if v == nil { + return nil + } + var uc UserClass + if err := uc.FromBytes(v); err != nil { + return nil + } + return &uc +} + +// VIVC returns the vendor-identifying vendor class option if present. +func (d *DHCPv4) VIVC() VIVCIdentifiers { + v := d.Options.Get(OptionVendorIdentifyingVendorClass) + if v == nil { + return nil + } + var ids VIVCIdentifiers + if err := ids.FromBytes(v); err != nil { + return nil + } + return ids +} diff --git a/dhcpv4/modifiers.go b/dhcpv4/modifiers.go index 431fdfd..41ad4f4 100644 --- a/dhcpv4/modifiers.go +++ b/dhcpv4/modifiers.go @@ -112,14 +112,9 @@ func WithMessageType(m MessageType) Modifier { // WithRequestedOptions adds requested options to the packet. func WithRequestedOptions(optionCodes ...OptionCode) Modifier { return func(d *DHCPv4) { - params := d.GetOneOption(OptionParameterRequestList) - if params == nil { - d.UpdateOption(OptParameterRequestList(optionCodes...)) - } else { - cl := OptionCodeList(GetParameterRequestList(d.Options)) - cl.Add(optionCodes...) - d.UpdateOption(OptParameterRequestList(cl...)) - } + cl := d.ParameterRequestList() + cl.Add(optionCodes...) + d.UpdateOption(OptParameterRequestList(cl...)) } } @@ -149,3 +144,7 @@ func WithDomainSearchList(searchList ...string) Modifier { Labels: searchList, })) } + +func WithGeneric(code OptionCode, value []byte) Modifier { + return WithOption(OptGeneric(code, value)) +} diff --git a/dhcpv4/modifiers_test.go b/dhcpv4/modifiers_test.go index 6233a7d..9b6c8ca 100644 --- a/dhcpv4/modifiers_test.go +++ b/dhcpv4/modifiers_test.go @@ -39,7 +39,7 @@ func TestWithOptionModifier(t *testing.T) { d, err := New(WithOption(OptDomainName("slackware.it"))) require.NoError(t, err) - dnOpt := GetDomainName(d.Options) + dnOpt := d.DomainName() require.NotNil(t, dnOpt) require.Equal(t, "slackware.it", dnOpt) } @@ -68,28 +68,28 @@ func TestWithNetboot(t *testing.T) { d, err := New(WithNetboot) require.NoError(t, err) - require.Equal(t, "TFTP Server Name, Bootfile Name", GetParameterRequestList(d.Options).String()) + require.Equal(t, "TFTP Server Name, Bootfile Name", d.ParameterRequestList().String()) } func TestWithNetbootExistingTFTP(t *testing.T) { d, _ := New() d.UpdateOption(OptParameterRequestList(OptionTFTPServerName)) WithNetboot(d) - require.Equal(t, "TFTP Server Name, Bootfile Name", GetParameterRequestList(d.Options).String()) + require.Equal(t, "TFTP Server Name, Bootfile Name", d.ParameterRequestList().String()) } func TestWithNetbootExistingBootfileName(t *testing.T) { d, _ := New() d.UpdateOption(OptParameterRequestList(OptionBootfileName)) WithNetboot(d) - require.Equal(t, "TFTP Server Name, Bootfile Name", GetParameterRequestList(d.Options).String()) + require.Equal(t, "TFTP Server Name, Bootfile Name", d.ParameterRequestList().String()) } func TestWithNetbootExistingBoth(t *testing.T) { d, _ := New() d.UpdateOption(OptParameterRequestList(OptionBootfileName, OptionTFTPServerName)) WithNetboot(d) - require.Equal(t, "TFTP Server Name, Bootfile Name", GetParameterRequestList(d.Options).String()) + require.Equal(t, "TFTP Server Name, Bootfile Name", d.ParameterRequestList().String()) } func TestWithRequestedOptions(t *testing.T) { @@ -98,13 +98,13 @@ func TestWithRequestedOptions(t *testing.T) { require.NoError(t, err) require.NotNil(t, d) - opts := GetParameterRequestList(d.Options) + opts := d.ParameterRequestList() require.NotNil(t, opts) require.ElementsMatch(t, opts, []OptionCode{OptionFQDN}) // Check if already set options are preserved WithRequestedOptions(OptionHostName)(d) require.NotNil(t, d) - opts = GetParameterRequestList(d.Options) + opts = d.ParameterRequestList() require.NotNil(t, opts) require.ElementsMatch(t, opts, []OptionCode{OptionFQDN, OptionHostName}) } @@ -123,8 +123,7 @@ func TestWithNetmask(t *testing.T) { d, err := New(WithNetmask(net.IPv4Mask(255, 255, 255, 0))) require.NoError(t, err) - osm := GetSubnetMask(d.Options) - require.Equal(t, net.IPv4Mask(255, 255, 255, 0), osm) + require.Equal(t, net.IPv4Mask(255, 255, 255, 0), d.SubnetMask()) } func TestWithLeaseTime(t *testing.T) { @@ -132,15 +131,14 @@ func TestWithLeaseTime(t *testing.T) { require.NoError(t, err) require.True(t, d.Options.Has(OptionIPAddressLeaseTime)) - olt := GetIPAddressLeaseTime(d.Options, 10*time.Second) - require.Equal(t, 3600*time.Second, olt) + require.Equal(t, 3600*time.Second, d.IPAddressLeaseTime(10*time.Second)) } func TestWithDNS(t *testing.T) { d, err := New(WithDNS(net.ParseIP("10.0.0.1"), net.ParseIP("10.0.0.2"))) require.NoError(t, err) - dns := GetDNS(d.Options) + dns := d.DNS() require.Equal(t, net.ParseIP("10.0.0.1").To4(), dns[0]) require.Equal(t, net.ParseIP("10.0.0.2").To4(), dns[1]) } @@ -149,7 +147,7 @@ func TestWithDomainSearchList(t *testing.T) { d, err := New(WithDomainSearchList("slackware.it", "dhcp.slackware.it")) require.NoError(t, err) - osl := GetDomainSearch(d.Options) + osl := d.DomainSearch() require.NotNil(t, osl) require.Equal(t, 2, len(osl.Labels)) require.Equal(t, "slackware.it", osl.Labels[0]) @@ -161,6 +159,6 @@ func TestWithRouter(t *testing.T) { d, err := New(WithRouter(rtr)) require.NoError(t, err) - ortr := GetRouter(d.Options) + ortr := d.Router() require.Equal(t, rtr, ortr[0]) } diff --git a/dhcpv4/option_archtype.go b/dhcpv4/option_archtype.go index 00a4417..c0a901d 100644 --- a/dhcpv4/option_archtype.go +++ b/dhcpv4/option_archtype.go @@ -8,16 +8,3 @@ import ( func OptClientArch(archs ...iana.Arch) Option { return Option{Code: OptionClientSystemArchitectureType, Value: iana.Archs(archs)} } - -// GetClientArch returns the Client System Architecture Type option. -func GetClientArch(o Options) []iana.Arch { - v := o.Get(OptionClientSystemArchitectureType) - if v == nil { - return nil - } - var archs iana.Archs - if err := archs.FromBytes(v); err != nil { - return nil - } - return archs -} diff --git a/dhcpv4/option_archtype_test.go b/dhcpv4/option_archtype_test.go index fcf526b..e5e501a 100644 --- a/dhcpv4/option_archtype_test.go +++ b/dhcpv4/option_archtype_test.go @@ -8,33 +8,34 @@ import ( ) func TestParseOptClientArchType(t *testing.T) { - o := Options{OptionClientSystemArchitectureType.Code(): []byte{ + m, _ := New(WithGeneric(OptionClientSystemArchitectureType, []byte{ 0, 6, // EFI_IA32 - }} - archs := GetClientArch(o) + })) + archs := m.ClientArch() require.NotNil(t, archs) require.Equal(t, archs[0], iana.EFI_IA32) } func TestParseOptClientArchTypeMultiple(t *testing.T) { - o := Options{OptionClientSystemArchitectureType.Code(): []byte{ + m, _ := New(WithGeneric(OptionClientSystemArchitectureType, []byte{ 0, 6, // EFI_IA32 0, 2, // EFI_ITANIUM - }} - archs := GetClientArch(o) + })) + archs := m.ClientArch() require.NotNil(t, archs) require.Equal(t, archs[0], iana.EFI_IA32) require.Equal(t, archs[1], iana.EFI_ITANIUM) } func TestParseOptClientArchTypeInvalid(t *testing.T) { - o := Options{OptionClientSystemArchitectureType.Code(): []byte{42}} - archs := GetClientArch(o) + m, _ := New(WithGeneric(OptionClientSystemArchitectureType, []byte{42})) + archs := m.ClientArch() require.Nil(t, archs) } func TestGetClientArchEmpty(t *testing.T) { - require.Nil(t, GetClientArch(Options{})) + m, _ := New() + require.Nil(t, m.ClientArch()) } func TestOptClientArchTypeParseAndToBytesMultiple(t *testing.T) { diff --git a/dhcpv4/option_domain_search.go b/dhcpv4/option_domain_search.go index 6d2f7b2..b57754f 100644 --- a/dhcpv4/option_domain_search.go +++ b/dhcpv4/option_domain_search.go @@ -10,18 +10,3 @@ import ( func OptDomainSearch(labels *rfc1035label.Labels) Option { return Option{Code: OptionDNSDomainSearchList, Value: labels} } - -// GetDomainSearch returns the domain search list in o, if present. -// -// The domain search option is described by RFC 3397, Section 2. -func GetDomainSearch(o Options) *rfc1035label.Labels { - v := o.Get(OptionDNSDomainSearchList) - if v == nil { - return nil - } - labels, err := rfc1035label.FromBytes(v) - if err != nil { - return nil - } - return labels -} diff --git a/dhcpv4/option_domain_search_test.go b/dhcpv4/option_domain_search_test.go index 9a508f2..508797c 100644 --- a/dhcpv4/option_domain_search_test.go +++ b/dhcpv4/option_domain_search_test.go @@ -12,10 +12,8 @@ func TestGetDomainSearch(t *testing.T) { 7, 'e', 'x', 'a', 'm', 'p', 'l', 'e', 3, 'c', 'o', 'm', 0, 6, 's', 'u', 'b', 'n', 'e', 't', 7, 'e', 'x', 'a', 'm', 'p', 'l', 'e', 3, 'o', 'r', 'g', 0, } - o := Options{ - OptionDNSDomainSearchList.Code(): data, - } - labels := GetDomainSearch(o) + m, _ := New(WithGeneric(OptionDNSDomainSearchList, data)) + labels := m.DomainSearch() require.NotNil(t, labels) require.Equal(t, 2, len(labels.Labels)) require.Equal(t, data, labels.ToBytes()) @@ -33,7 +31,6 @@ func TestOptDomainSearchToBytes(t *testing.T) { "example.com", "subnet.example.org", }, - }, - ) + }) require.Equal(t, opt.Value.ToBytes(), expected) } diff --git a/dhcpv4/option_ip.go b/dhcpv4/option_ip.go index 6a4206c..a32d215 100644 --- a/dhcpv4/option_ip.go +++ b/dhcpv4/option_ip.go @@ -40,13 +40,6 @@ func GetIP(code OptionCode, o Options) net.IP { return net.IP(ip) } -// GetBroadcastAddress returns the DHCPv4 Broadcast Address value in o. -// -// The broadcast address option is described in RFC 2132, Section 5.3. -func GetBroadcastAddress(o Options) net.IP { - return GetIP(OptionBroadcastAddress, o) -} - // OptBroadcastAddress returns a new DHCPv4 Broadcast Address option. // // The broadcast address option is described in RFC 2132, Section 5.3. @@ -54,13 +47,6 @@ func OptBroadcastAddress(ip net.IP) Option { return Option{Code: OptionBroadcastAddress, Value: IP(ip)} } -// GetRequestedIPAddress returns the DHCPv4 Requested IP Address value in o. -// -// The requested IP address option is described by RFC 2132, Section 9.1. -func GetRequestedIPAddress(o Options) net.IP { - return GetIP(OptionRequestedIPAddress, o) -} - // OptRequestedIPAddress returns a new DHCPv4 Requested IP Address option. // // The requested IP address option is described by RFC 2132, Section 9.1. @@ -68,13 +54,6 @@ func OptRequestedIPAddress(ip net.IP) Option { return Option{Code: OptionRequestedIPAddress, Value: IP(ip)} } -// GetServerIdentifier returns the DHCPv4 Server Identifier value in o. -// -// The server identifier option is described by RFC 2132, Section 9.7. -func GetServerIdentifier(o Options) net.IP { - return GetIP(OptionServerIdentifier, o) -} - // OptServerIdentifier returns a new DHCPv4 Server Identifier option. // // The server identifier option is described by RFC 2132, Section 9.7. diff --git a/dhcpv4/option_ip_address_lease_time.go b/dhcpv4/option_ip_address_lease_time.go index 6e09233..20ee2b8 100644 --- a/dhcpv4/option_ip_address_lease_time.go +++ b/dhcpv4/option_ip_address_lease_time.go @@ -36,19 +36,3 @@ func (d Duration) String() string { func OptIPAddressLeaseTime(d time.Duration) Option { return Option{Code: OptionIPAddressLeaseTime, Value: Duration(d)} } - -// GetIPAddressLeaseTime returns the IP address lease time in o, or the given -// default duration if not present. -// -// The IP address lease time option is described by RFC 2132, Section 9.2. -func GetIPAddressLeaseTime(o Options, def time.Duration) time.Duration { - v := o.Get(OptionIPAddressLeaseTime) - if v == nil { - return def - } - var d Duration - if err := d.FromBytes(v); err != nil { - return def - } - return time.Duration(d) -} diff --git a/dhcpv4/option_ip_address_lease_time_test.go b/dhcpv4/option_ip_address_lease_time_test.go index 70c4047..edd709f 100644 --- a/dhcpv4/option_ip_address_lease_time_test.go +++ b/dhcpv4/option_ip_address_lease_time_test.go @@ -15,20 +15,21 @@ func TestOptIPAddressLeaseTime(t *testing.T) { } func TestGetIPAddressLeaseTime(t *testing.T) { - o := Options{OptionIPAddressLeaseTime.Code(): []byte{0, 0, 168, 192}} - leaseTime := GetIPAddressLeaseTime(o, 0) + m, _ := New(WithGeneric(OptionIPAddressLeaseTime, []byte{0, 0, 168, 192})) + leaseTime := m.IPAddressLeaseTime(0) require.Equal(t, 43200*time.Second, leaseTime) // Too short. - o = Options{OptionIPAddressLeaseTime.Code(): []byte{168, 192}} - leaseTime = GetIPAddressLeaseTime(o, 0) + m, _ = New(WithGeneric(OptionIPAddressLeaseTime, []byte{168, 192})) + leaseTime = m.IPAddressLeaseTime(0) require.Equal(t, time.Duration(0), leaseTime) // Too long. - o = Options{OptionIPAddressLeaseTime.Code(): []byte{1, 1, 1, 1, 1}} - leaseTime = GetIPAddressLeaseTime(o, 0) + m, _ = New(WithGeneric(OptionIPAddressLeaseTime, []byte{1, 1, 1, 1, 1})) + leaseTime = m.IPAddressLeaseTime(0) require.Equal(t, time.Duration(0), leaseTime) // Empty. - require.Equal(t, time.Duration(10), GetIPAddressLeaseTime(Options{}, 10)) + m, _ = New() + require.Equal(t, time.Duration(10), m.IPAddressLeaseTime(10)) } diff --git a/dhcpv4/option_ips.go b/dhcpv4/option_ips.go index 693d62d..2a43dcf 100644 --- a/dhcpv4/option_ips.go +++ b/dhcpv4/option_ips.go @@ -62,13 +62,6 @@ func GetIPs(code OptionCode, o Options) []net.IP { return []net.IP(ips) } -// GetRouter parses the DHCPv4 Router option if present. -// -// The Router option is described by RFC 2132, Section 3.5. -func GetRouter(o Options) []net.IP { - return GetIPs(OptionRouter, o) -} - // OptRouter returns a new DHCPv4 Router option. // // The Router option is described by RFC 2132, Section 3.5. @@ -84,13 +77,6 @@ func WithRouter(routers ...net.IP) Modifier { return WithOption(OptRouter(routers...)) } -// GetNTPServers parses the DHCPv4 NTP Servers option if present. -// -// The NTP servers option is described by RFC 2132, Section 8.3. -func GetNTPServers(o Options) []net.IP { - return GetIPs(OptionNTPServers, o) -} - // OptNTPServers returns a new DHCPv4 NTP Server option. // // The NTP servers option is described by RFC 2132, Section 8.3. @@ -101,13 +87,6 @@ func OptNTPServers(ntpServers ...net.IP) Option { } } -// GetDNS parses the DHCPv4 Domain Name Server option if present. -// -// The DNS server option is described by RFC 2132, Section 3.8. -func GetDNS(o Options) []net.IP { - return GetIPs(OptionDomainNameServer, o) -} - // OptDNS returns a new DHCPv4 Domain Name Server option. // // The DNS server option is described by RFC 2132, Section 3.8. diff --git a/dhcpv4/option_ips_test.go b/dhcpv4/option_ips_test.go index 1e1b772..05b2939 100644 --- a/dhcpv4/option_ips_test.go +++ b/dhcpv4/option_ips_test.go @@ -43,9 +43,11 @@ func TestGetDomainNameServer(t *testing.T) { net.IP{192, 168, 0, 1}, net.IP{192, 168, 0, 10}, } - o := OptionsFromList(OptDNS(ips...)) - require.Equal(t, ips, GetDNS(o)) - require.Nil(t, GetDNS(Options{})) + m, _ := New(WithOption(OptDNS(ips...))) + require.Equal(t, ips, m.DNS()) + + m, _ = New() + require.Nil(t, m.DNS()) } func TestOptNTPServers(t *testing.T) { @@ -60,9 +62,11 @@ func TestGetNTPServers(t *testing.T) { net.IP{192, 168, 0, 1}, net.IP{192, 168, 0, 10}, } - o := OptionsFromList(OptNTPServers(ips...)) - require.Equal(t, ips, GetNTPServers(o)) - require.Nil(t, GetNTPServers(Options{})) + m, _ := New(WithOption(OptNTPServers(ips...))) + require.Equal(t, ips, m.NTPServers()) + + m, _ = New() + require.Nil(t, m.NTPServers()) } func TestOptRouter(t *testing.T) { @@ -77,7 +81,9 @@ func TestGetRouter(t *testing.T) { net.IP{192, 168, 0, 1}, net.IP{192, 168, 0, 10}, } - o := OptionsFromList(OptRouter(ips...)) - require.Equal(t, ips, GetRouter(o)) - require.Nil(t, GetRouter(Options{})) + m, _ := New(WithOption(OptRouter(ips...))) + require.Equal(t, ips, m.Router()) + + m, _ = New() + require.Nil(t, m.Router()) } diff --git a/dhcpv4/option_maximum_dhcp_message_size.go b/dhcpv4/option_maximum_dhcp_message_size.go index 900eea5..1f31bc2 100644 --- a/dhcpv4/option_maximum_dhcp_message_size.go +++ b/dhcpv4/option_maximum_dhcp_message_size.go @@ -48,10 +48,3 @@ func GetUint16(code OptionCode, o Options) (uint16, error) { func OptMaxMessageSize(size uint16) Option { return Option{Code: OptionMaximumDHCPMessageSize, Value: Uint16(size)} } - -// GetMaxMessageSize returns the DHCP Maximum Message Size in o if present. -// -// The Maximum DHCP Message Size option is described by RFC 2132, Section 9.10. -func GetMaxMessageSize(o Options) (uint16, error) { - return GetUint16(OptionMaximumDHCPMessageSize, o) -} diff --git a/dhcpv4/option_maximum_dhcp_message_size_test.go b/dhcpv4/option_maximum_dhcp_message_size_test.go index 147f280..2f85560 100644 --- a/dhcpv4/option_maximum_dhcp_message_size_test.go +++ b/dhcpv4/option_maximum_dhcp_message_size_test.go @@ -14,18 +14,18 @@ func TestOptMaximumDHCPMessageSize(t *testing.T) { } func TestGetMaximumDHCPMessageSize(t *testing.T) { - options := Options{OptionMaximumDHCPMessageSize.Code(): []byte{5, 220}} - o, err := GetMaxMessageSize(options) + m, _ := New(WithGeneric(OptionMaximumDHCPMessageSize, []byte{5, 220})) + o, err := m.MaxMessageSize() require.NoError(t, err) require.Equal(t, uint16(1500), o) // Short byte stream - options = Options{OptionMaximumDHCPMessageSize.Code(): []byte{2}} - _, err = GetMaxMessageSize(options) + m, _ = New(WithGeneric(OptionMaximumDHCPMessageSize, []byte{2})) + _, err = m.MaxMessageSize() require.Error(t, err, "should get error from short byte stream") // Bad length - options = Options{OptionMaximumDHCPMessageSize.Code(): []byte{1, 1, 1}} - _, err = GetMaxMessageSize(options) + m, _ = New(WithGeneric(OptionMaximumDHCPMessageSize, []byte{2, 2, 2})) + _, err = m.MaxMessageSize() require.Error(t, err, "should get error from bad length") } diff --git a/dhcpv4/option_message_type.go b/dhcpv4/option_message_type.go index 141e3b7..1f4c14f 100644 --- a/dhcpv4/option_message_type.go +++ b/dhcpv4/option_message_type.go @@ -4,16 +4,3 @@ package dhcpv4 func OptMessageType(m MessageType) Option { return Option{Code: OptionDHCPMessageType, Value: m} } - -// GetMessageType returns the DHCPv4 Message Type option in o. -func GetMessageType(o Options) MessageType { - v := o.Get(OptionDHCPMessageType) - if v == nil { - return MessageTypeNone - } - var m MessageType - if err := m.FromBytes(v); err != nil { - return MessageTypeNone - } - return m -} diff --git a/dhcpv4/option_message_type_test.go b/dhcpv4/option_message_type_test.go index a97889e..4ba5338 100644 --- a/dhcpv4/option_message_type_test.go +++ b/dhcpv4/option_message_type_test.go @@ -31,7 +31,9 @@ func TestParseOptMessageType(t *testing.T) { } func TestGetMessageType(t *testing.T) { - o := OptionsFromList(OptMessageType(MessageTypeDiscover)) - require.Equal(t, MessageTypeDiscover, GetMessageType(o)) - require.Equal(t, MessageTypeNone, GetMessageType(Options{})) + m, _ := New(WithMessageType(MessageTypeDiscover)) + require.Equal(t, MessageTypeDiscover, m.MessageType()) + + m, _ = New() + require.Equal(t, MessageTypeNone, m.MessageType()) } diff --git a/dhcpv4/option_parameter_request_list.go b/dhcpv4/option_parameter_request_list.go index 81d5efb..99f2445 100644 --- a/dhcpv4/option_parameter_request_list.go +++ b/dhcpv4/option_parameter_request_list.go @@ -64,21 +64,6 @@ func (ol *OptionCodeList) FromBytes(data []byte) error { return buf.FinError() } -// GetParameterRequestList returns the DHCPv4 Parameter Request List in o. -// -// The parameter request list option is described by RFC 2132, Section 9.8. -func GetParameterRequestList(o Options) OptionCodeList { - v := o.Get(OptionParameterRequestList) - if v == nil { - return nil - } - var codes OptionCodeList - if err := codes.FromBytes(v); err != nil { - return nil - } - return codes -} - // OptParameterRequestList returns a new DHCPv4 Parameter Request List. // // The parameter request list option is described by RFC 2132, Section 9.8. diff --git a/dhcpv4/option_relay_agent_information.go b/dhcpv4/option_relay_agent_information.go index fb86c70..8b88b0c 100644 --- a/dhcpv4/option_relay_agent_information.go +++ b/dhcpv4/option_relay_agent_information.go @@ -36,18 +36,3 @@ func (r *RelayOptions) FromBytes(data []byte) error { func OptRelayAgentInfo(o ...Option) Option { return Option{Code: OptionRelayAgentInformation, Value: RelayOptions{OptionsFromList(o...)}} } - -// GetRelayAgentInfo returns options embedded by the relay agent. -// -// The relay agent info option is described by RFC 3046. -func GetRelayAgentInfo(o Options) *RelayOptions { - v := o.Get(OptionRelayAgentInformation) - if v == nil { - return nil - } - var relayOptions RelayOptions - if err := relayOptions.FromBytes(v); err != nil { - return nil - } - return &relayOptions -} diff --git a/dhcpv4/option_relay_agent_information_test.go b/dhcpv4/option_relay_agent_information_test.go index 6a7b275..d16aedd 100644 --- a/dhcpv4/option_relay_agent_information_test.go +++ b/dhcpv4/option_relay_agent_information_test.go @@ -7,14 +7,12 @@ import ( ) func TestGetRelayAgentInformation(t *testing.T) { - o := Options{ - OptionRelayAgentInformation.Code(): []byte{ - 1, 5, 'l', 'i', 'n', 'u', 'x', - 2, 4, 'b', 'o', 'o', 't', - }, - } + m, _ := New(WithGeneric(OptionRelayAgentInformation, []byte{ + 1, 5, 'l', 'i', 'n', 'u', 'x', + 2, 4, 'b', 'o', 'o', 't', + })) - opt := GetRelayAgentInfo(o) + opt := m.RelayAgentInfo() require.NotNil(t, opt) require.Equal(t, len(opt.Options), 2) @@ -24,15 +22,14 @@ func TestGetRelayAgentInformation(t *testing.T) { require.Equal(t, remote, []byte("boot")) // Empty. - require.Nil(t, GetRelayAgentInfo(Options{})) + m, _ = New() + require.Nil(t, m.RelayAgentInfo()) // Invalid contents. - o = Options{ - OptionRelayAgentInformation.Code(): []byte{ - 1, 7, 'l', 'i', 'n', 'u', 'x', - }, - } - require.Nil(t, GetRelayAgentInfo(o)) + m, _ = New(WithGeneric(OptionRelayAgentInformation, []byte{ + 1, 7, 'l', 'i', 'n', 'u', 'x', + })) + require.Nil(t, m.RelayAgentInfo()) } func TestOptRelayAgentInfo(t *testing.T) { diff --git a/dhcpv4/option_string.go b/dhcpv4/option_string.go index 9e16d6c..b505575 100644 --- a/dhcpv4/option_string.go +++ b/dhcpv4/option_string.go @@ -38,13 +38,6 @@ func OptDomainName(name string) Option { return Option{Code: OptionDomainName, Value: String(name)} } -// GetDomainName parses the DHCPv4 Domain Name option from o if present. -// -// The Domain Name option is described by RFC 2132, Section 3.17. -func GetDomainName(o Options) string { - return GetString(OptionDomainName, o) -} - // OptHostName returns a new DHCPv4 Host Name option. // // The Host Name option is described by RFC 2132, Section 3.14. @@ -52,13 +45,6 @@ func OptHostName(name string) Option { return Option{Code: OptionHostName, Value: String(name)} } -// GetHostName parses the DHCPv4 Host Name option from o if present. -// -// The Host Name option is described by RFC 2132, Section 3.14. -func GetHostName(o Options) string { - return GetString(OptionHostName, o) -} - // OptRootPath returns a new DHCPv4 Root Path option. // // The Root Path option is described by RFC 2132, Section 3.19. @@ -66,13 +52,6 @@ func OptRootPath(name string) Option { return Option{Code: OptionRootPath, Value: String(name)} } -// GetRootPath parses the DHCPv4 Root Path option from o if present. -// -// The Root Path option is described by RFC 2132, Section 3.19. -func GetRootPath(o Options) string { - return GetString(OptionRootPath, o) -} - // OptBootFileName returns a new DHCPv4 Boot File Name option. // // The Bootfile Name option is described by RFC 2132, Section 9.5. @@ -80,13 +59,6 @@ func OptBootFileName(name string) Option { return Option{Code: OptionBootfileName, Value: String(name)} } -// GetBootFileName parses the DHCPv4 Bootfile Name option from o if present. -// -// The Bootfile Name option is described by RFC 2132, Section 9.5. -func GetBootFileName(o Options) string { - return GetString(OptionBootfileName, o) -} - // OptTFTPServerName returns a new DHCPv4 TFTP Server Name option. // // The TFTP Server Name option is described by RFC 2132, Section 9.4. @@ -94,24 +66,9 @@ func OptTFTPServerName(name string) Option { return Option{Code: OptionTFTPServerName, Value: String(name)} } -// GetTFTPServerName parses the DHCPv4 TFTP Server Name option from o if -// present. -// -// The TFTP Server Name option is described by RFC 2132, Section 9.4. -func GetTFTPServerName(o Options) string { - return GetString(OptionTFTPServerName, o) -} - // OptClassIdentifier returns a new DHCPv4 Class Identifier option. // // The Vendor Class Identifier option is described by RFC 2132, Section 9.13. func OptClassIdentifier(name string) Option { return Option{Code: OptionClassIdentifier, Value: String(name)} } - -// GetClassIdentifier parses the DHCPv4 Class Identifier option from o if present. -// -// The Vendor Class Identifier option is described by RFC 2132, Section 9.13. -func GetClassIdentifier(o Options) string { - return GetString(OptionClassIdentifier, o) -} diff --git a/dhcpv4/option_string_test.go b/dhcpv4/option_string_test.go index bda6009..4dbb77d 100644 --- a/dhcpv4/option_string_test.go +++ b/dhcpv4/option_string_test.go @@ -14,11 +14,11 @@ func TestOptDomainName(t *testing.T) { } func TestParseOptDomainName(t *testing.T) { - o := Options{ - OptionDomainName.Code(): []byte{'t', 'e', 's', 't'}, - } - require.Equal(t, "test", GetDomainName(o)) - require.Equal(t, "", GetDomainName(Options{})) + m, _ := New(WithGeneric(OptionDomainName, []byte{'t', 'e', 's', 't'})) + require.Equal(t, "test", m.DomainName()) + + m, _ = New() + require.Equal(t, "", m.DomainName()) } func TestOptHostName(t *testing.T) { @@ -29,11 +29,11 @@ func TestOptHostName(t *testing.T) { } func TestParseOptHostName(t *testing.T) { - o := Options{ - OptionHostName.Code(): []byte{'t', 'e', 's', 't'}, - } - require.Equal(t, "test", GetHostName(o)) - require.Equal(t, "", GetHostName(Options{})) + m, _ := New(WithGeneric(OptionHostName, []byte{'t', 'e', 's', 't'})) + require.Equal(t, "test", m.HostName()) + + m, _ = New() + require.Equal(t, "", m.HostName()) } func TestOptRootPath(t *testing.T) { @@ -44,9 +44,11 @@ func TestOptRootPath(t *testing.T) { } func TestParseOptRootPath(t *testing.T) { - o := OptionsFromList(OptRootPath("test")) - require.Equal(t, "test", GetRootPath(o)) - require.Equal(t, "", GetRootPath(Options{})) + m, _ := New(WithGeneric(OptionRootPath, []byte{'t', 'e', 's', 't'})) + require.Equal(t, "test", m.RootPath()) + + m, _ = New() + require.Equal(t, "", m.RootPath()) } func TestOptBootFileName(t *testing.T) { @@ -57,9 +59,11 @@ func TestOptBootFileName(t *testing.T) { } func TestParseOptBootFileName(t *testing.T) { - o := OptionsFromList(OptBootFileName("test")) - require.Equal(t, "test", GetBootFileName(o)) - require.Equal(t, "", GetBootFileName(Options{})) + m, _ := New(WithGeneric(OptionBootfileName, []byte{'t', 'e', 's', 't'})) + require.Equal(t, "test", m.BootFileNameOption()) + + m, _ = New() + require.Equal(t, "", m.BootFileNameOption()) } func TestOptTFTPServerName(t *testing.T) { @@ -70,9 +74,11 @@ func TestOptTFTPServerName(t *testing.T) { } func TestParseOptTFTPServerName(t *testing.T) { - o := OptionsFromList(OptTFTPServerName("test")) - require.Equal(t, "test", GetTFTPServerName(o)) - require.Equal(t, "", GetTFTPServerName(Options{})) + m, _ := New(WithGeneric(OptionTFTPServerName, []byte{'t', 'e', 's', 't'})) + require.Equal(t, "test", m.TFTPServerName()) + + m, _ = New() + require.Equal(t, "", m.TFTPServerName()) } func TestOptClassIdentifier(t *testing.T) { @@ -83,7 +89,9 @@ func TestOptClassIdentifier(t *testing.T) { } func TestParseOptClassIdentifier(t *testing.T) { - o := OptionsFromList(OptClassIdentifier("test")) - require.Equal(t, "test", GetClassIdentifier(o)) - require.Equal(t, "", GetClassIdentifier(Options{})) + m, _ := New(WithGeneric(OptionClassIdentifier, []byte{'t', 'e', 's', 't'})) + require.Equal(t, "test", m.ClassIdentifier()) + + m, _ = New() + require.Equal(t, "", m.ClassIdentifier()) } diff --git a/dhcpv4/option_subnet_mask.go b/dhcpv4/option_subnet_mask.go index 82b344b..a0c6567 100644 --- a/dhcpv4/option_subnet_mask.go +++ b/dhcpv4/option_subnet_mask.go @@ -31,21 +31,6 @@ func (im *IPMask) FromBytes(data []byte) error { return buf.FinError() } -// GetSubnetMask returns a subnet mask option contained in o, if there is one. -// -// The subnet mask option is described by RFC 2132, Section 3.3. -func GetSubnetMask(o Options) net.IPMask { - v := o.Get(OptionSubnetMask) - if v == nil { - return nil - } - var im IPMask - if err := im.FromBytes(v); err != nil { - return nil - } - return net.IPMask(im) -} - // OptSubnetMask returns a new DHCPv4 SubnetMask option per RFC 2132, Section 3.3. func OptSubnetMask(mask net.IPMask) Option { return Option{ diff --git a/dhcpv4/option_subnet_mask_test.go b/dhcpv4/option_subnet_mask_test.go index bc82cf1..f9831b6 100644 --- a/dhcpv4/option_subnet_mask_test.go +++ b/dhcpv4/option_subnet_mask_test.go @@ -15,15 +15,15 @@ func TestOptSubnetMask(t *testing.T) { } func TestGetSubnetMask(t *testing.T) { - o := OptionsFromList(OptSubnetMask(net.IPMask{})) - mask := GetSubnetMask(o) + m, _ := New(WithOption(OptSubnetMask(net.IPMask{}))) + mask := m.SubnetMask() require.Nil(t, mask, "empty byte stream") - o = OptionsFromList(OptSubnetMask(net.IPMask{255})) - mask = GetSubnetMask(o) + m, _ = New(WithOption(OptSubnetMask(net.IPMask{255}))) + mask = m.SubnetMask() require.Nil(t, mask, "short byte stream") - o = OptionsFromList(OptSubnetMask(net.IPMask{255, 255, 255, 0})) - mask = GetSubnetMask(o) + m, _ = New(WithOption(OptSubnetMask(net.IPMask{255, 255, 255, 0}))) + mask = m.SubnetMask() require.Equal(t, net.IPMask{255, 255, 255, 0}, mask) } diff --git a/dhcpv4/option_userclass.go b/dhcpv4/option_userclass.go index f273a84..69ceeba 100644 --- a/dhcpv4/option_userclass.go +++ b/dhcpv4/option_userclass.go @@ -14,21 +14,6 @@ type UserClass struct { RFC3004 bool } -// GetUserClass returns the user class in o if present. -// -// The user class information option is defined by RFC 3004. -func GetUserClass(o Options) *UserClass { - v := o.Get(OptionUserClassInformation) - if v == nil { - return nil - } - var uc UserClass - if err := uc.FromBytes(v); err != nil { - return nil - } - return &uc -} - // OptUserClass returns a new user class option. func OptUserClass(v []byte) Option { return Option{ diff --git a/dhcpv4/option_vivc.go b/dhcpv4/option_vivc.go index 509ba80..a669d27 100644 --- a/dhcpv4/option_vivc.go +++ b/dhcpv4/option_vivc.go @@ -25,19 +25,6 @@ func OptVIVC(identifiers ...VIVCIdentifier) Option { } } -// GetVIVC returns the vendor-identifying vendor class option in o if present. -func GetVIVC(o Options) VIVCIdentifiers { - v := o.Get(OptionVendorIdentifyingVendorClass) - if v == nil { - return nil - } - var ids VIVCIdentifiers - if err := ids.FromBytes(v); err != nil { - return nil - } - return ids -} - // VIVCIdentifiers implements encoding and decoding methods for a DHCP option // described in RFC 3925. type VIVCIdentifiers []VIVCIdentifier diff --git a/dhcpv4/option_vivc_test.go b/dhcpv4/option_vivc_test.go index b1ec398..77b64b7 100644 --- a/dhcpv4/option_vivc_test.go +++ b/dhcpv4/option_vivc_test.go @@ -30,23 +30,24 @@ func TestOptVIVCInterfaceMethods(t *testing.T) { } func TestParseOptVICO(t *testing.T) { - options := Options{OptionVendorIdentifyingVendorClass.Code(): sampleVIVCOptRaw} - o := GetVIVC(options) + m, _ := New(WithGeneric(OptionVendorIdentifyingVendorClass, sampleVIVCOptRaw)) + o := m.VIVC() require.Equal(t, sampleVIVCOpt, o) // Identifier len too long data := make([]byte, len(sampleVIVCOptRaw)) copy(data, sampleVIVCOptRaw) data[4] = 40 - options = Options{OptionVendorIdentifyingVendorClass.Code(): data} - o = GetVIVC(options) + m, _ = New(WithGeneric(OptionVendorIdentifyingVendorClass, data)) + o = m.VIVC() require.Nil(t, o, "should get error from bad length") // Longer than length data[4] = 5 - options = Options{OptionVendorIdentifyingVendorClass.Code(): data[:10]} - o = GetVIVC(options) + m, _ = New(WithGeneric(OptionVendorIdentifyingVendorClass, data[:10])) + o = m.VIVC() require.Equal(t, o[0].Data, []byte("Cisco")) - require.Equal(t, VIVCIdentifiers(nil), GetVIVC(Options{})) + m, _ = New() + require.Equal(t, VIVCIdentifiers(nil), m.VIVC()) } diff --git a/dhcpv4/server_test.go b/dhcpv4/server_test.go index 8626451..3cfd4e1 100644 --- a/dhcpv4/server_test.go +++ b/dhcpv4/server_test.go @@ -42,8 +42,7 @@ func DORAHandler(conn net.PacketConn, peer net.Addr, m *DHCPv4) { return } reply.UpdateOption(OptServerIdentifier(net.IP{1, 2, 3, 4})) - mt := GetMessageType(m.Options) - switch mt { + switch mt := m.MessageType(); mt { case MessageTypeDiscover: reply.UpdateOption(OptMessageType(MessageTypeOffer)) case MessageTypeRequest: diff --git a/dhcpv4/ztpv4/ztp.go b/dhcpv4/ztpv4/ztp.go index 4401e9d..1bcb44f 100644 --- a/dhcpv4/ztpv4/ztp.go +++ b/dhcpv4/ztpv4/ztp.go @@ -18,7 +18,7 @@ var errVendorOptionMalformed = errors.New("malformed vendor option") // ParseVendorData will try to parse dhcp4 options looking for more // specific vendor data (like model, serial number, etc). func ParseVendorData(packet *dhcpv4.DHCPv4) (*VendorData, error) { - vc := dhcpv4.GetClassIdentifier(packet.Options) + vc := packet.ClassIdentifier() if len(vc) == 0 { return nil, errors.New("vendor options not found") } @@ -58,7 +58,7 @@ func ParseVendorData(packet *dhcpv4.DHCPv4) (*VendorData, error) { p := strings.Split(vc, "-") if len(p) < 3 { vd.Model = p[1] - vd.Serial = dhcpv4.GetHostName(packet.Options) + vd.Serial = packet.HostName() if len(vd.Serial) == 0 { return nil, errors.New("host name option is missing") } |