VirtualBox

source: vbox/trunk/src/VBox/Main/linux/NetIfList-linux.cpp@ 15463

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

#3282 HostNetIf API: 64-bit Linux tested. Enabled HostNetIf API by default for Darwin, Solaris and Linux.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.3 KB
Line 
1/* $Id: NetIfList-linux.cpp 15463 2008-12-14 14:09:06Z 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.215389.xyz. 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 <list>
40#include <sys/ioctl.h>
41#include <net/if.h>
42#include <net/if_arp.h>
43#include <netinet/in.h>
44#include <stdio.h>
45#include <unistd.h>
46
47#include "HostNetworkInterfaceImpl.h"
48#include "netif.h"
49
50int NetIfList(std::list <ComObjPtr <HostNetworkInterface> > &list)
51{
52 int sock = socket(AF_INET, SOCK_DGRAM, 0);
53 if (sock >= 0)
54 {
55 char pBuffer[2048];
56 struct ifconf ifConf;
57 ifConf.ifc_len = sizeof(pBuffer);
58 ifConf.ifc_buf = pBuffer;
59 if (ioctl(sock, SIOCGIFCONF, &ifConf) >= 0)
60 {
61 for (struct ifreq *pReq = ifConf.ifc_req; (char*)pReq < pBuffer + ifConf.ifc_len; pReq++)
62 {
63 if (ioctl(sock, SIOCGIFHWADDR, pReq) >= 0)
64 {
65 if (pReq->ifr_hwaddr.sa_family == ARPHRD_ETHER)
66 {
67 NETIFINFO Info;
68 memset(&Info, 0, sizeof(Info));
69 Info.enmType = NETIF_T_ETHERNET;
70 /* Pick up some garbage from stack. */
71 RTUUID uuid;
72 Assert(sizeof(uuid) <= sizeof(*pReq));
73 memcpy(uuid.Gen.au8Node, &pReq->ifr_hwaddr.sa_data, sizeof(uuid.Gen.au8Node));
74 Info.Uuid = uuid;
75 memcpy(&Info.MACAddress, pReq->ifr_hwaddr.sa_data, sizeof(Info.MACAddress));
76
77 if (ioctl(sock, SIOCGIFADDR, pReq) >= 0)
78 memcpy(Info.IPAddress.au8,
79 &((struct sockaddr_in *)&pReq->ifr_addr)->sin_addr.s_addr,
80 sizeof(Info.IPAddress.au8));
81
82 if (ioctl(sock, SIOCGIFNETMASK, pReq) >= 0)
83 memcpy(Info.IPNetMask.au8,
84 &((struct sockaddr_in *)&pReq->ifr_addr)->sin_addr.s_addr,
85 sizeof(Info.IPNetMask.au8));
86
87 if (ioctl(sock, SIOCGIFFLAGS, pReq) >= 0)
88 Info.enmStatus = pReq->ifr_flags & IFF_UP ? NETIF_S_UP : NETIF_S_DOWN;
89
90 FILE *fp = fopen("/proc/net/if_inet6", "r");
91 if (fp)
92 {
93 RTNETADDRIPV6 IPv6Address;
94 unsigned uIndex, uLength, uScope, uTmp;
95 char szName[30];
96 while (fscanf(fp,
97 "%08x%08x%08x%08x"
98 " %02x %02x %02x %02x %20s\n",
99 &IPv6Address.au32[0], &IPv6Address.au32[1],
100 &IPv6Address.au32[2], &IPv6Address.au32[3],
101 &uIndex, &uLength, &uScope, &uTmp, szName) != EOF)
102 {
103 if (!strcmp(pReq->ifr_name, szName))
104 {
105 Info.IPv6Address.au32[0] = htonl(IPv6Address.au32[0]);
106 Info.IPv6Address.au32[1] = htonl(IPv6Address.au32[1]);
107 Info.IPv6Address.au32[2] = htonl(IPv6Address.au32[2]);
108 Info.IPv6Address.au32[3] = htonl(IPv6Address.au32[3]);
109 ASMBitSetRange(&Info.IPv6NetMask, 0, uLength);
110 }
111 }
112 fclose(fp);
113 }
114 ComObjPtr<HostNetworkInterface> IfObj;
115 IfObj.createObject();
116 if (SUCCEEDED(IfObj->init(Bstr(pReq->ifr_name), &Info)))
117 list.push_back(IfObj);
118 }
119 }
120 }
121 }
122 close(sock);
123 }
124 return VINF_SUCCESS;
125}
126
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette