summaryrefslogtreecommitdiffhomepage
path: root/dhcpv4/option_maximum_dhcp_message_size.go
blob: 26865fd24f941762543f4dc5f601000d08775a78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package dhcpv4

import (
	"encoding/binary"
	"fmt"

	"github.com/u-root/u-root/pkg/uio"
)

// This option implements the Maximum DHCP Message size option
// https://tools.ietf.org/html/rfc2132

// OptMaximumDHCPMessageSize represents the Maximum DHCP Message size 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) {
	buf := uio.NewBigEndianBuffer(data)
	return &OptMaximumDHCPMessageSize{Size: buf.Read16()}, buf.FinError()
}

// 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
}