xrpld
Loading...
Searching...
No Matches
Door.h
1#pragma once
2
3#include <xrpl/basics/Log.h>
4#include <xrpl/basics/contract.h>
5#include <xrpl/beast/utility/Journal.h>
6#include <xrpl/server/Port.h>
7#include <xrpl/server/detail/PlainHTTPPeer.h>
8#include <xrpl/server/detail/SSLHTTPPeer.h>
9#include <xrpl/server/detail/io_list.h>
10
11#include <boost/asio/basic_waitable_timer.hpp>
12#include <boost/asio/buffer.hpp>
13#include <boost/asio/io_context.hpp>
14#include <boost/asio/ip/tcp.hpp>
15#include <boost/asio/post.hpp>
16#include <boost/asio/spawn.hpp>
17#include <boost/asio/steady_timer.hpp>
18#include <boost/beast/core/detect_ssl.hpp>
19#include <boost/beast/core/multi_buffer.hpp>
20#include <boost/beast/core/tcp_stream.hpp>
21#include <boost/container/flat_map.hpp>
22#include <boost/predef.h>
23
24#include <exception>
25#include <stdexcept>
26
27#if !BOOST_OS_WINDOWS
28#include <sys/resource.h>
29
30#include <dirent.h>
31#endif
32
33#include <algorithm>
34#include <chrono>
35#include <cstdint>
36#include <memory>
37#include <optional>
38#include <sstream>
39#include <utility>
40
41namespace xrpl {
42
46template <class Handler>
47class Door : public IOList::Work, public std::enable_shared_from_this<Door<Handler>>
48{
49private:
51 using TimerType = boost::asio::basic_waitable_timer<ClockType>;
52 using ErrorCode = boost::system::error_code;
53 using YieldContext = boost::asio::yield_context;
54 using ProtocolType = boost::asio::ip::tcp;
55 using AcceptorType = ProtocolType::acceptor;
56 using EndpointType = ProtocolType::endpoint;
57 using SocketType = boost::asio::ip::tcp::socket;
58 using StreamType = boost::beast::tcp_stream;
59
60 // Detects SSL on a socket
61 class Detector : public IOList::Work, public std::enable_shared_from_this<Detector>
62 {
63 private:
64 Port const& port_;
65 Handler& handler_;
66 boost::asio::io_context& ioc_;
70 boost::asio::strand<boost::asio::io_context::executor_type> strand_;
72
73 public:
75 Port const& port,
76 Handler& handler,
77 boost::asio::io_context& ioc,
78 StreamType&& stream,
79 EndpointType remoteAddress,
81 void
82 run();
83 void
84 close() override;
85
86 private:
87 void
89 };
90
92 Port const& port_;
93 Handler& handler_;
94 boost::asio::io_context& ioc_;
96 boost::asio::strand<boost::asio::io_context::executor_type> strand_;
97 bool ssl_{
98 port_.protocol.contains("https") || port_.protocol.contains("wss") ||
99 port_.protocol.contains("wss2") || port_.protocol.contains("peer"),
100 };
101 bool plain_{
102 port_.protocol.contains("http") || port_.protocol.contains("ws") ||
103 port_.protocol.contains("ws2"),
104 };
108 boost::asio::steady_timer backoffTimer_;
109 static constexpr std::uint64_t kMaxUsedFdPercent = 70;
111 ClockType::time_point fdSampleAt_;
112 bool cachedThrottle_{false};
113
119
120 void
121 reOpen();
122
124 queryFdStats() const;
125
126 bool
128
129public:
130 Door(Handler& handler, boost::asio::io_context& ioContext, Port const& port, beast::Journal j);
131
132 // Work-around because we can't call shared_from_this in ctor
133 void
134 run();
135
143 void
144 close() override;
145
146 [[nodiscard]] EndpointType
148 {
149 return acceptor_.local_endpoint();
150 }
151
152private:
153 template <class ConstBufferSequence>
154 void
155 create(
156 bool ssl,
157 ConstBufferSequence const& buffers,
158 StreamType&& stream,
159 EndpointType remoteAddress);
160
161 void
162 doAccept(YieldContext yield);
163};
164
165template <class Handler>
167 Port const& port,
168 Handler& handler,
169 boost::asio::io_context& ioc,
170 StreamType&& stream,
171 EndpointType remoteAddress,
173 : port_(port)
174 , handler_(handler)
175 , ioc_(ioc)
176 , stream_(std::move(stream))
177 , socket_(stream_.socket())
178 , remoteAddress_(std::move(remoteAddress))
179 , strand_(boost::asio::make_strand(ioc_))
180 , j_(j)
181{
182}
183
184template <class Handler>
185void
187{
189 strand_, [self = this->shared_from_this()](YieldContext yield) { self->doDetect(yield); });
190}
191
192template <class Handler>
193void
198
199template <class Handler>
200void
201Door<Handler>::Detector::doDetect(boost::asio::yield_context doYield)
202{
203 boost::beast::multi_buffer buf(16);
204 stream_.expires_after(std::chrono::seconds(15));
205 boost::system::error_code ec;
206 bool const ssl = async_detect_ssl(stream_, buf, doYield[ec]);
207 stream_.expires_never();
208 if (!ec)
209 {
210 if (ssl)
211 {
212 if (auto const sp = ios().template emplace<SSLHTTPPeer<Handler>>(
213 port_, handler_, ioc_, j_, remoteAddress_, buf.data(), std::move(stream_)))
214 sp->run();
215 return;
216 }
217 if (auto const sp = ios().template emplace<PlainHTTPPeer<Handler>>(
218 port_, handler_, ioc_, j_, remoteAddress_, buf.data(), std::move(stream_)))
219 sp->run();
220 return;
221 }
222 if (ec != boost::asio::error::operation_aborted)
223 {
224 JLOG(j_.trace()) << "Error detecting ssl: " << ec.message() << " from " << remoteAddress_;
225 }
226}
227
228//------------------------------------------------------------------------------
229
230template <class Handler>
231void
233{
234 ErrorCode ec;
235
236 if (acceptor_.is_open())
237 {
238 acceptor_.close(ec);
239 if (ec)
240 {
242 ss << "Can't close acceptor: " << port_.name << ", " << ec.message();
243 JLOG(j_.error()) << ss.str();
245 }
246 }
247
248 EndpointType const localAddress = EndpointType(port_.ip, port_.port);
249
250 acceptor_.open(localAddress.protocol(), ec);
251 if (ec)
252 {
253 JLOG(j_.error()) << "Open port '" << port_.name << "' failed:" << ec.message();
255 }
256
257 acceptor_.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true), ec);
258 if (ec)
259 {
260 JLOG(j_.error()) << "Option for port '" << port_.name << "' failed:" << ec.message();
262 }
263
264 acceptor_.bind(localAddress, ec);
265 if (ec)
266 {
267 JLOG(j_.error()) << "Bind port '" << port_.name << "' failed:" << ec.message();
269 }
270
271 acceptor_.listen(boost::asio::socket_base::max_listen_connections, ec);
272 if (ec)
273 {
274 JLOG(j_.error()) << "Listen on port '" << port_.name << "' failed:" << ec.message();
276 }
277
278 JLOG(j_.info()) << "Opened " << port_;
279}
280
281template <class Handler>
283 Handler& handler,
284 boost::asio::io_context& ioContext,
285 Port const& port,
287 : j_(j)
288 , port_(port)
289 , handler_(handler)
290 , ioc_(ioContext)
291 , acceptor_(ioContext)
292 , strand_(boost::asio::make_strand(ioContext))
293 , backoffTimer_(ioContext)
295{
296 reOpen();
297}
298
299template <class Handler>
300void
302{
304 strand_, [self = this->shared_from_this()](YieldContext yield) { self->doAccept(yield); });
305}
306
307template <class Handler>
308void
310{
311 if (!strand_.running_in_this_thread())
312 {
313 return boost::asio::post(strand_, [self = this->shared_from_this()] { self->close(); });
314 }
315 backoffTimer_.cancel();
316 ErrorCode ec;
317 acceptor_.close(ec);
318}
319
320//------------------------------------------------------------------------------
321
322template <class Handler>
323template <class ConstBufferSequence>
324void
326 bool ssl,
327 ConstBufferSequence const& buffers,
328 StreamType&& stream,
329 EndpointType remoteAddress)
330{
331 if (ssl)
332 {
333 if (auto const sp = ios().template emplace<SSLHTTPPeer<Handler>>(
334 port_, handler_, ioc_, j_, remoteAddress, buffers, std::move(stream)))
335 sp->run();
336 return;
337 }
338 if (auto const sp = ios().template emplace<PlainHTTPPeer<Handler>>(
339 port_, handler_, ioc_, j_, remoteAddress, buffers, std::move(stream)))
340 sp->run();
341}
342
343template <class Handler>
344void
345Door<Handler>::doAccept(boost::asio::yield_context doYield)
346{
347 while (acceptor_.is_open())
348 {
350 {
351 JLOG(j_.warn()) << "Throttling do_accept for " << acceptDelay_.count() << "ms.";
352 backoffTimer_.expires_after(acceptDelay_);
353 boost::system::error_code tec;
354 backoffTimer_.async_wait(doYield[tec]);
356 continue;
357 }
358
359 ErrorCode ec;
360 EndpointType remoteAddress;
361 StreamType stream(ioc_);
362 SocketType& socket = stream.socket();
363 acceptor_.async_accept(socket, remoteAddress, doYield[ec]);
364 if (ec)
365 {
366 if (ec == boost::asio::error::operation_aborted)
367 break;
368
369 if (ec == boost::asio::error::no_descriptors ||
370 ec == boost::asio::error::no_buffer_space)
371 {
372 char const* const cause = (ec == boost::asio::error::no_descriptors)
373 ? "too many open files"
374 : "kernel buffer space exhausted";
375 JLOG(j_.warn()) << "accept: " << cause << ". Pausing for " << acceptDelay_.count()
376 << "ms.";
377
378 backoffTimer_.expires_after(acceptDelay_);
379 boost::system::error_code tec;
380 backoffTimer_.async_wait(doYield[tec]);
381
383 }
384 else
385 {
386 JLOG(j_.error()) << "accept error: " << ec.message();
387 }
388 continue;
389 }
390
392
393 if (ssl_ && plain_)
394 {
395 if (auto const sp = ios().template emplace<Detector>(
396 port_, handler_, ioc_, std::move(stream), remoteAddress, j_))
397 sp->run();
398 }
399 else if (ssl_ || plain_)
400 {
401 create(ssl_, boost::asio::null_buffers{}, std::move(stream), remoteAddress);
402 }
403 }
404}
405
406template <class Handler>
409{
410#if BOOST_OS_WINDOWS
411 return std::nullopt;
412#else
413 FDStats s;
414 struct rlimit rl{};
415 if (getrlimit(RLIMIT_NOFILE, &rl) != 0 || rl.rlim_cur == RLIM_INFINITY)
416 return std::nullopt;
417 s.limit = static_cast<std::uint64_t>(rl.rlim_cur);
418#if BOOST_OS_LINUX
419 static constexpr char const* kFdDir = "/proc/self/fd";
420#else
421 static constexpr char const* kFdDir = "/dev/fd";
422#endif
423 if (DIR* d = ::opendir(kFdDir))
424 {
425 std::uint64_t cnt = 0;
426 while (::readdir(d) != nullptr)
427 ++cnt;
428 ::closedir(d);
429 // readdir counts '.', '..', and the DIR* itself shows in the list
430 s.used = (cnt >= 3) ? (cnt - 3) : 0;
431 return s;
432 }
433 return std::nullopt;
434#endif
435}
436
437template <class Handler>
438bool
440{
441#if BOOST_OS_WINDOWS
442 return false;
443#else
444 auto const now = ClockType::now();
445 if (now - fdSampleAt_ < kFdSampleInterval)
446 return cachedThrottle_;
447
448 fdSampleAt_ = now;
449 auto const stats = queryFdStats();
451 stats && stats->limit > 0 && stats->used * 100 > stats->limit * kMaxUsedFdPercent;
452 return cachedThrottle_;
453#endif
454}
455
456} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:44
Detector(Port const &port, Handler &handler, boost::asio::io_context &ioc, StreamType &&stream, EndpointType remoteAddress, beast::Journal j)
Definition Door.h:166
Handler & handler_
Definition Door.h:65
void doDetect(YieldContext yield)
Definition Door.h:201
Port const & port_
Definition Door.h:64
boost::asio::strand< boost::asio::io_context::executor_type > strand_
Definition Door.h:70
void close() override
Definition Door.h:194
SocketType & socket_
Definition Door.h:68
beast::Journal const j_
Definition Door.h:71
StreamType stream_
Definition Door.h:67
boost::asio::io_context & ioc_
Definition Door.h:66
EndpointType remoteAddress_
Definition Door.h:69
beast::Journal const j_
Definition Door.h:91
void create(bool ssl, ConstBufferSequence const &buffers, StreamType &&stream, EndpointType remoteAddress)
Definition Door.h:325
boost::asio::ip::tcp ProtocolType
Definition Door.h:54
void run()
Definition Door.h:301
boost::asio::ip::tcp::socket SocketType
Definition Door.h:57
boost::asio::io_context & ioc_
Definition Door.h:94
bool ssl_
Definition Door.h:97
static constexpr std::chrono::milliseconds kFdSampleInterval
Definition Door.h:110
EndpointType getEndpoint() const
Definition Door.h:147
boost::asio::strand< boost::asio::io_context::executor_type > strand_
Definition Door.h:96
bool plain_
Definition Door.h:101
static constexpr std::uint64_t kMaxUsedFdPercent
Definition Door.h:109
AcceptorType acceptor_
Definition Door.h:95
boost::system::error_code ErrorCode
Definition Door.h:52
static constexpr std::chrono::milliseconds kMaxAcceptDelay
Definition Door.h:106
ProtocolType::endpoint EndpointType
Definition Door.h:56
std::chrono::milliseconds acceptDelay_
Definition Door.h:107
void close() override
Close the Door listening socket and connections.
Definition Door.h:309
void doAccept(YieldContext yield)
Definition Door.h:345
Door(Handler &handler, boost::asio::io_context &ioContext, Port const &port, beast::Journal j)
Definition Door.h:282
bool shouldThrottleForFds()
Definition Door.h:439
std::optional< FDStats > queryFdStats() const
Definition Door.h:408
Handler & handler_
Definition Door.h:93
boost::asio::basic_waitable_timer< ClockType > TimerType
Definition Door.h:51
boost::beast::tcp_stream StreamType
Definition Door.h:58
std::chrono::steady_clock ClockType
Definition Door.h:50
Port const & port_
Definition Door.h:92
bool cachedThrottle_
Definition Door.h:112
void reOpen()
Definition Door.h:232
ProtocolType::acceptor AcceptorType
Definition Door.h:55
static constexpr std::chrono::milliseconds kInitialAcceptDelay
Definition Door.h:105
boost::asio::steady_timer backoffTimer_
Definition Door.h:108
ClockType::time_point fdSampleAt_
Definition Door.h:111
boost::asio::yield_context YieldContext
Definition Door.h:53
IOList & ios()
Return the IOList associated with the work.
Definition io_list.h:44
T min(T... args)
STL namespace.
void spawn(Ctx &&ctx, F &&func)
Spawns a coroutine using boost::asio::spawn.
Definition Spawn.h:67
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
boost::beast::ssl_stream< SocketType > StreamType
Definition Handshake.h:24
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition contract.h:52
T str(T... args)
std::uint64_t limit
Definition Door.h:117
std::uint64_t used
Definition Door.h:116
Configuration information for a Server listening port.
Definition Port.h:29