1#include <xrpld/app/ledger/LedgerMaster.h>
2#include <xrpld/app/main/Application.h>
3#include <xrpld/overlay/detail/Handshake.h>
5#include <xrpl/basics/base64.h>
6#include <xrpl/beast/core/LexicalCast.h>
7#include <xrpl/beast/rfc2616.h>
8#include <xrpl/protocol/digest.h>
10#include <boost/regex.hpp>
22 auto const header = headers.find(
"X-Protocol-Ctl");
23 if (header == headers.end())
26 boost::regex rx(feature +
"=([^;\\s]+)");
28 if (boost::regex_search(allFeatures,
match, rx))
51 bool ledgerReplayEnabled,
52 bool txReduceRelayEnabled,
53 bool vpReduceRelayEnabled)
58 if (ledgerReplayEnabled)
60 if (txReduceRelayEnabled)
62 if (vpReduceRelayEnabled)
71 bool ledgerReplayEnabled,
72 bool txReduceRelayEnabled,
73 bool vpReduceRelayEnabled)
104 constexpr std::size_t sslMinimumFinishedLength = 12;
106 unsigned char buf[1024];
107 size_t len =
get(ssl, buf,
sizeof(buf));
109 if (len < sslMinimumFinishedLength)
115 SHA512(buf, len, cookie.
data());
122 auto const cookie1 =
hashLastMessage(ssl.native_handle(), SSL_get_finished);
125 JLOG(journal.
error()) <<
"Cookie generation: local setup not complete";
129 auto const cookie2 =
hashLastMessage(ssl.native_handle(), SSL_get_peer_finished);
132 JLOG(journal.
error()) <<
"Cookie generation: peer setup not complete";
136 auto const result = (*cookie1 ^ *cookie2);
140 if (result == beast::zero)
142 JLOG(journal.
error()) <<
"Cookie generation: identical finished messages";
151 boost::beast::http::fields& h,
172 h.insert(
"Session-Signature",
base64_encode(sig.data(), sig.size()));
181 h.insert(
"Remote-IP", remote_ip.to_string());
183 if (!public_ip.is_unspecified())
184 h.insert(
"Local-IP", public_ip.to_string());
188 h.insert(
"Closed-Ledger",
strHex(cl->header().hash));
189 h.insert(
"Previous-Ledger",
strHex(cl->header().parentHash));
195 boost::beast::http::fields
const& headers,
202 if (
auto const iter = headers.find(
"Server-Domain"); iter != headers.end())
208 if (
auto const iter = headers.find(
"Network-ID"); iter != headers.end())
215 if (networkID && nid != *networkID)
219 if (
auto const iter = headers.find(
"Network-Time"); iter != headers.end())
222 TimeKeeper::duration::rep val;
235 auto const tolerance = 20s;
242 return duration_cast<std::chrono::seconds>(a - b);
243 return -duration_cast<std::chrono::seconds>(b - a);
246 auto const offset = calculateOffset(netTime, ourTime);
248 if (
abs(offset) > tolerance)
253 if (
auto const iter = headers.find(
"Public-Key"); iter != headers.end())
276 auto const iter = headers.find(
"Session-Signature");
278 if (iter == headers.end())
290 if (
auto const iter = headers.find(
"Local-IP"); iter != headers.end())
292 boost::system::error_code ec;
293 auto const local_ip = boost::asio::ip::make_address(
std::string_view(iter->value()), ec);
300 "Incorrect Local-IP: " + remote.to_string() +
" instead of " + local_ip.to_string());
303 if (
auto const iter = headers.find(
"Remote-IP"); iter != headers.end())
305 boost::system::error_code ec;
306 auto const remote_ip = boost::asio::ip::make_address(
std::string_view(iter->value()), ec);
315 if (remote_ip != public_ip)
317 "Incorrect Remote-IP: " + public_ip.to_string() +
" instead of " + remote_ip.to_string());
328 bool ledgerReplayEnabled,
329 bool txReduceRelayEnabled,
333 m.method(boost::beast::http::verb::get);
338 m.insert(
"Connection",
"Upgrade");
339 m.insert(
"Connect-As",
"Peer");
340 m.insert(
"Crawl", crawlPublic ?
"public" :
"private");
359 resp.result(boost::beast::http::status::switching_protocols);
360 resp.version(req.version());
361 resp.insert(
"Connection",
"Upgrade");
363 resp.insert(
"Connect-As",
"Peer");
365 resp.insert(
"Crawl", crawlPublic ?
"public" :
"private");
375 buildHandshake(resp, sharedValue, networkID, public_ip, remote_ip, app);
A generic endpoint for log messages.
typename Clock::time_point time_point
typename Clock::duration duration
virtual Config & config()=0
virtual std::uint64_t instanceID() const =0
Returns a 64-bit instance identifier, generated at startup.
virtual std::pair< PublicKey, SecretKey > const & nodeIdentity()=0
bool TX_REDUCE_RELAY_ENABLE
std::string SERVER_DOMAIN
bool VP_REDUCE_RELAY_BASE_SQUELCH_ENABLE
std::shared_ptr< Ledger const > getClosedLedger()
virtual LedgerMaster & getLedgerMaster()=0
virtual TimeKeeper & timeKeeper()=0
An immutable linear range of bytes.
time_point now() const override
Returns the current time, using the server's clock.
Integers of any length that is a multiple of 32-bits.
bool is_public(Address const &addr)
Returns true if the address is a public routable address.
bool is_unspecified(Address const &addr)
Returns true if the address is unspecified.
boost::asio::ip::address Address
bool token_in_list(boost::string_ref const &value, boost::string_ref const &token)
Returns true if the specified token exists in the list.
bool lexicalCastChecked(Out &out, In in)
Intelligently convert from one type to another.
std::string const & getFullVersionString()
Full server version string.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
boost::beast::http::response< boost::beast::http::dynamic_body > http_response_type
static std::optional< base_uint< 512 > > hashLastMessage(SSL const *ssl, size_t(*get)(const SSL *, void *, size_t))
Hashes the latest finished message from an SSL stream.
bool featureEnabled(boost::beast::http::fields const &headers, std::string const &feature)
Check if a feature is enabled.
bool verifyDigest(PublicKey const &publicKey, uint256 const &digest, Slice const &sig, bool mustBeFullyCanonical=true) noexcept
Verify a secp256k1 signature on the digest of a message.
sha512_half_hasher::result_type sha512Half(Args const &... args)
Returns the SHA512-Half of a series of objects.
std::optional< uint256 > makeSharedValue(stream_type &ssl, beast::Journal journal)
Computes a shared value based on the SSL connection state.
static constexpr char FEATURE_COMPR[]
bool isProperlyFormedTomlDomain(std::string_view domain)
Determines if the given string looks like a TOML-file hosting domain.
std::string to_string(base_uint< Bits, Tag > const &a)
T get(Section const §ion, std::string const &name, T const &defaultValue=T{})
Retrieve a key/value pair from a section.
std::string strHex(FwdIt begin, FwdIt end)
boost::beast::http::request< boost::beast::http::dynamic_body > http_request_type
std::string base64_decode(std::string_view data)
boost::beast::ssl_stream< socket_type > stream_type
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
std::string base64_encode(std::uint8_t const *data, std::size_t len)
auto makeRequest(bool crawlPublic, bool comprEnabled, bool ledgerReplayEnabled, bool txReduceRelayEnabled, bool vpReduceRelayEnabled) -> request_type
Make outbound http request.
std::optional< KeyType > publicKeyType(Slice const &slice)
Returns the type of public key.
boost::beast::http::request< boost::beast::http::empty_body > request_type
std::string makeFeaturesRequestHeader(bool comprEnabled, bool ledgerReplayEnabled, bool txReduceRelayEnabled, bool vpReduceRelayEnabled)
Make request header X-Protocol-Ctl value with supported features.
Buffer signDigest(PublicKey const &pk, SecretKey const &sk, uint256 const &digest)
Generate a signature for a message digest.
constexpr Number abs(Number x) noexcept
static constexpr char DELIM_FEATURE[]
http_response_type makeResponse(bool crawlPublic, http_request_type const &req, beast::IP::Address public_ip, beast::IP::Address remote_ip, uint256 const &sharedValue, std::optional< std::uint32_t > networkID, ProtocolVersion protocol, Application &app)
Make http response.
std::string const & supportedProtocolVersions()
The list of all the protocol versions we support.
std::optional< std::string > getFeatureValue(boost::beast::http::fields const &headers, std::string const &feature)
Get feature's header value.
std::enable_if_t< std::is_same< T, char >::value||std::is_same< T, unsigned char >::value, Slice > makeSlice(std::array< T, N > const &a)
static constexpr char FEATURE_LEDGER_REPLAY[]
static constexpr char FEATURE_VPRR[]
std::string makeFeaturesResponseHeader(http_request_type const &headers, bool comprEnabled, bool ledgerReplayEnabled, bool txReduceRelayEnabled, bool vpReduceRelayEnabled)
Make response header X-Protocol-Ctl value with supported features.
PublicKey verifyHandshake(boost::beast::http::fields const &headers, xrpl::uint256 const &sharedValue, std::optional< std::uint32_t > networkID, beast::IP::Address public_ip, beast::IP::Address remote, Application &app)
Validate header fields necessary for upgrading the link to the peer protocol.
bool isFeatureValue(boost::beast::http::fields const &headers, std::string const &feature, std::string const &value)
Check if a feature's value is equal to the specified value.
static constexpr char FEATURE_TXRR[]
void buildHandshake(boost::beast::http::fields &h, xrpl::uint256 const &sharedValue, std::optional< std::uint32_t > networkID, beast::IP::Address public_ip, beast::IP::Address remote_ip, Application &app)
Insert fields headers necessary for upgrading the link to the peer protocol.