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

libbitcoin / libbitcoin-network / 36663710005

30 Sep 2026 03:17AM UTC coverage: 94.992% (+47.2%) from 47.759%
36663710005

push

github

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

Fix tls handshake test harness, mac global ns conflict, delint.

1 of 1 new or added line in 1 file covered. (100.0%)

9 existing lines in 2 files now uncovered.

10642 of 11203 relevant lines covered (94.99%)

669.35 hits per line

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

96.49
/src/settings.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/settings.hpp>
20

21
#include <filesystem>
22
#include <bitcoin/network/async/async.hpp>
23
#include <bitcoin/network/config/config.hpp>
24
#include <bitcoin/network/messages/messages.hpp>
25

26
namespace libbitcoin {
27
namespace network {
28

29
using namespace system;
30
using namespace messages::peer;
31

32
// Default (zero) threads is capped at this many hardware threads.
33
constexpr auto maximum_default_threads = 32_size;
34

35
// socks5
36
// ----------------------------------------------------------------------------
37

38
settings::socks5::socks5() NOEXCEPT
892 ✔
39
{
40
}
×
41

42
bool settings::socks5::proxied() const NOEXCEPT
212 ✔
43
{
44
    return is_nonzero(socks.port());
212 ✔
45
}
46

47
bool settings::socks5::authenticated() const NOEXCEPT
31 ✔
48
{
49
    return !(username.empty() && password.empty());
31 ✔
50
}
51

52
// sam
53
// ----------------------------------------------------------------------------
54

55
settings::sam::sam() NOEXCEPT
482 ✔
56
{
57
}
482 ✔
58

59
// geti2p.net/en/docs/api/samv3
60
// A destination is a fixed prefix and a length-prefixed certificate.
61
constexpr auto destination_size = 387_size;
62
constexpr auto certificate_position = 385_size;
63

64
// I2P has no ports.
65
constexpr uint16_t sam_port = 0;
66

67
// The destination (public) prefix of a private key, empty if truncated.
68
static data_chunk to_destination(const data_chunk& private_key) NOEXCEPT
16 ✔
69
{
70
    read::bytes::copy source(private_key);
16 ✔
71
    source.skip_bytes(certificate_position);
16 ✔
72
    const auto size = destination_size + source.read_2_bytes_big_endian();
16 ✔
73

74
    if (!source || private_key.size() < size)
16 ✔
75
        return {};
2 ✔
76

77
    return { private_key.begin(), std::next(private_key.begin(), size) };
14 ✔
78
}
79

80
// The address of a stored private key (base64), unspecified if invalid.
81
config::address settings::sam::to_self(const std::string& key) NOEXCEPT
18 ✔
82
{
83
    data_chunk private_key{};
18 ✔
84
    if (!decode_base64(private_key, key))
18 ✔
85
        return {};
2 ✔
86

87
    const auto destination = to_destination(private_key);
16 ✔
88
    if (destination.empty())
16 ✔
89
        return {};
2 ✔
90

91
    return address_item
14 ✔
92
    {
93
        unix_time(), service::node_none, i2p_t{ sha256_hash(destination) },
28 ✔
94
        sam_port
95
    };
14 ✔
96
}
97

98
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
99
code settings::sam::initialize() NOEXCEPT
3 ✔
100
{
101
    if (key_path.empty())
3 ✔
102
        return error::success;
2 ✔
103

104
    try
105
    {
106
        ifstream file{ key_path, ifstream::in };
1 ✔
107
        if (file.good() && !!std::getline(file, key) && !key.empty())
1 ✔
108
            self = to_self(key);
×
109
    }
1 ✔
110
    catch (const std::exception&)
×
111
    {
112
        key.clear();
113
    }
×
114

115
    return error::success;
1 ✔
116
}
117
BC_POP_WARNING()
118

119
bool settings::sam::bridged() const NOEXCEPT
113 ✔
120
{
121
    return is_nonzero(bridge.port());
113 ✔
122
}
123

124
bool settings::sam::authenticated() const NOEXCEPT
3 ✔
125
{
126
    return !(username.empty() && password.empty());
3 ✔
127
}
128

129
// tcp_server
130
// ----------------------------------------------------------------------------
131

132
settings::tcp_server::tcp_server(const std::string_view& logging_name) NOEXCEPT
1,632 ✔
133
  : name(logging_name)
1,632 ✔
134
{
135
}
1,632 ✔
136

137
bool settings::tcp_server::enabled() const NOEXCEPT
251 ✔
138
{
139
    return to_bool(connections);
251 ✔
140
}
141

142
steady_clock::duration settings::tcp_server::inactivity() const NOEXCEPT
192 ✔
143
{
144
    return minutes{ inactivity_minutes };
192 ✔
145
}
146

147
steady_clock::duration settings::tcp_server::expiration() const NOEXCEPT
189 ✔
148
{
149
    return minutes{ expiration_minutes };
189 ✔
150
}
151

152
asio::endpoint settings::tcp_server::binding(size_t slot) const NOEXCEPT
129 ✔
153
{
154
    if (binds.empty())
129 ✔
155
        return {};
156

157
    return binds.at(slot % binds.size()).to_endpoint();
6 ✔
158
}
159

160
// secure_server
161
// ----------------------------------------------------------------------------
162

163
settings::secure_server::secure_server(
32 ✔
164
    const std::string_view& logging_name) NOEXCEPT
32 ✔
165
  : tcp_server(logging_name)
32 ✔
166
{
167
}
32 ✔
168

169
bool settings::secure_server::secure() const NOEXCEPT
11 ✔
170
{
171
    return false;
11 ✔
172
}
173

174
bool settings::secure_server::authenticate() const NOEXCEPT
6 ✔
175
{
176
    return false;
6 ✔
177
}
178

179
code settings::secure_server::initialize_context() NOEXCEPT
1 ✔
180
{
181
    return error::success;
1 ✔
182
}
183

184
settings::transport settings::secure_server::clear_context() const NOEXCEPT
6 ✔
185
{
186
    return {};
6 ✔
187
}
188

189
settings::transport settings::secure_server::secure_context() const NOEXCEPT
1 ✔
190
{
191
    return {};
1 ✔
192
}
193

194
// tls_server
195
// ----------------------------------------------------------------------------
196

197
settings::tls_server::tls_server(const std::string_view& logging_name) NOEXCEPT
19 ✔
198
  : secure_server(logging_name)
19 ✔
199
{
200
}
19 ✔
201

202
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
203
code settings::tls_server::initialize_context() NOEXCEPT
8 ✔
204
{
205
    if (context)
8 ✔
206
        return error::operation_failed;
1 ✔
207

208
    context = std::make_unique<asio::ssl::context>(asio::ssl::version);
14 ✔
209
    if (!secure())
7 ✔
210
        return error::success;
3 ✔
211

212
    boost_code ec{};
4 ✔
213
    context->set_options(asio::ssl::options, ec);
4 ✔
214
    if (ec) return error::tls_set_options;
×
215

216
    context->use_certificate_chain_file(cert_path.string(), ec);
8 ✔
217
    if (ec) return error::tls_use_certificate;
1 ✔
218

219
    if (!key_pass.empty())
3 ✔
220
        context->set_password_callback([&](auto, auto){ return key_pass; });
×
221

222
    constexpr auto pem = asio::ssl::context::pem;
223
    context->use_private_key_file(key_path.string(), pem, ec);
6 ✔
224
    if (ec) return error::tls_use_private_key;
×
225

226
    if (authenticate())
3 ✔
227
    {
228
        context->set_verify_mode(asio::ssl::authenticate);
2 ✔
229
        context->add_verify_path(cert_auth.string(), ec);
4 ✔
UNCOV
230
        if (ec) return error::tls_set_add_verify;
×
231
    }
232

233
    return error::success;
3 ✔
234
}
235
BC_POP_WARNING();
236

237
bool settings::tls_server::secure() const NOEXCEPT
23 ✔
238
{
239
    return !safes.empty() && !cert_path.empty() && !key_path.empty();
23 ✔
240
}
241

242
bool settings::tls_server::authenticate() const NOEXCEPT
7 ✔
243
{
244
    return secure() && !cert_auth.empty();
7 ✔
245
}
246

247
settings::transport settings::tls_server::secure_context() const NOEXCEPT
4 ✔
248
{
249
    BC_ASSERT(context);
4 ✔
250
    return std::ref(*context);
4 ✔
251
}
252

253
// zmtp_server
254
// ----------------------------------------------------------------------------
255

256
settings::zmtp_server::zmtp_server(
8 ✔
257
    const std::string_view& logging_name) NOEXCEPT
8 ✔
258
  : secure_server(logging_name)
8 ✔
259
{
260
}
8 ✔
261

262
bool settings::zmtp_server::secure() const NOEXCEPT
10 ✔
263
{
264
    const system::data_chunk& secret = key;
10 ✔
265
    return !safes.empty() && !secret.empty();
10 ✔
266
}
267

268
bool settings::zmtp_server::authenticate() const NOEXCEPT
2 ✔
269
{
270
    return secure() && !certs.empty();
2 ✔
271
}
272

273
code settings::zmtp_server::initialize_context() NOEXCEPT
7 ✔
274
{
275
    if (context)
7 ✔
276
        return error::operation_failed;
1 ✔
277

278
    system::data_stack clients{};
6 ✔
279
    for (const system::data_chunk& cert: certs)
8 ✔
280
        clients.push_back(cert);
2 ✔
281

282
    context = std::make_unique<zmtp::context>(key, clients);
12 ✔
283

284
    if (!secure())
6 ✔
285
        return error::success;
3 ✔
286

287
    return context->curve() ? error::success : error::invalid_configuration;
6 ✔
288
}
6 ✔
289

290
settings::transport settings::zmtp_server::clear_context() const NOEXCEPT
1 ✔
291
{
292
    return std::cref(clear);
1 ✔
293
}
294

295
settings::transport settings::zmtp_server::secure_context() const NOEXCEPT
1 ✔
296
{
297
    BC_ASSERT(context);
1 ✔
298
    return std::cref(*context);
1 ✔
299
}
300

301
// http_server
302
// ----------------------------------------------------------------------------
303

304
system::string_list settings::http_server::host_names() const NOEXCEPT
2 ✔
305
{
306
    // secure changes default port from 80 to 443.
307
    const auto port = secure() ? http::default_tls : http::default_http;
2 ✔
308
    return config::to_host_names(hosts, port);
2 ✔
309
}
310

311
system::string_list settings::http_server::origin_names() const NOEXCEPT
2 ✔
312
{
313
    // secure changes default port from 80 to 443.
314
    const auto port = secure() ? http::default_tls : http::default_http;
2 ✔
315
    return config::to_host_names(origins, port);
2 ✔
316
}
317

318
// local
319
static config::credentials::const_iterator to_credential(
16 ✔
320
    const config::credentials& credentials,
321
    const hash_digest& digest) NOEXCEPT
322
{
323
    return std::find_if(credentials.begin(), credentials.end(),
16 ✔
324
        [&digest](const config::credential& credential) NOEXCEPT
16 ✔
325
        {
326
            return credential.digest() == digest;
16 ✔
327
        });
16 ✔
328
}
329

330
bool settings::http_server::authorize() const NOEXCEPT
30 ✔
331
{
332
    return !credentials.empty();
30 ✔
333
}
334

335
bool settings::http_server::authorized(const hash_digest& digest) const NOEXCEPT
8 ✔
336
{
337
    return to_credential(credentials, digest) != credentials.end();
8 ✔
338
}
339

340
bool settings::http_server::permitted(const hash_digest& digest,
8 ✔
341
    const std::string& method) const NOEXCEPT
342
{
343
    const auto credential = to_credential(credentials, digest);
8 ✔
344
    return credential != credentials.end() && credential->permitted(method);
8 ✔
345
}
346

347
// websocket_server
348
// ----------------------------------------------------------------------------
349

350
// [outbound]
351
// ----------------------------------------------------------------------------
352

353
bool settings::peer_outbound::enabled() const NOEXCEPT
100 ✔
354
{
355
    return settings::tcp_server::enabled()
100 ✔
356
        && to_bool(host_pool_capacity)
23 ✔
357
        && to_bool(connect_batch_size);
120 ✔
358
}
359

360
size_t settings::peer_outbound::minimum_address_count() const NOEXCEPT
65 ✔
361
{
362
    // Cannot overflow size_t as long as both are uint16_t.
363
    return connect_batch_size * peer_outbound::connections;
65 ✔
364
}
365

366
steady_clock::duration settings::peer_outbound::seeding_timeout() const NOEXCEPT
50 ✔
367
{
368
    return seconds(seeding_timeout_seconds);
50 ✔
369
}
370

371
// [inbound]
372
// ----------------------------------------------------------------------------
373

374
config::address settings::peer_inbound::first_self() const NOEXCEPT
110 ✔
375
{
376
    return selfs.empty() ? config::address{} : selfs.front();
110 ✔
377
}
378

379
bool settings::peer_inbound::advertise() const NOEXCEPT
41 ✔
380
{
381
    return enabled() && (!selfs.empty() || bridged());
41 ✔
382
}
383

384
bool settings::peer_inbound::enabled() const NOEXCEPT
141 ✔
385
{
386
    return settings::tcp_server::enabled() && (!binds.empty() || bridged());
141 ✔
387
}
388

389
// [manual]
390
// ----------------------------------------------------------------------------
391

392
void settings::peer_manual::initialize() NOEXCEPT
5 ✔
393
{
394
    BC_ASSERT_MSG(friends.empty(), "friends not empty");
5 ✔
395

396
    // Dynamic conversion of peers is O(N^2), so set on initialize.
397
    // This converts endpoints to addresses so will produce the default
398
    // address for any hosts that are DNS names (i.e. not IP addresses).
399
    friends = system::projection<network::config::addresses>(peers);
10 ✔
400
}
5 ✔
401

402
bool settings::peer_manual::peered(const address_item& item) const NOEXCEPT
111 ✔
403
{
404
    // Friends should be mapped from peers by initialize().
405
    return contains(friends, item);
111 ✔
406
}
407

408
bool settings::peer_manual::enabled() const NOEXCEPT
127 ✔
409
{
410
    // connections field is not currently used, and this applies only to
411
    // initial configuration of manual connections, as they can be added.
412
    return /* settings::tcp_server::enabled() && */ !peers.empty();
127 ✔
413
}
414

415
// [network]
416
// ----------------------------------------------------------------------------
417

418
static_assert(heading::maximum_payload(true) == 4'000'000);
419
static_assert(heading::maximum_payload(false) == 1'800'003);
420

421
static uint32_t identifier_from_context(chain::selection context) NOEXCEPT
426 ✔
422
{
423
    switch (context)
426 ✔
424
    {
425
        case chain::selection::mainnet:  return 3652501241;
426
        case chain::selection::testnet3: return 118034699;
1 ✔
427
        case chain::selection::regtest:  return 3669344250;
1 ✔
428
        default: return 0;
1 ✔
429
    }
430
}
431

432
settings::settings(chain::selection context) NOEXCEPT
426 ✔
433
  : outbound{ context },
426 ✔
434
    inbound{ context },
426 ✔
435
    manual{ context },
426 ✔
436
    identifier(identifier_from_context(context))
852 ✔
437
{
438
}
426 ✔
439

440
// Derived values are read and computed here, and are const thereafter.
441
code settings::initialize() NOEXCEPT
1 ✔
442
{
443
    peer.identifier = identifier;
1 ✔
444
    manual.initialize();
1 ✔
445
    return inbound.initialize();
1 ✔
446
}
447

448
settings::transport settings::peer_context() const NOEXCEPT
3 ✔
449
{
450
    return std::cref(peer);
3 ✔
451
}
452

453
size_t settings::threads_() const NOEXCEPT
234 ✔
454
{
455
    // Zero implies the lesser of hardware threads and the default cap.
456
    return to_bool(threads) ? threads :
467 ✔
457
        std::min(cores(), maximum_default_threads);
233 ✔
458
}
459

460
// Randomized from 50% to maximum milliseconds (specified in seconds).
461
steady_clock::duration settings::retry_timeout() const NOEXCEPT
48 ✔
462
{
463
    const auto from = retry_timeout_seconds * 500_u64;
48 ✔
464
    const auto to = retry_timeout_seconds * 1'000_u64;
48 ✔
465
    return milliseconds{ system::maybe_random::next(from, to) };
48 ✔
466
}
467

468
steady_clock::duration settings::channel_handshake() const NOEXCEPT
102 ✔
469
{
470
    return seconds(handshake_timeout_seconds);
102 ✔
471
}
472

473
steady_clock::duration settings::channel_heartbeat() const NOEXCEPT
77 ✔
474
{
475
    return minutes(channel_heartbeat_minutes);
77 ✔
476
}
477

478
steady_clock::duration settings::maximum_skew() const NOEXCEPT
1 ✔
479
{
480
    return minutes(maximum_skew_minutes);
1 ✔
481
}
482

483
std::filesystem::path settings::file() const NOEXCEPT
72 ✔
484
{
485
    BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
486
    return path / "hosts.cache";
144 ✔
487
    BC_POP_WARNING()
488
}
489

490
bool settings::gossip_v2() const NOEXCEPT
33 ✔
491
{
492
    // Only tor and i2p addresses are unrepresentable in the v1 protocol.
493
    return gossip_tor || gossip_i2p;
33 ✔
494
}
495

496
// Randomized from 50% to maximum milliseconds (specified in seconds).
497
steady_clock::duration settings::connect_timeout(
197 ✔
498
    const tcp_server& options) const NOEXCEPT
499
{
500
    const auto seconds_ = to_bool(options.connect_timeout_seconds) ?
197 ✔
501
        options.connect_timeout_seconds : connect_timeout_seconds;
502

503
    const auto from = seconds_ * 500_u64;
197 ✔
504
    const auto to = seconds_ * 1'000_u64;
197 ✔
505
    return milliseconds{ system::maybe_random::next(from, to) };
197 ✔
506
}
507

508
uint32_t settings::rate_limited(const tcp_server& options) const NOEXCEPT
191 ✔
509
{
510
    return to_bool(rate_limit) && to_bool(options.rate_limit) ?
382 ✔
511
        std::min(rate_limit, options.rate_limit) :
2 ✔
512
        std::max(rate_limit, options.rate_limit);
189 ✔
513
}
514

515
bool settings::connectable(const address_item& item) const NOEXCEPT
22 ✔
516
{
517
    // A proxy connects by name, otherwise only an ip address is routable.
518
    return is_v4(item.address) || is_v6(item.address) ||
519
        is_cjdns(item.address) || (outbound.proxied() && is_named(item.address));
9 ✔
520
}
521

522
bool settings::gossiped(const address_item& item) const NOEXCEPT
122 ✔
523
{
524
    switch (item.address.index())
122 ✔
525
    {
526
        case ipv4_t::id:
91 ✔
527
            return gossip_ipv4;
91 ✔
528
        case ipv6_t::id:
22 ✔
529
        case cjdns_t::id:
530
            return gossip_ipv6;
22 ✔
531
        case torv3_t::id:
4 ✔
532
            return gossip_tor;
4 ✔
533
        case i2p_t::id:
2 ✔
534
            return gossip_i2p;
2 ✔
535
        default:
536
            return false;
537
    }
538
}
539

540
bool settings::unsupported(const address_item& item) const NOEXCEPT
100 ✔
541
{
542
    return to_bool(bit_and(item.services, invalid_services));
100 ✔
543
}
544

545
bool settings::blacklisted(const address_item& item) const NOEXCEPT
166 ✔
546
{
547
    return contains(blacklists, item);
166 ✔
548
}
549

550
bool settings::whitelisted(const address_item& item) const NOEXCEPT
167 ✔
551
{
552
    return whitelists.empty() || contains(whitelists, item);
167 ✔
553
}
554

555
bool settings::excluded(const address_item& item) const NOEXCEPT
70 ✔
556
{
557
    return !is_specified(item)
70 ✔
558
        || !gossiped(item)
68 ✔
559
        || unsupported(item)
68 ✔
560
        || manual.peered(item)
65 ✔
561
        || blacklisted(item)
65 ✔
562
        || !whitelisted(item);
135 ✔
563
}
564

565
} // namespace network
566
} // 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