-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbasecom.cpp
More file actions
425 lines (319 loc) · 13.3 KB
/
basecom.cpp
File metadata and controls
425 lines (319 loc) · 13.3 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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/*
Socle - Socket Library Ecosystem
Copyright (c) 2014, Ales Stibal <astib@mag0.net>, All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library.
*/
#include <basecom.hpp>
#include <hostcx.hpp>
#include <internet.hpp>
#include <vars.hpp>
#include <netinet/tcp.h>
#include <linux/in6.h>
#include <linux/netfilter_ipv4.h>
#include <linux/netfilter_ipv6.h>
#include <numops.hpp>
#ifndef IP6T_SO_ORIGINAL_DST
// including netfilter includes produce compile error, so this is the only working possibility.
// yes, I am aware this may (and probably will) break one day. :/
#define IP6T_SO_ORIGINAL_DST 80
#endif
using namespace socle;
void baseCom::init(baseHostCX* owner) {
if(!_static_init) {
static_init();
_static_init = true;
}
owner_cx_ = owner;
// non-local sockets support
nonlocal_dst_ = false;
nonlocal_dst_resolved_ = false;
nonlocal_dst_host_ = "";
nonlocal_dst_port_ = 0;
memset(&nonlocal_dst_peer_info_,0,sizeof(nonlocal_dst_peer_info_));
}
int baseCom::nonlocal_bind (unsigned short port) {
nonlocal_dst(true);
int r = bind(port);
if (r < 0) {
nonlocal_dst(false);
}
return r;
}
int baseCom::unblock(int s) const {
int client_oldFlag = fcntl(s, F_GETFL, 0);
if (! (client_oldFlag & O_NONBLOCK)) {
if (fcntl(s, F_SETFL, client_oldFlag | O_NONBLOCK) < 0) {
_err("Error setting socket %d as non-blocking",s);
return -1;
} else {
_deb("Setting socket %d as non-blocking",s);
}
}
return 0;
}
int baseCom::namesocket(int sockfd, std::string& addr, unsigned short port, sa_family_t family) {
sockaddr_storage sa {};
sa.ss_family = family;
if(family == AF_INET or family == AF_UNSPEC) {
inet::to_sockaddr_in(&sa)->sin_port = htons(port);
inet_pton(family,addr.c_str(),&inet::to_sockaddr_in(&sa)->sin_addr);
if(so_transparent_v4(sockfd) != 0) {
_err("baseCom::namesocket[%d]: making transparent failed (IPv4)", sockfd);
}
}
else if(family == AF_INET6) {
inet::to_sockaddr_in6(&sa)->sin6_port = htons(port);
inet_pton(family,addr.c_str(),&inet::to_sockaddr_in6(&sa)->sin6_addr);
if(so_transparent_v6(sockfd) != 0) {
_err("baseCom::namesocket[%d]: making transparent failed (IPv6)", sockfd);
}
}
else {
_err("cannot name socket: unsupported protocol family");
}
int ret_bind = ::bind(sockfd, (sockaddr*)&sa, sizeof(sockaddr_storage));
if(ret_bind != 0) {
err_errno(string_format("baseCom::namesocket[%d]: bind", sockfd).c_str(), "<nil>", ret_bind);
ret_bind = errno;
}
return ret_bind;
}
bool baseCom::resolve_redirected(int s, std::string* target_host, std::string* target_port, sockaddr_storage* target_storage) {
char orig_host[INET6_ADDRSTRLEN];
struct sockaddr_storage peer_info_{};
struct sockaddr_storage *ptr_peer_info = &peer_info_;
//clear peer info struct
socklen_t addrlen = sizeof(peer_info_);
memset(ptr_peer_info, 0, addrlen);
const char* op = "getsockopt(redir)";
int ret = getsockopt( s, SOL_IP, SO_ORIGINAL_DST, ptr_peer_info, &addrlen );
if ( ret != 0) {
ret = getsockopt( s, SOL_IPV6, IP6T_SO_ORIGINAL_DST, ptr_peer_info, &addrlen );
}
if( ret != 0) {
_err("error getting original DST: %s", string_error().c_str());
}
else {
unsigned short orig_port = 0;
if (ptr_peer_info->ss_family == AF_INET) {
inet_ntop(AF_INET, &(((struct sockaddr_in *) ptr_peer_info)->sin_addr), orig_host, INET_ADDRSTRLEN);
orig_port = ntohs(((struct sockaddr_in *) ptr_peer_info)->sin_port);
_deb("baseCom::resolve_socket(ipv4-redir): %s returns %s:%d", op, orig_host, orig_port);
l3_proto(AF_INET);
} else if (ptr_peer_info->ss_family == AF_INET6) {
inet_ntop(AF_INET6, &(((struct sockaddr_in6 *) ptr_peer_info)->sin6_addr), orig_host, INET6_ADDRSTRLEN);
orig_port = ntohs(((struct sockaddr_in6 *) ptr_peer_info)->sin6_port);
_deb("baseCom::resolve_socket(ipv6-redir): %s returns %s:%d", op, orig_host, orig_port);
l3_proto(AF_INET6);
}
std::string mapped4_temp = orig_host;
if (mapped4_temp.find("::ffff:") == 0) {
_deb("baseCom::resolve_socket: mapped IPv4 detected, removing mapping prefix");
mapped4_temp = mapped4_temp.substr(7);
l3_proto(AF_INET);
}
if (target_host != nullptr) *target_host = mapped4_temp;
if (target_port != nullptr) *target_port = std::to_string(tainted::var<unsigned>(orig_port, tainted::any<unsigned>));
if (target_storage != nullptr) *target_storage = peer_info_;
return true;
}
return false;
}
bool baseCom::resolve_socket(bool source, int s, std::string* target_host, std::string* target_port, sockaddr_storage* target_storage) {
char orig_host[INET6_ADDRSTRLEN];
struct sockaddr_storage peer_info_{};
struct sockaddr_storage *ptr_peer_info = &peer_info_;
//clear peer info struct
socklen_t addrlen = sizeof(peer_info_);
memset(ptr_peer_info, 0, addrlen);
//For UDP transparent proxying:
//Set IP_RECVORIGDSTADDR socket option for getting the original
//destination of a datagram
//Socket is bound to original destination
int ret = -1;
const char* op = str_unknown;
if (source) {
op = str_getpeername;
ret = getpeername(s, (struct sockaddr*) ptr_peer_info, &addrlen);
} else {
op = str_getsockname;
ret = getsockname(s, (struct sockaddr*) ptr_peer_info, &addrlen);
}
if(ret < 0) {
_dia("baseCom::resolve_socket: %s failed!",op);
return false;
}
else {
unsigned short orig_port = 0;
if(ptr_peer_info->ss_family == AF_INET){
inet_ntop(AF_INET, &(((struct sockaddr_in*) ptr_peer_info)->sin_addr),orig_host, INET_ADDRSTRLEN);
orig_port = ntohs(((struct sockaddr_in*) ptr_peer_info)->sin_port);
_deb("baseCom::resolve_socket(ipv4): %s returns %s:%d",op,orig_host,orig_port);
l3_proto(AF_INET);
}
else if(ptr_peer_info->ss_family == AF_INET6){
inet_ntop(AF_INET6, &(((struct sockaddr_in6*) ptr_peer_info)->sin6_addr), orig_host, INET6_ADDRSTRLEN);
orig_port = ntohs(((struct sockaddr_in6*) ptr_peer_info)->sin6_port);
_deb("baseCom::resolve_socket(ipv6): %s returns %s:%d",op,orig_host,orig_port);
l3_proto(AF_INET6);
}
std::string mapped4_temp = orig_host;
if(mapped4_temp.find("::ffff:") == 0) {
_deb("baseCom::resolve_socket: mapped IPv4 detected, removing mapping prefix");
mapped4_temp = mapped4_temp.substr(7);
l3_proto(AF_INET);
}
if(target_host != nullptr) *target_host = mapped4_temp;
if(target_port != nullptr) *target_port = std::to_string(tainted::var<unsigned>(orig_port, tainted::any<unsigned>));
if(target_storage != nullptr) *target_storage = peer_info_;
return true;
}
return false;
}
bool baseCom::resolve_nonlocal_dst_socket(int sock) {
std::string h("0.0.0.0");
std::string p("0");
struct sockaddr_storage s; memset(&s,0,sizeof(s));
nonlocal_dst_resolved_ = resolve_socket_dst(sock, &h, &p, &s);
if(nonlocal_dst_resolved()) {
nonlocal_dst_host_ = h;
nonlocal_dst_port_ = raw::down_cast_signed<unsigned short>(std::stoi(p)).value_or(0);
nonlocal_dst_peer_info_ = s;
}
_dia("baseCom::resolve_nonlocal_dst_socket: nonlocal dst: %s:%s", h.c_str(), p.c_str());
return nonlocal_dst_resolved_;
}
bool baseCom::resolve_redirected_dst_socket(int sock) {
std::string h("0.0.0.0");
std::string p("0");
struct sockaddr_storage s; memset(&s,0,sizeof(s));
nonlocal_dst_resolved_ = resolve_redirected(sock, &h, &p, &s);
if(nonlocal_dst_resolved()) {
nonlocal_dst_host_ = h;
nonlocal_dst_port_ = raw::down_cast_signed<unsigned short>(std::stoi(p)).value_or(0);
nonlocal_dst_peer_info_ = s;
}
_dia("baseCom::resolve_redirected_dst_socket: nonlocal redirected dst: %s:%s", h.c_str(), p.c_str());
return nonlocal_dst_resolved_;
}
int baseCom::poll() {
_ext("baseCom::poll: called");
auto wait_time = poll_msec;
if(not poller.rescans_empty()) {
_dia("baseCom::poll: rescans not empty, shorter poll cycle!");
wait_time = rescan_msec;
}
int r = poller.wait(wait_time);
_ext("baseCom::poll: poller returned %d",r);
if (r < 0) {
_dia("baseCom::poll: returned by poll: %s",string_error().c_str());
}
poll_result = r;
return r;
}
void baseCom::close(int _fd) {
//really close the socket! Beware, from this point it can be reused!
if(_fd > 0) {
shutdown(_fd);
int r = ::close(_fd);
if(r < 0) _dia("baseCom::close[%d]: error: %s", _fd, string_error().c_str());
}
}
std::string baseCom::full_flags_str() {
std::string msg = flags_str();
if(peer() != nullptr) {
msg += "|" + peer()->flags_str();
} else {
msg += "|X";
}
return msg;
}
void baseCom::err_errno(const char* fn, const char* params, int rv) const {
_err("%s: error: %d params: %s: %s", fn, rv, params, string_error().c_str());
}
int baseCom::so_reuseaddr(int sock) const {
constexpr int optval = 1;
int sso = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof optval);
if(sso != 0) err_errno(string_format("baseCom::so_reuseaddr: setsockopt[%d]", sock).c_str(),
"SOL_SOCKET/SO_REUSEADDR", sso);
return sso;
}
int baseCom::so_broadcast(int sock) const {
constexpr int optval = 1;
int sso = setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &optval, sizeof optval);
if(sso != 0) err_errno(string_format("baseCom::so_broadcast: setsockopt[%d]", sock).c_str(),
"SOL_SOCKET/SO_BROADCAST", sso);
return sso;
}
int baseCom::so_nodelay(int sock) const {
constexpr int optval = 1;
int sso = setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &optval, sizeof optval);
if(sso != 0) err_errno(string_format("baseCom::so_nodelay: setsockopt[%d]", sock).c_str(),
"IPPROTO_TCP/TCP_NODELAY", sso);
return sso;
}
int baseCom::so_quickack(int sock) const {
constexpr int optval = 1;
int sso = setsockopt(sock, IPPROTO_TCP, TCP_QUICKACK , &optval, sizeof optval);
if(sso != 0) err_errno(string_format("baseCom::so_quickack: setsockopt[%d]", sock).c_str(),
"IPPROTO_TCP/TCP_QUICKACK", sso);
return sso;
}
int baseCom::so_keepalive(int sock) const {
constexpr int optval = 1;
int sso = setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE , &optval, sizeof optval);
if(sso != 0) err_errno(string_format("baseCom::so_keepalive: setsockopt[%d]", sock).c_str(),
"SOL_SOCKET/SO_KEEPALIVE", sso);
return sso;
}
int baseCom::so_transparent_v4(int sock) const {
constexpr int optval = 1;
int sso = setsockopt(sock, SOL_IP, IP_TRANSPARENT, &optval, sizeof(optval));
if(sso != 0) err_errno(string_format("baseCom::so_transparent_v4: setsockopt[%d]", sock).c_str(),
"SOL_IP/IP_TRANSPARENT", sso);
return sso;
}
int baseCom::so_transparent_v6(int sock) const {
constexpr int optval = 1;
int sso = setsockopt(sock, SOL_IPV6, IPV6_TRANSPARENT, &optval, sizeof(optval));
if(sso != 0) err_errno(string_format("baseCom::so_transparent_v6: setsockopt[%d]", sock).c_str(),
"SOL_IPV6/IPV6_TRANSPARENT", sso);
return sso;
}
int baseCom::so_transparent(int sock) const {
if(l3_proto() == AF_INET) {
return so_transparent_v4(sock);
}
else if(l3_proto() == AF_INET6) {
return so_transparent_v6(sock);
}
else {
_err("baseCom::so_transparent: fallback IPv4 setsockopt");
return so_transparent_v4(sock);
}
}
int baseCom::so_recvorigdstaddr_v4(int sock) const {
constexpr int optval = 1;
int sso = setsockopt(sock, SOL_IP, IP_RECVORIGDSTADDR, &optval, sizeof optval);
if (sso != 0)
err_errno(string_format("baseCom::so_recvorigdstaddr_v4[%d]", sock).c_str(),
"SOL_IP/IP_RECVORIGDSTADDR", sso);
return sso;
}
int baseCom::so_recvorigdstaddr_v6(int sock) const {
constexpr int optval = 1;
int sso = setsockopt(sock, SOL_IPV6, IPV6_RECVORIGDSTADDR, &optval, sizeof optval);
if (sso != 0)
err_errno(string_format("baseCom::so_recvorigdstaddr_v6[%d]", sock).c_str(),
"SOL_IPV6/IPV6_RECVORIGDSTADDR", sso);
return sso;
}