VirtualBox

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

Last change on this file since 49047 was 48101, checked in by vboxsync, 11 years ago

BUGZ#6780: API change: introduced GetShortName() method for HostNetworkInterface class, updated host interfaces checker on GUI start in order to take into account interface short names.

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