1
/* $Id: HostNetworkInterfaceImpl.cpp 35368 2010-12-30 13:38:23Z vboxsync $ */
5
* VirtualBox COM class implementation
9
* Copyright (C) 2006-2008 Oracle Corporation
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.
20
#include "HostNetworkInterfaceImpl.h"
21
#include "AutoCaller.h"
25
#include <iprt/cpp/utils.h>
28
# include <netinet/in.h> /* INADDR_NONE */
29
#endif /* RT_OS_FREEBSD */
31
// constructor / destructor
32
/////////////////////////////////////////////////////////////////////////////
34
HostNetworkInterface::HostNetworkInterface()
39
HostNetworkInterface::~HostNetworkInterface()
43
HRESULT HostNetworkInterface::FinalConstruct()
48
void HostNetworkInterface::FinalRelease()
53
// public initializer/uninitializer for internal purposes only
54
/////////////////////////////////////////////////////////////////////////////
57
* Initializes the host object.
59
* @returns COM result indicator
60
* @param aInterfaceName name of the network interface
61
* @param aGuid GUID of the host network interface
63
HRESULT HostNetworkInterface::init(Bstr aInterfaceName, Guid aGuid, HostNetworkInterfaceType_T ifType)
65
LogFlowThisFunc(("aInterfaceName={%ls}, aGuid={%s}\n",
66
aInterfaceName.raw(), aGuid.toString().c_str()));
68
ComAssertRet(!aInterfaceName.isEmpty(), E_INVALIDARG);
69
ComAssertRet(!aGuid.isEmpty(), E_INVALIDARG);
71
/* Enclose the state transition NotReady->InInit->Ready */
72
AutoInitSpan autoInitSpan(this);
73
AssertReturn(autoInitSpan.isOk(), E_FAIL);
75
unconst(mInterfaceName) = aInterfaceName;
76
unconst(mGuid) = aGuid;
79
/* Confirm a successful initialization */
80
autoInitSpan.setSucceeded();
85
#ifdef VBOX_WITH_HOSTNETIF_API
87
HRESULT HostNetworkInterface::updateConfig ()
90
int rc = NetIfGetConfig(this, &info);
93
m.realIPAddress = m.IPAddress = info.IPAddress.u;
94
m.realNetworkMask = m.networkMask = info.IPNetMask.u;
95
m.dhcpEnabled = info.bDhcpEnabled;
96
m.realIPV6Address = m.IPV6Address = composeIPv6Address(&info.IPv6Address);
97
m.realIPV6PrefixLength = m.IPV6NetworkMaskPrefixLength = composeIPv6PrefixLenghFromAddress(&info.IPv6NetMask);
98
m.hardwareAddress = composeHardwareAddress(&info.MACAddress);
100
m.mediumType = (HostNetworkInterfaceMediumType)info.enmMediumType;
101
m.status = (HostNetworkInterfaceStatus)info.enmStatus;
102
#else /* !RT_OS_WINDOWS */
103
m.mediumType = info.enmMediumType;
104
m.status = info.enmStatus;
106
#endif /* !RT_OS_WINDOWS */
109
return rc == VERR_NOT_IMPLEMENTED ? E_NOTIMPL : E_FAIL;
113
* Initializes the host object.
115
* @returns COM result indicator
116
* @param aInterfaceName name of the network interface
117
* @param aGuid GUID of the host network interface
119
HRESULT HostNetworkInterface::init (Bstr aInterfaceName, HostNetworkInterfaceType_T ifType, PNETIFINFO pIf)
121
// LogFlowThisFunc(("aInterfaceName={%ls}, aGuid={%s}\n",
122
// aInterfaceName.raw(), aGuid.toString().raw()));
124
// ComAssertRet(aInterfaceName, E_INVALIDARG);
125
// ComAssertRet(!aGuid.isEmpty(), E_INVALIDARG);
126
ComAssertRet(pIf, E_INVALIDARG);
128
/* Enclose the state transition NotReady->InInit->Ready */
129
AutoInitSpan autoInitSpan(this);
130
AssertReturn(autoInitSpan.isOk(), E_FAIL);
132
unconst(mInterfaceName) = aInterfaceName;
133
unconst(mGuid) = pIf->Uuid;
136
m.realIPAddress = m.IPAddress = pIf->IPAddress.u;
137
m.realNetworkMask = m.networkMask = pIf->IPNetMask.u;
138
m.realIPV6Address = m.IPV6Address = composeIPv6Address(&pIf->IPv6Address);
139
m.realIPV6PrefixLength = m.IPV6NetworkMaskPrefixLength = composeIPv6PrefixLenghFromAddress(&pIf->IPv6NetMask);
140
m.dhcpEnabled = pIf->bDhcpEnabled;
141
m.hardwareAddress = composeHardwareAddress(&pIf->MACAddress);
143
m.mediumType = (HostNetworkInterfaceMediumType)pIf->enmMediumType;
144
m.status = (HostNetworkInterfaceStatus)pIf->enmStatus;
145
#else /* !RT_OS_WINDOWS */
146
m.mediumType = pIf->enmMediumType;
147
m.status = pIf->enmStatus;
148
#endif /* !RT_OS_WINDOWS */
150
/* Confirm a successful initialization */
151
autoInitSpan.setSucceeded();
157
// IHostNetworkInterface properties
158
/////////////////////////////////////////////////////////////////////////////
161
* Returns the name of the host network interface.
163
* @returns COM status code
164
* @param aInterfaceName address of result pointer
166
STDMETHODIMP HostNetworkInterface::COMGETTER(Name) (BSTR *aInterfaceName)
168
CheckComArgOutPointerValid(aInterfaceName);
170
AutoCaller autoCaller(this);
171
if (FAILED(autoCaller.rc())) return autoCaller.rc();
173
mInterfaceName.cloneTo(aInterfaceName);
179
* Returns the GUID of the host network interface.
181
* @returns COM status code
182
* @param aGuid address of result pointer
184
STDMETHODIMP HostNetworkInterface::COMGETTER(Id) (BSTR *aGuid)
186
CheckComArgOutPointerValid(aGuid);
188
AutoCaller autoCaller(this);
189
if (FAILED(autoCaller.rc())) return autoCaller.rc();
191
mGuid.toUtf16().cloneTo(aGuid);
196
STDMETHODIMP HostNetworkInterface::COMGETTER(DhcpEnabled) (BOOL *aDhcpEnabled)
198
CheckComArgOutPointerValid(aDhcpEnabled);
200
AutoCaller autoCaller(this);
201
if (FAILED(autoCaller.rc())) return autoCaller.rc();
203
*aDhcpEnabled = m.dhcpEnabled;
210
* Returns the IP address of the host network interface.
212
* @returns COM status code
213
* @param aIPAddress address of result pointer
215
STDMETHODIMP HostNetworkInterface::COMGETTER(IPAddress) (BSTR *aIPAddress)
217
CheckComArgOutPointerValid(aIPAddress);
219
AutoCaller autoCaller(this);
220
if (FAILED(autoCaller.rc())) return autoCaller.rc();
222
if (m.IPAddress == 0)
224
getDefaultIPv4Address(mInterfaceName).detachTo(aIPAddress);
229
#if defined(RT_OS_WINDOWS)
230
tmp.S_un.S_addr = m.IPAddress;
232
tmp.s_addr = m.IPAddress;
234
char *addr = inet_ntoa(tmp);
237
Bstr(addr).detachTo(aIPAddress);
245
* Returns the netwok mask of the host network interface.
247
* @returns COM status code
248
* @param aNetworkMask address of result pointer
250
STDMETHODIMP HostNetworkInterface::COMGETTER(NetworkMask) (BSTR *aNetworkMask)
252
CheckComArgOutPointerValid(aNetworkMask);
254
AutoCaller autoCaller(this);
255
if (FAILED(autoCaller.rc())) return autoCaller.rc();
257
if (m.networkMask == 0)
259
Bstr(VBOXNET_IPV4MASK_DEFAULT).detachTo(aNetworkMask);
264
#if defined(RT_OS_WINDOWS)
265
tmp.S_un.S_addr = m.networkMask;
267
tmp.s_addr = m.networkMask;
269
char *addr = inet_ntoa(tmp);
272
Bstr(addr).detachTo(aNetworkMask);
279
STDMETHODIMP HostNetworkInterface::COMGETTER(IPV6Supported) (BOOL *aIPV6Supported)
281
CheckComArgOutPointerValid(aIPV6Supported);
282
#if defined(RT_OS_WINDOWS)
283
*aIPV6Supported = FALSE;
285
*aIPV6Supported = TRUE;
292
* Returns the IP V6 address of the host network interface.
294
* @returns COM status code
295
* @param aIPV6Address address of result pointer
297
STDMETHODIMP HostNetworkInterface::COMGETTER(IPV6Address) (BSTR *aIPV6Address)
299
CheckComArgOutPointerValid(aIPV6Address);
301
AutoCaller autoCaller(this);
302
if (FAILED(autoCaller.rc())) return autoCaller.rc();
304
m.IPV6Address.cloneTo(aIPV6Address);
310
* Returns the IP V6 network mask of the host network interface.
312
* @returns COM status code
313
* @param aIPV6Mask address of result pointer
315
STDMETHODIMP HostNetworkInterface::COMGETTER(IPV6NetworkMaskPrefixLength) (ULONG *aIPV6NetworkMaskPrefixLength)
317
CheckComArgOutPointerValid(aIPV6NetworkMaskPrefixLength);
319
AutoCaller autoCaller(this);
320
if (FAILED(autoCaller.rc())) return autoCaller.rc();
322
*aIPV6NetworkMaskPrefixLength = m.IPV6NetworkMaskPrefixLength;
328
* Returns the hardware address of the host network interface.
330
* @returns COM status code
331
* @param aHardwareAddress address of result pointer
333
STDMETHODIMP HostNetworkInterface::COMGETTER(HardwareAddress) (BSTR *aHardwareAddress)
335
CheckComArgOutPointerValid(aHardwareAddress);
337
AutoCaller autoCaller(this);
338
if (FAILED(autoCaller.rc())) return autoCaller.rc();
340
m.hardwareAddress.cloneTo(aHardwareAddress);
346
* Returns the encapsulation protocol type of the host network interface.
348
* @returns COM status code
349
* @param aType address of result pointer
351
STDMETHODIMP HostNetworkInterface::COMGETTER(MediumType) (HostNetworkInterfaceMediumType_T *aType)
353
CheckComArgOutPointerValid(aType);
355
AutoCaller autoCaller(this);
356
if (FAILED(autoCaller.rc())) return autoCaller.rc();
358
*aType = m.mediumType;
364
* Returns the current state of the host network interface.
366
* @returns COM status code
367
* @param aStatus address of result pointer
369
STDMETHODIMP HostNetworkInterface::COMGETTER(Status) (HostNetworkInterfaceStatus_T *aStatus)
371
CheckComArgOutPointerValid(aStatus);
373
AutoCaller autoCaller(this);
374
if (FAILED(autoCaller.rc())) return autoCaller.rc();
382
* Returns network interface type
384
* @returns COM status code
385
* @param aType address of result pointer
387
STDMETHODIMP HostNetworkInterface::COMGETTER(InterfaceType) (HostNetworkInterfaceType_T *aType)
389
CheckComArgOutPointerValid(aType);
391
AutoCaller autoCaller(this);
392
if (FAILED(autoCaller.rc())) return autoCaller.rc();
400
STDMETHODIMP HostNetworkInterface::COMGETTER(NetworkName) (BSTR *aNetworkName)
402
AutoCaller autoCaller(this);
403
if (FAILED(autoCaller.rc())) return autoCaller.rc();
405
Utf8Str utf8Name("HostInterfaceNetworking-");
406
utf8Name.append(Utf8Str(mInterfaceName)) ;
407
Bstr netName(utf8Name);
408
netName.detachTo(aNetworkName);
413
STDMETHODIMP HostNetworkInterface::EnableStaticIpConfig (IN_BSTR aIPAddress, IN_BSTR aNetMask)
415
#ifndef VBOX_WITH_HOSTNETIF_API
418
AutoCaller autoCaller(this);
419
if (FAILED(autoCaller.rc())) return autoCaller.rc();
421
if (Bstr(aIPAddress).isEmpty())
425
int rc = NetIfEnableStaticIpConfig(mVBox, this, m.IPAddress, 0, 0);
429
if (FAILED(mVBox->SetExtraData(BstrFmt("HostOnly/%ls/IPAddress", mInterfaceName.raw()).raw(), NULL)))
431
if (FAILED(mVBox->SetExtraData(BstrFmt("HostOnly/%ls/IPNetMask", mInterfaceName.raw()).raw(), NULL)))
441
ip = inet_addr(Utf8Str(aIPAddress).c_str());
442
if (ip != INADDR_NONE)
444
if (Bstr(aNetMask).isEmpty())
447
mask = inet_addr(Utf8Str(aNetMask).c_str());
448
if (mask != INADDR_NONE)
450
if (m.realIPAddress == ip && m.realNetworkMask == mask)
452
int rc = NetIfEnableStaticIpConfig(mVBox, this, m.IPAddress, ip, mask);
455
m.realIPAddress = ip;
456
m.realNetworkMask = mask;
457
if (FAILED(mVBox->SetExtraData(BstrFmt("HostOnly/%ls/IPAddress", mInterfaceName.raw()).raw(), Bstr(aIPAddress).raw())))
459
if (FAILED(mVBox->SetExtraData(BstrFmt("HostOnly/%ls/IPNetMask", mInterfaceName.raw()).raw(), Bstr(aNetMask).raw())))
465
LogRel(("Failed to EnableStaticIpConfig with rc=%Rrc\n", rc));
466
return rc == VERR_NOT_IMPLEMENTED ? E_NOTIMPL : E_FAIL;
475
STDMETHODIMP HostNetworkInterface::EnableStaticIpConfigV6 (IN_BSTR aIPV6Address, ULONG aIPV6MaskPrefixLength)
477
#ifndef VBOX_WITH_HOSTNETIF_API
482
if (aIPV6MaskPrefixLength > 128)
485
AutoCaller autoCaller(this);
486
if (FAILED(autoCaller.rc())) return autoCaller.rc();
489
if (m.realIPV6Address != aIPV6Address || m.realIPV6PrefixLength != aIPV6MaskPrefixLength)
491
if (aIPV6MaskPrefixLength == 0)
492
aIPV6MaskPrefixLength = 64;
493
rc = NetIfEnableStaticIpConfigV6(mVBox, this, m.IPV6Address.raw(), aIPV6Address, aIPV6MaskPrefixLength);
496
LogRel(("Failed to EnableStaticIpConfigV6 with rc=%Rrc\n", rc));
497
return rc == VERR_NOT_IMPLEMENTED ? E_NOTIMPL : E_FAIL;
501
m.realIPV6Address = aIPV6Address;
502
m.realIPV6PrefixLength = aIPV6MaskPrefixLength;
503
if (FAILED(mVBox->SetExtraData(BstrFmt("HostOnly/%ls/IPV6Address", mInterfaceName.raw()).raw(), Bstr(aIPV6Address).raw())))
505
if (FAILED(mVBox->SetExtraData(BstrFmt("HostOnly/%ls/IPV6NetMask", mInterfaceName.raw()).raw(),
506
BstrFmt("%u", aIPV6MaskPrefixLength).raw())))
515
STDMETHODIMP HostNetworkInterface::EnableDynamicIpConfig ()
517
#ifndef VBOX_WITH_HOSTNETIF_API
520
AutoCaller autoCaller(this);
521
if (FAILED(autoCaller.rc())) return autoCaller.rc();
523
int rc = NetIfEnableDynamicIpConfig(mVBox, this);
526
LogRel(("Failed to EnableDynamicIpConfig with rc=%Rrc\n", rc));
527
return rc == VERR_NOT_IMPLEMENTED ? E_NOTIMPL : E_FAIL;
533
STDMETHODIMP HostNetworkInterface::DhcpRediscover ()
535
#ifndef VBOX_WITH_HOSTNETIF_API
538
AutoCaller autoCaller(this);
539
if (FAILED(autoCaller.rc())) return autoCaller.rc();
541
int rc = NetIfDhcpRediscover(mVBox, this);
544
LogRel(("Failed to DhcpRediscover with rc=%Rrc\n", rc));
545
return rc == VERR_NOT_IMPLEMENTED ? E_NOTIMPL : E_FAIL;
551
HRESULT HostNetworkInterface::setVirtualBox(VirtualBox *pVBox)
554
AutoCaller autoCaller(this);
555
if (FAILED(autoCaller.rc())) return autoCaller.rc();
556
unconst(mVBox) = pVBox;
558
/* If IPv4 address hasn't been initialized */
559
if (m.IPAddress == 0)
561
Bstr tmpAddr, tmpMask;
562
hrc = mVBox->GetExtraData(BstrFmt("HostOnly/%ls/IPAddress", mInterfaceName.raw()).raw(), tmpAddr.asOutParam());
563
hrc = mVBox->GetExtraData(BstrFmt("HostOnly/%ls/IPNetMask", mInterfaceName.raw()).raw(), tmpMask.asOutParam());
564
if (tmpAddr.isEmpty())
565
tmpAddr = getDefaultIPv4Address(mInterfaceName);
566
if (tmpMask.isEmpty())
567
tmpMask = Bstr(VBOXNET_IPV4MASK_DEFAULT);
568
m.IPAddress = inet_addr(Utf8Str(tmpAddr).c_str());
569
m.networkMask = inet_addr(Utf8Str(tmpMask).c_str());
572
if (m.IPV6Address.isEmpty())
575
hrc = mVBox->GetExtraData(BstrFmt("HostOnly/%ls/IPV6Address", mInterfaceName.raw()).raw(), m.IPV6Address.asOutParam());
576
if (!m.IPV6Address.isEmpty())
578
hrc = mVBox->GetExtraData(BstrFmt("HostOnly/%ls/IPV6PrefixLen", mInterfaceName.raw()).raw(), tmpPrefixLen.asOutParam());
579
if (SUCCEEDED(hrc) && !tmpPrefixLen.isEmpty())
580
m.IPV6NetworkMaskPrefixLength = Utf8Str(tmpPrefixLen).toUInt32();
582
m.IPV6NetworkMaskPrefixLength = 64;
589
/* vi: set tabstop=4 shiftwidth=4 expandtab: */