Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
This repository was archived by the owner on Aug 11, 2020. It is now read-only.

Commit ccf1f4b

Browse filesBrowse files
authored
Merge pull request #94 from iot-dsa-v2/feature/154904_stcp_ssl_context
setup one ssl context per secure tcp connection
2 parents fa3c7fd + 98c7cc2 commit ccf1f4b
Copy full SHA for ccf1f4b

File tree

Expand file treeCollapse file tree

11 files changed

+160
-158
lines changed
Filter options
Expand file treeCollapse file tree

11 files changed

+160
-158
lines changed

‎benchmark/broker_throughput.cc

Copy file name to clipboardExpand all lines: benchmark/broker_throughput.cc
+51-58Lines changed: 51 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,10 @@ WrapperStrand get_client_wrapper_strand(shared_ptr_<App>& app,
8282
client_strand.ws_port = 8080;
8383
client_strand.ws_path = "/";
8484

85-
client_strand.client_connection_maker = [
86-
dsid_prefix = dsid_prefix, ws_host = client_strand.ws_host,
87-
ws_port = client_strand.ws_port
88-
](const SharedLinkStrandRef& strand)->shared_ptr_<Connection> {
85+
client_strand.client_connection_maker =
86+
[dsid_prefix = dsid_prefix, ws_host = client_strand.ws_host,
87+
ws_port = client_strand.ws_port](
88+
const SharedLinkStrandRef& strand) -> shared_ptr_<Connection> {
8989
return make_shared_<WsClientConnection>(false, strand, dsid_prefix,
9090
ws_host, ws_port);
9191
};
@@ -94,32 +94,27 @@ WrapperStrand get_client_wrapper_strand(shared_ptr_<App>& app,
9494
client_strand.ws_port = 8443;
9595
client_strand.ws_path = "/";
9696

97-
client_strand.client_connection_maker = [
98-
dsid_prefix = dsid_prefix, ws_host = client_strand.ws_host,
99-
ws_port = client_strand.ws_port
100-
](const SharedLinkStrandRef& strand) {
101-
return make_shared_<WsClientConnection>(true, strand, dsid_prefix,
102-
ws_host, ws_port);
103-
};
97+
client_strand.client_connection_maker =
98+
[dsid_prefix = dsid_prefix, ws_host = client_strand.ws_host,
99+
ws_port = client_strand.ws_port](const SharedLinkStrandRef& strand) {
100+
return make_shared_<WsClientConnection>(true, strand, dsid_prefix,
101+
ws_host, ws_port);
102+
};
104103
} else if (!protocol.compare("dss")) {
105104
client_strand.tcp_port = 4128;
106105

107-
static boost::asio::ssl::context context(boost::asio::ssl::context::sslv23);
108-
boost::system::error_code error_code;
109-
load_root_certificate(context, error_code);
110-
111-
client_strand.client_connection_maker = [
112-
dsid_prefix = dsid_prefix, tcp_host = client_strand.tcp_host,
113-
tcp_port = client_strand.tcp_port
114-
](const SharedLinkStrandRef& strand)->shared_ptr_<Connection> {
115-
return make_shared_<StcpClientConnection>(strand, context, dsid_prefix,
116-
tcp_host, tcp_port);
106+
client_strand.client_connection_maker =
107+
[dsid_prefix = dsid_prefix, tcp_host = client_strand.tcp_host,
108+
tcp_port = client_strand.tcp_port](
109+
const SharedLinkStrandRef& strand) -> shared_ptr_<Connection> {
110+
return make_shared_<StcpClientConnection>(strand, dsid_prefix, tcp_host,
111+
tcp_port);
117112
};
118113
} else {
119-
client_strand.client_connection_maker = [
120-
dsid_prefix = dsid_prefix, tcp_host = client_strand.tcp_host,
121-
tcp_port = client_strand.tcp_port
122-
](const SharedLinkStrandRef& strand)->shared_ptr_<Connection> {
114+
client_strand.client_connection_maker =
115+
[dsid_prefix = dsid_prefix, tcp_host = client_strand.tcp_host,
116+
tcp_port = client_strand.tcp_port](
117+
const SharedLinkStrandRef& strand) -> shared_ptr_<Connection> {
123118
return make_shared_<TcpClientConnection>(strand, dsid_prefix, tcp_host,
124119
tcp_port);
125120
};
@@ -179,10 +174,8 @@ int main(int argc, const char* argv[]) {
179174
strand.strand->set_responder_model(std::move(root_node));
180175
clients.emplace_back(client);
181176

182-
client->connect([
183-
=, &count = message_receive_count[i], &client = clients[i]
184-
](const shared_ptr_<Connection>&) {
185-
177+
client->connect([=, &count = message_receive_count[i],
178+
&client = clients[i]](const shared_ptr_<Connection>&) {
186179
SubscribeOptions options;
187180
options.qos = QosLevel::_1;
188181
for (int a = 0; a < client_count; ++a) {
@@ -214,37 +207,37 @@ int main(int argc, const char* argv[]) {
214207

215208
int64_t last_count = 0;
216209
int64_t last_time = DateTime::ms_since_epoch();
217-
std::function<void(const boost::system::error_code&)> timer_callback = [&](
218-
const boost::system::error_code& error) {
219-
try {
220-
int64_t current_time = DateTime::ms_since_epoch();
221-
int64_t count = 0;
222-
for (int i = 0; i < client_count; ++i) {
223-
count += message_receive_count[i];
224-
}
225-
if (current_time - last_time > 1000) {
226-
LOG_INFO("benchmark",
227-
LOG << "per second: " << ceil((count - last_count) * 1000.0 /
228-
(current_time - last_time))
229-
<< " total: " << count);
230-
last_time = current_time;
231-
last_count = count;
232-
}
233-
for (int i = 0; i < client_count; ++i) {
234-
strands[i]->dispatch([&, i]() {
235-
auto& node = root_nodes[i];
236-
for (int j = 0; j < msg_per_interval; ++j) {
237-
node->new_value();
210+
std::function<void(const boost::system::error_code&)> timer_callback =
211+
[&](const boost::system::error_code& error) {
212+
try {
213+
int64_t current_time = DateTime::ms_since_epoch();
214+
int64_t count = 0;
215+
for (int i = 0; i < client_count; ++i) {
216+
count += message_receive_count[i];
217+
}
218+
if (current_time - last_time > 1000) {
219+
LOG_INFO("benchmark", LOG << "per second: "
220+
<< ceil((count - last_count) * 1000.0 /
221+
(current_time - last_time))
222+
<< " total: " << count);
223+
last_time = current_time;
224+
last_count = count;
225+
}
226+
for (int i = 0; i < client_count; ++i) {
227+
strands[i]->dispatch([&, i]() {
228+
auto& node = root_nodes[i];
229+
for (int j = 0; j < msg_per_interval; ++j) {
230+
node->new_value();
231+
}
232+
});
238233
}
239-
});
240-
}
241234

242-
} catch (std::exception& e) {
243-
LOG_ERROR("benchmark", LOG << e.what());
244-
}
245-
timer.expires_from_now(interval);
246-
timer.async_wait(timer_callback);
247-
};
235+
} catch (std::exception& e) {
236+
LOG_ERROR("benchmark", LOG << e.what());
237+
}
238+
timer.expires_from_now(interval);
239+
timer.async_wait(timer_callback);
240+
};
248241
timer.async_wait(timer_callback);
249242
app->wait();
250243
}

‎src/sdk/core/editable_strand.cc

Copy file name to clipboardExpand all lines: src/sdk/core/editable_strand.cc
+11-20Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ void EditableStrand::check_injected() {
9696

9797
void EditableStrand::_prepare_inject_callback() {
9898
std::lock_guard<std::mutex> lock(_inject_mutex);
99-
_inject_callback = [ this, keep_ref = get_ref() ]() {
99+
_inject_callback = [this, keep_ref = get_ref()]() {
100100
if (is_destroyed()) return;
101101
_prepare_inject_callback();
102102
check_injected();
@@ -113,7 +113,7 @@ void EditableStrand::inject(std::function<void()>&& callback) {
113113
} else if (is_destroyed()) {
114114
// no need to call it
115115
// just destroy the callback in strand
116-
post([callback = std::move(callback)](){});
116+
post([callback = std::move(callback)]() {});
117117
} else {
118118
// callback not ready, but still need to put it in queue
119119
_inject_queue.emplace_back(std::move(callback));
@@ -125,37 +125,28 @@ string_ WrapperStrand::get_dsid() const {
125125
}
126126

127127
void WrapperStrand::set_client_connection_maker() {
128-
static boost::asio::ssl::context context(boost::asio::ssl::context::sslv23);
129-
boost::system::error_code error_code;
130-
131128
if (tcp_port > 0 && secure) {
132-
load_root_certificate(context, error_code);
133-
134129
client_connection_maker =
135-
[ dsid_prefix = dsid_prefix, tcp_host = tcp_host,
136-
tcp_port = tcp_port ](const SharedLinkStrandRef& strand)
137-
->shared_ptr_<Connection> {
138-
return make_shared_<StcpClientConnection>(strand, context, dsid_prefix,
139-
tcp_host, tcp_port);
130+
[dsid_prefix = dsid_prefix, tcp_host = tcp_host, tcp_port = tcp_port](
131+
const SharedLinkStrandRef& strand) -> shared_ptr_<Connection> {
132+
return make_shared_<StcpClientConnection>(strand, dsid_prefix, tcp_host,
133+
tcp_port);
140134
};
141135
return;
142136
}
143137
if (ws_port > 0) {
144138
client_connection_maker =
145-
[
146-
dsid_prefix = dsid_prefix, ws_host = ws_host, ws_port = ws_port,
147-
secure = secure
148-
](const SharedLinkStrandRef& strand)
149-
->shared_ptr_<Connection> {
139+
[dsid_prefix = dsid_prefix, ws_host = ws_host, ws_port = ws_port,
140+
secure = secure](
141+
const SharedLinkStrandRef& strand) -> shared_ptr_<Connection> {
150142
return make_shared_<WsClientConnection>(secure, strand, dsid_prefix,
151143
ws_host, ws_port);
152144
};
153145
return;
154146
}
155147
client_connection_maker =
156-
[ dsid_prefix = dsid_prefix, tcp_host = tcp_host,
157-
tcp_port = tcp_port ](const SharedLinkStrandRef& strand)
158-
->shared_ptr_<Connection> {
148+
[dsid_prefix = dsid_prefix, tcp_host = tcp_host, tcp_port = tcp_port](
149+
const SharedLinkStrandRef& strand) -> shared_ptr_<Connection> {
159150
return make_shared_<TcpClientConnection>(strand, dsid_prefix, tcp_host,
160151
tcp_port);
161152
};

‎src/sdk/network/tcp/stcp_client_connection.cc

Copy file name to clipboardExpand all lines: src/sdk/network/tcp/stcp_client_connection.cc
+33-23Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,30 @@
44

55
#include "module/logger.h"
66
#include "stcp_client_connection.h"
7+
#include "util/certificate.h"
78

89
namespace dsa {
910

10-
StcpClientConnection::StcpClientConnection(const SharedLinkStrandRef &strand,
11-
boost::asio::ssl::context& context,
11+
StcpClientConnection::StcpClientConnection(const SharedLinkStrandRef& strand,
1212
const string_& dsid_prefix,
1313
const string_& tcp_host,
1414
uint16_t tcp_port)
15-
: StcpConnection(strand, context, dsid_prefix),
15+
: StcpConnection(strand, dsid_prefix),
1616
_hostname(tcp_host),
17-
_port(tcp_port) {
18-
_socket.set_verify_mode(boost::asio::ssl::verify_peer);
19-
_socket.set_verify_callback(
17+
_port(tcp_port),
18+
_context(boost::asio::ssl::context::sslv23) {
19+
boost::system::error_code error_code;
20+
_context.load_verify_file("certificate.pem", error_code);
21+
if (error_code) {
22+
LOG_FATAL(__FILENAME__, LOG << "Failed to verify cetificate");
23+
}
24+
25+
load_root_certificate(_context, error_code);
26+
27+
_socket = make_shared_<ssl_socket>(strand->get_io_context(), _context);
28+
29+
_socket->set_verify_mode(boost::asio::ssl::verify_peer);
30+
_socket->set_verify_callback(
2031
[this](bool preverified,
2132
boost::asio::ssl::verify_context& context) -> bool {
2233
return verify_certificate(preverified, context);
@@ -27,17 +38,16 @@ void StcpClientConnection::connect(size_t reconnect_interval) {
2738
// connect to server
2839
using tcp = boost::asio::ip::tcp;
2940
tcp::resolver resolver(_shared_strand->get_io_context());
30-
LOG_FINE(
31-
__FILENAME__,
32-
LOG << "Secure TCP client connecting to " << _hostname << ":" << _port);
41+
LOG_FINE(__FILENAME__, LOG << "Secure TCP client connecting to " << _hostname
42+
<< ":" << _port);
3343

3444
tcp::resolver::results_type results =
3545
resolver.resolve(tcp::resolver::query(_hostname, std::to_string(_port)));
3646
boost::asio::async_connect(
37-
_socket.lowest_layer(), results.begin(), results.end(),
47+
_socket->lowest_layer(), results.begin(), results.end(),
3848
// capture shared_ptr to keep the instance
3949
// capture this to access protected member
40-
[ connection = share_this<StcpConnection>(), this ](
50+
[connection = share_this<StcpConnection>(), this](
4151
const boost::system::error_code& error,
4252
tcp::resolver::iterator) mutable {
4353
if (is_destroyed()) return;
@@ -47,20 +57,20 @@ void StcpClientConnection::connect(size_t reconnect_interval) {
4757
return;
4858
}
4959

50-
_socket.async_handshake(boost::asio::ssl::stream_base::client, [
51-
connection = connection, this
52-
](const boost::system::error_code& error) mutable {
53-
if (error != boost::system::errc::success) {
54-
destroy_in_strand(std::move(connection));
55-
LOG_ERROR(__FILENAME__,
56-
LOG << "Client SSL handshake failed");
57-
return;
58-
}
60+
_socket->async_handshake(
61+
boost::asio::ssl::stream_base::client,
62+
[connection = connection,
63+
this](const boost::system::error_code& error) mutable {
64+
if (error != boost::system::errc::success) {
65+
destroy_in_strand(std::move(connection));
66+
LOG_ERROR(__FILENAME__, LOG << "Client SSL handshake failed");
67+
return;
68+
}
5969

60-
start_client_f0();
70+
start_client_f0();
6171

62-
StcpConnection::start_read(std::move(connection));
63-
});
72+
StcpConnection::start_read(std::move(connection));
73+
});
6474
});
6575
// use half of the reconnection time to resolve host
6676
start_deadline_timer((reconnect_interval >> 1) + 1);

‎src/sdk/network/tcp/stcp_client_connection.h

Copy file name to clipboardExpand all lines: src/sdk/network/tcp/stcp_client_connection.h
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@ class Client;
1616
// Handles client side of DSA handshake and starts read loop.
1717
class StcpClientConnection final : public StcpConnection {
1818
private:
19+
boost::asio::ssl::context _context;
20+
1921
protected:
2022
string_ _hostname;
2123
uint16_t _port;
2224

2325
public:
2426
StcpClientConnection(const SharedLinkStrandRef &strand,
25-
boost::asio::ssl::context &context,
2627
const string_ &dsid_prefix, const string_ &tcp_host,
2728
uint16_t tcp_port);
2829

‎src/sdk/network/tcp/stcp_connection.cc

Copy file name to clipboardExpand all lines: src/sdk/network/tcp/stcp_connection.cc
+6-8Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,12 @@
1010
namespace dsa {
1111

1212
StcpConnection::StcpConnection(const SharedLinkStrandRef &strand,
13-
boost::asio::ssl::context &context,
1413
const string_ &dsid_prefix, const string_ &path)
15-
: BaseSocketConnection(strand, dsid_prefix, path),
16-
_socket(strand->get_io_context(), context) {}
14+
: BaseSocketConnection(strand, dsid_prefix, path) {}
1715

1816
void StcpConnection::destroy_impl() {
1917
LOG_DEBUG(__FILENAME__, LOG << "connection closed");
20-
_socket.lowest_layer().close();
18+
_socket->lowest_layer().close();
2119
Connection::destroy_impl();
2220
}
2321

@@ -31,9 +29,9 @@ void StcpConnection::start_read(shared_ptr_<Connection> &&connection) {
3129
if (_read_next * 2 > buffer.size() && buffer.size() < MAX_BUFFER_SIZE) {
3230
buffer.resize(buffer.size() * 4);
3331
}
34-
_socket.async_read_some(
32+
_socket->async_read_some(
3533
boost::asio::buffer(&buffer[partial_size], buffer.size() - partial_size),
36-
[ this, connection = std::move(connection), partial_size ](
34+
[this, connection = std::move(connection), partial_size](
3735
const boost::system::error_code &err, size_t transferred) mutable {
3836
read_loop_(std::move(connection), partial_size, err, transferred);
3937
});
@@ -63,7 +61,7 @@ void StcpConnection::WriteBuffer::add(const Message &message, int32_t rid,
6361
}
6462
void StcpConnection::WriteBuffer::write(WriteHandler &&callback) {
6563
boost::asio::async_write(
66-
connection._socket,
64+
*connection._socket,
6765
boost::asio::buffer(connection._write_buffer.data(), size),
6866
[callback = std::move(callback)](const boost::system::error_code &error,
6967
size_t bytes_transferred) {
@@ -74,7 +72,7 @@ void StcpConnection::WriteBuffer::write(WriteHandler &&callback) {
7472
}
7573

7674
ssl_socket::lowest_layer_type &StcpConnection::socket() {
77-
return _socket.lowest_layer();
75+
return _socket->lowest_layer();
7876
}
7977

8078
} // namespace dsa

‎src/sdk/network/tcp/stcp_connection.h

Copy file name to clipboardExpand all lines: src/sdk/network/tcp/stcp_connection.h
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ class StcpConnection : public BaseSocketConnection {
3636
void continue_read_loop(shared_ptr_<Connection> &&sthis) final {
3737
start_read(std::move(sthis));
3838
}
39-
ssl_socket _socket;
39+
shared_ptr_<ssl_socket> _socket;
4040

4141
void destroy_impl() override;
4242

4343
public:
44-
StcpConnection(const SharedLinkStrandRef &strand, boost::asio::ssl::context &context,
45-
const string_ &dsid_prefix, const string_ &path = "");
44+
StcpConnection(const SharedLinkStrandRef &strand, const string_ &dsid_prefix,
45+
const string_ &path = "");
4646

4747
void start_read(shared_ptr_<Connection> &&connection) final;
4848

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.