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