-
Notifications
You must be signed in to change notification settings - Fork 276
/
Copy pathNetworkInfo.cpp
278 lines (239 loc) · 7.15 KB
/
NetworkInfo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
* Copyright (C) 2017 by Lieven De Samblanx ON7LDS
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "NetworkInfo.h"
#include "Log.h"
#include <cstdio>
#include <cassert>
#include <cstring>
#include <ctime>
#include <clocale>
#include <sys/types.h>
#if defined(__linux__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__APPLE__)
#include <ifaddrs.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__APPLE__)
#include <sys/sysctl.h>
#include <net/if.h>
#include <net/route.h>
#endif
#elif defined(_WIN32) || defined(_WIN64)
#include <ws2tcpip.h>
#include <WinSock2.h>
#include <iphlpapi.h>
#pragma comment(lib, "iphlpapi.lib")
#ifndef NO_ERROR
#define NO_ERROR 0
#endif
#ifndef ERROR_BUFFER_OVERFLOW
#define ERROR_BUFFER_OVERFLOW 111
#endif
#ifndef ERROR_INSUFFICIENT_BUFFER
#define ERROR_INSUFFICIENT_BUFFER 122
#endif
#endif
CNetworkInfo::CNetworkInfo()
{
}
CNetworkInfo::~CNetworkInfo()
{
}
void CNetworkInfo::getNetworkInterface(unsigned char* info)
{
LogInfo("Interfaces Info");
::strcpy((char*)info, "(address unknown)");
#if defined(__linux__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__APPLE__)
char* dflt = NULL;
#if defined(__linux__)
FILE* fp = ::fopen("/proc/net/route" , "r"); // IPv4 routing
if (fp == NULL) {
LogError("Unabled to open /proc/route");
return;
}
char line[100U];
while (::fgets(line, 100U, fp)) {
char* p1 = strtok(line , " \t");
char* p2 = strtok(NULL , " \t");
if (p1 != NULL && p2 != NULL) {
if (::strcmp(p2, "00000000") == 0) {
dflt = p1;
break;
}
}
}
::fclose(fp);
#elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__APPLE__)
int mib[] = {
CTL_NET,
PF_ROUTE,
0, // protocol
AF_INET, // IPv4 routing
NET_RT_DUMP,
0, // show all routes
#if defined(__OpenBSD__) || defined(__FreeBSD__)
0, // table id
#endif
};
const int cnt = sizeof(mib) / sizeof(int);
size_t size;
char ifname[IF_NAMESIZE] = {};
if (::sysctl(mib, cnt, NULL, &size, NULL, 0) == -1 || size <= 0) {
LogError("Unable to estimate routing table size");
return;
}
char *buf = new char[size];
if (::sysctl(mib, cnt, buf, &size, NULL, 0) == -1) {
LogError("Unable to get routing table");
delete[] buf;
return;
}
struct rt_msghdr *rtm;
for (char *p = buf; p < buf + size; p += rtm->rtm_msglen) {
rtm = (struct rt_msghdr *)p;
if (rtm->rtm_version != RTM_VERSION)
continue;
#if defined(__OpenBSD__)
struct sockaddr_in *sa = (struct sockaddr_in *)(p + rtm->rtm_hdrlen);
#elif defined(__NetBSD__) || defined(__FreeBSD__) || defined(__APPLE__)
struct sockaddr_in *sa = (struct sockaddr_in *)(rtm + 1);
#endif
if (sa->sin_addr.s_addr == INADDR_ANY) {
::if_indextoname(rtm->rtm_index, ifname);
break;
}
}
delete[] buf;
if (::strlen(ifname))
dflt = ifname;
#endif
if (dflt == NULL) {
LogError("Unable to find the default route");
return;
}
const unsigned int IFLISTSIZ = 25U;
char interfacelist[IFLISTSIZ][50+INET6_ADDRSTRLEN];
for (unsigned int n = 0U; n < IFLISTSIZ; n++)
interfacelist[n][0] = 0;
struct ifaddrs* ifaddr;
if (::getifaddrs(&ifaddr) == -1) {
LogError("getifaddrs failure");
return;
}
unsigned int ifnr = 0U;
for (struct ifaddrs* ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
if (ifa->ifa_addr == NULL)
continue;
int family = ifa->ifa_addr->sa_family;
if (family == AF_INET || family == AF_INET6) {
char host[NI_MAXHOST];
int s = ::getnameinfo(ifa->ifa_addr, family == AF_INET ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
if (s != 0) {
LogError("getnameinfo() failed: %s\n", gai_strerror(s));
continue;
}
if (family == AF_INET) {
::sprintf(interfacelist[ifnr], "%s:%s", ifa->ifa_name, host);
LogInfo(" IPv4: %s", interfacelist[ifnr]);
ifnr++;
} else {
::sprintf(interfacelist[ifnr], "%s:%s", ifa->ifa_name, host);
LogInfo(" IPv6: %s", interfacelist[ifnr]);
// due to default routing is for IPv4, other
// protocols are not candidate to display.
}
}
}
::freeifaddrs(ifaddr);
LogInfo(" Default interface is : %s" , dflt);
for (unsigned int n = 0U; n < ifnr; n++) {
char* p = ::strchr(interfacelist[n], '%');
if (p != NULL)
*p = 0;
if (::strstr(interfacelist[n], dflt) != 0) {
::strcpy((char*)info, interfacelist[n]);
break;
}
}
LogInfo(" IP to show: %s", info);
#elif defined(_WIN32) || defined(_WIN64)
PMIB_IPFORWARDTABLE pIpForwardTable = (MIB_IPFORWARDTABLE *)::malloc(sizeof(MIB_IPFORWARDTABLE));
if (pIpForwardTable == NULL) {
LogError("Error allocating memory");
return;
}
DWORD dwSize = 0U;
if (::GetIpForwardTable(pIpForwardTable, &dwSize, 0) == ERROR_INSUFFICIENT_BUFFER) {
::free(pIpForwardTable);
pIpForwardTable = (MIB_IPFORWARDTABLE *)::malloc(dwSize);
if (pIpForwardTable == NULL) {
LogError("Error allocating memory");
return;
}
}
DWORD ret = ::GetIpForwardTable(pIpForwardTable, &dwSize, 0);
if (ret != NO_ERROR) {
::free(pIpForwardTable);
LogError("GetIpForwardTable failed.");
return;
}
DWORD found = 999U;
for (DWORD i = 0U; i < pIpForwardTable->dwNumEntries; i++) {
if (pIpForwardTable->table[i].dwForwardDest == 0U) {
found = i;
break;
}
}
if (found == 999U) {
::free(pIpForwardTable);
LogError("Unable to find the default destination in the routing table.");
return;
}
DWORD ifnr = pIpForwardTable->table[found].dwForwardIfIndex;
::free(pIpForwardTable);
PIP_ADAPTER_INFO pAdapterInfo = (IP_ADAPTER_INFO *)::malloc(sizeof(IP_ADAPTER_INFO));
if (pAdapterInfo == NULL) {
LogError("Error allocating memory");
return;
}
ULONG buflen = sizeof(IP_ADAPTER_INFO);
if (::GetAdaptersInfo(pAdapterInfo, &buflen) == ERROR_BUFFER_OVERFLOW) {
::free(pAdapterInfo);
pAdapterInfo = (IP_ADAPTER_INFO *)::malloc(buflen);
if (pAdapterInfo == NULL) {
LogError("Error allocating memory");
return;
}
}
if (::GetAdaptersInfo(pAdapterInfo, &buflen) != NO_ERROR) {
::free(pAdapterInfo);
LogError("Call to GetAdaptersInfo failed.");
return;
}
PIP_ADAPTER_INFO pAdapter = pAdapterInfo;
while (pAdapter != NULL) {
LogInfo(" IP : %s", pAdapter->IpAddressList.IpAddress.String);
if (pAdapter->Index == ifnr)
::strcpy((char*)info, pAdapter->IpAddressList.IpAddress.String);
pAdapter = pAdapter->Next;
}
::free(pAdapterInfo);
LogInfo(" IP to show: %s", info);
#endif
}