File: options.go

package info (click to toggle)
golang-github-mdlayher-dhcp6 0.0~git20190311.2a67805-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 400 kB
  • sloc: makefile: 3
file content (430 lines) | stat: -rw-r--r-- 11,198 bytes parent folder | download
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
package dhcp6opts

import (
	"github.com/mdlayher/dhcp6"
)

// GetClientID returns the Client Identifier Option value, as described in RFC
// 3315, Section 22.2.
//
// The DUID returned allows unique identification of a client to a server.
func GetClientID(o dhcp6.Options) (DUID, error) {
	v, err := o.GetOne(dhcp6.OptionClientID)
	if err != nil {
		return nil, err
	}

	return parseDUID(v)
}

// GetServerID returns the Server Identifier Option value, as described in RFC
// 3315, Section 22.3.
//
// The DUID returned allows unique identification of a server to a client.
func GetServerID(o dhcp6.Options) (DUID, error) {
	v, err := o.GetOne(dhcp6.OptionServerID)
	if err != nil {
		return nil, err
	}

	return parseDUID(v)
}

// GetIANA returns the Identity Association for Non-temporary Addresses Option
// value, as described in RFC 3315, Section 22.4.
//
// Multiple IANA values may be present in a single DHCP request.
func GetIANA(o dhcp6.Options) ([]*IANA, error) {
	vv, err := o.Get(dhcp6.OptionIANA)
	if err != nil {
		return nil, err
	}

	// Parse each IA_NA value
	iana := make([]*IANA, len(vv))
	for i := range vv {
		iana[i] = &IANA{}
		if err := iana[i].UnmarshalBinary(vv[i]); err != nil {
			return nil, err
		}
	}
	return iana, nil
}

// GetIATA returns the Identity Association for Temporary Addresses Option
// value, as described in RFC 3315, Section 22.5.
//
// Multiple IATA values may be present in a single DHCP request.
func GetIATA(o dhcp6.Options) ([]*IATA, error) {
	vv, err := o.Get(dhcp6.OptionIATA)
	if err != nil {
		return nil, err
	}

	// Parse each IA_NA value
	iata := make([]*IATA, len(vv))
	for i := range vv {
		iata[i] = &IATA{}
		if err := iata[i].UnmarshalBinary(vv[i]); err != nil {
			return nil, err
		}
	}
	return iata, nil
}

// GetIAAddr returns the Identity Association Address Option value, as described
// in RFC 3315, Section 22.6.
//
// The IAAddr option must always appear encapsulated in the Options map of a
// IANA or IATA option.  Multiple IAAddr values may be present in a single DHCP
// request.
func GetIAAddr(o dhcp6.Options) ([]*IAAddr, error) {
	vv, err := o.Get(dhcp6.OptionIAAddr)
	if err != nil {
		return nil, err
	}

	iaAddr := make([]*IAAddr, len(vv))
	for i := range vv {
		iaAddr[i] = &IAAddr{}
		if err := iaAddr[i].UnmarshalBinary(vv[i]); err != nil {
			return nil, err
		}
	}
	return iaAddr, nil
}

// GetOptionRequest returns the Option Request Option value, as described in
// RFC 3315, Section 22.7.
//
// The slice of OptionCode values indicates the options a DHCP client is
// interested in receiving from a server.
func GetOptionRequest(o dhcp6.Options) (OptionRequestOption, error) {
	v, err := o.GetOne(dhcp6.OptionORO)
	if err != nil {
		return nil, err
	}

	var oro OptionRequestOption
	err = oro.UnmarshalBinary(v)
	return oro, err
}

// GetPreference returns the Preference Option value, as described in RFC 3315,
// Section 22.8.
//
// The integer preference value is sent by a server to a client to affect the
// selection of a server by the client.
func GetPreference(o dhcp6.Options) (Preference, error) {
	v, err := o.GetOne(dhcp6.OptionPreference)
	if err != nil {
		return 0, err
	}

	var p Preference
	err = (&p).UnmarshalBinary(v)
	return p, err
}

// GetElapsedTime returns the Elapsed Time Option value, as described in RFC
// 3315, Section 22.9.
//
// The time.Duration returned reports the time elapsed during a DHCP
// transaction, as reported by a client.
func GetElapsedTime(o dhcp6.Options) (ElapsedTime, error) {
	v, err := o.GetOne(dhcp6.OptionElapsedTime)
	if err != nil {
		return 0, err
	}

	var t ElapsedTime
	err = (&t).UnmarshalBinary(v)
	return t, err
}

