VirtualBox

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

Last change on this file since 23454 was 23203, checked in by vboxsync, 15 years ago

Main/NetIf-linux: don't crash VBoxSVC if /proc/net/route couldn't be opened

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