VirtualBox

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

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

fixed file headers

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