summaryrefslogtreecommitdiffhomepage
path: root/dhcpv6/option_remoteid.go
blob: 6b1831d9ab0b94b62bdda07e3c1e1063b3b784a7 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package dhcpv6

import (
	"fmt"

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

// OptRemoteId implemens the Remote ID option.
//
// https://www.ietf.org/rfc/rfc4649.txt
type OptRemoteId struct {
	enterpriseNumber uint32
	remoteId         []byte
}

func (op *OptRemoteId) Code() OptionCode {
	return OptionRemoteID
}

// ToBytes serializes this option to a byte stream.
func (op *OptRemoteId) ToBytes() []byte {
	buf := uio.NewBigEndianBuffer(nil)
	buf.Write16(uint16(OptionRemoteID))
	buf.Write16(uint16(op.Length()))
	buf.Write32(uint32(op.enterpriseNumber))
	buf.WriteBytes(op.remoteId)
	return buf.Data()
}

func (op *OptRemoteId) EnterpriseNumber() uint32 {
	return op.enterpriseNumber
}

func (op *OptRemoteId) SetEnterpriseNumber(enterpriseNumber uint32) {
	op.enterpriseNumber = enterpriseNumber
}

func (op *OptRemoteId) RemoteID() []byte {
	return op.remoteId
}

func (op *OptRemoteId) SetRemoteID(remoteId []byte) {
	op.remoteId = append([]byte(nil), remoteId...)
}

func (op *OptRemoteId) Length() int {
	return 4 + len(op.remoteId)
}

func (op *OptRemoteId) String() string {
	return fmt.Sprintf("OptRemoteId{enterprisenum=%v, remoteid=%v}",
		op.enterpriseNumber, op.remoteId,
	)
}

// ParseOptRemoteId builds an OptRemoteId structure from a sequence of bytes.
// The input data does not include option code and length bytes.
func ParseOptRemoteId(data []byte) (*OptRemoteId, error) {
	var opt OptRemoteId
	buf := uio.NewBigEndianBuffer(data)
	opt.enterpriseNumber = buf.Read32()
	opt.remoteId = buf.ReadAll()
	return &opt, buf.FinError()
}