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