summaryrefslogtreecommitdiffhomepage
path: root/dhcpv6/option_clientlinklayeraddress.go
blob: 878a5763bf1da539f0991aacc58c47db8ae71438 (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
package dhcpv6

import (
	"fmt"
	"net"

	"github.com/insomniacslk/dhcp/iana"
	"github.com/u-root/uio/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("%s: Type=%s LinkLayerAddress=%s", op.Code(), op.LinkLayerType, op.LinkLayerAddress)
}

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