[17875] | 1 | /* $Id: DHCPServerImpl.cpp 25310 2009-12-10 17:06:44Z vboxsync $ */
|
---|
| 2 |
|
---|
| 3 | /** @file
|
---|
| 4 | *
|
---|
| 5 | * VirtualBox COM class implementation
|
---|
| 6 | */
|
---|
| 7 |
|
---|
| 8 | /*
|
---|
| 9 | * Copyright (C) 2006-2008 Sun Microsystems, Inc.
|
---|
| 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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
| 20 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
| 21 | * additional information or have any questions.
|
---|
| 22 | */
|
---|
| 23 |
|
---|
[18208] | 24 | #include "DHCPServerRunner.h"
|
---|
[18023] | 25 | #include "DHCPServerImpl.h"
|
---|
[17875] | 26 | #include "Logging.h"
|
---|
| 27 |
|
---|
[17881] | 28 | #include <VBox/settings.h>
|
---|
| 29 |
|
---|
[23319] | 30 | #include "VirtualBoxImpl.h"
|
---|
| 31 |
|
---|
[17875] | 32 | // constructor / destructor
|
---|
| 33 | /////////////////////////////////////////////////////////////////////////////
|
---|
| 34 |
|
---|
[18023] | 35 | DEFINE_EMPTY_CTOR_DTOR (DHCPServer)
|
---|
[17875] | 36 |
|
---|
[18023] | 37 | HRESULT DHCPServer::FinalConstruct()
|
---|
[17875] | 38 | {
|
---|
| 39 | return S_OK;
|
---|
| 40 | }
|
---|
| 41 |
|
---|
[18023] | 42 | void DHCPServer::FinalRelease()
|
---|
[17875] | 43 | {
|
---|
| 44 | uninit ();
|
---|
| 45 | }
|
---|
| 46 |
|
---|
[18023] | 47 | void DHCPServer::uninit()
|
---|
[17888] | 48 | {
|
---|
[17893] | 49 | /* Enclose the state transition Ready->InUninit->NotReady */
|
---|
[21878] | 50 | AutoUninitSpan autoUninitSpan(this);
|
---|
[17893] | 51 | if (autoUninitSpan.uninitDone())
|
---|
| 52 | return;
|
---|
| 53 |
|
---|
[21878] | 54 | unconst(mVirtualBox).setNull();
|
---|
[17888] | 55 | }
|
---|
| 56 |
|
---|
[18023] | 57 | HRESULT DHCPServer::init(VirtualBox *aVirtualBox, IN_BSTR aName)
|
---|
[17875] | 58 | {
|
---|
[21878] | 59 | AssertReturn(aName != NULL, E_INVALIDARG);
|
---|
[17881] | 60 |
|
---|
[21878] | 61 | AutoInitSpan autoInitSpan(this);
|
---|
| 62 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
[17881] | 63 |
|
---|
[17887] | 64 | /* share VirtualBox weakly (parent remains NULL so far) */
|
---|
[21878] | 65 | unconst(mVirtualBox) = aVirtualBox;
|
---|
[17887] | 66 |
|
---|
[17881] | 67 | unconst(mName) = aName;
|
---|
[17887] | 68 | m.IPAddress = "0.0.0.0";
|
---|
| 69 | m.networkMask = "0.0.0.0";
|
---|
| 70 | m.enabled = FALSE;
|
---|
[17888] | 71 | m.lowerIP = "0.0.0.0";
|
---|
| 72 | m.upperIP = "0.0.0.0";
|
---|
[17881] | 73 |
|
---|
| 74 | /* Confirm a successful initialization */
|
---|
| 75 | autoInitSpan.setSucceeded();
|
---|
| 76 |
|
---|
| 77 | return S_OK;
|
---|
[17875] | 78 | }
|
---|
| 79 |
|
---|
[22173] | 80 | HRESULT DHCPServer::init(VirtualBox *aVirtualBox,
|
---|
| 81 | const settings::DHCPServer &data)
|
---|
[17875] | 82 | {
|
---|
[17881] | 83 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
[21878] | 84 | AutoInitSpan autoInitSpan(this);
|
---|
| 85 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
[17881] | 86 |
|
---|
[17887] | 87 | /* share VirtualBox weakly (parent remains NULL so far) */
|
---|
[21878] | 88 | unconst(mVirtualBox) = aVirtualBox;
|
---|
[17887] | 89 |
|
---|
[22173] | 90 | unconst(mName) = data.strNetworkName;
|
---|
| 91 | m.IPAddress = data.strIPAddress;
|
---|
| 92 | m.networkMask = data.strIPNetworkMask;
|
---|
| 93 | m.enabled = data.fEnabled;
|
---|
| 94 | m.lowerIP = data.strIPLower;
|
---|
| 95 | m.upperIP = data.strIPUpper;
|
---|
[17881] | 96 |
|
---|
| 97 | autoInitSpan.setSucceeded();
|
---|
| 98 |
|
---|
| 99 | return S_OK;
|
---|
[17875] | 100 | }
|
---|
| 101 |
|
---|
[22173] | 102 | HRESULT DHCPServer::saveSettings(settings::DHCPServer &data)
|
---|
[17875] | 103 | {
|
---|
[21878] | 104 | AutoCaller autoCaller(this);
|
---|
[25149] | 105 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
[17881] | 106 |
|
---|
[25310] | 107 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
[17881] | 108 |
|
---|
[22173] | 109 | data.strNetworkName = mName;
|
---|
| 110 | data.strIPAddress = m.IPAddress;
|
---|
| 111 | data.strIPNetworkMask = m.networkMask;
|
---|
[22186] | 112 | data.fEnabled = !!m.enabled;
|
---|
[22173] | 113 | data.strIPLower = m.lowerIP;
|
---|
| 114 | data.strIPUpper = m.upperIP;
|
---|
[17881] | 115 |
|
---|
| 116 | return S_OK;
|
---|
[17875] | 117 | }
|
---|
| 118 |
|
---|
[18023] | 119 | STDMETHODIMP DHCPServer::COMGETTER(NetworkName) (BSTR *aName)
|
---|
[17875] | 120 | {
|
---|
| 121 | CheckComArgOutPointerValid(aName);
|
---|
| 122 |
|
---|
[21878] | 123 | AutoCaller autoCaller(this);
|
---|
[25149] | 124 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
[17875] | 125 |
|
---|
| 126 | mName.cloneTo(aName);
|
---|
| 127 |
|
---|
| 128 | return S_OK;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
[18023] | 131 | STDMETHODIMP DHCPServer::COMGETTER(Enabled) (BOOL *aEnabled)
|
---|
[17875] | 132 | {
|
---|
| 133 | CheckComArgOutPointerValid(aEnabled);
|
---|
| 134 |
|
---|
[21878] | 135 | AutoCaller autoCaller(this);
|
---|
[25149] | 136 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
[17875] | 137 |
|
---|
| 138 | *aEnabled = m.enabled;
|
---|
| 139 |
|
---|
| 140 | return S_OK;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
[18023] | 143 | STDMETHODIMP DHCPServer::COMSETTER(Enabled) (BOOL aEnabled)
|
---|
[17875] | 144 | {
|
---|
[21878] | 145 | AutoCaller autoCaller(this);
|
---|
[25149] | 146 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
[17875] | 147 |
|
---|
[17887] | 148 | /* VirtualBox::saveSettings() needs a write lock */
|
---|
[25310] | 149 | AutoMultiWriteLock2 alock(mVirtualBox, this COMMA_LOCKVAL_SRC_POS);
|
---|
[17887] | 150 |
|
---|
[17875] | 151 | m.enabled = aEnabled;
|
---|
| 152 |
|
---|
[17887] | 153 | HRESULT rc = mVirtualBox->saveSettings();
|
---|
[17875] | 154 |
|
---|
[17887] | 155 | return rc;
|
---|
[17875] | 156 | }
|
---|
| 157 |
|
---|
[18023] | 158 | STDMETHODIMP DHCPServer::COMGETTER(IPAddress) (BSTR *aIPAddress)
|
---|
[17875] | 159 | {
|
---|
| 160 | CheckComArgOutPointerValid(aIPAddress);
|
---|
| 161 |
|
---|
[21878] | 162 | AutoCaller autoCaller(this);
|
---|
[25149] | 163 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
[17875] | 164 |
|
---|
| 165 | m.IPAddress.cloneTo(aIPAddress);
|
---|
| 166 |
|
---|
| 167 | return S_OK;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
[18023] | 170 | STDMETHODIMP DHCPServer::COMGETTER(NetworkMask) (BSTR *aNetworkMask)
|
---|
[17875] | 171 | {
|
---|
| 172 | CheckComArgOutPointerValid(aNetworkMask);
|
---|
| 173 |
|
---|
[21878] | 174 | AutoCaller autoCaller(this);
|
---|
[25149] | 175 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
[17875] | 176 |
|
---|
| 177 | m.networkMask.cloneTo(aNetworkMask);
|
---|
| 178 |
|
---|
| 179 | return S_OK;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
[18023] | 182 | STDMETHODIMP DHCPServer::COMGETTER(LowerIP) (BSTR *aIPAddress)
|
---|
[17875] | 183 | {
|
---|
| 184 | CheckComArgOutPointerValid(aIPAddress);
|
---|
| 185 |
|
---|
[21878] | 186 | AutoCaller autoCaller(this);
|
---|
[25149] | 187 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
[17875] | 188 |
|
---|
[17888] | 189 | m.lowerIP.cloneTo(aIPAddress);
|
---|
[17875] | 190 |
|
---|
| 191 | return S_OK;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
[18023] | 194 | STDMETHODIMP DHCPServer::COMGETTER(UpperIP) (BSTR *aIPAddress)
|
---|
[17875] | 195 | {
|
---|
| 196 | CheckComArgOutPointerValid(aIPAddress);
|
---|
| 197 |
|
---|
[21878] | 198 | AutoCaller autoCaller(this);
|
---|
[25149] | 199 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
[17875] | 200 |
|
---|
[17888] | 201 | m.upperIP.cloneTo(aIPAddress);
|
---|
[17875] | 202 |
|
---|
| 203 | return S_OK;
|
---|
| 204 | }
|
---|
| 205 |
|
---|
[18023] | 206 | STDMETHODIMP DHCPServer::SetConfiguration (IN_BSTR aIPAddress, IN_BSTR aNetworkMask, IN_BSTR aLowerIP, IN_BSTR aUpperIP)
|
---|
[17875] | 207 | {
|
---|
[21878] | 208 | AssertReturn(aIPAddress != NULL, E_INVALIDARG);
|
---|
| 209 | AssertReturn(aNetworkMask != NULL, E_INVALIDARG);
|
---|
| 210 | AssertReturn(aLowerIP != NULL, E_INVALIDARG);
|
---|
| 211 | AssertReturn(aUpperIP != NULL, E_INVALIDARG);
|
---|
[17881] | 212 |
|
---|
[21878] | 213 | AutoCaller autoCaller(this);
|
---|
[25149] | 214 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
[17887] | 215 |
|
---|
| 216 | /* VirtualBox::saveSettings() needs a write lock */
|
---|
[25310] | 217 | AutoMultiWriteLock2 alock(mVirtualBox, this COMMA_LOCKVAL_SRC_POS);
|
---|
[17887] | 218 |
|
---|
[17881] | 219 | m.IPAddress = aIPAddress;
|
---|
| 220 | m.networkMask = aNetworkMask;
|
---|
[17888] | 221 | m.lowerIP = aLowerIP;
|
---|
| 222 | m.upperIP = aUpperIP;
|
---|
[17881] | 223 |
|
---|
[17911] | 224 | return mVirtualBox->saveSettings();
|
---|
[17875] | 225 | }
|
---|
[18208] | 226 |
|
---|
[22173] | 227 | STDMETHODIMP DHCPServer::Start(IN_BSTR aNetworkName, IN_BSTR aTrunkName, IN_BSTR aTrunkType)
|
---|
[18208] | 228 | {
|
---|
| 229 | /* Silently ignore attepmts to run disabled servers. */
|
---|
| 230 | if (!m.enabled)
|
---|
| 231 | return S_OK;
|
---|
| 232 |
|
---|
[21860] | 233 | m.dhcp.setOption(DHCPCFG_NETNAME, Utf8Str(aNetworkName), true);
|
---|
[18208] | 234 | Bstr tmp(aTrunkName);
|
---|
| 235 | if (!tmp.isEmpty())
|
---|
[21860] | 236 | m.dhcp.setOption(DHCPCFG_TRUNKNAME, Utf8Str(tmp), true);
|
---|
| 237 | m.dhcp.setOption(DHCPCFG_TRUNKTYPE, Utf8Str(aTrunkType), true);
|
---|
[18208] | 238 | //temporary hack for testing
|
---|
| 239 | // DHCPCFG_NAME
|
---|
| 240 | char strMAC[13];
|
---|
| 241 | Guid guid;
|
---|
| 242 | guid.create();
|
---|
| 243 | RTStrPrintf (strMAC, sizeof(strMAC), "080027%02X%02X%02X",
|
---|
| 244 | guid.ptr()->au8[0], guid.ptr()->au8[1], guid.ptr()->au8[2]);
|
---|
[21860] | 245 | m.dhcp.setOption(DHCPCFG_MACADDRESS, strMAC, true);
|
---|
| 246 | m.dhcp.setOption(DHCPCFG_IPADDRESS, Utf8Str(m.IPAddress), true);
|
---|
[18208] | 247 | // DHCPCFG_LEASEDB,
|
---|
| 248 | // DHCPCFG_VERBOSE,
|
---|
| 249 | // DHCPCFG_GATEWAY,
|
---|
[21860] | 250 | m.dhcp.setOption(DHCPCFG_LOWERIP, Utf8Str(m.lowerIP), true);
|
---|
| 251 | m.dhcp.setOption(DHCPCFG_UPPERIP, Utf8Str(m.upperIP), true);
|
---|
| 252 | m.dhcp.setOption(DHCPCFG_NETMASK, Utf8Str(m.networkMask), true);
|
---|
[18208] | 253 |
|
---|
| 254 | // DHCPCFG_HELP,
|
---|
| 255 | // DHCPCFG_VERSION,
|
---|
| 256 | // DHCPCFG_NOTOPT_MAXVAL
|
---|
[21860] | 257 | m.dhcp.setOption(DHCPCFG_BEGINCONFIG, "", true);
|
---|
[18208] | 258 |
|
---|
| 259 | return RT_FAILURE(m.dhcp.start()) ? E_FAIL : S_OK;
|
---|
| 260 | //m.dhcp.detachFromServer(); /* need to do this to avoid server shutdown on runner destruction */
|
---|
| 261 | }
|
---|
| 262 |
|
---|
| 263 | STDMETHODIMP DHCPServer::Stop (void)
|
---|
| 264 | {
|
---|
| 265 | return RT_FAILURE(m.dhcp.stop()) ? E_FAIL : S_OK;
|
---|
| 266 | }
|
---|