rippled
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 <ripple/app/main/Application.h>
24 #include <ripple/beast/utility/Journal.h>
25 #include <ripple/overlay/impl/ProtocolVersion.h>
26 #include <ripple/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 
40 namespace ripple {
41 
42 using socket_type = boost::beast::tcp_stream;
43 using stream_type = boost::beast::ssl_stream<socket_type>;
44 using request_type =
45  boost::beast::http::request<boost::beast::http::empty_body>;
46 using http_request_type =
47  boost::beast::http::request<boost::beast::http::dynamic_body>;
48 using http_response_type =
49  boost::beast::http::response<boost::beast::http::dynamic_body>;
50 
62 
65 void
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
142 static constexpr char FEATURE_COMPR[] = "compr";
143 // validation/proposal reduce-relay feature
144 static constexpr char FEATURE_VPRR[] = "vprr";
145 // transaction reduce-relay feature
146 static constexpr char FEATURE_TXRR[] = "txrr";
147 // ledger replay
148 static constexpr char FEATURE_LEDGER_REPLAY[] = "ledgerreplay";
149 static constexpr char DELIM_FEATURE[] = ";";
150 static constexpr char DELIM_VALUE[] = ",";
151 
160  boost::beast::http::fields const& headers,
161  std::string const& feature);
162 
171 bool
173  boost::beast::http::fields const& headers,
174  std::string const& feature,
175  std::string const& value);
176 
182 bool
184  boost::beast::http::fields const& headers,
185  std::string const& feature);
186 
197 template <typename headers>
198 bool
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 
209 template <typename headers>
210 bool
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
ripple::Application
Definition: Application.h:115
std::string
STL class.
utility
std::pair
ripple::DELIM_FEATURE
static constexpr char DELIM_FEATURE[]
Definition: Handshake.h:149
ripple::makeFeaturesRequestHeader
std::string makeFeaturesRequestHeader(bool comprEnabled, bool ledgerReplayEnabled, bool txReduceRelayEnabled, bool vpReduceRelayEnabled)
Make request header X-Protocol-Ctl value with supported features.
Definition: Handshake.cpp:74
ripple::FEATURE_LEDGER_REPLAY
static constexpr char FEATURE_LEDGER_REPLAY[]
Definition: Handshake.h:148
ripple::makeSharedValue
std::optional< uint256 > makeSharedValue(stream_type &ssl, beast::Journal journal)
Computes a shared value based on the SSL connection state.
Definition: Handshake.cpp:145
ripple::FEATURE_VPRR
static constexpr char FEATURE_VPRR[]
Definition: Handshake.h:144
ripple::isFeatureValue
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:54
ripple::FEATURE_TXRR
static constexpr char FEATURE_TXRR[]
Definition: Handshake.h:146
ripple::featureEnabled
bool featureEnabled(boost::beast::http::fields const &headers, std::string const &feature)
Check if a feature is enabled.
Definition: Handshake.cpp:66
ripple::base_uint< 256 >
beast::IP::Address
boost::asio::ip::address Address
Definition: IPAddress.h:41
ripple::PublicKey
A public key.
Definition: PublicKey.h:59
ripple::FEATURE_COMPR
static constexpr char FEATURE_COMPR[]
Definition: Handshake.h:142
ripple::DELIM_VALUE
static constexpr char DELIM_VALUE[]
Definition: Handshake.h:150
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
ripple::peerFeatureEnabled
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
ripple::getFeatureValue
std::optional< std::string > getFeatureValue(boost::beast::http::fields const &headers, std::string const &feature)
Get feature's header value.
Definition: Handshake.cpp:38
ripple::stream_type
boost::beast::ssl_stream< socket_type > stream_type
Definition: Handshake.h:43
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::socket_type
boost::beast::tcp_stream socket_type
Definition: Handshake.h:42
ripple::request_type
boost::beast::http::request< boost::beast::http::empty_body > request_type
Definition: Handshake.h:45
optional
ripple::verifyHandshake
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:226
ripple::makeFeaturesResponseHeader
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:93
ripple::makeRequest
auto makeRequest(bool crawlPublic, bool comprEnabled, bool ledgerReplayEnabled, bool txReduceRelayEnabled, bool vpReduceRelayEnabled) -> request_type
Make outbound http request.
Definition: Handshake.cpp:365
ripple::makeResponse
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:392
ripple::http_request_type
boost::beast::http::request< boost::beast::http::dynamic_body > http_request_type
Definition: Handshake.h:47
ripple::http_response_type
boost::beast::http::response< boost::beast::http::dynamic_body > http_response_type
Definition: Handshake.h:49
ripple::buildHandshake
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:177