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