VirtualBox

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

Last change on this file since 61888 was 60509, checked in by vboxsync, 9 years ago

Main/Console+Host: winsock2 include fixing

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