VirtualBox

source: vbox/trunk/src/VBox/Main/src-server/DHCPServerImpl.cpp@ 54351

Last change on this file since 54351 was 54351, checked in by vboxsync, 10 years ago

Main/DHCPServerImpl: factor out code to encode option values for
globalOptions and getVmSlotOptions methods. Encode DhcpOptValue::HEX
options differently. Try to be forward-compatible.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.8 KB
Line 
1/* $Id: DHCPServerImpl.cpp 54351 2015-02-21 01:27:45Z vboxsync $ */
2
3/** @file
4 *
5 * VirtualBox COM class implementation
6 */
7
8/*
9 * Copyright (C) 2006-2013 Oracle Corporation
10 *
11 * This file is part of VirtualBox Open Source Edition (OSE), as
12 * available from http://www.215389.xyz. 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/cpp/utils.h>
27
28#include <VBox/com/array.h>
29#include <VBox/settings.h>
30
31#include "VirtualBoxImpl.h"
32
33// constructor / destructor
34/////////////////////////////////////////////////////////////////////////////
35const std::string DHCPServerRunner::kDsrKeyGateway = "--gateway";
36const std::string DHCPServerRunner::kDsrKeyLowerIp = "--lower-ip";
37const std::string DHCPServerRunner::kDsrKeyUpperIp = "--upper-ip";
38
39
40struct DHCPServer::Data
41{
42 Data() : enabled(FALSE) {}
43
44 Bstr IPAddress;
45 Bstr lowerIP;
46 Bstr upperIP;
47
48 BOOL enabled;
49 DHCPServerRunner dhcp;
50
51 DhcpOptionMap GlobalDhcpOptions;
52 VmSlot2OptionsMap VmSlot2Options;
53};
54
55
56DHCPServer::DHCPServer()
57 : m(NULL), mVirtualBox(NULL)
58{
59 m = new DHCPServer::Data();
60}
61
62
63DHCPServer::~DHCPServer()
64{
65 if (m)
66 {
67 delete m;
68 m = NULL;
69 }
70}
71
72
73HRESULT DHCPServer::FinalConstruct()
74{
75 return BaseFinalConstruct();
76}
77
78
79void DHCPServer::FinalRelease()
80{
81 uninit ();
82
83 BaseFinalRelease();
84}
85
86
87void DHCPServer::uninit()
88{
89 /* Enclose the state transition Ready->InUninit->NotReady */
90 AutoUninitSpan autoUninitSpan(this);
91 if (autoUninitSpan.uninitDone())
92 return;
93
94 unconst(mVirtualBox) = NULL;
95}
96
97
98HRESULT DHCPServer::init(VirtualBox *aVirtualBox, IN_BSTR aName)
99{
100 AssertReturn(aName != NULL, E_INVALIDARG);
101
102 AutoInitSpan autoInitSpan(this);
103 AssertReturn(autoInitSpan.isOk(), E_FAIL);
104
105 /* share VirtualBox weakly (parent remains NULL so far) */
106 unconst(mVirtualBox) = aVirtualBox;
107
108 unconst(mName) = aName;
109 m->IPAddress = "0.0.0.0";
110 m->GlobalDhcpOptions[DhcpOpt_SubnetMask] = DhcpOptValue("0.0.0.0");
111 m->enabled = FALSE;
112
113 m->lowerIP = "0.0.0.0";
114 m->upperIP = "0.0.0.0";
115
116 /* Confirm a successful initialization */
117 autoInitSpan.setSucceeded();
118
119 return S_OK;
120}
121
122
123HRESULT DHCPServer::init(VirtualBox *aVirtualBox,
124 const settings::DHCPServer &data)
125{
126 /* Enclose the state transition NotReady->InInit->Ready */
127 AutoInitSpan autoInitSpan(this);
128 AssertReturn(autoInitSpan.isOk(), E_FAIL);
129
130 /* share VirtualBox weakly (parent remains NULL so far) */
131 unconst(mVirtualBox) = aVirtualBox;
132
133 unconst(mName) = data.strNetworkName;
134 m->IPAddress = data.strIPAddress;
135 m->enabled = data.fEnabled;
136 m->lowerIP = data.strIPLower;
137 m->upperIP = data.strIPUpper;
138
139 m->GlobalDhcpOptions.clear();
140 m->GlobalDhcpOptions.insert(data.GlobalDhcpOptions.begin(),
141 data.GlobalDhcpOptions.end());
142
143 m->VmSlot2Options.clear();
144 m->VmSlot2Options.insert(data.VmSlot2OptionsM.begin(),
145 data.VmSlot2OptionsM.end());
146
147 autoInitSpan.setSucceeded();
148
149 return S_OK;
150}
151
152
153HRESULT DHCPServer::i_saveSettings(settings::DHCPServer &data)
154{
155 AutoCaller autoCaller(this);
156 if (FAILED(autoCaller.rc())) return autoCaller.rc();
157
158 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
159
160 data.strNetworkName = mName;
161 data.strIPAddress = m->IPAddress;
162
163 data.fEnabled = !!m->enabled;
164 data.strIPLower = m->lowerIP;
165 data.strIPUpper = m->upperIP;
166
167 data.GlobalDhcpOptions.clear();
168 data.GlobalDhcpOptions.insert(m->GlobalDhcpOptions.begin(),
169 m->GlobalDhcpOptions.end());
170
171 data.VmSlot2OptionsM.clear();
172 data.VmSlot2OptionsM.insert(m->VmSlot2Options.begin(),
173 m->VmSlot2Options.end());
174
175 return S_OK;
176}
177
178
179HRESULT DHCPServer::getNetworkName(com::Utf8Str &aName)
180{
181 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
182
183 aName = mName;
184 return S_OK;
185}
186
187
188HRESULT DHCPServer::getEnabled(BOOL *aEnabled)
189{
190 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
191
192 *aEnabled = m->enabled;
193 return S_OK;
194}
195
196
197HRESULT DHCPServer::setEnabled(BOOL aEnabled)
198{
199 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
200 m->enabled = aEnabled;
201
202 // save the global settings; for that we should hold only the VirtualBox lock
203 alock.release();
204 AutoWriteLock vboxLock(mVirtualBox COMMA_LOCKVAL_SRC_POS);
205 HRESULT rc = mVirtualBox->i_saveSettings();
206
207 return rc;
208}
209
210
211HRESULT DHCPServer::getIPAddress(com::Utf8Str &aIPAddress)
212{
213 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
214
215 aIPAddress = Utf8Str(m->IPAddress);
216 return S_OK;
217}
218
219
220HRESULT DHCPServer::getNetworkMask(com::Utf8Str &aNetworkMask)
221{
222 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
223
224 aNetworkMask = m->GlobalDhcpOptions[DhcpOpt_SubnetMask].text;
225 return S_OK;
226}
227
228
229HRESULT DHCPServer::getLowerIP(com::Utf8Str &aIPAddress)
230{
231 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
232
233 aIPAddress = Utf8Str(m->lowerIP);
234 return S_OK;
235}
236
237
238HRESULT DHCPServer::getUpperIP(com::Utf8Str &aIPAddress)
239{
240 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
241
242 aIPAddress = Utf8Str(m->upperIP);
243 return S_OK;
244}
245
246
247HRESULT DHCPServer::setConfiguration(const com::Utf8Str &aIPAddress,
248 const com::Utf8Str &aNetworkMask,
249 const com::Utf8Str &aLowerIP,
250 const com::Utf8Str &aUpperIP)
251{
252 AssertReturn(!aIPAddress.isEmpty(), E_INVALIDARG);
253 AssertReturn(!aNetworkMask.isEmpty(), E_INVALIDARG);
254 AssertReturn(!aLowerIP.isEmpty(), E_INVALIDARG);
255 AssertReturn(!aUpperIP.isEmpty(), E_INVALIDARG);
256
257 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
258 m->IPAddress = aIPAddress;
259 m->GlobalDhcpOptions[DhcpOpt_SubnetMask] = aNetworkMask;
260
261 m->lowerIP = aLowerIP;
262 m->upperIP = aUpperIP;
263
264 // save the global settings; for that we should hold only the VirtualBox lock
265 alock.release();
266 AutoWriteLock vboxLock(mVirtualBox COMMA_LOCKVAL_SRC_POS);
267 return mVirtualBox->i_saveSettings();
268}
269
270
271HRESULT DHCPServer::encodeOption(com::Utf8Str &aEncoded,
272 uint32_t aOptCode, const DhcpOptValue &aOptValue)
273{
274 switch (aOptValue.encoding)
275 {
276 case DhcpOptValue::LEGACY:
277 {
278 /*
279 * This is original encoding which assumed that for each
280 * option we know its format and so we know how option
281 * "value" text is to be interpreted.
282 *
283 * "2:10800" # integer 32
284 * "6:1.2.3.4 8.8.8.8" # array of ip-address
285 */
286 aEncoded = Utf8StrFmt("%d:%s", aOptCode, aOptValue.text.c_str());
287 break;
288 }
289
290 case DhcpOptValue::HEX:
291 {
292 /*
293 * This is a bypass for any option - preformatted value as
294 * hex string with no semantic involved in formatting the
295 * value for the DHCP reply.
296 *
297 * 234=68:65:6c:6c:6f:2c:20:77:6f:72:6c:64
298 */
299 aEncoded = Utf8StrFmt("%d=%s", aOptCode, aOptValue.text.c_str());
300 break;
301 }
302
303 default:
304 {
305 /*
306 * Try to be forward compatible.
307 *
308 * "254@42=i hope you know what this means"
309 */
310 aEncoded = Utf8StrFmt("%d@%d=%s", aOptCode, (int)aOptValue.encoding,
311 aOptValue.text.c_str());
312 break;
313 }
314 }
315
316 return S_OK;
317}
318
319
320HRESULT DHCPServer::addGlobalOption(DhcpOpt_T aOption, const com::Utf8Str &aValue)
321{
322 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
323
324 m->GlobalDhcpOptions[aOption] = aValue;
325
326 /* Indirect way to understand that we're on NAT network */
327 if (aOption == DhcpOpt_Router)
328 m->dhcp.setOption(NetworkServiceRunner::kNsrKeyNeedMain, "on");
329
330 alock.release();
331
332 AutoWriteLock vboxLock(mVirtualBox COMMA_LOCKVAL_SRC_POS);
333 return mVirtualBox->i_saveSettings();
334}
335
336
337HRESULT DHCPServer::getGlobalOptions(std::vector<com::Utf8Str> &aValues)
338{
339 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
340 aValues.resize(m->GlobalDhcpOptions.size());
341 DhcpOptionMap::const_iterator it;
342 size_t i = 0;
343 for (it = m->GlobalDhcpOptions.begin(); it != m->GlobalDhcpOptions.end(); ++it, ++i)
344 {
345 uint32_t OptCode = (*it).first;
346 const DhcpOptValue &OptValue = (*it).second;
347
348 encodeOption(aValues[i], OptCode, OptValue);
349 }
350
351 return S_OK;
352}
353
354HRESULT DHCPServer::getVmConfigs(std::vector<com::Utf8Str> &aValues)
355{
356 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
357 aValues.resize(m->VmSlot2Options.size());
358 VmSlot2OptionsMap::const_iterator it;
359 size_t i = 0;
360 for (it = m->VmSlot2Options.begin(); it != m->VmSlot2Options.end(); ++it, ++i)
361 {
362 aValues[i] = Utf8StrFmt("[%s]:%d", it->first.VmName.c_str(), it->first.Slot);
363 }
364
365 return S_OK;
366}
367
368
369HRESULT DHCPServer::addVmSlotOption(const com::Utf8Str &aVmName,
370 LONG aSlot,
371 DhcpOpt_T aOption,
372 const com::Utf8Str &aValue)
373{
374 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
375 m->VmSlot2Options[settings::VmNameSlotKey(aVmName, aSlot)][aOption] = aValue;
376 alock.release();
377
378 AutoWriteLock vboxLock(mVirtualBox COMMA_LOCKVAL_SRC_POS);
379 return mVirtualBox->i_saveSettings();
380}
381
382
383HRESULT DHCPServer::removeVmSlotOptions(const com::Utf8Str &aVmName, LONG aSlot)
384{
385 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
386 DhcpOptionMap& map = i_findOptMapByVmNameSlot(aVmName, aSlot);
387 map.clear();
388
389 alock.release();
390
391 AutoWriteLock vboxLock(mVirtualBox COMMA_LOCKVAL_SRC_POS);
392 return mVirtualBox->i_saveSettings();
393}
394
395/**
396 * this is mapping (vm, slot)
397 */
398HRESULT DHCPServer::getVmSlotOptions(const com::Utf8Str &aVmName,
399 LONG aSlot,
400 std::vector<com::Utf8Str> &aValues)
401{
402
403 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
404 DhcpOptionMap& map = i_findOptMapByVmNameSlot(aVmName, aSlot);
405 aValues.resize(map.size());
406 size_t i = 0;
407 DhcpOptionMap::const_iterator it;
408 for (it = map.begin(); it != map.end(); ++it, ++i)
409 {
410 uint32_t OptCode = (*it).first;
411 const DhcpOptValue &OptValue = (*it).second;
412
413 encodeOption(aValues[i], OptCode, OptValue);
414 }
415
416 return S_OK;
417}
418
419
420HRESULT DHCPServer::getMacOptions(const com::Utf8Str &aMAC, std::vector<com::Utf8Str> &aOption)
421{
422 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
423 HRESULT hrc = S_OK;
424 ComPtr<IMachine> machine;
425 ComPtr<INetworkAdapter> nic;
426 VmSlot2OptionsIterator it;
427 for(it = m->VmSlot2Options.begin(); it != m->VmSlot2Options.end(); ++it)
428 {
429 alock.release();
430 hrc = mVirtualBox->FindMachine(Bstr(it->first.VmName).raw(), machine.asOutParam());
431 alock.acquire();
432
433 if (FAILED(hrc))
434 continue;
435
436 alock.release();
437 hrc = machine->GetNetworkAdapter(it->first.Slot, nic.asOutParam());
438 alock.acquire();
439
440 if (FAILED(hrc))
441 continue;
442
443 com::Bstr mac;
444
445 alock.release();
446 hrc = nic->COMGETTER(MACAddress)(mac.asOutParam());
447 alock.acquire();
448
449 if (FAILED(hrc)) /* no MAC address ??? */
450 break;
451 if (!RTStrICmp(com::Utf8Str(mac).c_str(), aMAC.c_str()))
452 return getVmSlotOptions(it->first.VmName,
453 it->first.Slot,
454 aOption);
455 } /* end of for */
456
457 return hrc;
458}
459
460HRESULT DHCPServer::getEventSource(ComPtr<IEventSource> &aEventSource)
461{
462 NOREF(aEventSource);
463 ReturnComNotImplemented();
464}
465
466
467HRESULT DHCPServer::start(const com::Utf8Str &aNetworkName,
468 const com::Utf8Str &aTrunkName,
469 const com::Utf8Str &aTrunkType)
470{
471 /* Silently ignore attempts to run disabled servers. */
472 if (!m->enabled)
473 return S_OK;
474
475 /* Commmon Network Settings */
476 m->dhcp.setOption(NetworkServiceRunner::kNsrKeyNetwork, aNetworkName.c_str());
477
478 if (!aTrunkName.isEmpty())
479 m->dhcp.setOption(NetworkServiceRunner::kNsrTrunkName, aTrunkName.c_str());
480
481 m->dhcp.setOption(NetworkServiceRunner::kNsrKeyTrunkType, aTrunkType.c_str());
482
483 /* XXX: should this MAC default initialization moved to NetworkServiceRunner? */
484 char strMAC[32];
485 Guid guid;
486 guid.create();
487 RTStrPrintf (strMAC, sizeof(strMAC), "08:00:27:%02X:%02X:%02X",
488 guid.raw()->au8[0],
489 guid.raw()->au8[1],
490 guid.raw()->au8[2]);
491 m->dhcp.setOption(NetworkServiceRunner::kNsrMacAddress, strMAC);
492 m->dhcp.setOption(NetworkServiceRunner::kNsrIpAddress, Utf8Str(m->IPAddress).c_str());
493 m->dhcp.setOption(NetworkServiceRunner::kNsrIpNetmask, Utf8Str(m->GlobalDhcpOptions[DhcpOpt_SubnetMask].text).c_str());
494 m->dhcp.setOption(DHCPServerRunner::kDsrKeyLowerIp, Utf8Str(m->lowerIP).c_str());
495 m->dhcp.setOption(DHCPServerRunner::kDsrKeyUpperIp, Utf8Str(m->upperIP).c_str());
496
497 /* XXX: This parameters Dhcp Server will fetch via API */
498 return RT_FAILURE(m->dhcp.start()) ? E_FAIL : S_OK;
499 //m->dhcp.detachFromServer(); /* need to do this to avoid server shutdown on runner destruction */
500}
501
502
503HRESULT DHCPServer::stop (void)
504{
505 return RT_FAILURE(m->dhcp.stop()) ? E_FAIL : S_OK;
506}
507
508
509DhcpOptionMap& DHCPServer::i_findOptMapByVmNameSlot(const com::Utf8Str& aVmName,
510 LONG aSlot)
511{
512 return m->VmSlot2Options[settings::VmNameSlotKey(aVmName, aSlot)];
513}
Note: See TracBrowser for help on using the repository browser.

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