summaryrefslogtreecommitdiffhomepage
path: root/dhcpv4/option_subnet_mask.go
blob: a2f9f62f3b7ea4550cf8ab50616101dbcf9c9759 (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/uio/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),
	}
}