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

libbitcoin / libbitcoin-network / 35304898903

18 Sep 2026 03:53AM UTC coverage: 48.369% (+0.02%) from 48.346%
35304898903

push

github

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

Validate bip155 address tags and reject tor v2 addresses.

33 of 81 new or added lines in 9 files covered. (40.74%)

7 existing lines in 3 files now uncovered.

27784 of 57442 relevant lines covered (48.37%)

164455.13 hits per line

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

53.13
/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
367 ✔
39
{
40
}
×
41

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

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

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

55
settings::sam::sam() NOEXCEPT
195 ✔
56
{
57
}
195 ✔
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.
NEW
68
static data_chunk to_destination(const data_chunk& private_key) NOEXCEPT
×
69
{
NEW
70
    read::bytes::copy source(private_key);
×
NEW
71
    source.skip_bytes(certificate_position);
×
NEW
72
    const auto size = destination_size + source.read_2_bytes_big_endian();
×
73

NEW
74
    if (!source || private_key.size() < size)
×
NEW
75
        return {};
×
76

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

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

NEW
87
    const auto destination = to_destination(private_key);
×
NEW
88
    if (destination.empty())
×
NEW
89
        return {};
×
90

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

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

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

NEW
115
    return error::success;
×
116
}
117
BC_POP_WARNING()
118

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

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

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

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

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

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

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

152
// secure_server
153
// ----------------------------------------------------------------------------
154

155
settings::secure_server::secure_server(
7 ✔
156
    const std::string_view& logging_name) NOEXCEPT
7 ✔
157
  : tcp_server(logging_name)
7 ✔
158
{
159
}
7 ✔
160

161
bool settings::secure_server::secure() const NOEXCEPT
×
162
{
163
    return false;
×
164
}
165

166
bool settings::secure_server::authenticate() const NOEXCEPT
×
167
{
168
    return false;
×
169
}
170

NEW
171
code settings::secure_server::initialize_context() NOEXCEPT
×
172
{
173
    return error::success;
×
174
}
175

176
settings::transport settings::secure_server::clear_context() const NOEXCEPT
×
177
{
178
    return {};
×
179
}
180

181
settings::transport settings::secure_server::secure_context() const NOEXCEPT
×
182
{
183
    return {};
×
184
}
185

186
// tls_server
187
// ----------------------------------------------------------------------------
188

189
settings::tls_server::tls_server(const std::string_view& logging_name) NOEXCEPT
7 ✔
190
  : secure_server(logging_name)
7 ✔
191
{
192
    maximum_request = maximum_service_default();
7 ✔
193
    minimum_buffer = minimum_service_default();
7 ✔
194
}
7 ✔
195

196
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
NEW
197
code settings::tls_server::initialize_context() NOEXCEPT
×
198
{
199
    if (context)
200
        return error::operation_failed;
×
201

202
    context = std::make_unique<asio::ssl::context>(asio::ssl::version);
×
203
    if (!secure())
×
204
        return error::success;
×
205

206
    boost_code ec{};
×
207
    context->set_options(asio::ssl::options, ec);
×
208
    if (ec) return error::tls_set_options;
×
209

210
    context->use_certificate_chain_file(cert_path.string(), ec);
×
211
    if (ec) return error::tls_use_certificate;
×
212

213
    if (!key_pass.empty())
×
214
        context->set_password_callback([&](auto, auto){ return key_pass; });
×
215

216
    constexpr auto pem = asio::ssl::context::pem;
217
    context->use_private_key_file(key_path.string(), pem, ec);
×
218
    if (ec) return error::tls_use_private_key;
×
219

220
    if (authenticate())
×
221
    {
222
        context->set_verify_mode(asio::ssl::authenticate);
×
223
        if (cert_auth.empty())
×
224
        {
225
            context->set_default_verify_paths(ec);
×
226
            if (ec) return error::tls_set_default_verify;
×
227
        }
228
        else
229
        {
230
            context->add_verify_path(cert_auth.string(), ec);
×
231
            if (ec) return error::tls_set_add_verify;
×
232
        }
233
    }
234

235
    return error::success;
×
236
}
237
BC_POP_WARNING();
238

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

