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>
21 boost::beast::http::fields
const& headers,
24 auto const header = headers.find(
"X-Protocol-Ctl");
25 if (header == headers.end())
28 boost::regex rx(feature +
"=([^;\\s]+)");
30 if (boost::regex_search(allFeatures,
match, rx))
37 boost::beast::http::fields
const& headers,
49 boost::beast::http::fields
const& headers,
58 bool ledgerReplayEnabled,
59 bool txReduceRelayEnabled,
60 bool vpReduceRelayEnabled)
65 if (ledgerReplayEnabled)
67 if (txReduceRelayEnabled)
69 if (vpReduceRelayEnabled)
78 bool ledgerReplayEnabled,
79 bool txReduceRelayEnabled,
80 bool vpReduceRelayEnabled)
111 constexpr std::size_t sslMinimumFinishedLength = 12;
113 unsigned char buf[1024];
114 size_t len =
get(ssl, buf,
sizeof(buf));
116 if (len < sslMinimumFinishedLength)
122 SHA512(buf, len, cookie.
data());
129 auto const cookie1 =
hashLastMessage(ssl.native_handle(), SSL_get_finished);
132 JLOG(journal.
error()) <<
"Cookie generation: local setup not complete";
140 JLOG(journal.
error()) <<
"Cookie generation: peer setup not complete";
144 auto const result = (*cookie1 ^ *cookie2);
148 if (result == beast::zero)
150 JLOG(journal.
error())
151 <<
"Cookie generation: identical finished messages";
160 boost::beast::http::fields& h,
186 h.insert(
"Session-Signature",
base64_encode(sig.data(), sig.size()));
195 h.insert(
"Remote-IP", remote_ip.to_string());
197 if (!public_ip.is_unspecified())
198 h.insert(
"Local-IP", public_ip.to_string());
202 h.insert(
"Closed-Ledger",
strHex(cl->info().hash));
203 h.insert(
"Previous-Ledger",
strHex(cl->info().parentHash));
209 boost::beast::http::fields
const& headers,
216 if (
auto const iter = headers.find(
"Server-Domain"); iter != headers.end())
222 if (
auto const iter = headers.find(
"Network-ID"); iter != headers.end())
229 if (networkID && nid != *networkID)
233 if (
auto const iter = headers.find(
"Network-Time"); iter != headers.end())
236 TimeKeeper::duration::rep val;
249 auto const tolerance = 20s;
257 return duration_cast<std::chrono::seconds>(a - b);
258 return -duration_cast<std::chrono::seconds>(b - a);
261 auto const offset = calculateOffset(netTime, ourTime);
263 if (
abs(offset) > tolerance)
268 if (
auto const iter = headers.find(
"Public-Key"); iter != headers.end())
292 auto const iter = headers.find(
"Session-Signature");
294 if (iter == headers.end())
306 if (
auto const iter = headers.find(
"Local-IP"); iter != headers.end())
308 boost::system::error_code ec;
309 auto const local_ip =
317 "Incorrect Local-IP: " + remote.to_string() +
" instead of " +
318 local_ip.to_string());
321 if (
auto const iter = headers.find(
"Remote-IP"); iter != headers.end())
323 boost::system::error_code ec;
324 auto const remote_ip =
335 if (remote_ip != public_ip)
337 "Incorrect Remote-IP: " + public_ip.to_string() +
338 " instead of " + remote_ip.to_string());
349 bool ledgerReplayEnabled,
350 bool txReduceRelayEnabled,
354 m.method(boost::beast::http::verb::get);
359 m.insert(
"Connection",
"Upgrade");
360 m.insert(
"Connect-As",
"Peer");
361 m.insert(
"Crawl", crawlPublic ?
"public" :
"private");
367 txReduceRelayEnabled,
368 vpReduceRelayEnabled));
384 resp.result(boost::beast::http::status::switching_protocols);
385 resp.version(req.version());
386 resp.insert(
"Connection",
"Upgrade");
388 resp.insert(
"Connect-As",
"Peer");
390 resp.insert(
"Crawl", crawlPublic ?
"public" :
"private");
400 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 TimeKeeper & timeKeeper()=0
virtual LedgerMaster & getLedgerMaster()=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 VP_REDUCE_RELAY_BASE_SQUELCH_ENABLE
bool TX_REDUCE_RELAY_ENABLE
std::string SERVER_DOMAIN
std::shared_ptr< Ledger const > getClosedLedger()
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::ssl_stream< socket_type > stream_type
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
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.
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.
static constexpr char DELIM_FEATURE[]
boost::beast::http::response< boost::beast::http::dynamic_body > http_response_type
void buildHandshake(boost::beast::http::fields &h, ripple::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.
static constexpr char FEATURE_COMPR[]
std::string makeFeaturesRequestHeader(bool comprEnabled, bool ledgerReplayEnabled, bool txReduceRelayEnabled, bool vpReduceRelayEnabled)
Make request header X-Protocol-Ctl value with supported features.
std::string base64_decode(std::string_view data)
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.
static constexpr char FEATURE_LEDGER_REPLAY[]
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.
std::optional< uint256 > makeSharedValue(stream_type &ssl, beast::Journal journal)
Computes a shared value based on the SSL connection state.
std::string const & supportedProtocolVersions()
The list of all the protocol versions we support.
std::optional< KeyType > publicKeyType(Slice const &slice)
Returns the type of public key.
std::string strHex(FwdIt begin, FwdIt end)
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.
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)
std::string base64_encode(std::uint8_t const *data, std::size_t len)
Buffer signDigest(PublicKey const &pk, SecretKey const &sk, uint256 const &digest)
Generate a signature for a message digest.
boost::beast::http::request< boost::beast::http::dynamic_body > http_request_type
bool featureEnabled(boost::beast::http::fields const &headers, std::string const &feature)
Check if a feature is enabled.
std::optional< std::string > getFeatureValue(boost::beast::http::fields const &headers, std::string const &feature)
Get feature's header value.
static constexpr char FEATURE_TXRR[]
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.
auto makeRequest(bool crawlPublic, bool comprEnabled, bool ledgerReplayEnabled, bool txReduceRelayEnabled, bool vpReduceRelayEnabled) -> request_type
Make outbound http request.
boost::beast::http::request< boost::beast::http::empty_body > request_type
PublicKey verifyHandshake(boost::beast::http::fields const &headers, ripple::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 isProperlyFormedTomlDomain(std::string_view domain)
Determines if the given string looks like a TOML-file hosting domain.
sha512_half_hasher::result_type sha512Half(Args const &... args)
Returns the SHA512-Half of a series of objects.
static constexpr char FEATURE_VPRR[]
constexpr Number abs(Number x) noexcept