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