VirtualBox

source: vbox/trunk/src/VBox/Main/linux/NetIf-linux.cpp@ 21525

Last change on this file since 21525 was 20481, checked in by vboxsync, 15 years ago

#3773: Detection of primary NIC for bridging in Linux.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.4 KB
Line 
1/* $Id: NetIf-linux.cpp 20481 2009-06-11 19:30:12Z vboxsync $ */
2/** @file
3 * Main - NetIfList, Linux 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 <list>
31#include <sys/ioctl.h>
32#include <net/if.h>
33#include <net/if_arp.h>
34#include <net/route.h>
35#include <netinet/in.h>
36#include <stdio.h>
37#include <unistd.h>
38#include <iprt/asm.h>
39
40#include "HostNetworkInterfaceImpl.h"
41#include "netif.h"
42#include "Logging.h"
43
44static int getDefaultIfaceName(char *pszName)
45{
46 FILE *fp = fopen("/proc/net/route", "r");
47 char szBuf[1024];
48 char szIfName[17];
49 char szAddr[129];
50 char szGateway[129];
51 char szMask[129];
52 int iTmp;
53 int iFlags;
54
55 while (fgets(szBuf, sizeof(szBuf)-1, fp))
56 {
57 int n = sscanf(szBuf, "%16s %128s %128s %X %d %d %d %128s %d %d %d\n",
58 szIfName, szAddr, szGateway, &iFlags, &iTmp, &iTmp, &iTmp,
59 szMask, &iTmp, &iTmp, &iTmp);
60 if (n < 10 || !(iFlags & RTF_UP))
61 continue;
62
63 if (strcmp(szAddr, "00000000") == 0 && strcmp(szMask, "00000000") == 0)
64 {
65 fclose(fp);
66 strncpy(pszName, szIfName, 16);
67 pszName[16] = 0;
68 return VINF_SUCCESS;
69 }
70 }
71 return VERR_INTERNAL_ERROR;
72}
73
74static int getInterfaceInfo(int iSocket, const char *pszName, PNETIFINFO pInfo)
75{
76 memset(pInfo, 0, sizeof(*pInfo));
77 struct ifreq Req;
78 memset(&Req, 0, sizeof(Req));
79 strncpy(Req.ifr_name, pszName, sizeof(Req.ifr_name) - 1);
80 if (ioctl(iSocket, SIOCGIFHWADDR, &Req) >= 0)
81 {
82 switch (Req.ifr_hwaddr.sa_family)
83 {
84 case ARPHRD_ETHER:
85 pInfo->enmMediumType = NETIF_T_ETHERNET;
86 break;
87 default:
88 pInfo->enmMediumType = NETIF_T_UNKNOWN;
89 break;
90 }
91 /* Generate UUID from name and MAC address. */
92 RTUUID uuid;
93 RTUuidClear(&uuid);
94 memcpy(&uuid, Req.ifr_name, RT_MIN(sizeof(Req.ifr_name), sizeof(uuid)));
95 uuid.Gen.u8ClockSeqHiAndReserved = (uuid.Gen.u8ClockSeqHiAndReserved & 0x3f) | 0x80;
96 uuid.Gen.u16TimeHiAndVersion = (uuid.Gen.u16TimeHiAndVersion & 0x0fff) | 0x4000;
97 memcpy(uuid.Gen.au8Node, &Req.ifr_hwaddr.sa_data, sizeof(uuid.Gen.au8Node));
98 pInfo->Uuid = uuid;
99
100 memcpy(&pInfo->MACAddress, Req.ifr_hwaddr.sa_data, sizeof(pInfo->MACAddress));
101
102 if (ioctl(iSocket, SIOCGIFADDR, &Req) >= 0)
103 memcpy(pInfo->IPAddress.au8,
104 &((struct sockaddr_in *)&Req.ifr_addr)->sin_addr.s_addr,
105 sizeof(pInfo->IPAddress.au8));
106
107 if (ioctl(iSocket, SIOCGIFNETMASK, &Req) >= 0)
108 memcpy(pInfo->IPNetMask.au8,
109 &((struct sockaddr_in *)&Req.ifr_addr)->sin_addr.s_addr,
110 sizeof(pInfo->IPNetMask.au8));
111
112 if (ioctl(iSocket, SIOCGIFFLAGS, &Req) >= 0)
113 pInfo->enmStatus = Req.ifr_flags & IFF_UP ? NETIF_S_UP : NETIF_S_DOWN;
114
115 FILE *fp = fopen("/proc/net/if_inet6", "r");
116 if (fp)
117 {
118 RTNETADDRIPV6 IPv6Address;
119 unsigned uIndex, uLength, uScope, uTmp;
120 char szName[30];
121 for (;;)
122 {
123 memset(szName, 0, sizeof(szName));
124 int n = fscanf(fp,
125 "%08x%08x%08x%08x"
126 " %02x %02x %02x %02x %20s\n",
127 &IPv6Address.au32[0], &IPv6Address.au32[1],
128 &IPv6Address.au32[2], &IPv6Address.au32[3],
129 &uIndex, &uLength, &uScope, &uTmp, szName);
130 if (n == EOF)
131 break;
132 if (n != 9 || uLength > 128)
133 {
134 Log(("getInterfaceInfo: Error while reading /proc/net/if_inet6, n=%d uLength=%u\n",
135 n, uLength));
136 break;
137 }
138 if (!strcmp(Req.ifr_name, szName))
139 {
140 pInfo->IPv6Address.au32[0] = htonl(IPv6Address.au32[0]);
141 pInfo->IPv6Address.au32[1] = htonl(IPv6Address.au32[1]);
142 pInfo->IPv6Address.au32[2] = htonl(IPv6Address.au32[2]);
143 pInfo->IPv6Address.au32[3] = htonl(IPv6Address.au32[3]);
144 ASMBitSetRange(&pInfo->IPv6NetMask, 0, uLength);
145 }
146 }
147 fclose(fp);
148 }
149 }
150 return VINF_SUCCESS;
151}
152
153int NetIfList(std::list <ComObjPtr <HostNetworkInterface> > &list)
154{
155 char szDefaultIface[256];
156 int rc = getDefaultIfaceName(szDefaultIface);
157 if (RT_FAILURE(rc))
158 {
159 Log(("NetIfList: Failed to find default interface.\n"));
160 szDefaultIface[0] = 0;
161 }
162 int sock = socket(AF_INET, SOCK_DGRAM, 0);
163 if (sock >= 0)
164 {
165 FILE *fp = fopen("/proc/net/dev", "r");
166 if (fp)
167 {
168 char buf[256];
169 while (fgets(buf, sizeof(buf), fp))
170 {
171 char *pszEndOfName = strchr(buf, ':');
172 if (!pszEndOfName)
173 continue;
174 *pszEndOfName = 0;
175 int iFirstNonWS = strspn(buf, " ");
176 char *pszName = buf+iFirstNonWS;
177 NETIFINFO Info;
178 rc = getInterfaceInfo(sock, pszName, &Info);
179 if (RT_FAILURE(rc))
180 break;
181 if (Info.enmMediumType == NETIF_T_ETHERNET)
182 {
183 ComObjPtr<HostNetworkInterface> IfObj;
184 IfObj.createObject();
185
186 HostNetworkInterfaceType_T enmType;
187 if (strncmp("vboxnet", pszName, 7))
188 enmType = HostNetworkInterfaceType_Bridged;
189 else
190 enmType = HostNetworkInterfaceType_HostOnly;
191
192 if (SUCCEEDED(IfObj->init(Bstr(pszName), enmType, &Info)))
193 {
194 if (strcmp(pszName, szDefaultIface) == 0)
195 list.push_front(IfObj);
196 else
197 list.push_back(IfObj);
198 }
199 }
200
201 }
202 fclose(fp);
203 }
204 close(sock);
205 }
206 else
207 rc = VERR_INTERNAL_ERROR;
208
209 return rc;
210}
211
212int NetIfGetConfigByName(PNETIFINFO pInfo)
213{
214 int rc = VINF_SUCCESS;
215 int sock = socket(AF_INET, SOCK_DGRAM, 0);
216 if (sock < 0)
217 return VERR_NOT_IMPLEMENTED;
218 rc = getInterfaceInfo(sock, pInfo->szShortName, pInfo);
219 close(sock);
220 return rc;
221}
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