summaryrefslogtreecommitdiffhomepage
path: root/dhcpv6/option_requestedoption.go
blob: 0324642d6bcccff89e6b7c41aa3a459856e20cb5 (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
66
67
68
69
70
71
72
73
74
package dhcpv6

// This module defines the OptRequestedOption structure.
// https://www.ietf.org/rfc/rfc3315.txt

import (
	"encoding/binary"
	"fmt"
	"strings"
)

type OptRequestedOption struct {
	requestedOptions []OptionCode
}

func (op *OptRequestedOption) Code() OptionCode {
	return OptionORO
}

func (op *OptRequestedOption) ToBytes() []byte {
	buf := make([]byte, 4)
	roBytes := make([]byte, 2)
	binary.BigEndian.PutUint16(buf[0:2], uint16(OptionORO))
	binary.BigEndian.PutUint16(buf[2:4], uint16(op.Length()))
	for _, ro := range op.requestedOptions {
		binary.BigEndian.PutUint16(roBytes, uint16(ro))
		buf = append(buf, roBytes...)
	}
	return buf
}

func (op *OptRequestedOption) RequestedOptions() []OptionCode {
	return op.requestedOptions
}

func (op *OptRequestedOption) SetRequestedOptions(opts []OptionCode) {
	op.requestedOptions = opts
}

func (op *OptRequestedOption) AddRequestedOption(opt OptionCode) {
	for _, requestedOption := range op.requestedOptions {
		if opt == requestedOption {
			fmt.Printf("Warning: option %s is already set, appending duplicate", opt)
		}
	}
	op.requestedOptions = append(op.requestedOptions, opt)
}

func (op *OptRequestedOption) Length() int {
	return len(op.requestedOptions) * 2
}

func (op *OptRequestedOption) String() string {
	names := make([]string, 0, len(op.requestedOptions))
	for _, code := range op.requestedOptions {
		names = append(names, code.String())
	}
	return fmt.Sprintf("OptRequestedOption{options=[%v]}", strings.Join(names, ", "))
}

// build an OptRequestedOption structure from a sequence of bytes.
// The input data does not include option code and length bytes.
func ParseOptRequestedOption(data []byte) (*OptRequestedOption, error) {
	if len(data)%2 != 0 {
		return nil, fmt.Errorf("Invalid OptRequestedOption data: length is not a multiple of 2")
	}
	opt := OptRequestedOption{}
	var rOpts []OptionCode
	for i := 0; i < len(data); i += 2 {
		rOpts = append(rOpts, OptionCode(binary.BigEndian.Uint16(data[i:i+2])))
	}
	opt.requestedOptions = rOpts
	return &opt, nil
}