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