rippled
Loading...
Searching...
No Matches
ProtocolVersion.cpp
1#include <xrpld/overlay/detail/ProtocolVersion.h>
2
3#include <xrpl/beast/core/LexicalCast.h>
4#include <xrpl/beast/rfc2616.h>
5
6#include <boost/iterator/function_output_iterator.hpp>
7#include <boost/regex.hpp>
8
9#include <algorithm>
10#include <functional>
11
12namespace xrpl {
13
21 {2, 1},
22 {2, 2},
23};
24
25// This ugly construct ensures that supportedProtocolList is sorted in strictly
26// ascending order and doesn't contain any duplicates.
27// FIXME: With C++20 we can use std::is_sorted with an appropriate comparator
28static_assert(
29 []() constexpr -> bool {
30 auto const len =
32
33 // There should be at least one protocol we're willing to speak.
34 if (len == 0)
35 return false;
36
37 // A list with only one entry is, by definition, sorted so we don't
38 // need to check it.
39 if (len != 1)
40 {
41 for (auto i = 0; i != len - 1; ++i)
42 {
44 return false;
45 }
46 }
47
48 return true;
49 }(),
50 "The list of supported protocols isn't properly sorted.");
51
54{
55 return "XRPL/" + std::to_string(p.first) + "." + std::to_string(p.second);
56}
57
59parseProtocolVersions(boost::beast::string_view const& value)
60{
61 static boost::regex const re(
62 "^" // start of line
63 "XRPL/" // The string "XRPL/"
64 "([2-9]|(?:[1-9][0-9]+))" // a number (greater than 2 with no leading
65 // zeroes)
66 "\\." // a period
67 "(0|(?:[1-9][0-9]*))" // a number (no leading zeroes unless exactly
68 // zero)
69 "$" // The end of the string
70 ,
71 boost::regex_constants::optimize);
72
74
75 for (auto const& s : beast::rfc2616::split_commas(value))
76 {
77 boost::smatch m;
78
79 if (boost::regex_match(s, m, re))
80 {
81 std::uint16_t major = 0;
82 std::uint16_t minor = 0;
83 if (!beast::lexicalCastChecked(major, std::string(m[1])))
84 continue;
85
86 if (!beast::lexicalCastChecked(minor, std::string(m[2])))
87 continue;
88
89 auto const proto = make_protocol(major, minor);
90
91 // This is an extra sanity check: we check that the protocol we just
92 // decoded corresponds to the token we were parsing.
93 if (to_string(proto) == s)
94 result.push_back(make_protocol(major, minor));
95 }
96 }
97
98 // We guarantee that the returned list is sorted and contains no duplicates:
99 std::sort(result.begin(), result.end());
100 result.erase(std::unique(result.begin(), result.end()), result.end());
101
102 return result;
103}
104
107{
109
110 // The protocol version we want to negotiate is the largest item in the
111 // intersection of the versions supported by us and the peer. Since the
112 // output of std::set_intersection is sorted, that item is always going
113 // to be the last one. So we get a little clever and avoid the need for
114 // a container:
115 std::function<void(ProtocolVersion const&)> const pickVersion =
116 [&result](ProtocolVersion const& v) { result = v; };
117
119 std::begin(versions),
120 std::end(versions),
123 boost::make_function_output_iterator(pickVersion));
124
125 return result;
126}
127
129negotiateProtocolVersion(boost::beast::string_view const& versions)
130{
131 auto const them = parseProtocolVersions(versions);
132
133 return negotiateProtocolVersion(them);
134}
135
136std::string const&
138{
139 static std::string const supported = []() {
140 std::string ret;
141 for (auto const& v : supportedProtocolList)
142 {
143 if (!ret.empty())
144 ret += ", ";
145 ret += to_string(v);
146 }
147
148 return ret;
149 }();
150
151 return supported;
152}
153
154bool
160
161} // namespace xrpl
T begin(T... args)
T distance(T... args)
T empty(T... args)
T end(T... args)
T erase(T... args)
T find(T... args)
Result split_commas(FwdIt first, FwdIt last)
Definition rfc2616.h:177
bool lexicalCastChecked(Out &out, In in)
Intelligently convert from one type to another.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
std::vector< ProtocolVersion > parseProtocolVersions(boost::beast::string_view const &value)
Parse a set of protocol versions.
bool isProtocolSupported(ProtocolVersion const &v)
Determine whether we support a specific protocol version.
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:602
std::optional< ProtocolVersion > negotiateProtocolVersion(std::vector< ProtocolVersion > const &versions)
Given a list of supported protocol versions, choose the one we prefer.
constexpr ProtocolVersion make_protocol(std::uint16_t major, std::uint16_t minor)
constexpr ProtocolVersion const supportedProtocolList[]
The list of protocol versions we speak and we prefer to use.
std::string const & supportedProtocolVersions()
The list of all the protocol versions we support.
T push_back(T... args)
T set_intersection(T... args)
T sort(T... args)
T to_string(T... args)
T unique(T... args)