Repository navigation
Expand file tree
/
Copy pathutility.cpp
More file actions
347 lines (302 loc) · 10.5 KB
/
Copy pathutility.cpp
File metadata and controls
347 lines (302 loc) · 10.5 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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#include "utility.h"
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <inttypes.h>
#include <ifaddrs.h>
#include <netdb.h>
#include "globals.h"
int check_root_privilege() {
uid_t ruid, euid, suid;
int rc = getresuid(&ruid, &euid, &suid);
if (rc == -1) {
SYSLOG("getresuid() failed (%s)", strerror(errno));
return -1;
}
//SYSLOG("ruid: %d euid: %d, suid: %d", ruid, euid, suid);
/* program may have suid bit set to get root privilege */
if (euid != 0 )
return -1;
return 0;
}
/*
* simple stun client implement base on RFC3489 and RFC5389
* http://www.ietf.org/rfc/rfc3489.txt (obsoleted)
* https://tools.ietf.org/html/rfc5389
* most servers are not usable or need permission
*/
int stun_get_public_ip_imp(const char *server, unsigned short remote_port, unsigned short local_port, char *public_ip) {
int stun_socket = socket(AF_INET, SOCK_DGRAM, 0);
if (stun_socket == -1) {
SF_SYSLOG("failed to create stun socket (%s).", strerror(errno));
return -1;
}
struct sockaddr_in stun_client;
memset(&stun_client, 0, sizeof(stun_client));
stun_client.sin_family = AF_INET;
stun_client.sin_port = htons(local_port);
int rc = bind(stun_socket, (struct sockaddr *)&stun_client, sizeof(stun_client));
if (rc == -1) {
SF_SYSLOG("failed to bind stun socket (%s).", strerror(errno));
close(stun_socket);
return -1;
}
struct sockaddr_in stun_server;
memset(&stun_server, 0, sizeof(stun_server));
stun_server.sin_family = AF_INET;
stun_server.sin_port = htons(remote_port);
inet_pton(AF_INET, server, &stun_server.sin_addr);
typedef struct stun_header_tag {
uint16_t message_type;
uint16_t message_length;
unsigned char transaction_id[16];
} stun_header_t;
stun_header_t header;
header.message_type = htons(0x0001); /* Binding Request */
header.message_length = htons(0);
*(uint32_t *)(&header.transaction_id[0]) = htonl(0x2112A442);
/* 96bit transaction id */
*(uint32_t *)(&header.transaction_id[4]) = 0xAAAABBBB;
*(uint32_t *)(&header.transaction_id[8]) = 0xCCCCDDDD;
*(uint32_t *)(&header.transaction_id[12]) = 0xEEEEFFFF;
rc = sendto(stun_socket, (void *)&header, sizeof(header), 0, (struct sockaddr *)&stun_server, sizeof(stun_server));
if (rc == -1) {
SF_SYSLOG("failed to send request to stun server %s (%s)", server, strerror(errno));
close(stun_socket);
return -1;
}
unsigned char response[1024];
rc = recvfrom(stun_socket, response, 1024, 0, NULL, 0);
if (rc == -1) {
SF_SYSLOG("failed to receive from stun server %s (%s)", server, strerror(errno));
close(stun_socket);
return -1;
}
/* determine if it is a Binding Response 0x0101 */
if (*(uint16_t *)(&response[0]) == htons(0x0101)) {
uint16_t resp_length = ntohs(*(uint16_t *)(&response[2]));
/* check transaction id consistence */
if (*(uint32_t *)(&response[8]) == 0xAAAABBBB && *(uint32_t *)(&response[12]) == 0xCCCCDDDD && *(uint32_t *)(&response[16]) == 0xEEEEFFFF) {
uint16_t attr_length = 0, pos = 0;
while (pos < resp_length) {
if (*(uint16_t *)(&response[20]) == htons(0x0001)) { /* check if Binding Response Attribute type is `MAPPED-ADDRESS`*/
sprintf(public_ip, "%d.%d.%d.%d", response[28], response[29], response[30], response[31]);
break;
} else if (*(uint16_t *)(&response[20]) == htons(0x0020)) { /* check if Binding Resopnse Attribute type is `XOR-MAPPED-ADDRESS` */
sprintf(public_ip, "%d.%d.%d.%d", response[28]^0x21, response[29]^0x12, response[30]^0xA4, response[31]^0x42);
break;
}
attr_length = ntohs(*(uint16_t *)(&response[20 + pos + 2]));
pos += attr_length + 4;
}
if (pos == resp_length) {
SF_SYSLOG("STUN server doesn't response `XOR-MAPPPED-ADDRESS` or `MAPPED-ADDRESS`");
close(stun_socket);
return -1;
}
} else {
SF_SYSLOG("stun transaction id not consistent");
close(stun_socket);
return -1;
}
} else {
SF_SYSLOG("stun server doesn't response a Binding Response");
close(stun_socket);
return -1;
}
close(stun_socket);
return 0;
}
int stun_get_public_ip(const char *server, unsigned short remote_port, unsigned short local_port, char *public_ip) {
vector<string> ips;
if (is_valid_ip(server)) {
return stun_get_public_ip_imp(server, remote_port, local_port, public_ip);
} else {
int rc = resolve_fqdn_to_ip(server, ips);
if (rc != 0) {
SF_SYSLOG("resolve dns for %s failed.", server);
return -1;
}
return stun_get_public_ip_imp(ips[0].c_str(), remote_port, local_port, public_ip);
}
}
int dig_get_public_ip(char *public_ip, int len) {
const char *dig_cmd = "dig +short myip.opendns.com @resolver1.opendns.com";
FILE *cmd_fp = popen(dig_cmd, "r");
if (cmd_fp == NULL) {
SF_SYSLOG("popen failed, command (%s) (%s)", dig_cmd, strerror(errno));
return -1;
}
char *rc = fgets(public_ip, len, cmd_fp);
if (rc == NULL) {
SF_SYSLOG("fgets from pipe failed.");
pclose(cmd_fp);
return -1;
}
/* omit the last `\n` */
public_ip[strlen(public_ip) - 1] = 0;
pclose(cmd_fp);
return 0;
}
/**
* get ip address the router choosed
*/
char *get_primary_ip(char *ip, size_t len) {
int sock = socket(AF_INET, SOCK_DGRAM, 0);
if (unlikely(sock == -1)) {
SF_SYSLOG("create socket failed (%s)", strerror(errno));
return NULL;
}
/**
* use google dns for test, we don't need to send any packet, the router will determine
* which interface to use.
*/
const char *google_dns = "8.8.8.8";
uint16_t dns_port = 53;
struct sockaddr_in serv;
memset(&serv, 0, sizeof(serv));
serv.sin_family = AF_INET;
serv.sin_addr.s_addr = inet_addr(google_dns);
serv.sin_port = htons(dns_port);
int rc = connect(sock, (struct sockaddr *)&serv, sizeof(serv));
if (unlikely(rc == -1)) {
SF_SYSLOG("UDP connect failed (%s)", strerror(errno));
return NULL;
}
sockaddr_in name;
socklen_t name_len = sizeof(name);
rc = getsockname(sock, (struct sockaddr *)&name, &name_len);
if (unlikely(rc == -1)) {
SF_SYSLOG("getsockname() failed (%s)", strerror(errno));
return NULL;
}
const char *p = inet_ntop(AF_INET, &name.sin_addr, ip, len);
if (unlikely(p == NULL)) {
SF_SYSLOG("inet_ntop() failed (%s)", strerror(errno));
return NULL;
}
return ip;
}
/**
* get ip address of specified ethernet.
*/
char *get_ethernet_ip(const char *ethernet, char *ip, size_t len) {
struct ifaddrs *ips, *iter;
int rc = getifaddrs(&ips);
if (unlikely(rc == -1)) {
SF_SYSLOG("getifaddrs() failed (%s)", strerror(errno));
freeifaddrs(ips);
return NULL;
}
for (iter = ips; iter != NULL; iter = ips->ifa_next) {
if (strcasecmp(ethernet, iter->ifa_name) == 0 && iter->ifa_addr->sa_family == AF_INET) {
sockaddr_in *local_ip = (sockaddr_in *)iter->ifa_addr;
const char *p = inet_ntop(AF_INET, &local_ip->sin_addr, ip, len);
if (unlikely(p == NULL)) {
SF_SYSLOG("inet_ntop() failed (%s)", strerror(errno));
freeifaddrs(ips);
return NULL;
}
freeifaddrs(ips);
return ip;
}
}
freeifaddrs(ips);
return NULL;
}
/**
* get ip address of specified ethernet.
*/
char *get_ethname_by_ip(const char *ip, char *ethname, size_t len) {
struct ifaddrs *ips, *iter;
int rc = getifaddrs(&ips);
if (unlikely(rc == -1)) {
SF_SYSLOG("getifaddrs() failed (%s)", strerror(errno));
freeifaddrs(ips);
return NULL;
}
char temp_ip[INET_ADDRSTRLEN];
for (iter = ips; iter != NULL; iter = iter->ifa_next) {
if (iter->ifa_addr->sa_family == AF_INET) {
sockaddr_in *local_ip = (sockaddr_in *)iter->ifa_addr;
const char *p = inet_ntop(AF_INET, &local_ip->sin_addr, temp_ip, INET_ADDRSTRLEN);
if (unlikely(p == NULL)) {
SF_SYSLOG("inet_ntop() failed (%s)", strerror(errno));
freeifaddrs(ips);
return NULL;
} else {
if (strcmp(ip, p) != 0)
continue;
else
strncpy(ethname, iter->ifa_name, len);
}
freeifaddrs(ips);
return ethname;
}
}
freeifaddrs(ips);
return NULL;
}
/*
* Resolve fqdn to ip address
* Note: Now we only resolve ipv4 address, if you need to use ipv6,
* modify this function. We only use one IP even though resolving
* dns gives us AN IP LIST.
*/
int resolve_fqdn_to_ip(const char *fqdn, vector<string> &ips) {
struct addrinfo hints, *res;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
int rc = getaddrinfo(fqdn, NULL, &hints, &res);
if (unlikely(rc != 0)) {
SF_SYSLOG("getaddrinfo() failed, host(%s) msg(%s)", fqdn, gai_strerror(rc));
return -1;
}
struct sockaddr_in *addr;
char ip[INET_ADDRSTRLEN];
for (; res != NULL; res = res->ai_next) {
addr = (struct sockaddr_in *)res->ai_addr;
inet_ntop(AF_INET, &addr->sin_addr, ip, INET_ADDRSTRLEN);
ips.push_back(ip);
}
return 0;
}
/*
* determine whether an IP is valid or not
*/
bool is_valid_ip(const char *host) {
struct sockaddr_in ipv4;
struct sockaddr_in6 ipv6;
const char *mark = strchr(host, ':');
if (mark != NULL) {
/* ipv6 */
int rc = inet_pton(AF_INET6, host, &(ipv6.sin6_addr));
return rc;
} else {
/* ipv4 */
int rc = inet_pton(AF_INET, host, &(ipv4.sin_addr));
return rc;
}
}
/*
* Test
*/
#if 0
global_config_t g_config;
int main(int argc, char **argv) {
/*
vector<string> ip;
resolve_fqdn_to_ip(argv[1], ip);
for (int i = 0; i < ip.size(); ++i)
cout<<"ip: "<<ip[i]<<endl;
*/
char ethname[32];
get_ethname_by_ip("127.0.0.1", ethname, 32);
cout<<"ethname: "<<ethname<<endl;
get_ethname_by_ip("172.31.43.244", ethname, 32);
cout<<"ethname: "<<ethname<<endl;
return 0;
}
#endif