summaryrefslogtreecommitdiffhomepage
path: root/dhcpv4/option_maximum_dhcp_message_size.go
blob: f2830238303d721b1681e572087abe8d9d4e0941 (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
48
49
50
package dhcpv4

import (
	"fmt"

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

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

// 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)
	*o = Uint16(buf.Read16())
	return buf.FinError()
}

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

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