VirtualBox

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

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

Main: coding style: have Main obey the standard VirtualBox coding style rules (no functional changes)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 17.8 KB
Line 
1/* $Id: NetIf-darwin.cpp 21878 2009-07-30 12:42:08Z 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 VERR_INTERNAL_ERROR;
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;
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 for (pNIC = pEtherNICs; pNIC; pNIC = pNIC->pNext)
338 if (!strncmp(pSdl->sdl_data, pNIC->szBSDName, pSdl->sdl_len))
339 {
340 cbNameLen = strlen(pEtherNICs->szName) + 1;
341 break;
342 }
343 PNETIFINFO pNew = (PNETIFINFO)RTMemAllocZ(RT_OFFSETOF(NETIFINFO, szName[cbNameLen]));
344 if (!pNew)
345 {
346 rc = VERR_NO_MEMORY;
347 break;
348 }
349 memcpy(pNew->MACAddress.au8, LLADDR(pSdl), sizeof(pNew->MACAddress.au8));
350 pNew->enmMediumType = NETIF_T_ETHERNET;
351 Assert(sizeof(pNew->szShortName) >= cbNameLen);
352 memcpy(pNew->szShortName, pSdl->sdl_data, cbNameLen);
353 /*
354 * If we found the adapter in the list returned by
355 * DarwinGetEthernetControllers() copy the name and UUID from there.
356 */
357 if (pNIC)
358 {
359 memcpy(pNew->szName, pNIC->szName, cbNameLen);
360 pNew->Uuid = pNIC->Uuid;
361 }
362 else
363 {
364 memcpy(pNew->szName, pSdl->sdl_data, cbNameLen);
365 /* Generate UUID from name and MAC address. */
366 RTUUID uuid;
367 RTUuidClear(&uuid);
368 memcpy(&uuid, pNew->szShortName, RT_MIN(cbNameLen, sizeof(uuid)));
369 uuid.Gen.u8ClockSeqHiAndReserved = (uuid.Gen.u8ClockSeqHiAndReserved & 0x3f) | 0x80;
370 uuid.Gen.u16TimeHiAndVersion = (uuid.Gen.u16TimeHiAndVersion & 0x0fff) | 0x4000;
371 memcpy(uuid.Gen.au8Node, pNew->MACAddress.au8, sizeof(uuid.Gen.au8Node));
372 pNew->Uuid = uuid;
373 }
374
375 pNext += pIfMsg->ifm_msglen;
376 while (pNext < pEnd)
377 {
378 struct ifa_msghdr *pIfAddrMsg = (struct ifa_msghdr *)pNext;
379
380 if (pIfAddrMsg->ifam_type != RTM_NEWADDR)
381 break;
382 extractAddressesToNetInfo(pIfAddrMsg->ifam_addrs,
383 (char *)(pIfAddrMsg + 1),
384 pIfAddrMsg->ifam_msglen + (char *)pIfAddrMsg,
385 pNew);
386 pNext += pIfAddrMsg->ifam_msglen;
387 }
388
389 if (pSdl->sdl_type == IFT_ETHER)
390 {
391 struct ifreq IfReq;
392 strcpy(IfReq.ifr_name, pNew->szShortName);
393 if (ioctl(sock, SIOCGIFFLAGS, &IfReq) < 0)
394 {
395 Log(("NetIfList: ioctl(SIOCGIFFLAGS) -> %d\n", errno));
396 pNew->enmStatus = NETIF_S_UNKNOWN;
397 }
398 else
399 pNew->enmStatus = (IfReq.ifr_flags & IFF_UP) ? NETIF_S_UP : NETIF_S_DOWN;
400
401 HostNetworkInterfaceType_T enmType;
402 if (strncmp("vboxnet", pNew->szName, 7))
403 enmType = HostNetworkInterfaceType_Bridged;
404 else
405 enmType = HostNetworkInterfaceType_HostOnly;
406
407 ComObjPtr<HostNetworkInterface> IfObj;
408 IfObj.createObject();
409 if (SUCCEEDED(IfObj->init(Bstr(pNew->szName), enmType, pNew)))
410 /* Make sure the default interface gets to the beginning. */
411 if (pIfMsg->ifm_index == u16DefaultIface)
412 list.push_front(IfObj);
413 else
414 list.push_back(IfObj);
415 }
416 RTMemFree(pNew);
417 }
418 for (pNIC = pEtherNICs; pNIC;)
419 {
420 void *pvFree = pNIC;
421 pNIC = pNIC->pNext;
422 RTMemFree(pvFree);
423 }
424 close(sock);
425 free(pBuf);
426 return rc;
427}
428
429int NetIfGetConfigByName(PNETIFINFO pInfo)
430{
431 int rc = VINF_SUCCESS;
432 size_t cbNeeded;
433 char *pBuf, *pNext;
434 int aiMib[6];
435
436 aiMib[0] = CTL_NET;
437 aiMib[1] = PF_ROUTE;
438 aiMib[2] = 0;
439 aiMib[3] = 0; /* address family */
440 aiMib[4] = NET_RT_IFLIST;
441 aiMib[5] = 0;
442
443 if (sysctl(aiMib, 6, NULL, &cbNeeded, NULL, 0) < 0)
444 {
445 Log(("NetIfList: Failed to get estimate for list size (errno=%d).\n", errno));
446 return RTErrConvertFromErrno(errno);
447 }
448 if ((pBuf = (char*)malloc(cbNeeded)) == NULL)
449 return VERR_NO_MEMORY;
450 if (sysctl(aiMib, 6, pBuf, &cbNeeded, NULL, 0) < 0)
451 {
452 free(pBuf);
453 Log(("NetIfList: Failed to retrieve interface table (errno=%d).\n", errno));
454 return RTErrConvertFromErrno(errno);
455 }
456
457 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
458 if (sock < 0)
459 {
460 free(pBuf);
461 Log(("NetIfList: socket() -> %d\n", errno));
462 return RTErrConvertFromErrno(errno);
463 }
464
465 char *pEnd = pBuf + cbNeeded;
466 for (pNext = pBuf; pNext < pEnd;)
467 {
468 struct if_msghdr *pIfMsg = (struct if_msghdr *)pNext;
469
470 if (pIfMsg->ifm_type != RTM_IFINFO)
471 {
472 Log(("NetIfList: Got message %u while expecting %u.\n",
473 pIfMsg->ifm_type, RTM_IFINFO));
474 rc = VERR_INTERNAL_ERROR;
475 break;
476 }
477 struct sockaddr_dl *pSdl = (struct sockaddr_dl *)(pIfMsg + 1);
478
479 bool fSkip = !!strcmp(pInfo->szShortName, pSdl->sdl_data);
480
481 pNext += pIfMsg->ifm_msglen;
482 while (pNext < pEnd)
483 {
484 struct ifa_msghdr *pIfAddrMsg = (struct ifa_msghdr *)pNext;
485
486 if (pIfAddrMsg->ifam_type != RTM_NEWADDR)
487 break;
488 if (!fSkip)
489 extractAddressesToNetInfo(pIfAddrMsg->ifam_addrs,
490 (char *)(pIfAddrMsg + 1),
491 pIfAddrMsg->ifam_msglen + (char *)pIfAddrMsg,
492 pInfo);
493 pNext += pIfAddrMsg->ifam_msglen;
494 }
495
496 if (!fSkip && pSdl->sdl_type == IFT_ETHER)
497 {
498 size_t cbNameLen = pSdl->sdl_nlen + 1;
499 memcpy(pInfo->MACAddress.au8, LLADDR(pSdl), sizeof(pInfo->MACAddress.au8));
500 pInfo->enmMediumType = NETIF_T_ETHERNET;
501 /* Generate UUID from name and MAC address. */
502 RTUUID uuid;
503 RTUuidClear(&uuid);
504 memcpy(&uuid, pInfo->szShortName, RT_MIN(cbNameLen, sizeof(uuid)));
505 uuid.Gen.u8ClockSeqHiAndReserved = (uuid.Gen.u8ClockSeqHiAndReserved & 0x3f) | 0x80;
506 uuid.Gen.u16TimeHiAndVersion = (uuid.Gen.u16TimeHiAndVersion & 0x0fff) | 0x4000;
507 memcpy(uuid.Gen.au8Node, pInfo->MACAddress.au8, sizeof(uuid.Gen.au8Node));
508 pInfo->Uuid = uuid;
509
510 struct ifreq IfReq;
511 strcpy(IfReq.ifr_name, pInfo->szShortName);
512 if (ioctl(sock, SIOCGIFFLAGS, &IfReq) < 0)
513 {
514 Log(("NetIfList: ioctl(SIOCGIFFLAGS) -> %d\n", errno));
515 pInfo->enmStatus = NETIF_S_UNKNOWN;
516 }
517 else
518 pInfo->enmStatus = (IfReq.ifr_flags & IFF_UP) ? NETIF_S_UP : NETIF_S_DOWN;
519
520 return VINF_SUCCESS;
521 }
522 }
523 close(sock);
524 free(pBuf);
525 return rc;
526}
527
528#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