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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
|
package dhcpv6
import (
"fmt"
"strings"
"github.com/u-root/uio/uio"
)
// Option is an interface that all DHCPv6 options adhere to.
type Option interface {
Code() OptionCode
ToBytes() []byte
String() string
FromBytes([]byte) error
}
type OptionGeneric struct {
OptionCode OptionCode
OptionData []byte
}
func (og *OptionGeneric) Code() OptionCode {
return og.OptionCode
}
func (og *OptionGeneric) ToBytes() []byte {
return og.OptionData
}
func (og *OptionGeneric) String() string {
if len(og.OptionData) == 0 {
return og.OptionCode.String()
}
return fmt.Sprintf("%s: %v", og.OptionCode, og.OptionData)
}
// FromBytes resets OptionData to p.
func (og *OptionGeneric) FromBytes(p []byte) error {
og.OptionData = append([]byte(nil), p...)
return nil
}
// ParseOption parses data according to the given code.
func ParseOption(code OptionCode, optData []byte) (Option, error) {
// Parse a sequence of bytes as a single DHCPv6 option.
// Returns the option structure, or an error if any.
var (
err error
opt Option
)
switch code {
case OptionClientID:
var o optClientID
err = o.FromBytes(optData)
opt = &o
case OptionServerID:
var o optServerID
err = o.FromBytes(optData)
opt = &o
case OptionIANA:
var o OptIANA
err = o.FromBytes(optData)
opt = &o
case OptionIATA:
var o OptIATA
err = o.FromBytes(optData)
opt = &o
case OptionIAAddr:
var o OptIAAddress
err = o.FromBytes(optData)
opt = &o
case OptionORO:
var o optRequestedOption
err = o.FromBytes(optData)
opt = &o
case OptionElapsedTime:
var o optElapsedTime
err = o.FromBytes(optData)
opt = &o
case OptionRelayMsg:
var o optRelayMsg
err = o.FromBytes(optData)
opt = &o
case OptionStatusCode:
var o OptStatusCode
err = o.FromBytes(optData)
opt = &o
case OptionUserClass:
var o OptUserClass
err = o.FromBytes(optData)
opt = &o
case OptionVendorClass:
var o OptVendorClass
err = o.FromBytes(optData)
opt = &o
case OptionVendorOpts:
var o OptVendorOpts
err = o.FromBytes(optData)
opt = &o
case OptionInterfaceID:
var o optInterfaceID
err = o.FromBytes(optData)
opt = &o
case OptionDNSRecursiveNameServer:
var o optDNS
err = o.FromBytes(optData)
opt = &o
case OptionDomainSearchList:
var o optDomainSearchList
err = o.FromBytes(optData)
opt = &o
case OptionIAPD:
var o OptIAPD
err = o.FromBytes(optData)
opt = &o
case OptionIAPrefix:
var o OptIAPrefix
err = o.FromBytes(optData)
opt = &o
case OptionInformationRefreshTime:
var o optInformationRefreshTime
err = o.FromBytes(optData)
opt = &o
case OptionRemoteID:
var o OptRemoteID
err = o.FromBytes(optData)
opt = &o
case OptionFQDN:
var o OptFQDN
err = o.FromBytes(optData)
opt = &o
case OptionNTPServer:
var o OptNTPServer
err = o.FromBytes(optData)
opt = &o
case OptionBootfileURL:
var o optBootFileURL
err = o.FromBytes(optData)
opt = &o
case OptionBootfileParam:
var o optBootFileParam
err = o.FromBytes(optData)
opt = &o
case OptionClientArchType:
var o optClientArchType
err = o.FromBytes(optData)
opt = &o
case OptionNII:
var o OptNetworkInterfaceID
err = o.FromBytes(optData)
opt = &o
case OptionClientLinkLayerAddr:
var o optClientLinkLayerAddress
err = o.FromBytes(optData)
opt = &o
case OptionDHCPv4Msg:
var o OptDHCPv4Msg
err = o.FromBytes(optData)
opt = &o
case OptionDHCP4oDHCP6Server:
var o OptDHCP4oDHCP6Server
err = o.FromBytes(optData)
opt = &o
case Option4RD:
var o Opt4RD
err = o.FromBytes(optData)
opt = &o
case Option4RDMapRule:
var o Opt4RDMapRule
err = o.FromBytes(optData)
opt = &o
case Option4RDNonMapRule:
var o Opt4RDNonMapRule
err = o.FromBytes(optData)
opt = &o
case OptionRelayPort:
var o optRelayPort
err = o.FromBytes(optData)
opt = &o
default:
opt = &OptionGeneric{OptionCode: code, OptionData: optData}
}
if err != nil {
return nil, err
}
return opt, nil
}
type longStringer interface {
LongString(spaceIndent int) string
}
// Options is a collection of options.
type Options []Option
// LongString prints options with indentation of at least spaceIndent spaces.
func (o Options) LongString(spaceIndent int) string {
indent := strings.Repeat(" ", spaceIndent)
var s strings.Builder
if len(o) == 0 {
s.WriteString("[]")
} else {
s.WriteString("[\n")
for _, opt := range o {
s.WriteString(indent)
s.WriteString(" ")
if ls, ok := opt.(longStringer); ok {
s.WriteString(ls.LongString(spaceIndent + 2))
} else {
s.WriteString(opt.String())
}
s.WriteString("\n")
}
s.WriteString(indent)
s.WriteString("]")
}
return s.String()
}
// 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)
}
// ToBytes marshals all options to bytes.
func (o Options) ToBytes() []byte {
buf := uio.NewBigEndianBuffer(nil)
for _, opt := range o {
buf.Write16(uint16(opt.Code()))
val := opt.ToBytes()
buf.Write16(uint16(len(val)))
buf.WriteBytes(val)
}
return buf.Data()
}
// 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(code OptionCode, 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 {
*o = make(Options, 0, 10)
if len(data) == 0 {
// no options, no party
return nil
}
buf := uio.NewBigEndianBuffer(data)
for buf.Has(4) {
code := OptionCode(buf.Read16())
length := int(buf.Read16())
// Consume, but do not Copy. Each parser will make a copy of
// pertinent data.
optData := buf.Consume(length)
opt, err := parser(code, optData)
if err != nil {
return err
}
*o = append(*o, opt)
}
return buf.FinError()
}
|