• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

libbitcoin / libbitcoin-network / 45096769114

23 Nov 2026 01:49AM UTC coverage: 94.187% (-0.03%) from 94.216%
45096769114

push

github

web-flow
Merge pull request #1058 from evoskuil/master

http compliance fixes.

39 of 54 new or added lines in 5 files covered. (72.22%)

165 existing lines in 20 files now uncovered.

16204 of 17204 relevant lines covered (94.19%)

2171.08 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.0
/src/sessions/session_server.cpp
1
/**
2
 * Copyright (c) 2011-2026 libbitcoin developers
3
 *
4
 * This file is part of libbitcoin.
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Affero General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU Affero General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Affero General Public License
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19
#include <bitcoin/network/sessions/session_server.hpp>
20

21
#include <bitcoin/network/config/config.hpp>
22
#include <bitcoin/network/define.hpp>
23
#include <bitcoin/network/net.hpp>
24
#include <bitcoin/network/sessions/session.hpp>
25

26
namespace libbitcoin {
27
namespace network {
28

29
#define CLASS session_server
30

31
using namespace system;
32
using namespace std::placeholders;
33

34
// Bind throws (ok).
35
// Shared pointers required in handler parameters so closures control lifetime.
36
BC_PUSH_WARNING(NO_VALUE_OR_CONST_REF_SHARED_PTR)
37
BC_PUSH_WARNING(SMART_PTR_NOT_NEEDED)
38
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
39

40
session_server::session_server(net& network, uint64_t identifier,
11 ✔
41
    const options_t& options) NOEXCEPT
11 ✔
42
  : session(network, identifier),
43
    ////network_(network),
44
    options_(options),
11 ✔
45
    name_(options.name)
11 ✔
46
{
47
}
11 ✔
48

49
// Start/stop sequence.
50
// ----------------------------------------------------------------------------
51

52
void session_server::start(result_handler&& handler) NOEXCEPT
6 ✔
53
{
54
    BC_ASSERT(stranded());
6 ✔
55

56
    if (!options_.enabled())
6 ✔
57
    {
58
        LOGN("Not configured for " << name_ << " connections.");
1 ✔
59
        handler(error::success);
1 ✔
60
        unsubscribe_close();
1 ✔
61
        return;
1 ✔
62
    }
63

64
    session::start(BIND(handle_started, _1, std::move(handler)));
5 ✔
65
}
66

67
void session_server::handle_started(const code& ec,
5 ✔
68
    const result_handler& handler) NOEXCEPT
69
{
70
    BC_ASSERT(stranded());
5 ✔
71
    BC_ASSERT_MSG(!stopped(), "session stopped in start");
5 ✔
72

73
    if (ec)
5 ✔
74
    {
75
        handler(ec);
×
76
        unsubscribe_close();
×
77
        return;
×
78
    }
79

80
    LOGN("Accepting " << options_.connections << " " << name_
5 ✔
81
        << " connections on " << options_.binds.size()
82
        << " clear and " << (options_.secure() ? options_.safes.size() : zero)
83
        << " private" << (options_.authenticate() ? " authenticated" : "")
84
        << " bindings.");
85

86
    if (options_.secure())
5 ✔
87
    {
88
        if (const auto code = do_accept(options_.safes,
×
89
            options_.secure_context(), true))
×
90
        {
91
            handler(code);
×
92
            return;
×
93
        }
94
    }
95

96
    handler(do_accept(options_.binds, options_.clear_context(), false));
5 ✔
97
}
98

99
zmtp::role session_server::role() const NOEXCEPT
5 ✔
100
{
101
    return zmtp::role::undefined;
5 ✔
102
}
103

104
// private
105
code session_server::do_accept(const config::authorities& binds,
5 ✔
106
    const socket::context& context, bool secure) NOEXCEPT
107
{
108
    BC_ASSERT(stranded());
5 ✔
109

110
    for (const auto& bind: binds)
10 ✔
111
    {
112
        // Each acceptor owns its parameters.
113
        const auto acceptor = create_service(
5 ✔
114
        {
115
            .connect_timeout = network_settings().connect_timeout(options_),
5 ✔
116
            .maximum_request = options_.maximum_request,
5 ✔
117
            .minimum_buffer = options_.minimum_response,
5 ✔
118
            .maximum_buffer = options_.maximum_buffer,
5 ✔
119
            .context = context,
120
            .role = role()
5 ✔
121
        });
5 ✔
122

123
        // Require that all acceptors at least start.
124
        if (const auto ec = acceptor->start(bind))
5 ✔
125
            return ec;
×
126

127
        LOGN("Bound to " << (secure ? "private " : "clear ")
5 ✔
128
            << name_ << " endpoint [" << acceptor->local() << "].");
129

130
        // Subscribe acceptor to stop desubscriber.
131
        subscribe_stop([=](const code&) NOEXCEPT
5 ✔
132
        {
133
            acceptor->stop();
5 ✔
134
            return false;
5 ✔
135
        });
136

137
        start_accept(error::success, acceptor, secure);
5 ✔
138
    }
5 ✔
139

140
    return error::success;
5 ✔
141
}
142

143
// Accept cycle.
144
// ----------------------------------------------------------------------------
145

146
// Attempt to accept clients on each configured endpoint.
147
void session_server::start_accept(const code&,
11 ✔
148
    const acceptor::ptr& acceptor, bool secure) NOEXCEPT
149
{
150
    BC_ASSERT(stranded());
11 ✔
151

152
    // Terminates accept loop (and acceptor is restartable).
153
    if (stopped())
11 ✔
154
        return;
155

156
    acceptor->accept(BIND(handle_connected, _1, acceptor, secure),
6 ✔
157
        BIND(handle_accepted, _1, _2, acceptor, secure));
12 ✔
158
}
159

160
void session_server::handle_connected(const code& ec,
6 ✔
161
    const acceptor::ptr& acceptor, bool secure) NOEXCEPT
162
{
163
    BC_ASSERT(stranded());
6 ✔
164

165
    // Guard restartable timer (shutdown delay).
166
    if (stopped())
6 ✔
167
        return;
168

169
    // Suspension is controlled via acceptor construction.
170
    if (ec == error::service_suspended)
6 ✔
171
    {
172
        ////LOGS("Suspended " << name_ << " channel start.");
173
        defer(BIND(start_accept, _1, acceptor, secure));
×
174
        return;
×
175
    }
176

177
    // There was an error accepting the connection, so try again.
178
    if (ec)
6 ✔
179
    {
180
        LOGF("Failed to accept " << (secure ? "private " : "clear ")
×
181
            << name_ << " connection, " << ec.message());
182
        ////defer(BIND(start_accept, _1, acceptor, secure));
183
        start_accept(error::success, acceptor, secure);
×
184
        return;
×
185
    }
186

187
    // Pending handshakes count against the limit, so listen after delay.
188
    ++handshake_count_;
6 ✔
189
    const auto pending = ceilinged_add(channel_count_, handshake_count_);
6 ✔
190
    if (!enabled() || pending >= options_.connections)
6 ✔
191
    {
192
        defer(BIND(start_accept, _1, acceptor, secure));
6 ✔
193
        return;
6 ✔
194
    }
195

196
    // The handshake proceeds independently, so listen again without delay.
197
    start_accept(error::success, acceptor, secure);
×
198
}
199

200
void session_server::handle_accepted(const code& ec, const socket::ptr& socket,
6 ✔
201
    const acceptor::ptr& acceptor, bool secure) NOEXCEPT
202
{
203
    BC_ASSERT(stranded());
6 ✔
204
    handshake_count_ = floored_subtract(handshake_count_, one);
6 ✔
205

206
    // Guard restartable timer (shutdown delay).
207
    if (stopped())
6 ✔
208
    {
209
        if (socket) socket->stop();
×
210
        return;
4 ✔
211
    }
212

213
    if (ec)
6 ✔
214
    {
215
        BC_ASSERT_MSG(!socket || socket->stopped(), "unexpected socket");
×
216
        LOGS("Failed to handshake " << (secure ? "private " : "clear ")
×
217
            << name_ << " connection, " << ec.message());
218
        return;
×
219
    }
220

221
    if (!enabled())
6 ✔
222
    {
223
        LOGS("Dropping " << (secure ? "private " : "clear ")
1 ✔
224
            << name_ << " connection (disabled).");
225
        socket->stop();
1 ✔
226
        return;
1 ✔
227
    }
228

229
    // Must drop without responding, otherwise count will overflow.
230
    if (channel_count_ >= options_.connections)
5 ✔
231
    {
232
        LOGS("Dropping " << (secure ? "private " : "clear ")
1 ✔
233
            << name_ << " connection [" << socket->endpoint()
234
            << "] (oversubscribed).");
235
        socket->stop();
1 ✔
236
        return;
1 ✔
237
    }
238

239
    if (!whitelisted(socket->address()))
4 ✔
240
    {
241
        LOGV("Dropping not whitelisted client [" << socket->endpoint() << "].");
1 ✔
242
        socket->stop();
1 ✔
243
        return;
1 ✔
244
    }
245

246
    if (blacklisted(socket->address()))
3 ✔
247
    {
248
        LOGV("Dropping blacklisted client [" << socket->endpoint() << "].");
1 ✔
249
        socket->stop();
1 ✔
250
        return;
1 ✔
251
    }
252

253
    // Creates channel_xxxx cast as channel::ptr.
254
    const auto channel = create_channel(socket);
2 ✔
255

256
    LOGS("Accepted " << (secure ? "private " : "clear ")
2 ✔
257
        << name_ << " connection [" << channel->endpoint()
258
        << "] on binding [" << acceptor->local() << "].");
259

260
    start_channel(channel,
4 ✔
261
        BIND(handle_channel_start, _1, channel),
4 ✔
262
        BIND(handle_channel_stop, _1, channel));
4 ✔
263
}
2 ✔
264

265
bool session_server::blacklisted(const config::address& address) const NOEXCEPT
3 ✔
266
{
267
    return network_settings().blacklisted(address);
3 ✔
268
}
269

270
bool session_server::whitelisted(const config::address& address) const NOEXCEPT
4 ✔
271
{
272
    return network_settings().whitelisted(address);
4 ✔
273
}
274

275
bool session_server::enabled() const NOEXCEPT
10 ✔
276
{
277
    return true;
10 ✔
278
}
279

280
// Channel sequence.
281
// ----------------------------------------------------------------------------
282

283
// Some client types do not utilize a handshake, so default is bypassed.
UNCOV
284
void session_server::attach_handshake(const channel::ptr&,
×
285
    result_handler&&) NOEXCEPT
286
{
UNCOV
287
    BC_ASSERT(false);
×
288
}
289

290
// Handshake bypassed, channel remains paused until after protocol attach.
291
void session_server::do_attach_handshake(
3 ✔
292
    const channel::ptr& BC_DEBUG_ONLY(channel),
293
    const result_handler& handshake) NOEXCEPT
294
{
295
    BC_ASSERT_MSG(channel->stranded(), "channel strand");
3 ✔
296
    BC_ASSERT_MSG(!channel->held(), "gate held before handshake attach");
3 ✔
297
    handshake(error::success);
3 ✔
298
}
3 ✔
299

300
// Completion sequence.
301
// ----------------------------------------------------------------------------
302

303
void session_server::handle_channel_start(const code& LOG_ONLY(ec),
2 ✔
304
    const channel::ptr& LOG_ONLY(channel)) NOEXCEPT
305
{
306
    BC_ASSERT(stranded());
2 ✔
307
    LOGS("Inbound " << name_ << " channel start [" << channel->endpoint()
2 ✔
308
        << "] " << ec.message());
309

310
    BC_ASSERT(!is_zero(add1(channel_count_)));
2 ✔
311
    channel_count_ = ceilinged_add(channel_count_, one);
2 ✔
312
}
2 ✔
313

314
void session_server::handle_channel_stop(const code& LOG_ONLY(ec),
2 ✔
315
    const channel::ptr& LOG_ONLY(channel)) NOEXCEPT
316
{
317
    BC_ASSERT(stranded());
2 ✔
318
    LOGS("Inbound " << name_ << " channel stop [" << channel->endpoint()
2 ✔
319
        << "] " << ec.message());
320

321
    // Start/stop race with handle_channel_start can leave channel_count_ zero
322
    // here before first increment.
323
    ////BC_ASSERT(!is_zero(channel_count_));
324
    channel_count_ = floored_subtract(channel_count_, one);
2 ✔
325
}
2 ✔
326

327
BC_POP_WARNING()
328
BC_POP_WARNING()
329
BC_POP_WARNING()
330

331
} // namespace network
332
} // namespace libbitcoin
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc