VirtualBox

source: vbox/trunk/src/VBox/Main/darwin/NetIf-darwin.cpp@ 27527

Last change on this file since 27527 was 27527, checked in by vboxsync, 15 years ago

Net-OSX: try to narrow down a bug when getting the host network interfaces (temporary)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 18.4 KB
Line 
1/* $Id: NetIf-darwin.cpp 27527 2010-03-19 12:21:53Z vboxsync $ */
2/** @file
3 * Main - NetIfList, Darwin implementation.
4 */
5
6/*
7 * Copyright (C) 2008 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22
23
24/*******************************************************************************
25* Header Files *
26*******************************************************************************/
27/*
28 * Deal with conflicts first.
29 * PVM - BSD mess, that FreeBSD has correct a long time ago.
30 * iprt/types.h before sys/param.h - prevents UINT32_C and friends.
31 */
32#include <iprt/types.h>
33#include <sys/param.h>
34#undef PVM
35
36#define LOG_GROUP LOG_GROUP_MAIN
37
38#include <iprt/err.h>
39#include <iprt/alloc.h>
40
41#include <string.h>
42#include <sys/socket.h>
43#include <sys/ioctl.h>
44#include <sys/sysctl.h>
45#include <netinet/in.h>
46#include <net/if.h>
47#include <net/if_dl.h>
48#include <net/if_types.h>
49#include <net/route.h>
50#include <ifaddrs.h>
51#include <errno.h>
52#include <unistd.h>
53#include <list>
54
55#include "HostNetworkInterfaceImpl.h"
56#include "netif.h"
57#include "iokit.h"
58#include "Logging.h"
59
60#if 0
61int NetIfList(std::list <ComObjPtr<HostNetworkInterface> > &list)
62{
63 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
64 if (sock < 0)
65 {
66 Log(("NetIfList: socket() -> %d\n", errno));
67 return NULL;
68 }
69 struct ifaddrs *IfAddrs, *pAddr;
70 int rc = getifaddrs(&IfAddrs);
71 if (rc)
72 {
73 close(sock);
74 Log(("NetIfList: getifaddrs() -> %d\n", rc));
75 return VERR_INTERNAL_ERROR;
76 }
77
78 PDARWINETHERNIC pEtherNICs = DarwinGetEthernetControllers();
79 while (pEtherNICs)
80 {
81 size_t cbNameLen = strlen(pEtherNICs->szName) + 1;
82 PNETIFINFO pNew = (PNETIFINFO)RTMemAllocZ(RT_OFFSETOF(NETIFINFO, szName[cbNameLen]));
83 pNew->MACAddress = pEtherNICs->Mac;
84 pNew->enmMediumType = NETIF_T_ETHERNET;
85 pNew->Uuid = pEtherNICs->Uuid;
86 Assert(sizeof(pNew->szShortName) > sizeof(pEtherNICs->szBSDName));
87 memcpy(pNew->szShortName, pEtherNICs->szBSDName, sizeof(pEtherNICs->szBSDName));
88 pNew->szShortName[sizeof(pEtherNICs->szBSDName)] = '\0';
89 memcpy(pNew->szName, pEtherNICs->szName, cbNameLen);
90
91 struct ifreq IfReq;
92 strcpy(IfReq.ifr_name, pNew->szShortName);
93 if (ioctl(sock, SIOCGIFFLAGS, &IfReq) < 0)
94 {
95 Log(("NetIfList: ioctl(SIOCGIFFLAGS) -> %d\n", errno));
96 pNew->enmStatus = NETIF_S_UNKNOWN;
97 }
98 else
99 pNew->enmStatus = (IfReq.ifr_flags & IFF_UP) ? NETIF_S_UP : NETIF_S_DOWN;
100
101 for (pAddr = IfAddrs; pAddr != NULL; pAddr = pAddr->ifa_next)
102 {
103 if (strcmp(pNew->szShortName, pAddr->ifa_name))
104 continue;
105
106 struct sockaddr_in *pIPAddr, *pIPNetMask;
107 struct sockaddr_in6 *pIPv6Addr, *pIPv6NetMask;
108
109 switch (pAddr->ifa_addr->sa_family)
110 {
111 case AF_INET:
112 if (pNew->IPAddress.u)
113 break;
114 pIPAddr = (struct sockaddr_in *)pAddr->ifa_addr;
115 Assert(sizeof(pNew->IPAddress) == sizeof(pIPAddr->sin_addr));
116 pNew->IPAddress.u = pIPAddr->sin_addr.s_addr;
117 pIPNetMask = (struct sockaddr_in *)pAddr->ifa_netmask;
118 Assert(pIPNetMask->sin_family == AF_INET);
119 Assert(sizeof(pNew->IPNetMask) == sizeof(pIPNetMask->sin_addr));
120 pNew->IPNetMask.u = pIPNetMask->sin_addr.s_addr;
121 break;
122 case AF_INET6:
123 if (pNew->IPv6Address.s.Lo || pNew->IPv6Address.s.Hi)
124 break;
125 pIPv6Addr = (struct sockaddr_in6 *)pAddr->ifa_addr;
126 Assert(sizeof(pNew->IPv6Address) == sizeof(pIPv6Addr->sin6_addr));
127 memcpy(pNew->IPv6Address.au8,
128 pIPv6Addr->sin6_addr.__u6_addr.__u6_addr8,
129 sizeof(pNew->IPv6Address));
130 pIPv6NetMask = (struct sockaddr_in6 *)pAddr->ifa_netmask;
131 Assert(pIPv6NetMask->sin6_family == AF_INET6);
132 Assert(sizeof(pNew->IPv6NetMask) == sizeof(pIPv6NetMask->sin6_addr));
133 memcpy(pNew->IPv6NetMask.au8,
134 pIPv6NetMask->sin6_addr.__u6_addr.__u6_addr8,
135 sizeof(pNew->IPv6NetMask));
136 break;
137 }
138 }
139
140 ComObjPtr<HostNetworkInterface> IfObj;
141 IfObj.createObject();
142 if (SUCCEEDED(IfObj->init(Bstr(pEtherNICs->szName), HostNetworkInterfaceType_Bridged, pNew)))
143 list.push_back(IfObj);
144 RTMemFree(pNew);
145
146 /* next, free current */
147 void *pvFree = pEtherNICs;
148 pEtherNICs = pEtherNICs->pNext;
149 RTMemFree(pvFree);
150 }
151
152 freeifaddrs(IfAddrs);
153 close(sock);
154 return VINF_SUCCESS;
155}
156#else
157
158#define ROUNDUP(a) \
159 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
160#define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
161
162void extractAddresses(int iAddrMask, caddr_t cp, caddr_t cplim, struct sockaddr **pAddresses)
163{
164 struct sockaddr *sa;
165
166 for (int i = 0; i < RTAX_MAX && cp < cplim; i++) {
167 if (!(iAddrMask & (1 << i)))
168 continue;
169
170 sa = (struct sockaddr *)cp;
171
172 pAddresses[i] = sa;
173
174 ADVANCE(cp, sa);
175 }
176}
177
178void extractAddressesToNetInfo(int iAddrMask, caddr_t cp, caddr_t cplim, PNETIFINFO pInfo)
179{
180 struct sockaddr *addresses[RTAX_MAX];
181
182 extractAddresses(iAddrMask, cp, cplim, addresses);
183 switch (addresses[RTAX_IFA]->sa_family)
184 {
185 case AF_INET:
186 if (!pInfo->IPAddress.u)
187 {
188 pInfo->IPAddress.u = ((struct sockaddr_in *)addresses[RTAX_IFA])->sin_addr.s_addr;
189 pInfo->IPNetMask.u = ((struct sockaddr_in *)addresses[RTAX_NETMASK])->sin_addr.s_addr;
190 }
191 break;
192 case AF_INET6:
193 if (!pInfo->IPv6Address.s.Lo && !pInfo->IPv6Address.s.Hi)
194 {
195 memcpy(pInfo->IPv6Address.au8,
196 ((struct sockaddr_in6 *)addresses[RTAX_IFA])->sin6_addr.__u6_addr.__u6_addr8,
197 sizeof(pInfo->IPv6Address));
198 memcpy(pInfo->IPv6NetMask.au8,
199 ((struct sockaddr_in6 *)addresses[RTAX_NETMASK])->sin6_addr.__u6_addr.__u6_addr8,
200 sizeof(pInfo->IPv6NetMask));
201 }
202 break;
203 default:
204 Log(("NetIfList: Unsupported address family: %u\n", addresses[RTAX_IFA]->sa_family));
205 break;
206 }
207}
208
209static int getDefaultIfaceIndex(unsigned short *pu16Index)
210{
211 size_t cbNeeded;
212 char *pBuf, *pNext;
213 int aiMib[6];
214 struct sockaddr *addresses[RTAX_MAX];
215
216 aiMib[0] = CTL_NET;
217 aiMib[1] = PF_ROUTE;
218 aiMib[2] = 0;
219 aiMib[3] = PF_INET; /* address family */
220 aiMib[4] = NET_RT_DUMP;
221 aiMib[5] = 0;
222
223 if (sysctl(aiMib, 6, NULL, &cbNeeded, NULL, 0) < 0)
224 {
225 Log(("getDefaultIfaceIndex: Failed to get estimate for list size (errno=%d).\n", errno));
226 return RTErrConvertFromErrno(errno);
227 }
228 if ((pBuf = (char*)malloc(cbNeeded)) == NULL)
229 return VERR_NO_MEMORY;
230 if (sysctl(aiMib, 6, pBuf, &cbNeeded, NULL, 0) < 0)
231 {
232 free(pBuf);
233 Log(("getDefaultIfaceIndex: Failed to retrieve interface table (errno=%d).\n", errno));
234 return RTErrConvertFromErrno(errno);
235 }
236
237 char *pEnd = pBuf + cbNeeded;
238 struct rt_msghdr *pRtMsg;
239 for (pNext = pBuf; pNext < pEnd; pNext += pRtMsg->rtm_msglen)
240 {
241 pRtMsg = (struct rt_msghdr *)pNext;
242
243 if (pRtMsg->rtm_type != RTM_GET)
244 {
245 Log(("getDefaultIfaceIndex: Got message %u while expecting %u.\n",
246 pRtMsg->rtm_type, RTM_GET));
247 //rc = VERR_INTERNAL_ERROR;
248 continue;
249 }
250 if ((char*)(pRtMsg + 1) < pEnd)
251 {
252 /* Extract addresses from the message. */
253 extractAddresses(pRtMsg->rtm_addrs, (char *)(pRtMsg + 1),
254 pRtMsg->rtm_msglen + (char *)pRtMsg, addresses);
255 if ((pRtMsg->rtm_addrs & RTA_DST))
256 {
257 if (addresses[RTAX_DST]->sa_family != AF_INET)
258 continue;
259 struct sockaddr_in *addr = (struct sockaddr_in *)addresses[RTAX_DST];
260 struct sockaddr_in *mask = (struct sockaddr_in *)addresses[RTAX_NETMASK];
261 if ((addr->sin_addr.s_addr == INADDR_ANY) &&
262 mask &&
263 (ntohl(mask->sin_addr.s_addr) == 0L ||
264 mask->sin_len == 0))
265 {
266 *pu16Index = pRtMsg->rtm_index;
267 free(pBuf);
268 return VINF_SUCCESS;
269 }
270 }
271 }
272 }
273 free(pBuf);
274 return 0; /* Failed to find default interface, take the first one in the list. */
275}
276
277int NetIfList(std::list <ComObjPtr<HostNetworkInterface> > &list)
278{
279 int rc = VINF_SUCCESS;
280 size_t cbNeeded;
281 char *pBuf, *pNext;
282 int aiMib[6];
283 unsigned short u16DefaultIface = 0; /* initialized to shut up gcc */
284
285 /* Get the index of the interface associated with default route. */
286 rc = getDefaultIfaceIndex(&u16DefaultIface);
287 if (RT_FAILURE(rc))
288 return rc;
289
290 aiMib[0] = CTL_NET;
291 aiMib[1] = PF_ROUTE;
292 aiMib[2] = 0;
293 aiMib[3] = 0; /* address family */
294 aiMib[4] = NET_RT_IFLIST;
295 aiMib[5] = 0;
296
297 if (sysctl(aiMib, 6, NULL, &cbNeeded, NULL, 0) < 0)
298 {
299 Log(("NetIfList: Failed to get estimate for list size (errno=%d).\n", errno));
300 return RTErrConvertFromErrno(errno);
301 }
302 if ((pBuf = (char*)malloc(cbNeeded)) == NULL)
303 return VERR_NO_MEMORY;
304 if (sysctl(aiMib, 6, pBuf, &cbNeeded, NULL, 0) < 0)
305 {
306 free(pBuf);
307 Log(("NetIfList: Failed to retrieve interface table (errno=%d).\n", errno));
308 return RTErrConvertFromErrno(errno);
309 }
310
311 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
312 if (sock < 0)
313 {
314 free(pBuf);
315 Log(("NetIfList: socket() -> %d\n", errno));
316 return RTErrConvertFromErrno(errno);
317 }
318
319 PDARWINETHERNIC pNIC;
320 PDARWINETHERNIC pEtherNICs = DarwinGetEthernetControllers();
321
322 char *pEnd = pBuf + cbNeeded;
323 for (pNext = pBuf; pNext < pEnd;)
324 {
325 struct if_msghdr *pIfMsg = (struct if_msghdr *)pNext;
326
327 if (pIfMsg->ifm_type != RTM_IFINFO)
328 {
329 Log(("NetIfList: Got message %u while expecting %u.\n",
330 pIfMsg->ifm_type, RTM_IFINFO));
331 rc = VERR_INTERNAL_ERROR;
332 break;
333 }
334 struct sockaddr_dl *pSdl = (struct sockaddr_dl *)(pIfMsg + 1);
335
336 size_t cbNameLen = pSdl->sdl_nlen + 1;
337 Assert(pSdl->sdl_nlen < sizeof(pNIC->szBSDName));
338 for (pNIC = pEtherNICs; pNIC; pNIC = pNIC->pNext)
339 if ( !strncmp(pSdl->sdl_data, pNIC->szBSDName, pSdl->sdl_nlen)
340 && pNIC->szBSDName[pSdl->sdl_nlen] == '\0')
341 {
342 cbNameLen = strlen(pEtherNICs->szName) + 1;
343 break;
344 }
345 PNETIFINFO pNew = (PNETIFINFO)RTMemAllocZ(RT_OFFSETOF(NETIFINFO, szName[cbNameLen]));
346 if (!pNew)
347 {
348 rc = VERR_NO_MEMORY;
349 break;
350 }
351 memcpy(pNew->MACAddress.au8, LLADDR(pSdl), sizeof(pNew->MACAddress.au8));
352 pNew->enmMediumType = NETIF_T_ETHERNET;
353 Assert(sizeof(pNew->szShortName) > pSdl->sdl_nlen);
354 memcpy(pNew->szShortName, pSdl->sdl_data, RT_MIN(pSdl->sdl_nlen, sizeof(pNew->szShortName) - 1));
355
356 /*
357 * If we found the adapter in the list returned by
358 * DarwinGetEthernetControllers() copy the name and UUID from there.
359 */
360 if (pNIC)
361 {
362 memcpy(pNew->szName, pNIC->szName, cbNameLen);
363 pNew->Uuid = pNIC->Uuid;
364 }
365 else
366 {
367 memcpy(pNew->szName, pSdl->sdl_data, pSdl->sdl_nlen);
368 /* Generate UUID from name and MAC address. */
369 RTUUID uuid;
370 RTUuidClear(&uuid);
371 memcpy(&uuid, pNew->szShortName, RT_MIN(cbNameLen, sizeof(uuid)));
372 uuid.Gen.u8ClockSeqHiAndReserved = (uuid.Gen.u8ClockSeqHiAndReserved & 0x3f) | 0x80;
373 uuid.Gen.u16TimeHiAndVersion = (uuid.Gen.u16TimeHiAndVersion & 0x0fff) | 0x4000;
374 memcpy(uuid.Gen.au8Node, pNew->MACAddress.au8, sizeof(uuid.Gen.au8Node));
375 pNew->Uuid = uuid;
376 }
377
378 pNext += pIfMsg->ifm_msglen;
379 while (pNext < pEnd)
380 {
381 struct ifa_msghdr *pIfAddrMsg = (struct ifa_msghdr *)pNext;
382
383 if (pIfAddrMsg->ifam_type != RTM_NEWADDR)
384 break;
385 extractAddressesToNetInfo(pIfAddrMsg->ifam_addrs,
386 (char *)(pIfAddrMsg + 1),
387 pIfAddrMsg->ifam_msglen + (char *)pIfAddrMsg,
388 pNew);
389 pNext += pIfAddrMsg->ifam_msglen;
390 }
391
392 if (pSdl->sdl_type == IFT_ETHER)
393 {
394 struct ifreq IfReq;
395 strcpy(IfReq.ifr_name, pNew->szShortName);
396 if (ioctl(sock, SIOCGIFFLAGS, &IfReq) < 0)
397 {
398 Log(("NetIfList: ioctl(SIOCGIFFLAGS) -> %d\n", errno));
399 pNew->enmStatus = NETIF_S_UNKNOWN;
400 }
401 else
402 pNew->enmStatus = (IfReq.ifr_flags & IFF_UP) ? NETIF_S_UP : NETIF_S_DOWN;
403
404 HostNetworkInterfaceType_T enmType;
405 if (strncmp("vboxnet", pNew->szName, 7))
406 enmType = HostNetworkInterfaceType_Bridged;
407 else
408 enmType = HostNetworkInterfaceType_HostOnly;
409
410 ComObjPtr<HostNetworkInterface> IfObj;
411 IfObj.createObject();
412 try
413 {
414#ifdef DEBUG
415 printf("IF '%s'\n", pNew->szName);
416#endif /* DEBUG */
417 if (SUCCEEDED(IfObj->init(Bstr(pNew->szName), enmType, pNew)))
418 /* Make sure the default interface gets to the beginning. */
419 if (pIfMsg->ifm_index == u16DefaultIface)
420 list.push_front(IfObj);
421 else
422 list.push_back(IfObj);
423 }
424 catch(...)
425 {
426#ifdef DEBUG
427 printf("Throw happens\n");
428#endif /* DEBUG */
429 }
430
431 }
432 RTMemFree(pNew);
433 }
434 for (pNIC = pEtherNICs; pNIC;)
435 {
436 void *pvFree = pNIC;
437 pNIC = pNIC->pNext;
438 RTMemFree(pvFree);
439 }
440 close(sock);
441 free(pBuf);
442 return rc;
443}
444
445int NetIfGetConfigByName(PNETIFINFO pInfo)
446{
447 int rc = VINF_SUCCESS;
448 size_t cbNeeded;
449 char *pBuf, *pNext;
450 int aiMib[6];
451
452 aiMib[0] = CTL_NET;
453 aiMib[1] = PF_ROUTE;
454 aiMib[2] = 0;
455 aiMib[3] = 0; /* address family */
456 aiMib[4] = NET_RT_IFLIST;
457 aiMib[5] = 0;
458
459 if (sysctl(aiMib, 6, NULL, &cbNeeded, NULL, 0) < 0)
460 {
461 Log(("NetIfList: Failed to get estimate for list size (errno=%d).\n", errno));
462 return RTErrConvertFromErrno(errno);
463 }
464 if ((pBuf = (char*)malloc(cbNeeded)) == NULL)
465 return VERR_NO_MEMORY;
466 if (sysctl(aiMib, 6, pBuf, &cbNeeded, NULL, 0) < 0)
467 {
468 free(pBuf);
469 Log(("NetIfList: Failed to retrieve interface table (errno=%d).\n", errno));
470 return RTErrConvertFromErrno(errno);
471 }
472
473 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
474 if (sock < 0)
475 {
476 free(pBuf);
477 Log(("NetIfList: socket() -> %d\n", errno));
478 return RTErrConvertFromErrno(errno);
479 }
480
481 char *pEnd = pBuf + cbNeeded;
482 for (pNext = pBuf; pNext < pEnd;)
483 {
484 struct if_msghdr *pIfMsg = (struct if_msghdr *)pNext;
485
486 if (pIfMsg->ifm_type != RTM_IFINFO)
487 {
488 Log(("NetIfList: Got message %u while expecting %u.\n",
489 pIfMsg->ifm_type, RTM_IFINFO));
490 rc = VERR_INTERNAL_ERROR;
491 break;
492 }
493 struct sockaddr_dl *pSdl = (struct sockaddr_dl *)(pIfMsg + 1);
494
495 bool fSkip = !!strncmp(pInfo->szShortName, pSdl->sdl_data, pSdl->sdl_nlen)
496 || pInfo->szShortName[pSdl->sdl_nlen] != '\0';
497
498 pNext += pIfMsg->ifm_msglen;
499 while (pNext < pEnd)
500 {
501 struct ifa_msghdr *pIfAddrMsg = (struct ifa_msghdr *)pNext;
502
503 if (pIfAddrMsg->ifam_type != RTM_NEWADDR)
504 break;
505 if (!fSkip)
506 extractAddressesToNetInfo(pIfAddrMsg->ifam_addrs,
507 (char *)(pIfAddrMsg + 1),
508 pIfAddrMsg->ifam_msglen + (char *)pIfAddrMsg,
509 pInfo);
510 pNext += pIfAddrMsg->ifam_msglen;
511 }
512
513 if (!fSkip && pSdl->sdl_type == IFT_ETHER)
514 {
515 size_t cbNameLen = pSdl->sdl_nlen + 1;
516 memcpy(pInfo->MACAddress.au8, LLADDR(pSdl), sizeof(pInfo->MACAddress.au8));
517 pInfo->enmMediumType = NETIF_T_ETHERNET;
518 /* Generate UUID from name and MAC address. */
519 RTUUID uuid;
520 RTUuidClear(&uuid);
521 memcpy(&uuid, pInfo->szShortName, RT_MIN(cbNameLen, sizeof(uuid)));
522 uuid.Gen.u8ClockSeqHiAndReserved = (uuid.Gen.u8ClockSeqHiAndReserved & 0x3f) | 0x80;
523 uuid.Gen.u16TimeHiAndVersion = (uuid.Gen.u16TimeHiAndVersion & 0x0fff) | 0x4000;
524 memcpy(uuid.Gen.au8Node, pInfo->MACAddress.au8, sizeof(uuid.Gen.au8Node));
525 pInfo->Uuid = uuid;
526
527 struct ifreq IfReq;
528 strcpy(IfReq.ifr_name, pInfo->szShortName);
529 if (ioctl(sock, SIOCGIFFLAGS, &IfReq) < 0)
530 {
531 Log(("NetIfList: ioctl(SIOCGIFFLAGS) -> %d\n", errno));
532 pInfo->enmStatus = NETIF_S_UNKNOWN;
533 }
534 else
535 pInfo->enmStatus = (IfReq.ifr_flags & IFF_UP) ? NETIF_S_UP : NETIF_S_DOWN;
536
537 return VINF_SUCCESS;
538 }
539 }
540 close(sock);
541 free(pBuf);
542 return rc;
543}
544
545#endif
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