diff options
Diffstat (limited to 'dhcpv4/option_maximum_dhcp_message_size.go')
-rw-r--r-- | dhcpv4/option_maximum_dhcp_message_size.go | 58 |
1 files changed, 39 insertions, 19 deletions
diff --git a/dhcpv4/option_maximum_dhcp_message_size.go b/dhcpv4/option_maximum_dhcp_message_size.go index 904f3d2..900eea5 100644 --- a/dhcpv4/option_maximum_dhcp_message_size.go +++ b/dhcpv4/option_maximum_dhcp_message_size.go @@ -6,32 +6,52 @@ import ( "github.com/u-root/u-root/pkg/uio" ) -// OptMaximumDHCPMessageSize implements the maximum DHCP message size option -// described by RFC 2132, Section 9.10. -type OptMaximumDHCPMessageSize struct { - Size uint16 +// Uint16 implements encoding and decoding functions for a uint16 as used in +// RFC 2132, Section 9.10. +type Uint16 uint16 + +// ToBytes returns a serialized stream of bytes for this option. +func (o Uint16) ToBytes() []byte { + buf := uio.NewBigEndianBuffer(nil) + buf.Write16(uint16(o)) + return buf.Data() } -// ParseOptMaximumDHCPMessageSize constructs an OptMaximumDHCPMessageSize -// struct from a sequence of bytes and returns it, or an error. -func ParseOptMaximumDHCPMessageSize(data []byte) (*OptMaximumDHCPMessageSize, error) { +// String returns a human-readable string for this option. +func (o Uint16) String() string { + return fmt.Sprintf("%d", uint16(o)) +} + +// FromBytes decodes data into o as per RFC 2132, Section 9.10. +func (o *Uint16) FromBytes(data []byte) error { buf := uio.NewBigEndianBuffer(data) - return &OptMaximumDHCPMessageSize{Size: buf.Read16()}, buf.FinError() + *o = Uint16(buf.Read16()) + return buf.FinError() } -// Code returns the option code. -func (o *OptMaximumDHCPMessageSize) Code() OptionCode { - return OptionMaximumDHCPMessageSize +// GetUint16 parses a uint16 from code in o. +func GetUint16(code OptionCode, o Options) (uint16, error) { + v := o.Get(code) + if v == nil { + return 0, fmt.Errorf("option not present") + } + var u Uint16 + if err := u.FromBytes(v); err != nil { + return 0, err + } + return uint16(u), nil } -// ToBytes returns a serialized stream of bytes for this option. -func (o *OptMaximumDHCPMessageSize) ToBytes() []byte { - buf := uio.NewBigEndianBuffer(nil) - buf.Write16(o.Size) - return buf.Data() +// OptMaxMessageSize returns a new DHCP Maximum Message Size option. +// +// The Maximum DHCP Message Size option is described by RFC 2132, Section 9.10. +func OptMaxMessageSize(size uint16) Option { + return Option{Code: OptionMaximumDHCPMessageSize, Value: Uint16(size)} } -// String returns a human-readable string for this option. -func (o *OptMaximumDHCPMessageSize) String() string { - return fmt.Sprintf("Maximum DHCP Message Size -> %v", o.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) } |