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