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