VirtualBox

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

Last change on this file since 98110 was 98103, checked in by vboxsync, 23 months ago

Copyright year updates by scm.

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