VirtualBox

source: vbox/trunk/src/VBox/Main/include/DHCPConfigImpl.h@ 79822

Last change on this file since 79822 was 79822, checked in by vboxsync, 5 years ago

Main/DHCPServer: Fixed MAC resolving of MachineMAC configs. bugref:9288

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 17.9 KB
Line 
1/* $Id: DHCPConfigImpl.h 79822 2019-07-16 20:12:28Z vboxsync $ */
2/** @file
3 * VirtualBox Main - IDHCPConfig, IDHCPConfigGlobal, IDHCPConfigGroup, IDHCPConfigIndividual header.
4 */
5
6/*
7 * Copyright (C) 2006-2019 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18#ifndef MAIN_INCLUDED_DHCPConfigImpl_h
19#define MAIN_INCLUDED_DHCPConfigImpl_h
20#ifndef RT_WITHOUT_PRAGMA_ONCE
21# pragma once
22#endif
23
24#include "DHCPGlobalConfigWrap.h"
25#include "DHCPGroupConditionWrap.h"
26#include "DHCPGroupConfigWrap.h"
27#include "DHCPIndividualConfigWrap.h"
28#include <VBox/settings.h>
29
30
31class DHCPServer;
32class DHCPGroupConfig;
33
34
35/**
36 * Base class for the a DHCP configration layer.
37 *
38 * This does not inherit from DHCPConfigWrap because its children need to
39 * inherit from children of DHCPConfigWrap, which smells like trouble and thus
40 * wasn't even attempted. Instead, we have a hack for passing a pointer that we
41 * can call setError and such on.
42 */
43class DHCPConfig
44{
45protected:
46 /** Config scope (global, group, vm+nic, mac). */
47 DHCPConfigScope_T const m_enmScope;
48 /** Minimum lease time. */
49 ULONG m_secMinLeaseTime;
50 /** Default lease time. */
51 ULONG m_secDefaultLeaseTime;
52 /** Maximum lease time. */
53 ULONG m_secMaxLeaseTime;
54 /** DHCP option map. */
55 settings::DhcpOptionMap m_OptionMap;
56 /** The DHCP server parent (weak). */
57 DHCPServer * const m_pParent;
58 /** The DHCP server parent (weak). */
59 VirtualBox * const m_pVirtualBox;
60private:
61 /** For setError and such. */
62 VirtualBoxBase * const m_pHack;
63
64protected:
65 /** @name Constructors and destructors.
66 * @{ */
67 DHCPConfig(DHCPConfigScope_T a_enmScope, VirtualBoxBase *a_pHack)
68 : m_enmScope(a_enmScope), m_secMinLeaseTime(0), m_secDefaultLeaseTime(0), m_secMaxLeaseTime(0), m_OptionMap()
69 , m_pParent(NULL), m_pVirtualBox(NULL), m_pHack(a_pHack)
70 {}
71 DHCPConfig();
72 virtual ~DHCPConfig()
73 {}
74 HRESULT i_initWithDefaults(VirtualBox *a_pVirtualBox, DHCPServer *a_pParent);
75 HRESULT i_initWithSettings(VirtualBox *a_pVirtualBox, DHCPServer *a_pParent, const settings::DHCPConfig &rConfig);
76 /** @} */
77
78 /** @name IDHCPConfig properties
79 * @{ */
80 HRESULT i_getScope(DHCPConfigScope_T *aScope);
81 HRESULT i_getMinLeaseTime(ULONG *aMinLeaseTime);
82 HRESULT i_setMinLeaseTime(ULONG aMinLeaseTime);
83 HRESULT i_getDefaultLeaseTime(ULONG *aDefaultLeaseTime);
84 HRESULT i_setDefaultLeaseTime(ULONG aDefaultLeaseTime);
85 HRESULT i_getMaxLeaseTime(ULONG *aMaxLeaseTime);
86 HRESULT i_setMaxLeaseTime(ULONG aMaxLeaseTime);
87 /** @} */
88
89public:
90 /** @name IDHCPConfig methods
91 * @note public because the DHCPServer needs them for 6.0 interfaces.
92 * @todo Make protected again when IDHCPServer is cleaned up.
93 * @{ */
94 virtual HRESULT i_setOption(DhcpOpt_T aOption, DHCPOptionEncoding_T aEncoding, const com::Utf8Str &aValue);
95
96 virtual HRESULT i_removeOption(DhcpOpt_T aOption);
97 virtual HRESULT i_removeAllOptions();
98 HRESULT i_getOption(DhcpOpt_T aOption, DHCPOptionEncoding_T *aEncoding, com::Utf8Str &aValue);
99 HRESULT i_getAllOptions(std::vector<DhcpOpt_T> &aOptions, std::vector<DHCPOptionEncoding_T> &aEncodings,
100 std::vector<com::Utf8Str> &aValues);
101 virtual HRESULT i_remove();
102 /** @} */
103
104
105public:
106 HRESULT i_doWriteConfig();
107 HRESULT i_saveSettings(settings::DHCPConfig &a_rDst);
108 DHCPConfigScope_T i_getScope() const RT_NOEXCEPT { return m_enmScope; }
109 virtual void i_writeDhcpdConfig(xml::ElementNode *pElm);
110};
111
112
113/**
114 * Global DHCP configuration.
115 */
116class DHCPGlobalConfig : public DHCPGlobalConfigWrap, public DHCPConfig
117{
118public:
119 /** @name Constructors and destructors.
120 * @{ */
121 DHCPGlobalConfig()
122 : DHCPConfig(DHCPConfigScope_Global, this)
123 { }
124 HRESULT FinalConstruct()
125 {
126 return BaseFinalConstruct();
127 }
128 void FinalRelease()
129 {
130 uninit();
131 BaseFinalRelease();
132 }
133 HRESULT initWithDefaults(VirtualBox *a_pVirtualBox, DHCPServer *a_pParent);
134 HRESULT initWithSettings(VirtualBox *a_pVirtualBox, DHCPServer *a_pParent, const settings::DHCPConfig &rConfig);
135 void uninit();
136 /** @} */
137
138 HRESULT i_saveSettings(settings::DHCPConfig &a_rDst);
139 HRESULT i_getNetworkMask(com::Utf8Str &a_rDst);
140 HRESULT i_setNetworkMask(const com::Utf8Str &a_rSrc);
141
142protected:
143 /** @name wrapped IDHCPConfig properties
144 * @{ */
145 HRESULT getScope(DHCPConfigScope_T *aScope) RT_OVERRIDE { return i_getScope(aScope); }
146 HRESULT getMinLeaseTime(ULONG *aMinLeaseTime) RT_OVERRIDE { return i_getMinLeaseTime(aMinLeaseTime); }
147 HRESULT setMinLeaseTime(ULONG aMinLeaseTime) RT_OVERRIDE { return i_setMinLeaseTime(aMinLeaseTime); }
148 HRESULT getDefaultLeaseTime(ULONG *aDefaultLeaseTime) RT_OVERRIDE { return i_getDefaultLeaseTime(aDefaultLeaseTime); }
149 HRESULT setDefaultLeaseTime(ULONG aDefaultLeaseTime) RT_OVERRIDE { return i_setDefaultLeaseTime(aDefaultLeaseTime); }
150 HRESULT getMaxLeaseTime(ULONG *aMaxLeaseTime) RT_OVERRIDE { return i_getMaxLeaseTime(aMaxLeaseTime); }
151 HRESULT setMaxLeaseTime(ULONG aMaxLeaseTime) RT_OVERRIDE { return i_setMaxLeaseTime(aMaxLeaseTime); }
152 /** @} */
153
154 /** @name wrapped IDHCPConfig methods
155 * @{ */
156 HRESULT setOption(DhcpOpt_T aOption, DHCPOptionEncoding_T aEncoding, const com::Utf8Str &aValue) RT_OVERRIDE
157 {
158 return i_setOption(aOption, aEncoding, aValue);
159 }
160
161 HRESULT removeOption(DhcpOpt_T aOption) RT_OVERRIDE
162 {
163 return i_removeOption(aOption);
164 }
165
166 HRESULT removeAllOptions() RT_OVERRIDE
167 {
168 return i_removeAllOptions();
169 }
170
171 HRESULT getOption(DhcpOpt_T aOption, DHCPOptionEncoding_T *aEncoding, com::Utf8Str &aValue) RT_OVERRIDE
172 {
173 return i_getOption(aOption, aEncoding, aValue);
174 }
175
176 HRESULT getAllOptions(std::vector<DhcpOpt_T> &aOptions, std::vector<DHCPOptionEncoding_T> &aEncodings,
177 std::vector<com::Utf8Str> &aValues) RT_OVERRIDE
178 {
179 return i_getAllOptions(aOptions, aEncodings, aValues);
180 }
181
182 HRESULT remove() RT_OVERRIDE
183 {
184 return i_remove();
185 }
186 /** @} */
187
188public:
189 HRESULT i_setOption(DhcpOpt_T aOption, DHCPOptionEncoding_T aEncoding, const com::Utf8Str &aValue) RT_OVERRIDE;
190 HRESULT i_removeOption(DhcpOpt_T aOption) RT_OVERRIDE;
191 HRESULT i_removeAllOptions() RT_OVERRIDE;
192 HRESULT i_remove() RT_OVERRIDE;
193};
194
195
196/**
197 * DHCP Group inclusion/exclusion condition.
198 */
199class DHCPGroupCondition : public DHCPGroupConditionWrap
200{
201private:
202 /** Inclusive or exclusive condition. */
203 bool m_fInclusive;
204 /** The condition type (or how m_strValue should be interpreted). */
205 DHCPGroupConditionType_T m_enmType;
206 /** The value. Interpreted according to m_enmType. */
207 com::Utf8Str m_strValue;
208 /** Pointer to the parent (weak). */
209 DHCPGroupConfig *m_pParent;
210
211public:
212 /** @name Constructors and destructors.
213 * @{ */
214 DHCPGroupCondition()
215 : m_enmType(DHCPGroupConditionType_MAC)
216 , m_pParent(NULL)
217 {}
218 HRESULT FinalConstruct()
219 {
220 return BaseFinalConstruct();
221 }
222 void FinalRelease()
223 {
224 uninit();
225 BaseFinalRelease();
226 }
227 HRESULT initWithDefaults(DHCPGroupConfig *a_pParent, bool a_fInclusive, DHCPGroupConditionType_T a_enmType,
228 const com::Utf8Str a_strValue);
229 HRESULT initWithSettings(DHCPGroupConfig *a_pParent, const settings::DHCPGroupCondition &a_rSrc);
230 void uninit();
231 /** @} */
232
233 HRESULT i_saveSettings(settings::DHCPGroupCondition &a_rDst);
234 static HRESULT i_validateTypeAndValue(DHCPGroupConditionType_T enmType, com::Utf8Str const &strValue,
235 VirtualBoxBase *pErrorDst);
236
237 /** @name Internal accessors
238 * @{ */
239 bool i_getInclusive() const RT_NOEXCEPT { return m_fInclusive; }
240 DHCPGroupConditionType_T i_getType() const RT_NOEXCEPT { return m_enmType; }
241 com::Utf8Str const &i_getValue() const RT_NOEXCEPT { return m_strValue; }
242 /** @} */
243
244protected:
245 /** @name Wrapped IDHCPGroupCondition properties
246 * @{ */
247 HRESULT getInclusive(BOOL *aInclusive) RT_OVERRIDE;
248 HRESULT setInclusive(BOOL aInclusive) RT_OVERRIDE;
249 HRESULT getType(DHCPGroupConditionType_T *aType) RT_OVERRIDE;
250 HRESULT setType(DHCPGroupConditionType_T aType) RT_OVERRIDE;
251 HRESULT getValue(com::Utf8Str &aValue) RT_OVERRIDE;
252 HRESULT setValue(const com::Utf8Str &aValue) RT_OVERRIDE;
253 /** @} */
254
255 /** @name Wrapped IDHCPGroupCondition methods
256 * @{ */
257 HRESULT remove() RT_OVERRIDE;
258 /** @} */
259};
260
261
262/**
263 * Group configuration.
264 */
265class DHCPGroupConfig : public DHCPGroupConfigWrap, public DHCPConfig
266{
267private:
268 /** Group name. */
269 com::Utf8Str m_strName;
270 /** Group membership conditions. */
271 std::vector<ComObjPtr<DHCPGroupCondition> > m_Conditions;
272 /** Iterator for m_Conditions. */
273 typedef std::vector<ComObjPtr<DHCPGroupCondition> >::iterator ConditionsIterator;
274
275public:
276 /** @name Constructors and destructors.
277 * @{ */
278 DHCPGroupConfig()
279 : DHCPConfig(DHCPConfigScope_Group, this)
280 { }
281 HRESULT FinalConstruct()
282 {
283 return BaseFinalConstruct();
284 }
285 void FinalRelease()
286 {
287 uninit();
288 BaseFinalRelease();
289 }
290 HRESULT initWithDefaults(VirtualBox *a_pVirtualBox, DHCPServer *a_pParent, const com::Utf8Str &a_rName);
291 HRESULT initWithSettings(VirtualBox *a_pVirtualBox, DHCPServer *a_pParent, const settings::DHCPGroupConfig &a_rSrc);
292 void uninit();
293 /** @} */
294
295 HRESULT i_saveSettings(settings::DHCPGroupConfig &a_rDst);
296 HRESULT i_removeCondition(DHCPGroupCondition *a_pCondition);
297 void i_writeDhcpdConfig(xml::ElementNode *a_pElmGroup) RT_OVERRIDE;
298
299protected:
300 /** @name Wrapped IDHCPConfig properties
301 * @{ */
302 HRESULT getScope(DHCPConfigScope_T *aScope) RT_OVERRIDE { return i_getScope(aScope); }
303 HRESULT getMinLeaseTime(ULONG *aMinLeaseTime) RT_OVERRIDE { return i_getMinLeaseTime(aMinLeaseTime); }
304 HRESULT setMinLeaseTime(ULONG aMinLeaseTime) RT_OVERRIDE { return i_setMinLeaseTime(aMinLeaseTime); }
305 HRESULT getDefaultLeaseTime(ULONG *aDefaultLeaseTime) RT_OVERRIDE { return i_getDefaultLeaseTime(aDefaultLeaseTime); }
306 HRESULT setDefaultLeaseTime(ULONG aDefaultLeaseTime) RT_OVERRIDE { return i_setDefaultLeaseTime(aDefaultLeaseTime); }
307 HRESULT getMaxLeaseTime(ULONG *aMaxLeaseTime) RT_OVERRIDE { return i_getMaxLeaseTime(aMaxLeaseTime); }
308 HRESULT setMaxLeaseTime(ULONG aMaxLeaseTime) RT_OVERRIDE { return i_setMaxLeaseTime(aMaxLeaseTime); }
309 /** @} */
310
311 /** @name Wrapped IDHCPGroupConfig properties
312 * @{ */
313 HRESULT getName(com::Utf8Str &aName) RT_OVERRIDE;
314 HRESULT setName(const com::Utf8Str &aName) RT_OVERRIDE;
315 HRESULT getConditions(std::vector<ComPtr<IDHCPGroupCondition> > &aConditions) RT_OVERRIDE;
316 /** @} */
317
318 /** @name Wrapped IDHCPConfig methods
319 * @{ */
320 HRESULT setOption(DhcpOpt_T aOption, DHCPOptionEncoding_T aEncoding, const com::Utf8Str &aValue) RT_OVERRIDE
321 {
322 return i_setOption(aOption, aEncoding, aValue);
323 }
324
325 HRESULT removeOption(DhcpOpt_T aOption) RT_OVERRIDE
326 {
327 return i_removeOption(aOption);
328 }
329
330 HRESULT removeAllOptions() RT_OVERRIDE
331 {
332 return i_removeAllOptions();
333 }
334
335 HRESULT getOption(DhcpOpt_T aOption, DHCPOptionEncoding_T *aEncoding, com::Utf8Str &aValue) RT_OVERRIDE
336 {
337 return i_getOption(aOption, aEncoding, aValue);
338 }
339
340 HRESULT getAllOptions(std::vector<DhcpOpt_T> &aOptions, std::vector<DHCPOptionEncoding_T> &aEncodings,
341 std::vector<com::Utf8Str> &aValues) RT_OVERRIDE
342 {
343 return i_getAllOptions(aOptions, aEncodings, aValues);
344 }
345
346 HRESULT remove() RT_OVERRIDE
347 {
348 return i_remove();
349 }
350 /** @} */
351
352 /** @name Wrapped IDHCPGroupConfig methods
353 * @{ */
354 HRESULT addCondition(BOOL aInclusive, DHCPGroupConditionType_T aType, const com::Utf8Str &aValue,
355 ComPtr<IDHCPGroupCondition> &aCondition) RT_OVERRIDE;
356 HRESULT removeAllConditions() RT_OVERRIDE;
357 /** @} */
358};
359
360
361/**
362 * Individual DHCP configuration.
363 */
364class DHCPIndividualConfig : public DHCPIndividualConfigWrap, public DHCPConfig
365{
366private:
367 /** The MAC address or all zeros. */
368 RTMAC m_MACAddress;
369 /** The VM ID or all zeros. */
370 com::Guid const m_idMachine;
371 /** The VM NIC slot number, or ~(ULONG)0. */
372 ULONG const m_uSlot;
373 /** This is part of a hack to resolve the MAC address for
374 * DHCPConfigScope_MachineNIC instances. If non-zero, we m_MACAddress is valid.
375 * To deal with the impossibly theoretical scenario that the DHCP server is
376 * being started by more than one thread, this is a version number and not just
377 * a boolean indicator. */
378 uint32_t volatile m_uMACAddressResolvedVersion;
379
380 /** The fixed IPv4 address, empty if dynamic. */
381 com::Utf8Str m_strFixedAddress;
382
383public:
384 /** @name Constructors and destructors.
385 * @{ */
386 DHCPIndividualConfig()
387 : DHCPConfig(DHCPConfigScope_MAC, this)
388 , m_uSlot(~(ULONG)0)
389 , m_uMACAddressResolvedVersion(0)
390 {
391 RT_ZERO(m_MACAddress);
392 }
393 HRESULT FinalConstruct()
394 {
395 return BaseFinalConstruct();
396 }
397 void FinalRelease()
398 {
399 uninit();
400 BaseFinalRelease();
401 }
402 HRESULT initWithMachineIdAndSlot(VirtualBox *a_pVirtualBox, DHCPServer *a_pParent, com::Guid const &a_idMachine,
403 ULONG a_uSlot, uint32_t a_uMACAddressVersion);
404 HRESULT initWithMACAddress(VirtualBox *a_pVirtualBox, DHCPServer *a_pParent, PCRTMAC a_pMACAddress);
405 HRESULT initWithSettingsAndMachineIdAndSlot(VirtualBox *a_pVirtualBox, DHCPServer *a_pParent,
406 const settings::DHCPIndividualConfig &rData, com::Guid const &a_idMachine,
407 ULONG a_uSlot, uint32_t a_uMACAddressVersion);
408 HRESULT initWithSettingsAndMACAddress(VirtualBox *a_pVirtualBox, DHCPServer *a_pParent,
409 const settings::DHCPIndividualConfig &rData, PCRTMAC a_pMACAddress);
410 void uninit();
411 /** @} */
412
413 /** @name Internal methods that are public for various reasons
414 * @{ */
415 HRESULT i_saveSettings(settings::DHCPIndividualConfig &a_rDst);
416 RTMAC const &i_getMACAddress() const RT_NOEXCEPT { return m_MACAddress; }
417 com::Guid const &i_getMachineId() const RT_NOEXCEPT { return m_idMachine; }
418 ULONG i_getSlot() const RT_NOEXCEPT { return m_uSlot; }
419 HRESULT i_getMachineMAC(PRTMAC pMACAddress);
420
421 HRESULT i_resolveMACAddress(uint32_t uVersion);
422 /** This is used to avoid producing bogus Dhcpd configuration elements. */
423 bool i_isMACAddressResolved(uint32_t uVersion) const
424 {
425 return m_enmScope != DHCPConfigScope_MachineNIC || (int32_t)(m_uMACAddressResolvedVersion - uVersion) >= 0;
426 }
427 void i_writeDhcpdConfig(xml::ElementNode *pElm) RT_OVERRIDE;
428 /** @} */
429
430protected:
431 /** @name wrapped IDHCPConfig properties
432 * @{ */
433 HRESULT getScope(DHCPConfigScope_T *aScope) RT_OVERRIDE { return i_getScope(aScope); }
434 HRESULT getMinLeaseTime(ULONG *aMinLeaseTime) RT_OVERRIDE { return i_getMinLeaseTime(aMinLeaseTime); }
435 HRESULT setMinLeaseTime(ULONG aMinLeaseTime) RT_OVERRIDE { return i_setMinLeaseTime(aMinLeaseTime); }
436 HRESULT getDefaultLeaseTime(ULONG *aDefaultLeaseTime) RT_OVERRIDE { return i_getDefaultLeaseTime(aDefaultLeaseTime); }
437 HRESULT setDefaultLeaseTime(ULONG aDefaultLeaseTime) RT_OVERRIDE { return i_setDefaultLeaseTime(aDefaultLeaseTime); }
438 HRESULT getMaxLeaseTime(ULONG *aMaxLeaseTime) RT_OVERRIDE { return i_getMaxLeaseTime(aMaxLeaseTime); }
439 HRESULT setMaxLeaseTime(ULONG aMaxLeaseTime) RT_OVERRIDE { return i_setMaxLeaseTime(aMaxLeaseTime); }
440 /** @} */
441
442 /** @name wrapped IDHCPConfig methods
443 * @{ */
444 HRESULT setOption(DhcpOpt_T aOption, DHCPOptionEncoding_T aEncoding, const com::Utf8Str &aValue) RT_OVERRIDE
445 {
446 return i_setOption(aOption, aEncoding, aValue);
447 }
448
449 HRESULT removeOption(DhcpOpt_T aOption) RT_OVERRIDE
450 {
451 return i_removeOption(aOption);
452 }
453
454 HRESULT removeAllOptions() RT_OVERRIDE
455 {
456 return i_removeAllOptions();
457 }
458
459 HRESULT getOption(DhcpOpt_T aOption, DHCPOptionEncoding_T *aEncoding, com::Utf8Str &aValue) RT_OVERRIDE
460 {
461 return i_getOption(aOption, aEncoding, aValue);
462 }
463
464 HRESULT getAllOptions(std::vector<DhcpOpt_T> &aOptions, std::vector<DHCPOptionEncoding_T> &aEncodings,
465 std::vector<com::Utf8Str> &aValues) RT_OVERRIDE
466 {
467 return i_getAllOptions(aOptions, aEncodings, aValues);
468 }
469
470 HRESULT remove() RT_OVERRIDE
471 {
472 return i_remove();
473 }
474 /** @} */
475
476 /** @name IDHCPIndividualConfig properties
477 * @{ */
478 HRESULT getMACAddress(com::Utf8Str &aMacAddress) RT_OVERRIDE;
479 HRESULT getMachineId(com::Guid &aId) RT_OVERRIDE;
480 HRESULT getSlot(ULONG *aSlot) RT_OVERRIDE;
481 HRESULT getFixedAddress(com::Utf8Str &aFixedAddress) RT_OVERRIDE;
482 HRESULT setFixedAddress(const com::Utf8Str &aFixedAddress) RT_OVERRIDE;
483 /** @} */
484};
485
486#endif /* !MAIN_INCLUDED_DHCPConfigImpl_h */
487
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette