VirtualBox

source: vbox/trunk/src/VBox/Main/src-server/HostNetworkInterfaceImpl.cpp@ 49963

Last change on this file since 49963 was 49872, checked in by vboxsync, 11 years ago

6813 stage 4 - bug removal

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 18.8 KB
Line 
1/* $Id: HostNetworkInterfaceImpl.cpp 49872 2013-12-10 17:22:44Z vboxsync $ */
2
3/** @file
4 *
5 * VirtualBox COM class implementation
6 */
7
8/*
9 * Copyright (C) 2006-2013 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#include "HostNetworkInterfaceImpl.h"
20#include "AutoCaller.h"
21#include "Logging.h"
22#include "netif.h"
23#ifdef VBOX_WITH_RESOURCE_USAGE_API
24# include "Performance.h"
25# include "PerformanceImpl.h"
26#endif
27
28#include <iprt/cpp/utils.h>
29
30#ifdef RT_OS_FREEBSD
31# include <netinet/in.h> /* INADDR_NONE */
32#endif /* RT_OS_FREEBSD */
33
34#include "VirtualBoxImpl.h"
35
36// constructor / destructor
37/////////////////////////////////////////////////////////////////////////////
38
39HostNetworkInterface::HostNetworkInterface()
40 : mVBox(NULL)
41{
42}
43
44HostNetworkInterface::~HostNetworkInterface()
45{
46}
47
48HRESULT HostNetworkInterface::FinalConstruct()
49{
50 return BaseFinalConstruct();
51}
52
53void HostNetworkInterface::FinalRelease()
54{
55 uninit();
56 BaseFinalRelease();
57}
58
59// public initializer/uninitializer for internal purposes only
60/////////////////////////////////////////////////////////////////////////////
61
62/**
63 * Initializes the host object.
64 *
65 * @returns COM result indicator
66 * @param aInterfaceName name of the network interface
67 * @param aGuid GUID of the host network interface
68 */
69HRESULT HostNetworkInterface::init(Bstr aInterfaceName, Bstr aShortName, Guid aGuid, HostNetworkInterfaceType_T ifType)
70{
71 LogFlowThisFunc(("aInterfaceName={%ls}, aGuid={%s}\n",
72 aInterfaceName.raw(), aGuid.toString().c_str()));
73
74 ComAssertRet(!aInterfaceName.isEmpty(), E_INVALIDARG);
75 ComAssertRet(aGuid.isValid(), E_INVALIDARG);
76
77 /* Enclose the state transition NotReady->InInit->Ready */
78 AutoInitSpan autoInitSpan(this);
79 AssertReturn(autoInitSpan.isOk(), E_FAIL);
80
81 unconst(mInterfaceName) = aInterfaceName;
82 unconst(mNetworkName) = i_composeNetworkName(aShortName);
83 unconst(mShortName) = aShortName;
84 unconst(mGuid) = aGuid;
85 mIfType = ifType;
86
87 /* Confirm a successful initialization */
88 autoInitSpan.setSucceeded();
89
90 return S_OK;
91}
92
93#ifdef VBOX_WITH_RESOURCE_USAGE_API
94
95void HostNetworkInterface::i_registerMetrics(PerformanceCollector *aCollector, ComPtr<IUnknown> objptr)
96{
97 LogFlowThisFunc(("mShortName={%ls}, mInterfaceName={%ls}, mGuid={%s}, mSpeedMbits=%u\n",
98 mShortName.raw(), mInterfaceName.raw(), mGuid.toString().c_str(), m.speedMbits));
99 pm::CollectorHAL *hal = aCollector->getHAL();
100 /* Create sub metrics */
101 Utf8StrFmt strName("Net/%ls", mShortName.raw());
102 pm::SubMetric *networkLoadRx = new pm::SubMetric(strName + "/Load/Rx",
103 "Percentage of network interface receive bandwidth used.");
104 pm::SubMetric *networkLoadTx = new pm::SubMetric(strName + "/Load/Tx",
105 "Percentage of network interface transmit bandwidth used.");
106 pm::SubMetric *networkLinkSpeed = new pm::SubMetric(strName + "/LinkSpeed",
107 "Physical link speed.");
108
109 /* Create and register base metrics */
110 pm::BaseMetric *networkSpeed = new pm::HostNetworkSpeed(hal, objptr, strName + "/LinkSpeed", Utf8Str(mShortName), Utf8Str(mInterfaceName), m.speedMbits, networkLinkSpeed);
111 aCollector->registerBaseMetric(networkSpeed);
112 pm::BaseMetric *networkLoad = new pm::HostNetworkLoadRaw(hal, objptr, strName + "/Load", Utf8Str(mShortName), Utf8Str(mInterfaceName), m.speedMbits, networkLoadRx, networkLoadTx);
113 aCollector->registerBaseMetric(networkLoad);
114
115 aCollector->registerMetric(new pm::Metric(networkSpeed, networkLinkSpeed, 0));
116 aCollector->registerMetric(new pm::Metric(networkSpeed, networkLinkSpeed,
117 new pm::AggregateAvg()));
118 aCollector->registerMetric(new pm::Metric(networkSpeed, networkLinkSpeed,
119 new pm::AggregateMin()));
120 aCollector->registerMetric(new pm::Metric(networkSpeed, networkLinkSpeed,
121 new pm::AggregateMax()));
122
123 aCollector->registerMetric(new pm::Metric(networkLoad, networkLoadRx, 0));
124 aCollector->registerMetric(new pm::Metric(networkLoad, networkLoadRx,
125 new pm::AggregateAvg()));
126 aCollector->registerMetric(new pm::Metric(networkLoad, networkLoadRx,
127 new pm::AggregateMin()));
128 aCollector->registerMetric(new pm::Metric(networkLoad, networkLoadRx,
129 new pm::AggregateMax()));
130
131 aCollector->registerMetric(new pm::Metric(networkLoad, networkLoadTx, 0));
132 aCollector->registerMetric(new pm::Metric(networkLoad, networkLoadTx,
133 new pm::AggregateAvg()));
134 aCollector->registerMetric(new pm::Metric(networkLoad, networkLoadTx,
135 new pm::AggregateMin()));
136 aCollector->registerMetric(new pm::Metric(networkLoad, networkLoadTx,
137 new pm::AggregateMax()));
138}
139
140void HostNetworkInterface::i_unregisterMetrics(PerformanceCollector *aCollector, ComPtr<IUnknown> objptr)
141{
142 LogFlowThisFunc(("mShortName={%ls}, mInterfaceName={%ls}, mGuid={%s}\n",
143 mShortName.raw(), mInterfaceName.raw(), mGuid.toString().c_str()));
144 Utf8StrFmt name("Net/%ls", mShortName.raw());
145 aCollector->unregisterMetricsFor(objptr, name + "/*");
146 aCollector->unregisterBaseMetricsFor(objptr, name);
147}
148
149#endif /* VBOX_WITH_RESOURCE_USAGE_API */
150
151#ifdef VBOX_WITH_HOSTNETIF_API
152
153HRESULT HostNetworkInterface::updateConfig()
154{
155 NETIFINFO info;
156 int rc = NetIfGetConfig(this, &info);
157 if (RT_SUCCESS(rc))
158 {
159 m.realIPAddress = m.IPAddress = info.IPAddress.u;
160 m.realNetworkMask = m.networkMask = info.IPNetMask.u;
161 m.dhcpEnabled = info.bDhcpEnabled;
162 m.realIPV6Address = m.IPV6Address = composeIPv6Address(&info.IPv6Address);
163 m.realIPV6PrefixLength = m.IPV6NetworkMaskPrefixLength = composeIPv6PrefixLenghFromAddress(&info.IPv6NetMask);
164 m.hardwareAddress = composeHardwareAddress(&info.MACAddress);
165#ifdef RT_OS_WINDOWS
166 m.mediumType = (HostNetworkInterfaceMediumType)info.enmMediumType;
167 m.status = (HostNetworkInterfaceStatus)info.enmStatus;
168#else /* !RT_OS_WINDOWS */
169 m.mediumType = info.enmMediumType;
170 m.status = info.enmStatus;
171#endif /* !RT_OS_WINDOWS */
172 m.speedMbits = info.uSpeedMbits;
173 return S_OK;
174 }
175 return rc == VERR_NOT_IMPLEMENTED ? E_NOTIMPL : E_FAIL;
176}
177
178Bstr HostNetworkInterface::i_composeNetworkName(const Utf8Str aShortName)
179{
180 return Utf8Str("HostInterfaceNetworking-").append(aShortName);
181}
182/**
183 * Initializes the host object.
184 *
185 * @returns COM result indicator
186 * @param aInterfaceName name of the network interface
187 * @param aGuid GUID of the host network interface
188 */
189HRESULT HostNetworkInterface::init(Bstr aInterfaceName, HostNetworkInterfaceType_T ifType, PNETIFINFO pIf)
190{
191// LogFlowThisFunc(("aInterfaceName={%ls}, aGuid={%s}\n",
192// aInterfaceName.raw(), aGuid.toString().raw()));
193
194// ComAssertRet(aInterfaceName, E_INVALIDARG);
195// ComAssertRet(aGuid.isValid(), E_INVALIDARG);
196 ComAssertRet(pIf, E_INVALIDARG);
197
198 /* Enclose the state transition NotReady->InInit->Ready */
199 AutoInitSpan autoInitSpan(this);
200 AssertReturn(autoInitSpan.isOk(), E_FAIL);
201
202 unconst(mInterfaceName) = aInterfaceName;
203 unconst(mGuid) = pIf->Uuid;
204 if (pIf->szShortName[0])
205 {
206 unconst(mNetworkName) = i_composeNetworkName(pIf->szShortName);
207 unconst(mShortName) = pIf->szShortName;
208 }
209 else
210 {
211 unconst(mNetworkName) = i_composeNetworkName(aInterfaceName);
212 unconst(mShortName) = aInterfaceName;
213 }
214 mIfType = ifType;
215
216 m.realIPAddress = m.IPAddress = pIf->IPAddress.u;
217 m.realNetworkMask = m.networkMask = pIf->IPNetMask.u;
218 m.realIPV6Address = m.IPV6Address = composeIPv6Address(&pIf->IPv6Address);
219 m.realIPV6PrefixLength = m.IPV6NetworkMaskPrefixLength = composeIPv6PrefixLenghFromAddress(&pIf->IPv6NetMask);
220 m.dhcpEnabled = pIf->bDhcpEnabled;
221 m.hardwareAddress = composeHardwareAddress(&pIf->MACAddress);
222#ifdef RT_OS_WINDOWS
223 m.mediumType = (HostNetworkInterfaceMediumType)pIf->enmMediumType;
224 m.status = (HostNetworkInterfaceStatus)pIf->enmStatus;
225#else /* !RT_OS_WINDOWS */
226 m.mediumType = pIf->enmMediumType;
227 m.status = pIf->enmStatus;
228#endif /* !RT_OS_WINDOWS */
229 m.speedMbits = pIf->uSpeedMbits;
230
231 /* Confirm a successful initialization */
232 autoInitSpan.setSucceeded();
233
234 return S_OK;
235}
236#endif
237
238// wrapped IHostNetworkInterface properties
239/////////////////////////////////////////////////////////////////////////////
240/**
241 * Returns the name of the host network interface.
242 *
243 * @returns COM status code
244 * @param aInterfaceName - Interface Name
245 */
246
247HRESULT HostNetworkInterface::getName(com::Utf8Str &aInterfaceName)
248{
249 aInterfaceName = mInterfaceName;
250 return S_OK;
251}
252
253/**
254 * Returns the short name of the host network interface.
255 *
256 * @returns COM status code
257 * @param aShortName Short Name
258 */
259
260HRESULT HostNetworkInterface::getShortName(com::Utf8Str &aShortName)
261{
262 aShortName = mShortName;
263
264 return S_OK;
265}
266
267/**
268 * Returns the GUID of the host network interface.
269 *
270 * @returns COM status code
271 * @param aGuid GUI Id
272 */
273HRESULT HostNetworkInterface::getId(com::Guid &aGuiId)
274{
275 aGuiId = mGuid;
276
277 return S_OK;
278}
279
280HRESULT HostNetworkInterface::getDHCPEnabled(BOOL *aDHCPEnabled)
281{
282 *aDHCPEnabled = m.dhcpEnabled;
283
284 return S_OK;
285}
286
287
288/**
289 * Returns the IP address of the host network interface.
290 *
291 * @returns COM status code
292 * @param aIPAddress Address name
293 */
294HRESULT HostNetworkInterface::getIPAddress(com::Utf8Str &aIPAddress)
295{
296 in_addr tmp;
297#if defined(RT_OS_WINDOWS)
298 tmp.S_un.S_addr = m.IPAddress;
299#else
300 tmp.s_addr = m.IPAddress;
301#endif
302 char *addr = inet_ntoa(tmp);
303 if (addr)
304 {
305 aIPAddress = addr;
306 return S_OK;
307 }
308
309 return E_FAIL;
310}
311
312/**
313 * Returns the netwok mask of the host network interface.
314 *
315 * @returns COM status code
316 * @param aNetworkMask name.
317 */
318HRESULT HostNetworkInterface::getNetworkMask(com::Utf8Str &aNetworkMask)
319{
320
321 in_addr tmp;
322#if defined(RT_OS_WINDOWS)
323 tmp.S_un.S_addr = m.networkMask;
324#else
325 tmp.s_addr = m.networkMask;
326#endif
327 char *addr = inet_ntoa(tmp);
328 if (addr)
329 {
330 aNetworkMask = Utf8Str(addr);
331 return S_OK;
332 }
333
334 return E_FAIL;
335}
336
337HRESULT HostNetworkInterface::getIPV6Supported(BOOL *aIPV6Supported)
338{
339#if defined(RT_OS_WINDOWS)
340 *aIPV6Supported = FALSE;
341#else
342 *aIPV6Supported = TRUE;
343#endif
344
345 return S_OK;
346}
347
348/**
349 * Returns the IP V6 address of the host network interface.
350 *
351 * @returns COM status code
352 * @param aIPV6Address
353 */
354HRESULT HostNetworkInterface::getIPV6Address(com::Utf8Str &aIPV6Address)
355{
356 aIPV6Address = m.IPV6Address;
357 return S_OK;
358}
359
360/**
361 * Returns the IP V6 network mask of the host network interface.
362 *
363 * @returns COM status code
364 * @param aIPV6Mask address of result pointer
365 */
366HRESULT HostNetworkInterface::getIPV6NetworkMaskPrefixLength(ULONG *aIPV6NetworkMaskPrefixLength)
367{
368 *aIPV6NetworkMaskPrefixLength = m.IPV6NetworkMaskPrefixLength;
369
370 return S_OK;
371}
372
373/**
374 * Returns the hardware address of the host network interface.
375 *
376 * @returns COM status code
377 * @param aHardwareAddress hardware address
378 */
379HRESULT HostNetworkInterface::getHardwareAddress(com::Utf8Str &aHardwareAddress)
380{
381 aHardwareAddress = m.hardwareAddress;
382 return S_OK;
383}
384
385/**
386 * Returns the encapsulation protocol type of the host network interface.
387 *
388 * @returns COM status code
389 * @param aType address of result pointer
390 */
391HRESULT HostNetworkInterface::getMediumType(HostNetworkInterfaceMediumType_T *aType)
392{
393 *aType = m.mediumType;
394
395 return S_OK;
396}
397
398/**
399 * Returns the current state of the host network interface.
400 *
401 * @returns COM status code
402 * @param aStatus address of result pointer
403 */
404HRESULT HostNetworkInterface::getStatus(HostNetworkInterfaceStatus_T *aStatus)
405{
406 *aStatus = m.status;
407
408 return S_OK;
409}
410
411/**
412 * Returns network interface type
413 *
414 * @returns COM status code
415 * @param aType address of result pointer
416 */
417HRESULT HostNetworkInterface::getInterfaceType(HostNetworkInterfaceType_T *aType)
418{
419 *aType = mIfType;
420
421 return S_OK;
422
423}
424
425HRESULT HostNetworkInterface::getNetworkName(com::Utf8Str &aNetworkName)
426{
427 aNetworkName = mNetworkName;
428
429 return S_OK;
430}
431
432HRESULT HostNetworkInterface::enableStaticIPConfig(const com::Utf8Str &aIPAddress,
433 const com::Utf8Str &aNetworkMask)
434{
435#ifndef VBOX_WITH_HOSTNETIF_API
436 return E_NOTIMPL;
437#else
438 if (aIPAddress.isEmpty())
439 {
440 if (m.IPAddress)
441 {
442 int rc = NetIfEnableStaticIpConfig(mVBox, this, m.IPAddress, 0, 0);
443 if (RT_SUCCESS(rc))
444 {
445 m.realIPAddress = 0;
446 if (FAILED(mVBox->SetExtraData(BstrFmt("HostOnly/%ls/IPAddress", mInterfaceName.raw()).raw(), NULL)))
447 return E_FAIL;
448 if (FAILED(mVBox->SetExtraData(BstrFmt("HostOnly/%ls/IPNetMask", mInterfaceName.raw()).raw(), NULL)))
449 return E_FAIL;
450 return S_OK;
451 }
452 }
453 else
454 return S_OK;
455 }
456
457 ULONG ip, mask;
458 ip = inet_addr(aIPAddress.c_str());
459 if (ip != INADDR_NONE)
460 {
461 if (aNetworkMask.isEmpty())
462 mask = 0xFFFFFF;
463 else
464 mask = inet_addr(aNetworkMask.c_str());
465 if (mask != INADDR_NONE)
466 {
467 if (m.realIPAddress == ip && m.realNetworkMask == mask)
468 return S_OK;
469 int rc = NetIfEnableStaticIpConfig(mVBox, this, m.IPAddress, ip, mask);
470 if (RT_SUCCESS(rc))
471 {
472 m.realIPAddress = ip;
473 m.realNetworkMask = mask;
474 if (FAILED(mVBox->SetExtraData(BstrFmt("HostOnly/%ls/IPAddress", mInterfaceName.raw()).raw(),
475 Bstr(aIPAddress).raw())))
476 return E_FAIL;
477 if (FAILED(mVBox->SetExtraData(BstrFmt("HostOnly/%ls/IPNetMask", mInterfaceName.raw()).raw(),
478 Bstr(aNetworkMask).raw())))
479 return E_FAIL;
480 return S_OK;
481 }
482 else
483 {
484 LogRel(("Failed to EnableStaticIpConfig with rc=%Rrc\n", rc));
485 return rc == VERR_NOT_IMPLEMENTED ? E_NOTIMPL : E_FAIL;
486 }
487
488 }
489 }
490 return E_FAIL;
491#endif
492}
493
494HRESULT HostNetworkInterface::enableStaticIPConfigV6(const com::Utf8Str &aIPV6Address,
495 ULONG aIPV6NetworkMaskPrefixLength)
496{
497#ifndef VBOX_WITH_HOSTNETIF_API
498 return E_NOTIMPL;
499#else
500 if (aIPV6NetworkMaskPrefixLength > 128)
501 return E_INVALIDARG;
502
503 int rc = S_OK;
504 if (m.realIPV6Address != aIPV6Address || m.realIPV6PrefixLength != aIPV6NetworkMaskPrefixLength)
505 {
506 BSTR bstr;
507 aIPV6Address.cloneTo(&bstr);
508 if (aIPV6NetworkMaskPrefixLength == 0)
509 aIPV6NetworkMaskPrefixLength = 64;
510 rc = NetIfEnableStaticIpConfigV6(mVBox, this, m.IPV6Address.raw(), bstr, aIPV6NetworkMaskPrefixLength);
511 if (RT_FAILURE(rc))
512 {
513 LogRel(("Failed to EnableStaticIpConfigV6 with rc=%Rrc\n", rc));
514 return rc == VERR_NOT_IMPLEMENTED ? E_NOTIMPL : E_FAIL;
515 }
516 else
517 {
518 m.realIPV6Address = aIPV6Address;
519 m.realIPV6PrefixLength = aIPV6NetworkMaskPrefixLength;
520 if (FAILED(mVBox->SetExtraData(BstrFmt("HostOnly/%ls/IPV6Address", mInterfaceName.raw()).raw(),
521 Bstr(aIPV6Address).raw())))
522 return E_FAIL;
523 if (FAILED(mVBox->SetExtraData(BstrFmt("HostOnly/%ls/IPV6NetMask", mInterfaceName.raw()).raw(),
524 BstrFmt("%u", aIPV6NetworkMaskPrefixLength).raw())))
525 return E_FAIL;
526 }
527
528 }
529 return S_OK;
530#endif
531}
532
533HRESULT HostNetworkInterface::enableDynamicIPConfig()
534{
535#ifndef VBOX_WITH_HOSTNETIF_API
536 return E_NOTIMPL;
537#else
538 int rc = NetIfEnableDynamicIpConfig(mVBox, this);
539 if (RT_FAILURE(rc))
540 {
541 LogRel(("Failed to EnableDynamicIpConfig with rc=%Rrc\n", rc));
542 return rc == VERR_NOT_IMPLEMENTED ? E_NOTIMPL : E_FAIL;
543 }
544 return S_OK;
545#endif
546}
547
548HRESULT HostNetworkInterface::dHCPRediscover()
549{
550#ifndef VBOX_WITH_HOSTNETIF_API
551 return E_NOTIMPL;
552#else
553 int rc = NetIfDhcpRediscover(mVBox, this);
554 if (RT_FAILURE(rc))
555 {
556 LogRel(("Failed to DhcpRediscover with rc=%Rrc\n", rc));
557 return rc == VERR_NOT_IMPLEMENTED ? E_NOTIMPL : E_FAIL;
558 }
559 return S_OK;
560#endif
561}
562
563HRESULT HostNetworkInterface::i_setVirtualBox(VirtualBox *pVBox)
564{
565 AutoCaller autoCaller(this);
566 if (FAILED(autoCaller.rc())) return autoCaller.rc();
567
568 AssertReturn(mVBox != pVBox, S_OK);
569
570 unconst(mVBox) = pVBox;
571
572#if !defined(RT_OS_WINDOWS)
573 /* If IPv4 address hasn't been initialized */
574 if (m.IPAddress == 0 && mIfType == HostNetworkInterfaceType_HostOnly)
575 {
576 Bstr tmpAddr, tmpMask;
577 HRESULT hrc = mVBox->GetExtraData(BstrFmt("HostOnly/%ls/IPAddress", mInterfaceName.raw()).raw(),
578 tmpAddr.asOutParam());
579 if (FAILED(hrc) || tmpAddr.isEmpty())
580 tmpAddr = getDefaultIPv4Address(mInterfaceName);
581
582 hrc = mVBox->GetExtraData(BstrFmt("HostOnly/%ls/IPNetMask", mInterfaceName.raw()).raw(),
583 tmpMask.asOutParam());
584 if (FAILED(hrc) || tmpMask.isEmpty())
585 tmpMask = Bstr(VBOXNET_IPV4MASK_DEFAULT);
586
587 m.IPAddress = inet_addr(Utf8Str(tmpAddr).c_str());
588 m.networkMask = inet_addr(Utf8Str(tmpMask).c_str());
589 }
590
591 if (m.IPV6Address.isEmpty())
592 {
593 Bstr tmpPrefixLen;
594 HRESULT hrc = mVBox->GetExtraData(BstrFmt("HostOnly/%ls/IPV6Address", mInterfaceName.raw()).raw(),
595 m.IPV6Address.asOutParam());
596 if (SUCCEEDED(hrc) && !m.IPV6Address.isEmpty())
597 {
598 hrc = mVBox->GetExtraData(BstrFmt("HostOnly/%ls/IPV6PrefixLen", mInterfaceName.raw()).raw(),
599 tmpPrefixLen.asOutParam());
600 if (SUCCEEDED(hrc) && !tmpPrefixLen.isEmpty())
601 m.IPV6NetworkMaskPrefixLength = Utf8Str(tmpPrefixLen).toUInt32();
602 else
603 m.IPV6NetworkMaskPrefixLength = 64;
604 }
605 }
606#endif
607
608 return S_OK;
609}
610
611/* vi: set tabstop=4 shiftwidth=4 expandtab: */
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