244
bool settings::tls_server::authenticate() const NOEXCEPT
×
245
{
246
    return secure() && !cert_auth.empty();
×
247
}
248

249
settings::transport settings::tls_server::secure_context() const NOEXCEPT
×
250
{
251
    BC_ASSERT(context);
×
252
    return std::ref(*context);
×
253
}
254

255
// zmtp_server
256
// ----------------------------------------------------------------------------
257

258
settings::zmtp_server::zmtp_server(
×
259
    const std::string_view& logging_name) NOEXCEPT
×
260
  : secure_server(logging_name)
×
261
{
262
}
×
263

264
bool settings::zmtp_server::secure() const NOEXCEPT
×
265
{
266
    const system::data_chunk& secret = key;
×
267
    return !safes.empty() && !secret.empty();
×
268
}
269

270
bool settings::zmtp_server::authenticate() const NOEXCEPT
×
271
{
272
    return secure() && !certs.empty();
×
273
}
274

NEW
275
code settings::zmtp_server::initialize_context() NOEXCEPT
×
276
{
277
    if (context)
×
278
        return error::operation_failed;
×
279

280
    system::data_stack clients{};
×
281
    for (const system::data_chunk& cert: certs)
×
282
        clients.push_back(cert);
×
283

284
    context = std::make_unique<zmtp::context>(key, clients);
×
285

286
    if (!secure())
×
287
        return error::success;
×
288

289
    return context->curve() ? error::success : error::invalid_configuration;
×
290
}
×
291

292
settings::transport settings::zmtp_server::clear_context() const NOEXCEPT
×
293
{
294
    return std::cref(clear);
×
295
}
296

297
settings::transport settings::zmtp_server::secure_context() const NOEXCEPT
×
298
{
299
    BC_ASSERT(context);
×
300
    return std::cref(*context);
×
301
}
302

303
// http_server
304
// ----------------------------------------------------------------------------
305

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

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

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

332
bool settings::http_server::authorize() const NOEXCEPT
10 ✔
333
{
334
    return !credentials.empty();
10 ✔
335
}
336

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

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

349
// websocket_server
350
// ----------------------------------------------------------------------------
351

352
// [outbound]
353
// ----------------------------------------------------------------------------
354

355
bool settings::peer_outbound::enabled() const NOEXCEPT
20 ✔
356
{
357
    return settings::tcp_server::enabled()
20 ✔
358
        && to_bool(host_pool_capacity)
14 ✔
359
        && to_bool(connect_batch_size);
31 ✔
360
}
361

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

368
steady_clock::duration settings::peer_outbound::seeding_timeout() const NOEXCEPT
21 ✔
369
{
370
    return seconds(seeding_timeout_seconds);
21 ✔
371
}
372

373
// [inbound]
374
// ----------------------------------------------------------------------------
375

376
config::address settings::peer_inbound::first_self() const NOEXCEPT
4 ✔
377
{
378
    return selfs.empty() ? config::address{} : selfs.front();
4 ✔
379
}
380

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

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

391
// [manual]
392
// ----------------------------------------------------------------------------
393

