This repository was archived by the owner on Mar 21, 2023. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathnetwork.cpp
More file actions
240 lines (200 loc) · 5.65 KB
/
Copy pathnetwork.cpp
File metadata and controls
240 lines (200 loc) · 5.65 KB
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
/*********************************************************************
* Copyright (c) Intel Corporation 2019 - 2020
* SPDX-License-Identifier: Apache-2.0
**********************************************************************/
#include "network.h"
#include "commands.h"
#include <iostream>
#include <vector>
#ifdef _WIN32
#include <winsock2.h>
#include <iphlpapi.h>
#define WORKING_BUFFER_SIZE 15000
#define MAX_TRIES 3
#else
#include <string.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
typedef int SOCKET;
#endif
#ifdef _WIN32
std::string net_get_dns(char* macAddress)
{
std::string dnsSuffix = "";
char dns[256];
memset(dns, 0, 256);
PIP_ADAPTER_ADDRESSES pAddresses = NULL;
DWORD dwSize = 0;
DWORD dwRetVal = 0;
ULONG outBufLen = 0;
ULONG Iterations = 0;
outBufLen = WORKING_BUFFER_SIZE;
// get info for all adapters
do {
pAddresses = (IP_ADAPTER_ADDRESSES*)malloc(outBufLen);
if (pAddresses == NULL) {
std::cout << "dns memory error" << std::endl;
return dnsSuffix;
}
dwRetVal = GetAdaptersAddresses(AF_INET, GAA_FLAG_INCLUDE_PREFIX, NULL, pAddresses, &outBufLen);
if (dwRetVal == ERROR_BUFFER_OVERFLOW)
{
free(pAddresses);
pAddresses = NULL;
}
else
{
break;
}
Iterations++;
} while ((dwRetVal == ERROR_BUFFER_OVERFLOW) && (Iterations < MAX_TRIES));
// get DNS from MAC
PIP_ADAPTER_ADDRESSES pCurrAddresses = NULL;
pCurrAddresses = pAddresses;
while (pCurrAddresses)
{
if (pCurrAddresses->PhysicalAddressLength != 0)
{
if (memcmp(macAddress, (char*)pCurrAddresses->PhysicalAddress, 6) == 0)
{
if (wcslen(pCurrAddresses->DnsSuffix) > 0)
{
snprintf(dns, 256, "%ws", pCurrAddresses->DnsSuffix);
break;
}
}
}
pCurrAddresses = pCurrAddresses->Next;
}
dnsSuffix = dns;
return dnsSuffix;
}
#else
std::string net_get_dns(char* macAddress)
{
std::string dnsSuffix = "";
// get socket
SOCKET s = 0;
s = socket(PF_INET, SOCK_DGRAM, 0);
if (s < 0)
{
std::cout << "couldn't get socket" << std::endl;
return dnsSuffix;
}
// get info for all adapters
struct ifconf ifc;
memset(&ifc, 0, sizeof(ifconf));
char buffer[8192];
memset(buffer, 0, sizeof(buffer));
ifc.ifc_buf = buffer;
ifc.ifc_len = sizeof(buffer);
if (ioctl(s, SIOCGIFCONF, &ifc) < 0)
{
std::cout << "ioctl SIOCGIFCONF failed" << std::endl;
return dnsSuffix;
}
// get DNS from IP associated with MAC
struct ifreq* ifr = ifc.ifc_req;
int interfaceCount = ifc.ifc_len / sizeof(struct ifreq);
char ip[INET6_ADDRSTRLEN] = { 0 };
struct ifreq* item;
struct sockaddr* addr;
for (int i = 0; i < interfaceCount; i++)
{
item = &ifr[i];
addr = &(item->ifr_addr);
// get IP address
if (ioctl(s, SIOCGIFADDR, item) < 0)
{
std::cout << "ioctl SIOCGIFADDR failed" << std::endl;
continue;
}
if (inet_ntop(AF_INET, &(((struct sockaddr_in*)addr)->sin_addr),
ip, sizeof(ip)) == NULL)
{
std::cout << "inet_ntop" << std::endl;
continue;
}
// get MAC address
if (ioctl(s, SIOCGIFHWADDR, item) < 0)
{
std::cout << "ioctl SIOCGIFHWADDR failed" << std::endl;
continue;
}
if (memcmp(macAddress, (char*)item->ifr_hwaddr.sa_data, 6) == 0)
{
// Get host by using the IP address which AMT device is using
struct in_addr addr = { 0 };
struct hostent* host;
addr.s_addr = inet_addr(ip);
host = gethostbyaddr((char*)&addr, 4, AF_INET);
if (host == NULL)
{
std::cout << "gethostbyaddr() failed";
return dnsSuffix;
}
// strip off the hostname to get actual domain name
int domainNameSize = 256;
char domainName[domainNameSize];
memset(domainName, 0, domainNameSize);
char* dn = strchr(host->h_name, '.');
if (dn != NULL)
{
if (domainNameSize >= strlen(dn + 1))
{
snprintf(domainName, domainNameSize, "%s", ++dn);
dnsSuffix = domainName;
}
}
}
}
return dnsSuffix;
}
#endif
std::string net_get_hostname()
{
char hostname[256];
std::string hostname_string = "";
int result;
#ifdef WIN32
WSADATA wsa;
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
{
throw std::runtime_error("error: network error");
}
#endif
// get hostname
result = gethostname(hostname, sizeof(hostname));
#ifdef WIN32
WSACleanup();
#endif
if (result == 0)
{
hostname_string = hostname;
}
return hostname_string;
}
std::string net_get_dns()
{
std::string dns_suffix;
std::vector<unsigned char> address;
cmd_get_wired_mac_address(address);
if (address.size() == 6)
{
char macAddress[6];
macAddress[0] = address[0];
macAddress[1] = address[1];
macAddress[2] = address[2];
macAddress[3] = address[3];
macAddress[4] = address[4];
macAddress[5] = address[5];
// get DNS from OS
dns_suffix = net_get_dns(macAddress);
}
return dns_suffix;
}