summaryrefslogtreecommitdiffhomepage
path: root/dhcpv6/option_clientlinklayeraddress.go
blob: 91d8a2306f3a78dd7b0ae1df0b27e1637efa9721 (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 dhcpv6

import (
	"fmt"
	"net"

	"github.com/insomniacslk/dhcp/iana"
	"github.com/u-root/u-root/pkg/uio"
)

// OptClientLinkLayerAddress implements OptionClientLinkLayerAddr option.
// https://tools.ietf.org/html/rfc6939
func OptClientLinkLayerAddress(ht iana.HWType, lla net.HardwareAddr) *optClientLinkLayerAddress {
	return &optClientLinkLayerAddress{LinkLayerType: ht, LinkLayerAddress: lla}
}

type optClientLinkLayerAddress struct {
	LinkLayerType    iana.HWType
	LinkLayerAddress net.HardwareAddr
}

// Code returns the option code.
func (op *optClientLinkLayerAddress) Code() OptionCode {
	return OptionClientLinkLayerAddr
}

// ToBytes serializes the option and returns it as a sequence of bytes
func (op *optClientLinkLayerAddress) ToBytes() []byte {
	buf := uio.NewBigEndianBuffer(nil)
	buf.Write16(uint16(op.LinkLayerType))
	buf.WriteBytes(op.LinkLayerAddress)
	return buf.Data()
}

func (op *optClientLinkLayerAddress) String() string {
	return fmt.Sprintf("ClientLinkLayerAddress: Type=%s LinkLayerAddress=%s", op.LinkLayerType, op.LinkLayerAddress)
}

// parseOptClientLinkLayerAddress deserializes from bytes
// to build an optClientLinkLayerAddress structure.
func parseOptClientLinkLayerAddress(data []byte) (*optClientLinkLayerAddress, error) {
	var opt optClientLinkLayerAddress
	buf := uio.NewBigEndianBuffer(data)
	opt.LinkLayerType = iana.HWType(buf.Read16())
	opt.LinkLayerAddress = buf.ReadAll()
	return &opt, buf.FinError()
}