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

libbitcoin / libbitcoin-network / 46042104491

27 Nov 2026 05:28PM UTC coverage: 93.826% (-0.7%) from 94.551%
46042104491

push

github

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

Allow an opaque origin when no origins are configured.

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

196 existing lines in 13 files now uncovered.

17841 of 19015 relevant lines covered (93.83%)

1918.12 hits per line

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

84.71
/src/net/socket_stop.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/net/socket.hpp>
20

21
#include <bitcoin/network/config/config.hpp>
22
#include <bitcoin/network/define.hpp>
23

24
namespace libbitcoin {
25
namespace network {
26

27
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
28

29
using namespace system;
30
using namespace std::placeholders;
31

32
constexpr size_t drain_size = 1024;
33

34
// The socket does not (must not) stop itself.
35

36
// Stop (async).
37
// ----------------------------------------------------------------------------
38
// Hard stop (no graceful websocket/tls closure).
39

40
void socket::stop() NOEXCEPT
767 ✔
41
{
42
    // Stop flag accelerates work stoppage, as it does not wait on strand.
43
    if (stopped_.exchange(true) && !lazy_.exchange(false))
767 ✔
44
        return;
45

46
    // Stop is posted to strand to protect the socket.
47
    io::post(strand_,
545 ✔
48
        std::bind(&socket::do_stop, shared_from_this()));
1,090 ✔
49
}
50

51
// private
52
void socket::do_stop() NOEXCEPT
631 ✔
53
{
54
    BC_ASSERT(stranded());
631 ✔
55

56
    watch_->stop();
631 ✔
57
    timer_->stop();
631 ✔
58

59
    code ignore{};
631 ✔
60
    auto& layer = get_base();
631 ✔
61
    layer.shutdown(io::socket::shutdown_type::both, ignore);
631 ✔
62
    layer.close(ignore);
631 ✔
63
}
631 ✔
64

65
// Stop (lazy).
66
// ----------------------------------------------------------------------------
67
// Lazy stop (graceful websocket/tls closure).
68

69
void socket::lazy_stop() NOEXCEPT
87 ✔
70
{
71
    // Stop flag accelerates work stoppage, as it does not wait on strand.
72
    if (stopped_.exchange(true))
87 ✔
73
        return;
74

75
    lazy_.store(true);
86 ✔
76

77
    // Async stop is dispatched to strand to protect the socket.
78
    io::dispatch(strand_,
86 ✔
79
        std::bind(&socket::do_lazy_stop, shared_from_this()));
172 ✔
80
}
81

82
// private
83
void socket::do_lazy_stop() NOEXCEPT
86 ✔
84
{
85
    BC_ASSERT(stranded());
86 ✔
86

87
    // The peer is not required to answer a close, so the wait is bounded.
88
    if (is_zero(timer_->timeout().count()))
86 ✔
89
    {
90
        do_stop();
82 ✔
91
        return;
82 ✔
92
    }
93

94
    timer_->start(std::bind(&socket::handle_lazy_timer,
4 ✔
95
        shared_from_this(), _1));
8 ✔
96

97
    // Held writes have completed, so they precede the close.
98
    hold_ = false;
4 ✔
99
    if (holding())
4 ✔
100
    {
101
        release(std::bind(&socket::do_ws_stop, shared_from_this()));
1 ✔
102
        return;
1 ✔
103
    }
104

105
    do_ws_stop();
3 ✔
106
}
107

108
// private
109
// Socket stop cancels pending close, which completes its handler.
110
void socket::handle_lazy_timer(const code& ec) NOEXCEPT
4 ✔
111
{
112
    if (!ec)
4 ✔
UNCOV
113
        stop();
×
114
}
4 ✔
115

116
// private
117
void socket::do_ws_stop() NOEXCEPT
4 ✔
118
{
119
    BC_ASSERT(stranded());
4 ✔
120
    if (!websocket())
4 ✔
121
    {
122
        do_tls_stop();
2 ✔
123
        return;
2 ✔
124
    }
125

126
    // A close already sent (answering the peer) is not repeated.
127
    if (ws_closing_)
2 ✔
128
    {
129
        do_tls_stop();
×
130
        return;
×
131
    }
132

133
    ws_closing_ = true;
2 ✔
134
    ws_send(ws::opcode::close, ws::close_payload(ws::close_code::normal),
2 ✔
135
        std::bind(&socket::handle_ws_close, shared_from_this(), _1));
4 ✔
136
}
137

138
// private
139
void socket::handle_ws_close(const code& ec) NOEXCEPT
2 ✔
140
{
141
    BC_ASSERT(stranded());
2 ✔
142
    if (ec && ec != error::peer_disconnect && ec != error::channel_stopped)
2 ✔
143
    {
144
        LOGX("WS close [" << endpoint() << "] " << ec.message());
×
145
    }
146

147
    do_tls_stop();
2 ✔
148
}
2 ✔
149

150
// private
151
void socket::do_tls_stop() NOEXCEPT
4 ✔
152
{
153
    BC_ASSERT(stranded());
4 ✔
154
    if (!secure())
4 ✔
155
    {
156
        do_linger();
4 ✔
157
        return;
4 ✔
158
    }
159

UNCOV
160
    get_tls().async_shutdown(
×
UNCOV
161
        std::bind(&socket::handle_tls_close,
×
UNCOV
162
            shared_from_this(), _1));
×
163
}
164

165
// private
UNCOV
166
void socket::handle_tls_close(const code& ec) NOEXCEPT
×
167
{
UNCOV
168
    BC_ASSERT(stranded());
×
UNCOV
169
    if (ec && ec != error::peer_disconnect && ec != error::operation_canceled)
×
170
        logx("tls_stop", ec);
×
171

UNCOV
172
    do_linger();
×
UNCOV
173
}
×
174

175
// private
176
// Lingering close (rfc9112 9.6): send closed and receive drained to peer's
177
// close, so unread bytes do not reset connection under response.
178
void socket::do_linger() NOEXCEPT
4 ✔
179
{
180
    BC_ASSERT(stranded());
4 ✔
181

182
    code ignore{};
4 ✔
183
    auto& layer = get_base();
4 ✔
184
    layer.cancel(ignore);
4 ✔
185
    layer.shutdown(io::socket::shutdown_type::send, ignore);
4 ✔
186
    do_drain(emplace_shared<data_chunk>(drain_size));
4 ✔
187
}
4 ✔
188

189
// private
190
void socket::do_drain(const chunk_ptr& sink) NOEXCEPT
6 ✔
191
{
192
    BC_ASSERT(stranded());
6 ✔
193

194
    get_base().async_read_some(io::buffer(*sink),
12 ✔
195
        std::bind(&socket::handle_drain,
12 ✔
196
            shared_from_this(), _1, sink));
12 ✔
197
}
6 ✔
198

199
// private
200
void socket::handle_drain(const code& ec,
6 ✔
201
    const chunk_ptr& sink) NOEXCEPT
202
{
203
    BC_ASSERT(stranded());
6 ✔
204

205
    if (ec)
6 ✔
206
    {
207
        do_stop();
4 ✔
208
        return;
4 ✔
209
    }
210

211
    do_drain(sink);
2 ✔
212
}
213

214
BC_POP_WARNING()
215

216
} // namespace network
217
} // 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