1 | /* $Id: DHCPServerImpl.cpp 20596 2009-06-15 17:45:17Z 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 |
|
---|
24 | #include "DHCPServerRunner.h"
|
---|
25 | #include "DHCPServerImpl.h"
|
---|
26 | #include "Logging.h"
|
---|
27 |
|
---|
28 | #include <VBox/settings.h>
|
---|
29 |
|
---|
30 | // constructor / destructor
|
---|
31 | /////////////////////////////////////////////////////////////////////////////
|
---|
32 |
|
---|
33 | DEFINE_EMPTY_CTOR_DTOR (DHCPServer)
|
---|
34 |
|
---|
35 | HRESULT DHCPServer::FinalConstruct()
|
---|
36 | {
|
---|
37 | return S_OK;
|
---|
38 | }
|
---|
39 |
|
---|
40 | void DHCPServer::FinalRelease()
|
---|
41 | {
|
---|
42 | uninit ();
|
---|
43 | }
|
---|
44 |
|
---|
45 | void DHCPServer::uninit()
|
---|
46 | {
|
---|
47 | /* Enclose the state transition Ready->InUninit->NotReady */
|
---|
48 | AutoUninitSpan autoUninitSpan (this);
|
---|
49 | if (autoUninitSpan.uninitDone())
|
---|
50 | return;
|
---|
51 |
|
---|
52 | // /* we uninit children and reset mParent
|
---|
53 | // * and VirtualBox::removeDependentChild() needs a write lock */
|
---|
54 | // AutoMultiWriteLock2 alock (mVirtualBox->lockHandle(), this->treeLock());
|
---|
55 |
|
---|
56 | mVirtualBox->removeDependentChild (this);
|
---|
57 |
|
---|
58 | unconst (mVirtualBox).setNull();
|
---|
59 | }
|
---|
60 |
|
---|
61 | HRESULT DHCPServer::init(VirtualBox *aVirtualBox, IN_BSTR aName)
|
---|
62 | {
|
---|
63 | AssertReturn (aName != NULL, E_INVALIDARG);
|
---|
64 |
|
---|
65 | AutoInitSpan autoInitSpan (this);
|
---|
66 | AssertReturn (autoInitSpan.isOk(), E_FAIL);
|
---|
67 |
|
---|
68 | /* share VirtualBox weakly (parent remains NULL so far) */
|
---|
69 | unconst (mVirtualBox) = aVirtualBox;
|
---|
70 |
|
---|
71 | unconst(mName) = aName;
|
---|
72 | m.IPAddress = "0.0.0.0";
|
---|
73 | m.networkMask = "0.0.0.0";
|
---|
74 | m.enabled = FALSE;
|
---|
75 | m.lowerIP = "0.0.0.0";
|
---|
76 | m.upperIP = "0.0.0.0";
|
---|
77 |
|
---|
78 | /* register with VirtualBox early, since uninit() will
|
---|
79 | * unconditionally unregister on failure */
|
---|
80 | aVirtualBox->addDependentChild (this);
|
---|
81 |
|
---|
82 | /* Confirm a successful initialization */
|
---|
83 | autoInitSpan.setSucceeded();
|
---|
84 |
|
---|
85 | return S_OK;
|
---|
86 | }
|
---|
87 |
|
---|
88 | HRESULT DHCPServer::init(VirtualBox *aVirtualBox, const settings::Key &aNode)
|
---|
89 | {
|
---|
90 | using namespace settings;
|
---|
91 |
|
---|
92 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
93 | AutoInitSpan autoInitSpan (this);
|
---|
94 | AssertReturn (autoInitSpan.isOk(), E_FAIL);
|
---|
95 |
|
---|
96 | /* share VirtualBox weakly (parent remains NULL so far) */
|
---|
97 | unconst (mVirtualBox) = aVirtualBox;
|
---|
98 |
|
---|
99 | aVirtualBox->addDependentChild (this);
|
---|
100 |
|
---|
101 | unconst(mName) = aNode.stringValue ("networkName");
|
---|
102 | m.IPAddress = aNode.stringValue ("IPAddress");
|
---|
103 | m.networkMask = aNode.stringValue ("networkMask");
|
---|
104 | m.enabled = aNode.value <bool> ("enabled");
|
---|
105 | m.lowerIP = aNode.stringValue ("lowerIP");
|
---|
106 | m.upperIP = aNode.stringValue ("upperIP");
|
---|
107 |
|
---|
108 | autoInitSpan.setSucceeded();
|
---|
109 |
|
---|
110 | return S_OK;
|
---|
111 | }
|
---|
112 |
|
---|
113 | HRESULT DHCPServer::saveSettings (settings::Key &aParentNode)
|
---|
114 | {
|
---|
115 | using namespace settings;
|
---|
116 |
|
---|
117 | AssertReturn (!aParentNode.isNull(), E_FAIL);
|
---|
118 |
|
---|
119 | AutoCaller autoCaller (this);
|
---|
120 | CheckComRCReturnRC (autoCaller.rc());
|
---|
121 |
|
---|
122 | AutoReadLock alock (this);
|
---|
123 |
|
---|
124 | Key aNode = aParentNode.appendKey ("DHCPServer");
|
---|
125 | /* required */
|
---|
126 | aNode.setValue <Bstr> ("networkName", mName);
|
---|
127 | aNode.setValue <Bstr> ("IPAddress", m.IPAddress);
|
---|
128 | aNode.setValue <Bstr> ("networkMask", m.networkMask);
|
---|
129 | aNode.setValue <Bstr> ("lowerIP", m.lowerIP);
|
---|
130 | aNode.setValue <Bstr> ("upperIP", m.upperIP);
|
---|
131 | /* To force it back to a numeric value; will otherwise break for 2.2.x. */
|
---|
132 | aNode.setValue <ULONG> ("enabled", m.enabled);
|
---|
133 |
|
---|
134 | return S_OK;
|
---|
135 | }
|
---|
136 |
|
---|
137 | STDMETHODIMP DHCPServer::COMGETTER(NetworkName) (BSTR *aName)
|
---|
138 | {
|
---|
139 | CheckComArgOutPointerValid(aName);
|
---|
140 |
|
---|
141 | AutoCaller autoCaller (this);
|
---|
142 | CheckComRCReturnRC (autoCaller.rc());
|
---|
143 |
|
---|
144 | mName.cloneTo(aName);
|
---|
145 |
|
---|
146 | return S_OK;
|
---|
147 | }
|
---|
148 |
|
---|
149 | STDMETHODIMP DHCPServer::COMGETTER(Enabled) (BOOL *aEnabled)
|
---|
150 | {
|
---|
151 | CheckComArgOutPointerValid(aEnabled);
|
---|
152 |
|
---|
153 | AutoCaller autoCaller (this);
|
---|
154 | CheckComRCReturnRC (autoCaller.rc());
|
---|
155 |
|
---|
156 | *aEnabled = m.enabled;
|
---|
157 |
|
---|
158 | return S_OK;
|
---|
159 | }
|
---|
160 |
|
---|
161 | STDMETHODIMP DHCPServer::COMSETTER(Enabled) (BOOL aEnabled)
|
---|
162 | {
|
---|
163 | AutoCaller autoCaller (this);
|
---|
164 | CheckComRCReturnRC (autoCaller.rc());
|
---|
165 |
|
---|
166 | /* VirtualBox::saveSettings() needs a write lock */
|
---|
167 | AutoMultiWriteLock2 alock (mVirtualBox, this);
|
---|
168 |
|
---|
169 | m.enabled = aEnabled;
|
---|
170 |
|
---|
171 | HRESULT rc = mVirtualBox->saveSettings();
|
---|
172 |
|
---|
173 | return rc;
|
---|
174 | }
|
---|
175 |
|
---|
176 | STDMETHODIMP DHCPServer::COMGETTER(IPAddress) (BSTR *aIPAddress)
|
---|
177 | {
|
---|
178 | CheckComArgOutPointerValid(aIPAddress);
|
---|
179 |
|
---|
180 | AutoCaller autoCaller (this);
|
---|
181 | CheckComRCReturnRC (autoCaller.rc());
|
---|
182 |
|
---|
183 | m.IPAddress.cloneTo(aIPAddress);
|
---|
184 |
|
---|
185 | return S_OK;
|
---|
186 | }
|
---|
187 |
|
---|
188 | STDMETHODIMP DHCPServer::COMGETTER(NetworkMask) (BSTR *aNetworkMask)
|
---|
189 | {
|
---|
190 | CheckComArgOutPointerValid(aNetworkMask);
|
---|
191 |
|
---|
192 | AutoCaller autoCaller (this);
|
---|
193 | CheckComRCReturnRC (autoCaller.rc());
|
---|
194 |
|
---|
195 | m.networkMask.cloneTo(aNetworkMask);
|
---|
196 |
|
---|
197 | return S_OK;
|
---|
198 | }
|
---|
199 |
|
---|
200 | STDMETHODIMP DHCPServer::COMGETTER(LowerIP) (BSTR *aIPAddress)
|
---|
201 | {
|
---|
202 | CheckComArgOutPointerValid(aIPAddress);
|
---|
203 |
|
---|
204 | AutoCaller autoCaller (this);
|
---|
205 | CheckComRCReturnRC (autoCaller.rc());
|
---|
206 |
|
---|
207 | m.lowerIP.cloneTo(aIPAddress);
|
---|
208 |
|
---|
209 | return S_OK;
|
---|
210 | }
|
---|
211 |
|
---|
212 | STDMETHODIMP DHCPServer::COMGETTER(UpperIP) (BSTR *aIPAddress)
|
---|
213 | {
|
---|
214 | CheckComArgOutPointerValid(aIPAddress);
|
---|
215 |
|
---|
216 | AutoCaller autoCaller (this);
|
---|
217 | CheckComRCReturnRC (autoCaller.rc());
|
---|
218 |
|
---|
219 | m.upperIP.cloneTo(aIPAddress);
|
---|
220 |
|
---|
221 | return S_OK;
|
---|
222 | }
|
---|
223 |
|
---|
224 | STDMETHODIMP DHCPServer::SetConfiguration (IN_BSTR aIPAddress, IN_BSTR aNetworkMask, IN_BSTR aLowerIP, IN_BSTR aUpperIP)
|
---|
225 | {
|
---|
226 | AssertReturn (aIPAddress != NULL, E_INVALIDARG);
|
---|
227 | AssertReturn (aNetworkMask != NULL, E_INVALIDARG);
|
---|
228 | AssertReturn (aLowerIP != NULL, E_INVALIDARG);
|
---|
229 | AssertReturn (aUpperIP != NULL, E_INVALIDARG);
|
---|
230 |
|
---|
231 | AutoCaller autoCaller (this);
|
---|
232 | CheckComRCReturnRC (autoCaller.rc());
|
---|
233 |
|
---|
234 | /* VirtualBox::saveSettings() needs a write lock */
|
---|
235 | AutoMultiWriteLock2 alock (mVirtualBox, this);
|
---|
236 |
|
---|
237 | m.IPAddress = aIPAddress;
|
---|
238 | m.networkMask = aNetworkMask;
|
---|
239 | m.lowerIP = aLowerIP;
|
---|
240 | m.upperIP = aUpperIP;
|
---|
241 |
|
---|
242 | return mVirtualBox->saveSettings();
|
---|
243 | }
|
---|
244 |
|
---|
245 | STDMETHODIMP DHCPServer::Start (IN_BSTR aNetworkName, IN_BSTR aTrunkName, IN_BSTR aTrunkType)
|
---|
246 | {
|
---|
247 | /* Silently ignore attepmts to run disabled servers. */
|
---|
248 | if (!m.enabled)
|
---|
249 | return S_OK;
|
---|
250 |
|
---|
251 | m.dhcp.setOption(DHCPCFG_NETNAME, Utf8Str(aNetworkName));
|
---|
252 | Bstr tmp(aTrunkName);
|
---|
253 | if (!tmp.isEmpty())
|
---|
254 | m.dhcp.setOption(DHCPCFG_TRUNKNAME, Utf8Str(tmp));
|
---|
255 | m.dhcp.setOption(DHCPCFG_TRUNKTYPE, Utf8Str(aTrunkType));
|
---|
256 | //temporary hack for testing
|
---|
257 | // DHCPCFG_NAME
|
---|
258 | char strMAC[13];
|
---|
259 | Guid guid;
|
---|
260 | guid.create();
|
---|
261 | RTStrPrintf (strMAC, sizeof(strMAC), "080027%02X%02X%02X",
|
---|
262 | guid.ptr()->au8[0], guid.ptr()->au8[1], guid.ptr()->au8[2]);
|
---|
263 | m.dhcp.setOption(DHCPCFG_MACADDRESS, strMAC);
|
---|
264 | m.dhcp.setOption(DHCPCFG_IPADDRESS, Utf8Str(m.IPAddress));
|
---|
265 | // DHCPCFG_LEASEDB,
|
---|
266 | // DHCPCFG_VERBOSE,
|
---|
267 | // DHCPCFG_GATEWAY,
|
---|
268 | m.dhcp.setOption(DHCPCFG_LOWERIP, Utf8Str(m.lowerIP));
|
---|
269 | m.dhcp.setOption(DHCPCFG_UPPERIP, Utf8Str(m.upperIP));
|
---|
270 | m.dhcp.setOption(DHCPCFG_NETMASK, Utf8Str(m.networkMask));
|
---|
271 |
|
---|
272 | // DHCPCFG_HELP,
|
---|
273 | // DHCPCFG_VERSION,
|
---|
274 | // DHCPCFG_NOTOPT_MAXVAL
|
---|
275 | m.dhcp.setOption(DHCPCFG_BEGINCONFIG, "");
|
---|
276 |
|
---|
277 | return RT_FAILURE(m.dhcp.start()) ? E_FAIL : S_OK;
|
---|
278 | //m.dhcp.detachFromServer(); /* need to do this to avoid server shutdown on runner destruction */
|
---|
279 | }
|
---|
280 |
|
---|
281 | STDMETHODIMP DHCPServer::Stop (void)
|
---|
282 | {
|
---|
283 | return RT_FAILURE(m.dhcp.stop()) ? E_FAIL : S_OK;
|
---|
284 | }
|
---|