1 | /* $Id: DhcpOptions.h 106061 2024-09-16 14:03:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * DHCP server - DHCP options
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2017-2024 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 | #ifndef VBOX_INCLUDED_SRC_Dhcpd_DhcpOptions_h
|
---|
29 | #define VBOX_INCLUDED_SRC_Dhcpd_DhcpOptions_h
|
---|
30 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
31 | # pragma once
|
---|
32 | #endif
|
---|
33 |
|
---|
34 | #include "DhcpdInternal.h"
|
---|
35 |
|
---|
36 | #include <iprt/asm.h>
|
---|
37 | #include <iprt/err.h>
|
---|
38 | #include <iprt/net.h>
|
---|
39 | #include <iprt/string.h>
|
---|
40 | #include <iprt/cpp/ministring.h>
|
---|
41 |
|
---|
42 |
|
---|
43 | class DhcpClientMessage;
|
---|
44 |
|
---|
45 | typedef struct DhcpIpv4AddrAndMask
|
---|
46 | {
|
---|
47 | RTNETADDRIPV4 Ipv4;
|
---|
48 | RTNETADDRIPV4 Mask;
|
---|
49 | } DhcpIpv4AddrAndMask;
|
---|
50 |
|
---|
51 |
|
---|
52 | class DhcpOption
|
---|
53 | {
|
---|
54 | protected:
|
---|
55 | uint8_t m_OptCode;
|
---|
56 | bool m_fPresent;
|
---|
57 |
|
---|
58 | public:
|
---|
59 | explicit DhcpOption(uint8_t aOptCode)
|
---|
60 | : m_OptCode(aOptCode), m_fPresent(true)
|
---|
61 | {}
|
---|
62 |
|
---|
63 | DhcpOption(uint8_t aOptCode, bool fPresent)
|
---|
64 | : m_OptCode(aOptCode), m_fPresent(fPresent)
|
---|
65 | {}
|
---|
66 |
|
---|
67 | virtual DhcpOption *clone() const = 0;
|
---|
68 |
|
---|
69 | virtual ~DhcpOption()
|
---|
70 | {}
|
---|
71 |
|
---|
72 | public:
|
---|
73 | static DhcpOption *parse(uint8_t aOptCode, int aEnc, const char *pcszValue, int *prc = NULL);
|
---|
74 | static const char *name(uint8_t bOptcode);
|
---|
75 |
|
---|
76 | public:
|
---|
77 | uint8_t optcode() const RT_NOEXCEPT { return m_OptCode; }
|
---|
78 | bool present() const RT_NOEXCEPT { return m_fPresent; }
|
---|
79 |
|
---|
80 | public:
|
---|
81 | int encode(octets_t &dst) const;
|
---|
82 |
|
---|
83 | int decode(const rawopts_t &map);
|
---|
84 | int decode(const DhcpClientMessage &req);
|
---|
85 |
|
---|
86 | protected:
|
---|
87 | virtual ssize_t encodeValue(octets_t &dst) const = 0;
|
---|
88 | virtual int decodeValue(const octets_t &src, size_t cb) = 0;
|
---|
89 |
|
---|
90 | protected:
|
---|
91 | static const octets_t *findOption(const rawopts_t &aOptMap, uint8_t aOptCode);
|
---|
92 |
|
---|
93 | protected:
|
---|
94 | /** @name Serialization
|
---|
95 | * @{ */
|
---|
96 | static void append(octets_t &aDst, bool aValue)
|
---|
97 | {
|
---|
98 | uint8_t b = aValue ? 1 : 0;
|
---|
99 | aDst.push_back(b);
|
---|
100 | }
|
---|
101 |
|
---|
102 | static void append(octets_t &aDst, uint8_t aValue)
|
---|
103 | {
|
---|
104 | aDst.push_back(aValue);
|
---|
105 | }
|
---|
106 |
|
---|
107 | static void append(octets_t &aDst, uint16_t aValue)
|
---|
108 | {
|
---|
109 | RTUINT16U u16 = { RT_H2N_U16(aValue) };
|
---|
110 | aDst.insert(aDst.end(), u16.au8, u16.au8 + sizeof(aValue));
|
---|
111 | }
|
---|
112 |
|
---|
113 | static void append(octets_t &aDst, uint32_t aValue)
|
---|
114 | {
|
---|
115 | RTUINT32U u32 = { RT_H2N_U32(aValue) };
|
---|
116 | aDst.insert(aDst.end(), u32.au8, u32.au8 + sizeof(aValue));
|
---|
117 | }
|
---|
118 |
|
---|
119 | static void append(octets_t &aDst, RTNETADDRIPV4 aIPv4)
|
---|
120 | {
|
---|
121 | aDst.insert(aDst.end(), aIPv4.au8, aIPv4.au8 + sizeof(aIPv4));
|
---|
122 | }
|
---|
123 |
|
---|
124 | static void append(octets_t &aDst, DhcpIpv4AddrAndMask aIPv4)
|
---|
125 | {
|
---|
126 | aDst.insert(aDst.end(), (uint8_t *)&aIPv4, (uint8_t *)&aIPv4 + sizeof(aIPv4));
|
---|
127 | }
|
---|
128 |
|
---|
129 | static void append(octets_t &aDst, const char *pszString, size_t cb)
|
---|
130 | {
|
---|
131 | aDst.insert(aDst.end(), pszString, pszString + cb);
|
---|
132 | }
|
---|
133 |
|
---|
134 | static void append(octets_t &aDst, const RTCString &str)
|
---|
135 | {
|
---|
136 | append(aDst, str.c_str(), str.length());
|
---|
137 | }
|
---|
138 |
|
---|
139 | /* non-overloaded name to avoid ambiguity */
|
---|
140 | static void appendLength(octets_t &aDst, size_t cb)
|
---|
141 | {
|
---|
142 | append(aDst, static_cast<uint8_t>(cb));
|
---|
143 | }
|
---|
144 |
|
---|
145 | /** @} */
|
---|
146 |
|
---|
147 |
|
---|
148 | /** @name Deserialization
|
---|
149 | * @{ */
|
---|
150 | static void extract(bool &aValue, octets_t::const_iterator &pos)
|
---|
151 | {
|
---|
152 | aValue = *pos != 0;
|
---|
153 | pos += sizeof(uint8_t);
|
---|
154 | }
|
---|
155 |
|
---|
156 | static void extract(uint8_t &aValue, octets_t::const_iterator &pos)
|
---|
157 | {
|
---|
158 | aValue = *pos;
|
---|
159 | pos += sizeof(uint8_t);
|
---|
160 | }
|
---|
161 |
|
---|
162 | static void extract(uint16_t &aValue, octets_t::const_iterator &pos)
|
---|
163 | {
|
---|
164 | RTUINT16U u16;
|
---|
165 | memcpy(u16.au8, &pos[0], sizeof(uint16_t));
|
---|
166 | aValue = RT_N2H_U16(u16.u);
|
---|
167 | pos += sizeof(uint16_t);
|
---|
168 | }
|
---|
169 |
|
---|
170 | static void extract(uint32_t &aValue, octets_t::const_iterator &pos)
|
---|
171 | {
|
---|
172 | RTUINT32U u32;
|
---|
173 | memcpy(u32.au8, &pos[0], sizeof(uint32_t));
|
---|
174 | aValue = RT_N2H_U32(u32.u);
|
---|
175 | pos += sizeof(uint32_t);
|
---|
176 | }
|
---|
177 |
|
---|
178 | static void extract(RTNETADDRIPV4 &aValue, octets_t::const_iterator &pos)
|
---|
179 | {
|
---|
180 | memcpy(aValue.au8, &pos[0], sizeof(RTNETADDRIPV4));
|
---|
181 | pos += sizeof(RTNETADDRIPV4);
|
---|
182 | }
|
---|
183 |
|
---|
184 | static void extract(DhcpIpv4AddrAndMask &aValue, octets_t::const_iterator &pos)
|
---|
185 | {
|
---|
186 | memcpy(&aValue, &pos[0], sizeof(aValue));
|
---|
187 | pos += sizeof(aValue);
|
---|
188 | }
|
---|
189 |
|
---|
190 | #if 0 /** @todo fix me */
|
---|
191 | static void extract(RTCString &aString, octets_t::const_iterator &pos, size_t cb)
|
---|
192 | {
|
---|
193 | aString.replace(aString.begin(), aString.end(), &pos[0], &pos[cb]);
|
---|
194 | pos += cb;
|
---|
195 | }
|
---|
196 | #endif
|
---|
197 |
|
---|
198 | /** @} */
|
---|
199 |
|
---|
200 | /** @name Parse textual representation (e.g. in config file)
|
---|
201 | * @{ */
|
---|
202 | static int parse1(bool &aValue, const char *pcszValue);
|
---|
203 | static int parse1(uint8_t &aValue, const char *pcszValue);
|
---|
204 | static int parse1(uint16_t &aValue, const char *pcszValue);
|
---|
205 | static int parse1(uint32_t &aValue, const char *pcszValue);
|
---|
206 | static int parse1(RTNETADDRIPV4 &aValue, const char *pcszValue);
|
---|
207 | static int parse1(DhcpIpv4AddrAndMask &aValue, const char *pcszValue);
|
---|
208 |
|
---|
209 | template <typename a_Type> static int parseList(std::vector<a_Type> &aList, const char *pcszValue);
|
---|
210 |
|
---|
211 | static int parseHex(octets_t &aRawValue, const char *pcszValue);
|
---|
212 |
|
---|
213 | /** @} */
|
---|
214 | };
|
---|
215 |
|
---|
216 |
|
---|
217 | inline octets_t &operator<<(octets_t &dst, const DhcpOption &option)
|
---|
218 | {
|
---|
219 | option.encode(dst);
|
---|
220 | return dst;
|
---|
221 | }
|
---|
222 |
|
---|
223 |
|
---|
224 | #ifndef IN_VBOXSVC
|
---|
225 | optmap_t &operator<<(optmap_t &optmap, DhcpOption *option);
|
---|
226 | optmap_t &operator<<(optmap_t &optmap, const std::shared_ptr<DhcpOption> &option);
|
---|
227 | #endif
|
---|
228 |
|
---|
229 |
|
---|
230 |
|
---|
231 | /**
|
---|
232 | * Only for << OptEnd() syntactic sugar...
|
---|
233 | */
|
---|
234 | struct OptEnd {};
|
---|
235 | inline octets_t &operator<<(octets_t &dst, const OptEnd &end)
|
---|
236 | {
|
---|
237 | RT_NOREF(end);
|
---|
238 |
|
---|
239 | dst.push_back(RTNET_DHCP_OPT_END);
|
---|
240 | return dst;
|
---|
241 | }
|
---|
242 |
|
---|
243 |
|
---|
244 |
|
---|
245 | /**
|
---|
246 | * Option that has no value
|
---|
247 | */
|
---|
248 | class OptNoValueBase
|
---|
249 | : public DhcpOption
|
---|
250 | {
|
---|
251 | public:
|
---|
252 | explicit OptNoValueBase(uint8_t aOptCode)
|
---|
253 | : DhcpOption(aOptCode, false)
|
---|
254 | {}
|
---|
255 |
|
---|
256 | OptNoValueBase(uint8_t aOptCode, bool fPresent)
|
---|
257 | : DhcpOption(aOptCode, fPresent)
|
---|
258 | {}
|
---|
259 |
|
---|
260 | OptNoValueBase(uint8_t aOptCode, const DhcpClientMessage &req)
|
---|
261 | : DhcpOption(aOptCode, false)
|
---|
262 | {
|
---|
263 | decode(req);
|
---|
264 | }
|
---|
265 |
|
---|
266 | virtual OptNoValueBase *clone() const
|
---|
267 | {
|
---|
268 | return new OptNoValueBase(*this);
|
---|
269 | }
|
---|
270 |
|
---|
271 | protected:
|
---|
272 | virtual ssize_t encodeValue(octets_t &dst) const
|
---|
273 | {
|
---|
274 | RT_NOREF(dst);
|
---|
275 | return 0;
|
---|
276 | }
|
---|
277 |
|
---|
278 | public:
|
---|
279 | static bool isLengthValid(size_t cb)
|
---|
280 | {
|
---|
281 | return cb == 0;
|
---|
282 | }
|
---|
283 |
|
---|
284 | virtual int decodeValue(const octets_t &src, size_t cb)
|
---|
285 | {
|
---|
286 | RT_NOREF(src);
|
---|
287 |
|
---|
288 | if (!isLengthValid(cb))
|
---|
289 | return VERR_INVALID_PARAMETER;
|
---|
290 |
|
---|
291 | m_fPresent = true;
|
---|
292 | return VINF_SUCCESS;
|
---|
293 | }
|
---|
294 | };
|
---|
295 |
|
---|
296 | template <uint8_t _OptCode>
|
---|
297 | class OptNoValue
|
---|
298 | : public OptNoValueBase
|
---|
299 | {
|
---|
300 | public:
|
---|
301 | static const uint8_t optcode = _OptCode;
|
---|
302 |
|
---|
303 | OptNoValue()
|
---|
304 | : OptNoValueBase(optcode)
|
---|
305 | {}
|
---|
306 |
|
---|
307 | explicit OptNoValue(bool fPresent) /* there's no overloaded ctor with value */
|
---|
308 | : OptNoValueBase(optcode, fPresent)
|
---|
309 | {}
|
---|
310 |
|
---|
311 | explicit OptNoValue(const DhcpClientMessage &req)
|
---|
312 | : OptNoValueBase(optcode, req)
|
---|
313 | {}
|
---|
314 | };
|
---|
315 |
|
---|
316 |
|
---|
317 |
|
---|
318 | /*
|
---|
319 | * Option that contains single value of fixed-size type T
|
---|
320 | */
|
---|
321 | template <typename T>
|
---|
322 | class OptValueBase
|
---|
323 | : public DhcpOption
|
---|
324 | {
|
---|
325 | public:
|
---|
326 | typedef T value_t;
|
---|
327 |
|
---|
328 | protected:
|
---|
329 | T m_Value;
|
---|
330 |
|
---|
331 | explicit OptValueBase(uint8_t aOptCode)
|
---|
332 | : DhcpOption(aOptCode, false), m_Value()
|
---|
333 | {}
|
---|
334 |
|
---|
335 | OptValueBase(uint8_t aOptCode, const T &aOptValue)
|
---|
336 | : DhcpOption(aOptCode), m_Value(aOptValue)
|
---|
337 | {}
|
---|
338 |
|
---|
339 | OptValueBase(uint8_t aOptCode, const DhcpClientMessage &req)
|
---|
340 | : DhcpOption(aOptCode, false), m_Value()
|
---|
341 | {
|
---|
342 | decode(req);
|
---|
343 | }
|
---|
344 |
|
---|
345 | public:
|
---|
346 | virtual OptValueBase *clone() const
|
---|
347 | {
|
---|
348 | return new OptValueBase(*this);
|
---|
349 | }
|
---|
350 |
|
---|
351 | public:
|
---|
352 | T &value() { return m_Value; }
|
---|
353 | const T &value() const { return m_Value; }
|
---|
354 |
|
---|
355 | protected:
|
---|
356 | virtual ssize_t encodeValue(octets_t &dst) const
|
---|
357 | {
|
---|
358 | append(dst, m_Value);
|
---|
359 | return sizeof(T);
|
---|
360 | }
|
---|
361 |
|
---|
362 | public:
|
---|
363 | static bool isLengthValid(size_t cb)
|
---|
364 | {
|
---|
365 | return cb == sizeof(T);
|
---|
366 | }
|
---|
367 |
|
---|
368 | virtual int decodeValue(const octets_t &src, size_t cb)
|
---|
369 | {
|
---|
370 | if (!isLengthValid(cb))
|
---|
371 | return VERR_INVALID_PARAMETER;
|
---|
372 |
|
---|
373 | octets_t::const_iterator pos(src.begin());
|
---|
374 | extract(m_Value, pos);
|
---|
375 |
|
---|
376 | m_fPresent = true;
|
---|
377 | return VINF_SUCCESS;
|
---|
378 | }
|
---|
379 | };
|
---|
380 |
|
---|
381 | template<uint8_t _OptCode, typename T>
|
---|
382 | class OptValue
|
---|
383 | : public OptValueBase<T>
|
---|
384 | {
|
---|
385 | public:
|
---|
386 | using typename OptValueBase<T>::value_t;
|
---|
387 |
|
---|
388 | public:
|
---|
389 | static const uint8_t optcode = _OptCode;
|
---|
390 |
|
---|
391 | OptValue()
|
---|
392 | : OptValueBase<T>(optcode)
|
---|
393 | {}
|
---|
394 |
|
---|
395 | explicit OptValue(const T &aOptValue)
|
---|
396 | : OptValueBase<T>(optcode, aOptValue)
|
---|
397 | {}
|
---|
398 |
|
---|
399 | explicit OptValue(const DhcpClientMessage &req)
|
---|
400 | : OptValueBase<T>(optcode, req)
|
---|
401 | {}
|
---|
402 |
|
---|
403 | static OptValue *parse(const char *pcszValue, int *prc)
|
---|
404 | {
|
---|
405 | typename OptValueBase<T>::value_t v;
|
---|
406 | int rc = DhcpOption::parse1(v, pcszValue);
|
---|
407 | *prc = rc;
|
---|
408 | if (RT_SUCCESS(rc))
|
---|
409 | return new OptValue(v);
|
---|
410 | return NULL;
|
---|
411 | }
|
---|
412 | };
|
---|
413 |
|
---|
414 |
|
---|
415 |
|
---|
416 | /**
|
---|
417 | * Option that contains a string.
|
---|
418 | */
|
---|
419 | class OptStringBase
|
---|
420 | : public DhcpOption
|
---|
421 | {
|
---|
422 | public:
|
---|
423 | typedef RTCString value_t;
|
---|
424 |
|
---|
425 | protected:
|
---|
426 | RTCString m_String;
|
---|
427 |
|
---|
428 | explicit OptStringBase(uint8_t aOptCode)
|
---|
429 | : DhcpOption(aOptCode, false), m_String()
|
---|
430 | {}
|
---|
431 |
|
---|
432 | OptStringBase(uint8_t aOptCode, const RTCString &aOptString)
|
---|
433 | : DhcpOption(aOptCode), m_String(aOptString)
|
---|
434 | {}
|
---|
435 |
|
---|
436 | OptStringBase(uint8_t aOptCode, const DhcpClientMessage &req)
|
---|
437 | : DhcpOption(aOptCode, false), m_String()
|
---|
438 | {
|
---|
439 | decode(req);
|
---|
440 | }
|
---|
441 |
|
---|
442 | public:
|
---|
443 | virtual OptStringBase *clone() const
|
---|
444 | {
|
---|
445 | return new OptStringBase(*this);
|
---|
446 | }
|
---|
447 |
|
---|
448 | public:
|
---|
449 | RTCString &value() { return m_String; }
|
---|
450 | const RTCString &value() const { return m_String; }
|
---|
451 |
|
---|
452 | protected:
|
---|
453 | virtual ssize_t encodeValue(octets_t &dst) const
|
---|
454 | {
|
---|
455 | if (!isLengthValid(m_String.length()))
|
---|
456 | return -1;
|
---|
457 |
|
---|
458 | append(dst, m_String);
|
---|
459 | return (ssize_t)m_String.length();
|
---|
460 | }
|
---|
461 |
|
---|
462 | public:
|
---|
463 | static bool isLengthValid(size_t cb)
|
---|
464 | {
|
---|
465 | return cb <= UINT8_MAX;
|
---|
466 | }
|
---|
467 |
|
---|
468 | virtual int decodeValue(const octets_t &src, size_t cb)
|
---|
469 | {
|
---|
470 | if (!isLengthValid(cb))
|
---|
471 | return VERR_INVALID_PARAMETER;
|
---|
472 |
|
---|
473 | int rc = m_String.assignNoThrow((char *)&src.front(), cb); /** @todo encoding. */
|
---|
474 | m_fPresent = true;
|
---|
475 | return rc;
|
---|
476 | }
|
---|
477 | };
|
---|
478 |
|
---|
479 | template<uint8_t _OptCode>
|
---|
480 | class OptString
|
---|
481 | : public OptStringBase
|
---|
482 | {
|
---|
483 | public:
|
---|
484 | static const uint8_t optcode = _OptCode;
|
---|
485 |
|
---|
486 | OptString()
|
---|
487 | : OptStringBase(optcode)
|
---|
488 | {}
|
---|
489 |
|
---|
490 | explicit OptString(const RTCString &aOptString)
|
---|
491 | : OptStringBase(optcode, aOptString)
|
---|
492 | {}
|
---|
493 |
|
---|
494 | explicit OptString(const DhcpClientMessage &req)
|
---|
495 | : OptStringBase(optcode, req)
|
---|
496 | {}
|
---|
497 |
|
---|
498 | static OptString *parse(const char *pcszValue, int *prc)
|
---|
499 | {
|
---|
500 | *prc = VINF_SUCCESS;
|
---|
501 | return new OptString(pcszValue);
|
---|
502 | }
|
---|
503 | };
|
---|
504 |
|
---|
505 |
|
---|
506 |
|
---|
507 | /*
|
---|
508 | * Option that contains a list of values of type T
|
---|
509 | */
|
---|
510 | template <typename T>
|
---|
511 | class OptListBase
|
---|
512 | : public DhcpOption
|
---|
513 | {
|
---|
514 | public:
|
---|
515 | typedef std::vector<T> value_t;
|
---|
516 |
|
---|
517 | protected:
|
---|
518 | std::vector<T> m_List;
|
---|
519 |
|
---|
520 | explicit OptListBase(uint8_t aOptCode)
|
---|
521 | : DhcpOption(aOptCode, false), m_List()
|
---|
522 | {}
|
---|
523 |
|
---|
524 | OptListBase(uint8_t aOptCode, const T &aOptSingle)
|
---|
525 | : DhcpOption(aOptCode), m_List(1, aOptSingle)
|
---|
526 | {}
|
---|
527 |
|
---|
528 | OptListBase(uint8_t aOptCode, const std::vector<T> &aOptList)
|
---|
529 | : DhcpOption(aOptCode), m_List(aOptList)
|
---|
530 | {}
|
---|
531 |
|
---|
532 | OptListBase(uint8_t aOptCode, const DhcpClientMessage &req)
|
---|
533 | : DhcpOption(aOptCode, false), m_List()
|
---|
534 | {
|
---|
535 | decode(req);
|
---|
536 | }
|
---|
537 |
|
---|
538 | public:
|
---|
539 | virtual OptListBase *clone() const
|
---|
540 | {
|
---|
541 | return new OptListBase(*this);
|
---|
542 | }
|
---|
543 |
|
---|
544 | public:
|
---|
545 | std::vector<T> &value() { return m_List; }
|
---|
546 | const std::vector<T> &value() const { return m_List; }
|
---|
547 |
|
---|
548 | protected:
|
---|
549 | virtual ssize_t encodeValue(octets_t &dst) const
|
---|
550 | {
|
---|
551 | const size_t cbItem = sizeof(T);
|
---|
552 | size_t cbValue = 0;
|
---|
553 |
|
---|
554 | for (size_t i = 0; i < m_List.size(); ++i)
|
---|
555 | {
|
---|
556 | if (cbValue + cbItem > UINT8_MAX)
|
---|
557 | break;
|
---|
558 |
|
---|
559 | append(dst, m_List[i]);
|
---|
560 | cbValue += cbItem;
|
---|
561 | }
|
---|
562 |
|
---|
563 | return (ssize_t)cbValue;
|
---|
564 | }
|
---|
565 |
|
---|
566 | public:
|
---|
567 | static bool isLengthValid(size_t cb)
|
---|
568 | {
|
---|
569 | return cb % sizeof(T) == 0;
|
---|
570 | }
|
---|
571 |
|
---|
572 | virtual int decodeValue(const octets_t &src, size_t cb)
|
---|
573 | {
|
---|
574 | if (!isLengthValid(cb))
|
---|
575 | return VERR_INVALID_PARAMETER;
|
---|
576 |
|
---|
577 | m_List.erase(m_List.begin(), m_List.end());
|
---|
578 |
|
---|
579 | octets_t::const_iterator pos(src.begin());
|
---|
580 | for (size_t i = 0; i < cb / sizeof(T); ++i)
|
---|
581 | {
|
---|
582 | T item;
|
---|
583 | extract(item, pos);
|
---|
584 | m_List.push_back(item);
|
---|
585 | }
|
---|
586 | m_fPresent = true;
|
---|
587 | return VINF_SUCCESS;
|
---|
588 | }
|
---|
589 | };
|
---|
590 |
|
---|
591 | template<uint8_t _OptCode, typename T>
|
---|
592 | class OptList
|
---|
593 | : public OptListBase<T>
|
---|
594 |
|
---|
595 | {
|
---|
596 | public:
|
---|
597 | using typename OptListBase<T>::value_t;
|
---|
598 |
|
---|
599 | public:
|
---|
600 | static const uint8_t optcode = _OptCode;
|
---|
601 |
|
---|
602 | OptList()
|
---|
603 | : OptListBase<T>(optcode)
|
---|
604 | {}
|
---|
605 |
|
---|
606 | explicit OptList(const T &aOptSingle)
|
---|
607 | : OptListBase<T>(optcode, aOptSingle)
|
---|
608 | {}
|
---|
609 |
|
---|
610 | explicit OptList(const std::vector<T> &aOptList)
|
---|
611 | : OptListBase<T>(optcode, aOptList)
|
---|
612 | {}
|
---|
613 |
|
---|
614 | explicit OptList(const DhcpClientMessage &req)
|
---|
615 | : OptListBase<T>(optcode, req)
|
---|
616 | {}
|
---|
617 |
|
---|
618 | static OptList *parse(const char *pcszValue, int *prc)
|
---|
619 | {
|
---|
620 | typename OptListBase<T>::value_t v;
|
---|
621 | int rc = DhcpOption::parseList<T>(v, pcszValue);
|
---|
622 | if (RT_SUCCESS(rc))
|
---|
623 | {
|
---|
624 | if (!v.empty())
|
---|
625 | {
|
---|
626 | *prc = rc;
|
---|
627 | return new OptList(v);
|
---|
628 | }
|
---|
629 | rc = VERR_NO_DATA;
|
---|
630 | }
|
---|
631 | *prc = rc;
|
---|
632 | return NULL;
|
---|
633 | }
|
---|
634 | };
|
---|
635 |
|
---|
636 |
|
---|
637 | template<uint8_t _OptCode, typename T>
|
---|
638 | class OptPairList
|
---|
639 | : public OptListBase<T>
|
---|
640 |
|
---|
641 | {
|
---|
642 | public:
|
---|
643 | using typename OptListBase<T>::value_t;
|
---|
644 |
|
---|
645 | public:
|
---|
646 | static const uint8_t optcode = _OptCode;
|
---|
647 |
|
---|
648 | OptPairList()
|
---|
649 | : OptListBase<T>(optcode)
|
---|
650 | {}
|
---|
651 |
|
---|
652 | explicit OptPairList(const T &aOptSingle)
|
---|
653 | : OptListBase<T>(optcode, aOptSingle)
|
---|
654 | {}
|
---|
655 |
|
---|
656 | explicit OptPairList(const std::vector<T> &aOptList)
|
---|
657 | : OptListBase<T>(optcode, aOptList)
|
---|
658 | {}
|
---|
659 |
|
---|
660 | explicit OptPairList(const DhcpClientMessage &req)
|
---|
661 | : OptListBase<T>(optcode, req)
|
---|
662 | {}
|
---|
663 |
|
---|
664 | static OptPairList *parse(const char *pcszValue, int *prc)
|
---|
665 | {
|
---|
666 | typename OptListBase<T>::value_t v;
|
---|
667 | int rc = DhcpOption::parseList<T>(v, pcszValue);
|
---|
668 | if (RT_SUCCESS(rc))
|
---|
669 | {
|
---|
670 | if (!v.empty())
|
---|
671 | {
|
---|
672 | if ((v.size() & 1) == 0)
|
---|
673 | {
|
---|
674 | *prc = rc;
|
---|
675 | return new OptPairList(v);
|
---|
676 | }
|
---|
677 | rc = VERR_UNEVEN_INPUT;
|
---|
678 | }
|
---|
679 | else
|
---|
680 | rc = VERR_NO_DATA;
|
---|
681 | }
|
---|
682 | *prc = rc;
|
---|
683 | return NULL;
|
---|
684 | }
|
---|
685 | };
|
---|
686 |
|
---|
687 |
|
---|
688 | /*
|
---|
689 | * Options specified by raw binary data that we don't know how to
|
---|
690 | * interpret.
|
---|
691 | */
|
---|
692 | class RawOption
|
---|
693 | : public DhcpOption
|
---|
694 | {
|
---|
695 | protected:
|
---|
696 | octets_t m_Data;
|
---|
697 |
|
---|
698 | public:
|
---|
699 | explicit RawOption(uint8_t aOptCode)
|
---|
700 | : DhcpOption(aOptCode, false), m_Data()
|
---|
701 | {}
|
---|
702 |
|
---|
703 | RawOption(uint8_t aOptCode, const octets_t &aSrc)
|
---|
704 | : DhcpOption(aOptCode), m_Data(aSrc)
|
---|
705 | {}
|
---|
706 |
|
---|
707 | public:
|
---|
708 | virtual RawOption *clone() const
|
---|
709 | {
|
---|
710 | return new RawOption(*this);
|
---|
711 | }
|
---|
712 |
|
---|
713 |
|
---|
714 | protected:
|
---|
715 | virtual ssize_t encodeValue(octets_t &dst) const
|
---|
716 | {
|
---|
717 | dst.insert(dst.end(), m_Data.begin(), m_Data.end());
|
---|
718 | return (ssize_t)m_Data.size();
|
---|
719 | }
|
---|
720 |
|
---|
721 | virtual int decodeValue(const octets_t &src, size_t cb)
|
---|
722 | {
|
---|
723 | octets_t::const_iterator beg(src.begin());
|
---|
724 | octets_t data(beg, beg + (ssize_t)cb);
|
---|
725 | m_Data.swap(data);
|
---|
726 |
|
---|
727 | m_fPresent = true;
|
---|
728 | return VINF_SUCCESS;
|
---|
729 | }
|
---|
730 |
|
---|
731 | public:
|
---|
732 | static RawOption *parse(uint8_t aOptCode, const char *pcszValue, int *prc)
|
---|
733 | {
|
---|
734 | octets_t data;
|
---|
735 | int rc = DhcpOption::parseHex(data, pcszValue);
|
---|
736 | *prc = rc;
|
---|
737 | if (RT_SUCCESS(rc))
|
---|
738 | return new RawOption(aOptCode, data);
|
---|
739 | return NULL;
|
---|
740 | }
|
---|
741 | };
|
---|
742 |
|
---|
743 |
|
---|
744 |
|
---|
745 | /** @name The DHCP options types.
|
---|
746 | * @{
|
---|
747 | */
|
---|
748 | typedef OptValue<1, RTNETADDRIPV4> OptSubnetMask;
|
---|
749 | typedef OptValue<2, uint32_t> OptTimeOffset;
|
---|
750 | typedef OptList<3, RTNETADDRIPV4> OptRouters;
|
---|
751 | typedef OptList<4, RTNETADDRIPV4> OptTimeServers;
|
---|
752 | typedef OptList<5, RTNETADDRIPV4> OptNameServers;
|
---|
753 | typedef OptList<6, RTNETADDRIPV4> OptDNSes;
|
---|
754 | typedef OptList<7, RTNETADDRIPV4> OptLogServers;
|
---|
755 | typedef OptList<8, RTNETADDRIPV4> OptCookieServers;
|
---|
756 | typedef OptList<9, RTNETADDRIPV4> OptLPRServers;
|
---|
757 | typedef OptList<10, RTNETADDRIPV4> OptImpressServers;
|
---|
758 | typedef OptList<11, RTNETADDRIPV4> OptResourceLocationServers;
|
---|
759 | typedef OptString<12> OptHostName;
|
---|
760 | typedef OptValue<13, uint16_t> OptBootFileSize;
|
---|
761 | typedef OptString<14> OptMeritDumpFile;
|
---|
762 | typedef OptString<15> OptDomainName;
|
---|
763 | typedef OptValue<16, RTNETADDRIPV4> OptSwapServer;
|
---|
764 | typedef OptString<17> OptRootPath;
|
---|
765 | typedef OptString<18> OptExtensionPath;
|
---|
766 | typedef OptValue<19, bool> OptIPForwarding;
|
---|
767 | typedef OptValue<20, bool> OptNonLocalSourceRouting;
|
---|
768 | typedef OptList<21, DhcpIpv4AddrAndMask> OptPolicyFilter;
|
---|
769 | typedef OptValue<22, uint16_t> OptMaxDgramReassemblySize;
|
---|
770 | typedef OptValue<23, uint16_t> OptDefaultIPTTL;
|
---|
771 | typedef OptValue<24, uint32_t> OptPathMTUAgingTimeout;
|
---|
772 | typedef OptList<25, uint16_t> OptPathMTUPlateauTable;
|
---|
773 | typedef OptValue<26, uint16_t> OptInterfaceMTU;
|
---|
774 | typedef OptValue<27, bool> OptAllSubnetsAreLocal;
|
---|
775 | typedef OptValue<28, RTNETADDRIPV4> OptBroadcastAddress;
|
---|
776 | typedef OptValue<29, bool> OptPerformMaskDiscovery;
|
---|
777 | typedef OptValue<30, bool> OptMaskSupplier;
|
---|
778 | typedef OptValue<31, bool> OptPerformRouterDiscovery;
|
---|
779 | typedef OptValue<32, RTNETADDRIPV4> OptRouterSolicitationAddress;
|
---|
780 | typedef OptPairList<33, RTNETADDRIPV4> OptStaticRoute;
|
---|
781 | typedef OptValue<34, bool> OptTrailerEncapsulation;
|
---|
782 | typedef OptValue<35, uint32_t> OptARPCacheTimeout;
|
---|
783 | typedef OptValue<36, bool> OptEthernetEncapsulation;
|
---|
784 | typedef OptValue<37, uint8_t> OptTCPDefaultTTL;
|
---|
785 | typedef OptValue<38, uint32_t> OptTCPKeepaliveInterval;
|
---|
786 | typedef OptValue<39, bool> OptTCPKeepaliveGarbage;
|
---|
787 | typedef OptString<40> OptNISDomain;
|
---|
788 | typedef OptList<41, RTNETADDRIPV4> OptNISServers;
|
---|
789 | typedef OptList<42, RTNETADDRIPV4> OptNTPServers;
|
---|
790 | /* DHCP related options: */
|
---|
791 | typedef OptList<43, uint8_t> OptVendorSpecificInfo;
|
---|
792 | typedef OptList<44, RTNETADDRIPV4> OptNetBIOSNameServers;
|
---|
793 | typedef OptList<45, RTNETADDRIPV4> OptNetBIOSDatagramServers;
|
---|
794 | typedef OptValue<46, uint8_t> OptNetBIOSNodeType;
|
---|
795 | typedef OptList<47, uint8_t> OptNetBIOSScope; /**< uint8_t or string? */
|
---|
796 | typedef OptList<48, RTNETADDRIPV4> OptXWindowsFontServers;
|
---|
797 | typedef OptList<49, RTNETADDRIPV4> OptXWindowsDisplayManager;
|
---|
798 | typedef OptValue<50, RTNETADDRIPV4> OptRequestedAddress;
|
---|
799 | typedef OptValue<51, uint32_t> OptLeaseTime;
|
---|
800 | /* 52 - option overload is syntactic and handled internally */
|
---|
801 | typedef OptValue<53, uint8_t> OptMessageType;
|
---|
802 | typedef OptValue<54, RTNETADDRIPV4> OptServerId;
|
---|
803 | typedef OptList<55, uint8_t> OptParameterRequest;
|
---|
804 | typedef OptString<56> OptMessage;
|
---|
805 | typedef OptValue<57, uint16_t> OptMaxDHCPMessageSize;
|
---|
806 | typedef OptValue<58, uint32_t> OptRenewalTime;
|
---|
807 | typedef OptValue<59, uint32_t> OptRebindingTime;
|
---|
808 | typedef OptList<60, uint8_t> OptVendorClassId;
|
---|
809 | typedef OptList<61, uint8_t> OptClientId;
|
---|
810 | typedef OptString<62> OptNetWareIPDomainName; /**< RFC2242 */
|
---|
811 | typedef OptList<63, uint8_t> OptNetWareIPInformation; /**< complicated, so just byte list for now. RFC2242 */
|
---|
812 | typedef OptString<64> OptNISPlusDomain;
|
---|
813 | typedef OptString<65> OptNISPlusServers;
|
---|
814 | typedef OptString<66> OptTFTPServerName; /**< when overloaded */
|
---|
815 | typedef OptString<67> OptBootfileName; /**< when overloaded */
|
---|
816 | typedef OptList<68, RTNETADDRIPV4> OptMobileIPHomeAgents;
|
---|
817 | typedef OptList<69, RTNETADDRIPV4> OptSMTPServers;
|
---|
818 | typedef OptList<70, RTNETADDRIPV4> OptPOP3Servers;
|
---|
819 | typedef OptList<71, RTNETADDRIPV4> OptNNTPServers;
|
---|
820 | typedef OptList<72, RTNETADDRIPV4> OptWWWServers;
|
---|
821 | typedef OptList<73, RTNETADDRIPV4> OptFingerServers;
|
---|
822 | typedef OptList<74, RTNETADDRIPV4> OptIRCServers;
|
---|
823 | typedef OptList<75, RTNETADDRIPV4> OptStreetTalkServers;
|
---|
824 | typedef OptList<76, RTNETADDRIPV4> OptSTDAServers;
|
---|
825 | typedef OptList<77, uint8_t> OptUserClassId;
|
---|
826 | typedef OptList<78, uint8_t> OptSLPDirectoryAgent; /**< complicated, so just byte list for now. RFC2610 */
|
---|
827 | typedef OptList<79, uint8_t> OptSLPServiceScope; /**< complicated, so just byte list for now. RFC2610 */
|
---|
828 | typedef OptNoValue<80> OptRapidCommit; /**< RFC4039 */
|
---|
829 | typedef OptList<119, uint8_t> OptDomainSearch; /**< RFC3397 */
|
---|
830 | /** @} */
|
---|
831 |
|
---|
832 | #endif /* !VBOX_INCLUDED_SRC_Dhcpd_DhcpOptions_h */
|
---|