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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
|
/* $Id: DhcpOptions.h $ */
/** @file
* DHCP server - DHCP options
*/
/*
* Copyright (C) 2017-2019 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
#ifndef VBOX_INCLUDED_SRC_Dhcpd_DhcpOptions_h
#define VBOX_INCLUDED_SRC_Dhcpd_DhcpOptions_h
#ifndef RT_WITHOUT_PRAGMA_ONCE
# pragma once
#endif
#include "Defs.h"
#include <string.h>
#include <iprt/err.h>
#include <iprt/types.h>
#include <iprt/asm.h>
#include <iprt/stdint.h>
#include <iprt/net.h>
#include <string>
class DhcpClientMessage;
class DhcpOption
{
protected:
uint8_t m_OptCode;
bool m_fPresent;
public:
explicit DhcpOption(uint8_t aOptCode)
: m_OptCode(aOptCode), m_fPresent(true) {}
DhcpOption(uint8_t aOptCode, bool fPresent)
: m_OptCode(aOptCode), m_fPresent(fPresent) {}
virtual DhcpOption *clone() const = 0;
virtual ~DhcpOption() {}
public:
static DhcpOption *parse(uint8_t aOptCode, int aEnc, const char *pcszValue);
public:
uint8_t optcode() const { return m_OptCode; }
bool present() const { return m_fPresent; }
public:
int encode(octets_t &dst) const;
int decode(const rawopts_t &map);
int decode(const DhcpClientMessage &req);
protected:
virtual ssize_t encodeValue(octets_t &dst) const = 0;
virtual int decodeValue(const octets_t &src, size_t cb) = 0;
protected:
static const octets_t *findOption(const rawopts_t &aOptMap, uint8_t aOptCode);
protected:
/*
* Serialization
*/
static void append(octets_t &aDst, uint8_t aValue)
{
aDst.push_back(aValue);
}
static void append(octets_t &aDst, uint16_t aValue)
{
RTUINT16U u16 = { RT_H2N_U16(aValue) };
aDst.insert(aDst.end(), u16.au8, u16.au8 + sizeof(aValue));
}
static void append(octets_t &aDst, uint32_t aValue)
{
RTUINT32U u32 = { RT_H2N_U32(aValue) };
aDst.insert(aDst.end(), u32.au8, u32.au8 + sizeof(aValue));
}
static void append(octets_t &aDst, RTNETADDRIPV4 aIPv4)
{
aDst.insert(aDst.end(), aIPv4.au8, aIPv4.au8 + sizeof(aIPv4));
}
static void append(octets_t &aDst, const char *pszString, size_t cb)
{
aDst.insert(aDst.end(), pszString, pszString + cb);
}
static void append(octets_t &aDst, const std::string &str)
{
append(aDst, str.c_str(), str.size());
}
/* non-overloaded name to avoid ambiguity */
static void appendLength(octets_t &aDst, size_t cb)
{
append(aDst, static_cast<uint8_t>(cb));
}
/*
* Deserialization
*/
static void extract(uint8_t &aValue, octets_t::const_iterator &pos)
{
aValue = *pos;
pos += sizeof(uint8_t);
}
static void extract(uint16_t &aValue, octets_t::const_iterator &pos)
{
RTUINT16U u16;
memcpy(u16.au8, &pos[0], sizeof(uint16_t));
aValue = RT_N2H_U16(u16.u);
pos += sizeof(uint16_t);
}
static void extract(uint32_t &aValue, octets_t::const_iterator &pos)
{
RTUINT32U u32;
memcpy(u32.au8, &pos[0], sizeof(uint32_t));
aValue = RT_N2H_U32(u32.u);
pos += sizeof(uint32_t);
}
static void extract(RTNETADDRIPV4 &aValue, octets_t::const_iterator &pos)
{
memcpy(aValue.au8, &pos[0], sizeof(RTNETADDRIPV4));
pos += sizeof(RTNETADDRIPV4);
}
static void extract(std::string &aString, octets_t::const_iterator &pos, size_t cb)
{
aString.replace(aString.begin(), aString.end(), &pos[0], &pos[cb]);
pos += cb;
}
/*
* Parse textual representation (e.g. in config file)
*/
static int parse1(uint8_t &aValue, const char *pcszValue);
static int parse1(uint16_t &aValue, const char *pcszValue);
static int parse1(uint32_t &aValue, const char *pcszValue);
static int parse1(RTNETADDRIPV4 &aValue, const char *pcszValue);
static int parseList(std::vector<RTNETADDRIPV4> &aList, const char *pcszValue);
static int parseHex(octets_t &aRawValue, const char *pcszValue);
};
inline octets_t &operator<<(octets_t &dst, const DhcpOption &option)
{
option.encode(dst);
return dst;
}
optmap_t &operator<<(optmap_t &optmap, DhcpOption *option);
optmap_t &operator<<(optmap_t &optmap, const std::shared_ptr<DhcpOption> &option);
/*
* Only for << OptEnd() syntactic sugar...
*/
struct OptEnd {};
inline octets_t &operator<<(octets_t &dst, const OptEnd &end)
{
RT_NOREF(end);
dst.push_back(RTNET_DHCP_OPT_END);
return dst;
}
/*
* Option that has no value
*/
class OptNoValueBase
: public DhcpOption
{
public:
explicit OptNoValueBase(uint8_t aOptCode)
: DhcpOption(aOptCode, false) {}
OptNoValueBase(uint8_t aOptCode, bool fPresent)
: DhcpOption(aOptCode, fPresent) {}
OptNoValueBase(uint8_t aOptCode, const DhcpClientMessage &req)
: DhcpOption(aOptCode, false)
{
decode(req);
}
virtual OptNoValueBase *clone() const
{
return new OptNoValueBase(*this);
}
protected:
virtual ssize_t encodeValue(octets_t &dst) const
{
RT_NOREF(dst);
return 0;
}
public:
static bool isLengthValid(size_t cb)
{
return cb == 0;
}
virtual int decodeValue(const octets_t &src, size_t cb)
{
RT_NOREF(src);
if (!isLengthValid(cb))
return VERR_INVALID_PARAMETER;
m_fPresent = true;
return VINF_SUCCESS;
}
};
template <uint8_t _OptCode>
class OptNoValue
: public OptNoValueBase
{
public:
static const uint8_t optcode = _OptCode;
OptNoValue()
: OptNoValueBase(optcode) {}
explicit OptNoValue(bool fPresent) /* there's no overloaded ctor with value */
: OptNoValueBase(optcode, fPresent) {}
explicit OptNoValue(const DhcpClientMessage &req)
: OptNoValueBase(optcode, req) {}
};
/*
* Option that contains single value of fixed-size type T
*/
template <typename T>
class OptValueBase
: public DhcpOption
{
public:
typedef T value_t;
protected:
T m_Value;
explicit OptValueBase(uint8_t aOptCode)
: DhcpOption(aOptCode, false), m_Value() {}
OptValueBase(uint8_t aOptCode, const T &aOptValue)
: DhcpOption(aOptCode), m_Value(aOptValue) {}
OptValueBase(uint8_t aOptCode, const DhcpClientMessage &req)
: DhcpOption(aOptCode, false), m_Value()
{
decode(req);
}
public:
virtual OptValueBase *clone() const
{
return new OptValueBase(*this);
}
public:
T &value() { return m_Value; }
const T &value() const { return m_Value; }
protected:
virtual ssize_t encodeValue(octets_t &dst) const
{
append(dst, m_Value);
return sizeof(T);
}
public:
static bool isLengthValid(size_t cb)
{
return cb == sizeof(T);
}
virtual int decodeValue(const octets_t &src, size_t cb)
{
if (!isLengthValid(cb))
return VERR_INVALID_PARAMETER;
octets_t::const_iterator pos(src.begin());
extract(m_Value, pos);
m_fPresent = true;
return VINF_SUCCESS;
}
};
template<uint8_t _OptCode, typename T>
class OptValue
: public OptValueBase<T>
{
public:
using typename OptValueBase<T>::value_t;
public:
static const uint8_t optcode = _OptCode;
OptValue()
: OptValueBase<T>(optcode) {}
explicit OptValue(const T &aOptValue)
: OptValueBase<T>(optcode, aOptValue) {}
explicit OptValue(const DhcpClientMessage &req)
: OptValueBase<T>(optcode, req) {}
static OptValue *parse(const char *pcszValue)
{
typename OptValueBase<T>::value_t v;
int rc = DhcpOption::parse1(v, pcszValue);
if (RT_FAILURE(rc))
return NULL;
return new OptValue(v);
}
};
/*
* Option that contains a string.
*/
class OptStringBase
: public DhcpOption
{
public:
typedef std::string value_t;
protected:
std::string m_String;
explicit OptStringBase(uint8_t aOptCode)
: DhcpOption(aOptCode, false), m_String() {}
OptStringBase(uint8_t aOptCode, const std::string &aOptString)
: DhcpOption(aOptCode), m_String(aOptString) {}
OptStringBase(uint8_t aOptCode, const DhcpClientMessage &req)
: DhcpOption(aOptCode, false), m_String()
{
decode(req);
}
public:
virtual OptStringBase *clone() const
{
return new OptStringBase(*this);
}
public:
std::string &value() { return m_String; }
const std::string &value() const { return m_String; }
protected:
virtual ssize_t encodeValue(octets_t &dst) const
{
if (!isLengthValid(m_String.size()))
return -1;
append(dst, m_String);
return m_String.size();
}
public:
static bool isLengthValid(size_t cb)
{
return cb <= UINT8_MAX;
}
virtual int decodeValue(const octets_t &src, size_t cb)
{
if (!isLengthValid(cb))
return VERR_INVALID_PARAMETER;
octets_t::const_iterator pos(src.begin());
extract(m_String, pos, cb);
m_fPresent = true;
return VINF_SUCCESS;
}
};
template<uint8_t _OptCode>
class OptString
: public OptStringBase
{
public:
static const uint8_t optcode = _OptCode;
OptString()
: OptStringBase(optcode) {}
explicit OptString(const std::string &aOptString)
: OptStringBase(optcode, aOptString) {}
explicit OptString(const DhcpClientMessage &req)
: OptStringBase(optcode, req) {}
static OptString *parse(const char *pcszValue)
{
return new OptString(pcszValue);
}
};
/*
* Option that contains a list of values of type T
*/
template <typename T>
class OptListBase
: public DhcpOption
{
public:
typedef std::vector<T> value_t;
protected:
std::vector<T> m_List;
explicit OptListBase(uint8_t aOptCode)
: DhcpOption(aOptCode, false), m_List() {}
OptListBase(uint8_t aOptCode, const T &aOptSingle)
: DhcpOption(aOptCode), m_List(1, aOptSingle) {}
OptListBase(uint8_t aOptCode, const std::vector<T> &aOptList)
: DhcpOption(aOptCode), m_List(aOptList) {}
OptListBase(uint8_t aOptCode, const DhcpClientMessage &req)
: DhcpOption(aOptCode, false), m_List()
{
decode(req);
}
public:
virtual OptListBase *clone() const
{
return new OptListBase(*this);
}
public:
std::vector<T> &value() { return m_List; }
const std::vector<T> &value() const { return m_List; }
protected:
virtual ssize_t encodeValue(octets_t &dst) const
{
const size_t cbItem = sizeof(T);
size_t cbValue = 0;
for (size_t i = 0; i < m_List.size(); ++i)
{
if (cbValue + cbItem > UINT8_MAX)
break;
append(dst, m_List[i]);
cbValue += cbItem;
}
return cbValue;
}
public:
static bool isLengthValid(size_t cb)
{
return cb % sizeof(T) == 0;
}
virtual int decodeValue(const octets_t &src, size_t cb)
{
if (!isLengthValid(cb))
return VERR_INVALID_PARAMETER;
m_List.erase(m_List.begin(), m_List.end());
octets_t::const_iterator pos(src.begin());
for (size_t i = 0; i < cb / sizeof(T); ++i)
{
T item;
extract(item, pos);
m_List.push_back(item);
}
m_fPresent = true;
return VINF_SUCCESS;
}
};
template<uint8_t _OptCode, typename T>
class OptList
: public OptListBase<T>
{
public:
using typename OptListBase<T>::value_t;
public:
static const uint8_t optcode = _OptCode;
OptList()
: OptListBase<T>(optcode) {}
explicit OptList(const T &aOptSingle)
: OptListBase<T>(optcode, aOptSingle) {}
explicit OptList(const std::vector<T> &aOptList)
: OptListBase<T>(optcode, aOptList) {}
explicit OptList(const DhcpClientMessage &req)
: OptListBase<T>(optcode, req) {}
static OptList *parse(const char *pcszValue)
{
typename OptListBase<T>::value_t v;
int rc = DhcpOption::parseList(v, pcszValue);
if (RT_FAILURE(rc) || v.empty())
return NULL;
return new OptList(v);
}
};
/*
* Options specified by raw binary data that we don't know how to
* interpret.
*/
class RawOption
: public DhcpOption
{
protected:
octets_t m_Data;
public:
explicit RawOption(uint8_t aOptCode)
: DhcpOption(aOptCode, false), m_Data() {}
RawOption(uint8_t aOptCode, const octets_t &aSrc)
: DhcpOption(aOptCode), m_Data(aSrc) {}
public:
virtual RawOption *clone() const
{
return new RawOption(*this);
}
protected:
virtual ssize_t encodeValue(octets_t &dst) const
{
dst.insert(dst.end(), m_Data.begin(), m_Data.end());
return m_Data.size();
}
virtual int decodeValue(const octets_t &src, size_t cb)
{
octets_t::const_iterator beg(src.begin());
octets_t data(beg, beg + cb);
m_Data.swap(data);
m_fPresent = true;
return VINF_SUCCESS;
}
public:
static RawOption *parse(uint8_t aOptCode, const char *pcszValue)
{
octets_t data;
int rc = DhcpOption::parseHex(data, pcszValue);
if (RT_FAILURE(rc))
return NULL;
return new RawOption(aOptCode, data);
}
};
/*
* Define the DHCP options we want to use.
*/
typedef OptValue<1, RTNETADDRIPV4> OptSubnetMask;
typedef OptValue<2, uint32_t> OptTimeOffset;
typedef OptList<3, RTNETADDRIPV4> OptRouter;
typedef OptList<4, RTNETADDRIPV4> OptTimeServer;
typedef OptList<6, RTNETADDRIPV4> OptDNS;
typedef OptString<12> OptHostName;
typedef OptString<15> OptDomainName;
typedef OptString<17> OptRootPath;
/* DHCP related options */
typedef OptList<43, uint8_t> OptVendorSpecificInfo;
typedef OptValue<50, RTNETADDRIPV4> OptRequestedAddress;
typedef OptValue<51, uint32_t> OptLeaseTime;
/* 52 - option overload is syntactic and handled internally */
typedef OptValue<53, uint8_t> OptMessageType;
typedef OptValue<54, RTNETADDRIPV4> OptServerId;
typedef OptList<55, uint8_t> OptParameterRequest;
typedef OptString<56> OptMessage;
typedef OptValue<57, uint16_t> OptMaxDHCPMessageSize;
typedef OptValue<58, uint32_t> OptRenewalTime;
typedef OptValue<59, uint32_t> OptRebindingTime;
typedef OptList<60, uint8_t> OptVendorClassId;
typedef OptList<61, uint8_t> OptClientId;
typedef OptString<66> OptTFTPServer; /* when overloaded */
typedef OptString<67> OptBootFileName; /* when overloaded */
typedef OptNoValue<80> OptRapidCommit; /* RFC4039 */
#endif /* !VBOX_INCLUDED_SRC_Dhcpd_DhcpOptions_h */
|