xrpld
Loading...
Searching...
No Matches
Handshake.h
1#pragma once
2
3#include <xrpld/app/main/Application.h>
4#include <xrpld/overlay/detail/ProtocolVersion.h>
5
6#include <xrpl/basics/base_uint.h>
7#include <xrpl/beast/net/IPAddress.h>
8#include <xrpl/beast/utility/Journal.h>
9
10#include <boost/asio/ssl.hpp>
11#include <boost/beast/core/tcp_stream.hpp>
12#include <boost/beast/http/dynamic_body.hpp>
13#include <boost/beast/http/empty_body.hpp>
14#include <boost/beast/http/fields.hpp>
15#include <boost/beast/ssl/ssl_stream.hpp>
16
17#include <cstdint>
18#include <optional>
19#include <string>
20
21namespace xrpl {
22
23using socket_type = boost::beast::tcp_stream;
24using stream_type = boost::beast::ssl_stream<socket_type>;
25using request_type = boost::beast::http::request<boost::beast::http::empty_body>;
26using http_request_type = boost::beast::http::request<boost::beast::http::dynamic_body>;
27using http_response_type = boost::beast::http::response<boost::beast::http::dynamic_body>;
28
41
45void
47 boost::beast::http::fields& h,
48 uint256 const& sharedValue,
50 beast::ip::Address publicIp,
51 beast::ip::Address remoteIp,
52 Application& app);
53
68 boost::beast::http::fields const& headers,
69 uint256 const& sharedValue,
71 beast::ip::Address publicIp,
72 beast::ip::Address remote,
73 Application& app);
74
89 bool crawlPublic,
90 bool comprEnabled,
91 bool ledgerReplayEnabled,
92 bool txReduceRelayEnabled,
93 bool vpReduceRelayEnabled);
94
110 bool crawlPublic,
111 http_request_type const& req,
112 beast::ip::Address publicIp,
113 beast::ip::Address remoteIp,
114 uint256 const& sharedValue,
116 ProtocolVersion version,
117 Application& app);
118
119// Protocol features negotiated via HTTP handshake.
120// The format is:
121// X-Protocol-Ctl: feature1=value1[,value2]*[\s*;\s*feature2=value1[,value2]*]*
122// value: \S+
123
124// compression feature
125static constexpr char kFeatureCompr[] = "compr";
126// validation/proposal reduce-relay base squelch feature
127static constexpr char kFeatureVprr[] = "vprr";
128// transaction reduce-relay feature
129static constexpr char kFeatureTxrr[] = "txrr";
130// ledger replay
131static constexpr char kFeatureLedgerReplay[] = "ledgerreplay";
132static constexpr char kDelimFeature[] = ";";
133static constexpr char kDelimValue[] = ",";
134
143getFeatureValue(boost::beast::http::fields const& headers, std::string const& feature);
144
154bool
156 boost::beast::http::fields const& headers,
157 std::string const& feature,
158 std::string const& value);
159
166bool
167featureEnabled(boost::beast::http::fields const& headers, std::string const& feature);
168
180template <typename Headers>
181bool
183 Headers const& request,
184 std::string const& feature,
185 std::string value,
186 bool config)
187{
188 return config && isFeatureValue(request, feature, value);
189}
190
194template <typename Headers>
195bool
196peerFeatureEnabled(Headers const& request, std::string const& feature, bool config)
197{
198 return config && peerFeatureEnabled(request, feature, "1", config);
199}
200
213 bool comprEnabled,
214 bool ledgerReplayEnabled,
215 bool txReduceRelayEnabled,
216 bool vpReduceRelayEnabled);
217
234 http_request_type const& headers,
235 bool comprEnabled,
236 bool ledgerReplayEnabled,
237 bool txReduceRelayEnabled,
238 bool vpReduceRelayEnabled);
239
240} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:44
A public key.
Definition PublicKey.h:53
boost::asio::ip::address Address
Definition IPAddress.h:20
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
boost::beast::tcp_stream socket_type
Definition Handshake.h:23
static constexpr char kDelimFeature[]
Definition Handshake.h:132
bool featureEnabled(boost::beast::http::fields const &headers, std::string const &feature)
Check if a feature is enabled.
Definition Handshake.cpp:77
std::optional< uint256 > makeSharedValue(stream_type &ssl, beast::Journal journal)
Computes a shared value based on the SSL connection state.
static constexpr char kFeatureLedgerReplay[]
Definition Handshake.h:131
boost::beast::http::request< boost::beast::http::empty_body > request_type
Definition Handshake.h:25
auto makeRequest(bool crawlPublic, bool comprEnabled, bool ledgerReplayEnabled, bool txReduceRelayEnabled, bool vpReduceRelayEnabled) -> request_type
Make outbound http request.
static constexpr char kDelimValue[]
Definition Handshake.h:133
static constexpr char kFeatureTxrr[]
Definition Handshake.h:129
std::string makeFeaturesRequestHeader(bool comprEnabled, bool ledgerReplayEnabled, bool txReduceRelayEnabled, bool vpReduceRelayEnabled)
Make request header X-Protocol-Ctl value with supported features.
Definition Handshake.cpp:83
void buildHandshake(boost::beast::http::fields &h, xrpl::uint256 const &sharedValue, std::optional< std::uint32_t > networkID, beast::ip::Address publicIp, beast::ip::Address remoteIp, Application &app)
Insert fields headers necessary for upgrading the link to the peer protocol.
http_response_type makeResponse(bool crawlPublic, http_request_type const &req, beast::ip::Address publicIp, beast::ip::Address remoteIp, uint256 const &sharedValue, std::optional< std::uint32_t > networkID, ProtocolVersion protocol, Application &app)
Make http response.
std::optional< std::string > getFeatureValue(boost::beast::http::fields const &headers, std::string const &feature)
Get feature's header value.
Definition Handshake.cpp:51
bool peerFeatureEnabled(Headers const &request, std::string const &feature, std::string value, bool config)
Check if a feature should be enabled for a peer.
Definition Handshake.h:182
std::pair< std::uint16_t, std::uint16_t > ProtocolVersion
Represents a particular version of the peer-to-peer protocol.
boost::beast::ssl_stream< socket_type > stream_type
Definition Handshake.h:24
boost::beast::http::request< boost::beast::http::dynamic_body > http_request_type
Definition Handoff.h:12
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.
static constexpr char kFeatureCompr[]
Definition Handshake.h:125
BaseUInt< 256 > uint256
Definition base_uint.h:580
PublicKey verifyHandshake(boost::beast::http::fields const &headers, xrpl::uint256 const &sharedValue, std::optional< std::uint32_t > networkID, beast::ip::Address publicIp, beast::ip::Address remote, Application &app)
Validate header fields necessary for upgrading the link to the peer protocol.
static constexpr char kFeatureVprr[]
Definition Handshake.h:127
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.
Definition Handshake.cpp:65
boost::beast::http::response< boost::beast::http::dynamic_body > http_response_type
Definition Handoff.h:14