VirtualBox

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

Last change on this file since 15462 was 15442, checked in by vboxsync, 16 years ago

#3282 HostNetIf API: updated common part, added draft implementation for Solaris.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.5 KB
Line 
1/* $Id: NetIfList-darwin.cpp 15442 2008-12-13 13:40:42Z 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 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 *
26 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31
32
33/*******************************************************************************
34* Header Files *
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 <netinet/in.h>
45#include <net/if.h>
46#include <ifaddrs.h>
47#include <errno.h>
48#include <unistd.h>
49#include <list>
50
51#include "HostNetworkInterfaceImpl.h"
52#include "netif.h"
53#include "iokit.h"
54#include "Logging.h"
55
56int NetIfList(std::list <ComObjPtr <HostNetworkInterface> > &list)
57{
58 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
59 if (sock < 0)
60 {
61 Log(("NetIfList: socket() -> %d\n", errno));
62 return NULL;
63 }
64 struct ifaddrs *IfAddrs, *pAddr;
65 int rc = getifaddrs(&IfAddrs);
66 if (rc)
67 {
68 close(sock);
69 Log(("NetIfList: getifaddrs() -> %d\n", rc));
70 return NULL;
71 }
72
73 PDARWINETHERNIC pEtherNICs = DarwinGetEthernetControllers();
74 while (pEtherNICs)
75 {
76 size_t cbNameLen = strlen(pEtherNICs->szName) + 1;
77 PNETIFINFO pNew = (PNETIFINFO)RTMemAllocZ(RT_OFFSETOF(NETIFINFO, szName[cbNameLen]));
78 pNew->MACAddress = pEtherNICs->Mac;
79 pNew->enmType = NETIF_T_ETHERNET;
80 pNew->Uuid = pEtherNICs->Uuid;
81 Assert(sizeof(pNew->szShortName) > sizeof(pEtherNICs->szBSDName));
82 memcpy(pNew->szShortName, pEtherNICs->szBSDName, sizeof(pEtherNICs->szBSDName));
83 pNew->szShortName[sizeof(pEtherNICs->szBSDName)] = '\0';
84 memcpy(pNew->szName, pEtherNICs->szName, cbNameLen);
85
86 struct ifreq IfReq;
87 strcpy(IfReq.ifr_name, pNew->szShortName);
88 if (ioctl(sock, SIOCGIFFLAGS, &IfReq) < 0)
89 {
90 Log(("NetIfList: ioctl(SIOCGIFFLAGS) -> %d\n", errno));
91 pNew->enmStatus = NETIF_S_UNKNOWN;
92 }
93 else
94 pNew->enmStatus = (IfReq.ifr_flags & IFF_UP) ? NETIF_S_UP : NETIF_S_DOWN;
95
96 for (pAddr = IfAddrs; pAddr != NULL; pAddr = pAddr->ifa_next)
97 {
98 if (strcmp(pNew->szShortName, pAddr->ifa_name))
99 continue;
100
101 struct sockaddr_in *pIPAddr, *pIPNetMask;
102 struct sockaddr_in6 *pIPv6Addr, *pIPv6NetMask;
103
104 switch (pAddr->ifa_addr->sa_family)
105 {
106 case AF_INET:
107 if (pNew->IPAddress.u)
108 break;
109 pIPAddr = (struct sockaddr_in *)pAddr->ifa_addr;
110 Assert(sizeof(pNew->IPAddress) == sizeof(pIPAddr->sin_addr));
111 pNew->IPAddress.u = pIPAddr->sin_addr.s_addr;
112 pIPNetMask = (struct sockaddr_in *)pAddr->ifa_netmask;
113 Assert(pIPNetMask->sin_family == AF_INET);
114 Assert(sizeof(pNew->IPNetMask) == sizeof(pIPNetMask->sin_addr));
115 pNew->IPNetMask.u = pIPNetMask->sin_addr.s_addr;
116 break;
117 case AF_INET6:
118 if (pNew->IPv6Address.s.Lo || pNew->IPv6Address.s.Hi)
119 break;
120 pIPv6Addr = (struct sockaddr_in6 *)pAddr->ifa_addr;
121 Assert(sizeof(pNew->IPv6Address) == sizeof(pIPv6Addr->sin6_addr));
122 memcpy(pNew->IPv6Address.au8,
123 pIPv6Addr->sin6_addr.__u6_addr.__u6_addr8,
124 sizeof(pNew->IPv6Address));
125 pIPv6NetMask = (struct sockaddr_in6 *)pAddr->ifa_netmask;
126 Assert(pIPv6NetMask->sin6_family == AF_INET6);
127 Assert(sizeof(pNew->IPv6NetMask) == sizeof(pIPv6NetMask->sin6_addr));
128 memcpy(pNew->IPv6NetMask.au8,
129 pIPv6NetMask->sin6_addr.__u6_addr.__u6_addr8,
130 sizeof(pNew->IPv6NetMask));
131 break;
132 }
133 }
134
135 ComObjPtr<HostNetworkInterface> IfObj;
136 IfObj.createObject();
137 if (SUCCEEDED(IfObj->init(Bstr(pEtherNICs->szName), pNew)))
138 list.push_back(IfObj);
139 RTMemFree(pNew);
140
141 /* next, free current */
142 void *pvFree = pEtherNICs;
143 pEtherNICs = pEtherNICs->pNext;
144 RTMemFree(pvFree);
145 }
146
147 freeifaddrs(IfAddrs);
148 close(sock);
149 return pIfs;
150}
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