VirtualBox

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

Last change on this file since 22266 was 22180, checked in by vboxsync, 15 years ago

Fixed failure in CreateHostOnlyNetworkInterface() on linux (no GUID).

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