VirtualBox

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

Last change on this file since 98292 was 98292, checked in by vboxsync, 2 years ago

Main/src-server: rc -> hrc/vrc. Enabled scm rc checks. bugref:10223

  • 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 98292 2023-01-25 01:14:53Z 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 hrc = 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 hrc = d->iface->init(Bstr(name), Bstr(name), guid, HostNetworkInterfaceType_HostOnly);
346 if (SUCCEEDED(hrc))
347 {
348 hrc = d->iface->i_setVirtualBox(d->ptrVBox);
349 if (SUCCEEDED(hrc))
350 {
351 hrc = d->iface->updateConfig();
352 if (SUCCEEDED(hrc))
353 hrc = 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 hrc = E_FAIL;
367 d->iface->setError(E_FAIL, errMsg.c_str());
368 endLoop = true;
369 break;
370 }
371 default:
372 {
373 endLoop = true;
374 hrc = E_FAIL;/// @todo ComAssertMsgFailedBreak((
375 //"Invalid message code %d (%08lX)\n",
376 //reply, reply),
377 //hrc = 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 hrc = 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 hrc = E_FAIL;
421 d->iface->setError(E_FAIL, errMsg.c_str());
422 endLoop = true;
423 break;
424 }
425 default:
426 {
427 endLoop = true;
428 hrc = E_FAIL; /// @todo ComAssertMsgFailedBreak((
429 //"Invalid message code %d (%08lX)\n",
430 //reply, reply),
431 //hrc = 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 hrc = 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 hrc = E_FAIL;
475 d->iface->setError(E_FAIL, errMsg.c_str());
476 endLoop = true;
477 break;
478 }
479 default:
480 {
481 endLoop = true;
482 hrc = E_FAIL; /// @todo ComAssertMsgFailedBreak((
483 //"Invalid message code %d (%08lX)\n",
484 //reply, reply),
485 //hrc = 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 hrc = 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 hrc = E_FAIL;
533 d->iface->setError(E_FAIL, errMsg.c_str());
534 endLoop = true;
535 break;
536 }
537 default:
538 {
539 endLoop = true;
540 hrc = E_FAIL; /// @todo ComAssertMsgFailedBreak((
541 //"Invalid message code %d (%08lX)\n",
542 //reply, reply),
543 //hrc = 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 hrc = 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 hrc = E_FAIL;
591 d->iface->setError(E_FAIL, errMsg.c_str());
592 endLoop = true;
593 break;
594 }
595 default:
596 {
597 endLoop = true;
598 hrc = E_FAIL; /// @todo ComAssertMsgFailedBreak((
599 //"Invalid message code %d (%08lX)\n",
600 //reply, reply),
601 //hrc = 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 hrc = 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 hrc = E_FAIL;
645 d->iface->setError(E_FAIL, errMsg.c_str());
646 endLoop = true;
647 break;
648 }
649 default:
650 {
651 endLoop = true;
652 hrc = E_FAIL; /// @todo ComAssertMsgFailedBreak((
653 //"Invalid message code %d (%08lX)\n",
654 //reply, reply),
655 //hrc = E_FAIL);
656 }
657 }
658 }
659
660 break;
661 }
662 default:
663 hrc = E_FAIL; /// @todo ComAssertMsgFailedBreak((
664// "Invalid message code %d (%08lX)\n",
665// d->msgCode, d->msgCode),
666// hrc = E_FAIL);
667 }
668
669 if (aVrc)
670 *aVrc = vrc;
671
672 LogFlowFunc(("hrc=0x%08X, vrc=%Rrc\n", hrc, vrc));
673 LogFlowFuncLeave();
674 return hrc;
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 OSVERSIONINFOEX info;
943 ZeroMemory(&info, sizeof(OSVERSIONINFOEX));
944 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
945 BOOL frc = GetVersionEx((OSVERSIONINFO *) &info);
946 AssertReturn(frc != FALSE, FALSE);
947
948 LogFlowFunc(("dwMajorVersion=%d, dwMinorVersion=%d\n", info.dwMajorVersion, info.dwMinorVersion));
949
950 /* we are interested only in Vista (and newer versions...). In all
951 * earlier versions UAC is not present. */
952 if (info.dwMajorVersion < 6)
953 return FALSE;
954
955 /* the default EnableLUA value is 1 (Enabled) */
956 DWORD dwEnableLUA = 1;
957
958 HKEY hKey;
959 LSTATUS lrc = RegOpenKeyExA(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System",
960 0, KEY_QUERY_VALUE, &hKey);
961
962 Assert(lrc == ERROR_SUCCESS || lrc == ERROR_PATH_NOT_FOUND);
963 if (lrc == ERROR_SUCCESS)
964 {
965
966 DWORD cbEnableLUA = sizeof(dwEnableLUA);
967 lrc = RegQueryValueExA(hKey, "EnableLUA", NULL, NULL, (LPBYTE) &dwEnableLUA, &cbEnableLUA);
968
969 RegCloseKey(hKey);
970
971 Assert(lrc == ERROR_SUCCESS || lrc == ERROR_FILE_NOT_FOUND);
972 }
973
974 LogFlowFunc(("lrc=%d, dwEnableLUA=%d\n", lrc, dwEnableLUA));
975
976 return dwEnableLUA == 1;
977}
978
979/* end */
980
981static int vboxNetWinAddComponent(std::list<ComObjPtr<HostNetworkInterface> > * pPist,
982 INetCfgComponent * pncc, HostNetworkInterfaceType enmType,
983 int iDefaultInterface)
984{
985 LPWSTR lpszName;
986 GUID IfGuid;
987 HRESULT hr;
988 int vrc = VERR_GENERAL_FAILURE;
989
990 hr = pncc->GetDisplayName(&lpszName);
991 Assert(hr == S_OK);
992 if (hr == S_OK)
993 {
994 Bstr name(lpszName);
995
996 hr = pncc->GetInstanceGuid(&IfGuid);
997 Assert(hr == S_OK);
998 if (hr == S_OK)
999 {
1000 Guid guidIfCopy(IfGuid);
1001 NETIFINFO Info;
1002 RT_ZERO(Info);
1003 Info.Uuid = *guidIfCopy.raw();
1004 vrc = collectNetIfInfo(name, guidIfCopy, &Info, iDefaultInterface);
1005 if (RT_FAILURE(vrc))
1006 LogRelFunc(("collectNetIfInfo() -> %Rrc\n", vrc));
1007 LogFunc(("adding %ls\n", lpszName));
1008 /* create a new object and add it to the list */
1009 ComObjPtr<HostNetworkInterface> iface;
1010 iface.createObject();
1011 /* remove the curly bracket at the end */
1012 vrc = iface->init(name, enmType, &Info);
1013 if (SUCCEEDED(vrc))
1014 {
1015 if (Info.fIsDefault)
1016 pPist->push_front(iface);
1017 else
1018 pPist->push_back(iface);
1019 }
1020 else
1021 {
1022 LogRelFunc(("HostNetworkInterface::init() -> %Rrc\n", vrc));
1023 AssertComRC(vrc);
1024 }
1025 }
1026 else
1027 LogRelFunc(("failed to get device instance GUID (0x%x)\n", hr));
1028 CoTaskMemFree(lpszName);
1029 }
1030 else
1031 LogRelFunc(("failed to get device display name (0x%x)\n", hr));
1032
1033 return vrc;
1034}
1035
1036#endif /* VBOX_WITH_NETFLT */
1037
1038
1039static int netIfListHostAdapters(INetCfg *pNc, std::list<ComObjPtr<HostNetworkInterface> > &list)
1040{
1041#ifndef VBOX_WITH_NETFLT
1042 /* VBoxNetAdp is available only when VBOX_WITH_NETFLT is enabled */
1043 return VERR_NOT_IMPLEMENTED;
1044#else /* # if defined VBOX_WITH_NETFLT */
1045 INetCfgComponent *pMpNcc;
1046 HRESULT hr;
1047 IEnumNetCfgComponent *pEnumComponent;
1048
1049 hr = pNc->EnumComponents(&GUID_DEVCLASS_NET, &pEnumComponent);
1050 if (hr == S_OK)
1051 {
1052 while ((hr = pEnumComponent->Next(1, &pMpNcc, NULL)) == S_OK)
1053 {
1054 LPWSTR pwszName;
1055 ULONG uComponentStatus;
1056 hr = pMpNcc->GetDisplayName(&pwszName);
1057 if (hr == S_OK)
1058 LogFunc(("%ls\n", pwszName));
1059 else
1060 LogRelFunc(("failed to get device display name (0x%x)\n", hr));
1061 hr = pMpNcc->GetDeviceStatus(&uComponentStatus);
1062 if (hr == S_OK)
1063 {
1064 if (uComponentStatus == 0)
1065 {
1066 LPWSTR pId;
1067 hr = pMpNcc->GetId(&pId);
1068 Assert(hr == S_OK);
1069 if (hr == S_OK)
1070 {
1071 LogFunc(("id = %ls\n", pId));
1072 if (!_wcsnicmp(pId, L"sun_VBoxNetAdp", sizeof(L"sun_VBoxNetAdp")/2))
1073 {
1074 vboxNetWinAddComponent(&list, pMpNcc, HostNetworkInterfaceType_HostOnly, -1);
1075 }
1076 CoTaskMemFree(pId);
1077 }
1078 else
1079 LogRelFunc(("failed to get device id (0x%x)\n", hr));
1080 }
1081 }
1082 else
1083 LogRelFunc(("failed to get device status (0x%x)\n", hr));
1084 pMpNcc->Release();
1085 }
1086 Assert(hr == S_OK || hr == S_FALSE);
1087
1088 pEnumComponent->Release();
1089 }
1090 else
1091 LogRelFunc(("EnumComponents error (0x%x)\n", hr));
1092#endif /* # if defined VBOX_WITH_NETFLT */
1093 return VINF_SUCCESS;
1094}
1095
1096int NetIfGetConfig(HostNetworkInterface * pIf, NETIFINFO *pInfo)
1097{
1098#ifndef VBOX_WITH_NETFLT
1099 return VERR_NOT_IMPLEMENTED;
1100#else
1101 Bstr name;
1102 HRESULT hr = pIf->COMGETTER(Name)(name.asOutParam());
1103 if (hr == S_OK)
1104 {
1105 Bstr IfGuid;
1106 hr = pIf->COMGETTER(Id)(IfGuid.asOutParam());
1107 Assert(hr == S_OK);
1108 if (hr == S_OK)
1109 {
1110 memset(pInfo, 0, sizeof(NETIFINFO));
1111 Guid guid(IfGuid);
1112 pInfo->Uuid = *(guid.raw());
1113
1114 return collectNetIfInfo(name, guid, pInfo, getDefaultInterfaceIndex());
1115 }
1116 }
1117 return VERR_GENERAL_FAILURE;
1118#endif
1119}
1120
1121int NetIfGetConfigByName(PNETIFINFO)
1122{
1123 return VERR_NOT_IMPLEMENTED;
1124}
1125
1126/**
1127 * Obtain the current state of the interface.
1128 *
1129 * @returns VBox status code.
1130 *
1131 * @param pcszIfName Interface name.
1132 * @param penmState Where to store the retrieved state.
1133 */
1134int NetIfGetState(const char *pcszIfName, NETIFSTATUS *penmState)
1135{
1136 RT_NOREF(pcszIfName, penmState);
1137 return VERR_NOT_IMPLEMENTED;
1138}
1139
1140/**
1141 * Retrieve the physical link speed in megabits per second. If the interface is
1142 * not up or otherwise unavailable the zero speed is returned.
1143 *
1144 * @returns VBox status code.
1145 *
1146 * @param pcszIfName Interface name.
1147 * @param puMbits Where to store the link speed.
1148 */
1149int NetIfGetLinkSpeed(const char *pcszIfName, uint32_t *puMbits)
1150{
1151 RT_NOREF(pcszIfName, puMbits);
1152 return VERR_NOT_IMPLEMENTED;
1153}
1154
1155int NetIfCreateHostOnlyNetworkInterface(VirtualBox *pVirtualBox,
1156 IHostNetworkInterface **aHostNetworkInterface,
1157 IProgress **aProgress,
1158 IN_BSTR aName)
1159{
1160#ifndef VBOX_WITH_NETFLT
1161 return VERR_NOT_IMPLEMENTED;
1162#else
1163 /* create a progress object */
1164 ComObjPtr<Progress> progress;
1165 HRESULT hrc = progress.createObject();
1166 AssertComRCReturn(hrc, Global::vboxStatusCodeFromCOM(hrc));
1167
1168 ComPtr<IHost> host;
1169 hrc = pVirtualBox->COMGETTER(Host)(host.asOutParam());
1170 if (SUCCEEDED(hrc))
1171 {
1172 hrc = progress->init(pVirtualBox, host,
1173 Bstr(NetIfWin::tr("Creating host only network interface")).raw(),
1174 FALSE /* aCancelable */);
1175 if (SUCCEEDED(hrc))
1176 {
1177 progress.queryInterfaceTo(aProgress);
1178
1179 /* create a new uninitialized host interface object */
1180 ComObjPtr<HostNetworkInterface> iface;
1181 iface.createObject();
1182 iface.queryInterfaceTo(aHostNetworkInterface);
1183
1184 /* create the networkInterfaceHelperClient() argument */
1185 NetworkInterfaceHelperClientData* d = new NetworkInterfaceHelperClientData();
1186
1187 d->msgCode = SVCHlpMsg::CreateHostOnlyNetworkInterface;
1188 d->name = aName;
1189 d->iface = iface;
1190 d->ptrVBox = pVirtualBox;
1191
1192 hrc = pVirtualBox->i_startSVCHelperClient(IsUACEnabled() == TRUE /* aPrivileged */,
1193 netIfNetworkInterfaceHelperClient,
1194 static_cast<void *>(d),
1195 progress);
1196 /* d is now owned by netIfNetworkInterfaceHelperClient(), no need to delete one here */
1197
1198 }
1199 }
1200
1201 return Global::vboxStatusCodeFromCOM(hrc);
1202#endif
1203}
1204
1205int NetIfRemoveHostOnlyNetworkInterface(VirtualBox *pVirtualBox, const Guid &aId,
1206 IProgress **aProgress)
1207{
1208#ifndef VBOX_WITH_NETFLT
1209 return VERR_NOT_IMPLEMENTED;
1210#else
1211 /* create a progress object */
1212 ComObjPtr<Progress> progress;
1213 HRESULT hrc = progress.createObject();
1214 AssertComRCReturn(hrc, Global::vboxStatusCodeFromCOM(hrc));
1215
1216 ComPtr<IHost> host;
1217 hrc = pVirtualBox->COMGETTER(Host)(host.asOutParam());
1218 if (SUCCEEDED(hrc))
1219 {
1220 hrc = progress->init(pVirtualBox, host,
1221 Bstr(NetIfWin::tr("Removing host network interface")).raw(),
1222 FALSE /* aCancelable */);
1223 if (SUCCEEDED(hrc))
1224 {
1225 progress.queryInterfaceTo(aProgress);
1226
1227 /* create the networkInterfaceHelperClient() argument */
1228 NetworkInterfaceHelperClientData* d = new NetworkInterfaceHelperClientData();
1229
1230 d->msgCode = SVCHlpMsg::RemoveHostOnlyNetworkInterface;
1231 d->guid = aId;
1232
1233 hrc = pVirtualBox->i_startSVCHelperClient(IsUACEnabled() == TRUE /* aPrivileged */,
1234 netIfNetworkInterfaceHelperClient,
1235 static_cast<void *>(d),
1236 progress);
1237 /* d is now owned by netIfNetworkInterfaceHelperClient(), no need to delete one here */
1238
1239 }
1240 }
1241
1242 return Global::vboxStatusCodeFromCOM(hrc);
1243#endif
1244}
1245
1246int NetIfEnableStaticIpConfig(VirtualBox *pVBox, HostNetworkInterface * pIf, ULONG aOldIp, ULONG ip, ULONG mask)
1247{
1248 RT_NOREF(aOldIp);
1249#ifndef VBOX_WITH_NETFLT
1250 return VERR_NOT_IMPLEMENTED;
1251#else
1252 Bstr guid;
1253 HRESULT hrc = pIf->COMGETTER(Id)(guid.asOutParam());
1254 if (SUCCEEDED(hrc))
1255 {
1256// ComPtr<VirtualBox> pVBox;
1257// hrc = pIf->getVirtualBox(pVBox.asOutParam());
1258// if (SUCCEEDED(hrc))
1259 {
1260 /* create a progress object */
1261 ComObjPtr<Progress> progress;
1262 progress.createObject();
1263// ComPtr<IHost> host;
1264// HRESULT hrc = pVBox->COMGETTER(Host)(host.asOutParam());
1265// if (SUCCEEDED(hrc))
1266 {
1267 hrc = progress->init(pVBox, (IHostNetworkInterface*)pIf,
1268 Bstr(NetIfWin::tr("Enabling Dynamic Ip Configuration")).raw(),
1269 FALSE /* aCancelable */);
1270 if (SUCCEEDED(hrc))
1271 {
1272 if (FAILED(hrc)) return hrc;
1273// progress.queryInterfaceTo(aProgress);
1274
1275 /* create the networkInterfaceHelperClient() argument */
1276 NetworkInterfaceHelperClientData* d = new NetworkInterfaceHelperClientData();
1277
1278 d->msgCode = SVCHlpMsg::EnableStaticIpConfig;
1279 d->guid = Guid(guid);
1280 d->iface = pIf;
1281 d->u.StaticIP.IPAddress = ip;
1282 d->u.StaticIP.IPNetMask = mask;
1283
1284 hrc = pVBox->i_startSVCHelperClient(IsUACEnabled() == TRUE /* aPrivileged */,
1285 netIfNetworkInterfaceHelperClient,
1286 static_cast<void *>(d),
1287 progress);
1288 /* d is now owned by netIfNetworkInterfaceHelperClient(), no need to delete one here */
1289
1290 if (SUCCEEDED(hrc))
1291 {
1292 progress->WaitForCompletion(-1);
1293 }
1294 }
1295 }
1296 }
1297 }
1298
1299 return SUCCEEDED(hrc) ? VINF_SUCCESS : VERR_GENERAL_FAILURE;
1300#endif
1301}
1302
1303int NetIfEnableStaticIpConfigV6(VirtualBox *pVBox, HostNetworkInterface * pIf, const Utf8Str &aOldIPV6Address,
1304 const Utf8Str &aIPV6Address, ULONG aIPV6MaskPrefixLength)
1305{
1306 RT_NOREF(aOldIPV6Address);
1307#ifndef VBOX_WITH_NETFLT
1308 return VERR_NOT_IMPLEMENTED;
1309#else
1310 Bstr guid;
1311 HRESULT hrc = pIf->COMGETTER(Id)(guid.asOutParam());
1312 if (SUCCEEDED(hrc))
1313 {
1314// ComPtr<VirtualBox> pVBox;
1315// hrc = pIf->getVirtualBox(pVBox.asOutParam());
1316// if (SUCCEEDED(hrc))
1317 {
1318 /* create a progress object */
1319 ComObjPtr<Progress> progress;
1320 progress.createObject();
1321// ComPtr<IHost> host;
1322// HRESULT hrc = pVBox->COMGETTER(Host)(host.asOutParam());
1323// if (SUCCEEDED(hrc))
1324 {
1325 hrc = progress->init(pVBox, (IHostNetworkInterface*)pIf,
1326 Bstr(NetIfWin::tr("Enabling Dynamic Ip Configuration")).raw(),
1327 FALSE /* aCancelable */);
1328 if (SUCCEEDED(hrc))
1329 {
1330 if (FAILED(hrc)) return hrc;
1331// progress.queryInterfaceTo(aProgress);
1332
1333 /* create the networkInterfaceHelperClient() argument */
1334 NetworkInterfaceHelperClientData* d = new NetworkInterfaceHelperClientData();
1335
1336 d->msgCode = SVCHlpMsg::EnableStaticIpConfigV6;
1337 d->guid = guid;
1338 d->iface = pIf;
1339 d->u.StaticIPV6.IPV6Address = RTStrDup(aIPV6Address.c_str());
1340 d->u.StaticIPV6.IPV6NetMaskLength = aIPV6MaskPrefixLength;
1341
1342 hrc = pVBox->i_startSVCHelperClient(IsUACEnabled() == TRUE /* aPrivileged */,
1343 netIfNetworkInterfaceHelperClient,
1344 static_cast<void *>(d),
1345 progress);
1346 /* d is now owned by netIfNetworkInterfaceHelperClient(), no need to delete one here */
1347
1348 if (SUCCEEDED(hrc))
1349 {
1350 progress->WaitForCompletion(-1);
1351 }
1352 }
1353 }
1354 }
1355 }
1356
1357 return SUCCEEDED(hrc) ? VINF_SUCCESS : VERR_GENERAL_FAILURE;
1358#endif
1359}
1360
1361int NetIfEnableDynamicIpConfig(VirtualBox *pVBox, HostNetworkInterface * pIf)
1362{
1363#ifndef VBOX_WITH_NETFLT
1364 return VERR_NOT_IMPLEMENTED;
1365#else
1366 HRESULT hrc;
1367 Bstr guid;
1368 hrc = pIf->COMGETTER(Id)(guid.asOutParam());
1369 if (SUCCEEDED(hrc))
1370 {
1371// ComPtr<VirtualBox> pVBox;
1372// hrc = pIf->getVirtualBox(pVBox.asOutParam());
1373// if (SUCCEEDED(hrc))
1374 {
1375 /* create a progress object */
1376 ComObjPtr<Progress> progress;
1377 progress.createObject();
1378// ComPtr<IHost> host;
1379// HRESULT hrc = pVBox->COMGETTER(Host)(host.asOutParam());
1380// if (SUCCEEDED(hrc))
1381 {
1382 hrc = progress->init(pVBox, (IHostNetworkInterface*)pIf,
1383 Bstr(NetIfWin::tr("Enabling Dynamic Ip Configuration")).raw(),
1384 FALSE /* aCancelable */);
1385 if (SUCCEEDED(hrc))
1386 {
1387 if (FAILED(hrc)) return hrc;
1388// progress.queryInterfaceTo(aProgress);
1389
1390 /* create the networkInterfaceHelperClient() argument */
1391 NetworkInterfaceHelperClientData* d = new NetworkInterfaceHelperClientData();
1392
1393 d->msgCode = SVCHlpMsg::EnableDynamicIpConfig;
1394 d->guid = guid;
1395 d->iface = pIf;
1396
1397 hrc = pVBox->i_startSVCHelperClient(IsUACEnabled() == TRUE /* aPrivileged */,
1398 netIfNetworkInterfaceHelperClient,
1399 static_cast<void *>(d),
1400 progress);
1401 /* d is now owned by netIfNetworkInterfaceHelperClient(), no need to delete one here */
1402
1403 if (SUCCEEDED(hrc))
1404 {
1405 progress->WaitForCompletion(-1);
1406 }
1407 }
1408 }
1409 }
1410 }
1411
1412 return SUCCEEDED(hrc) ? VINF_SUCCESS : VERR_GENERAL_FAILURE;
1413#endif
1414}
1415
1416int NetIfDhcpRediscover(VirtualBox *pVBox, HostNetworkInterface * pIf)
1417{
1418#ifndef VBOX_WITH_NETFLT
1419 return VERR_NOT_IMPLEMENTED;
1420#else
1421 HRESULT hrc;
1422 Bstr guid;
1423 hrc = pIf->COMGETTER(Id)(guid.asOutParam());
1424 if (SUCCEEDED(hrc))
1425 {
1426// ComPtr<VirtualBox> pVBox;
1427// hrc = pIf->getVirtualBox(pVBox.asOutParam());
1428// if (SUCCEEDED(hrc))
1429 {
1430 /* create a progress object */
1431 ComObjPtr<Progress> progress;
1432 progress.createObject();
1433// ComPtr<IHost> host;
1434// HRESULT hrc = pVBox->COMGETTER(Host)(host.asOutParam());
1435// if (SUCCEEDED(hrc))
1436 {
1437 hrc = progress->init(pVBox, (IHostNetworkInterface*)pIf,
1438 Bstr(NetIfWin::tr("Enabling Dynamic Ip Configuration")).raw(),
1439 FALSE /* aCancelable */);
1440 if (SUCCEEDED(hrc))
1441 {
1442 if (FAILED(hrc)) return hrc;
1443// progress.queryInterfaceTo(aProgress);
1444
1445 /* create the networkInterfaceHelperClient() argument */
1446 NetworkInterfaceHelperClientData* d = new NetworkInterfaceHelperClientData();
1447
1448 d->msgCode = SVCHlpMsg::DhcpRediscover;
1449 d->guid = guid;
1450 d->iface = pIf;
1451
1452 hrc = pVBox->i_startSVCHelperClient(IsUACEnabled() == TRUE /* aPrivileged */,
1453 netIfNetworkInterfaceHelperClient,
1454 static_cast<void *>(d),
1455 progress);
1456 /* d is now owned by netIfNetworkInterfaceHelperClient(), no need to delete one here */
1457
1458 if (SUCCEEDED(hrc))
1459 {
1460 progress->WaitForCompletion(-1);
1461 }
1462 }
1463 }
1464 }
1465 }
1466
1467 return SUCCEEDED(hrc) ? VINF_SUCCESS : VERR_GENERAL_FAILURE;
1468#endif
1469}
1470
1471
1472#define netIfLog LogFunc
1473
1474struct BoundAdapter
1475{
1476 LPWSTR pName;
1477 LPWSTR pHwId;
1478 RTUUID guid;
1479 PIP_ADAPTER_ADDRESSES pAdapter;
1480 BOOL fWireless;
1481};
1482
1483static int netIfGetUnboundHostOnlyAdapters(INetCfg *pNetCfg, std::list<BoundAdapter> &adapters)
1484{
1485 INetCfgComponent *pMiniport;
1486 HRESULT hr;
1487 IEnumNetCfgComponent *pEnumComponent;
1488
1489 if ((hr = pNetCfg->EnumComponents(&GUID_DEVCLASS_NET, &pEnumComponent)) != S_OK)
1490 LogRelFunc(("failed to enumerate network adapter components (0x%x)\n", hr));
1491 else
1492 {
1493 while ((hr = pEnumComponent->Next(1, &pMiniport, NULL)) == S_OK)
1494 {
1495 GUID guid;
1496 ULONG uComponentStatus;
1497 struct BoundAdapter adapter;
1498 memset(&adapter, 0, sizeof(adapter));
1499 if ((hr = pMiniport->GetDisplayName(&adapter.pName)) != S_OK)
1500 LogRelFunc(("failed to get device display name (0x%x)\n", hr));
1501 else if ((hr = pMiniport->GetDeviceStatus(&uComponentStatus)) != S_OK)
1502 netIfLog(("failed to get device status (0x%x)\n", hr));
1503 else if (uComponentStatus != 0)
1504 netIfLog(("wrong device status (0x%x)\n", uComponentStatus));
1505 else if ((hr = pMiniport->GetId(&adapter.pHwId)) != S_OK)
1506 LogRelFunc(("failed to get device id (0x%x)\n", hr));
1507 else if (_wcsnicmp(adapter.pHwId, L"sun_VBoxNetAdp", sizeof(L"sun_VBoxNetAdp")/2))
1508 netIfLog(("not host-only id = %ls, ignored\n", adapter.pHwId));
1509 else if ((hr = pMiniport->GetInstanceGuid(&guid)) != S_OK)
1510 LogRelFunc(("failed to get instance id (0x%x)\n", hr));
1511 else
1512 {
1513 adapter.guid = *(Guid(guid).raw());
1514 netIfLog(("guid=%RTuuid, name=%ls id = %ls\n", &adapter.guid, adapter.pName, adapter.pHwId));
1515 adapters.push_back(adapter);
1516 adapter.pName = adapter.pHwId = NULL; /* do not free, will be done later */
1517 }
1518 if (adapter.pHwId)
1519 CoTaskMemFree(adapter.pHwId);
1520 if (adapter.pName)
1521 CoTaskMemFree(adapter.pName);
1522 pMiniport->Release();
1523 }
1524 Assert(hr == S_OK || hr == S_FALSE);
1525
1526 pEnumComponent->Release();
1527 }
1528 netIfLog(("return\n"));
1529 return VINF_SUCCESS;
1530}
1531
1532#define DEVNAME_PREFIX L"\\\\.\\"
1533
1534static BOOL netIfIsWireless(INetCfgComponent *pAdapter)
1535{
1536 bool fWireless = false;
1537
1538 /* Construct a device name. */
1539 LPWSTR pwszBindName = NULL;
1540 HRESULT hrc = pAdapter->GetBindName(&pwszBindName);
1541 if (SUCCEEDED(hrc) && pwszBindName)
1542 {
1543 WCHAR wszFileName[MAX_PATH];
1544 int vrc = RTUtf16Copy(wszFileName, MAX_PATH, DEVNAME_PREFIX);
1545 if (RT_SUCCESS(vrc))
1546 vrc = RTUtf16Cat(wszFileName, MAX_PATH, pwszBindName);
1547 if (RT_SUCCESS(vrc))
1548 {
1549 /* open the device */
1550 HANDLE hDevice = CreateFileW(wszFileName,
1551 GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
1552 NULL,
1553 OPEN_EXISTING,
1554 FILE_ATTRIBUTE_NORMAL,
1555 NULL);
1556 if (hDevice != INVALID_HANDLE_VALUE)
1557 {
1558 /* now issue the OID_GEN_PHYSICAL_MEDIUM query */
1559 DWORD Oid = OID_GEN_PHYSICAL_MEDIUM;
1560 NDIS_PHYSICAL_MEDIUM PhMedium = NdisPhysicalMediumUnspecified;
1561 DWORD cbResultIgn = 0;
1562 if (DeviceIoControl(hDevice,
1563 IOCTL_NDIS_QUERY_GLOBAL_STATS,
1564 &Oid,
1565 sizeof(Oid),
1566 &PhMedium,
1567 sizeof(PhMedium),
1568 &cbResultIgn,
1569 NULL))
1570 {
1571 /* that was simple, now examine PhMedium */
1572 fWireless = PhMedium == NdisPhysicalMediumWirelessWan
1573 || PhMedium == NdisPhysicalMediumWirelessLan
1574 || PhMedium == NdisPhysicalMediumNative802_11
1575 || PhMedium == NdisPhysicalMediumBluetooth;
1576 }
1577 else
1578 {
1579 DWORD rcWin = GetLastError();
1580 LogRel(("netIfIsWireless: DeviceIoControl to '%ls' failed with rcWin=%u (%#x) - ignoring\n",
1581 wszFileName, rcWin, rcWin));
1582 Assert(rcWin == ERROR_INVALID_PARAMETER || rcWin == ERROR_NOT_SUPPORTED || rcWin == ERROR_BAD_COMMAND);
1583 }
1584 CloseHandle(hDevice);
1585 }
1586 else
1587 {
1588 DWORD rcWin = GetLastError();
1589#if 0 /* bird: Triggers on each VBoxSVC startup so, disabled. Whoever want it, can enable using DEBUG_xxxx. */
1590 AssertLogRelMsgFailed(("netIfIsWireless: CreateFile on '%ls' failed with rcWin=%u (%#x) - ignoring\n",
1591 wszFileName, rcWin, rcWin));
1592#else
1593 LogRel(("netIfIsWireless: CreateFile on '%ls' failed with rcWin=%u (%#x) - ignoring\n",
1594 wszFileName, rcWin, rcWin));
1595#endif
1596 }
1597 }
1598 CoTaskMemFree(pwszBindName);
1599 }
1600 else
1601 LogRel(("netIfIsWireless: GetBindName failed hrc=%Rhrc\n", hrc));
1602
1603 return fWireless;
1604}
1605
1606static HRESULT netIfGetBoundAdapters(std::list<BoundAdapter> &boundAdapters)
1607{
1608 INetCfg *pNetCfg = NULL;
1609 INetCfgComponent *pFilter;
1610 LPWSTR lpszApp;
1611 HRESULT hr;
1612
1613 netIfLog(("building the list of interfaces\n"));
1614 /* we are using the INetCfg API for getting the list of miniports */
1615 hr = VBoxNetCfgWinQueryINetCfg(&pNetCfg, FALSE,
1616 VBOX_APP_NAME,
1617 10000,
1618 &lpszApp);
1619 Assert(hr == S_OK);
1620 if (hr != S_OK)
1621 {
1622 LogRelFunc(("failed to query INetCfg (0x%x)\n", hr));
1623 return hr;
1624 }
1625
1626 if ((hr = pNetCfg->FindComponent(L"oracle_VBoxNetLwf", &pFilter)) != S_OK
1627 /* fall back to NDIS5 miniport lookup */
1628 && (hr = pNetCfg->FindComponent(L"sun_VBoxNetFlt", &pFilter)))
1629 LogRelFunc(("could not find either 'oracle_VBoxNetLwf' or 'sun_VBoxNetFlt' components (0x%x)\n", hr));
1630 else
1631 {
1632 INetCfgComponentBindings *pFilterBindings;
1633 if ((pFilter->QueryInterface(IID_INetCfgComponentBindings, (PVOID*)&pFilterBindings)) != S_OK)
1634 LogRelFunc(("failed to query INetCfgComponentBindings (0x%x)\n", hr));
1635 else
1636 {
1637 IEnumNetCfgBindingPath *pEnumBp;
1638 INetCfgBindingPath *pBp;
1639 if ((pFilterBindings->EnumBindingPaths(EBP_BELOW, &pEnumBp)) != S_OK)
1640 LogRelFunc(("failed to enumerate binding paths (0x%x)\n", hr));
1641 else
1642 {
1643 pEnumBp->Reset();
1644 while ((hr = pEnumBp->Next(1, &pBp, NULL)) == S_OK)
1645 {
1646 IEnumNetCfgBindingInterface *pEnumBi;
1647 INetCfgBindingInterface *pBi;
1648 if (pBp->IsEnabled() != S_OK)
1649 {
1650 /** @todo some id of disabled path could be useful. */
1651 netIfLog(("INetCfgBindingPath is disabled (0x%x)\n", hr));
1652 pBp->Release();
1653 continue;
1654 }
1655 if ((pBp->EnumBindingInterfaces(&pEnumBi)) != S_OK)
1656 LogRelFunc(("failed to enumerate binding interfaces (0x%x)\n", hr));
1657 else
1658 {
1659 hr = pEnumBi->Reset();
1660 while ((hr = pEnumBi->Next(1, &pBi, NULL)) == S_OK)
1661 {
1662 INetCfgComponent *pAdapter;
1663 if ((hr = pBi->GetLowerComponent(&pAdapter)) != S_OK)
1664 LogRelFunc(("failed to get lower component (0x%x)\n", hr));
1665 else
1666 {
1667 LPWSTR pwszName = NULL;
1668 if ((hr = pAdapter->GetDisplayName(&pwszName)) != S_OK)
1669 LogRelFunc(("failed to get display name (0x%x)\n", hr));
1670 else
1671 {
1672 ULONG uStatus;
1673 DWORD dwChars;
1674 if ((hr = pAdapter->GetDeviceStatus(&uStatus)) != S_OK)
1675 netIfLog(("%ls: failed to get device status (0x%x)\n",
1676 pwszName, hr));
1677 else if ((hr = pAdapter->GetCharacteristics(&dwChars)) != S_OK)
1678 netIfLog(("%ls: failed to get device characteristics (0x%x)\n",
1679 pwszName, hr));
1680 else if (uStatus != 0)
1681 netIfLog(("%ls: wrong status 0x%x\n",
1682 pwszName, uStatus));
1683 else if (dwChars & NCF_HIDDEN)
1684 netIfLog(("%ls: wrong characteristics 0x%x\n",
1685 pwszName, dwChars));
1686 else
1687 {
1688 GUID guid;
1689 LPWSTR pwszHwId = NULL;
1690 if ((hr = pAdapter->GetId(&pwszHwId)) != S_OK)
1691 LogRelFunc(("%ls: failed to get hardware id (0x%x)\n",
1692 pwszName, hr));
1693 else if (!_wcsnicmp(pwszHwId, L"sun_VBoxNetAdp", sizeof(L"sun_VBoxNetAdp")/2))
1694 netIfLog(("host-only adapter %ls, ignored\n", pwszName));
1695 else if ((hr = pAdapter->GetInstanceGuid(&guid)) != S_OK)
1696 LogRelFunc(("%ls: failed to get instance GUID (0x%x)\n",
1697 pwszName, hr));
1698 else
1699 {
1700 struct BoundAdapter adapter;
1701 adapter.pName = pwszName;
1702 adapter.pHwId = pwszHwId;
1703 adapter.guid = *(Guid(guid).raw());
1704 adapter.pAdapter = NULL;
1705 adapter.fWireless = netIfIsWireless(pAdapter);
1706 netIfLog(("guid=%RTuuid, name=%ls, hwid=%ls, status=%x, chars=%x\n",
1707 &adapter.guid, pwszName, pwszHwId, uStatus, dwChars));
1708 boundAdapters.push_back(adapter);
1709 pwszName = pwszHwId = NULL; /* do not free, will be done later */
1710 }
1711 if (pwszHwId)
1712 CoTaskMemFree(pwszHwId);
1713 }
1714 if (pwszName)
1715 CoTaskMemFree(pwszName);
1716 }
1717
1718 pAdapter->Release();
1719 }
1720 pBi->Release();
1721 }
1722 pEnumBi->Release();
1723 }
1724 pBp->Release();
1725 }
1726 pEnumBp->Release();
1727 }
1728 pFilterBindings->Release();
1729 }
1730 pFilter->Release();
1731 }
1732 /* Host-only adapters are not necessarily bound, add them separately. */
1733 netIfGetUnboundHostOnlyAdapters(pNetCfg, boundAdapters);
1734 VBoxNetCfgWinReleaseINetCfg(pNetCfg, FALSE);
1735
1736 return S_OK;
1737}
1738
1739#if 0
1740static HRESULT netIfGetBoundAdaptersFallback(std::list<BoundAdapter> &boundAdapters)
1741{
1742 return CO_E_NOT_SUPPORTED;
1743}
1744#endif
1745
1746/**
1747 * Walk through the list of adpater addresses and extract the required
1748 * information. XP and older don't not have the OnLinkPrefixLength field.
1749 */
1750static void netIfFillInfoWithAddressesXp(PNETIFINFO pInfo, PIP_ADAPTER_ADDRESSES pAdapter)
1751{
1752 PIP_ADAPTER_UNICAST_ADDRESS pAddr;
1753 bool fIPFound = false;
1754 bool fIPv6Found = false;
1755 for (pAddr = pAdapter->FirstUnicastAddress; pAddr; pAddr = pAddr->Next)
1756 {
1757 switch (pAddr->Address.lpSockaddr->sa_family)
1758 {
1759 case AF_INET:
1760 if (!fIPFound)
1761 {
1762 fIPFound = true;
1763 memcpy(&pInfo->IPAddress,
1764 &((struct sockaddr_in *)pAddr->Address.lpSockaddr)->sin_addr.s_addr,
1765 sizeof(pInfo->IPAddress));
1766 }
1767 break;
1768 case AF_INET6:
1769 if (!fIPv6Found)
1770 {
1771 fIPv6Found = true;
1772 memcpy(&pInfo->IPv6Address,
1773 ((struct sockaddr_in6 *)pAddr->Address.lpSockaddr)->sin6_addr.s6_addr,
1774 sizeof(pInfo->IPv6Address));
1775 }
1776 break;
1777 }
1778 }
1779 PIP_ADAPTER_PREFIX pPrefix;
1780 ULONG uPrefixLenV4 = 0;
1781 ULONG uPrefixLenV6 = 0;
1782 for (pPrefix = pAdapter->FirstPrefix; pPrefix && !(uPrefixLenV4 && uPrefixLenV6); pPrefix = pPrefix->Next)
1783 {
1784 switch (pPrefix->Address.lpSockaddr->sa_family)
1785 {
1786 case AF_INET:
1787 if (!uPrefixLenV4)
1788 {
1789 ULONG ip = ((PSOCKADDR_IN)(pPrefix->Address.lpSockaddr))->sin_addr.s_addr;
1790 netIfLog(("prefix=%RTnaipv4 len=%u\n", ip, pPrefix->PrefixLength));
1791 if ( pPrefix->PrefixLength < sizeof(pInfo->IPNetMask) * 8
1792 && pPrefix->PrefixLength > 0
1793 && (ip & 0xF0) < 224)
1794 {
1795 uPrefixLenV4 = pPrefix->PrefixLength;
1796 RTNetPrefixToMaskIPv4(pPrefix->PrefixLength, &pInfo->IPNetMask);
1797 }
1798 else
1799 netIfLog(("Unexpected IPv4 prefix length of %d\n",
1800 pPrefix->PrefixLength));
1801 }
1802 break;
1803 case AF_INET6:
1804 if (!uPrefixLenV6)
1805 {
1806 PBYTE ipv6 = ((PSOCKADDR_IN6)(pPrefix->Address.lpSockaddr))->sin6_addr.s6_addr;
1807 netIfLog(("prefix=%RTnaipv6 len=%u\n", ipv6, pPrefix->PrefixLength));
1808 if ( pPrefix->PrefixLength < sizeof(pInfo->IPv6NetMask) * 8
1809 && pPrefix->PrefixLength > 0
1810 && ipv6[0] != 0xFF)
1811 {
1812 uPrefixLenV6 = pPrefix->PrefixLength;
1813 RTNetPrefixToMaskIPv6(pPrefix->PrefixLength, &pInfo->IPv6NetMask);
1814 }
1815 else
1816 netIfLog(("Unexpected IPv6 prefix length of %d\n", pPrefix->PrefixLength));
1817 }
1818 break;
1819 }
1820 }
1821 netIfLog(("%RTnaipv4/%u\n", pInfo->IPAddress, uPrefixLenV4));
1822 netIfLog(("%RTnaipv6/%u\n", &pInfo->IPv6Address, uPrefixLenV6));
1823}
1824
1825/**
1826 * Walk through the list of adpater addresses and extract the required
1827 * information. XP and older don't not have the OnLinkPrefixLength field.
1828 */
1829static void netIfFillInfoWithAddressesVista(PNETIFINFO pInfo, PIP_ADAPTER_ADDRESSES pAdapter)
1830{
1831 PIP_ADAPTER_UNICAST_ADDRESS pAddr;
1832
1833 if (sizeof(pInfo->MACAddress) != pAdapter->PhysicalAddressLength)
1834 netIfLog(("Unexpected physical address length: %u\n", pAdapter->PhysicalAddressLength));
1835 else
1836 memcpy(pInfo->MACAddress.au8, pAdapter->PhysicalAddress, sizeof(pInfo->MACAddress));
1837
1838 bool fIPFound = false;
1839 bool fIPv6Found = false;
1840 for (pAddr = pAdapter->FirstUnicastAddress; pAddr; pAddr = pAddr->Next)
1841 {
1842 PIP_ADAPTER_UNICAST_ADDRESS_LH pAddrLh = (PIP_ADAPTER_UNICAST_ADDRESS_LH)pAddr;
1843 switch (pAddrLh->Address.lpSockaddr->sa_family)
1844 {
1845 case AF_INET:
1846 if (!fIPFound)
1847 {
1848 fIPFound = true;
1849 memcpy(&pInfo->IPAddress,
1850 &((struct sockaddr_in *)pAddrLh->Address.lpSockaddr)->sin_addr.s_addr,
1851 sizeof(pInfo->IPAddress));
1852 if (pAddrLh->OnLinkPrefixLength > 32)
1853 netIfLog(("Invalid IPv4 prefix length of %d\n", pAddrLh->OnLinkPrefixLength));
1854 else
1855 RTNetPrefixToMaskIPv4(pAddrLh->OnLinkPrefixLength, &pInfo->IPNetMask);
1856 }
1857 break;
1858 case AF_INET6:
1859 if (!fIPv6Found)
1860 {
1861 fIPv6Found = true;
1862 memcpy(&pInfo->IPv6Address,
1863 ((struct sockaddr_in6 *)pAddrLh->Address.lpSockaddr)->sin6_addr.s6_addr,
1864 sizeof(pInfo->IPv6Address));
1865 if (pAddrLh->OnLinkPrefixLength > 128)
1866 netIfLog(("Invalid IPv6 prefix length of %d\n", pAddrLh->OnLinkPrefixLength));
1867 else
1868 RTNetPrefixToMaskIPv6(pAddrLh->OnLinkPrefixLength, &pInfo->IPv6NetMask);
1869 }
1870 break;
1871 }
1872 }
1873
1874 if (fIPFound)
1875 {
1876 int iPrefixIPv4 = -1;
1877 RTNetMaskToPrefixIPv4(&pInfo->IPNetMask, &iPrefixIPv4);
1878 netIfLog(("%RTnaipv4/%u\n", pInfo->IPAddress, iPrefixIPv4));
1879 }
1880 if (fIPv6Found)
1881 {
1882 int iPrefixIPv6 = -1;
1883 RTNetMaskToPrefixIPv6(&pInfo->IPv6NetMask, &iPrefixIPv6);
1884 netIfLog(("%RTnaipv6/%u\n", &pInfo->IPv6Address, iPrefixIPv6));
1885 }
1886}
1887
1888#if (NTDDI_VERSION >= NTDDI_VISTA)
1889#define NETIF_GAA_FLAGS GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST
1890#else /* (NTDDI_VERSION < NTDDI_VISTA) */
1891#define NETIF_GAA_FLAGS GAA_FLAG_INCLUDE_PREFIX | GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST
1892#endif /* (NTDDI_VERSION < NTDDI_VISTA) */
1893
1894int NetIfList(std::list<ComObjPtr<HostNetworkInterface> > &list)
1895{
1896 HRESULT hr = S_OK;
1897 int iDefault = getDefaultInterfaceIndex();
1898 /* MSDN recommends to pre-allocate a 15KB buffer. */
1899 ULONG uBufLen = 15 * 1024;
1900 PIP_ADAPTER_ADDRESSES pAddresses = (PIP_ADAPTER_ADDRESSES)RTMemAlloc(uBufLen);
1901 if (!pAddresses)
1902 return HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
1903 DWORD dwRc = GetAdaptersAddresses(AF_UNSPEC, NETIF_GAA_FLAGS, NULL, pAddresses, &uBufLen);
1904 for (int tries = 0; tries < 3 && dwRc == ERROR_BUFFER_OVERFLOW; ++tries)
1905 {
1906 /* Get more memory and try again. */
1907 RTMemFree(pAddresses);
1908 pAddresses = (PIP_ADAPTER_ADDRESSES)RTMemAlloc(uBufLen);
1909 if (!pAddresses)
1910 return HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
1911 dwRc = GetAdaptersAddresses(AF_UNSPEC, NETIF_GAA_FLAGS, NULL, pAddresses, &uBufLen);
1912 }
1913 if (dwRc != NO_ERROR)
1914 {
1915 LogRelFunc(("GetAdaptersAddresses failed (0x%x)\n", dwRc));
1916 hr = HRESULT_FROM_WIN32(dwRc);
1917 }
1918 else
1919 {
1920 std::list<BoundAdapter> boundAdapters;
1921 hr = netIfGetBoundAdapters(boundAdapters);
1922#if 0
1923 if (hr != S_OK)
1924 hr = netIfGetBoundAdaptersFallback(boundAdapters);
1925#endif
1926 if (hr != S_OK)
1927 LogRelFunc(("netIfGetBoundAdapters failed (0x%x)\n", hr));
1928 else
1929 {
1930 PIP_ADAPTER_ADDRESSES pAdapter;
1931
1932 for (pAdapter = pAddresses; pAdapter; pAdapter = pAdapter->Next)
1933 {
1934 char *pszUuid = RTStrDup(pAdapter->AdapterName);
1935 if (!pszUuid)
1936 {
1937 LogRelFunc(("out of memory\n"));
1938 break;
1939 }
1940 size_t len = strlen(pszUuid) - 1;
1941 if (pszUuid[0] != '{' || pszUuid[len] != '}')
1942 LogRelFunc(("ignoring invalid GUID %s\n", pAdapter->AdapterName));
1943 else
1944 {
1945 std::list<BoundAdapter>::iterator it;
1946 pszUuid[len] = 0;
1947 for (it = boundAdapters.begin(); it != boundAdapters.end(); ++it)
1948 {
1949 if (!RTUuidCompareStr(&(*it).guid, pszUuid + 1))
1950 {
1951 (*it).pAdapter = pAdapter;
1952 break;
1953 }
1954 }
1955 }
1956 RTStrFree(pszUuid);
1957 }
1958 std::list<BoundAdapter>::iterator it;
1959 for (it = boundAdapters.begin(); it != boundAdapters.end(); ++it)
1960 {
1961 NETIFINFO info;
1962 memset(&info, 0, sizeof(info));
1963 info.Uuid = (*it).guid;
1964 info.enmMediumType = NETIF_T_ETHERNET;
1965 info.fWireless = (*it).fWireless;
1966 pAdapter = (*it).pAdapter;
1967 if (pAdapter)
1968 {
1969 info.enmStatus = pAdapter->OperStatus == IfOperStatusUp ? NETIF_S_UP : NETIF_S_DOWN;
1970 info.fIsDefault = (pAdapter->IfIndex == (DWORD)iDefault);
1971 info.fDhcpEnabled = pAdapter->Flags & IP_ADAPTER_DHCP_ENABLED;
1972 OSVERSIONINFOEX OSInfoEx;
1973 RT_ZERO(OSInfoEx);
1974 OSInfoEx.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
1975 if ( GetVersionEx((LPOSVERSIONINFO)&OSInfoEx)
1976 && OSInfoEx.dwMajorVersion < 6)
1977 netIfFillInfoWithAddressesXp(&info, pAdapter);
1978 else
1979 netIfFillInfoWithAddressesVista(&info, pAdapter);
1980 }
1981 else
1982 info.enmStatus = NETIF_S_DOWN;
1983 /* create a new object and add it to the list */
1984 ComObjPtr<HostNetworkInterface> iface;
1985 iface.createObject();
1986 HostNetworkInterfaceType enmType = _wcsnicmp((*it).pHwId, L"sun_VBoxNetAdp", sizeof(L"sun_VBoxNetAdp") / 2)
1987 ? HostNetworkInterfaceType_Bridged : HostNetworkInterfaceType_HostOnly;
1988 netIfLog(("Adding %ls as %s\n", (*it).pName,
1989 enmType == HostNetworkInterfaceType_Bridged ? "bridged" :
1990 enmType == HostNetworkInterfaceType_HostOnly ? "host-only" : "unknown"));
1991 HRESULT hrc = iface->init((*it).pName, enmType, &info);
1992 if (FAILED(hrc))
1993 LogRelFunc(("HostNetworkInterface::init() -> %Rrc\n", hrc));
1994 else
1995 {
1996 if (info.fIsDefault)
1997 list.push_front(iface);
1998 else
1999 list.push_back(iface);
2000 }
2001 if ((*it).pHwId)
2002 CoTaskMemFree((*it).pHwId);
2003 if ((*it).pName)
2004 CoTaskMemFree((*it).pName);
2005 }
2006 }
2007 }
2008 RTMemFree(pAddresses);
2009
2010 return hr;
2011}
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