1 | /* $Id: DHCPServerImpl.cpp 35638 2011-01-19 19:10:49Z vboxsync $ */
|
---|
2 |
|
---|
3 | /** @file
|
---|
4 | *
|
---|
5 | * VirtualBox COM class implementation
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * Copyright (C) 2006-2010 Oracle Corporation
|
---|
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 |
|
---|
20 | #include "DHCPServerRunner.h"
|
---|
21 | #include "DHCPServerImpl.h"
|
---|
22 | #include "AutoCaller.h"
|
---|
23 | #include "Logging.h"
|
---|
24 |
|
---|
25 | #include <iprt/cpp/utils.h>
|
---|
26 |
|
---|
27 | #include <VBox/settings.h>
|
---|
28 |
|
---|
29 | #include "VirtualBoxImpl.h"
|
---|
30 |
|
---|
31 | // constructor / destructor
|
---|
32 | /////////////////////////////////////////////////////////////////////////////
|
---|
33 |
|
---|
34 | DHCPServer::DHCPServer()
|
---|
35 | : mVirtualBox(NULL)
|
---|
36 | {
|
---|
37 | }
|
---|
38 |
|
---|
39 | DHCPServer::~DHCPServer()
|
---|
40 | {
|
---|
41 | }
|
---|
42 |
|
---|
43 | HRESULT DHCPServer::FinalConstruct()
|
---|
44 | {
|
---|
45 | return BaseFinalConstruct();
|
---|
46 | }
|
---|
47 |
|
---|
48 | void DHCPServer::FinalRelease()
|
---|
49 | {
|
---|
50 | uninit ();
|
---|
51 |
|
---|
52 | BaseFinalRelease();
|
---|
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 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
157 | m.enabled = aEnabled;
|
---|
158 |
|
---|
159 | // save the global settings; for that we should hold only the VirtualBox lock
|
---|
160 | alock.release();
|
---|
161 | AutoWriteLock vboxLock(mVirtualBox COMMA_LOCKVAL_SRC_POS);
|
---|
162 | HRESULT rc = mVirtualBox->saveSettings();
|
---|
163 |
|
---|
164 | return rc;
|
---|
165 | }
|
---|
166 |
|
---|
167 | STDMETHODIMP DHCPServer::COMGETTER(IPAddress) (BSTR *aIPAddress)
|
---|
168 | {
|
---|
169 | CheckComArgOutPointerValid(aIPAddress);
|
---|
170 |
|
---|
171 | AutoCaller autoCaller(this);
|
---|
172 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
173 |
|
---|
174 | m.IPAddress.cloneTo(aIPAddress);
|
---|
175 |
|
---|
176 | return S_OK;
|
---|
177 | }
|
---|
178 |
|
---|
179 | STDMETHODIMP DHCPServer::COMGETTER(NetworkMask) (BSTR *aNetworkMask)
|
---|
180 | {
|
---|
181 | CheckComArgOutPointerValid(aNetworkMask);
|
---|
182 |
|
---|
183 | AutoCaller autoCaller(this);
|
---|
184 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
185 |
|
---|
186 | m.networkMask.cloneTo(aNetworkMask);
|
---|
187 |
|
---|
188 | return S_OK;
|
---|
189 | }
|
---|
190 |
|
---|
191 | STDMETHODIMP DHCPServer::COMGETTER(LowerIP) (BSTR *aIPAddress)
|
---|
192 | {
|
---|
193 | CheckComArgOutPointerValid(aIPAddress);
|
---|
194 |
|
---|
195 | AutoCaller autoCaller(this);
|
---|
196 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
197 |
|
---|
198 | m.lowerIP.cloneTo(aIPAddress);
|
---|
199 |
|
---|
200 | return S_OK;
|
---|
201 | }
|
---|
202 |
|
---|
203 | STDMETHODIMP DHCPServer::COMGETTER(UpperIP) (BSTR *aIPAddress)
|
---|
204 | {
|
---|
205 | CheckComArgOutPointerValid(aIPAddress);
|
---|
206 |
|
---|
207 | AutoCaller autoCaller(this);
|
---|
208 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
209 |
|
---|
210 | m.upperIP.cloneTo(aIPAddress);
|
---|
211 |
|
---|
212 | return S_OK;
|
---|
213 | }
|
---|
214 |
|
---|
215 | STDMETHODIMP DHCPServer::SetConfiguration (IN_BSTR aIPAddress, IN_BSTR aNetworkMask, IN_BSTR aLowerIP, IN_BSTR aUpperIP)
|
---|
216 | {
|
---|
217 | AssertReturn(aIPAddress != NULL, E_INVALIDARG);
|
---|
218 | AssertReturn(aNetworkMask != NULL, E_INVALIDARG);
|
---|
219 | AssertReturn(aLowerIP != NULL, E_INVALIDARG);
|
---|
220 | AssertReturn(aUpperIP != NULL, E_INVALIDARG);
|
---|
221 |
|
---|
222 | AutoCaller autoCaller(this);
|
---|
223 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
224 |
|
---|
225 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
226 | m.IPAddress = aIPAddress;
|
---|
227 | m.networkMask = aNetworkMask;
|
---|
228 | m.lowerIP = aLowerIP;
|
---|
229 | m.upperIP = aUpperIP;
|
---|
230 |
|
---|
231 | // save the global settings; for that we should hold only the VirtualBox lock
|
---|
232 | alock.release();
|
---|
233 | AutoWriteLock vboxLock(mVirtualBox COMMA_LOCKVAL_SRC_POS);
|
---|
234 | return mVirtualBox->saveSettings();
|
---|
235 | }
|
---|
236 |
|
---|
237 | STDMETHODIMP DHCPServer::Start(IN_BSTR aNetworkName, IN_BSTR aTrunkName, IN_BSTR aTrunkType)
|
---|
238 | {
|
---|
239 | /* Silently ignore attempts to run disabled servers. */
|
---|
240 | if (!m.enabled)
|
---|
241 | return S_OK;
|
---|
242 |
|
---|
243 | m.dhcp.setOption(DHCPCFG_NETNAME, Utf8Str(aNetworkName), true);
|
---|
244 | Bstr tmp(aTrunkName);
|
---|
245 | if (!tmp.isEmpty())
|
---|
246 | m.dhcp.setOption(DHCPCFG_TRUNKNAME, Utf8Str(tmp), true);
|
---|
247 | m.dhcp.setOption(DHCPCFG_TRUNKTYPE, Utf8Str(aTrunkType), true);
|
---|
248 | //temporary hack for testing
|
---|
249 | // DHCPCFG_NAME
|
---|
250 | char strMAC[32];
|
---|
251 | Guid guid;
|
---|
252 | guid.create();
|
---|
253 | RTStrPrintf (strMAC, sizeof(strMAC), "08:00:27:%02X:%02X:%02X",
|
---|
254 | guid.raw()->au8[0], guid.raw()->au8[1], guid.raw()->au8[2]);
|
---|
255 | m.dhcp.setOption(DHCPCFG_MACADDRESS, strMAC, true);
|
---|
256 | m.dhcp.setOption(DHCPCFG_IPADDRESS, Utf8Str(m.IPAddress), true);
|
---|
257 | // DHCPCFG_LEASEDB,
|
---|
258 | // DHCPCFG_VERBOSE,
|
---|
259 | // DHCPCFG_GATEWAY,
|
---|
260 | m.dhcp.setOption(DHCPCFG_LOWERIP, Utf8Str(m.lowerIP), true);
|
---|
261 | m.dhcp.setOption(DHCPCFG_UPPERIP, Utf8Str(m.upperIP), true);
|
---|
262 | m.dhcp.setOption(DHCPCFG_NETMASK, Utf8Str(m.networkMask), true);
|
---|
263 |
|
---|
264 | // DHCPCFG_HELP,
|
---|
265 | // DHCPCFG_VERSION,
|
---|
266 | // DHCPCFG_NOTOPT_MAXVAL
|
---|
267 | m.dhcp.setOption(DHCPCFG_BEGINCONFIG, "", true);
|
---|
268 |
|
---|
269 | return RT_FAILURE(m.dhcp.start()) ? E_FAIL : S_OK;
|
---|
270 | //m.dhcp.detachFromServer(); /* need to do this to avoid server shutdown on runner destruction */
|
---|
271 | }
|
---|
272 |
|
---|
273 | STDMETHODIMP DHCPServer::Stop (void)
|
---|
274 | {
|
---|
275 | return RT_FAILURE(m.dhcp.stop()) ? E_FAIL : S_OK;
|
---|
276 | }
|
---|