1 | /* $Id: DHCPServerImpl.cpp 28800 2010-04-27 08:22:32Z 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 | /* VirtualBox::saveSettings() needs a write lock */
|
---|
153 | AutoMultiWriteLock2 alock(mVirtualBox, this COMMA_LOCKVAL_SRC_POS);
|
---|
154 |
|
---|
155 | m.enabled = aEnabled;
|
---|
156 |
|
---|
157 | HRESULT rc = mVirtualBox->saveSettings();
|
---|
158 |
|
---|
159 | return rc;
|
---|
160 | }
|
---|
161 |
|
---|
162 | STDMETHODIMP DHCPServer::COMGETTER(IPAddress) (BSTR *aIPAddress)
|
---|
163 | {
|
---|
164 | CheckComArgOutPointerValid(aIPAddress);
|
---|
165 |
|
---|
166 | AutoCaller autoCaller(this);
|
---|
167 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
168 |
|
---|
169 | m.IPAddress.cloneTo(aIPAddress);
|
---|
170 |
|
---|
171 | return S_OK;
|
---|
172 | }
|
---|
173 |
|
---|
174 | STDMETHODIMP DHCPServer::COMGETTER(NetworkMask) (BSTR *aNetworkMask)
|
---|
175 | {
|
---|
176 | CheckComArgOutPointerValid(aNetworkMask);
|
---|
177 |
|
---|
178 | AutoCaller autoCaller(this);
|
---|
179 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
180 |
|
---|
181 | m.networkMask.cloneTo(aNetworkMask);
|
---|
182 |
|
---|
183 | return S_OK;
|
---|
184 | }
|
---|
185 |
|
---|
186 | STDMETHODIMP DHCPServer::COMGETTER(LowerIP) (BSTR *aIPAddress)
|
---|
187 | {
|
---|
188 | CheckComArgOutPointerValid(aIPAddress);
|
---|
189 |
|
---|
190 | AutoCaller autoCaller(this);
|
---|
191 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
192 |
|
---|
193 | m.lowerIP.cloneTo(aIPAddress);
|
---|
194 |
|
---|
195 | return S_OK;
|
---|
196 | }
|
---|
197 |
|
---|
198 | STDMETHODIMP DHCPServer::COMGETTER(UpperIP) (BSTR *aIPAddress)
|
---|
199 | {
|
---|
200 | CheckComArgOutPointerValid(aIPAddress);
|
---|
201 |
|
---|
202 | AutoCaller autoCaller(this);
|
---|
203 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
204 |
|
---|
205 | m.upperIP.cloneTo(aIPAddress);
|
---|
206 |
|
---|
207 | return S_OK;
|
---|
208 | }
|
---|
209 |
|
---|
210 | STDMETHODIMP DHCPServer::SetConfiguration (IN_BSTR aIPAddress, IN_BSTR aNetworkMask, IN_BSTR aLowerIP, IN_BSTR aUpperIP)
|
---|
211 | {
|
---|
212 | AssertReturn(aIPAddress != NULL, E_INVALIDARG);
|
---|
213 | AssertReturn(aNetworkMask != NULL, E_INVALIDARG);
|
---|
214 | AssertReturn(aLowerIP != NULL, E_INVALIDARG);
|
---|
215 | AssertReturn(aUpperIP != NULL, E_INVALIDARG);
|
---|
216 |
|
---|
217 | AutoCaller autoCaller(this);
|
---|
218 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
219 |
|
---|
220 | /* VirtualBox::saveSettings() needs a write lock */
|
---|
221 | AutoMultiWriteLock2 alock(mVirtualBox, this COMMA_LOCKVAL_SRC_POS);
|
---|
222 |
|
---|
223 | m.IPAddress = aIPAddress;
|
---|
224 | m.networkMask = aNetworkMask;
|
---|
225 | m.lowerIP = aLowerIP;
|
---|
226 | m.upperIP = aUpperIP;
|
---|
227 |
|
---|
228 | return mVirtualBox->saveSettings();
|
---|
229 | }
|
---|
230 |
|
---|
231 | STDMETHODIMP DHCPServer::Start(IN_BSTR aNetworkName, IN_BSTR aTrunkName, IN_BSTR aTrunkType)
|
---|
232 | {
|
---|
233 | /* Silently ignore attepmts to run disabled servers. */
|
---|
234 | if (!m.enabled)
|
---|
235 | return S_OK;
|
---|
236 |
|
---|
237 | m.dhcp.setOption(DHCPCFG_NETNAME, Utf8Str(aNetworkName), true);
|
---|
238 | Bstr tmp(aTrunkName);
|
---|
239 | if (!tmp.isEmpty())
|
---|
240 | m.dhcp.setOption(DHCPCFG_TRUNKNAME, Utf8Str(tmp), true);
|
---|
241 | m.dhcp.setOption(DHCPCFG_TRUNKTYPE, Utf8Str(aTrunkType), true);
|
---|
242 | //temporary hack for testing
|
---|
243 | // DHCPCFG_NAME
|
---|
244 | char strMAC[32];
|
---|
245 | Guid guid;
|
---|
246 | guid.create();
|
---|
247 | RTStrPrintf (strMAC, sizeof(strMAC), "08:00:27:%02X:%02X:%02X",
|
---|
248 | guid.ptr()->au8[0], guid.ptr()->au8[1], guid.ptr()->au8[2]);
|
---|
249 | m.dhcp.setOption(DHCPCFG_MACADDRESS, strMAC, true);
|
---|
250 | m.dhcp.setOption(DHCPCFG_IPADDRESS, Utf8Str(m.IPAddress), true);
|
---|
251 | // DHCPCFG_LEASEDB,
|
---|
252 | // DHCPCFG_VERBOSE,
|
---|
253 | // DHCPCFG_GATEWAY,
|
---|
254 | m.dhcp.setOption(DHCPCFG_LOWERIP, Utf8Str(m.lowerIP), true);
|
---|
255 | m.dhcp.setOption(DHCPCFG_UPPERIP, Utf8Str(m.upperIP), true);
|
---|
256 | m.dhcp.setOption(DHCPCFG_NETMASK, Utf8Str(m.networkMask), true);
|
---|
257 |
|
---|
258 | // DHCPCFG_HELP,
|
---|
259 | // DHCPCFG_VERSION,
|
---|
260 | // DHCPCFG_NOTOPT_MAXVAL
|
---|
261 | m.dhcp.setOption(DHCPCFG_BEGINCONFIG, "", true);
|
---|
262 |
|
---|
263 | return RT_FAILURE(m.dhcp.start()) ? E_FAIL : S_OK;
|
---|
264 | //m.dhcp.detachFromServer(); /* need to do this to avoid server shutdown on runner destruction */
|
---|
265 | }
|
---|
266 |
|
---|
267 | STDMETHODIMP DHCPServer::Stop (void)
|
---|
268 | {
|
---|
269 | return RT_FAILURE(m.dhcp.stop()) ? E_FAIL : S_OK;
|
---|
270 | }
|
---|