VirtualBox

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

Last change on this file since 25184 was 25184, checked in by vboxsync, 15 years ago

Main: bring back r55600 with fix for broken machine creation

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