// GetRelayMessageOption returns the Relay Message Option value, as described
// in RFC 3315, Section 22.10.
//
// The RelayMessage option carries a DHCP message in a Relay-forward or
// Relay-reply message.
func GetRelayMessageOption(o dhcp6.Options) (RelayMessageOption, error) {
	v, err := o.GetOne(dhcp6.OptionRelayMsg)
	if err != nil {
		return nil, err
	}

	var r RelayMessageOption
	err = (&r).UnmarshalBinary(v)
	return r, err
}

// GetAuthentication returns the Authentication Option value, as described in
// RFC 3315, Section 22.11.
//
// The Authentication option carries authentication information to
// authenticate the identity and contents of DHCP messages.
func GetAuthentication(o dhcp6.Options) (*Authentication, error) {
	v, err := o.GetOne(dhcp6.OptionAuth)
	if err != nil {
		return nil, err
	}

	a := new(Authentication)
	err = a.UnmarshalBinary(v)
	return a, err
}

// GetUnicast returns the IP from a Unicast Option value, described in RFC
// 3315, Section 22.12.
//
// The IP return value indicates a server's IPv6 address, which a client may
// use to contact the server via unicast.
func GetUnicast(o dhcp6.Options) (IP, error) {
	v, err := o.GetOne(dhcp6.OptionUnicast)
	if err != nil {
		return nil, err
	}

	var ip IP
	err = ip.UnmarshalBinary(v)
	return ip, err
}

// GetStatusCode returns the Status Code Option value, described in RFC 3315,
// Section 22.13.
//
// The StatusCode return value may be used to determine a code and an
// explanation for the status.
func GetStatusCode(o dhcp6.Options) (*StatusCode, error) {
	v, err := o.GetOne(dhcp6.OptionStatusCode)
	if err != nil {
		return nil, err
	}

	s := new(StatusCode)
	err = s.UnmarshalBinary(v)
	return s, err
}

// GetRapidCommit returns the Rapid Commit Option value, described in RFC 3315,
// Section 22.14.
//
// Nil is returned if OptionRapidCommit was present in the Options map.
func GetRapidCommit(o dhcp6.Options) error {
	v, err := o.GetOne(dhcp6.OptionRapidCommit)
	if err != nil {
		return err
	}

	// Data must be completely empty; presence of the Rapid Commit option
	// indicates it is requested.
	if len(v) != 0 {
		return dhcp6.ErrInvalidPacket
	}
	return nil
}

// GetUserClass returns the User Class Option value, described in RFC 3315,
// Section 22.15.
//
// The Data structure returned contains any raw class data present in
// the option.
func GetUserClass(o dhcp6.Options) (Data, error) {
	v, err := o.GetOne(dhcp6.OptionUserClass)
	if err != nil {
		return nil, err
	}

	var d Data
	err = d.UnmarshalBinary(v)
	return d, err
}

// GetVendorClass returns the Vendor Class Option value, described in RFC 3315,
// Section 22.16.
//
// The VendorClass structure returned contains VendorClass in
// the option.
func GetVendorClass(o dhcp6.Options) (*VendorClass, error) {
	v, err := o.GetOne(dhcp6.OptionVendorClass)
	if err != nil {
		return nil, err
	}

	vc := new(VendorClass)
	err = vc.UnmarshalBinary(v)
	return vc, err
}

// GetVendorOpts returns the Vendor-specific Information Option value,
// described in RFC 3315, Section 22.17.
//
// The VendorOpts structure returned contains Vendor-specific Information data
// present in the option.
func GetVendorOpts(o dhcp6.Options) (*VendorOpts, error) {
	v, err := o.GetOne(dhcp6.OptionVendorOpts)
	if err != nil {
		return nil, err
	}

	vo := new(VendorOpts)
	err = vo.UnmarshalBinary(v)
	return vo, err
}

// GetInterfaceID returns the Interface-Id Option value, described in RFC 3315,
// Section 22.18.
//
// The InterfaceID structure returned contains any raw class data present in
// the option.
func GetInterfaceID(o dhcp6.Options) (InterfaceID, error) {
	v, err := o.GetOne(dhcp6.OptionInterfaceID)
	if err != nil {
		return nil, err
	}

	var i InterfaceID
	err = i.UnmarshalBinary(v)
	return i, err
}

