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 
104  bool crawlPublic,
105  bool comprEnabled,
106  bool vpReduceRelayEnabled,
107  bool ledgerReplayEnabled);
108 
123  bool crawlPublic,
124  http_request_type const& req,
125  beast::IP::Address public_ip,
126  beast::IP::Address remote_ip,
127  uint256 const& sharedValue,
129  ProtocolVersion version,
130  Application& app);
131 
132 // Protocol features negotiated via HTTP handshake.
133 // The format is:
134 // X-Protocol-Ctl: feature1=value1[,value2]*[\s*;\s*feature2=value1[,value2]*]*
135 // value: \S+
136 static constexpr char FEATURE_COMPR[] = "compr"; // compression
137 static constexpr char FEATURE_VPRR[] =
138  "vprr"; // validation/proposal reduce-relay
139 static constexpr char FEATURE_LEDGER_REPLAY[] =
140  "ledgerreplay"; // ledger replay
141 static constexpr char DELIM_FEATURE[] = ";";
142 static constexpr char DELIM_VALUE[] = ",";
143 
152  boost::beast::http::fields const& headers,
153  std::string const& feature);
154 
163 bool
165  boost::beast::http::fields const& headers,
166  std::string const& feature,
167  std::string const& value);
168 
174 bool
176  boost::beast::http::fields const& headers,
177  std::string const& feature);
178 
189 template <typename headers>
190 bool
192  headers const& request,
193  std::string const& feature,
194  std::string value,
195  bool config)
196 {
197  return config && isFeatureValue(request, feature, value);
198 }
199 
201 template <typename headers>
202 bool
204  headers const& request,
205  std::string const& feature,
206  bool config)
207 {
208  return config && peerFeatureEnabled(request, feature, "1", config);
209 }
210 
219  bool comprEnabled,
220  bool vpReduceRelayEnabled,
221  bool ledgerReplayEnabled);
222 
235  http_request_type const& headers,
236  bool comprEnabled,
237  bool vpReduceRelayEnabled,
238  bool ledgerReplayEnabled);
239 
240 } // namespace ripple
241 
242 #endif
ripple::Application
Definition: Application.h:102
std::string
STL class.
utility
std::pair
ripple::DELIM_FEATURE
static constexpr char DELIM_FEATURE[]
Definition: Handshake.h:141
ripple::makeFeaturesRequestHeader
std::string makeFeaturesRequestHeader(bool comprEnabled, bool vpReduceRelayEnabled, bool ledgerReplayEnabled)
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:139
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:139
ripple::FEATURE_VPRR
static constexpr char FEATURE_VPRR[]
Definition: Handshake.h:137
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::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
Integers of any length that is a multiple of 32-bits.
Definition: base_uint.h:73
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:136
ripple::DELIM_VALUE
static constexpr char DELIM_VALUE[]
Definition: Handshake.h:142
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:191
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::makeRequest
auto makeRequest(bool crawlPublic, bool comprEnabled, bool vpReduceRelayEnabled, bool ledgerReplayEnabled) -> request_type
Make outbound http request.
Definition: Handshake.cpp:364
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::makeFeaturesResponseHeader
std::string makeFeaturesResponseHeader(http_request_type const &headers, bool comprEnabled, bool vpReduceRelayEnabled, bool ledgerReplayEnabled)
Make response header X-Protocol-Ctl value with supported features.
Definition: Handshake.cpp:90
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:224
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:387
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:171