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  {2, 2}
43 };
44 // clang-format on
45 
46 // This ugly construct ensures that supportedProtocolList is sorted in strictly
47 // ascending order and doesn't contain any duplicates.
48 // FIXME: With C++20 we can use std::is_sorted with an appropriate comparator
49 static_assert(
50  []() constexpr->bool {
51  auto const len = std::distance(
52  std::begin(supportedProtocolList), std::end(supportedProtocolList));
53 
54  // There should be at least one protocol we're willing to speak.
55  if (len == 0)
56  return false;
57 
58  // A list with only one entry is, by definition, sorted so we don't
59  // need to check it.
60  if (len != 1)
61  {
62  for (auto i = 0; i != len - 1; ++i)
63  {
64  if (supportedProtocolList[i] >= supportedProtocolList[i + 1])
65  return false;
66  }
67  }
68 
69  return true;
70  }(),
71  "The list of supported protocols isn't properly sorted.");
72 
74 to_string(ProtocolVersion const& p)
75 {
76  // The legacy protocol uses a different name. This can be removed when we
77  // migrate away from it and require 2.0 or later.
78  if (p == ProtocolVersion{1, 2})
79  return "RTXP/1.2";
80 
81  return "XRPL/" + std::to_string(p.first) + "." + std::to_string(p.second);
82 }
83 
85 parseProtocolVersions(boost::beast::string_view const& value)
86 {
87  static boost::regex re(
88  "^" // start of line
89  "XRPL/" // The string "XRPL/"
90  "([2-9]|(?:[1-9][0-9]+))" // a number (greater than 2 with no leading
91  // zeroes)
92  "\\." // a period
93  "(0|(?:[1-9][0-9]*))" // a number (no leading zeroes unless exactly
94  // zero)
95  "$" // The end of the string
96  ,
97  boost::regex_constants::optimize);
98 
100 
101  for (auto const& s : beast::rfc2616::split_commas(value))
102  {
103  if (s == "RTXP/1.2")
104  {
105  result.push_back(make_protocol(1, 2));
106  continue;
107  }
108 
109  boost::smatch m;
110 
111  if (boost::regex_match(s, m, re))
112  {
113  std::uint16_t major;
114  std::uint16_t minor;
115  if (!beast::lexicalCastChecked(major, std::string(m[1])))
116  continue;
117 
118  if (!beast::lexicalCastChecked(minor, std::string(m[2])))
119  continue;
120 
121  auto const proto = make_protocol(major, minor);
122 
123  // This is an extra sanity check: we check that the protocol we just
124  // decoded corresponds to the token we were parsing.
125  if (to_string(proto) == s)
126  result.push_back(make_protocol(major, minor));
127  }
128  }
129 
130  // We guarantee that the returned list is sorted and contains no duplicates:
131  std::sort(result.begin(), result.end());
132  result.erase(std::unique(result.begin(), result.end()), result.end());
133 
134  return result;
135 }
136 
137 boost::optional<ProtocolVersion>
139 {
140  boost::optional<ProtocolVersion> result;
141 
142  // The protocol version we want to negotiate is the largest item in the
143  // intersection of the versions supported by us and the peer. Since the
144  // output of std::set_intersection is sorted, that item is always going
145  // to be the last one. So we get a little clever and avoid the need for
146  // a container:
147  std::function<void(ProtocolVersion const&)> pickVersion =
148  [&result](ProtocolVersion const& v) { result = v; };
149 
151  std::begin(versions),
152  std::end(versions),
155  boost::make_function_output_iterator(pickVersion));
156 
157  return result;
158 }
159 
160 boost::optional<ProtocolVersion>
161 negotiateProtocolVersion(boost::beast::string_view const& versions)
162 {
163  auto const them = parseProtocolVersions(versions);
164 
165  return negotiateProtocolVersion(them);
166 }
167 
168 std::string const&
170 {
171  static std::string const supported = []() {
172  std::string ret;
173  for (auto const& v : supportedProtocolList)
174  {
175  if (!ret.empty())
176  ret += ", ";
177  ret += to_string(v);
178  }
179 
180  return ret;
181  }();
182 
183  return supported;
184 }
185 
186 bool
188 {
190  std::find(
193  v);
194 }
195 
196 } // 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:85
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:138
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:199
ripple::supportedProtocolVersions
std::string const & supportedProtocolVersions()
The list of all the protocol versions we support.
Definition: ProtocolVersion.cpp:169
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:187
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)