VirtualBox

source: vbox/trunk/src/VBox/Main/darwin/NetIfList-darwin.cpp@ 16596

Last change on this file since 16596 was 15548, checked in by vboxsync, 16 years ago

fixed file headers

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.1 KB
Line 
1/* $Id: NetIfList-darwin.cpp 15548 2008-12-15 21:33:35Z 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#define LOG_GROUP LOG_GROUP_MAIN
28
29#include <iprt/err.h>
30#include <iprt/alloc.h>
31
32#include <string.h>
33#include <sys/socket.h>
34#include <sys/ioctl.h>
35#include <netinet/in.h>
36#include <net/if.h>
37#include <ifaddrs.h>
38#include <errno.h>
39#include <unistd.h>
40#include <list>
41
42#include "HostNetworkInterfaceImpl.h"
43#include "netif.h"
44#include "iokit.h"
45#include "Logging.h"
46
47int NetIfList(std::list <ComObjPtr <HostNetworkInterface> > &list)
48{
49 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
50 if (sock < 0)
51 {
52 Log(("NetIfList: socket() -> %d\n", errno));
53 return NULL;
54 }
55 struct ifaddrs *IfAddrs, *pAddr;
56 int rc = getifaddrs(&IfAddrs);
57 if (rc)
58 {
59 close(sock);
60 Log(("NetIfList: getifaddrs() -> %d\n", rc));
61 return VERR_INTERNAL_ERROR;
62 }
63
64 PDARWINETHERNIC pEtherNICs = DarwinGetEthernetControllers();
65 while (pEtherNICs)
66 {
67 size_t cbNameLen = strlen(pEtherNICs->szName) + 1;
68 PNETIFINFO pNew = (PNETIFINFO)RTMemAllocZ(RT_OFFSETOF(NETIFINFO, szName[cbNameLen]));
69 pNew->MACAddress = pEtherNICs->Mac;
70 pNew->enmType = NETIF_T_ETHERNET;
71 pNew->Uuid = pEtherNICs->Uuid;
72 Assert(sizeof(pNew->szShortName) > sizeof(pEtherNICs->szBSDName));
73 memcpy(pNew->szShortName, pEtherNICs->szBSDName, sizeof(pEtherNICs->szBSDName));
74 pNew->szShortName[sizeof(pEtherNICs->szBSDName)] = '\0';
75 memcpy(pNew->szName, pEtherNICs->szName, cbNameLen);
76
77 struct ifreq IfReq;
78 strcpy(IfReq.ifr_name, pNew->szShortName);
79 if (ioctl(sock, SIOCGIFFLAGS, &IfReq) < 0)
80 {
81 Log(("NetIfList: ioctl(SIOCGIFFLAGS) -> %d\n", errno));
82 pNew->enmStatus = NETIF_S_UNKNOWN;
83 }
84 else
85 pNew->enmStatus = (IfReq.ifr_flags & IFF_UP) ? NETIF_S_UP : NETIF_S_DOWN;
86
87 for (pAddr = IfAddrs; pAddr != NULL; pAddr = pAddr->ifa_next)
88 {
89 if (strcmp(pNew->szShortName, pAddr->ifa_name))
90 continue;
91
92 struct sockaddr_in *pIPAddr, *pIPNetMask;
93 struct sockaddr_in6 *pIPv6Addr, *pIPv6NetMask;
94
95 switch (pAddr->ifa_addr->sa_family)
96 {
97 case AF_INET:
98 if (pNew->IPAddress.u)
99 break;
100 pIPAddr = (struct sockaddr_in *)pAddr->ifa_addr;
101 Assert(sizeof(pNew->IPAddress) == sizeof(pIPAddr->sin_addr));
102 pNew->IPAddress.u = pIPAddr->sin_addr.s_addr;
103 pIPNetMask = (struct sockaddr_in *)pAddr->ifa_netmask;
104 Assert(pIPNetMask->sin_family == AF_INET);
105 Assert(sizeof(pNew->IPNetMask) == sizeof(pIPNetMask->sin_addr));
106 pNew->IPNetMask.u = pIPNetMask->sin_addr.s_addr;
107 break;
108 case AF_INET6:
109 if (pNew->IPv6Address.s.Lo || pNew->IPv6Address.s.Hi)
110 break;
111 pIPv6Addr = (struct sockaddr_in6 *)pAddr->ifa_addr;
112 Assert(sizeof(pNew->IPv6Address) == sizeof(pIPv6Addr->sin6_addr));
113 memcpy(pNew->IPv6Address.au8,
114 pIPv6Addr->sin6_addr.__u6_addr.__u6_addr8,
115 sizeof(pNew->IPv6Address));
116 pIPv6NetMask = (struct sockaddr_in6 *)pAddr->ifa_netmask;
117 Assert(pIPv6NetMask->sin6_family == AF_INET6);
118 Assert(sizeof(pNew->IPv6NetMask) == sizeof(pIPv6NetMask->sin6_addr));
119 memcpy(pNew->IPv6NetMask.au8,
120 pIPv6NetMask->sin6_addr.__u6_addr.__u6_addr8,
121 sizeof(pNew->IPv6NetMask));
122 break;
123 }
124 }
125
126 ComObjPtr<HostNetworkInterface> IfObj;
127 IfObj.createObject();
128 if (SUCCEEDED(IfObj->init(Bstr(pEtherNICs->szName), pNew)))
129 list.push_back(IfObj);
130 RTMemFree(pNew);
131
132 /* next, free current */
133 void *pvFree = pEtherNICs;
134 pEtherNICs = pEtherNICs->pNext;
135 RTMemFree(pvFree);
136 }
137
138 freeifaddrs(IfAddrs);
139 close(sock);
140 return VINF_SUCCESS;
141}
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