forked from libbitcoin/libbitcoin-network
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsession_inbound.cpp
More file actions
306 lines (248 loc) · 9.67 KB
/
Copy pathsession_inbound.cpp
File metadata and controls
306 lines (248 loc) · 9.67 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
/**
* Copyright (c) 2011-2025 libbitcoin developers (see AUTHORS)
*
* This file is part of libbitcoin.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <bitcoin/network/sessions/session_inbound.hpp>
#include <utility>
#include <bitcoin/system.hpp>
#include <bitcoin/network/log/log.hpp>
#include <bitcoin/network/p2p.hpp>
#include <bitcoin/network/protocols/protocols.hpp>
namespace libbitcoin {
namespace network {
#define CLASS session_inbound
using namespace system;
using namespace std::placeholders;
// Bind throws (ok).
// Shared pointers required in handler parameters so closures control lifetime.
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
BC_PUSH_WARNING(SMART_PTR_NOT_NEEDED)
BC_PUSH_WARNING(NO_VALUE_OR_CONST_REF_SHARED_PTR)
session_inbound::session_inbound(p2p& network, uint64_t identifier) NOEXCEPT
: session(network, identifier), tracker<session_inbound>(network.log)
{
}
// Start/stop sequence.
// ----------------------------------------------------------------------------
void session_inbound::start(result_handler&& handler) NOEXCEPT
{
BC_ASSERT_MSG(stranded(), "strand");
if (!settings().inbound_enabled())
{
LOGN("Not configured for inbound connections.");
handler(error::success);
unsubscribe_close();
return;
}
session::start(BIND(handle_started, _1, std::move(handler)));
}
void session_inbound::handle_started(const code& ec,
const result_handler& handler) NOEXCEPT
{
BC_ASSERT_MSG(stranded(), "strand");
BC_ASSERT_MSG(!stopped(), "session stopped in start");
if (ec)
{
handler(ec);
unsubscribe_close();
return;
}
LOGN("Accepting " << settings().inbound_connections << " connections on "
<< settings().binds.size() << " bindings.");
for (const auto& bind: settings().binds)
{
const auto acceptor = create_acceptor();
// Require that all acceptors at least start.
if (const auto error_code = acceptor->start(bind))
{
handler(error_code);
return;
}
LOGN("Bound to endpoint [" << acceptor->local() << "].");
// Subscribe acceptor to stop desubscriber.
subscribe_stop([=](const code&) NOEXCEPT
{
acceptor->stop();
return false;
});
start_accept(error::success, acceptor);
}
handler(error::success);
}
// Accept cycle.
// ----------------------------------------------------------------------------
// Attempt to accept peers on each configured endpoint.
void session_inbound::start_accept(const code&,
const acceptor::ptr& acceptor) NOEXCEPT
{
BC_ASSERT_MSG(stranded(), "strand");
// Terminates accept loop (and acceptor is restartable).
if (stopped())
return;
acceptor->accept(BIND(handle_accept, _1, _2, acceptor));
}
void session_inbound::handle_accept(const code& ec,
const socket::ptr& socket, const acceptor::ptr& acceptor) NOEXCEPT
{
BC_ASSERT_MSG(stranded(), "strand");
// Guard restartable timer (shutdown delay).
if (stopped())
{
if (socket) socket->stop();
return;
}
if (ec == error::service_suspended)
{
////LOGS("Suspended inbound channel start.");
defer(BIND(start_accept, _1, acceptor));
return;
}
// There was an error accepting the channel, so try again after delay.
if (ec)
{
BC_ASSERT_MSG(!socket || socket->stopped(), "unexpected socket");
LOGF("Failed to accept inbound connection, " << ec.message());
defer(BIND(start_accept, _1, acceptor));
return;
}
// There was no error, so listen again without delay.
start_accept(error::success, acceptor);
const auto address = socket->authority().to_address_item();
if (!whitelisted(address))
{
////LOGS("Dropping not whitelisted connection [" << socket->authority() << "].");
socket->stop();
return;
}
if (blacklisted(address))
{
////LOGS("Dropping blacklisted connection [" << socket->authority() << "].");
socket->stop();
return;
}
// Could instead stop listening when at limit, though this is simpler.
if (inbound_channel_count() >= settings().inbound_connections)
{
LOGS("Dropping oversubscribed connection [" << socket->authority() << "].");
socket->stop();
return;
}
const auto channel = create_channel(socket, false);
LOGS("Accepted inbound connection [" << channel->authority() << "] on binding ["
<< acceptor->local() << "].");
start_channel(channel,
BIND(handle_channel_start, _1, channel),
BIND(handle_channel_stop, _1, channel));
}
bool session_inbound::blacklisted(const config::address& address) const NOEXCEPT
{
return settings().blacklisted(address);
}
bool session_inbound::whitelisted(const config::address& address) const NOEXCEPT
{
return settings().whitelisted(address);
}
// Completion sequence.
// ----------------------------------------------------------------------------
void session_inbound::attach_handshake(const channel::ptr& channel,
result_handler&& handler) NOEXCEPT
{
BC_ASSERT_MSG(channel->stranded(), "channel strand");
BC_ASSERT_MSG(channel->paused(), "channel not paused for attach");
// Inbound does not require any node services.
constexpr auto minimum_services = messages::service::node_none;
const auto maximum_services = settings().services_maximum;
// Protocol must pause the channel after receiving version and verack.
const auto self = shared_from_this();
const auto relay = settings().enable_relay;
const auto reject = settings().enable_reject;
const auto address_v2 = settings().enable_address_v2;
// protocol_version_70016 sends and receives send_address_v2 even though
// inbound connections do not accept addresses. There is no message to
// disable address broadcasting, so this is just allowed to upgrade.
// Address v2 can be disabled, independent of version.
if (is_configured(messages::level::bip155) && address_v2)
channel->attach<protocol_version_70016>(self, minimum_services,
maximum_services, relay, reject)
->shake(std::move(handler));
// Protocol versions are cumulative, but reject is deprecated.
else if (is_configured(messages::level::bip61) && reject)
channel->attach<protocol_version_70002>(self, minimum_services,
maximum_services, relay)
->shake(std::move(handler));
// settings().enable_relay is always passed to the peer during handshake.
else if (is_configured(messages::level::bip37))
channel->attach<protocol_version_70001>(self, minimum_services,
maximum_services, relay)
->shake(std::move(handler));
else if (is_configured(messages::level::version_message))
channel->attach<protocol_version_106>(self, minimum_services,
maximum_services)
->shake(std::move(handler));
}
void session_inbound::handle_channel_start(const code&,
const channel::ptr&) NOEXCEPT
{
BC_ASSERT_MSG(stranded(), "strand");
////LOGS("Inbound channel start [" << channel->authority() << "] "
//// << ec.message());
}
void session_inbound::attach_protocols(
const channel::ptr& channel) NOEXCEPT
{
BC_ASSERT_MSG(channel->stranded(), "channel strand");
BC_ASSERT_MSG(channel->paused(), "channel not paused for protocol attach");
const auto self = shared_from_this();
// Alert is deprecated, independent of version.
if (channel->is_negotiated(messages::level::alert_message) &&
settings().enable_alert)
channel->attach<protocol_alert_311>(self)->start();
// Reject is deprecated, independent of version.
if (channel->is_negotiated(messages::level::bip61) &&
settings().enable_reject)
channel->attach<protocol_reject_70002>(self)->start();
if (channel->is_negotiated(messages::level::bip31))
channel->attach<protocol_ping_60001>(self)->start();
else if (channel->is_negotiated(messages::level::version_message))
channel->attach<protocol_ping_106>(self)->start();
// Attach is overridden to disable inbound address protocols.
if (settings().enable_address_v2)
{
////// Sending address v2 is enabled in handshake.
////if (channel->send_address_v2())
//// channel->attach<protocol_address_out_70016>(self)->start();
}
if (settings().enable_address)
{
if (channel->is_negotiated(messages::level::getaddr_message))
channel->attach<protocol_address_out_209>(self)->start();
////else if (channel->is_negotiated(messages::level::version_message))
//// channel->attach<protocol_address_out_106>(self)->start();
}
}
void session_inbound::handle_channel_stop(const code& LOG_ONLY(ec),
const channel::ptr& LOG_ONLY(channel)) NOEXCEPT
{
BC_ASSERT_MSG(stranded(), "strand");
LOGS("Inbound channel stop [" << channel->authority() << "] "
<< ec.message());
}
BC_POP_WARNING()
BC_POP_WARNING()
BC_POP_WARNING()
} // namespace network
} // namespace libbitcoin