diff options
Diffstat (limited to 'dhcpv4/option_maximum_dhcp_message_size.go')
-rw-r--r-- | dhcpv4/option_maximum_dhcp_message_size.go | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/dhcpv4/option_maximum_dhcp_message_size.go b/dhcpv4/option_maximum_dhcp_message_size.go new file mode 100644 index 0000000..05186f5 --- /dev/null +++ b/dhcpv4/option_maximum_dhcp_message_size.go @@ -0,0 +1,57 @@ +package dhcpv4 + +import ( + "encoding/binary" + "fmt" +) + +// This option implements the Maximum DHCP Message size option +// https://tools.ietf.org/html/rfc2132 + +// OptMaximumDHCPMessageSize represents the DHCP message type option. +type OptMaximumDHCPMessageSize struct { + Size uint16 +} + +// ParseOptMaximumDHCPMessageSize constructs an OptMaximumDHCPMessageSize struct from a sequence of +// bytes and returns it, or an error. +func ParseOptMaximumDHCPMessageSize(data []byte) (*OptMaximumDHCPMessageSize, error) { + // Should at least have code, length, and message type. + if len(data) < 4 { + return nil, ErrShortByteStream + } + code := OptionCode(data[0]) + if code != OptionMaximumDHCPMessageSize { + return nil, fmt.Errorf("expected option %v, got %v instead", OptionMaximumDHCPMessageSize, code) + } + length := int(data[1]) + if length != 2 { + return nil, fmt.Errorf("expected length 2, got %v instead", length) + } + msgSize := binary.BigEndian.Uint16(data[2:4]) + return &OptMaximumDHCPMessageSize{Size: msgSize}, nil +} + +// Code returns the option code. +func (o *OptMaximumDHCPMessageSize) Code() OptionCode { + return OptionMaximumDHCPMessageSize +} + +// ToBytes returns a serialized stream of bytes for this option. +func (o *OptMaximumDHCPMessageSize) ToBytes() []byte { + serializedSize := make([]byte, 2) + binary.BigEndian.PutUint16(serializedSize, o.Size) + serializedOpt := []byte{byte(o.Code()), byte(o.Length())} + return append(serializedOpt, serializedSize...) +} + +// String returns a human-readable string for this option. +func (o *OptMaximumDHCPMessageSize) String() string { + return fmt.Sprintf("Maximum DHCP Message Size -> %v", o.Size) +} + +// Length returns the length of the data portion (excluding option code and byte +// for length, if any). +func (o *OptMaximumDHCPMessageSize) Length() int { + return 2 +} |