-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconnection.cpp
625 lines (544 loc) · 21.5 KB
/
connection.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
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
/**
Copyright 2008-2019 Red Anchor Trading Co. Ltd.
Distributed under the Boost Software License, Version 1.0.
See <http://www.boost.org/LICENSE_1_0.txt>
*/
#include "fost-inet.hpp"
#include <fost/connection.hpp>
#include <fost/insert>
#include <fost/log>
#include <boost/asio/ssl.hpp>
using namespace fostlib;
/**
* ## Configuration defaults
*/
fostlib::setting<int64_t> const fostlib::c_connect_timeout{
"fost-internet/Cpp/fost-inet/connection.cpp", "Network settings",
"Connect time out", 10, true};
fostlib::setting<int64_t> const fostlib::c_read_timeout{
"fost-internet/Cpp/fost-inet/connection.cpp", "Network settings",
"Read time out", 30, true};
fostlib::setting<int64_t> const fostlib::c_large_read_chunk_size{
"fost-internet/Cpp/fost-inet/connection.cpp", "Network settings",
"Large read chunk size", 1024, true};
fostlib::setting<fostlib::json> const fostlib::c_socks_version{
"fost-internet/Cpp/fost-inet/connection.cpp", "Network settings",
"Socks version", json{}, true};
fostlib::setting<fostlib::string> const fostlib::c_socks_host{
"fost-internet/Cpp/fost-inet/connection.cpp", "Network settings",
"Socks host", "localhost:8888", true};
fostlib::setting<bool> const fostlib::c_always_skip_cert_verification{
"fost-internet/Cpp/fost-inet/connection.cpp", "TLS",
"Always skip TLS server certificate verification", false, true};
/// ### Android
#ifdef ANDROID
fostlib::setting<bool> const fostlib::c_tls_use_standard_verify_paths{
"fost-internet/Cpp/fost-inet/connection.cpp", "TLS",
"Use standard verify paths", false, true};
fostlib::setting<fostlib::json> const fostlib::c_extra_ca_cert_paths{
"fost-internet/Cpp/fost-inet/connection.cpp", "TLS",
"Extra CA certificate paths",
fostlib::json::array_t{fostlib::json{"/system/etc/security/cacerts/"}},
true};
/// ### Default configuration
#else
fostlib::setting<bool> const fostlib::c_tls_use_standard_verify_paths{
"fost-internet/Cpp/fost-inet/connection.cpp", "TLS",
"Use standard verify paths", true, true};
fostlib::setting<fostlib::json> const fostlib::c_extra_ca_cert_paths{
"fost-internet/Cpp/fost-inet/connection.cpp", "TLS",
"Extra CA certificate paths", fostlib::json::array_t{}, true};
#endif
fostlib::setting<fostlib::nullable<fostlib::string>> const
fostlib::c_certificate_verification_file{
"fost-internet/Cpp/fost-inet/connection.cpp",
"TLS",
"Certificate verification file",
{},
true};
fostlib::setting<fostlib::json> const fostlib::c_extra_leaf_certificates{
"fost-internet/Cpp/fost-inet/connection.cpp", "TLS",
"Extra CA leaf certificates", fostlib::json::array_t{}, true};
/**
* ## TLS implementation
*/
struct ssl_data {
ssl_data(io_context_type &, socket_type &sock)
: ctx(boost::asio::ssl::context::sslv23_client), ssl_sock(sock, ctx) {
ssl_sock.handshake(boost::asio::ssl::stream_base::client);
}
ssl_data(
io_context_type &,
socket_type &sock,
std::string const &hostname,
bool const verify)
: ctx(boost::asio::ssl::context::sslv23_client), ssl_sock(sock, ctx) {
if (not SSL_set_tlsext_host_name(
ssl_sock.native_handle(), hostname.c_str())) {
boost::system::error_code ec{
static_cast<int>(::ERR_get_error()),
boost::asio::error::get_ssl_category()};
throw exceptions::socket_error(ec);
}
ssl_sock.lowest_layer().set_option(
boost::asio::ip::tcp::no_delay(true));
if (verify && not c_always_skip_cert_verification.value()) {
if (c_tls_use_standard_verify_paths.value()) {
ctx.set_default_verify_paths();
}
for (auto const &path : c_extra_ca_cert_paths.value()) {
ctx.add_verify_path(static_cast<std::string>(
fostlib::coerce<fostlib::string>(path)));
}
if (c_certificate_verification_file.value()) {
auto certs = *c_certificate_verification_file.value();
ctx.load_verify_file(certs.shrink_to_fit());
}
for (auto const &certjs : c_extra_leaf_certificates.value()) {
auto const cert = fostlib::coerce<f5::u8view>(certjs);
ctx.add_certificate_authority(boost::asio::buffer(
cert.memory().data(), cert.memory().size()));
}
ssl_sock.set_verify_mode(boost::asio::ssl::verify_peer);
ssl_sock.set_verify_callback(
boost::asio::ssl::rfc2818_verification(hostname));
}
ssl_sock.handshake(boost::asio::ssl::stream_base::client);
}
boost::asio::ssl::context ctx;
boost::asio::ssl::stream<socket_type &> ssl_sock;
};
/// Because `ssl` is a private member of `network_connection` we can't
/// just use this in the free functions below that implement the actual
/// networking. This sub-classing mechanism works around that.
struct network_connection::ssl : public ssl_data {
template<typename... Args>
ssl(Args &&... args) : ssl_data{std::forward<Args>(args)...} {}
};
/**
* ## Networking implementation
*/
namespace {
void handle_error(
nliteral func,
nliteral msg,
const boost::system::error_code &error) {
if (error == boost::asio::error::eof)
throw exceptions::unexpected_eof(string(msg));
else if (error)
throw exceptions::socket_error(error, msg);
}
std::size_t
send(socket_type &sock, ssl_data *ssl, boost::asio::streambuf &b) {
try {
if (ssl)
return boost::asio::write(ssl->ssl_sock, b);
else
return boost::asio::write(sock, b);
} catch (boost::system::system_error &e) {
throw exceptions::socket_error(e.code());
}
}
struct timeout_wrapper {
typedef nullable<boost::system::error_code> timeout_error;
typedef nullable<std::pair<boost::system::error_code, std::size_t>>
read_error;
using connect_async_function_type =
std::function<void(boost::system::error_code)>;
using read_async_function_type =
std::function<void(boost::system::error_code, std::size_t)>;
socket_type &sock;
boost::system::error_code &error;
boost::asio::deadline_timer timer;
timeout_error timeout_result;
read_error read_result;
std::size_t received;
static void timedout(
socket_type &sock,
timeout_error &n,
boost::system::error_code e) {
if (e != boost::asio::error::operation_aborted) {
#ifdef FOST_OS_WINDOWS
sock.close();
#else
boost::system::error_code cancel_error; // We ignore this
sock.cancel(cancel_error);
#endif // FOST_OS_WINDOWS
}
n = e;
}
static void connect_done(
boost::asio::deadline_timer &timer,
read_error &n,
boost::system::error_code e) {
timer.cancel();
n = std::make_pair(e, std::size_t{});
}
static void read_done(
boost::asio::deadline_timer &timer,
read_error &n,
boost::system::error_code e,
std::size_t s) {
timer.cancel();
n = std::make_pair(e, s);
}
timeout_wrapper(
socket_type &sock,
boost::system::error_code &e,
const setting<int64_t> &timeout = c_read_timeout)
: sock(sock),
error(e),
#if BOOST_VERSION >= 107000 // 1.70.0
timer(sock.get_executor()),
#elif BOOST_VERSION >= 106600 // 1.66.0
timer(sock.get_io_context()),
#else
timer(sock.get_io_service()),
#endif
received(0) {
timer.expires_from_now(
boost::posix_time::seconds(coerce<long>(timeout.value())));
timer.async_wait([this, &sock](auto err) {
return timedout(sock, timeout_result, err);
});
}
connect_async_function_type connect_async_function() {
return [this](auto err) {
return connect_done(timer, read_result, err);
};
}
read_async_function_type read_async_function() {
return [this](auto err, auto size) {
return read_done(timer, read_result, err, size);
};
}
std::size_t complete() {
#if BOOST_VERSION >= 107000 // 1.70.0
sock.get_executor()
.template target<io_context_type::executor_type>()
->context()
.reset();
sock.get_executor()
.template target<io_context_type::executor_type>()
->context()
.run();
#elif BOOST_VERSION >= 106600 // 1.66.0
sock.get_io_context().reset();
sock.get_io_context().run();
#else
sock.get_io_service().reset();
sock.get_io_service().run();
#endif
if (read_result.value().first
&& read_result.value().first != boost::asio::error::eof) {
fostlib::log::debug(c_fost_inet)(
"",
"Got an error that will be treated as a normal TCP "
"connection closure")(
"error", read_result.value().first);
}
error = read_result.value().first;
received = read_result.value().second;
return received;
}
};
inline std::size_t read_until(
socket_type &sock,
ssl_data *ssl,
boost::asio::streambuf &b,
const char *term,
boost::system::error_code &e) {
timeout_wrapper timeout(sock, e);
if (ssl)
boost::asio::async_read_until(
ssl->ssl_sock, b, term, timeout.read_async_function());
else
boost::asio::async_read_until(
sock, b, term, timeout.read_async_function());
return timeout.complete();
}
template<typename F>
inline std::size_t
read(socket_type &sock,
ssl_data *ssl,
boost::asio::streambuf &b,
F f,
boost::system::error_code &e) {
timeout_wrapper timeout(sock, e);
if (ssl)
boost::asio::async_read(
ssl->ssl_sock, b, f, timeout.read_async_function());
else
boost::asio::async_read(sock, b, f, timeout.read_async_function());
return timeout.complete();
}
inline std::size_t read_until(
socket_type &sock,
ssl_data *ssl,
boost::asio::streambuf &b,
const char *term) {
boost::system::error_code error;
std::size_t bytes = read_until(sock, ssl, b, term, error);
handle_error(
"read_until(socket_type &sock, ssl_data *ssl, "
"boost::asio::streambuf &b, const char *term)",
"Whilst reading data from a socket", error);
return bytes;
}
template<typename F>
inline std::size_t read(
socket_type &sock, ssl_data *ssl, boost::asio::streambuf &b, F f) {
boost::system::error_code error;
std::size_t bytes = read(sock, ssl, b, f, error);
handle_error(
"read<F>(socket_type &sock, ssl_data *ssl, "
"boost::asio::streambuf &b, F f)",
"Whilst reading data from a socket", error);
return bytes;
}
void
connect(io_context_type &io_service,
socket_type &socket,
const host &host,
port_number port) {
using namespace boost::asio::ip;
tcp::resolver resolver(io_service);
tcp::resolver::query q(
static_cast<std::string>(
coerce<ascii_string>(host.name()).underlying()),
static_cast<std::string>(
coerce<ascii_string>(coerce<string>(port)).underlying()));
boost::system::error_code host_error;
tcp::resolver::iterator endpoint = resolver.resolve(q, host_error), end;
if (host_error == boost::asio::error::host_not_found)
throw exceptions::host_not_found(host.name());
boost::system::error_code connect_error =
boost::asio::error::host_not_found;
while (connect_error && endpoint != end) {
socket.close();
timeout_wrapper timeout(socket, connect_error, c_connect_timeout);
socket.async_connect(*endpoint++, timeout.connect_async_function());
try {
timeout.complete();
} catch (exceptions::read_timeout &) {
connect_error = boost::asio::error::timed_out;
}
}
if (connect_error)
throw exceptions::connect_failure(connect_error, host, port);
}
}
fostlib::network_connection::network_connection(network_connection &&cnx)
: io_service(std::move(cnx.io_service)),
m_socket(std::move(cnx.m_socket)),
m_input_buffer(std::move(cnx.m_input_buffer)),
m_ssl_data(std::move(cnx.m_ssl_data)) {}
fostlib::network_connection::network_connection(
std::unique_ptr<io_context_type> io_service,
std::unique_ptr<socket_type> socket)
: io_service(std::move(io_service)),
m_socket(std::move(socket)),
m_input_buffer(new boost::asio::streambuf),
m_ssl_data(nullptr) {}
fostlib::network_connection::network_connection(
const host &h, nullable<port_number> p)
: io_service(new io_context_type),
m_socket(new socket_type(*io_service)),
m_input_buffer(new boost::asio::streambuf),
m_ssl_data(nullptr) {
const port_number port =
p.value_or(coerce<port_number>(h.service().value_or("0")));
json socks(c_socks_version.value());
if (!socks.isnull()) {
const host socks_host(coerce<host>(c_socks_host.value()));
connect(*io_service, *m_socket, socks_host,
coerce<port_number>(socks_host.service().value_or("0")));
if (c_socks_version.value() == json(4)) {
boost::asio::streambuf b;
// Build and send the command to establish the connection
b.sputc(0x4); // SOCKS v 4
b.sputc(0x1); // stream
b.sputc((port & 0xff00) >> 8);
b.sputc(port & 0xff); // Destination port
boost::asio::ip::address_v4::bytes_type bytes(
h.address().to_v4().to_bytes());
for (std::size_t p = 0; p < 4; ++p) b.sputc(bytes[p]);
b.sputc(0); // User ID
send(*m_socket, NULL, b);
// Receive the response
read(*m_socket, NULL, *m_input_buffer,
boost::asio::transfer_at_least(8));
if (m_input_buffer->sbumpc() != 0x00
|| m_input_buffer->sbumpc() != 0x5a)
throw exceptions::socket_error(
"SOCKS 4 error handling where the response values are "
"not 0x00 0x5a");
char ignore[6];
m_input_buffer->sgetn(ignore, 6);
} else {
throw exceptions::socket_error(
"SOCKS version not implemented",
coerce<string>(c_socks_version.value()));
}
} else {
connect(*io_service, *m_socket, h, port);
}
}
fostlib::network_connection::~network_connection() {
if (m_socket) { delete m_ssl_data; }
}
void fostlib::network_connection::start_ssl() {
m_ssl_data = new ssl(*io_service, *m_socket);
}
void fostlib::network_connection::start_ssl(f5::u8view hostname, bool verify) {
try {
m_ssl_data =
new ssl{*io_service, *m_socket,
static_cast<std::string>(hostname), verify};
} catch (boost::system::system_error &e) {
throw exceptions::socket_error(e.code());
}
}
fostlib::host fostlib::network_connection::remote_end() {
return host(m_socket->remote_endpoint().address().to_string());
}
network_connection &
fostlib::network_connection::operator<<(const const_memory_block &p) {
const unsigned char *begin = reinterpret_cast<const unsigned char *>(
p.first),
*end = reinterpret_cast<const unsigned char *>(
p.second);
std::size_t length = end - begin;
if (length) {
boost::asio::streambuf b;
for (std::size_t pos = 0; pos != length; ++pos) b.sputc(begin[pos]);
std::size_t sent(send(*m_socket, m_ssl_data, b));
b.consume(sent);
}
return *this;
}
network_connection &
fostlib::network_connection::operator<<(const utf8_string &s) {
boost::asio::streambuf b;
std::ostream os(&b);
os << s.underlying();
std::size_t length(send(*m_socket, m_ssl_data, b));
b.consume(length);
return *this;
}
network_connection &
fostlib::network_connection::operator<<(const std::stringstream &ss) {
return this->operator<<(utf8_string{ss.str()});
}
network_connection &fostlib::network_connection::operator>>(utf8_string &s) {
std::string next;
(*this) >> next;
s += utf8_string(next);
return *this;
}
network_connection &fostlib::network_connection::operator>>(std::string &s) {
std::size_t length(
read_until(*m_socket, m_ssl_data, *m_input_buffer, "\r\n"));
if (length >= 2) {
for (std::size_t c = 0; c < length - 2; ++c)
s += m_input_buffer->sbumpc();
m_input_buffer->sbumpc();
m_input_buffer->sbumpc();
} else {
throw exceptions::unexpected_eof(
"Could not find a \\r\\n sequence before network connection "
"ended");
}
return *this;
}
network_connection &
fostlib::network_connection::operator>>(std::vector<utf8> &v) {
const std::size_t chunk =
coerce<std::size_t>(c_large_read_chunk_size.value());
while (v.size() - m_input_buffer->size()
&& read(*m_socket, m_ssl_data, *m_input_buffer,
boost::asio::transfer_at_least(
std::min(v.size() - m_input_buffer->size(), chunk))))
;
if (m_input_buffer->size() < v.size()) {
exceptions::unexpected_eof exception(
"Could not read all of the requested bytes from the network "
"connection");
insert(exception.data(), "bytes read",
coerce<int64_t>(m_input_buffer->size()));
insert(exception.data(), "bytes expected", coerce<int64_t>(v.size()));
throw exception;
}
for (std::size_t p = 0; p < v.size(); ++p) v[p] = m_input_buffer->sbumpc();
return *this;
}
void fostlib::network_connection::operator>>(boost::asio::streambuf &b) {
while (m_input_buffer->size()) { b.sputc(m_input_buffer->sbumpc()); }
/**
* There's lots of ways that the read below can finish, in all of them
* we're going to ignore the error and just return whatever data we
* do have to the application. It asked for all data available so it will
* get all data that's available. If it's not happy with it, well it will
* have to sort the mess out.
*/
boost::system::error_code error;
read(*m_socket, m_ssl_data, b, boost::asio::transfer_all(), error);
}
/**
## `fostlib::exceptions::socket_error`
*/
fostlib::exceptions::socket_error::socket_error() noexcept {}
fostlib::exceptions::socket_error::socket_error(const string &message) noexcept
: exception(message) {}
fostlib::exceptions::socket_error::socket_error(
const string &message, const string &extra) noexcept
: exception(message) {
insert(data(), "context", extra);
}
fostlib::exceptions::socket_error::socket_error(
boost::system::error_code error) noexcept
: error(error) {
insert(data(), "error",
string(boost::lexical_cast<std::string>(error).c_str()));
}
fostlib::exceptions::socket_error::socket_error(
boost::system::error_code error, const string &message) noexcept
: exception(message), error(error) {
insert(data(), "error",
string(boost::lexical_cast<std::string>(error).c_str()));
}
fostlib::exceptions::socket_error::~socket_error() noexcept try {
} catch (...) { fostlib::absorb_exception(); }
wliteral const fostlib::exceptions::socket_error::message() const noexcept {
return L"Socket error";
}
/**
## `fostlib::exceptions::connect_failure`
*/
fostlib::exceptions::connect_failure::connect_failure(
boost::system::error_code error, const host &h, port_number p) noexcept
: socket_error(error) {
insert(data(), "host", h);
insert(data(), "port", p);
}
fostlib::wliteral const
fostlib::exceptions::connect_failure::message() const noexcept {
return L"Network connection failure";
}
/**
## `fostlib::exceptions::read_timeout`
*/
fostlib::exceptions::read_timeout::read_timeout() noexcept {}
wliteral const fostlib::exceptions::read_timeout::message() const noexcept {
return L"Read time out";
}
/**
## `fostlib::exceptions::read_error`
*/
fostlib::exceptions::read_error::read_error() noexcept {}
fostlib::exceptions::read_error::read_error(
boost::system::error_code error) noexcept
: socket_error(error) {}
wliteral const fostlib::exceptions::read_error::message() const noexcept {
return L"Read error";
}