summaryrefslogtreecommitdiffhomepage
path: root/dhcpv4/option_subnet_mask.go
blob: a0c6567bdbeeec1876700e4a418fc9b7b2610f09 (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
package dhcpv4

import (
	"net"

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

// IPMask represents an option encapsulating the subnet mask.
//
// This option implements the subnet mask option in RFC 2132, Section 3.3.
type IPMask net.IPMask

// ToBytes returns a serialized stream of bytes for this option.
func (im IPMask) ToBytes() []byte {
	if len(im) > net.IPv4len {
		return im[:net.IPv4len]
	}
	return im
}

// String returns a human-readable string.
func (im IPMask) String() string {
	return net.IPMask(im).String()
}

// FromBytes parses im from data per RFC 2132.
func (im *IPMask) FromBytes(data []byte) error {
	buf := uio.NewBigEndianBuffer(data)
	*im = IPMask(buf.CopyN(net.IPv4len))
	return buf.FinError()
}

// OptSubnetMask returns a new DHCPv4 SubnetMask option per RFC 2132, Section 3.3.
func OptSubnetMask(mask net.IPMask) Option {
	return Option{
		Code:  OptionSubnetMask,
		Value: IPMask(mask),
	}
}