VirtualBox

source: vbox/trunk/src/VBox/Main/src-server/win/NetIf-win.cpp@ 67725

Last change on this file since 67725 was 67725, checked in by vboxsync, 7 years ago

Main/NetIf-win: bugref:8346: the distinction between XP and Vista doesn't work according to the length of IP_ADAPTER_UNICAST_ADDRESS_XP versus IP_ADAPTER_UNICAST_ADDRESS_LH -- both have the same length despite the fact that the former lacks the OnLinkPrefixLength field. So just use GetVersionEx(). Also logging fixes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 69.6 KB
Line 
1/* $Id: NetIf-win.cpp 67725 2017-06-30 11:35:32Z vboxsync $ */
2/** @file
3 * Main - NetIfList, Windows implementation.
4 */
5
6/*
7 * Copyright (C) 2008-2016 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19
20/*********************************************************************************************************************************
21* Header Files *
22*********************************************************************************************************************************/
23#define LOG_GROUP LOG_GROUP_MAIN
24
25#define NETIF_WITHOUT_NETCFG
26
27#include <iprt/err.h>
28#include <list>
29
30#define _WIN32_DCOM
31#include <iprt/win/winsock2.h>
32#include <iprt/win/ws2tcpip.h>
33#include <iprt/win/windows.h>
34#include <tchar.h>
35
36#ifdef VBOX_WITH_NETFLT
37# include "VBox/VBoxNetCfg-win.h"
38# include "devguid.h"
39#endif
40
41#include <iprt/win/iphlpapi.h>
42
43#include "Logging.h"
44#include "HostNetworkInterfaceImpl.h"
45#include "ProgressImpl.h"
46#include "VirtualBoxImpl.h"
47#include "netif.h"
48#include "ThreadTask.h"
49
50#ifdef VBOX_WITH_NETFLT
51#include <Wbemidl.h>
52#include <comdef.h>
53
54#include "svchlp.h"
55
56#include <shellapi.h>
57#define INITGUID
58#include <guiddef.h>
59#include <devguid.h>
60#include <iprt/win/objbase.h>
61#include <iprt/win/setupapi.h>
62#include <iprt/win/shlobj.h>
63#include <cfgmgr32.h>
64
65#define VBOX_APP_NAME L"VirtualBox"
66
67static int getDefaultInterfaceIndex()
68{
69 PMIB_IPFORWARDTABLE pIpTable;
70 DWORD dwSize = sizeof(MIB_IPFORWARDTABLE) * 20;
71 DWORD dwRC = NO_ERROR;
72 int iIndex = -1;
73
74 pIpTable = (MIB_IPFORWARDTABLE *)RTMemAlloc(dwSize);
75 if (GetIpForwardTable(pIpTable, &dwSize, 0) == ERROR_INSUFFICIENT_BUFFER)
76 {
77 RTMemFree(pIpTable);
78 pIpTable = (MIB_IPFORWARDTABLE *)RTMemAlloc(dwSize);
79 if (!pIpTable)
80 return -1;
81 }
82 dwRC = GetIpForwardTable(pIpTable, &dwSize, 0);
83 if (dwRC == NO_ERROR)
84 {
85 for (unsigned int i = 0; i < pIpTable->dwNumEntries; i++)
86 if (pIpTable->table[i].dwForwardDest == 0)
87 {
88 iIndex = pIpTable->table[i].dwForwardIfIndex;
89 break;
90 }
91 }
92 RTMemFree(pIpTable);
93 return iIndex;
94}
95
96static int collectNetIfInfo(Bstr &strName, Guid &guid, PNETIFINFO pInfo, int iDefault)
97{
98 RT_NOREF(strName);
99
100 /*
101 * Most of the hosts probably have less than 10 adapters,
102 * so we'll mostly succeed from the first attempt.
103 */
104 ULONG uBufLen = sizeof(IP_ADAPTER_ADDRESSES) * 10;
105 PIP_ADAPTER_ADDRESSES pAddresses = (PIP_ADAPTER_ADDRESSES)RTMemAlloc(uBufLen);
106 if (!pAddresses)
107 return VERR_NO_MEMORY;
108 DWORD dwRc = GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_PREFIX, NULL, pAddresses, &uBufLen);
109 if (dwRc == ERROR_BUFFER_OVERFLOW)
110 {
111 /* Impressive! More than 10 adapters! Get more memory and try again. */
112 RTMemFree(pAddresses);
113 pAddresses = (PIP_ADAPTER_ADDRESSES)RTMemAlloc(uBufLen);
114 if (!pAddresses)
115 return VERR_NO_MEMORY;
116 dwRc = GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_PREFIX, NULL, pAddresses, &uBufLen);
117 }
118 if (dwRc == NO_ERROR)
119 {
120 PIP_ADAPTER_ADDRESSES pAdapter;
121 for (pAdapter = pAddresses; pAdapter; pAdapter = pAdapter->Next)
122 {
123 char *pszUuid = RTStrDup(pAdapter->AdapterName);
124 size_t len = strlen(pszUuid) - 1;
125 if (pszUuid[0] == '{' && pszUuid[len] == '}')
126 {
127 pszUuid[len] = 0;
128 if (!RTUuidCompareStr(&pInfo->Uuid, pszUuid + 1))
129 {
130 bool fIPFound, fIPv6Found;
131 PIP_ADAPTER_UNICAST_ADDRESS pAddr;
132 fIPFound = fIPv6Found = false;
133 for (pAddr = pAdapter->FirstUnicastAddress; pAddr; pAddr = pAddr->Next)
134 {
135 switch (pAddr->Address.lpSockaddr->sa_family)
136 {
137 case AF_INET:
138 if (!fIPFound)
139 {
140 fIPFound = true;
141 memcpy(&pInfo->IPAddress,
142 &((struct sockaddr_in *)pAddr->Address.lpSockaddr)->sin_addr.s_addr,
143 sizeof(pInfo->IPAddress));
144 }
145 break;
146 case AF_INET6:
147 if (!fIPv6Found)
148 {
149 fIPv6Found = true;
150 memcpy(&pInfo->IPv6Address,
151 ((struct sockaddr_in6 *)pAddr->Address.lpSockaddr)->sin6_addr.s6_addr,
152 sizeof(pInfo->IPv6Address));
153 }
154 break;
155 }
156 }
157 PIP_ADAPTER_PREFIX pPrefix;
158 fIPFound = fIPv6Found = false;
159 for (pPrefix = pAdapter->FirstPrefix; pPrefix; pPrefix = pPrefix->Next)
160 {
161 switch (pPrefix->Address.lpSockaddr->sa_family)
162 {
163 case AF_INET:
164 if (!fIPFound)
165 {
166 if (pPrefix->PrefixLength <= sizeof(pInfo->IPNetMask) * 8)
167 {
168 fIPFound = true;
169 RTNetPrefixToMaskIPv4(pPrefix->PrefixLength, &pInfo->IPNetMask);
170 }
171 else
172 LogFunc(("Unexpected IPv4 prefix length of %d\n",
173 pPrefix->PrefixLength));
174 }
175 break;
176 case AF_INET6:
177 if (!fIPv6Found)
178 {
179 if (pPrefix->PrefixLength <= sizeof(pInfo->IPv6NetMask) * 8)
180 {
181 fIPv6Found = true;
182 RTNetPrefixToMaskIPv6(pPrefix->PrefixLength, &pInfo->IPv6NetMask);
183 }
184 else
185 LogFunc(("Unexpected IPv6 prefix length of %d\n",
186 pPrefix->PrefixLength));
187 }
188 break;
189 }
190 }
191 if (sizeof(pInfo->MACAddress) != pAdapter->PhysicalAddressLength)
192 LogFunc(("Unexpected physical address length: %u\n", pAdapter->PhysicalAddressLength));
193 else
194 memcpy(pInfo->MACAddress.au8, pAdapter->PhysicalAddress, sizeof(pInfo->MACAddress));
195 pInfo->enmMediumType = NETIF_T_ETHERNET;
196 pInfo->enmStatus = pAdapter->OperStatus == IfOperStatusUp ? NETIF_S_UP : NETIF_S_DOWN;
197 pInfo->bIsDefault = (pAdapter->IfIndex == (DWORD)iDefault);
198 RTStrFree(pszUuid);
199 break;
200 }
201 }
202 RTStrFree(pszUuid);
203 }
204
205 ADAPTER_SETTINGS Settings;
206 HRESULT hr = VBoxNetCfgWinGetAdapterSettings((const GUID *)guid.raw(), &Settings);
207 if (hr == S_OK)
208 {
209 if (Settings.ip)
210 {
211 pInfo->IPAddress.u = Settings.ip;
212 pInfo->IPNetMask.u = Settings.mask;
213 }
214 pInfo->bDhcpEnabled = Settings.bDhcp;
215 }
216 else
217 {
218 pInfo->bDhcpEnabled = false;
219 }
220 }
221 RTMemFree(pAddresses);
222
223 return VINF_SUCCESS;
224}
225
226/* svc helper func */
227
228struct StaticIpConfig
229{
230 ULONG IPAddress;
231 ULONG IPNetMask;
232};
233
234struct StaticIpV6Config
235{
236 BSTR IPV6Address;
237 ULONG IPV6NetMaskLength;
238};
239
240class NetworkInterfaceHelperClientData : public ThreadVoidData
241{
242public:
243 NetworkInterfaceHelperClientData(){};
244 ~NetworkInterfaceHelperClientData(){};
245
246 SVCHlpMsg::Code msgCode;
247 /* for SVCHlpMsg::CreateHostOnlyNetworkInterface */
248 Bstr name;
249 ComObjPtr<HostNetworkInterface> iface;
250 ComObjPtr<VirtualBox> vBox;
251 /* for SVCHlpMsg::RemoveHostOnlyNetworkInterface */
252 Guid guid;
253
254 union
255 {
256 StaticIpConfig StaticIP;
257 StaticIpV6Config StaticIPV6;
258 } u;
259
260};
261
262static HRESULT netIfNetworkInterfaceHelperClient(SVCHlpClient *aClient,
263 Progress *aProgress,
264 void *aUser, int *aVrc)
265{
266 LogFlowFuncEnter();
267 LogFlowFunc(("aClient={%p}, aProgress={%p}, aUser={%p}\n",
268 aClient, aProgress, aUser));
269
270 AssertReturn( (aClient == NULL && aProgress == NULL && aVrc == NULL)
271 || (aClient != NULL && aProgress != NULL && aVrc != NULL),
272 E_POINTER);
273 AssertReturn(aUser, E_POINTER);
274
275 NetworkInterfaceHelperClientData* d = static_cast<NetworkInterfaceHelperClientData *>(aUser);
276
277 if (aClient == NULL)
278 {
279 /* "cleanup only" mode, just return (it will free aUser) */
280 return S_OK;
281 }
282
283 HRESULT rc = S_OK;
284 int vrc = VINF_SUCCESS;
285
286 switch (d->msgCode)
287 {
288 case SVCHlpMsg::CreateHostOnlyNetworkInterface:
289 {
290 LogFlowFunc(("CreateHostOnlyNetworkInterface:\n"));
291 LogFlowFunc(("Network connection name = '%ls'\n", d->name.raw()));
292
293 /* write message and parameters */
294 vrc = aClient->write(d->msgCode);
295 if (RT_FAILURE(vrc)) break;
296// vrc = aClient->write(Utf8Str(d->name));
297// if (RT_FAILURE(vrc)) break;
298
299 /* wait for a reply */
300 bool endLoop = false;
301 while (!endLoop)
302 {
303 SVCHlpMsg::Code reply = SVCHlpMsg::Null;
304
305 vrc = aClient->read(reply);
306 if (RT_FAILURE(vrc)) break;
307
308 switch (reply)
309 {
310 case SVCHlpMsg::CreateHostOnlyNetworkInterface_OK:
311 {
312 /* read the GUID */
313 Guid guid;
314 Utf8Str name;
315 vrc = aClient->read(name);
316 if (RT_FAILURE(vrc)) break;
317 vrc = aClient->read(guid);
318 if (RT_FAILURE(vrc)) break;
319
320 LogFlowFunc(("Network connection GUID = {%RTuuid}\n", guid.raw()));
321
322 /* initialize the object returned to the caller by
323 * CreateHostOnlyNetworkInterface() */
324 rc = d->iface->init(Bstr(name), Bstr(name), guid, HostNetworkInterfaceType_HostOnly);
325 if (SUCCEEDED(rc))
326 {
327 rc = d->iface->i_setVirtualBox(d->vBox);
328 if (SUCCEEDED(rc))
329 {
330 rc = d->iface->updateConfig();
331 }
332 }
333 endLoop = true;
334 break;
335 }
336 case SVCHlpMsg::Error:
337 {
338 /* read the error message */
339 Utf8Str errMsg;
340 vrc = aClient->read(errMsg);
341 if (RT_FAILURE(vrc)) break;
342
343 rc = E_FAIL;
344 d->iface->setError(E_FAIL, errMsg.c_str());
345 endLoop = true;
346 break;
347 }
348 default:
349 {
350 endLoop = true;
351 rc = E_FAIL;/// @todo ComAssertMsgFailedBreak((
352 //"Invalid message code %d (%08lX)\n",
353 //reply, reply),
354 //rc = E_FAIL);
355 }
356 }
357 }
358
359 break;
360 }
361 case SVCHlpMsg::RemoveHostOnlyNetworkInterface:
362 {
363 LogFlowFunc(("RemoveHostOnlyNetworkInterface:\n"));
364 LogFlowFunc(("Network connection GUID = {%RTuuid}\n", d->guid.raw()));
365
366 /* write message and parameters */
367 vrc = aClient->write(d->msgCode);
368 if (RT_FAILURE(vrc)) break;
369 vrc = aClient->write(d->guid);
370 if (RT_FAILURE(vrc)) break;
371
372 /* wait for a reply */
373 bool endLoop = false;
374 while (!endLoop)
375 {
376 SVCHlpMsg::Code reply = SVCHlpMsg::Null;
377
378 vrc = aClient->read(reply);
379 if (RT_FAILURE(vrc)) break;
380
381 switch (reply)
382 {
383 case SVCHlpMsg::OK:
384 {
385 /* no parameters */
386 rc = S_OK;
387 endLoop = true;
388 break;
389 }
390 case SVCHlpMsg::Error:
391 {
392 /* read the error message */
393 Utf8Str errMsg;
394 vrc = aClient->read(errMsg);
395 if (RT_FAILURE(vrc)) break;
396
397 rc = E_FAIL;
398 d->iface->setError(E_FAIL, errMsg.c_str());
399 endLoop = true;
400 break;
401 }
402 default:
403 {
404 endLoop = true;
405 rc = E_FAIL; /// @todo ComAssertMsgFailedBreak((
406 //"Invalid message code %d (%08lX)\n",
407 //reply, reply),
408 //rc = E_FAIL);
409 }
410 }
411 }
412
413 break;
414 }
415 case SVCHlpMsg::EnableDynamicIpConfig: /* see usage in code */
416 {
417 LogFlowFunc(("EnableDynamicIpConfig:\n"));
418 LogFlowFunc(("Network connection name = '%ls'\n", d->name.raw()));
419
420 /* write message and parameters */
421 vrc = aClient->write(d->msgCode);
422 if (RT_FAILURE(vrc)) break;
423 vrc = aClient->write(d->guid);
424 if (RT_FAILURE(vrc)) break;
425
426 /* wait for a reply */
427 bool endLoop = false;
428 while (!endLoop)
429 {
430 SVCHlpMsg::Code reply = SVCHlpMsg::Null;
431
432 vrc = aClient->read(reply);
433 if (RT_FAILURE(vrc)) break;
434
435 switch (reply)
436 {
437 case SVCHlpMsg::OK:
438 {
439 /* no parameters */
440 rc = d->iface->updateConfig();
441 endLoop = true;
442 break;
443 }
444 case SVCHlpMsg::Error:
445 {
446 /* read the error message */
447 Utf8Str errMsg;
448 vrc = aClient->read(errMsg);
449 if (RT_FAILURE(vrc)) break;
450
451 rc = E_FAIL;
452 d->iface->setError(E_FAIL, errMsg.c_str());
453 endLoop = true;
454 break;
455 }
456 default:
457 {
458 endLoop = true;
459 rc = E_FAIL; /// @todo ComAssertMsgFailedBreak((
460 //"Invalid message code %d (%08lX)\n",
461 //reply, reply),
462 //rc = E_FAIL);
463 }
464 }
465 }
466
467 break;
468 }
469 case SVCHlpMsg::EnableStaticIpConfig: /* see usage in code */
470 {
471 LogFlowFunc(("EnableStaticIpConfig:\n"));
472 LogFlowFunc(("Network connection name = '%ls'\n", d->name.raw()));
473
474 /* write message and parameters */
475 vrc = aClient->write(d->msgCode);
476 if (RT_FAILURE(vrc)) break;
477 vrc = aClient->write(d->guid);
478 if (RT_FAILURE(vrc)) break;
479 vrc = aClient->write(d->u.StaticIP.IPAddress);
480 if (RT_FAILURE(vrc)) break;
481 vrc = aClient->write(d->u.StaticIP.IPNetMask);
482 if (RT_FAILURE(vrc)) break;
483
484 /* wait for a reply */
485 bool endLoop = false;
486 while (!endLoop)
487 {
488 SVCHlpMsg::Code reply = SVCHlpMsg::Null;
489
490 vrc = aClient->read(reply);
491 if (RT_FAILURE(vrc)) break;
492
493 switch (reply)
494 {
495 case SVCHlpMsg::OK:
496 {
497 /* no parameters */
498 rc = d->iface->updateConfig();
499 endLoop = true;
500 break;
501 }
502 case SVCHlpMsg::Error:
503 {
504 /* read the error message */
505 Utf8Str errMsg;
506 vrc = aClient->read(errMsg);
507 if (RT_FAILURE(vrc)) break;
508
509 rc = E_FAIL;
510 d->iface->setError(E_FAIL, errMsg.c_str());
511 endLoop = true;
512 break;
513 }
514 default:
515 {
516 endLoop = true;
517 rc = E_FAIL; /// @todo ComAssertMsgFailedBreak((
518 //"Invalid message code %d (%08lX)\n",
519 //reply, reply),
520 //rc = E_FAIL);
521 }
522 }
523 }
524
525 break;
526 }
527 case SVCHlpMsg::EnableStaticIpConfigV6: /* see usage in code */
528 {
529 LogFlowFunc(("EnableStaticIpConfigV6:\n"));
530 LogFlowFunc(("Network connection name = '%ls'\n", d->name.raw()));
531
532 /* write message and parameters */
533 vrc = aClient->write(d->msgCode);
534 if (RT_FAILURE(vrc)) break;
535 vrc = aClient->write(d->guid);
536 if (RT_FAILURE(vrc)) break;
537 vrc = aClient->write(Utf8Str(d->u.StaticIPV6.IPV6Address));
538 if (RT_FAILURE(vrc)) break;
539 vrc = aClient->write(d->u.StaticIPV6.IPV6NetMaskLength);
540 if (RT_FAILURE(vrc)) break;
541
542 /* wait for a reply */
543 bool endLoop = false;
544 while (!endLoop)
545 {
546 SVCHlpMsg::Code reply = SVCHlpMsg::Null;
547
548 vrc = aClient->read(reply);
549 if (RT_FAILURE(vrc)) break;
550
551 switch (reply)
552 {
553 case SVCHlpMsg::OK:
554 {
555 /* no parameters */
556 rc = d->iface->updateConfig();
557 endLoop = true;
558 break;
559 }
560 case SVCHlpMsg::Error:
561 {
562 /* read the error message */
563 Utf8Str errMsg;
564 vrc = aClient->read(errMsg);
565 if (RT_FAILURE(vrc)) break;
566
567 rc = E_FAIL;
568 d->iface->setError(E_FAIL, errMsg.c_str());
569 endLoop = true;
570 break;
571 }
572 default:
573 {
574 endLoop = true;
575 rc = E_FAIL; /// @todo ComAssertMsgFailedBreak((
576 //"Invalid message code %d (%08lX)\n",
577 //reply, reply),
578 //rc = E_FAIL);
579 }
580 }
581 }
582
583 break;
584 }
585 case SVCHlpMsg::DhcpRediscover: /* see usage in code */
586 {
587 LogFlowFunc(("DhcpRediscover:\n"));
588 LogFlowFunc(("Network connection name = '%ls'\n", d->name.raw()));
589
590 /* write message and parameters */
591 vrc = aClient->write(d->msgCode);
592 if (RT_FAILURE(vrc)) break;
593 vrc = aClient->write(d->guid);
594 if (RT_FAILURE(vrc)) break;
595
596 /* wait for a reply */
597 bool endLoop = false;
598 while (!endLoop)
599 {
600 SVCHlpMsg::Code reply = SVCHlpMsg::Null;
601
602 vrc = aClient->read(reply);
603 if (RT_FAILURE(vrc)) break;
604
605 switch (reply)
606 {
607 case SVCHlpMsg::OK:
608 {
609 /* no parameters */
610 rc = d->iface->updateConfig();
611 endLoop = true;
612 break;
613 }
614 case SVCHlpMsg::Error:
615 {
616 /* read the error message */
617 Utf8Str errMsg;
618 vrc = aClient->read(errMsg);
619 if (RT_FAILURE(vrc)) break;
620
621 rc = E_FAIL;
622 d->iface->setError(E_FAIL, errMsg.c_str());
623 endLoop = true;
624 break;
625 }
626 default:
627 {
628 endLoop = true;
629 rc = E_FAIL; /// @todo ComAssertMsgFailedBreak((
630 //"Invalid message code %d (%08lX)\n",
631 //reply, reply),
632 //rc = E_FAIL);
633 }
634 }
635 }
636
637 break;
638 }
639 default:
640 rc = E_FAIL; /// @todo ComAssertMsgFailedBreak((
641// "Invalid message code %d (%08lX)\n",
642// d->msgCode, d->msgCode),
643// rc = E_FAIL);
644 }
645
646 if (aVrc)
647 *aVrc = vrc;
648
649 LogFlowFunc(("rc=0x%08X, vrc=%Rrc\n", rc, vrc));
650 LogFlowFuncLeave();
651 return rc;
652}
653
654
655int netIfNetworkInterfaceHelperServer(SVCHlpClient *aClient,
656 SVCHlpMsg::Code aMsgCode)
657{
658 LogFlowFuncEnter();
659 LogFlowFunc(("aClient={%p}, aMsgCode=%d\n", aClient, aMsgCode));
660
661 AssertReturn(aClient, VERR_INVALID_POINTER);
662
663 int vrc = VINF_SUCCESS;
664 HRESULT hrc;
665
666 switch (aMsgCode)
667 {
668 case SVCHlpMsg::CreateHostOnlyNetworkInterface:
669 {
670 LogFlowFunc(("CreateHostOnlyNetworkInterface:\n"));
671
672// Utf8Str name;
673// vrc = aClient->read(name);
674// if (RT_FAILURE(vrc)) break;
675
676 Guid guid;
677 Utf8Str errMsg;
678 Bstr name;
679 Bstr bstrErr;
680
681#ifdef VBOXNETCFG_DELAYEDRENAME
682 Bstr devId;
683 hrc = VBoxNetCfgWinCreateHostOnlyNetworkInterface(NULL, false, guid.asOutParam(), devId.asOutParam(),
684 bstrErr.asOutParam());
685#else /* !VBOXNETCFG_DELAYEDRENAME */
686 hrc = VBoxNetCfgWinCreateHostOnlyNetworkInterface(NULL, false, guid.asOutParam(), name.asOutParam(),
687 bstrErr.asOutParam());
688#endif /* !VBOXNETCFG_DELAYEDRENAME */
689
690 if (hrc == S_OK)
691 {
692 ULONG ip, mask;
693 hrc = VBoxNetCfgWinGenHostOnlyNetworkNetworkIp(&ip, &mask);
694 if (hrc == S_OK)
695 {
696 /* ip returned by VBoxNetCfgWinGenHostOnlyNetworkNetworkIp is a network ip,
697 * i.e. 192.168.xxx.0, assign 192.168.xxx.1 for the hostonly adapter */
698 ip = ip | (1 << 24);
699 hrc = VBoxNetCfgWinEnableStaticIpConfig((const GUID*)guid.raw(), ip, mask);
700 if (hrc != S_OK)
701 LogRel(("VBoxNetCfgWinEnableStaticIpConfig failed (0x%x)\n", hrc));
702 }
703 else
704 LogRel(("VBoxNetCfgWinGenHostOnlyNetworkNetworkIp failed (0x%x)\n", hrc));
705#ifdef VBOXNETCFG_DELAYEDRENAME
706 hrc = VBoxNetCfgWinRenameHostOnlyConnection((const GUID*)guid.raw(), devId.raw(), name.asOutParam());
707 if (hrc != S_OK)
708 LogRel(("VBoxNetCfgWinRenameHostOnlyConnection failed, error = 0x%x", hrc));
709#endif /* VBOXNETCFG_DELAYEDRENAME */
710 /* write success followed by GUID */
711 vrc = aClient->write(SVCHlpMsg::CreateHostOnlyNetworkInterface_OK);
712 if (RT_FAILURE(vrc)) break;
713 vrc = aClient->write(Utf8Str(name));
714 if (RT_FAILURE(vrc)) break;
715 vrc = aClient->write(guid);
716 if (RT_FAILURE(vrc)) break;
717 }
718 else
719 {
720 vrc = VERR_GENERAL_FAILURE;
721 errMsg = Utf8Str(bstrErr);
722 /* write failure followed by error message */
723 if (errMsg.isEmpty())
724 errMsg = Utf8StrFmt("Unspecified error (%Rrc)", vrc);
725 vrc = aClient->write(SVCHlpMsg::Error);
726 if (RT_FAILURE(vrc)) break;
727 vrc = aClient->write(errMsg);
728 if (RT_FAILURE(vrc)) break;
729 }
730
731 break;
732 }
733 case SVCHlpMsg::RemoveHostOnlyNetworkInterface:
734 {
735 LogFlowFunc(("RemoveHostOnlyNetworkInterface:\n"));
736
737 Guid guid;
738 Bstr bstrErr;
739
740 vrc = aClient->read(guid);
741 if (RT_FAILURE(vrc)) break;
742
743 Utf8Str errMsg;
744 hrc = VBoxNetCfgWinRemoveHostOnlyNetworkInterface((const GUID*)guid.raw(), bstrErr.asOutParam());
745
746 if (hrc == S_OK)
747 {
748 /* write parameter-less success */
749 vrc = aClient->write(SVCHlpMsg::OK);
750 if (RT_FAILURE(vrc)) break;
751 }
752 else
753 {
754 vrc = VERR_GENERAL_FAILURE;
755 errMsg = Utf8Str(bstrErr);
756 /* write failure followed by error message */
757 if (errMsg.isEmpty())
758 errMsg = Utf8StrFmt("Unspecified error (%Rrc)", vrc);
759 vrc = aClient->write(SVCHlpMsg::Error);
760 if (RT_FAILURE(vrc)) break;
761 vrc = aClient->write(errMsg);
762 if (RT_FAILURE(vrc)) break;
763 }
764
765 break;
766 }
767 case SVCHlpMsg::EnableStaticIpConfigV6:
768 {
769 LogFlowFunc(("EnableStaticIpConfigV6:\n"));
770
771 Guid guid;
772 Utf8Str ipV6;
773 ULONG maskLengthV6;
774 vrc = aClient->read(guid);
775 if (RT_FAILURE(vrc)) break;
776 vrc = aClient->read(ipV6);
777 if (RT_FAILURE(vrc)) break;
778 vrc = aClient->read(maskLengthV6);
779 if (RT_FAILURE(vrc)) break;
780
781 Utf8Str errMsg;
782 vrc = VERR_NOT_IMPLEMENTED;
783
784 if (RT_SUCCESS(vrc))
785 {
786 /* write success followed by GUID */
787 vrc = aClient->write(SVCHlpMsg::OK);
788 if (RT_FAILURE(vrc)) break;
789 }
790 else
791 {
792 /* write failure followed by error message */
793 if (errMsg.isEmpty())
794 errMsg = Utf8StrFmt("Unspecified error (%Rrc)", vrc);
795 vrc = aClient->write(SVCHlpMsg::Error);
796 if (RT_FAILURE(vrc)) break;
797 vrc = aClient->write(errMsg);
798 if (RT_FAILURE(vrc)) break;
799 }
800
801 break;
802 }
803 case SVCHlpMsg::EnableStaticIpConfig:
804 {
805 LogFlowFunc(("EnableStaticIpConfig:\n"));
806
807 Guid guid;
808 ULONG ip, mask;
809 vrc = aClient->read(guid);
810 if (RT_FAILURE(vrc)) break;
811 vrc = aClient->read(ip);
812 if (RT_FAILURE(vrc)) break;
813 vrc = aClient->read(mask);
814 if (RT_FAILURE(vrc)) break;
815
816 Utf8Str errMsg;
817 hrc = VBoxNetCfgWinEnableStaticIpConfig((const GUID *)guid.raw(), ip, mask);
818
819 if (hrc == S_OK)
820 {
821 /* write success followed by GUID */
822 vrc = aClient->write(SVCHlpMsg::OK);
823 if (RT_FAILURE(vrc)) break;
824 }
825 else
826 {
827 vrc = VERR_GENERAL_FAILURE;
828 /* write failure followed by error message */
829 if (errMsg.isEmpty())
830 errMsg = Utf8StrFmt("Unspecified error (%Rrc)", vrc);
831 vrc = aClient->write(SVCHlpMsg::Error);
832 if (RT_FAILURE(vrc)) break;
833 vrc = aClient->write(errMsg);
834 if (RT_FAILURE(vrc)) break;
835 }
836
837 break;
838 }
839 case SVCHlpMsg::EnableDynamicIpConfig:
840 {
841 LogFlowFunc(("EnableDynamicIpConfig:\n"));
842
843 Guid guid;
844 vrc = aClient->read(guid);
845 if (RT_FAILURE(vrc)) break;
846
847 Utf8Str errMsg;
848 hrc = VBoxNetCfgWinEnableDynamicIpConfig((const GUID *)guid.raw());
849
850 if (hrc == S_OK)
851 {
852 /* write success followed by GUID */
853 vrc = aClient->write(SVCHlpMsg::OK);
854 if (RT_FAILURE(vrc)) break;
855 }
856 else
857 {
858 vrc = VERR_GENERAL_FAILURE;
859 /* write failure followed by error message */
860 if (errMsg.isEmpty())
861 errMsg = Utf8StrFmt("Unspecified error (%Rrc)", vrc);
862 vrc = aClient->write(SVCHlpMsg::Error);
863 if (RT_FAILURE(vrc)) break;
864 vrc = aClient->write(errMsg);
865 if (RT_FAILURE(vrc)) break;
866 }
867
868 break;
869 }
870 case SVCHlpMsg::DhcpRediscover:
871 {
872 LogFlowFunc(("DhcpRediscover:\n"));
873
874 Guid guid;
875 vrc = aClient->read(guid);
876 if (RT_FAILURE(vrc)) break;
877
878 Utf8Str errMsg;
879 hrc = VBoxNetCfgWinDhcpRediscover((const GUID *)guid.raw());
880
881 if (hrc == S_OK)
882 {
883 /* write success followed by GUID */
884 vrc = aClient->write(SVCHlpMsg::OK);
885 if (RT_FAILURE(vrc)) break;
886 }
887 else
888 {
889 vrc = VERR_GENERAL_FAILURE;
890 /* write failure followed by error message */
891 if (errMsg.isEmpty())
892 errMsg = Utf8StrFmt("Unspecified error (%Rrc)", vrc);
893 vrc = aClient->write(SVCHlpMsg::Error);
894 if (RT_FAILURE(vrc)) break;
895 vrc = aClient->write(errMsg);
896 if (RT_FAILURE(vrc)) break;
897 }
898
899 break;
900 }
901 default:
902 AssertMsgFailedBreakStmt(
903 ("Invalid message code %d (%08lX)\n", aMsgCode, aMsgCode),
904 VERR_GENERAL_FAILURE);
905 }
906
907 LogFlowFunc(("vrc=%Rrc\n", vrc));
908 LogFlowFuncLeave();
909 return vrc;
910}
911
912/** @todo REMOVE. OBSOLETE NOW. */
913/**
914 * Returns TRUE if the Windows version is 6.0 or greater (i.e. it's Vista and
915 * later OSes) and it has the UAC (User Account Control) feature enabled.
916 */
917static BOOL IsUACEnabled()
918{
919 LONG rc = 0;
920
921 OSVERSIONINFOEX info;
922 ZeroMemory(&info, sizeof(OSVERSIONINFOEX));
923 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
924 rc = GetVersionEx((OSVERSIONINFO *) &info);
925 AssertReturn(rc != 0, FALSE);
926
927 LogFlowFunc(("dwMajorVersion=%d, dwMinorVersion=%d\n",
928 info.dwMajorVersion, info.dwMinorVersion));
929
930 /* we are interested only in Vista (and newer versions...). In all
931 * earlier versions UAC is not present. */
932 if (info.dwMajorVersion < 6)
933 return FALSE;
934
935 /* the default EnableLUA value is 1 (Enabled) */
936 DWORD dwEnableLUA = 1;
937
938 HKEY hKey;
939 rc = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
940 "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System",
941 0, KEY_QUERY_VALUE, &hKey);
942
943 Assert(rc == ERROR_SUCCESS || rc == ERROR_PATH_NOT_FOUND);
944 if (rc == ERROR_SUCCESS)
945 {
946
947 DWORD cbEnableLUA = sizeof(dwEnableLUA);
948 rc = RegQueryValueExA(hKey, "EnableLUA", NULL, NULL,
949 (LPBYTE) &dwEnableLUA, &cbEnableLUA);
950
951 RegCloseKey(hKey);
952
953 Assert(rc == ERROR_SUCCESS || rc == ERROR_FILE_NOT_FOUND);
954 }
955
956 LogFlowFunc(("rc=%d, dwEnableLUA=%d\n", rc, dwEnableLUA));
957
958 return dwEnableLUA == 1;
959}
960
961/* end */
962
963static int vboxNetWinAddComponent(std::list<ComObjPtr<HostNetworkInterface> > * pPist,
964 INetCfgComponent * pncc, HostNetworkInterfaceType enmType,
965 int iDefaultInterface)
966{
967 LPWSTR lpszName;
968 GUID IfGuid;
969 HRESULT hr;
970 int rc = VERR_GENERAL_FAILURE;
971
972 hr = pncc->GetDisplayName(&lpszName);
973 Assert(hr == S_OK);
974 if (hr == S_OK)
975 {
976 Bstr name(lpszName);
977
978 hr = pncc->GetInstanceGuid(&IfGuid);
979 Assert(hr == S_OK);
980 if (hr == S_OK)
981 {
982 Guid guidIfCopy(IfGuid);
983 NETIFINFO Info;
984 RT_ZERO(Info);
985 Info.Uuid = *guidIfCopy.raw();
986 rc = collectNetIfInfo(name, guidIfCopy, &Info, iDefaultInterface);
987 if (RT_FAILURE(rc))
988 LogRelFunc(("collectNetIfInfo() -> %Rrc\n", rc));
989 LogFunc(("adding %ls\n", lpszName));
990 /* create a new object and add it to the list */
991 ComObjPtr<HostNetworkInterface> iface;
992 iface.createObject();
993 /* remove the curly bracket at the end */
994 rc = iface->init(name, enmType, &Info);
995 if (SUCCEEDED(rc))
996 {
997 if (Info.bIsDefault)
998 pPist->push_front(iface);
999 else
1000 pPist->push_back(iface);
1001 }
1002 else
1003 {
1004 LogRelFunc(("HostNetworkInterface::init() -> %Rrc\n", rc));
1005 AssertComRC(rc);
1006 }
1007 }
1008 else
1009 LogRelFunc(("failed to get device instance GUID (0x%x)\n", hr));
1010 CoTaskMemFree(lpszName);
1011 }
1012 else
1013 LogRelFunc(("failed to get device display name (0x%x)\n", hr));
1014
1015 return rc;
1016}
1017
1018#endif /* VBOX_WITH_NETFLT */
1019
1020
1021static int netIfListHostAdapters(INetCfg *pNc, std::list<ComObjPtr<HostNetworkInterface> > &list)
1022{
1023#ifndef VBOX_WITH_NETFLT
1024 /* VBoxNetAdp is available only when VBOX_WITH_NETFLT is enabled */
1025 return VERR_NOT_IMPLEMENTED;
1026#else /* # if defined VBOX_WITH_NETFLT */
1027 INetCfgComponent *pMpNcc;
1028 HRESULT hr;
1029 IEnumNetCfgComponent *pEnumComponent;
1030
1031 hr = pNc->EnumComponents(&GUID_DEVCLASS_NET, &pEnumComponent);
1032 if (hr == S_OK)
1033 {
1034 while ((hr = pEnumComponent->Next(1, &pMpNcc, NULL)) == S_OK)
1035 {
1036 LPWSTR pwszName;
1037 ULONG uComponentStatus;
1038 hr = pMpNcc->GetDisplayName(&pwszName);
1039 if (hr == S_OK)
1040 LogFunc(("%ls\n", pwszName));
1041 else
1042 LogRelFunc(("failed to get device display name (0x%x)\n", hr));
1043 hr = pMpNcc->GetDeviceStatus(&uComponentStatus);
1044 if (hr == S_OK)
1045 {
1046 if (uComponentStatus == 0)
1047 {
1048 LPWSTR pId;
1049 hr = pMpNcc->GetId(&pId);
1050 Assert(hr == S_OK);
1051 if (hr == S_OK)
1052 {
1053 LogFunc(("id = %ls\n", pId));
1054 if (!_wcsnicmp(pId, L"sun_VBoxNetAdp", sizeof(L"sun_VBoxNetAdp")/2))
1055 {
1056 vboxNetWinAddComponent(&list, pMpNcc, HostNetworkInterfaceType_HostOnly, -1);
1057 }
1058 CoTaskMemFree(pId);
1059 }
1060 else
1061 LogRelFunc(("failed to get device id (0x%x)\n", hr));
1062 }
1063 }
1064 else
1065 LogRelFunc(("failed to get device status (0x%x)\n", hr));
1066 pMpNcc->Release();
1067 }
1068 Assert(hr == S_OK || hr == S_FALSE);
1069
1070 pEnumComponent->Release();
1071 }
1072 else
1073 LogRelFunc(("EnumComponents error (0x%x)\n", hr));
1074#endif /* # if defined VBOX_WITH_NETFLT */
1075 return VINF_SUCCESS;
1076}
1077
1078int NetIfGetConfig(HostNetworkInterface * pIf, NETIFINFO *pInfo)
1079{
1080#ifndef VBOX_WITH_NETFLT
1081 return VERR_NOT_IMPLEMENTED;
1082#else
1083 Bstr name;
1084 HRESULT hr = pIf->COMGETTER(Name)(name.asOutParam());
1085 if (hr == S_OK)
1086 {
1087 Bstr IfGuid;
1088 hr = pIf->COMGETTER(Id)(IfGuid.asOutParam());
1089 Assert(hr == S_OK);
1090 if (hr == S_OK)
1091 {
1092 memset(pInfo, 0, sizeof(NETIFINFO));
1093 Guid guid(IfGuid);
1094 pInfo->Uuid = *(guid.raw());
1095
1096 return collectNetIfInfo(name, guid, pInfo, getDefaultInterfaceIndex());
1097 }
1098 }
1099 return VERR_GENERAL_FAILURE;
1100#endif
1101}
1102
1103int NetIfGetConfigByName(PNETIFINFO)
1104{
1105 return VERR_NOT_IMPLEMENTED;
1106}
1107
1108/**
1109 * Obtain the current state of the interface.
1110 *
1111 * @returns VBox status code.
1112 *
1113 * @param pcszIfName Interface name.
1114 * @param penmState Where to store the retrieved state.
1115 */
1116int NetIfGetState(const char *pcszIfName, NETIFSTATUS *penmState)
1117{
1118 RT_NOREF(pcszIfName, penmState);
1119 return VERR_NOT_IMPLEMENTED;
1120}
1121
1122/**
1123 * Retrieve the physical link speed in megabits per second. If the interface is
1124 * not up or otherwise unavailable the zero speed is returned.
1125 *
1126 * @returns VBox status code.
1127 *
1128 * @param pcszIfName Interface name.
1129 * @param puMbits Where to store the link speed.
1130 */
1131int NetIfGetLinkSpeed(const char *pcszIfName, uint32_t *puMbits)
1132{
1133 RT_NOREF(pcszIfName, puMbits);
1134 return VERR_NOT_IMPLEMENTED;
1135}
1136
1137int NetIfCreateHostOnlyNetworkInterface(VirtualBox *pVirtualBox,
1138 IHostNetworkInterface **aHostNetworkInterface,
1139 IProgress **aProgress,
1140 const char *pszName)
1141{
1142 RT_NOREF(pszName);
1143#ifndef VBOX_WITH_NETFLT
1144 return VERR_NOT_IMPLEMENTED;
1145#else
1146 /* create a progress object */
1147 ComObjPtr<Progress> progress;
1148 progress.createObject();
1149
1150 ComPtr<IHost> host;
1151 HRESULT rc = pVirtualBox->COMGETTER(Host)(host.asOutParam());
1152 if (SUCCEEDED(rc))
1153 {
1154 rc = progress->init(pVirtualBox, host,
1155 Bstr(_T("Creating host only network interface")).raw(),
1156 FALSE /* aCancelable */);
1157 if (SUCCEEDED(rc))
1158 {
1159 if (FAILED(rc)) return rc;
1160 progress.queryInterfaceTo(aProgress);
1161
1162 /* create a new uninitialized host interface object */
1163 ComObjPtr<HostNetworkInterface> iface;
1164 iface.createObject();
1165 iface.queryInterfaceTo(aHostNetworkInterface);
1166
1167 /* create the networkInterfaceHelperClient() argument */
1168 NetworkInterfaceHelperClientData* d = new NetworkInterfaceHelperClientData();
1169
1170 d->msgCode = SVCHlpMsg::CreateHostOnlyNetworkInterface;
1171// d->name = aName;
1172 d->iface = iface;
1173 d->vBox = pVirtualBox;
1174
1175 rc = pVirtualBox->i_startSVCHelperClient(IsUACEnabled() == TRUE /* aPrivileged */,
1176 netIfNetworkInterfaceHelperClient,
1177 static_cast<void *>(d),
1178 progress);
1179 /* d is now owned by netIfNetworkInterfaceHelperClient(), no need to delete one here */
1180
1181 }
1182 }
1183
1184 return SUCCEEDED(rc) ? VINF_SUCCESS : VERR_GENERAL_FAILURE;
1185#endif
1186}
1187
1188int NetIfRemoveHostOnlyNetworkInterface(VirtualBox *pVirtualBox, IN_GUID aId,
1189 IProgress **aProgress)
1190{
1191#ifndef VBOX_WITH_NETFLT
1192 return VERR_NOT_IMPLEMENTED;
1193#else
1194 /* create a progress object */
1195 ComObjPtr<Progress> progress;
1196 progress.createObject();
1197 ComPtr<IHost> host;
1198 HRESULT rc = pVirtualBox->COMGETTER(Host)(host.asOutParam());
1199 if (SUCCEEDED(rc))
1200 {
1201 rc = progress->init(pVirtualBox, host,
1202 Bstr(_T("Removing host network interface")).raw(),
1203 FALSE /* aCancelable */);
1204 if (SUCCEEDED(rc))
1205 {
1206 if (FAILED(rc)) return rc;
1207 progress.queryInterfaceTo(aProgress);
1208
1209 /* create the networkInterfaceHelperClient() argument */
1210 NetworkInterfaceHelperClientData* d = new NetworkInterfaceHelperClientData();
1211
1212 d->msgCode = SVCHlpMsg::RemoveHostOnlyNetworkInterface;
1213 d->guid = aId;
1214
1215 rc = pVirtualBox->i_startSVCHelperClient(IsUACEnabled() == TRUE /* aPrivileged */,
1216 netIfNetworkInterfaceHelperClient,
1217 static_cast<void *>(d),
1218 progress);
1219 /* d is now owned by netIfNetworkInterfaceHelperClient(), no need to delete one here */
1220
1221 }
1222 }
1223
1224 return SUCCEEDED(rc) ? VINF_SUCCESS : VERR_GENERAL_FAILURE;
1225#endif
1226}
1227
1228int NetIfEnableStaticIpConfig(VirtualBox *vBox, HostNetworkInterface * pIf, ULONG aOldIp, ULONG ip, ULONG mask)
1229{
1230 RT_NOREF(aOldIp);
1231#ifndef VBOX_WITH_NETFLT
1232 return VERR_NOT_IMPLEMENTED;
1233#else
1234 Bstr guid;
1235 HRESULT rc = pIf->COMGETTER(Id)(guid.asOutParam());
1236 if (SUCCEEDED(rc))
1237 {
1238// ComPtr<VirtualBox> vBox;
1239// rc = pIf->getVirtualBox(vBox.asOutParam());
1240// if (SUCCEEDED(rc))
1241 {
1242 /* create a progress object */
1243 ComObjPtr<Progress> progress;
1244 progress.createObject();
1245// ComPtr<IHost> host;
1246// HRESULT rc = vBox->COMGETTER(Host)(host.asOutParam());
1247// if (SUCCEEDED(rc))
1248 {
1249 rc = progress->init(vBox, (IHostNetworkInterface*)pIf,
1250 Bstr("Enabling Dynamic Ip Configuration").raw(),
1251 FALSE /* aCancelable */);
1252 if (SUCCEEDED(rc))
1253 {
1254 if (FAILED(rc)) return rc;
1255// progress.queryInterfaceTo(aProgress);
1256
1257 /* create the networkInterfaceHelperClient() argument */
1258 NetworkInterfaceHelperClientData* d = new NetworkInterfaceHelperClientData();
1259
1260 d->msgCode = SVCHlpMsg::EnableStaticIpConfig;
1261 d->guid = Guid(guid);
1262 d->iface = pIf;
1263 d->u.StaticIP.IPAddress = ip;
1264 d->u.StaticIP.IPNetMask = mask;
1265
1266 rc = vBox->i_startSVCHelperClient(IsUACEnabled() == TRUE /* aPrivileged */,
1267 netIfNetworkInterfaceHelperClient,
1268 static_cast<void *>(d),
1269 progress);
1270 /* d is now owned by netIfNetworkInterfaceHelperClient(), no need to delete one here */
1271
1272 if (SUCCEEDED(rc))
1273 {
1274 progress->WaitForCompletion(-1);
1275 }
1276 }
1277 }
1278 }
1279 }
1280
1281 return SUCCEEDED(rc) ? VINF_SUCCESS : VERR_GENERAL_FAILURE;
1282#endif
1283}
1284
1285int NetIfEnableStaticIpConfigV6(VirtualBox *vBox, HostNetworkInterface * pIf, IN_BSTR aOldIPV6Address,
1286 IN_BSTR aIPV6Address, ULONG aIPV6MaskPrefixLength)
1287{
1288 RT_NOREF(aOldIPV6Address);
1289#ifndef VBOX_WITH_NETFLT
1290 return VERR_NOT_IMPLEMENTED;
1291#else
1292 Bstr guid;
1293 HRESULT rc = pIf->COMGETTER(Id)(guid.asOutParam());
1294 if (SUCCEEDED(rc))
1295 {
1296// ComPtr<VirtualBox> vBox;
1297// rc = pIf->getVirtualBox(vBox.asOutParam());
1298// if (SUCCEEDED(rc))
1299 {
1300 /* create a progress object */
1301 ComObjPtr<Progress> progress;
1302 progress.createObject();
1303// ComPtr<IHost> host;
1304// HRESULT rc = vBox->COMGETTER(Host)(host.asOutParam());
1305// if (SUCCEEDED(rc))
1306 {
1307 rc = progress->init(vBox, (IHostNetworkInterface*)pIf,
1308 Bstr("Enabling Dynamic Ip Configuration").raw(),
1309 FALSE /* aCancelable */);
1310 if (SUCCEEDED(rc))
1311 {
1312 if (FAILED(rc)) return rc;
1313// progress.queryInterfaceTo(aProgress);
1314
1315 /* create the networkInterfaceHelperClient() argument */
1316 NetworkInterfaceHelperClientData* d = new NetworkInterfaceHelperClientData();
1317
1318 d->msgCode = SVCHlpMsg::EnableStaticIpConfigV6;
1319 d->guid = guid;
1320 d->iface = pIf;
1321 d->u.StaticIPV6.IPV6Address = aIPV6Address;
1322 d->u.StaticIPV6.IPV6NetMaskLength = aIPV6MaskPrefixLength;
1323
1324 rc = vBox->i_startSVCHelperClient(IsUACEnabled() == TRUE /* aPrivileged */,
1325 netIfNetworkInterfaceHelperClient,
1326 static_cast<void *>(d),
1327 progress);
1328 /* d is now owned by netIfNetworkInterfaceHelperClient(), no need to delete one here */
1329
1330 if (SUCCEEDED(rc))
1331 {
1332 progress->WaitForCompletion(-1);
1333 }
1334 }
1335 }
1336 }
1337 }
1338
1339 return SUCCEEDED(rc) ? VINF_SUCCESS : VERR_GENERAL_FAILURE;
1340#endif
1341}
1342
1343int NetIfEnableDynamicIpConfig(VirtualBox *vBox, HostNetworkInterface * pIf)
1344{
1345#ifndef VBOX_WITH_NETFLT
1346 return VERR_NOT_IMPLEMENTED;
1347#else
1348 HRESULT rc;
1349 Bstr guid;
1350 rc = pIf->COMGETTER(Id)(guid.asOutParam());
1351 if (SUCCEEDED(rc))
1352 {
1353// ComPtr<VirtualBox> vBox;
1354// rc = pIf->getVirtualBox(vBox.asOutParam());
1355// if (SUCCEEDED(rc))
1356 {
1357 /* create a progress object */
1358 ComObjPtr<Progress> progress;
1359 progress.createObject();
1360// ComPtr<IHost> host;
1361// HRESULT rc = vBox->COMGETTER(Host)(host.asOutParam());
1362// if (SUCCEEDED(rc))
1363 {
1364 rc = progress->init(vBox, (IHostNetworkInterface*)pIf,
1365 Bstr("Enabling Dynamic Ip Configuration").raw(),
1366 FALSE /* aCancelable */);
1367 if (SUCCEEDED(rc))
1368 {
1369 if (FAILED(rc)) return rc;
1370// progress.queryInterfaceTo(aProgress);
1371
1372 /* create the networkInterfaceHelperClient() argument */
1373 NetworkInterfaceHelperClientData* d = new NetworkInterfaceHelperClientData();
1374
1375 d->msgCode = SVCHlpMsg::EnableDynamicIpConfig;
1376 d->guid = guid;
1377 d->iface = pIf;
1378
1379 rc = vBox->i_startSVCHelperClient(IsUACEnabled() == TRUE /* aPrivileged */,
1380 netIfNetworkInterfaceHelperClient,
1381 static_cast<void *>(d),
1382 progress);
1383 /* d is now owned by netIfNetworkInterfaceHelperClient(), no need to delete one here */
1384
1385 if (SUCCEEDED(rc))
1386 {
1387 progress->WaitForCompletion(-1);
1388 }
1389 }
1390 }
1391 }
1392 }
1393
1394 return SUCCEEDED(rc) ? VINF_SUCCESS : VERR_GENERAL_FAILURE;
1395#endif
1396}
1397
1398int NetIfDhcpRediscover(VirtualBox *vBox, HostNetworkInterface * pIf)
1399{
1400#ifndef VBOX_WITH_NETFLT
1401 return VERR_NOT_IMPLEMENTED;
1402#else
1403 HRESULT rc;
1404 Bstr guid;
1405 rc = pIf->COMGETTER(Id)(guid.asOutParam());
1406 if (SUCCEEDED(rc))
1407 {
1408// ComPtr<VirtualBox> vBox;
1409// rc = pIf->getVirtualBox(vBox.asOutParam());
1410// if (SUCCEEDED(rc))
1411 {
1412 /* create a progress object */
1413 ComObjPtr<Progress> progress;
1414 progress.createObject();
1415// ComPtr<IHost> host;
1416// HRESULT rc = vBox->COMGETTER(Host)(host.asOutParam());
1417// if (SUCCEEDED(rc))
1418 {
1419 rc = progress->init(vBox, (IHostNetworkInterface*)pIf,
1420 Bstr("Enabling Dynamic Ip Configuration").raw(),
1421 FALSE /* aCancelable */);
1422 if (SUCCEEDED(rc))
1423 {
1424 if (FAILED(rc)) return rc;
1425// progress.queryInterfaceTo(aProgress);
1426
1427 /* create the networkInterfaceHelperClient() argument */
1428 NetworkInterfaceHelperClientData* d = new NetworkInterfaceHelperClientData();
1429
1430 d->msgCode = SVCHlpMsg::DhcpRediscover;
1431 d->guid = guid;
1432 d->iface = pIf;
1433
1434 rc = vBox->i_startSVCHelperClient(IsUACEnabled() == TRUE /* aPrivileged */,
1435 netIfNetworkInterfaceHelperClient,
1436 static_cast<void *>(d),
1437 progress);
1438 /* d is now owned by netIfNetworkInterfaceHelperClient(), no need to delete one here */
1439
1440 if (SUCCEEDED(rc))
1441 {
1442 progress->WaitForCompletion(-1);
1443 }
1444 }
1445 }
1446 }
1447 }
1448
1449 return SUCCEEDED(rc) ? VINF_SUCCESS : VERR_GENERAL_FAILURE;
1450#endif
1451}
1452
1453
1454#define netIfLog LogFunc
1455
1456struct BoundAdapter
1457{
1458 LPWSTR pName;
1459 LPWSTR pHwId;
1460 RTUUID guid;
1461 PIP_ADAPTER_ADDRESSES pAdapter;
1462};
1463
1464static int netIfGetUnboundHostOnlyAdapters(INetCfg *pNetCfg, std::list<BoundAdapter> &adapters)
1465{
1466 INetCfgComponent *pMiniport;
1467 HRESULT hr;
1468 IEnumNetCfgComponent *pEnumComponent;
1469
1470 if ((hr = pNetCfg->EnumComponents(&GUID_DEVCLASS_NET, &pEnumComponent)) != S_OK)
1471 LogRelFunc(("failed to enumerate network adapter components (0x%x)\n", hr));
1472 else
1473 {
1474 while ((hr = pEnumComponent->Next(1, &pMiniport, NULL)) == S_OK)
1475 {
1476 GUID guid;
1477 ULONG uComponentStatus;
1478 struct BoundAdapter adapter;
1479 memset(&adapter, 0, sizeof(adapter));
1480 if ((hr = pMiniport->GetDisplayName(&adapter.pName)) != S_OK)
1481 LogRelFunc(("failed to get device display name (0x%x)\n", hr));
1482 else if ((hr = pMiniport->GetDeviceStatus(&uComponentStatus)) != S_OK)
1483 netIfLog(("failed to get device status (0x%x)\n", hr));
1484 else if (uComponentStatus != 0)
1485 netIfLog(("wrong device status (0x%x)\n", uComponentStatus));
1486 else if ((hr = pMiniport->GetId(&adapter.pHwId)) != S_OK)
1487 LogRelFunc(("failed to get device id (0x%x)\n", hr));
1488 else if (_wcsnicmp(adapter.pHwId, L"sun_VBoxNetAdp", sizeof(L"sun_VBoxNetAdp")/2))
1489 netIfLog(("not host-only id = %ls, ignored\n", adapter.pHwId));
1490 else if ((hr = pMiniport->GetInstanceGuid(&guid)) != S_OK)
1491 LogRelFunc(("failed to get instance id (0x%x)\n", hr));
1492 else
1493 {
1494 adapter.guid = *(Guid(guid).raw());
1495 netIfLog(("guid=%RTuuid, name=%ls id = %ls\n", &adapter.guid, adapter.pName, adapter.pHwId));
1496 adapters.push_back(adapter);
1497 adapter.pName = adapter.pHwId = NULL; /* do not free, will be done later */
1498 }
1499 if (adapter.pHwId)
1500 CoTaskMemFree(adapter.pHwId);
1501 if (adapter.pName)
1502 CoTaskMemFree(adapter.pName);
1503 pMiniport->Release();
1504 }
1505 Assert(hr == S_OK || hr == S_FALSE);
1506
1507 pEnumComponent->Release();
1508 }
1509 netIfLog(("return\n"));
1510 return VINF_SUCCESS;
1511}
1512
1513static HRESULT netIfGetBoundAdapters(std::list<BoundAdapter> &boundAdapters)
1514{
1515 INetCfg *pNetCfg = NULL;
1516 INetCfgComponent *pFilter;
1517 LPWSTR lpszApp;
1518 HRESULT hr;
1519
1520 netIfLog(("building the list of interfaces\n"));
1521 /* we are using the INetCfg API for getting the list of miniports */
1522 hr = VBoxNetCfgWinQueryINetCfg(&pNetCfg, FALSE,
1523 VBOX_APP_NAME,
1524 10000,
1525 &lpszApp);
1526 Assert(hr == S_OK);
1527 if (hr != S_OK)
1528 {
1529 LogRelFunc(("failed to query INetCfg (0x%x)\n", hr));
1530 return hr;
1531 }
1532
1533 if ((hr = pNetCfg->FindComponent(L"oracle_VBoxNetLwf", &pFilter)) != S_OK
1534 /* fall back to NDIS5 miniport lookup */
1535 && (hr = pNetCfg->FindComponent(L"sun_VBoxNetFlt", &pFilter)))
1536 LogRelFunc(("could not find either 'oracle_VBoxNetLwf' or 'sun_VBoxNetFlt' components (0x%x)\n", hr));
1537 else
1538 {
1539 INetCfgComponentBindings *pFilterBindings;
1540 if ((pFilter->QueryInterface(IID_INetCfgComponentBindings, (PVOID*)&pFilterBindings)) != S_OK)
1541 LogRelFunc(("failed to query INetCfgComponentBindings (0x%x)\n", hr));
1542 else
1543 {
1544 IEnumNetCfgBindingPath *pEnumBp;
1545 INetCfgBindingPath *pBp;
1546 if ((pFilterBindings->EnumBindingPaths(EBP_BELOW, &pEnumBp)) != S_OK)
1547 LogRelFunc(("failed to enumerate binding paths (0x%x)\n", hr));
1548 else
1549 {
1550 pEnumBp->Reset();
1551 while ((hr = pEnumBp->Next(1, &pBp, NULL)) == S_OK)
1552 {
1553 IEnumNetCfgBindingInterface *pEnumBi;
1554 INetCfgBindingInterface *pBi;
1555 if (pBp->IsEnabled() != S_OK)
1556 {
1557 /** @todo some id of disabled path could be useful. */
1558 netIfLog(("INetCfgBindingPath is disabled (0x%x)\n", hr));
1559 pBp->Release();
1560 continue;
1561 }
1562 if ((pBp->EnumBindingInterfaces(&pEnumBi)) != S_OK)
1563 LogRelFunc(("failed to enumerate binding interfaces (0x%x)\n", hr));
1564 else
1565 {
1566 hr = pEnumBi->Reset();
1567 while ((hr = pEnumBi->Next(1, &pBi, NULL)) == S_OK)
1568 {
1569 INetCfgComponent *pAdapter;
1570 if ((hr = pBi->GetLowerComponent(&pAdapter)) != S_OK)
1571 LogRelFunc(("failed to get lower component (0x%x)\n", hr));
1572 else
1573 {
1574 LPWSTR pwszName = NULL;
1575 if ((hr = pAdapter->GetDisplayName(&pwszName)) != S_OK)
1576 LogRelFunc(("failed to get display name (0x%x)\n", hr));
1577 else
1578 {
1579 ULONG uStatus;
1580 DWORD dwChars;
1581 if ((hr = pAdapter->GetDeviceStatus(&uStatus)) != S_OK)
1582 netIfLog(("%ls: failed to get device status (0x%x)\n",
1583 pwszName, hr));
1584 else if ((hr = pAdapter->GetCharacteristics(&dwChars)) != S_OK)
1585 netIfLog(("%ls: failed to get device characteristics (0x%x)\n",
1586 pwszName, hr));
1587 else if (uStatus != 0)
1588 netIfLog(("%ls: wrong status 0x%x\n",
1589 pwszName, uStatus));
1590 else if (dwChars & NCF_HIDDEN)
1591 netIfLog(("%ls: wrong characteristics 0x%x\n",
1592 pwszName, dwChars));
1593 else
1594 {
1595 GUID guid;
1596 LPWSTR pwszHwId = NULL;
1597 if ((hr = pAdapter->GetId(&pwszHwId)) != S_OK)
1598 LogRelFunc(("%ls: failed to get hardware id (0x%x)\n",
1599 pwszName, hr));
1600 else if (!_wcsnicmp(pwszHwId, L"sun_VBoxNetAdp", sizeof(L"sun_VBoxNetAdp")/2))
1601 netIfLog(("host-only adapter %ls, ignored\n", pwszName));
1602 else if ((hr = pAdapter->GetInstanceGuid(&guid)) != S_OK)
1603 LogRelFunc(("%ls: failed to get instance GUID (0x%x)\n",
1604 pwszName, hr));
1605 else
1606 {
1607 struct BoundAdapter adapter;
1608 adapter.pName = pwszName;
1609 adapter.pHwId = pwszHwId;
1610 adapter.guid = *(Guid(guid).raw());
1611 adapter.pAdapter = NULL;
1612 netIfLog(("guid=%RTuuid, name=%ls, hwid=%ls, status=%x, chars=%x\n",
1613 &adapter.guid, pwszName, pwszHwId, uStatus, dwChars));
1614 boundAdapters.push_back(adapter);
1615 pwszName = pwszHwId = NULL; /* do not free, will be done later */
1616 }
1617 if (pwszHwId)
1618 CoTaskMemFree(pwszHwId);
1619 }
1620 if (pwszName)
1621 CoTaskMemFree(pwszName);
1622 }
1623
1624 pAdapter->Release();
1625 }
1626 pBi->Release();
1627 }
1628 pEnumBi->Release();
1629 }
1630 pBp->Release();
1631 }
1632 pEnumBp->Release();
1633 }
1634 pFilterBindings->Release();
1635 }
1636 pFilter->Release();
1637 }
1638 /* Host-only adapters are not necessarily bound, add them separately. */
1639 netIfGetUnboundHostOnlyAdapters(pNetCfg, boundAdapters);
1640 VBoxNetCfgWinReleaseINetCfg(pNetCfg, FALSE);
1641
1642 return S_OK;
1643}
1644
1645#if 0
1646static HRESULT netIfGetBoundAdaptersFallback(std::list<BoundAdapter> &boundAdapters)
1647{
1648 return CO_E_NOT_SUPPORTED;
1649}
1650#endif
1651
1652/**
1653 * Walk through the list of adpater addresses and extract the required
1654 * information. XP and older don't not have the OnLinkPrefixLength field.
1655 */
1656static void netIfFillInfoWithAddressesXp(PNETIFINFO pInfo, PIP_ADAPTER_ADDRESSES pAdapter)
1657{
1658 PIP_ADAPTER_UNICAST_ADDRESS pAddr;
1659 bool fIPFound = false;
1660 bool fIPv6Found = false;
1661 for (pAddr = pAdapter->FirstUnicastAddress; pAddr; pAddr = pAddr->Next)
1662 {
1663 switch (pAddr->Address.lpSockaddr->sa_family)
1664 {
1665 case AF_INET:
1666 if (!fIPFound)
1667 {
1668 fIPFound = true;
1669 memcpy(&pInfo->IPAddress,
1670 &((struct sockaddr_in *)pAddr->Address.lpSockaddr)->sin_addr.s_addr,
1671 sizeof(pInfo->IPAddress));
1672 }
1673 break;
1674 case AF_INET6:
1675 if (!fIPv6Found)
1676 {
1677 fIPv6Found = true;
1678 memcpy(&pInfo->IPv6Address,
1679 ((struct sockaddr_in6 *)pAddr->Address.lpSockaddr)->sin6_addr.s6_addr,
1680 sizeof(pInfo->IPv6Address));
1681 }
1682 break;
1683 }
1684 }
1685 PIP_ADAPTER_PREFIX pPrefix;
1686 ULONG uPrefixLenV4 = 0;
1687 ULONG uPrefixLenV6 = 0;
1688 for (pPrefix = pAdapter->FirstPrefix; pPrefix && !(uPrefixLenV4 && uPrefixLenV6); pPrefix = pPrefix->Next)
1689 {
1690 switch (pPrefix->Address.lpSockaddr->sa_family)
1691 {
1692 case AF_INET:
1693 if (!uPrefixLenV4)
1694 {
1695 ULONG ip = ((PSOCKADDR_IN)(pPrefix->Address.lpSockaddr))->sin_addr.s_addr;
1696 netIfLog(("prefix=%RTnaipv4 len=%u\n", ip, pPrefix->PrefixLength));
1697 if ( pPrefix->PrefixLength < sizeof(pInfo->IPNetMask) * 8
1698 && pPrefix->PrefixLength > 0
1699 && (ip & 0xF0) < 224)
1700 {
1701 uPrefixLenV4 = pPrefix->PrefixLength;
1702 RTNetPrefixToMaskIPv4(pPrefix->PrefixLength, &pInfo->IPNetMask);
1703 }
1704 else
1705 netIfLog(("Unexpected IPv4 prefix length of %d\n",
1706 pPrefix->PrefixLength));
1707 }
1708 break;
1709 case AF_INET6:
1710 if (!uPrefixLenV6)
1711 {
1712 PBYTE ipv6 = ((PSOCKADDR_IN6)(pPrefix->Address.lpSockaddr))->sin6_addr.s6_addr;
1713 netIfLog(("prefix=%RTnaipv6 len=%u\n", ipv6, pPrefix->PrefixLength));
1714 if ( pPrefix->PrefixLength < sizeof(pInfo->IPv6NetMask) * 8
1715 && pPrefix->PrefixLength > 0
1716 && ipv6[0] != 0xFF)
1717 {
1718 uPrefixLenV6 = pPrefix->PrefixLength;
1719 RTNetPrefixToMaskIPv6(pPrefix->PrefixLength, &pInfo->IPv6NetMask);
1720 }
1721 else
1722 netIfLog(("Unexpected IPv6 prefix length of %d\n", pPrefix->PrefixLength));
1723 }
1724 break;
1725 }
1726 }
1727 netIfLog(("%RTnaipv4/%u\n", pInfo->IPAddress, uPrefixLenV4));
1728 netIfLog(("%RTnaipv6/%u\n", &pInfo->IPv6Address, uPrefixLenV6));
1729}
1730
1731/**
1732 * Walk through the list of adpater addresses and extract the required
1733 * information. XP and older don't not have the OnLinkPrefixLength field.
1734 */
1735static void netIfFillInfoWithAddressesVista(PNETIFINFO pInfo, PIP_ADAPTER_ADDRESSES pAdapter)
1736{
1737 PIP_ADAPTER_UNICAST_ADDRESS pAddr;
1738
1739 if (sizeof(pInfo->MACAddress) != pAdapter->PhysicalAddressLength)
1740 netIfLog(("Unexpected physical address length: %u\n", pAdapter->PhysicalAddressLength));
1741 else
1742 memcpy(pInfo->MACAddress.au8, pAdapter->PhysicalAddress, sizeof(pInfo->MACAddress));
1743
1744 bool fIPFound = false;
1745 bool fIPv6Found = false;
1746 for (pAddr = pAdapter->FirstUnicastAddress; pAddr; pAddr = pAddr->Next)
1747 {
1748 PIP_ADAPTER_UNICAST_ADDRESS_LH pAddrLh = (PIP_ADAPTER_UNICAST_ADDRESS_LH)pAddr;
1749 switch (pAddrLh->Address.lpSockaddr->sa_family)
1750 {
1751 case AF_INET:
1752 if (!fIPFound)
1753 {
1754 fIPFound = true;
1755 memcpy(&pInfo->IPAddress,
1756 &((struct sockaddr_in *)pAddrLh->Address.lpSockaddr)->sin_addr.s_addr,
1757 sizeof(pInfo->IPAddress));
1758 if (pAddrLh->OnLinkPrefixLength > 32)
1759 netIfLog(("Invalid IPv4 prefix length of %d\n", pAddrLh->OnLinkPrefixLength));
1760 else
1761 RTNetPrefixToMaskIPv4(pAddrLh->OnLinkPrefixLength, &pInfo->IPNetMask);
1762 }
1763 break;
1764 case AF_INET6:
1765 if (!fIPv6Found)
1766 {
1767 fIPv6Found = true;
1768 memcpy(&pInfo->IPv6Address,
1769 ((struct sockaddr_in6 *)pAddrLh->Address.lpSockaddr)->sin6_addr.s6_addr,
1770 sizeof(pInfo->IPv6Address));
1771 if (pAddrLh->OnLinkPrefixLength > 128)
1772 netIfLog(("Invalid IPv6 prefix length of %d\n", pAddrLh->OnLinkPrefixLength));
1773 else
1774 RTNetPrefixToMaskIPv6(pAddrLh->OnLinkPrefixLength, &pInfo->IPv6NetMask);
1775 }
1776 break;
1777 }
1778 }
1779
1780 if (fIPFound)
1781 {
1782 int iPrefixIPv4 = -1;
1783 RTNetMaskToPrefixIPv4(&pInfo->IPNetMask, &iPrefixIPv4);
1784 netIfLog(("%RTnaipv4/%u\n", pInfo->IPAddress, iPrefixIPv4));
1785 }
1786 if (fIPv6Found)
1787 {
1788 int iPrefixIPv6 = -1;
1789 RTNetMaskToPrefixIPv6(&pInfo->IPv6NetMask, &iPrefixIPv6);
1790 netIfLog(("%RTnaipv6/%u\n", &pInfo->IPv6Address, iPrefixIPv6));
1791 }
1792}
1793
1794#if (NTDDI_VERSION >= NTDDI_VISTA)
1795#define NETIF_GAA_FLAGS GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST
1796#else /* (NTDDI_VERSION < NTDDI_VISTA) */
1797#define NETIF_GAA_FLAGS GAA_FLAG_INCLUDE_PREFIX | GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST
1798#endif /* (NTDDI_VERSION < NTDDI_VISTA) */
1799
1800int NetIfList(std::list<ComObjPtr<HostNetworkInterface> > &list)
1801{
1802 HRESULT hr = S_OK;
1803 int iDefault = getDefaultInterfaceIndex();
1804 /* MSDN recommends to pre-allocate a 15KB buffer. */
1805 ULONG uBufLen = 15 * 1024;
1806 PIP_ADAPTER_ADDRESSES pAddresses = (PIP_ADAPTER_ADDRESSES)RTMemAlloc(uBufLen);
1807 if (!pAddresses)
1808 return HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
1809 DWORD dwRc = GetAdaptersAddresses(AF_UNSPEC, NETIF_GAA_FLAGS, NULL, pAddresses, &uBufLen);
1810 for (int tries = 0; tries < 3 && dwRc == ERROR_BUFFER_OVERFLOW; ++tries)
1811 {
1812 /* Get more memory and try again. */
1813 RTMemFree(pAddresses);
1814 pAddresses = (PIP_ADAPTER_ADDRESSES)RTMemAlloc(uBufLen);
1815 if (!pAddresses)
1816 return HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
1817 dwRc = GetAdaptersAddresses(AF_UNSPEC, NETIF_GAA_FLAGS, NULL, pAddresses, &uBufLen);
1818 }
1819 if (dwRc != NO_ERROR)
1820 {
1821 LogRelFunc(("GetAdaptersAddresses failed (0x%x)\n", dwRc));
1822 hr = HRESULT_FROM_WIN32(dwRc);
1823 }
1824 else
1825 {
1826 std::list<BoundAdapter> boundAdapters;
1827 HRESULT hr = netIfGetBoundAdapters(boundAdapters);
1828#if 0
1829 if (hr != S_OK)
1830 hr = netIfGetBoundAdaptersFallback(boundAdapters);
1831#endif
1832 if (hr != S_OK)
1833 LogRelFunc(("netIfGetBoundAdapters failed (0x%x)\n", hr));
1834 else
1835 {
1836 PIP_ADAPTER_ADDRESSES pAdapter;
1837
1838 for (pAdapter = pAddresses; pAdapter; pAdapter = pAdapter->Next)
1839 {
1840 char *pszUuid = RTStrDup(pAdapter->AdapterName);
1841 if (!pszUuid)
1842 {
1843 LogRelFunc(("out of memory\n"));
1844 break;
1845 }
1846 size_t len = strlen(pszUuid) - 1;
1847 if (pszUuid[0] != '{' || pszUuid[len] != '}')
1848 LogRelFunc(("ignoring invalid GUID %s\n", pAdapter->AdapterName));
1849 else
1850 {
1851 std::list<BoundAdapter>::iterator it;
1852 pszUuid[len] = 0;
1853 for (it = boundAdapters.begin(); it != boundAdapters.end(); ++it)
1854 {
1855 if (!RTUuidCompareStr(&(*it).guid, pszUuid + 1))
1856 {
1857 (*it).pAdapter = pAdapter;
1858 break;
1859 }
1860 }
1861 }
1862 RTStrFree(pszUuid);
1863 }
1864 std::list<BoundAdapter>::iterator it;
1865 for (it = boundAdapters.begin(); it != boundAdapters.end(); ++it)
1866 {
1867 NETIFINFO info;
1868 memset(&info, 0, sizeof(info));
1869 info.Uuid = (*it).guid;
1870 info.enmMediumType = NETIF_T_ETHERNET;
1871 pAdapter = (*it).pAdapter;
1872 if (pAdapter)
1873 {
1874 info.enmStatus = pAdapter->OperStatus == IfOperStatusUp ? NETIF_S_UP : NETIF_S_DOWN;
1875 info.bIsDefault = (pAdapter->IfIndex == (DWORD)iDefault);
1876 info.bDhcpEnabled = pAdapter->Flags & IP_ADAPTER_DHCP_ENABLED;
1877 OSVERSIONINFOEX OSInfoEx;
1878 RT_ZERO(OSInfoEx);
1879 OSInfoEx.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
1880 if ( GetVersionEx((LPOSVERSIONINFO)&OSInfoEx)
1881 && OSInfoEx.dwMajorVersion < 6)
1882 netIfFillInfoWithAddressesXp(&info, pAdapter);
1883 else
1884 netIfFillInfoWithAddressesVista(&info, pAdapter);
1885 }
1886 else
1887 info.enmStatus = NETIF_S_DOWN;
1888 /* create a new object and add it to the list */
1889 ComObjPtr<HostNetworkInterface> iface;
1890 iface.createObject();
1891 HostNetworkInterfaceType enmType =
1892 _wcsnicmp((*it).pHwId, L"sun_VBoxNetAdp", sizeof(L"sun_VBoxNetAdp")/2) ?
1893 HostNetworkInterfaceType_Bridged : HostNetworkInterfaceType_HostOnly;
1894 netIfLog(("Adding %ls as %s\n", (*it).pName,
1895 enmType == HostNetworkInterfaceType_Bridged ? "bridged" :
1896 enmType == HostNetworkInterfaceType_HostOnly ? "host-only" : "unknown"));
1897 int rc = iface->init((*it).pName, enmType, &info);
1898 if (FAILED(rc))
1899 LogRelFunc(("HostNetworkInterface::init() -> %Rrc\n", rc));
1900 else
1901 {
1902 if (info.bIsDefault)
1903 list.push_front(iface);
1904 else
1905 list.push_back(iface);
1906 }
1907 if ((*it).pHwId)
1908 CoTaskMemFree((*it).pHwId);
1909 if ((*it).pName)
1910 CoTaskMemFree((*it).pName);
1911 }
1912 }
1913 }
1914 RTMemFree(pAddresses);
1915
1916 return hr;
1917}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette