NetIf-darwin.cpp revision 22db201c588c7242f5f3cd47d999225cefad6475
b9fc40283b616e122f07c8f48b0bf9c6afc3fab2vboxsync * Main - NetIfList, Darwin implementation.
b9fc40283b616e122f07c8f48b0bf9c6afc3fab2vboxsync * Copyright (C) 2008 Sun Microsystems, Inc.
b9fc40283b616e122f07c8f48b0bf9c6afc3fab2vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
b9fc40283b616e122f07c8f48b0bf9c6afc3fab2vboxsync * available from http://www.virtualbox.org. This file is free software;
b9fc40283b616e122f07c8f48b0bf9c6afc3fab2vboxsync * you can redistribute it and/or modify it under the terms of the GNU
b9fc40283b616e122f07c8f48b0bf9c6afc3fab2vboxsync * General Public License (GPL) as published by the Free Software
b9fc40283b616e122f07c8f48b0bf9c6afc3fab2vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
b9fc40283b616e122f07c8f48b0bf9c6afc3fab2vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
b9fc40283b616e122f07c8f48b0bf9c6afc3fab2vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
b9fc40283b616e122f07c8f48b0bf9c6afc3fab2vboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
b9fc40283b616e122f07c8f48b0bf9c6afc3fab2vboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
b9fc40283b616e122f07c8f48b0bf9c6afc3fab2vboxsync * additional information or have any questions.
b9fc40283b616e122f07c8f48b0bf9c6afc3fab2vboxsync/*******************************************************************************
b9fc40283b616e122f07c8f48b0bf9c6afc3fab2vboxsync* Header Files *
b9fc40283b616e122f07c8f48b0bf9c6afc3fab2vboxsync*******************************************************************************/
b9fc40283b616e122f07c8f48b0bf9c6afc3fab2vboxsync * Deal with conflicts first.
b9fc40283b616e122f07c8f48b0bf9c6afc3fab2vboxsync * PVM - BSD mess, that FreeBSD has correct a long time ago.
b9fc40283b616e122f07c8f48b0bf9c6afc3fab2vboxsync * iprt/types.h before sys/param.h - prevents UINT32_C and friends.
b9fc40283b616e122f07c8f48b0bf9c6afc3fab2vboxsyncint NetIfList(std::list <ComObjPtr <HostNetworkInterface> > &list)
b9fc40283b616e122f07c8f48b0bf9c6afc3fab2vboxsync int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
b9fc40283b616e122f07c8f48b0bf9c6afc3fab2vboxsync PDARWINETHERNIC pEtherNICs = DarwinGetEthernetControllers();
b9fc40283b616e122f07c8f48b0bf9c6afc3fab2vboxsync PNETIFINFO pNew = (PNETIFINFO)RTMemAllocZ(RT_OFFSETOF(NETIFINFO, szName[cbNameLen]));
b9fc40283b616e122f07c8f48b0bf9c6afc3fab2vboxsync Assert(sizeof(pNew->szShortName) > sizeof(pEtherNICs->szBSDName));
b9fc40283b616e122f07c8f48b0bf9c6afc3fab2vboxsync memcpy(pNew->szShortName, pEtherNICs->szBSDName, sizeof(pEtherNICs->szBSDName));
b9fc40283b616e122f07c8f48b0bf9c6afc3fab2vboxsync pNew->szShortName[sizeof(pEtherNICs->szBSDName)] = '\0';
b9fc40283b616e122f07c8f48b0bf9c6afc3fab2vboxsync memcpy(pNew->szName, pEtherNICs->szName, cbNameLen);
b9fc40283b616e122f07c8f48b0bf9c6afc3fab2vboxsync Log(("NetIfList: ioctl(SIOCGIFFLAGS) -> %d\n", errno));
b9fc40283b616e122f07c8f48b0bf9c6afc3fab2vboxsync pNew->enmStatus = (IfReq.ifr_flags & IFF_UP) ? NETIF_S_UP : NETIF_S_DOWN;
b9fc40283b616e122f07c8f48b0bf9c6afc3fab2vboxsync for (pAddr = IfAddrs; pAddr != NULL; pAddr = pAddr->ifa_next)
b9fc40283b616e122f07c8f48b0bf9c6afc3fab2vboxsync Assert(sizeof(pNew->IPAddress) == sizeof(pIPAddr->sin_addr));
b9fc40283b616e122f07c8f48b0bf9c6afc3fab2vboxsync pIPNetMask = (struct sockaddr_in *)pAddr->ifa_netmask;
b9fc40283b616e122f07c8f48b0bf9c6afc3fab2vboxsync Assert(sizeof(pNew->IPNetMask) == sizeof(pIPNetMask->sin_addr));
b9fc40283b616e122f07c8f48b0bf9c6afc3fab2vboxsync if (pNew->IPv6Address.s.Lo || pNew->IPv6Address.s.Hi)
b9fc40283b616e122f07c8f48b0bf9c6afc3fab2vboxsync pIPv6Addr = (struct sockaddr_in6 *)pAddr->ifa_addr;
b9fc40283b616e122f07c8f48b0bf9c6afc3fab2vboxsync Assert(sizeof(pNew->IPv6Address) == sizeof(pIPv6Addr->sin6_addr));
b9fc40283b616e122f07c8f48b0bf9c6afc3fab2vboxsync pIPv6NetMask = (struct sockaddr_in6 *)pAddr->ifa_netmask;
b9fc40283b616e122f07c8f48b0bf9c6afc3fab2vboxsync Assert(sizeof(pNew->IPv6NetMask) == sizeof(pIPv6NetMask->sin6_addr));
b9fc40283b616e122f07c8f48b0bf9c6afc3fab2vboxsync if (SUCCEEDED(IfObj->init(Bstr(pEtherNICs->szName), HostNetworkInterfaceType_Bridged, pNew)))
b9fc40283b616e122f07c8f48b0bf9c6afc3fab2vboxsync /* next, free current */
b9fc40283b616e122f07c8f48b0bf9c6afc3fab2vboxsync ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
b9fc40283b616e122f07c8f48b0bf9c6afc3fab2vboxsyncvoid extractAddresses(int iAddrMask, caddr_t cp, caddr_t cplim, PNETIFINFO pInfo)
b9fc40283b616e122f07c8f48b0bf9c6afc3fab2vboxsync pInfo->IPAddress.u = ((struct sockaddr_in *)addresses[RTAX_IFA])->sin_addr.s_addr;
b9fc40283b616e122f07c8f48b0bf9c6afc3fab2vboxsync pInfo->IPNetMask.u = ((struct sockaddr_in *)addresses[RTAX_NETMASK])->sin_addr.s_addr;
b9fc40283b616e122f07c8f48b0bf9c6afc3fab2vboxsync if (!pInfo->IPv6Address.s.Lo && !pInfo->IPv6Address.s.Hi)
b9fc40283b616e122f07c8f48b0bf9c6afc3fab2vboxsync ((struct sockaddr_in6 *)addresses[RTAX_IFA])->sin6_addr.__u6_addr.__u6_addr8,
b9fc40283b616e122f07c8f48b0bf9c6afc3fab2vboxsync ((struct sockaddr_in6 *)addresses[RTAX_NETMASK])->sin6_addr.__u6_addr.__u6_addr8,
b9fc40283b616e122f07c8f48b0bf9c6afc3fab2vboxsync Log(("NetIfList: Unsupported address family: %u\n", sa->sa_family));
b9fc40283b616e122f07c8f48b0bf9c6afc3fab2vboxsyncint NetIfList(std::list <ComObjPtr <HostNetworkInterface> > &list)
b9fc40283b616e122f07c8f48b0bf9c6afc3fab2vboxsync if (sysctl(aiMib, 6, NULL, &cbNeeded, NULL, 0) < 0)
b9fc40283b616e122f07c8f48b0bf9c6afc3fab2vboxsync Log(("NetIfList: Failed to get estimate for list size (errno=%d).\n", errno));
b9fc40283b616e122f07c8f48b0bf9c6afc3fab2vboxsync if (sysctl(aiMib, 6, pBuf, &cbNeeded, NULL, 0) < 0)
b9fc40283b616e122f07c8f48b0bf9c6afc3fab2vboxsync Log(("NetIfList: Failed to retrieve interface table (errno=%d).\n", errno));
b9fc40283b616e122f07c8f48b0bf9c6afc3fab2vboxsync int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
b9fc40283b616e122f07c8f48b0bf9c6afc3fab2vboxsync PDARWINETHERNIC pEtherNICs = DarwinGetEthernetControllers();
b9fc40283b616e122f07c8f48b0bf9c6afc3fab2vboxsync struct if_msghdr *pIfMsg = (struct if_msghdr *)pNext;
if (!pNew)
if (pNIC)
extractAddresses(pIfAddrMsg->ifam_addrs, (char *)(pIfAddrMsg + 1), pIfAddrMsg->ifam_msglen + (char *)pIfAddrMsg, pNew);
return rc;