VirtualBox

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

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

#3282: Linux build fix.

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