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  {2, 0},
40  {2, 1},
41  {2, 2}
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  return "XRPL/" + std::to_string(p.first) + "." + std::to_string(p.second);
76 }
77 
79 parseProtocolVersions(boost::beast::string_view const& value)
80 {
81  static boost::regex re(
82  "^" // start of line
83  "XRPL/" // The string "XRPL/"
84  "([2-9]|(?:[1-9][0-9]+))" // a number (greater than 2 with no leading
85  // zeroes)
86  "\\." // a period
87  "(0|(?:[1-9][0-9]*))" // a number (no leading zeroes unless exactly
88  // zero)
89  "$" // The end of the string
90  ,
91  boost::regex_constants::optimize);
92 
94 
95  for (auto const& s : beast::rfc2616::split_commas(value))
96  {
97  boost::smatch m;
98 
99  if (boost::regex_match(s, m, re))
100  {
101  std::uint16_t major;
102  std::uint16_t minor;
103  if (!beast::lexicalCastChecked(major, std::string(m[1])))
104  continue;
105 
106  if (!beast::lexicalCastChecked(minor, std::string(m[2])))
107  continue;
108 
109  auto const proto = make_protocol(major, minor);
110 
111  // This is an extra sanity check: we check that the protocol we just
112  // decoded corresponds to the token we were parsing.
113  if (to_string(proto) == s)
114  result.push_back(make_protocol(major, minor));
115  }
116  }
117 
118  // We guarantee that the returned list is sorted and contains no duplicates:
119  std::sort(result.begin(), result.end());
120  result.erase(std::unique(result.begin(), result.end()), result.end());
121 
122  return result;
123 }
124 
127 {
129 
130  // The protocol version we want to negotiate is the largest item in the
131  // intersection of the versions supported by us and the peer. Since the
132  // output of std::set_intersection is sorted, that item is always going
133  // to be the last one. So we get a little clever and avoid the need for
134  // a container:
135  std::function<void(ProtocolVersion const&)> pickVersion =
136  [&result](ProtocolVersion const& v) { result = v; };
137 
139  std::begin(versions),
140  std::end(versions),
143  boost::make_function_output_iterator(pickVersion));
144 
145  return result;
146 }
147 
149 negotiateProtocolVersion(boost::beast::string_view const& versions)
150 {
151  auto const them = parseProtocolVersions(versions);
152 
153  return negotiateProtocolVersion(them);
154 }
155 
156 std::string const&
158 {
159  static std::string const supported = []() {
160  std::string ret;
161  for (auto const& v : supportedProtocolList)
162  {
163  if (!ret.empty())
164  ret += ", ";
165  ret += to_string(v);
166  }
167 
168  return ret;
169  }();
170 
171  return supported;
172 }
173 
174 bool
176 {
178  std::find(
181  v);
182 }
183 
184 } // 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:79
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
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:157
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:175
std::string::empty
T empty(T... args)
std::unique
T unique(T... args)
std::optional
std::vector::end
T end(T... args)
std::set_intersection
T set_intersection(T... args)
ripple::negotiateProtocolVersion
std::optional< ProtocolVersion > negotiateProtocolVersion(std::vector< ProtocolVersion > const &versions)
Given a list of supported protocol versions, choose the one we prefer.
Definition: ProtocolVersion.cpp:126