1 | /* $Id: DHCPConfigImpl.h 79735 2019-07-12 13:02:10Z 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 "DHCPGroupConfigWrap.h"
|
---|
26 | #include "DHCPIndividualConfigWrap.h"
|
---|
27 | #include <VBox/settings.h>
|
---|
28 |
|
---|
29 |
|
---|
30 | class DHCPServer;
|
---|
31 |
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Base class for the a DHCP configration layer.
|
---|
35 | *
|
---|
36 | * This does not inherit from DHCPConfigWrap because its children need to
|
---|
37 | * inherit from children of DHCPConfigWrap, which smells like trouble and thus
|
---|
38 | * wasn't even attempted. Instead, we have a hack for passing a pointer that we
|
---|
39 | * can call setError and such on.
|
---|
40 | */
|
---|
41 | class DHCPConfig
|
---|
42 | {
|
---|
43 | protected:
|
---|
44 | /** Config scope (global, group, vm+nic, mac). */
|
---|
45 | DHCPConfigScope_T const m_enmScope;
|
---|
46 | /** Minimum lease time. */
|
---|
47 | ULONG m_secMinLeaseTime;
|
---|
48 | /** Default lease time. */
|
---|
49 | ULONG m_secDefaultLeaseTime;
|
---|
50 | /** Maximum lease time. */
|
---|
51 | ULONG m_secMaxLeaseTime;
|
---|
52 | /** DHCP option map. */
|
---|
53 | settings::DhcpOptionMap m_OptionMap;
|
---|
54 | /** The DHCP server parent (weak). */
|
---|
55 | DHCPServer * const m_pParent;
|
---|
56 | /** The DHCP server parent (weak). */
|
---|
57 | VirtualBox * const m_pVirtualBox;
|
---|
58 | private:
|
---|
59 | /** For setError and such. */
|
---|
60 | VirtualBoxBase * const m_pHack;
|
---|
61 |
|
---|
62 | protected:
|
---|
63 | /** @name Constructors and destructors.
|
---|
64 | * @{ */
|
---|
65 | DHCPConfig(DHCPConfigScope_T a_enmScope, VirtualBoxBase *a_pHack)
|
---|
66 | : m_enmScope(a_enmScope), m_secMinLeaseTime(0), m_secDefaultLeaseTime(0), m_secMaxLeaseTime(0), m_OptionMap()
|
---|
67 | , m_pParent(NULL), m_pVirtualBox(NULL), m_pHack(a_pHack)
|
---|
68 | {}
|
---|
69 | DHCPConfig();
|
---|
70 | virtual ~DHCPConfig()
|
---|
71 | {}
|
---|
72 | HRESULT i_initWithDefaults(VirtualBox *a_pVirtualBox, DHCPServer *a_pParent);
|
---|
73 | HRESULT i_initWithSettings(VirtualBox *a_pVirtualBox, DHCPServer *a_pParent, const settings::DHCPConfig &rConfig);
|
---|
74 | /** @} */
|
---|
75 |
|
---|
76 | /** @name IDHCPConfig properties
|
---|
77 | * @{ */
|
---|
78 | HRESULT i_getScope(DHCPConfigScope_T *aScope);
|
---|
79 | HRESULT i_getMinLeaseTime(ULONG *aMinLeaseTime);
|
---|
80 | HRESULT i_setMinLeaseTime(ULONG aMinLeaseTime);
|
---|
81 | HRESULT i_getDefaultLeaseTime(ULONG *aDefaultLeaseTime);
|
---|
82 | HRESULT i_setDefaultLeaseTime(ULONG aDefaultLeaseTime);
|
---|
83 | HRESULT i_getMaxLeaseTime(ULONG *aMaxLeaseTime);
|
---|
84 | HRESULT i_setMaxLeaseTime(ULONG aMaxLeaseTime);
|
---|
85 | /** @} */
|
---|
86 |
|
---|
87 | public:
|
---|
88 | /** @name IDHCPConfig methods
|
---|
89 | * @note public because the DHCPServer needs them for 6.0 interfaces.
|
---|
90 | * @todo Make protected again when IDHCPServer is cleaned up.
|
---|
91 | * @{ */
|
---|
92 | virtual HRESULT i_setOption(DhcpOpt_T aOption, DHCPOptionEncoding_T aEncoding, const com::Utf8Str &aValue);
|
---|
93 |
|
---|
94 | virtual HRESULT i_removeOption(DhcpOpt_T aOption);
|
---|
95 | virtual HRESULT i_removeAllOptions();
|
---|
96 | HRESULT i_getOption(DhcpOpt_T aOption, DHCPOptionEncoding_T *aEncoding, com::Utf8Str &aValue);
|
---|
97 | HRESULT i_getAllOptions(std::vector<DhcpOpt_T> &aOptions, std::vector<DHCPOptionEncoding_T> &aEncodings,
|
---|
98 | std::vector<com::Utf8Str> &aValues);
|
---|
99 | /** @} */
|
---|
100 |
|
---|
101 | protected:
|
---|
102 | HRESULT i_doWriteConfig();
|
---|
103 |
|
---|
104 | public:
|
---|
105 | HRESULT i_saveSettings(settings::DHCPConfig &a_rDst);
|
---|
106 | DHCPConfigScope_T i_getScope() const RT_NOEXCEPT { return m_enmScope; }
|
---|
107 | virtual void i_writeDhcpdConfig(xml::ElementNode *pElm);
|
---|
108 | };
|
---|
109 |
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * Global DHCP configuration.
|
---|
113 | */
|
---|
114 | class DHCPGlobalConfig : public DHCPGlobalConfigWrap, public DHCPConfig
|
---|
115 | {
|
---|
116 | public:
|
---|
117 | /** @name Constructors and destructors.
|
---|
118 | * @{ */
|
---|
119 | DHCPGlobalConfig()
|
---|
120 | : DHCPConfig(DHCPConfigScope_Global, this)
|
---|
121 | { }
|
---|
122 | HRESULT FinalConstruct()
|
---|
123 | {
|
---|
124 | return BaseFinalConstruct();
|
---|
125 | }
|
---|
126 | void FinalRelease()
|
---|
127 | {
|
---|
128 | uninit();
|
---|
129 | BaseFinalRelease();
|
---|
130 | }
|
---|
131 | HRESULT initWithDefaults(VirtualBox *a_pVirtualBox, DHCPServer *a_pParent);
|
---|
132 | HRESULT initWithSettings(VirtualBox *a_pVirtualBox, DHCPServer *a_pParent, const settings::DHCPConfig &rConfig);
|
---|
133 | void uninit();
|
---|
134 | /** @} */
|
---|
135 |
|
---|
136 | HRESULT i_saveSettings(settings::DHCPConfig &a_rDst);
|
---|
137 | HRESULT i_getNetworkMask(com::Utf8Str &a_rDst);
|
---|
138 | HRESULT i_setNetworkMask(const com::Utf8Str &a_rSrc);
|
---|
139 |
|
---|
140 | protected:
|
---|
141 | /** @name wrapped IDHCPConfig properties
|
---|
142 | * @{ */
|
---|
143 | HRESULT getScope(DHCPConfigScope_T *aScope) RT_OVERRIDE { return i_getScope(aScope); }
|
---|
144 | HRESULT getMinLeaseTime(ULONG *aMinLeaseTime) RT_OVERRIDE { return i_getMinLeaseTime(aMinLeaseTime); }
|
---|
145 | HRESULT setMinLeaseTime(ULONG aMinLeaseTime) RT_OVERRIDE { return i_setMinLeaseTime(aMinLeaseTime); }
|
---|
146 | HRESULT getDefaultLeaseTime(ULONG *aDefaultLeaseTime) RT_OVERRIDE { return i_getDefaultLeaseTime(aDefaultLeaseTime); }
|
---|
147 | HRESULT setDefaultLeaseTime(ULONG aDefaultLeaseTime) RT_OVERRIDE { return i_setDefaultLeaseTime(aDefaultLeaseTime); }
|
---|
148 | HRESULT getMaxLeaseTime(ULONG *aMaxLeaseTime) RT_OVERRIDE { return i_getMaxLeaseTime(aMaxLeaseTime); }
|
---|
149 | HRESULT setMaxLeaseTime(ULONG aMaxLeaseTime) RT_OVERRIDE { return i_setMaxLeaseTime(aMaxLeaseTime); }
|
---|
150 | /** @} */
|
---|
151 |
|
---|
152 | /** @name wrapped IDHCPConfig methods
|
---|
153 | * @{ */
|
---|
154 | HRESULT setOption(DhcpOpt_T aOption, DHCPOptionEncoding_T aEncoding, const com::Utf8Str &aValue) RT_OVERRIDE
|
---|
155 | {
|
---|
156 | return i_setOption(aOption, aEncoding, aValue);
|
---|
157 | }
|
---|
158 |
|
---|
159 | HRESULT removeOption(DhcpOpt_T aOption) RT_OVERRIDE
|
---|
160 | {
|
---|
161 | return i_removeOption(aOption);
|
---|
162 | }
|
---|
163 |
|
---|
164 | HRESULT removeAllOptions() RT_OVERRIDE
|
---|
165 | {
|
---|
166 | return i_removeAllOptions();
|
---|
167 | }
|
---|
168 |
|
---|
169 | HRESULT getOption(DhcpOpt_T aOption, DHCPOptionEncoding_T *aEncoding, com::Utf8Str &aValue) RT_OVERRIDE
|
---|
170 | {
|
---|
171 | return i_getOption(aOption, aEncoding, aValue);
|
---|
172 | }
|
---|
173 |
|
---|
174 | HRESULT getAllOptions(std::vector<DhcpOpt_T> &aOptions, std::vector<DHCPOptionEncoding_T> &aEncodings,
|
---|
175 | std::vector<com::Utf8Str> &aValues) RT_OVERRIDE
|
---|
176 | {
|
---|
177 | return i_getAllOptions(aOptions, aEncodings, aValues);
|
---|
178 | }
|
---|
179 | /** @} */
|
---|
180 |
|
---|
181 | public:
|
---|
182 | HRESULT i_setOption(DhcpOpt_T aOption, DHCPOptionEncoding_T aEncoding, const com::Utf8Str &aValue) RT_OVERRIDE;
|
---|
183 | HRESULT i_removeOption(DhcpOpt_T aOption) RT_OVERRIDE;
|
---|
184 | HRESULT i_removeAllOptions() RT_OVERRIDE;
|
---|
185 | };
|
---|
186 |
|
---|
187 |
|
---|
188 | /**
|
---|
189 | * Group configuration.
|
---|
190 | */
|
---|
191 | class DHCPGroupConfig : public VirtualBoxBase, public DHCPConfig
|
---|
192 | {
|
---|
193 | /** @todo later */
|
---|
194 | };
|
---|
195 |
|
---|
196 |
|
---|
197 | /**
|
---|
198 | * Individual DHCP configuration.
|
---|
199 | */
|
---|
200 | class DHCPIndividualConfig : public DHCPIndividualConfigWrap, public DHCPConfig
|
---|
201 | {
|
---|
202 | private:
|
---|
203 | /** The MAC address or all zeros. */
|
---|
204 | RTMAC m_MACAddress;
|
---|
205 | /** The VM ID or all zeros. */
|
---|
206 | com::Guid const m_idMachine;
|
---|
207 | /** The VM NIC slot number, or ~(ULONG)0. */
|
---|
208 | ULONG const m_uSlot;
|
---|
209 | /** This is part of a hack to resolve the MAC address for
|
---|
210 | * DHCPConfigScope_MachineNIC instances. If non-zero, we m_MACAddress is valid.
|
---|
211 | * To deal with the impossibly theoretical scenario that the DHCP server is
|
---|
212 | * being started by more than one thread, this is a version number and not just
|
---|
213 | * a boolean indicator. */
|
---|
214 | uint32_t volatile m_uMACAddressResolvedVersion;
|
---|
215 |
|
---|
216 | /** The fixed IPv4 address, empty if dynamic. */
|
---|
217 | com::Utf8Str m_strFixedAddress;
|
---|
218 |
|
---|
219 | public:
|
---|
220 | /** @name Constructors and destructors.
|
---|
221 | * @{ */
|
---|
222 | DHCPIndividualConfig()
|
---|
223 | : DHCPConfig(DHCPConfigScope_MAC, this)
|
---|
224 | , m_uSlot(~(ULONG)0)
|
---|
225 | , m_uMACAddressResolvedVersion(false)
|
---|
226 | {
|
---|
227 | RT_ZERO(m_MACAddress);
|
---|
228 | }
|
---|
229 | HRESULT FinalConstruct()
|
---|
230 | {
|
---|
231 | return BaseFinalConstruct();
|
---|
232 | }
|
---|
233 | void FinalRelease()
|
---|
234 | {
|
---|
235 | uninit();
|
---|
236 | BaseFinalRelease();
|
---|
237 | }
|
---|
238 | HRESULT initWithMachineIdAndSlot(VirtualBox *a_pVirtualBox, DHCPServer *a_pParent, com::Guid const &a_idMachine,
|
---|
239 | ULONG a_uSlot, uint32_t a_uMACAddressVersion);
|
---|
240 | HRESULT initWithMACAddress(VirtualBox *a_pVirtualBox, DHCPServer *a_pParent, PCRTMAC a_pMACAddress);
|
---|
241 | HRESULT initWithSettingsAndMachineIdAndSlot(VirtualBox *a_pVirtualBox, DHCPServer *a_pParent,
|
---|
242 | const settings::DHCPIndividualConfig &rData, com::Guid const &a_idMachine,
|
---|
243 | ULONG a_uSlot, uint32_t a_uMACAddressVersion);
|
---|
244 | HRESULT initWithSettingsAndMACAddress(VirtualBox *a_pVirtualBox, DHCPServer *a_pParent,
|
---|
245 | const settings::DHCPIndividualConfig &rData, PCRTMAC a_pMACAddress);
|
---|
246 | void uninit();
|
---|
247 | /** @} */
|
---|
248 |
|
---|
249 | /** @name Internal methods that are public for various reasons
|
---|
250 | * @{ */
|
---|
251 | HRESULT i_saveSettings(settings::DHCPIndividualConfig &a_rDst);
|
---|
252 | RTMAC const &i_getMACAddress() const RT_NOEXCEPT { return m_MACAddress; }
|
---|
253 | com::Guid const &i_getMachineId() const RT_NOEXCEPT { return m_idMachine; }
|
---|
254 | ULONG i_getSlot() const RT_NOEXCEPT { return m_uSlot; }
|
---|
255 | HRESULT i_getMachineMAC(PRTMAC pMACAddress);
|
---|
256 |
|
---|
257 | HRESULT i_resolveMACAddress(uint32_t uVersion);
|
---|
258 | /** This is used to avoid producing bogus Dhcpd configuration elements. */
|
---|
259 | bool i_isMACAddressResolved(uint32_t uVersion) const
|
---|
260 | {
|
---|
261 | return m_enmScope != DHCPConfigScope_MachineNIC || (int32_t)(m_uMACAddressResolvedVersion - uVersion) >= 0;
|
---|
262 | }
|
---|
263 | void i_writeDhcpdConfig(xml::ElementNode *pElm) RT_OVERRIDE;
|
---|
264 | /** @} */
|
---|
265 |
|
---|
266 | protected:
|
---|
267 | /** @name wrapped IDHCPConfig properties
|
---|
268 | * @{ */
|
---|
269 | HRESULT getScope(DHCPConfigScope_T *aScope) RT_OVERRIDE { return i_getScope(aScope); }
|
---|
270 | HRESULT getMinLeaseTime(ULONG *aMinLeaseTime) RT_OVERRIDE { return i_getMinLeaseTime(aMinLeaseTime); }
|
---|
271 | HRESULT setMinLeaseTime(ULONG aMinLeaseTime) RT_OVERRIDE { return i_setMinLeaseTime(aMinLeaseTime); }
|
---|
272 | HRESULT getDefaultLeaseTime(ULONG *aDefaultLeaseTime) RT_OVERRIDE { return i_getDefaultLeaseTime(aDefaultLeaseTime); }
|
---|
273 | HRESULT setDefaultLeaseTime(ULONG aDefaultLeaseTime) RT_OVERRIDE { return i_setDefaultLeaseTime(aDefaultLeaseTime); }
|
---|
274 | HRESULT getMaxLeaseTime(ULONG *aMaxLeaseTime) RT_OVERRIDE { return i_getMaxLeaseTime(aMaxLeaseTime); }
|
---|
275 | HRESULT setMaxLeaseTime(ULONG aMaxLeaseTime) RT_OVERRIDE { return i_setMaxLeaseTime(aMaxLeaseTime); }
|
---|
276 | /** @} */
|
---|
277 |
|
---|
278 | /** @name wrapped IDHCPConfig methods
|
---|
279 | * @{ */
|
---|
280 | HRESULT setOption(DhcpOpt_T aOption, DHCPOptionEncoding_T aEncoding, const com::Utf8Str &aValue) RT_OVERRIDE
|
---|
281 | {
|
---|
282 | return i_setOption(aOption, aEncoding, aValue);
|
---|
283 | }
|
---|
284 |
|
---|
285 | HRESULT removeOption(DhcpOpt_T aOption) RT_OVERRIDE
|
---|
286 | {
|
---|
287 | return i_removeOption(aOption);
|
---|
288 | }
|
---|
289 |
|
---|
290 | HRESULT removeAllOptions() RT_OVERRIDE
|
---|
291 | {
|
---|
292 | return i_removeAllOptions();
|
---|
293 | }
|
---|
294 |
|
---|
295 | HRESULT getOption(DhcpOpt_T aOption, DHCPOptionEncoding_T *aEncoding, com::Utf8Str &aValue) RT_OVERRIDE
|
---|
296 | {
|
---|
297 | return i_getOption(aOption, aEncoding, aValue);
|
---|
298 | }
|
---|
299 |
|
---|
300 | HRESULT getAllOptions(std::vector<DhcpOpt_T> &aOptions, std::vector<DHCPOptionEncoding_T> &aEncodings,
|
---|
301 | std::vector<com::Utf8Str> &aValues) RT_OVERRIDE
|
---|
302 | {
|
---|
303 | return i_getAllOptions(aOptions, aEncodings, aValues);
|
---|
304 | }
|
---|
305 | /** @} */
|
---|
306 |
|
---|
307 | /** @name IDHCPIndividualConfig properties
|
---|
308 | * @{ */
|
---|
309 | HRESULT getMACAddress(com::Utf8Str &aMacAddress) RT_OVERRIDE;
|
---|
310 | HRESULT getMachineId(com::Guid &aId) RT_OVERRIDE;
|
---|
311 | HRESULT getSlot(ULONG *aSlot) RT_OVERRIDE;
|
---|
312 | HRESULT getFixedAddress(com::Utf8Str &aFixedAddress) RT_OVERRIDE;
|
---|
313 | HRESULT setFixedAddress(const com::Utf8Str &aFixedAddress) RT_OVERRIDE;
|
---|
314 | /** @} */
|
---|
315 | };
|
---|
316 |
|
---|
317 | #endif /* !MAIN_INCLUDED_DHCPConfigImpl_h */
|
---|
318 |
|
---|