summaryrefslogtreecommitdiffhomepage
path: root/dhcpv6/options.go
blob: 33f0d20df971288120596f9418c01583e1774e03 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package dhcpv6

import (
	"encoding/binary"
	"fmt"
)

// OptionCode is a single byte representing the code for a given Option.
type OptionCode uint16

// Option is an interface that all DHCPv6 options adhere to.
type Option interface {
	Code() OptionCode
	ToBytes() []byte
	Length() int
	String() string
}

type OptionGeneric struct {
	OptionCode OptionCode
	OptionData []byte
}

func (og *OptionGeneric) Code() OptionCode {
	return og.OptionCode
}

func (og *OptionGeneric) ToBytes() []byte {
	var ret []byte
	codeBytes := make([]byte, 2)
	binary.BigEndian.PutUint16(codeBytes, uint16(og.OptionCode))
	ret = append(ret, codeBytes...)
	lengthBytes := make([]byte, 2)
	binary.BigEndian.PutUint16(lengthBytes, uint16(len(og.OptionData)))
	ret = append(ret, lengthBytes...)
	ret = append(ret, og.OptionData...)
	return ret
}

func (og *OptionGeneric) String() string {
	code, ok := OptionCodeToString[og.OptionCode]
	if !ok {
		code = "UnknownOption"
	}
	return fmt.Sprintf("%v -> %v", code, og.OptionData)
}

func (og *OptionGeneric) Length() int {
	return len(og.OptionData)
}

func ParseOption(dataStart []byte) (Option, error) {
	// Parse a sequence of bytes as a single DHCPv6 option.
	// Returns the option structure, or an error if any.
	if len(dataStart) < 4 {
		return nil, fmt.Errorf("Invalid DHCPv6 option: less than 4 bytes")
	}
	code := OptionCode(binary.BigEndian.Uint16(dataStart[:2]))
	length := int(binary.BigEndian.Uint16(dataStart[2:4]))
	if len(dataStart) < length+4 {
		return nil, fmt.Errorf("Invalid option length for option %v. Declared %v, actual %v",
			code, length, len(dataStart)-4,
		)
	}
	var (
		err error
		opt Option
	)
	optData := dataStart[4 : 4+length]
	switch code {
	case OptionClientID:
		opt, err = ParseOptClientId(optData)
	case OptionServerID:
		opt, err = ParseOptServerId(optData)
	case OptionIANA:
		opt, err = ParseOptIANA(optData)
	case OptionIAAddr:
		opt, err = ParseOptIAAddress(optData)
	case OptionORO:
		opt, err = ParseOptRequestedOption(optData)
	case OptionElapsedTime:
		opt, err = ParseOptElapsedTime(optData)
	case OptionRelayMsg:
		opt, err = ParseOptRelayMsg(optData)
	case OptionStatusCode:
		opt, err = ParseOptStatusCode(optData)
	case OptionUserClass:
		opt, err = ParseOptUserClass(optData)
	case OptionVendorClass:
		opt, err = ParseOptVendorClass(optData)
	case OptionVendorOpts:
		opt, err = ParseOptVendorOpts(optData)
	case OptionInterfaceID:
		opt, err = ParseOptInterfaceId(optData)
	case OptionDNSRecursiveNameServer:
		opt, err = ParseOptDNSRecursiveNameServer(optData)
	case OptionDomainSearchList:
		opt, err = ParseOptDomainSearchList(optData)
	case OptionIAPD:
		opt, err = ParseOptIAForPrefixDelegation(optData)
	case OptionIAPrefix:
		opt, err = ParseOptIAPrefix(optData)
	case OptionRemoteID:
		opt, err = ParseOptRemoteId(optData)
	case OptionBootfileURL:
		opt, err = ParseOptBootFileURL(optData)
	case OptionClientArchType:
		opt, err = ParseOptClientArchType(optData)
	case OptionNII:
		opt, err = ParseOptNetworkInterfaceId(optData)
	default:
		opt = &OptionGeneric{OptionCode: code, OptionData: optData}
	}
	if err != nil {
		return nil, err
	}
	if length != opt.Length() {
		return nil, fmt.Errorf("Error: declared length is different from actual length for option %d: %d != %d",
			code, opt.Length(), length)
	}
	return opt, nil
}

// Options is a collection of options.
type Options []Option

// Get returns all options matching the option code.
func (o Options) Get(code OptionCode) []Option {
	var ret []Option
	for _, opt := range o {
		if opt.Code() == code {
			ret = append(ret, opt)
		}
	}
	return ret
}

// GetOne returns the first option matching the option code.
func (o Options) GetOne(code OptionCode) Option {
	for _, opt := range o {
		if opt.Code() == code {
			return opt
		}
	}
	return nil
}

// Add appends one option.
func (o *Options) Add(option Option) {
	*o = append(*o, option)
}

// Del deletes all options matching the option code.
func (o *Options) Del(code OptionCode) {
	newOpts := make(Options, 0, len(*o))
	for _, opt := range *o {
		if opt.Code() != code {
			newOpts = append(newOpts, opt)
		}
	}
	*o = newOpts
}

// Update replaces the first option of the same type as the specified one.
func (o *Options) Update(option Option) {
	for idx, opt := range *o {
		if opt.Code() == option.Code() {
			(*o)[idx] = option
			// don't look further
			return
		}
	}
	// if not found, add it
	o.Add(option)
}

// FromBytes reads data into o and returns an error if the options are not a
// valid serialized representation of DHCPv6 options per RFC 3315.
func (o *Options) FromBytes(data []byte) error {
	return o.FromBytesWithParser(data, ParseOption)
}

// OptionParser is a function signature for option parsing
type OptionParser func(data []byte) (Option, error)

// FromBytesWithParser parses Options from byte sequences using the parsing
// function that is passed in as a paremeter
func (o *Options) FromBytesWithParser(data []byte, parser OptionParser) error {
	// Parse a sequence of bytes until the end and build a list of options from
	// it. Returns an error if any invalid option or length is found.
	*o = make(Options, 0, 10)
	if len(data) == 0 {
		// no options, no party
		return nil
	}
	if len(data) < 4 {
		// cannot be shorter than option code (2 bytes) + length (2 bytes)
		return fmt.Errorf("Invalid options: shorter than 4 bytes")
	}
	idx := 0
	for {
		if idx == len(data) {
			break
		}
		if idx > len(data) {
			// this should never happen
			return fmt.Errorf("Error: reading past the end of options")
		}
		opt, err := parser(data[idx:])
		if err != nil {
			return err
		}
		*o = append(*o, opt)
		idx += opt.Length() + 4 // 4 bytes for type + length
	}
	return nil
}