VirtualBox

source: vbox/trunk/src/VBox/Main/DHCPServerImpl.cpp@ 30760

Last change on this file since 30760 was 30760, checked in by vboxsync, 14 years ago

Main: separate internal machine data structs into MachineImplPrivate.h to significantly speed up compilation and for better interface separation; remove obsolete ConsoleEvents.h file

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette