1 | /* $Id: DHCPServerImpl.cpp 69500 2017-10-28 15:14:05Z vboxsync $ */
|
---|
2 |
|
---|
3 | /** @file
|
---|
4 | *
|
---|
5 | * VirtualBox COM class implementation
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * Copyright (C) 2006-2017 Oracle Corporation
|
---|
10 | *
|
---|
11 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
12 | * available from http://www.virtualbox.org. This file is free software;
|
---|
13 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
14 | * General Public License (GPL) as published by the Free Software
|
---|
15 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
16 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
17 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
18 | */
|
---|
19 |
|
---|
20 | #include <string>
|
---|
21 | #include "NetworkServiceRunner.h"
|
---|
22 | #include "DHCPServerImpl.h"
|
---|
23 | #include "AutoCaller.h"
|
---|
24 | #include "Logging.h"
|
---|
25 |
|
---|
26 | #include <iprt/asm.h>
|
---|
27 | #include <iprt/net.h>
|
---|
28 | #include <iprt/cpp/utils.h>
|
---|
29 |
|
---|
30 | #include <VBox/com/array.h>
|
---|
31 | #include <VBox/settings.h>
|
---|
32 |
|
---|
33 | #include "VirtualBoxImpl.h"
|
---|
34 |
|
---|
35 | // constructor / destructor
|
---|
36 | /////////////////////////////////////////////////////////////////////////////
|
---|
37 | const std::string DHCPServerRunner::kDsrKeyGateway = "--gateway";
|
---|
38 | const std::string DHCPServerRunner::kDsrKeyLowerIp = "--lower-ip";
|
---|
39 | const std::string DHCPServerRunner::kDsrKeyUpperIp = "--upper-ip";
|
---|
40 |
|
---|
41 |
|
---|
42 | struct DHCPServer::Data
|
---|
43 | {
|
---|
44 | Data()
|
---|
45 | : enabled(FALSE)
|
---|
46 | , router(false)
|
---|
47 | {}
|
---|
48 |
|
---|
49 | Utf8Str IPAddress;
|
---|
50 | Utf8Str lowerIP;
|
---|
51 | Utf8Str upperIP;
|
---|
52 |
|
---|
53 | BOOL enabled;
|
---|
54 | bool router;
|
---|
55 | DHCPServerRunner dhcp;
|
---|
56 |
|
---|
57 | settings::DhcpOptionMap GlobalDhcpOptions;
|
---|
58 | settings::VmSlot2OptionsMap VmSlot2Options;
|
---|
59 | };
|
---|
60 |
|
---|
61 |
|
---|
62 | DHCPServer::DHCPServer()
|
---|
63 | : m(NULL)
|
---|
64 | , mVirtualBox(NULL)
|
---|
65 | {
|
---|
66 | m = new DHCPServer::Data();
|
---|
67 | }
|
---|
68 |
|
---|
69 |
|
---|
70 | DHCPServer::~DHCPServer()
|
---|
71 | {
|
---|
72 | if (m)
|
---|
73 | {
|
---|
74 | delete m;
|
---|
75 | m = NULL;
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 |
|
---|
80 | HRESULT DHCPServer::FinalConstruct()
|
---|
81 | {
|
---|
82 | return BaseFinalConstruct();
|
---|
83 | }
|
---|
84 |
|
---|
85 |
|
---|
86 | void DHCPServer::FinalRelease()
|
---|
87 | {
|
---|
88 | uninit ();
|
---|
89 |
|
---|
90 | BaseFinalRelease();
|
---|
91 | }
|
---|
92 |
|
---|
93 |
|
---|
94 | void DHCPServer::uninit()
|
---|
95 | {
|
---|
96 | /* Enclose the state transition Ready->InUninit->NotReady */
|
---|
97 | AutoUninitSpan autoUninitSpan(this);
|
---|
98 | if (autoUninitSpan.uninitDone())
|
---|
99 | return;
|
---|
100 |
|
---|
101 | unconst(mVirtualBox) = NULL;
|
---|
102 | }
|
---|
103 |
|
---|
104 |
|
---|
105 | HRESULT DHCPServer::init(VirtualBox *aVirtualBox, const Utf8Str &aName)
|
---|
106 | {
|
---|
107 | AssertReturn(!aName.isEmpty(), E_INVALIDARG);
|
---|
108 |
|
---|
109 | AutoInitSpan autoInitSpan(this);
|
---|
110 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
111 |
|
---|
112 | /* share VirtualBox weakly (parent remains NULL so far) */
|
---|
113 | unconst(mVirtualBox) = aVirtualBox;
|
---|
114 |
|
---|
115 | unconst(mName) = aName;
|
---|
116 | m->IPAddress = "0.0.0.0";
|
---|
117 | m->GlobalDhcpOptions[DhcpOpt_SubnetMask] = settings::DhcpOptValue("0.0.0.0");
|
---|
118 | m->enabled = FALSE;
|
---|
119 |
|
---|
120 | m->lowerIP = "0.0.0.0";
|
---|
121 | m->upperIP = "0.0.0.0";
|
---|
122 |
|
---|
123 | /* Confirm a successful initialization */
|
---|
124 | autoInitSpan.setSucceeded();
|
---|
125 |
|
---|
126 | return S_OK;
|
---|
127 | }
|
---|
128 |
|
---|
129 |
|
---|
130 | HRESULT DHCPServer::init(VirtualBox *aVirtualBox,
|
---|
131 | const settings::DHCPServer &data)
|
---|
132 | {
|
---|
133 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
134 | AutoInitSpan autoInitSpan(this);
|
---|
135 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
136 |
|
---|
137 | /* share VirtualBox weakly (parent remains NULL so far) */
|
---|
138 | unconst(mVirtualBox) = aVirtualBox;
|
---|
139 |
|
---|
140 | unconst(mName) = data.strNetworkName;
|
---|
141 | m->IPAddress = data.strIPAddress;
|
---|
142 | m->enabled = data.fEnabled;
|
---|
143 | m->lowerIP = data.strIPLower;
|
---|
144 | m->upperIP = data.strIPUpper;
|
---|
145 |
|
---|
146 | m->GlobalDhcpOptions.clear();
|
---|
147 | m->GlobalDhcpOptions.insert(data.GlobalDhcpOptions.begin(),
|
---|
148 | data.GlobalDhcpOptions.end());
|
---|
149 |
|
---|
150 | m->VmSlot2Options.clear();
|
---|
151 | m->VmSlot2Options.insert(data.VmSlot2OptionsM.begin(),
|
---|
152 | data.VmSlot2OptionsM.end());
|
---|
153 |
|
---|
154 | autoInitSpan.setSucceeded();
|
---|
155 |
|
---|
156 | return S_OK;
|
---|
157 | }
|
---|
158 |
|
---|
159 |
|
---|
160 | HRESULT DHCPServer::i_saveSettings(settings::DHCPServer &data)
|
---|
161 | {
|
---|
162 | AutoCaller autoCaller(this);
|
---|
163 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
164 |
|
---|
165 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
166 |
|
---|
167 | data.strNetworkName = mName;
|
---|
168 | data.strIPAddress = m->IPAddress;
|
---|
169 |
|
---|
170 | data.fEnabled = !!m->enabled;
|
---|
171 | data.strIPLower = m->lowerIP;
|
---|
172 | data.strIPUpper = m->upperIP;
|
---|
173 |
|
---|
174 | data.GlobalDhcpOptions.clear();
|
---|
175 | data.GlobalDhcpOptions.insert(m->GlobalDhcpOptions.begin(),
|
---|
176 | m->GlobalDhcpOptions.end());
|
---|
177 |
|
---|
178 | data.VmSlot2OptionsM.clear();
|
---|
179 | data.VmSlot2OptionsM.insert(m->VmSlot2Options.begin(),
|
---|
180 | m->VmSlot2Options.end());
|
---|
181 |
|
---|
182 | return S_OK;
|
---|
183 | }
|
---|
184 |
|
---|
185 |
|
---|
186 | HRESULT DHCPServer::getNetworkName(com::Utf8Str &aName)
|
---|
187 | {
|
---|
188 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
189 |
|
---|
190 | aName = mName;
|
---|
191 | return S_OK;
|
---|
192 | }
|
---|
193 |
|
---|
194 |
|
---|
195 | HRESULT DHCPServer::getEnabled(BOOL *aEnabled)
|
---|
196 | {
|
---|
197 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
198 |
|
---|
199 | *aEnabled = m->enabled;
|
---|
200 | return S_OK;
|
---|
201 | }
|
---|
202 |
|
---|
203 |
|
---|
204 | HRESULT DHCPServer::setEnabled(BOOL aEnabled)
|
---|
205 | {
|
---|
206 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
207 | m->enabled = aEnabled;
|
---|
208 |
|
---|
209 | // save the global settings; for that we should hold only the VirtualBox lock
|
---|
210 | alock.release();
|
---|
211 | AutoWriteLock vboxLock(mVirtualBox COMMA_LOCKVAL_SRC_POS);
|
---|
212 | HRESULT rc = mVirtualBox->i_saveSettings();
|
---|
213 |
|
---|
214 | return rc;
|
---|
215 | }
|
---|
216 |
|
---|
217 |
|
---|
218 | HRESULT DHCPServer::getIPAddress(com::Utf8Str &aIPAddress)
|
---|
219 | {
|
---|
220 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
221 |
|
---|
222 | aIPAddress = Utf8Str(m->IPAddress);
|
---|
223 | return S_OK;
|
---|
224 | }
|
---|
225 |
|
---|
226 |
|
---|
227 | HRESULT DHCPServer::getNetworkMask(com::Utf8Str &aNetworkMask)
|
---|
228 | {
|
---|
229 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
230 |
|
---|
231 | aNetworkMask = m->GlobalDhcpOptions[DhcpOpt_SubnetMask].text;
|
---|
232 | return S_OK;
|
---|
233 | }
|
---|
234 |
|
---|
235 |
|
---|
236 | HRESULT DHCPServer::getLowerIP(com::Utf8Str &aIPAddress)
|
---|
237 | {
|
---|
238 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
239 |
|
---|
240 | aIPAddress = Utf8Str(m->lowerIP);
|
---|
241 | return S_OK;
|
---|
242 | }
|
---|
243 |
|
---|
244 |
|
---|
245 | HRESULT DHCPServer::getUpperIP(com::Utf8Str &aIPAddress)
|
---|
246 | {
|
---|
247 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
248 |
|
---|
249 | aIPAddress = Utf8Str(m->upperIP);
|
---|
250 | return S_OK;
|
---|
251 | }
|
---|
252 |
|
---|
253 |
|
---|
254 | HRESULT DHCPServer::setConfiguration(const com::Utf8Str &aIPAddress,
|
---|
255 | const com::Utf8Str &aNetworkMask,
|
---|
256 | const com::Utf8Str &aLowerIP,
|
---|
257 | const com::Utf8Str &aUpperIP)
|
---|
258 | {
|
---|
259 | RTNETADDRIPV4 IPAddress, NetworkMask, LowerIP, UpperIP;
|
---|
260 | int rc;
|
---|
261 |
|
---|
262 | rc = RTNetStrToIPv4Addr(aIPAddress.c_str(), &IPAddress);
|
---|
263 | if (RT_FAILURE(rc))
|
---|
264 | return mVirtualBox->setErrorBoth(E_INVALIDARG, rc,
|
---|
265 | "Invalid server address");
|
---|
266 |
|
---|
267 | rc = RTNetStrToIPv4Addr(aNetworkMask.c_str(), &NetworkMask);
|
---|
268 | if (RT_FAILURE(rc))
|
---|
269 | return mVirtualBox->setErrorBoth(E_INVALIDARG, rc,
|
---|
270 | "Invalid netmask");
|
---|
271 |
|
---|
272 | rc = RTNetStrToIPv4Addr(aLowerIP.c_str(), &LowerIP);
|
---|
273 | if (RT_FAILURE(rc))
|
---|
274 | return mVirtualBox->setErrorBoth(E_INVALIDARG, rc,
|
---|
275 | "Invalid range lower address");
|
---|
276 |
|
---|
277 | rc = RTNetStrToIPv4Addr(aUpperIP.c_str(), &UpperIP);
|
---|
278 | if (RT_FAILURE(rc))
|
---|
279 | return mVirtualBox->setErrorBoth(E_INVALIDARG, rc,
|
---|
280 | "Invalid range upper address");
|
---|
281 |
|
---|
282 | /*
|
---|
283 | * Insist on continuous mask. May be also accept prefix length
|
---|
284 | * here or address/prefix for aIPAddress?
|
---|
285 | */
|
---|
286 | rc = RTNetMaskToPrefixIPv4(&NetworkMask, NULL);
|
---|
287 | if (RT_FAILURE(rc))
|
---|
288 | return mVirtualBox->setErrorBoth(E_INVALIDARG, rc,
|
---|
289 | "Invalid netmask");
|
---|
290 |
|
---|
291 | /* It's more convenient to convert to host order once */
|
---|
292 | IPAddress.u = RT_N2H_U32(IPAddress.u);
|
---|
293 | NetworkMask.u = RT_N2H_U32(NetworkMask.u);
|
---|
294 | LowerIP.u = RT_N2H_U32(LowerIP.u);
|
---|
295 | UpperIP.u = RT_N2H_U32(UpperIP.u);
|
---|
296 |
|
---|
297 | /*
|
---|
298 | * Addresses must be unicast and from the same network
|
---|
299 | */
|
---|
300 | if ( (IPAddress.u & UINT32_C(0xe0000000)) == UINT32_C(0xe0000000)
|
---|
301 | || (IPAddress.u & ~NetworkMask.u) == 0
|
---|
302 | || ((IPAddress.u & ~NetworkMask.u) | NetworkMask.u) == UINT32_C(0xffffffff))
|
---|
303 | {
|
---|
304 | return mVirtualBox->setError(E_INVALIDARG,
|
---|
305 | "Invalid server address");
|
---|
306 | }
|
---|
307 |
|
---|
308 | if ( (LowerIP.u & UINT32_C(0xe0000000)) == UINT32_C(0xe0000000)
|
---|
309 | || (LowerIP.u & NetworkMask.u) != (IPAddress.u &NetworkMask.u)
|
---|
310 | || (LowerIP.u & ~NetworkMask.u) == 0
|
---|
311 | || ((LowerIP.u & ~NetworkMask.u) | NetworkMask.u) == UINT32_C(0xffffffff))
|
---|
312 | {
|
---|
313 | return mVirtualBox->setError(E_INVALIDARG,
|
---|
314 | "Invalid range lower address");
|
---|
315 | }
|
---|
316 |
|
---|
317 | if ( (UpperIP.u & UINT32_C(0xe0000000)) == UINT32_C(0xe0000000)
|
---|
318 | || (UpperIP.u & NetworkMask.u) != (IPAddress.u &NetworkMask.u)
|
---|
319 | || (UpperIP.u & ~NetworkMask.u) == 0
|
---|
320 | || ((UpperIP.u & ~NetworkMask.u) | NetworkMask.u) == UINT32_C(0xffffffff))
|
---|
321 | {
|
---|
322 | return mVirtualBox->setError(E_INVALIDARG,
|
---|
323 | "Invalid range upper address");
|
---|
324 | }
|
---|
325 |
|
---|
326 | /* The range should be valid ... */
|
---|
327 | if (LowerIP.u > UpperIP.u)
|
---|
328 | return mVirtualBox->setError(E_INVALIDARG,
|
---|
329 | "Invalid range bounds");
|
---|
330 |
|
---|
331 | /* ... and shouldn't contain the server's address */
|
---|
332 | if (LowerIP.u <= IPAddress.u && IPAddress.u <= UpperIP.u)
|
---|
333 | return mVirtualBox->setError(E_INVALIDARG,
|
---|
334 | "Server address within range bounds");
|
---|
335 |
|
---|
336 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
337 | m->IPAddress = aIPAddress;
|
---|
338 | m->GlobalDhcpOptions[DhcpOpt_SubnetMask] = aNetworkMask;
|
---|
339 |
|
---|
340 | m->lowerIP = aLowerIP;
|
---|
341 | m->upperIP = aUpperIP;
|
---|
342 |
|
---|
343 | // save the global settings; for that we should hold only the VirtualBox lock
|
---|
344 | alock.release();
|
---|
345 | AutoWriteLock vboxLock(mVirtualBox COMMA_LOCKVAL_SRC_POS);
|
---|
346 | return mVirtualBox->i_saveSettings();
|
---|
347 | }
|
---|
348 |
|
---|
349 |
|
---|
350 | HRESULT DHCPServer::encodeOption(com::Utf8Str &aEncoded,
|
---|
351 | uint32_t aOptCode,
|
---|
352 | const settings::DhcpOptValue &aOptValue)
|
---|
353 | {
|
---|
354 | switch (aOptValue.encoding)
|
---|
355 | {
|
---|
356 | case DhcpOptEncoding_Legacy:
|
---|
357 | {
|
---|
358 | /*
|
---|
359 | * This is original encoding which assumed that for each
|
---|
360 | * option we know its format and so we know how option
|
---|
361 | * "value" text is to be interpreted.
|
---|
362 | *
|
---|
363 | * "2:10800" # integer 32
|
---|
364 | * "6:1.2.3.4 8.8.8.8" # array of ip-address
|
---|
365 | */
|
---|
366 | aEncoded = Utf8StrFmt("%d:%s", aOptCode, aOptValue.text.c_str());
|
---|
367 | break;
|
---|
368 | }
|
---|
369 |
|
---|
370 | case DhcpOptEncoding_Hex:
|
---|
371 | {
|
---|
372 | /*
|
---|
373 | * This is a bypass for any option - preformatted value as
|
---|
374 | * hex string with no semantic involved in formatting the
|
---|
375 | * value for the DHCP reply.
|
---|
376 | *
|
---|
377 | * 234=68:65:6c:6c:6f:2c:20:77:6f:72:6c:64
|
---|
378 | */
|
---|
379 | aEncoded = Utf8StrFmt("%d=%s", aOptCode, aOptValue.text.c_str());
|
---|
380 | break;
|
---|
381 | }
|
---|
382 |
|
---|
383 | default:
|
---|
384 | {
|
---|
385 | /*
|
---|
386 | * Try to be forward compatible.
|
---|
387 | *
|
---|
388 | * "254@42=i hope you know what this means"
|
---|
389 | */
|
---|
390 | aEncoded = Utf8StrFmt("%d@%d=%s", aOptCode, (int)aOptValue.encoding,
|
---|
391 | aOptValue.text.c_str());
|
---|
392 | break;
|
---|
393 | }
|
---|
394 | }
|
---|
395 |
|
---|
396 | return S_OK;
|
---|
397 | }
|
---|
398 |
|
---|
399 |
|
---|
400 | int DHCPServer::addOption(settings::DhcpOptionMap &aMap,
|
---|
401 | DhcpOpt_T aOption, const com::Utf8Str &aValue)
|
---|
402 | {
|
---|
403 | settings::DhcpOptValue OptValue;
|
---|
404 |
|
---|
405 | if (aOption != 0)
|
---|
406 | {
|
---|
407 | OptValue = settings::DhcpOptValue(aValue, DhcpOptEncoding_Legacy);
|
---|
408 | }
|
---|
409 | /*
|
---|
410 | * This is a kludge to sneak in option encoding information
|
---|
411 | * through existing API. We use option 0 and supply the real
|
---|
412 | * option/value in the same format that encodeOption() above
|
---|
413 | * produces for getter methods.
|
---|
414 | */
|
---|
415 | else
|
---|
416 | {
|
---|
417 | uint8_t u8Code;
|
---|
418 | uint32_t u32Enc;
|
---|
419 | char *pszNext;
|
---|
420 | int rc;
|
---|
421 |
|
---|
422 | rc = RTStrToUInt8Ex(aValue.c_str(), &pszNext, 10, &u8Code);
|
---|
423 | if (!RT_SUCCESS(rc))
|
---|
424 | return VERR_PARSE_ERROR;
|
---|
425 |
|
---|
426 | switch (*pszNext)
|
---|
427 | {
|
---|
428 | case ':': /* support legacy format too */
|
---|
429 | {
|
---|
430 | u32Enc = DhcpOptEncoding_Legacy;
|
---|
431 | break;
|
---|
432 | }
|
---|
433 |
|
---|
434 | case '=':
|
---|
435 | {
|
---|
436 | u32Enc = DhcpOptEncoding_Hex;
|
---|
437 | break;
|
---|
438 | }
|
---|
439 |
|
---|
440 | case '@':
|
---|
441 | {
|
---|
442 | rc = RTStrToUInt32Ex(pszNext + 1, &pszNext, 10, &u32Enc);
|
---|
443 | if (!RT_SUCCESS(rc))
|
---|
444 | return VERR_PARSE_ERROR;
|
---|
445 | if (*pszNext != '=')
|
---|
446 | return VERR_PARSE_ERROR;
|
---|
447 | break;
|
---|
448 | }
|
---|
449 |
|
---|
450 | default:
|
---|
451 | return VERR_PARSE_ERROR;
|
---|
452 | }
|
---|
453 |
|
---|
454 | aOption = (DhcpOpt_T)u8Code;
|
---|
455 | OptValue = settings::DhcpOptValue(pszNext + 1, (DhcpOptEncoding_T)u32Enc);
|
---|
456 | }
|
---|
457 |
|
---|
458 | aMap[aOption] = OptValue;
|
---|
459 | return VINF_SUCCESS;
|
---|
460 | }
|
---|
461 |
|
---|
462 |
|
---|
463 | HRESULT DHCPServer::addGlobalOption(DhcpOpt_T aOption, const com::Utf8Str &aValue)
|
---|
464 | {
|
---|
465 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
466 |
|
---|
467 | int rc = addOption(m->GlobalDhcpOptions, aOption, aValue);
|
---|
468 | if (!RT_SUCCESS(rc))
|
---|
469 | return E_INVALIDARG;
|
---|
470 |
|
---|
471 | /* Indirect way to understand that we're on NAT network */
|
---|
472 | if (aOption == DhcpOpt_Router)
|
---|
473 | {
|
---|
474 | m->dhcp.setOption(NetworkServiceRunner::kNsrKeyNeedMain, "on");
|
---|
475 | m->router = true;
|
---|
476 | }
|
---|
477 |
|
---|
478 | alock.release();
|
---|
479 |
|
---|
480 | AutoWriteLock vboxLock(mVirtualBox COMMA_LOCKVAL_SRC_POS);
|
---|
481 | return mVirtualBox->i_saveSettings();
|
---|
482 | }
|
---|
483 |
|
---|
484 |
|
---|
485 | HRESULT DHCPServer::getGlobalOptions(std::vector<com::Utf8Str> &aValues)
|
---|
486 | {
|
---|
487 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
488 | aValues.resize(m->GlobalDhcpOptions.size());
|
---|
489 | settings::DhcpOptionMap::const_iterator it;
|
---|
490 | size_t i = 0;
|
---|
491 | for (it = m->GlobalDhcpOptions.begin(); it != m->GlobalDhcpOptions.end(); ++it, ++i)
|
---|
492 | {
|
---|
493 | uint32_t OptCode = (*it).first;
|
---|
494 | const settings::DhcpOptValue &OptValue = (*it).second;
|
---|
495 |
|
---|
496 | encodeOption(aValues[i], OptCode, OptValue);
|
---|
497 | }
|
---|
498 |
|
---|
499 | return S_OK;
|
---|
500 | }
|
---|
501 |
|
---|
502 | HRESULT DHCPServer::getVmConfigs(std::vector<com::Utf8Str> &aValues)
|
---|
503 | {
|
---|
504 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
505 | aValues.resize(m->VmSlot2Options.size());
|
---|
506 | settings::VmSlot2OptionsMap::const_iterator it;
|
---|
507 | size_t i = 0;
|
---|
508 | for (it = m->VmSlot2Options.begin(); it != m->VmSlot2Options.end(); ++it, ++i)
|
---|
509 | {
|
---|
510 | aValues[i] = Utf8StrFmt("[%s]:%d", it->first.VmName.c_str(), it->first.Slot);
|
---|
511 | }
|
---|
512 |
|
---|
513 | return S_OK;
|
---|
514 | }
|
---|
515 |
|
---|
516 |
|
---|
517 | HRESULT DHCPServer::addVmSlotOption(const com::Utf8Str &aVmName,
|
---|
518 | LONG aSlot,
|
---|
519 | DhcpOpt_T aOption,
|
---|
520 | const com::Utf8Str &aValue)
|
---|
521 | {
|
---|
522 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
523 |
|
---|
524 | settings::DhcpOptionMap &map = m->VmSlot2Options[settings::VmNameSlotKey(aVmName, aSlot)];
|
---|
525 | int rc = addOption(map, aOption, aValue);
|
---|
526 | if (!RT_SUCCESS(rc))
|
---|
527 | return E_INVALIDARG;
|
---|
528 |
|
---|
529 | alock.release();
|
---|
530 |
|
---|
531 | AutoWriteLock vboxLock(mVirtualBox COMMA_LOCKVAL_SRC_POS);
|
---|
532 | return mVirtualBox->i_saveSettings();
|
---|
533 | }
|
---|
534 |
|
---|
535 |
|
---|
536 | HRESULT DHCPServer::removeVmSlotOptions(const com::Utf8Str &aVmName, LONG aSlot)
|
---|
537 | {
|
---|
538 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
539 | settings::DhcpOptionMap &map = i_findOptMapByVmNameSlot(aVmName, aSlot);
|
---|
540 | map.clear();
|
---|
541 |
|
---|
542 | alock.release();
|
---|
543 |
|
---|
544 | AutoWriteLock vboxLock(mVirtualBox COMMA_LOCKVAL_SRC_POS);
|
---|
545 | return mVirtualBox->i_saveSettings();
|
---|
546 | }
|
---|
547 |
|
---|
548 | /**
|
---|
549 | * this is mapping (vm, slot)
|
---|
550 | */
|
---|
551 | HRESULT DHCPServer::getVmSlotOptions(const com::Utf8Str &aVmName,
|
---|
552 | LONG aSlot,
|
---|
553 | std::vector<com::Utf8Str> &aValues)
|
---|
554 | {
|
---|
555 |
|
---|
556 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
557 | settings::DhcpOptionMap &map = i_findOptMapByVmNameSlot(aVmName, aSlot);
|
---|
558 | aValues.resize(map.size());
|
---|
559 | size_t i = 0;
|
---|
560 | settings::DhcpOptionMap::const_iterator it;
|
---|
561 | for (it = map.begin(); it != map.end(); ++it, ++i)
|
---|
562 | {
|
---|
563 | uint32_t OptCode = (*it).first;
|
---|
564 | const settings::DhcpOptValue &OptValue = (*it).second;
|
---|
565 |
|
---|
566 | encodeOption(aValues[i], OptCode, OptValue);
|
---|
567 | }
|
---|
568 |
|
---|
569 | return S_OK;
|
---|
570 | }
|
---|
571 |
|
---|
572 |
|
---|
573 | HRESULT DHCPServer::getMacOptions(const com::Utf8Str &aMAC, std::vector<com::Utf8Str> &aOption)
|
---|
574 | {
|
---|
575 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
576 | HRESULT hrc = S_OK;
|
---|
577 | ComPtr<IMachine> machine;
|
---|
578 | ComPtr<INetworkAdapter> nic;
|
---|
579 | settings::VmSlot2OptionsIterator it;
|
---|
580 | for(it = m->VmSlot2Options.begin(); it != m->VmSlot2Options.end(); ++it)
|
---|
581 | {
|
---|
582 | alock.release();
|
---|
583 | hrc = mVirtualBox->FindMachine(Bstr(it->first.VmName).raw(), machine.asOutParam());
|
---|
584 | alock.acquire();
|
---|
585 |
|
---|
586 | if (FAILED(hrc))
|
---|
587 | continue;
|
---|
588 |
|
---|
589 | alock.release();
|
---|
590 | hrc = machine->GetNetworkAdapter(it->first.Slot, nic.asOutParam());
|
---|
591 | alock.acquire();
|
---|
592 |
|
---|
593 | if (FAILED(hrc))
|
---|
594 | continue;
|
---|
595 |
|
---|
596 | com::Bstr mac;
|
---|
597 |
|
---|
598 | alock.release();
|
---|
599 | hrc = nic->COMGETTER(MACAddress)(mac.asOutParam());
|
---|
600 | alock.acquire();
|
---|
601 |
|
---|
602 | if (FAILED(hrc)) /* no MAC address ??? */
|
---|
603 | break;
|
---|
604 | if (!RTStrICmp(com::Utf8Str(mac).c_str(), aMAC.c_str()))
|
---|
605 | return getVmSlotOptions(it->first.VmName,
|
---|
606 | it->first.Slot,
|
---|
607 | aOption);
|
---|
608 | } /* end of for */
|
---|
609 |
|
---|
610 | return hrc;
|
---|
611 | }
|
---|
612 |
|
---|
613 | HRESULT DHCPServer::getEventSource(ComPtr<IEventSource> &aEventSource)
|
---|
614 | {
|
---|
615 | NOREF(aEventSource);
|
---|
616 | ReturnComNotImplemented();
|
---|
617 | }
|
---|
618 |
|
---|
619 |
|
---|
620 | HRESULT DHCPServer::start(const com::Utf8Str &aNetworkName,
|
---|
621 | const com::Utf8Str &aTrunkName,
|
---|
622 | const com::Utf8Str &aTrunkType)
|
---|
623 | {
|
---|
624 | /* Silently ignore attempts to run disabled servers. */
|
---|
625 | if (!m->enabled)
|
---|
626 | return S_OK;
|
---|
627 |
|
---|
628 | /* Commmon Network Settings */
|
---|
629 | m->dhcp.setOption(NetworkServiceRunner::kNsrKeyNetwork, aNetworkName.c_str());
|
---|
630 |
|
---|
631 | if (!aTrunkName.isEmpty())
|
---|
632 | m->dhcp.setOption(NetworkServiceRunner::kNsrTrunkName, aTrunkName.c_str());
|
---|
633 |
|
---|
634 | m->dhcp.setOption(NetworkServiceRunner::kNsrKeyTrunkType, aTrunkType.c_str());
|
---|
635 |
|
---|
636 | /* XXX: should this MAC default initialization moved to NetworkServiceRunner? */
|
---|
637 | char strMAC[32];
|
---|
638 | Guid guid;
|
---|
639 | guid.create();
|
---|
640 | RTStrPrintf (strMAC, sizeof(strMAC), "08:00:27:%02X:%02X:%02X",
|
---|
641 | guid.raw()->au8[0],
|
---|
642 | guid.raw()->au8[1],
|
---|
643 | guid.raw()->au8[2]);
|
---|
644 | m->dhcp.setOption(NetworkServiceRunner::kNsrMacAddress, strMAC);
|
---|
645 | m->dhcp.setOption(NetworkServiceRunner::kNsrIpAddress, Utf8Str(m->IPAddress).c_str());
|
---|
646 | m->dhcp.setOption(NetworkServiceRunner::kNsrIpNetmask, Utf8Str(m->GlobalDhcpOptions[DhcpOpt_SubnetMask].text).c_str());
|
---|
647 | m->dhcp.setOption(DHCPServerRunner::kDsrKeyLowerIp, Utf8Str(m->lowerIP).c_str());
|
---|
648 | m->dhcp.setOption(DHCPServerRunner::kDsrKeyUpperIp, Utf8Str(m->upperIP).c_str());
|
---|
649 |
|
---|
650 | /* XXX: This parameters Dhcp Server will fetch via API */
|
---|
651 | return RT_FAILURE(m->dhcp.start(!m->router /* KillProcOnExit */)) ? E_FAIL : S_OK;
|
---|
652 | //m->dhcp.detachFromServer(); /* need to do this to avoid server shutdown on runner destruction */
|
---|
653 | }
|
---|
654 |
|
---|
655 |
|
---|
656 | HRESULT DHCPServer::stop (void)
|
---|
657 | {
|
---|
658 | return RT_FAILURE(m->dhcp.stop()) ? E_FAIL : S_OK;
|
---|
659 | }
|
---|
660 |
|
---|
661 |
|
---|
662 | settings::DhcpOptionMap &DHCPServer::i_findOptMapByVmNameSlot(const com::Utf8Str &aVmName,
|
---|
663 | LONG aSlot)
|
---|
664 | {
|
---|
665 | return m->VmSlot2Options[settings::VmNameSlotKey(aVmName, aSlot)];
|
---|
666 | }
|
---|