394
void settings::peer_manual::initialize() NOEXCEPT
3 ✔
395
{
396
    BC_ASSERT_MSG(friends.empty(), "friends not empty");
3 ✔
397

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

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

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

417
// [network]
418
// ----------------------------------------------------------------------------
419

420
static_assert(heading::maximum_payload(level::canonical, true) == 4'000'000);
421
static_assert(heading::maximum_payload(level::canonical, false) == 1'800'003);
422

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

434
settings::settings(chain::selection context) NOEXCEPT
177 ✔
435
  : outbound{ context },
177 ✔
436
    inbound{ context },
177 ✔
437
    manual{ context },
177 ✔
438
    identifier(identifier_from_context(context))
354 ✔
439
{
440
}
177 ✔
441

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

NEW
450
settings::transport settings::peer_context() const NOEXCEPT
×
451
{
NEW
452
    return std::cref(peer);
×
453
}
454

455
size_t settings::threads_() const NOEXCEPT
98 ✔
456
{
457
    // Zero implies the lesser of hardware threads and the default cap.
458
    return to_bool(threads) ? threads :
195 ✔
459
        std::min(cores(), maximum_default_threads);
97 ✔
460
}
461

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

470
// Randomized from 50% to maximum milliseconds (specified in seconds).
471
steady_clock::duration settings::connect_timeout() const NOEXCEPT
76 ✔
472
{
473
    const auto from = connect_timeout_seconds * 500_u64;
76 ✔
474
    const auto to = connect_timeout_seconds * 1'000_u64;
76 ✔
475
    return milliseconds{ system::maybe_random::next(from, to) };
76 ✔
476
}
477

478
steady_clock::duration settings::channel_handshake() const NOEXCEPT
3 ✔
479
{
480
    return seconds(handshake_timeout_seconds);
3 ✔
481
}
482

483
steady_clock::duration settings::channel_heartbeat() const NOEXCEPT
7 ✔
484
{
485
    return minutes(channel_heartbeat_minutes);
7 ✔
486
}
487

488
steady_clock::duration settings::maximum_skew() const NOEXCEPT
1 ✔
489
{
490
    return minutes(maximum_skew_minutes);
1 ✔
491
}
492

493
std::filesystem::path settings::file() const NOEXCEPT
×
494
{
495
    BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
496
    return path / "hosts.cache";
×
497
    BC_POP_WARNING()
498
}
499

500
bool settings::gossip_v2() const NOEXCEPT
4 ✔
501
{
502
    // Only tor and i2p addresses are unrepresentable in the v1 protocol.
503
    return gossip_tor || gossip_i2p;
4 ✔
504
}
505

506
bool settings::connectable(const address_item& item) const NOEXCEPT
17 ✔
507
{
508
    // A proxy connects by name, otherwise only an ip address is routable.
509
    return is_v4(item.address) || is_v6(item.address) ||
510
        is_cjdns(item.address) || (outbound.proxied() && is_named(item.address));
9 ✔
511
}
512

513
bool settings::gossiped(const address_item& item) const NOEXCEPT
19 ✔
514
{
515
    switch (item.address.index())
19 ✔
516
    {
517
        case ipv4_t::id:
3 ✔
518
            return gossip_ipv4;
3 ✔
519
        case ipv6_t::id:
7 ✔
520
        case cjdns_t::id:
521
            return gossip_ipv6;
7 ✔
522
        case torv3_t::id:
4 ✔
523
            return gossip_tor;
4 ✔
524
        case i2p_t::id:
2 ✔
525
            return gossip_i2p;
2 ✔
526
        default:
527
            return false;
528
    }
529
}
530

531
bool settings::unsupported(const address_item& item) const NOEXCEPT
12 ✔
532
{
533
    return to_bool(bit_and(item.services, invalid_services));
12 ✔
534
}
535

536
bool settings::blacklisted(const address_item& item) const NOEXCEPT
21 ✔
537
{
538
    return contains(blacklists, item);
21 ✔
539
}
540

541
bool settings::whitelisted(const address_item& item) const NOEXCEPT
22 ✔
542
{
543
    return whitelists.empty() || contains(whitelists, item);
22 ✔
544
}
545

546
bool settings::excluded(const address_item& item) const NOEXCEPT
1 ✔
547
{
548
    return !is_specified(item)
1 ✔
549
        || !gossiped(item)
×
550
        || unsupported(item)
×
551
        || manual.peered(item)
×
552
        || blacklisted(item)
×
553
        || !whitelisted(item);
1 ✔
554
}
555

556
} // namespace network
557
} // 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