// GetIAPD returns the Identity Association for Prefix Delegation Option value,
// described in RFC 3633, Section 9.
//
// Multiple IAPD values may be present in a a single DHCP request.
func GetIAPD(o dhcp6.Options) ([]*IAPD, error) {
	vv, err := o.Get(dhcp6.OptionIAPD)
	if err != nil {
		return nil, err
	}

	// Parse each IA_PD value
	iapd := make([]*IAPD, len(vv))
	for i := range vv {
		iapd[i] = &IAPD{}
		if err := iapd[i].UnmarshalBinary(vv[i]); err != nil {
			return nil, err
		}
	}

	return iapd, nil
}

// GetIAPrefix returns the Identity Association Prefix Option value, as
// described in RFC 3633, Section 10.
//
// Multiple IAPrefix values may be present in a a single DHCP request.
func GetIAPrefix(o dhcp6.Options) ([]*IAPrefix, error) {
	vv, err := o.Get(dhcp6.OptionIAPrefix)
	if err != nil {
		return nil, err
	}

	// Parse each IAPrefix value
	iaPrefix := make([]*IAPrefix, len(vv))
	for i := range vv {
		iaPrefix[i] = &IAPrefix{}
		if err := iaPrefix[i].UnmarshalBinary(vv[i]); err != nil {
			return nil, err
		}
	}

	return iaPrefix, nil
}

// GetRemoteIdentifier returns the Remote Identifier, described in RFC 4649.
//
// This option may be added by DHCPv6 relay agents that terminate
// switched or permanent circuits and have mechanisms to identify the
// remote host end of the circuit.
func GetRemoteIdentifier(o dhcp6.Options) (*RemoteIdentifier, error) {
	v, err := o.GetOne(dhcp6.OptionRemoteIdentifier)
	if err != nil {
		return nil, err
	}

	r := new(RemoteIdentifier)
	err = r.UnmarshalBinary(v)
	return r, err
}

// GetBootFileURL returns the Boot File URL Option value, described in RFC
// 5970, Section 3.1.
//
// The URL return value contains a URL which may be used by clients to obtain
// a boot file for PXE.
func GetBootFileURL(o dhcp6.Options) (*URL, error) {
	v, err := o.GetOne(dhcp6.OptionBootFileURL)
	if err != nil {
		return nil, err
	}

	u := new(URL)
	err = u.UnmarshalBinary(v)
	return u, err
}

// GetBootFileParam returns the Boot File Parameters Option value, described in
// RFC 5970, Section 3.2.
//
// The Data structure returned contains any parameters needed for a boot
// file, such as a root filesystem label or a path to a configuration file for
// further chainloading.
func GetBootFileParam(o dhcp6.Options) (BootFileParam, error) {
	v, err := o.GetOne(dhcp6.OptionBootFileParam)
	if err != nil {
		return nil, err
	}

	var bfp BootFileParam
	err = bfp.UnmarshalBinary(v)
	return bfp, err
}

// GetClientArchType returns the Client System Architecture Type Option value,
// described in RFC 5970, Section 3.3.
//
// The ArchTypes slice returned contains a list of one or more ArchType values.
// The first ArchType listed is the client's most preferable value.
func GetClientArchType(o dhcp6.Options) (ArchTypes, error) {
	v, err := o.GetOne(dhcp6.OptionClientArchType)
	if err != nil {
		return nil, err
	}

	var a ArchTypes
	err = a.UnmarshalBinary(v)
	return a, err
}

// GetNII returns the Client Network Interface Identifier Option value,
// described in RFC 5970, Section 3.4.
//
// The NII value returned indicates a client's level of Universal Network
// Device Interface (UNDI) support.
func GetNII(o dhcp6.Options) (*NII, error) {
	v, err := o.GetOne(dhcp6.OptionNII)
	if err != nil {
		return nil, err
	}

	n := new(NII)
	err = n.UnmarshalBinary(v)
	return n, err
}

// GetDNSServers returns the DNS Recursive Name Servers Option value, as
// described in RFC 3646, Section 3.
//
// The DNS servers are listed in the order of preference for use by the client
// resolver.
func GetDNSServers(o dhcp6.Options) (IPs, error) {
	v, err := o.GetOne(dhcp6.OptionDNSServers)
	if err != nil {
		return nil, err
	}

	var ips IPs
	err = ips.UnmarshalBinary(v)
	return ips, err
}