VirtualBox

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

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

#3282 HostNetIf API: 32-bit Linux is operational.

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