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

libbitcoin / libbitcoin-system / 15655570685

14 Jun 2025 07:53PM UTC coverage: 81.172% (+0.08%) from 81.096%
15655570685

push

github

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

Move authority and endpoint config classes from network.

177 of 206 new or added lines in 5 files covered. (85.92%)

10610 of 13071 relevant lines covered (81.17%)

3626848.88 hits per line

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

96.23
/src/config/authority.cpp
1
/**
2
 * Copyright (c) 2011-2025 libbitcoin developers (see AUTHORS)
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/system/config/authority.hpp>
20

21
#include <sstream>
22
#include <bitcoin/system/config/utilities.hpp>
23
#include <bitcoin/system/define.hpp>
24
#include <bitcoin/system/serial/serial.hpp>
25

26
namespace libbitcoin {
27
namespace system {
28
namespace config {
29

30
// Contructors.
31
// ----------------------------------------------------------------------------
32

33
// Default authority is IPv6 unspecified (not IPv6-mapped unspecified).
34
authority::authority() NOEXCEPT
8✔
35
  : authority(boost::asio::ip::address_v6{}, {})
8✔
36
{
37
}
8✔
38

39
// Deserialzation does not map IPv4 and does not support mapped encoding.
40
authority::authority(const std::string& authority) THROWS
139✔
41
  : ip_{}, port_{}, cidr_{}
139✔
42
{
43
    std::stringstream(authority) >> *this;
278✔
44
}
126✔
45

46
// This allows unusable CIDR values (ok).
47
// IPv6-mapped IPv4 are normalized to IPv4.
48
authority::authority(const asio::address& ip, uint16_t port,
10✔
49
    uint8_t cidr) NOEXCEPT
10✔
50
  : ip_(config::denormalize(ip)), port_(port), cidr_(cidr)
10✔
51
{
52
}
10✔
53

54
authority::authority(const asio::endpoint& endpoint) NOEXCEPT
1✔
55
  : authority(endpoint.address(), endpoint.port())
1✔
56
{
57
}
1✔
58

59
// Properties.
60
// ----------------------------------------------------------------------------
61

62
const asio::address& authority::ip() const NOEXCEPT
70✔
63
{
64
    return ip_;
70✔
65
}
66

67
uint16_t authority::port() const NOEXCEPT
101✔
68
{
69
    return port_;
101✔
70
}
71

72
uint8_t authority::cidr() const NOEXCEPT
151✔
73
{
74
    return cidr_;
151✔
75
}
76

77
// Methods.
78
// ----------------------------------------------------------------------------
79

NEW
80
asio::endpoint authority::to_endpoint() const NOEXCEPT
×
81
{
NEW
82
    return { ip(), port() };
×
83
}
84

85
std::string authority::to_host() const NOEXCEPT
1✔
86
{
87
    return config::to_host(ip());
1✔
88
}
89

90
std::string authority::to_literal() const NOEXCEPT
12✔
91
{
92
    return config::to_literal(ip());
6✔
93
}
94

95
std::string authority::to_string() const NOEXCEPT
6✔
96
{
97
    std::stringstream value{};
6✔
98
    value << *this;
6✔
99
    BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
100
    return value.str();
6✔
101
    BC_POP_WARNING()
102
}
6✔
103

104
// Operators.
105
// ----------------------------------------------------------------------------
106

107
bool operator==(const authority& left, const authority& right) NOEXCEPT
62✔
108
{
109
    // both non-zero ports must match (zero/non-zero or both zero are matched).
110
    if ((!is_zero(left.port()) && !is_zero(right.port())) &&
62✔
111
        left.port() != right.port())
112
        return false;
113

114
    // both non-zero cidrs must match.
115
    if ((!is_zero(left.cidr()) && !is_zero(right.cidr())) &&
56✔
116
        left.cidr() != right.cidr())
117
        return false;
118

119
    // same cidrs, match ips only.
120
    if (left.cidr() == right.cidr())
52✔
121
        return left.ip() == right.ip();
26✔
122

123
    // one zero (host) and one non-zero (subnet) cidr, match host to subnet.
124
    return is_zero(left.cidr()) ?
26✔
125
        config::is_member(left.ip(), right.ip(), right.cidr()) :
10✔
126
        config::is_member(right.ip(), left.ip(), left.cidr());
16✔
127
}
128

129
bool operator!=(const authority& left, const authority& right) NOEXCEPT
12✔
130
{
131
    return !(left == right);
12✔
132
}
133

134
// This allows unusable CIDR values (ok).
135
std::istream& operator>>(std::istream& input,
139✔
136
    authority& argument) THROWS
137
{
138
    std::string value{};
139✔
139
    input >> value;
139✔
140

141
    if (!parse_authority(argument.ip_, argument.port_, argument.cidr_, value))
139✔
142
        throw istream_exception(value);
13✔
143

144
    return input;
126✔
145
}
146

147
// This allows unusable CIDR values (ok).
148
std::ostream& operator<<(std::ostream& output,
6✔
149
    const authority& argument) NOEXCEPT
150
{
151
    BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
152
    output
6✔
153
        << argument.to_literal()
6✔
154
        << (!is_zero(argument.port()) ? ":" + serialize(argument.port()) : "")
18✔
155
        << (!is_zero(argument.cidr()) ? "/" + serialize(argument.cidr()) : "");
24✔
156
    BC_POP_WARNING()
157
    return output;
6✔
158
}
159

160
} // namespace config
161
} // namespace system
162
} // namespace libbitcoin
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc