rippled
ProtocolVersion.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2019 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 #include <ripple/beast/core/LexicalCast.h>
21 #include <ripple/beast/rfc2616.h>
22 #include <ripple/overlay/impl/ProtocolVersion.h>
23 #include <boost/function_output_iterator.hpp>
24 #include <boost/regex.hpp>
25 #include <algorithm>
26 #include <functional>
27 
28 namespace ripple {
29 
36 // clang-format off
37 constexpr ProtocolVersion const supportedProtocolList[]
38 {
39  {1, 2},
40  {2, 0},
41  {2, 1}
42 };
43 // clang-format on
44 
45 // This ugly construct ensures that supportedProtocolList is sorted in strictly
46 // ascending order and doesn't contain any duplicates.
47 // FIXME: With C++20 we can use std::is_sorted with an appropriate comparator
48 static_assert(
49  []() constexpr->bool {
50  auto const len = std::distance(
51  std::begin(supportedProtocolList), std::end(supportedProtocolList));
52 
53  // There should be at least one protocol we're willing to speak.
54  if (len == 0)
55  return false;
56 
57  // A list with only one entry is, by definition, sorted so we don't
58  // need to check it.
59  if (len != 1)
60  {
61  for (auto i = 0; i != len - 1; ++i)
62  {
63  if (supportedProtocolList[i] >= supportedProtocolList[i + 1])
64  return false;
65  }
66  }
67 
68  return true;
69  }(),
70  "The list of supported protocols isn't properly sorted.");
71 
73 to_string(ProtocolVersion const& p)
74 {
75  // The legacy protocol uses a different name. This can be removed when we
76  // migrate away from it and require 2.0 or later.
77  if (p == ProtocolVersion{1, 2})
78  return "RTXP/1.2";
79 
80  return "XRPL/" + std::to_string(p.first) + "." + std::to_string(p.second);
81 }
82 
84 parseProtocolVersions(boost::beast::string_view const& value)
85 {
86  static boost::regex re(
87  "^" // start of line
88  "XRPL/" // The string "XRPL/"
89  "([2-9]|(?:[1-9][0-9]+))" // a number (greater than 2 with no leading
90  // zeroes)
91  "\\." // a period
92  "(0|(?:[1-9][0-9]*))" // a number (no leading zeroes unless exactly
93  // zero)
94  "$" // The end of the string
95  ,
96  boost::regex_constants::optimize);
97 
99 
100  for (auto const& s : beast::rfc2616::split_commas(value))
101  {
102  if (s == "RTXP/1.2")
103  {
104  result.push_back(make_protocol(1, 2));
105  continue;
106  }
107 
108  boost::smatch m;
109 
110  if (boost::regex_match(s, m, re))
111  {
112  std::uint16_t major;
113  std::uint16_t minor;
114  if (!beast::lexicalCastChecked(major, std::string(m[1])))
115  continue;
116 
117  if (!beast::lexicalCastChecked(minor, std::string(m[2])))
118  continue;
119 
120  auto const proto = make_protocol(major, minor);
121 
122  // This is an extra sanity check: we check that the protocol we just
123  // decoded corresponds to the token we were parsing.
124  if (to_string(proto) == s)
125  result.push_back(make_protocol(major, minor));
126  }
127  }
128 
129  // We guarantee that the returned list is sorted and contains no duplicates:
130  std::sort(result.begin(), result.end());
131  result.erase(std::unique(result.begin(), result.end()), result.end());
132 
133  return result;
134 }
135 
136 boost::optional<ProtocolVersion>
138 {
139  boost::optional<ProtocolVersion> result;
140 
141  // The protocol version we want to negotiate is the largest item in the
142  // intersection of the versions supported by us and the peer. Since the
143  // output of std::set_intersection is sorted, that item is always going
144  // to be the last one. So we get a little clever and avoid the need for
145  // a container:
146  std::function<void(ProtocolVersion const&)> pickVersion =
147  [&result](ProtocolVersion const& v) { result = v; };
148 
150  std::begin(versions),
151  std::end(versions),
154  boost::make_function_output_iterator(pickVersion));
155 
156  return result;
157 }
158 
159 boost::optional<ProtocolVersion>
160 negotiateProtocolVersion(boost::beast::string_view const& versions)
161 {
162  auto const them = parseProtocolVersions(versions);
163 
164  return negotiateProtocolVersion(them);
165 }
166 
167 std::string const&
169 {
170  static std::string const supported = []() {
171  std::string ret;
172  for (auto const& v : supportedProtocolList)
173  {
174  if (!ret.empty())
175  ret += ", ";
176  ret += to_string(v);
177  }
178 
179  return ret;
180  }();
181 
182  return supported;
183 }
184 
185 bool
187 {
189  std::find(
192  v);
193 }
194 
195 } // namespace ripple
std::string
STL class.
functional
std::pair
std::vector
STL class.
std::find
T find(T... args)
ripple::make_protocol
constexpr ProtocolVersion make_protocol(std::uint16_t major, std::uint16_t minor)
Definition: ProtocolVersion.h:40
ripple::parseProtocolVersions
std::vector< ProtocolVersion > parseProtocolVersions(boost::beast::string_view const &value)
Parse a set of protocol versions.
Definition: ProtocolVersion.cpp:84
ripple::ProtocolVersion
std::pair< std::uint16_t, std::uint16_t > ProtocolVersion
Represents a particular version of the peer-to-peer protocol.
Definition: ProtocolVersion.h:37
std::function
std::sort
T sort(T... args)
algorithm
ripple::negotiateProtocolVersion
boost::optional< ProtocolVersion > negotiateProtocolVersion(std::vector< ProtocolVersion > const &versions)
Given a list of supported protocol versions, choose the one we prefer.
Definition: ProtocolVersion.cpp:137
std::vector::push_back
T push_back(T... args)
ripple::supportedProtocolList
constexpr const ProtocolVersion supportedProtocolList[]
The list of protocol versions we speak and we prefer to use.
Definition: ProtocolVersion.cpp:38
std::to_string
T to_string(T... args)
std::vector::erase
T erase(T... args)
std::uint16_t
beast::rfc2616::split_commas
Result split_commas(FwdIt first, FwdIt last)
Definition: rfc2616.h:186
ripple::supportedProtocolVersions
std::string const & supportedProtocolVersions()
The list of all the protocol versions we support.
Definition: ProtocolVersion.cpp:168
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
beast::lexicalCastChecked
bool lexicalCastChecked(Out &out, In in)
Intelligently convert from one type to another.
Definition: LexicalCast.h:266
std::vector::begin
T begin(T... args)
ripple::isProtocolSupported
bool isProtocolSupported(ProtocolVersion const &v)
Determine whether we support a specific protocol version.
Definition: ProtocolVersion.cpp:186
std::string::empty
T empty(T... args)
std::unique
T unique(T... args)
std::vector::end
T end(T... args)
std::set_intersection
T set_intersection(T... args)