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 
102 makeRequest(bool crawlPublic, bool comprEnabled, bool vpReduceRelayEnabled);
103 
118  bool crawlPublic,
119  http_request_type const& req,
120  beast::IP::Address public_ip,
121  beast::IP::Address remote_ip,
122  uint256 const& sharedValue,
124  ProtocolVersion version,
125  Application& app);
126 
127 // Protocol features negotiated via HTTP handshake.
128 // The format is:
129 // X-Protocol-Ctl: feature1=value1[,value2]*[\s*;\s*feature2=value1[,value2]*]*
130 // value: \S+
131 static constexpr char FEATURE_COMPR[] = "compr"; // compression
132 static constexpr char FEATURE_VPRR[] =
133  "vprr"; // validation/proposal reduce-relay
134 static constexpr char DELIM_FEATURE[] = ";";
135 static constexpr char DELIM_VALUE[] = ",";
136 
145  boost::beast::http::fields const& headers,
146  std::string const& feature);
147 
156 bool
158  boost::beast::http::fields const& headers,
159  std::string const& feature,
160  std::string const& value);
161 
167 bool
169  boost::beast::http::fields const& headers,
170  std::string const& feature);
171 
182 template <typename headers>
183 bool
185  headers const& request,
186  std::string const& feature,
187  std::string value,
188  bool config)
189 {
190  return config && isFeatureValue(request, feature, value);
191 }
192 
194 template <typename headers>
195 bool
197  headers const& request,
198  std::string const& feature,
199  bool config)
200 {
201  return config && peerFeatureEnabled(request, feature, "1", config);
202 }
203 
210 makeFeaturesRequestHeader(bool comprEnabled, bool vpReduceRelayEnabled);
211 
223  http_request_type const& headers,
224  bool comprEnabled,
225  bool vpReduceRelayEnabled);
226 
227 } // namespace ripple
228 
229 #endif
ripple::Application
Definition: Application.h:101
std::string
STL class.
ripple::makeRequest
auto makeRequest(bool crawlPublic, bool comprEnabled, bool vpReduceRelayEnabled) -> request_type
Make outbound http request.
Definition: Handshake.cpp:356
utility
std::pair
ripple::makeFeaturesResponseHeader
std::string makeFeaturesResponseHeader(http_request_type const &headers, bool comprEnabled, bool vpReduceRelayEnabled)
Make response header X-Protocol-Ctl value with supported features.
Definition: Handshake.cpp:85
ripple::DELIM_FEATURE
static constexpr char DELIM_FEATURE[]
Definition: Handshake.h:134
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:131
ripple::FEATURE_VPRR
static constexpr char FEATURE_VPRR[]
Definition: Handshake.h:132
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:131
ripple::DELIM_VALUE
static constexpr char DELIM_VALUE[]
Definition: Handshake.h:135
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:184
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
ripple::makeFeaturesRequestHeader
std::string makeFeaturesRequestHeader(bool comprEnabled, bool vpReduceRelayEnabled)
Make request header X-Protocol-Ctl value with supported features.
Definition: Handshake.cpp:74
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:216
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:375
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:163