VirtualBox

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

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

Main: tabs -> spaces.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 18.1 KB
Line 
1/* $Id: NetIf-darwin.cpp 26511 2010-02-14 09:39:55Z 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 if (SUCCEEDED(IfObj->init(Bstr(pNew->szName), enmType, pNew)))
413 /* Make sure the default interface gets to the beginning. */
414 if (pIfMsg->ifm_index == u16DefaultIface)
415 list.push_front(IfObj);
416 else
417 list.push_back(IfObj);
418 }
419 RTMemFree(pNew);
420 }
421 for (pNIC = pEtherNICs; pNIC;)
422 {
423 void *pvFree = pNIC;
424 pNIC = pNIC->pNext;
425 RTMemFree(pvFree);
426 }
427 close(sock);
428 free(pBuf);
429 return rc;
430}
431
432int NetIfGetConfigByName(PNETIFINFO pInfo)
433{
434 int rc = VINF_SUCCESS;
435 size_t cbNeeded;
436 char *pBuf, *pNext;
437 int aiMib[6];
438
439 aiMib[0] = CTL_NET;
440 aiMib[1] = PF_ROUTE;
441 aiMib[2] = 0;
442 aiMib[3] = 0; /* address family */
443 aiMib[4] = NET_RT_IFLIST;
444 aiMib[5] = 0;
445
446 if (sysctl(aiMib, 6, NULL, &cbNeeded, NULL, 0) < 0)
447 {
448 Log(("NetIfList: Failed to get estimate for list size (errno=%d).\n", errno));
449 return RTErrConvertFromErrno(errno);
450 }
451 if ((pBuf = (char*)malloc(cbNeeded)) == NULL)
452 return VERR_NO_MEMORY;
453 if (sysctl(aiMib, 6, pBuf, &cbNeeded, NULL, 0) < 0)
454 {
455 free(pBuf);
456 Log(("NetIfList: Failed to retrieve interface table (errno=%d).\n", errno));
457 return RTErrConvertFromErrno(errno);
458 }
459
460 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
461 if (sock < 0)
462 {
463 free(pBuf);
464 Log(("NetIfList: socket() -> %d\n", errno));
465 return RTErrConvertFromErrno(errno);
466 }
467
468 char *pEnd = pBuf + cbNeeded;
469 for (pNext = pBuf; pNext < pEnd;)
470 {
471 struct if_msghdr *pIfMsg = (struct if_msghdr *)pNext;
472
473 if (pIfMsg->ifm_type != RTM_IFINFO)
474 {
475 Log(("NetIfList: Got message %u while expecting %u.\n",
476 pIfMsg->ifm_type, RTM_IFINFO));
477 rc = VERR_INTERNAL_ERROR;
478 break;
479 }
480 struct sockaddr_dl *pSdl = (struct sockaddr_dl *)(pIfMsg + 1);
481
482 bool fSkip = !!strncmp(pInfo->szShortName, pSdl->sdl_data, pSdl->sdl_nlen)
483 || pInfo->szShortName[pSdl->sdl_nlen] != '\0';
484
485 pNext += pIfMsg->ifm_msglen;
486 while (pNext < pEnd)
487 {
488 struct ifa_msghdr *pIfAddrMsg = (struct ifa_msghdr *)pNext;
489
490 if (pIfAddrMsg->ifam_type != RTM_NEWADDR)
491 break;
492 if (!fSkip)
493 extractAddressesToNetInfo(pIfAddrMsg->ifam_addrs,
494 (char *)(pIfAddrMsg + 1),
495 pIfAddrMsg->ifam_msglen + (char *)pIfAddrMsg,
496 pInfo);
497 pNext += pIfAddrMsg->ifam_msglen;
498 }
499
500 if (!fSkip && pSdl->sdl_type == IFT_ETHER)
501 {
502 size_t cbNameLen = pSdl->sdl_nlen + 1;
503 memcpy(pInfo->MACAddress.au8, LLADDR(pSdl), sizeof(pInfo->MACAddress.au8));
504 pInfo->enmMediumType = NETIF_T_ETHERNET;
505 /* Generate UUID from name and MAC address. */
506 RTUUID uuid;
507 RTUuidClear(&uuid);
508 memcpy(&uuid, pInfo->szShortName, RT_MIN(cbNameLen, sizeof(uuid)));
509 uuid.Gen.u8ClockSeqHiAndReserved = (uuid.Gen.u8ClockSeqHiAndReserved & 0x3f) | 0x80;
510 uuid.Gen.u16TimeHiAndVersion = (uuid.Gen.u16TimeHiAndVersion & 0x0fff) | 0x4000;
511 memcpy(uuid.Gen.au8Node, pInfo->MACAddress.au8, sizeof(uuid.Gen.au8Node));
512 pInfo->Uuid = uuid;
513
514 struct ifreq IfReq;
515 strcpy(IfReq.ifr_name, pInfo->szShortName);
516 if (ioctl(sock, SIOCGIFFLAGS, &IfReq) < 0)
517 {
518 Log(("NetIfList: ioctl(SIOCGIFFLAGS) -> %d\n", errno));
519 pInfo->enmStatus = NETIF_S_UNKNOWN;
520 }
521 else
522 pInfo->enmStatus = (IfReq.ifr_flags & IFF_UP) ? NETIF_S_UP : NETIF_S_DOWN;
523
524 return VINF_SUCCESS;
525 }
526 }
527 close(sock);
528 free(pBuf);
529 return rc;
530}
531
532#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