1 | /* $Id: HostNetworkInterfaceImpl.cpp 43507 2012-10-02 13:22:31Z 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.215389.xyz. 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), m.speedMbytes, 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 | #endif /* !RT_OS_WINDOWS */
|
---|
152 | m.speedMbytes = info.uSpeedMbytes;
|
---|
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 | m.speedMbytes = pIf->uSpeedMbytes;
|
---|
204 |
|
---|
205 | /* Confirm a successful initialization */
|
---|
206 | autoInitSpan.setSucceeded();
|
---|
207 |
|
---|
208 | return S_OK;
|
---|
209 | }
|
---|
210 | #endif
|
---|
211 |
|
---|
212 | // IHostNetworkInterface properties
|
---|
213 | /////////////////////////////////////////////////////////////////////////////
|
---|
214 |
|
---|
215 | /**
|
---|
216 | * Returns the name of the host network interface.
|
---|
217 | *
|
---|
218 | * @returns COM status code
|
---|
219 | * @param aInterfaceName address of result pointer
|
---|
220 | */
|
---|
221 | STDMETHODIMP HostNetworkInterface::COMGETTER(Name)(BSTR *aInterfaceName)
|
---|
222 | {
|
---|
223 | CheckComArgOutPointerValid(aInterfaceName);
|
---|
224 |
|
---|
225 | AutoCaller autoCaller(this);
|
---|
226 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
227 |
|
---|
228 | mInterfaceName.cloneTo(aInterfaceName);
|
---|
229 |
|
---|
230 | return S_OK;
|
---|
231 | }
|
---|
232 |
|
---|
233 | /**
|
---|
234 | * Returns the GUID of the host network interface.
|
---|
235 | *
|
---|
236 | * @returns COM status code
|
---|
237 | * @param aGuid address of result pointer
|
---|
238 | */
|
---|
239 | STDMETHODIMP HostNetworkInterface::COMGETTER(Id)(BSTR *aGuid)
|
---|
240 | {
|
---|
241 | CheckComArgOutPointerValid(aGuid);
|
---|
242 |
|
---|
243 | AutoCaller autoCaller(this);
|
---|
244 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
245 |
|
---|
246 | mGuid.toUtf16().cloneTo(aGuid);
|
---|
247 |
|
---|
248 | return S_OK;
|
---|
249 | }
|
---|
250 |
|
---|
251 | STDMETHODIMP HostNetworkInterface::COMGETTER(DHCPEnabled)(BOOL *aDHCPEnabled)
|
---|
252 | {
|
---|
253 | CheckComArgOutPointerValid(aDHCPEnabled);
|
---|
254 |
|
---|
255 | AutoCaller autoCaller(this);
|
---|
256 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
257 |
|
---|
258 | *aDHCPEnabled = m.dhcpEnabled;
|
---|
259 |
|
---|
260 | return S_OK;
|
---|
261 | }
|
---|
262 |
|
---|
263 |
|
---|
264 | /**
|
---|
265 | * Returns the IP address of the host network interface.
|
---|
266 | *
|
---|
267 | * @returns COM status code
|
---|
268 | * @param aIPAddress address of result pointer
|
---|
269 | */
|
---|
270 | STDMETHODIMP HostNetworkInterface::COMGETTER(IPAddress)(BSTR *aIPAddress)
|
---|
271 | {
|
---|
272 | CheckComArgOutPointerValid(aIPAddress);
|
---|
273 |
|
---|
274 | AutoCaller autoCaller(this);
|
---|
275 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
276 |
|
---|
277 | in_addr tmp;
|
---|
278 | #if defined(RT_OS_WINDOWS)
|
---|
279 | tmp.S_un.S_addr = m.IPAddress;
|
---|
280 | #else
|
---|
281 | tmp.s_addr = m.IPAddress;
|
---|
282 | #endif
|
---|
283 | char *addr = inet_ntoa(tmp);
|
---|
284 | if (addr)
|
---|
285 | {
|
---|
286 | Bstr(addr).detachTo(aIPAddress);
|
---|
287 | return S_OK;
|
---|
288 | }
|
---|
289 |
|
---|
290 | return E_FAIL;
|
---|
291 | }
|
---|
292 |
|
---|
293 | /**
|
---|
294 | * Returns the netwok mask of the host network interface.
|
---|
295 | *
|
---|
296 | * @returns COM status code
|
---|
297 | * @param aNetworkMask address of result pointer
|
---|
298 | */
|
---|
299 | STDMETHODIMP HostNetworkInterface::COMGETTER(NetworkMask)(BSTR *aNetworkMask)
|
---|
300 | {
|
---|
301 | CheckComArgOutPointerValid(aNetworkMask);
|
---|
302 |
|
---|
303 | AutoCaller autoCaller(this);
|
---|
304 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
305 |
|
---|
306 | in_addr tmp;
|
---|
307 | #if defined(RT_OS_WINDOWS)
|
---|
308 | tmp.S_un.S_addr = m.networkMask;
|
---|
309 | #else
|
---|
310 | tmp.s_addr = m.networkMask;
|
---|
311 | #endif
|
---|
312 | char *addr = inet_ntoa(tmp);
|
---|
313 | if (addr)
|
---|
314 | {
|
---|
315 | Bstr(addr).detachTo(aNetworkMask);
|
---|
316 | return S_OK;
|
---|
317 | }
|
---|
318 |
|
---|
319 | return E_FAIL;
|
---|
320 | }
|
---|
321 |
|
---|
322 | STDMETHODIMP HostNetworkInterface::COMGETTER(IPV6Supported)(BOOL *aIPV6Supported)
|
---|
323 | {
|
---|
324 | CheckComArgOutPointerValid(aIPV6Supported);
|
---|
325 | #if defined(RT_OS_WINDOWS)
|
---|
326 | *aIPV6Supported = FALSE;
|
---|
327 | #else
|
---|
328 | *aIPV6Supported = TRUE;
|
---|
329 | #endif
|
---|
330 |
|
---|
331 | return S_OK;
|
---|
332 | }
|
---|
333 |
|
---|
334 | /**
|
---|
335 | * Returns the IP V6 address of the host network interface.
|
---|
336 | *
|
---|
337 | * @returns COM status code
|
---|
338 | * @param aIPV6Address address of result pointer
|
---|
339 | */
|
---|
340 | STDMETHODIMP HostNetworkInterface::COMGETTER(IPV6Address)(BSTR *aIPV6Address)
|
---|
341 | {
|
---|
342 | CheckComArgOutPointerValid(aIPV6Address);
|
---|
343 |
|
---|
344 | AutoCaller autoCaller(this);
|
---|
345 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
346 |
|
---|
347 | m.IPV6Address.cloneTo(aIPV6Address);
|
---|
348 |
|
---|
349 | return S_OK;
|
---|
350 | }
|
---|
351 |
|
---|
352 | /**
|
---|
353 | * Returns the IP V6 network mask of the host network interface.
|
---|
354 | *
|
---|
355 | * @returns COM status code
|
---|
356 | * @param aIPV6Mask address of result pointer
|
---|
357 | */
|
---|
358 | STDMETHODIMP HostNetworkInterface::COMGETTER(IPV6NetworkMaskPrefixLength)(ULONG *aIPV6NetworkMaskPrefixLength)
|
---|
359 | {
|
---|
360 | CheckComArgOutPointerValid(aIPV6NetworkMaskPrefixLength);
|
---|
361 |
|
---|
362 | AutoCaller autoCaller(this);
|
---|
363 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
364 |
|
---|
365 | *aIPV6NetworkMaskPrefixLength = m.IPV6NetworkMaskPrefixLength;
|
---|
366 |
|
---|
367 | return S_OK;
|
---|
368 | }
|
---|
369 |
|
---|
370 | /**
|
---|
371 | * Returns the hardware address of the host network interface.
|
---|
372 | *
|
---|
373 | * @returns COM status code
|
---|
374 | * @param aHardwareAddress address of result pointer
|
---|
375 | */
|
---|
376 | STDMETHODIMP HostNetworkInterface::COMGETTER(HardwareAddress)(BSTR *aHardwareAddress)
|
---|
377 | {
|
---|
378 | CheckComArgOutPointerValid(aHardwareAddress);
|
---|
379 |
|
---|
380 | AutoCaller autoCaller(this);
|
---|
381 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
382 |
|
---|
383 | m.hardwareAddress.cloneTo(aHardwareAddress);
|
---|
384 |
|
---|
385 | return S_OK;
|
---|
386 | }
|
---|
387 |
|
---|
388 | /**
|
---|
389 | * Returns the encapsulation protocol type of the host network interface.
|
---|
390 | *
|
---|
391 | * @returns COM status code
|
---|
392 | * @param aType address of result pointer
|
---|
393 | */
|
---|
394 | STDMETHODIMP HostNetworkInterface::COMGETTER(MediumType)(HostNetworkInterfaceMediumType_T *aType)
|
---|
395 | {
|
---|
396 | CheckComArgOutPointerValid(aType);
|
---|
397 |
|
---|
398 | AutoCaller autoCaller(this);
|
---|
399 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
400 |
|
---|
401 | *aType = m.mediumType;
|
---|
402 |
|
---|
403 | return S_OK;
|
---|
404 | }
|
---|
405 |
|
---|
406 | /**
|
---|
407 | * Returns the current state of the host network interface.
|
---|
408 | *
|
---|
409 | * @returns COM status code
|
---|
410 | * @param aStatus address of result pointer
|
---|
411 | */
|
---|
412 | STDMETHODIMP HostNetworkInterface::COMGETTER(Status)(HostNetworkInterfaceStatus_T *aStatus)
|
---|
413 | {
|
---|
414 | CheckComArgOutPointerValid(aStatus);
|
---|
415 |
|
---|
416 | AutoCaller autoCaller(this);
|
---|
417 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
418 |
|
---|
419 | *aStatus = m.status;
|
---|
420 |
|
---|
421 | return S_OK;
|
---|
422 | }
|
---|
423 |
|
---|
424 | /**
|
---|
425 | * Returns network interface type
|
---|
426 | *
|
---|
427 | * @returns COM status code
|
---|
428 | * @param aType address of result pointer
|
---|
429 | */
|
---|
430 | STDMETHODIMP HostNetworkInterface::COMGETTER(InterfaceType)(HostNetworkInterfaceType_T *aType)
|
---|
431 | {
|
---|
432 | CheckComArgOutPointerValid(aType);
|
---|
433 |
|
---|
434 | AutoCaller autoCaller(this);
|
---|
435 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
436 |
|
---|
437 | *aType = mIfType;
|
---|
438 |
|
---|
439 | return S_OK;
|
---|
440 |
|
---|
441 | }
|
---|
442 |
|
---|
443 | STDMETHODIMP HostNetworkInterface::COMGETTER(NetworkName)(BSTR *aNetworkName)
|
---|
444 | {
|
---|
445 | CheckComArgOutPointerValid(aNetworkName);
|
---|
446 |
|
---|
447 | AutoCaller autoCaller(this);
|
---|
448 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
449 |
|
---|
450 | mNetworkName.cloneTo(aNetworkName);
|
---|
451 |
|
---|
452 | return S_OK;
|
---|
453 | }
|
---|
454 |
|
---|
455 | STDMETHODIMP HostNetworkInterface::EnableStaticIPConfig(IN_BSTR aIPAddress, IN_BSTR aNetMask)
|
---|
456 | {
|
---|
457 | #ifndef VBOX_WITH_HOSTNETIF_API
|
---|
458 | return E_NOTIMPL;
|
---|
459 | #else
|
---|
460 | AutoCaller autoCaller(this);
|
---|
461 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
462 |
|
---|
463 | if (Bstr(aIPAddress).isEmpty())
|
---|
464 | {
|
---|
465 | if (m.IPAddress)
|
---|
466 | {
|
---|
467 | int rc = NetIfEnableStaticIpConfig(mVBox, this, m.IPAddress, 0, 0);
|
---|
468 | if (RT_SUCCESS(rc))
|
---|
469 | {
|
---|
470 | m.realIPAddress = 0;
|
---|
471 | if (FAILED(mVBox->SetExtraData(BstrFmt("HostOnly/%ls/IPAddress", mInterfaceName.raw()).raw(), NULL)))
|
---|
472 | return E_FAIL;
|
---|
473 | if (FAILED(mVBox->SetExtraData(BstrFmt("HostOnly/%ls/IPNetMask", mInterfaceName.raw()).raw(), NULL)))
|
---|
474 | return E_FAIL;
|
---|
475 | return S_OK;
|
---|
476 | }
|
---|
477 | }
|
---|
478 | else
|
---|
479 | return S_OK;
|
---|
480 | }
|
---|
481 |
|
---|
482 | ULONG ip, mask;
|
---|
483 | ip = inet_addr(Utf8Str(aIPAddress).c_str());
|
---|
484 | if (ip != INADDR_NONE)
|
---|
485 | {
|
---|
486 | if (Bstr(aNetMask).isEmpty())
|
---|
487 | mask = 0xFFFFFF;
|
---|
488 | else
|
---|
489 | mask = inet_addr(Utf8Str(aNetMask).c_str());
|
---|
490 | if (mask != INADDR_NONE)
|
---|
491 | {
|
---|
492 | if (m.realIPAddress == ip && m.realNetworkMask == mask)
|
---|
493 | return S_OK;
|
---|
494 | int rc = NetIfEnableStaticIpConfig(mVBox, this, m.IPAddress, ip, mask);
|
---|
495 | if (RT_SUCCESS(rc))
|
---|
496 | {
|
---|
497 | m.realIPAddress = ip;
|
---|
498 | m.realNetworkMask = mask;
|
---|
499 | if (FAILED(mVBox->SetExtraData(BstrFmt("HostOnly/%ls/IPAddress", mInterfaceName.raw()).raw(),
|
---|
500 | Bstr(aIPAddress).raw())))
|
---|
501 | return E_FAIL;
|
---|
502 | if (FAILED(mVBox->SetExtraData(BstrFmt("HostOnly/%ls/IPNetMask", mInterfaceName.raw()).raw(),
|
---|
503 | Bstr(aNetMask).raw())))
|
---|
504 | return E_FAIL;
|
---|
505 | return S_OK;
|
---|
506 | }
|
---|
507 | else
|
---|
508 | {
|
---|
509 | LogRel(("Failed to EnableStaticIpConfig with rc=%Rrc\n", rc));
|
---|
510 | return rc == VERR_NOT_IMPLEMENTED ? E_NOTIMPL : E_FAIL;
|
---|
511 | }
|
---|
512 |
|
---|
513 | }
|
---|
514 | }
|
---|
515 | return E_FAIL;
|
---|
516 | #endif
|
---|
517 | }
|
---|
518 |
|
---|
519 | STDMETHODIMP HostNetworkInterface::EnableStaticIPConfigV6(IN_BSTR aIPV6Address, ULONG aIPV6MaskPrefixLength)
|
---|
520 | {
|
---|
521 | #ifndef VBOX_WITH_HOSTNETIF_API
|
---|
522 | return E_NOTIMPL;
|
---|
523 | #else
|
---|
524 | if (!aIPV6Address)
|
---|
525 | return E_INVALIDARG;
|
---|
526 | if (aIPV6MaskPrefixLength > 128)
|
---|
527 | return E_INVALIDARG;
|
---|
528 |
|
---|
529 | AutoCaller autoCaller(this);
|
---|
530 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
531 |
|
---|
532 | int rc = S_OK;
|
---|
533 | if (m.realIPV6Address != aIPV6Address || m.realIPV6PrefixLength != aIPV6MaskPrefixLength)
|
---|
534 | {
|
---|
535 | if (aIPV6MaskPrefixLength == 0)
|
---|
536 | aIPV6MaskPrefixLength = 64;
|
---|
537 | rc = NetIfEnableStaticIpConfigV6(mVBox, this, m.IPV6Address.raw(), aIPV6Address, aIPV6MaskPrefixLength);
|
---|
538 | if (RT_FAILURE(rc))
|
---|
539 | {
|
---|
540 | LogRel(("Failed to EnableStaticIpConfigV6 with rc=%Rrc\n", rc));
|
---|
541 | return rc == VERR_NOT_IMPLEMENTED ? E_NOTIMPL : E_FAIL;
|
---|
542 | }
|
---|
543 | else
|
---|
544 | {
|
---|
545 | m.realIPV6Address = aIPV6Address;
|
---|
546 | m.realIPV6PrefixLength = aIPV6MaskPrefixLength;
|
---|
547 | if (FAILED(mVBox->SetExtraData(BstrFmt("HostOnly/%ls/IPV6Address", mInterfaceName.raw()).raw(),
|
---|
548 | Bstr(aIPV6Address).raw())))
|
---|
549 | return E_FAIL;
|
---|
550 | if (FAILED(mVBox->SetExtraData(BstrFmt("HostOnly/%ls/IPV6NetMask", mInterfaceName.raw()).raw(),
|
---|
551 | BstrFmt("%u", aIPV6MaskPrefixLength).raw())))
|
---|
552 | return E_FAIL;
|
---|
553 | }
|
---|
554 |
|
---|
555 | }
|
---|
556 | return S_OK;
|
---|
557 | #endif
|
---|
558 | }
|
---|
559 |
|
---|
560 | STDMETHODIMP HostNetworkInterface::EnableDynamicIPConfig()
|
---|
561 | {
|
---|
562 | #ifndef VBOX_WITH_HOSTNETIF_API
|
---|
563 | return E_NOTIMPL;
|
---|
564 | #else
|
---|
565 | AutoCaller autoCaller(this);
|
---|
566 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
567 |
|
---|
568 | int rc = NetIfEnableDynamicIpConfig(mVBox, this);
|
---|
569 | if (RT_FAILURE(rc))
|
---|
570 | {
|
---|
571 | LogRel(("Failed to EnableDynamicIpConfig with rc=%Rrc\n", rc));
|
---|
572 | return rc == VERR_NOT_IMPLEMENTED ? E_NOTIMPL : E_FAIL;
|
---|
573 | }
|
---|
574 | return S_OK;
|
---|
575 | #endif
|
---|
576 | }
|
---|
577 |
|
---|
578 | STDMETHODIMP HostNetworkInterface::DHCPRediscover()
|
---|
579 | {
|
---|
580 | #ifndef VBOX_WITH_HOSTNETIF_API
|
---|
581 | return E_NOTIMPL;
|
---|
582 | #else
|
---|
583 | AutoCaller autoCaller(this);
|
---|
584 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
585 |
|
---|
586 | int rc = NetIfDhcpRediscover(mVBox, this);
|
---|
587 | if (RT_FAILURE(rc))
|
---|
588 | {
|
---|
589 | LogRel(("Failed to DhcpRediscover with rc=%Rrc\n", rc));
|
---|
590 | return rc == VERR_NOT_IMPLEMENTED ? E_NOTIMPL : E_FAIL;
|
---|
591 | }
|
---|
592 | return S_OK;
|
---|
593 | #endif
|
---|
594 | }
|
---|
595 |
|
---|
596 | HRESULT HostNetworkInterface::setVirtualBox(VirtualBox *pVBox)
|
---|
597 | {
|
---|
598 | AutoCaller autoCaller(this);
|
---|
599 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
600 | AssertReturn(mVBox != pVBox, S_OK);
|
---|
601 |
|
---|
602 | unconst(mVBox) = pVBox;
|
---|
603 |
|
---|
604 | #if !defined(RT_OS_WINDOWS)
|
---|
605 | /* If IPv4 address hasn't been initialized */
|
---|
606 | if (m.IPAddress == 0 && mIfType == HostNetworkInterfaceType_HostOnly)
|
---|
607 | {
|
---|
608 | Bstr tmpAddr, tmpMask;
|
---|
609 | HRESULT hrc = mVBox->GetExtraData(BstrFmt("HostOnly/%ls/IPAddress", mInterfaceName.raw()).raw(),
|
---|
610 | tmpAddr.asOutParam());
|
---|
611 | if (FAILED(hrc) || tmpAddr.isEmpty())
|
---|
612 | tmpAddr = getDefaultIPv4Address(mInterfaceName);
|
---|
613 |
|
---|
614 | hrc = mVBox->GetExtraData(BstrFmt("HostOnly/%ls/IPNetMask", mInterfaceName.raw()).raw(),
|
---|
615 | tmpMask.asOutParam());
|
---|
616 | if (FAILED(hrc) || tmpMask.isEmpty())
|
---|
617 | tmpMask = Bstr(VBOXNET_IPV4MASK_DEFAULT);
|
---|
618 |
|
---|
619 | m.IPAddress = inet_addr(Utf8Str(tmpAddr).c_str());
|
---|
620 | m.networkMask = inet_addr(Utf8Str(tmpMask).c_str());
|
---|
621 | }
|
---|
622 |
|
---|
623 | if (m.IPV6Address.isEmpty())
|
---|
624 | {
|
---|
625 | Bstr tmpPrefixLen;
|
---|
626 | HRESULT hrc = mVBox->GetExtraData(BstrFmt("HostOnly/%ls/IPV6Address", mInterfaceName.raw()).raw(),
|
---|
627 | m.IPV6Address.asOutParam());
|
---|
628 | if (SUCCEEDED(hrc) && !m.IPV6Address.isEmpty())
|
---|
629 | {
|
---|
630 | hrc = mVBox->GetExtraData(BstrFmt("HostOnly/%ls/IPV6PrefixLen", mInterfaceName.raw()).raw(),
|
---|
631 | tmpPrefixLen.asOutParam());
|
---|
632 | if (SUCCEEDED(hrc) && !tmpPrefixLen.isEmpty())
|
---|
633 | m.IPV6NetworkMaskPrefixLength = Utf8Str(tmpPrefixLen).toUInt32();
|
---|
634 | else
|
---|
635 | m.IPV6NetworkMaskPrefixLength = 64;
|
---|
636 | }
|
---|
637 | }
|
---|
638 | #endif
|
---|
639 |
|
---|
640 | return S_OK;
|
---|
641 | }
|
---|
642 |
|
---|
643 | /* vi: set tabstop=4 shiftwidth=4 expandtab: */
|
---|