1 | /* $Id: DHCPServerImpl.cpp 30764 2010-07-09 14:12:12Z vboxsync $ */
|
---|
2 |
|
---|
3 | /** @file
|
---|
4 | *
|
---|
5 | * VirtualBox COM class implementation
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * Copyright (C) 2006-2008 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 S_OK;
|
---|
46 | }
|
---|
47 |
|
---|
48 | void DHCPServer::FinalRelease()
|
---|
49 | {
|
---|
50 | uninit ();
|
---|
51 | }
|
---|
52 |
|
---|
53 | void DHCPServer::uninit()
|
---|
54 | {
|
---|
55 | /* Enclose the state transition Ready->InUninit->NotReady */
|
---|
56 | AutoUninitSpan autoUninitSpan(this);
|
---|
57 | if (autoUninitSpan.uninitDone())
|
---|
58 | return;
|
---|
59 |
|
---|
60 | unconst(mVirtualBox) = NULL;
|
---|
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 | /* Confirm a successful initialization */
|
---|
81 | autoInitSpan.setSucceeded();
|
---|
82 |
|
---|
83 | return S_OK;
|
---|
84 | }
|
---|
85 |
|
---|
86 | HRESULT DHCPServer::init(VirtualBox *aVirtualBox,
|
---|
87 | const settings::DHCPServer &data)
|
---|
88 | {
|
---|
89 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
90 | AutoInitSpan autoInitSpan(this);
|
---|
91 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
92 |
|
---|
93 | /* share VirtualBox weakly (parent remains NULL so far) */
|
---|
94 | unconst(mVirtualBox) = aVirtualBox;
|
---|
95 |
|
---|
96 | unconst(mName) = data.strNetworkName;
|
---|
97 | m.IPAddress = data.strIPAddress;
|
---|
98 | m.networkMask = data.strIPNetworkMask;
|
---|
99 | m.enabled = data.fEnabled;
|
---|
100 | m.lowerIP = data.strIPLower;
|
---|
101 | m.upperIP = data.strIPUpper;
|
---|
102 |
|
---|
103 | autoInitSpan.setSucceeded();
|
---|
104 |
|
---|
105 | return S_OK;
|
---|
106 | }
|
---|
107 |
|
---|
108 | HRESULT DHCPServer::saveSettings(settings::DHCPServer &data)
|
---|
109 | {
|
---|
110 | AutoCaller autoCaller(this);
|
---|
111 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
112 |
|
---|
113 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
114 |
|
---|
115 | data.strNetworkName = mName;
|
---|
116 | data.strIPAddress = m.IPAddress;
|
---|
117 | data.strIPNetworkMask = m.networkMask;
|
---|
118 | data.fEnabled = !!m.enabled;
|
---|
119 | data.strIPLower = m.lowerIP;
|
---|
120 | data.strIPUpper = m.upperIP;
|
---|
121 |
|
---|
122 | return S_OK;
|
---|
123 | }
|
---|
124 |
|
---|
125 | STDMETHODIMP DHCPServer::COMGETTER(NetworkName) (BSTR *aName)
|
---|
126 | {
|
---|
127 | CheckComArgOutPointerValid(aName);
|
---|
128 |
|
---|
129 | AutoCaller autoCaller(this);
|
---|
130 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
131 |
|
---|
132 | mName.cloneTo(aName);
|
---|
133 |
|
---|
134 | return S_OK;
|
---|
135 | }
|
---|
136 |
|
---|
137 | STDMETHODIMP DHCPServer::COMGETTER(Enabled) (BOOL *aEnabled)
|
---|
138 | {
|
---|
139 | CheckComArgOutPointerValid(aEnabled);
|
---|
140 |
|
---|
141 | AutoCaller autoCaller(this);
|
---|
142 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
143 |
|
---|
144 | *aEnabled = m.enabled;
|
---|
145 |
|
---|
146 | return S_OK;
|
---|
147 | }
|
---|
148 |
|
---|
149 | STDMETHODIMP DHCPServer::COMSETTER(Enabled) (BOOL aEnabled)
|
---|
150 | {
|
---|
151 | AutoCaller autoCaller(this);
|
---|
152 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
153 |
|
---|
154 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
155 | m.enabled = aEnabled;
|
---|
156 |
|
---|
157 | // save the global settings; for that we should hold only the VirtualBox lock
|
---|
158 | alock.release();
|
---|
159 | AutoWriteLock vboxLock(mVirtualBox COMMA_LOCKVAL_SRC_POS);
|
---|
160 | HRESULT rc = mVirtualBox->saveSettings();
|
---|
161 |
|
---|
162 | return rc;
|
---|
163 | }
|
---|
164 |
|
---|
165 | STDMETHODIMP DHCPServer::COMGETTER(IPAddress) (BSTR *aIPAddress)
|
---|
166 | {
|
---|
167 | CheckComArgOutPointerValid(aIPAddress);
|
---|
168 |
|
---|
169 | AutoCaller autoCaller(this);
|
---|
170 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
171 |
|
---|
172 | m.IPAddress.cloneTo(aIPAddress);
|
---|
173 |
|
---|
174 | return S_OK;
|
---|
175 | }
|
---|
176 |
|
---|
177 | STDMETHODIMP DHCPServer::COMGETTER(NetworkMask) (BSTR *aNetworkMask)
|
---|
178 | {
|
---|
179 | CheckComArgOutPointerValid(aNetworkMask);
|
---|
180 |
|
---|
181 | AutoCaller autoCaller(this);
|
---|
182 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
183 |
|
---|
184 | m.networkMask.cloneTo(aNetworkMask);
|
---|
185 |
|
---|
186 | return S_OK;
|
---|
187 | }
|
---|
188 |
|
---|
189 | STDMETHODIMP DHCPServer::COMGETTER(LowerIP) (BSTR *aIPAddress)
|
---|
190 | {
|
---|
191 | CheckComArgOutPointerValid(aIPAddress);
|
---|
192 |
|
---|
193 | AutoCaller autoCaller(this);
|
---|
194 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
195 |
|
---|
196 | m.lowerIP.cloneTo(aIPAddress);
|
---|
197 |
|
---|
198 | return S_OK;
|
---|
199 | }
|
---|
200 |
|
---|
201 | STDMETHODIMP DHCPServer::COMGETTER(UpperIP) (BSTR *aIPAddress)
|
---|
202 | {
|
---|
203 | CheckComArgOutPointerValid(aIPAddress);
|
---|
204 |
|
---|
205 | AutoCaller autoCaller(this);
|
---|
206 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
207 |
|
---|
208 | m.upperIP.cloneTo(aIPAddress);
|
---|
209 |
|
---|
210 | return S_OK;
|
---|
211 | }
|
---|
212 |
|
---|
213 | STDMETHODIMP DHCPServer::SetConfiguration (IN_BSTR aIPAddress, IN_BSTR aNetworkMask, IN_BSTR aLowerIP, IN_BSTR aUpperIP)
|
---|
214 | {
|
---|
215 | AssertReturn(aIPAddress != NULL, E_INVALIDARG);
|
---|
216 | AssertReturn(aNetworkMask != NULL, E_INVALIDARG);
|
---|
217 | AssertReturn(aLowerIP != NULL, E_INVALIDARG);
|
---|
218 | AssertReturn(aUpperIP != NULL, E_INVALIDARG);
|
---|
219 |
|
---|
220 | AutoCaller autoCaller(this);
|
---|
221 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
222 |
|
---|
223 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
224 | m.IPAddress = aIPAddress;
|
---|
225 | m.networkMask = aNetworkMask;
|
---|
226 | m.lowerIP = aLowerIP;
|
---|
227 | m.upperIP = aUpperIP;
|
---|
228 |
|
---|
229 | // save the global settings; for that we should hold only the VirtualBox lock
|
---|
230 | alock.release();
|
---|
231 | AutoWriteLock vboxLock(mVirtualBox COMMA_LOCKVAL_SRC_POS);
|
---|
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 | }
|
---|