File: option_maximum_dhcp_message_size.go

package info (click to toggle)
golang-github-insomniacslk-dhcp 0.0~git20200621.d74cd86-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,132 kB
  • sloc: makefile: 9
file content (50 lines) | stat: -rw-r--r-- 1,270 bytes parent folder | download
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/u-root/pkg/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)}
}