VirtualBox

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

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

Non-windows burn fix

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