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