VirtualBox

source: vbox/trunk/src/VBox/Main/HostNetworkInterfaceImpl.cpp@ 17759

Last change on this file since 17759 was 17759, checked in by vboxsync, 16 years ago

Main/NetIf: IPv4 properties changed from ULONG to wstring, VBoxManage fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.6 KB
Line 
1/* $Id: HostNetworkInterfaceImpl.cpp 17759 2009-03-12 15:57:18Z 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 "HostNetworkInterfaceImpl.h"
25#include "Logging.h"
26#include "netif.h"
27
28// constructor / destructor
29/////////////////////////////////////////////////////////////////////////////
30
31DEFINE_EMPTY_CTOR_DTOR (HostNetworkInterface)
32
33HRESULT HostNetworkInterface::FinalConstruct()
34{
35 return S_OK;
36}
37
38void HostNetworkInterface::FinalRelease()
39{
40 uninit ();
41}
42
43// public initializer/uninitializer for internal purposes only
44/////////////////////////////////////////////////////////////////////////////
45
46/**
47 * Initializes the host object.
48 *
49 * @returns COM result indicator
50 * @param aInterfaceName name of the network interface
51 * @param aGuid GUID of the host network interface
52 */
53HRESULT HostNetworkInterface::init (Bstr aInterfaceName, Guid aGuid, HostNetworkInterfaceType_T ifType)
54{
55 LogFlowThisFunc (("aInterfaceName={%ls}, aGuid={%s}\n",
56 aInterfaceName.raw(), aGuid.toString().raw()));
57
58 ComAssertRet (aInterfaceName, E_INVALIDARG);
59 ComAssertRet (!aGuid.isEmpty(), E_INVALIDARG);
60
61 /* Enclose the state transition NotReady->InInit->Ready */
62 AutoInitSpan autoInitSpan (this);
63 AssertReturn (autoInitSpan.isOk(), E_FAIL);
64
65 unconst (mInterfaceName) = aInterfaceName;
66 unconst (mGuid) = aGuid;
67 mIfType = ifType;
68
69
70 /* Confirm a successful initialization */
71 autoInitSpan.setSucceeded();
72
73 return S_OK;
74}
75
76#ifdef VBOX_WITH_HOSTNETIF_API
77
78HRESULT HostNetworkInterface::updateConfig ()
79{
80 NETIFINFO info;
81 int rc = NetIfGetConfig(this, &info);
82 if(RT_SUCCESS(rc))
83 {
84 m.IPAddress = info.IPAddress.u;
85 m.networkMask = info.IPNetMask.u;
86 m.dhcpEnabled = info.bDhcpEnabled;
87 m.IPV6Address = composeIPv6Address(&info.IPv6Address);
88 m.IPV6NetworkMaskPrefixLength = composeIPv6PrefixLenghFromAddress(&info.IPv6NetMask);
89 m.hardwareAddress = composeHardwareAddress(&info.MACAddress);
90#ifdef RT_OS_WINDOWS
91 m.mediumType = (HostNetworkInterfaceMediumType)info.enmMediumType;
92 m.status = (HostNetworkInterfaceStatus)info.enmStatus;
93#else /* !RT_OS_WINDOWS */
94 m.mediumType = info.enmMediumType;
95 m.status = info.enmStatus;
96
97#endif /* !RT_OS_WINDOWS */
98 return S_OK;
99 }
100 return rc == VERR_NOT_IMPLEMENTED ? E_NOTIMPL : E_FAIL;
101}
102
103/**
104 * Initializes the host object.
105 *
106 * @returns COM result indicator
107 * @param aInterfaceName name of the network interface
108 * @param aGuid GUID of the host network interface
109 */
110HRESULT HostNetworkInterface::init (Bstr aInterfaceName, HostNetworkInterfaceType_T ifType, PNETIFINFO pIf)
111{
112// LogFlowThisFunc (("aInterfaceName={%ls}, aGuid={%s}\n",
113// aInterfaceName.raw(), aGuid.toString().raw()));
114
115// ComAssertRet (aInterfaceName, E_INVALIDARG);
116// ComAssertRet (!aGuid.isEmpty(), E_INVALIDARG);
117 ComAssertRet (pIf, E_INVALIDARG);
118
119 /* Enclose the state transition NotReady->InInit->Ready */
120 AutoInitSpan autoInitSpan (this);
121 AssertReturn (autoInitSpan.isOk(), E_FAIL);
122
123 unconst (mInterfaceName) = aInterfaceName;
124 unconst (mGuid) = pIf->Uuid;
125 mIfType = ifType;
126
127 m.IPAddress = pIf->IPAddress.u;
128 m.networkMask = pIf->IPNetMask.u;
129 m.IPV6Address = composeIPv6Address(&pIf->IPv6Address);
130 m.IPV6NetworkMaskPrefixLength = composeIPv6PrefixLenghFromAddress(&pIf->IPv6NetMask);
131 m.dhcpEnabled = pIf->bDhcpEnabled;
132 m.hardwareAddress = composeHardwareAddress(&pIf->MACAddress);
133#ifdef RT_OS_WINDOWS
134 m.mediumType = (HostNetworkInterfaceMediumType)pIf->enmMediumType;
135 m.status = (HostNetworkInterfaceStatus)pIf->enmStatus;
136#else /* !RT_OS_WINDOWS */
137 m.mediumType = pIf->enmMediumType;
138 m.status = pIf->enmStatus;
139#endif /* !RT_OS_WINDOWS */
140
141 /* Confirm a successful initialization */
142 autoInitSpan.setSucceeded();
143
144 return S_OK;
145}
146#endif
147
148// IHostNetworkInterface properties
149/////////////////////////////////////////////////////////////////////////////
150
151/**
152 * Returns the name of the host network interface.
153 *
154 * @returns COM status code
155 * @param aInterfaceName address of result pointer
156 */
157STDMETHODIMP HostNetworkInterface::COMGETTER(Name) (BSTR *aInterfaceName)
158{
159 CheckComArgOutPointerValid(aInterfaceName);
160
161 AutoCaller autoCaller (this);
162 CheckComRCReturnRC (autoCaller.rc());
163
164 mInterfaceName.cloneTo (aInterfaceName);
165
166 return S_OK;
167}
168
169/**
170 * Returns the GUID of the host network interface.
171 *
172 * @returns COM status code
173 * @param aGuid address of result pointer
174 */
175STDMETHODIMP HostNetworkInterface::COMGETTER(Id) (OUT_GUID aGuid)
176{
177 CheckComArgOutPointerValid(aGuid);
178
179 AutoCaller autoCaller (this);
180 CheckComRCReturnRC (autoCaller.rc());
181
182 mGuid.cloneTo (aGuid);
183
184 return S_OK;
185}
186
187STDMETHODIMP HostNetworkInterface::COMGETTER(DhcpEnabled) (BOOL *aDhcpEnabled)
188{
189 CheckComArgOutPointerValid(aDhcpEnabled);
190
191 AutoCaller autoCaller (this);
192 CheckComRCReturnRC (autoCaller.rc());
193
194 *aDhcpEnabled = m.dhcpEnabled;
195
196 return S_OK;
197}
198
199
200/**
201 * Returns the IP address of the host network interface.
202 *
203 * @returns COM status code
204 * @param aIPAddress address of result pointer
205 */
206STDMETHODIMP HostNetworkInterface::COMGETTER(IPAddress) (BSTR *aIPAddress)
207{
208 CheckComArgOutPointerValid(aIPAddress);
209
210 AutoCaller autoCaller (this);
211 CheckComRCReturnRC (autoCaller.rc());
212
213 in_addr tmp;
214 tmp.S_un.S_addr = m.IPAddress;
215 char *addr = inet_ntoa(tmp);
216 if(addr)
217 {
218 Bstr(addr).detachTo(aIPAddress);
219 return S_OK;
220 }
221
222 return E_FAIL;
223}
224
225/**
226 * Returns the netwok mask of the host network interface.
227 *
228 * @returns COM status code
229 * @param aNetworkMask address of result pointer
230 */
231STDMETHODIMP HostNetworkInterface::COMGETTER(NetworkMask) (BSTR *aNetworkMask)
232{
233 CheckComArgOutPointerValid(aNetworkMask);
234
235 AutoCaller autoCaller (this);
236 CheckComRCReturnRC (autoCaller.rc());
237
238 in_addr tmp;
239 tmp.S_un.S_addr = m.networkMask;
240 char *addr = inet_ntoa(tmp);
241 if(addr)
242 {
243 Bstr(addr).detachTo(aNetworkMask);
244 return S_OK;
245 }
246
247 return E_FAIL;
248}
249
250STDMETHODIMP HostNetworkInterface::COMGETTER(IPV6Supported) (BOOL *aIPV6Supported)
251{
252 CheckComArgOutPointerValid(aIPV6Supported);
253#if defined(RT_OS_WINDOWS)
254 *aIPV6Supported = FALSE;
255#else
256 *aIPV6Supported = TRUE;
257#endif
258
259 return S_OK;
260}
261
262/**
263 * Returns the IP V6 address of the host network interface.
264 *
265 * @returns COM status code
266 * @param aIPV6Address address of result pointer
267 */
268STDMETHODIMP HostNetworkInterface::COMGETTER(IPV6Address) (BSTR *aIPV6Address)
269{
270 CheckComArgOutPointerValid(aIPV6Address);
271
272 AutoCaller autoCaller (this);
273 CheckComRCReturnRC (autoCaller.rc());
274
275 m.IPV6Address.cloneTo (aIPV6Address);
276
277 return S_OK;
278}
279
280/**
281 * Returns the IP V6 network mask of the host network interface.
282 *
283 * @returns COM status code
284 * @param aIPV6Mask address of result pointer
285 */
286STDMETHODIMP HostNetworkInterface::COMGETTER(IPV6NetworkMaskPrefixLength) (ULONG *aIPV6NetworkMaskPrefixLength)
287{
288 CheckComArgOutPointerValid(aIPV6NetworkMaskPrefixLength);
289
290 AutoCaller autoCaller (this);
291 CheckComRCReturnRC (autoCaller.rc());
292
293 *aIPV6NetworkMaskPrefixLength = m.IPV6NetworkMaskPrefixLength;
294
295 return S_OK;
296}
297
298/**
299 * Returns the hardware address of the host network interface.
300 *
301 * @returns COM status code
302 * @param aHardwareAddress address of result pointer
303 */
304STDMETHODIMP HostNetworkInterface::COMGETTER(HardwareAddress) (BSTR *aHardwareAddress)
305{
306 CheckComArgOutPointerValid(aHardwareAddress);
307
308 AutoCaller autoCaller (this);
309 CheckComRCReturnRC (autoCaller.rc());
310
311 m.hardwareAddress.cloneTo (aHardwareAddress);
312
313 return S_OK;
314}
315
316/**
317 * Returns the encapsulation protocol type of the host network interface.
318 *
319 * @returns COM status code
320 * @param aType address of result pointer
321 */
322STDMETHODIMP HostNetworkInterface::COMGETTER(MediumType) (HostNetworkInterfaceMediumType_T *aType)
323{
324 CheckComArgOutPointerValid(aType);
325
326 AutoCaller autoCaller (this);
327 CheckComRCReturnRC (autoCaller.rc());
328
329 *aType = m.mediumType;
330
331 return S_OK;
332}
333
334/**
335 * Returns the current state of the host network interface.
336 *
337 * @returns COM status code
338 * @param aStatus address of result pointer
339 */
340STDMETHODIMP HostNetworkInterface::COMGETTER(Status) (HostNetworkInterfaceStatus_T *aStatus)
341{
342 CheckComArgOutPointerValid(aStatus);
343
344 AutoCaller autoCaller (this);
345 CheckComRCReturnRC (autoCaller.rc());
346
347 *aStatus = m.status;
348
349 return S_OK;
350}
351
352/**
353 * Returns network interface type
354 *
355 * @returns COM status code
356 * @param aType address of result pointer
357 */
358STDMETHODIMP HostNetworkInterface::COMGETTER(InterfaceType) (HostNetworkInterfaceType_T *aType)
359{
360 CheckComArgOutPointerValid(aType);
361
362 AutoCaller autoCaller (this);
363 CheckComRCReturnRC (autoCaller.rc());
364
365 *aType = mIfType;
366
367 return S_OK;
368
369}
370
371STDMETHODIMP HostNetworkInterface::EnableStaticIpConfig (IN_BSTR aIPAddress, IN_BSTR aNetMask)
372{
373#ifndef VBOX_WITH_HOSTNETIF_API
374 return E_NOTIMPL;
375#else
376 AutoCaller autoCaller (this);
377 CheckComRCReturnRC (autoCaller.rc());
378
379 ULONG ip, mask;
380 ip = inet_addr(Utf8Str(aIPAddress).raw());
381 if(ip != INADDR_NONE)
382 {
383 mask = inet_addr(Utf8Str(aNetMask).raw());
384 if(mask != INADDR_NONE)
385 {
386 int rc = NetIfEnableStaticIpConfig(mVBox, this, ip, mask);
387 if (RT_SUCCESS(rc))
388 {
389 return S_OK;
390 }
391 else
392 {
393 LogRel(("Failed to EnableStaticIpConfig with rc=%Vrc\n", rc));
394 return rc == VERR_NOT_IMPLEMENTED ? E_NOTIMPL : E_FAIL;
395 }
396
397 }
398 }
399 return E_FAIL;
400#endif
401}
402
403STDMETHODIMP HostNetworkInterface::EnableStaticIpConfigV6 (IN_BSTR aIPV6Address, ULONG aIPV6MaskPrefixLength)
404{
405#ifndef VBOX_WITH_HOSTNETIF_API
406 return E_NOTIMPL;
407#else
408 if (!aIPV6Address)
409 return E_INVALIDARG;
410 if (aIPV6MaskPrefixLength > 128)
411 return E_INVALIDARG;
412
413 AutoCaller autoCaller (this);
414 CheckComRCReturnRC (autoCaller.rc());
415
416 int rc = NetIfEnableStaticIpConfigV6(mVBox, this, aIPV6Address, aIPV6MaskPrefixLength);
417 if (RT_FAILURE(rc))
418 {
419 LogRel(("Failed to EnableStaticIpConfigV6 with rc=%Vrc\n", rc));
420 return rc == VERR_NOT_IMPLEMENTED ? E_NOTIMPL : E_FAIL;
421 }
422 return S_OK;
423#endif
424}
425
426STDMETHODIMP HostNetworkInterface::EnableDynamicIpConfig ()
427{
428#ifndef VBOX_WITH_HOSTNETIF_API
429 return E_NOTIMPL;
430#else
431 AutoCaller autoCaller (this);
432 CheckComRCReturnRC (autoCaller.rc());
433
434 int rc = NetIfEnableDynamicIpConfig(mVBox, this);
435 if (RT_FAILURE(rc))
436 {
437 LogRel(("Failed to EnableStaticIpConfigV6 with rc=%Vrc\n", rc));
438 return rc == VERR_NOT_IMPLEMENTED ? E_NOTIMPL : E_FAIL;
439 }
440 return S_OK;
441#endif
442}
443
444HRESULT HostNetworkInterface::setVirtualBox(VirtualBox *pVBox)
445{
446 AutoCaller autoCaller (this);
447 CheckComRCReturnRC (autoCaller.rc());
448 mVBox = pVBox;
449
450 return S_OK;
451}
452
453/* vi: set tabstop=4 shiftwidth=4 expandtab: */
Note: See TracBrowser for help on using the repository browser.

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