rippled
Loading...
Searching...
No Matches
Handshake.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLE_OVERLAY_HANDSHAKE_H_INCLUDED
21#define RIPPLE_OVERLAY_HANDSHAKE_H_INCLUDED
22
23#include <xrpld/app/main/Application.h>
24#include <xrpld/overlay/detail/ProtocolVersion.h>
25#include <xrpl/beast/utility/Journal.h>
26#include <xrpl/protocol/BuildInfo.h>
27#include <boost/asio/ip/tcp.hpp>
28#include <boost/asio/ssl/context.hpp>
29#include <boost/asio/ssl/stream.hpp>
30#include <boost/beast/core/tcp_stream.hpp>
31#include <boost/beast/ssl/ssl_stream.hpp>
32
33#include <boost/asio/ssl.hpp>
34#include <boost/beast/http/dynamic_body.hpp>
35#include <boost/beast/http/empty_body.hpp>
36#include <boost/beast/http/fields.hpp>
37#include <optional>
38#include <utility>
39
40namespace ripple {
41
42using socket_type = boost::beast::tcp_stream;
43using stream_type = boost::beast::ssl_stream<socket_type>;
45 boost::beast::http::request<boost::beast::http::empty_body>;
47 boost::beast::http::request<boost::beast::http::dynamic_body>;
49 boost::beast::http::response<boost::beast::http::dynamic_body>;
50
62
65void
67 boost::beast::http::fields& h,
68 uint256 const& sharedValue,
70 beast::IP::Address public_ip,
71 beast::IP::Address remote_ip,
72 Application& app);
73
87 boost::beast::http::fields const& headers,
88 uint256 const& sharedValue,
90 beast::IP::Address public_ip,
91 beast::IP::Address remote,
92 Application& app);
93
107 bool crawlPublic,
108 bool comprEnabled,
109 bool ledgerReplayEnabled,
110 bool txReduceRelayEnabled,
111 bool vpReduceRelayEnabled);
112
127 bool crawlPublic,
128 http_request_type const& req,
129 beast::IP::Address public_ip,
130 beast::IP::Address remote_ip,
131 uint256 const& sharedValue,
133 ProtocolVersion version,
134 Application& app);
135
136// Protocol features negotiated via HTTP handshake.
137// The format is:
138// X-Protocol-Ctl: feature1=value1[,value2]*[\s*;\s*feature2=value1[,value2]*]*
139// value: \S+
140
141// compression feature
142static constexpr char FEATURE_COMPR[] = "compr";
143// validation/proposal reduce-relay feature
144static constexpr char FEATURE_VPRR[] = "vprr";
145// transaction reduce-relay feature
146static constexpr char FEATURE_TXRR[] = "txrr";
147// ledger replay
148static constexpr char FEATURE_LEDGER_REPLAY[] = "ledgerreplay";
149static constexpr char DELIM_FEATURE[] = ";";
150static constexpr char DELIM_VALUE[] = ",";
151
160 boost::beast::http::fields const& headers,
161 std::string const& feature);
162
171bool
173 boost::beast::http::fields const& headers,
174 std::string const& feature,
175 std::string const& value);
176
182bool
184 boost::beast::http::fields const& headers,
185 std::string const& feature);
186
197template <typename headers>
198bool
200 headers const& request,
201 std::string const& feature,
202 std::string value,
203 bool config)
204{
205 return config && isFeatureValue(request, feature, value);
206}
207
209template <typename headers>
210bool
212 headers const& request,
213 std::string const& feature,
214 bool config)
215{
216 return config && peerFeatureEnabled(request, feature, "1", config);
217}
218
230 bool comprEnabled,
231 bool ledgerReplayEnabled,
232 bool txReduceRelayEnabled,
233 bool vpReduceRelayEnabled);
234
251 http_request_type const& headers,
252 bool comprEnabled,
253 bool ledgerReplayEnabled,
254 bool txReduceRelayEnabled,
255 bool vpReduceRelayEnabled);
256
257} // namespace ripple
258
259#endif
A generic endpoint for log messages.
Definition: Journal.h:59
A public key.
Definition: PublicKey.h:62
boost::asio::ip::address Address
Definition: IPAddress.h:41
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
boost::beast::ssl_stream< socket_type > stream_type
Definition: Handshake.h:43
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.
Definition: Handshake.cpp:91
static constexpr char DELIM_FEATURE[]
Definition: Handshake.h:149
boost::beast::http::response< boost::beast::http::dynamic_body > http_response_type
Definition: Handoff.h:34
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.
Definition: Handshake.cpp:175
static constexpr char FEATURE_COMPR[]
Definition: Handshake.h:142
static constexpr char DELIM_VALUE[]
Definition: Handshake.h:150
std::string makeFeaturesRequestHeader(bool comprEnabled, bool ledgerReplayEnabled, bool txReduceRelayEnabled, bool vpReduceRelayEnabled)
Make request header X-Protocol-Ctl value with supported features.
Definition: Handshake.cpp:72
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.
Definition: Handshake.cpp:389
static constexpr char FEATURE_LEDGER_REPLAY[]
Definition: Handshake.h:148
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:52
std::optional< uint256 > makeSharedValue(stream_type &ssl, beast::Journal journal)
Computes a shared value based on the SSL connection state.
Definition: Handshake.cpp:143
boost::beast::http::request< boost::beast::http::dynamic_body > http_request_type
Definition: Handoff.h:31
bool featureEnabled(boost::beast::http::fields const &headers, std::string const &feature)
Check if a feature is enabled.
Definition: Handshake.cpp:64
std::optional< std::string > getFeatureValue(boost::beast::http::fields const &headers, std::string const &feature)
Get feature's header value.
Definition: Handshake.cpp:36
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:199
static constexpr char FEATURE_TXRR[]
Definition: Handshake.h:146
auto makeRequest(bool crawlPublic, bool comprEnabled, bool ledgerReplayEnabled, bool txReduceRelayEnabled, bool vpReduceRelayEnabled) -> request_type
Make outbound http request.
Definition: Handshake.cpp:362
boost::beast::http::request< boost::beast::http::empty_body > request_type
Definition: Handshake.h:45
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.
Definition: Handshake.cpp:224
boost::beast::tcp_stream socket_type
Definition: Handshake.h:42
static constexpr char FEATURE_VPRR[]
Definition: Handshake.h:144