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
20// clang-format off
22{
23 {2, 1},
24 {2, 2}
25};
26// clang-format on
27
28// This ugly construct ensures that supportedProtocolList is sorted in strictly
29// ascending order and doesn't contain any duplicates.
30// FIXME: With C++20 we can use std::is_sorted with an appropriate comparator
31static_assert(
32 []() constexpr -> bool {
34
35 // There should be at least one protocol we're willing to speak.
36 if (len == 0)
37 return false;
38
39 // A list with only one entry is, by definition, sorted so we don't
40 // need to check it.
41 if (len != 1)
42 {
43 for (auto i = 0; i != len - 1; ++i)
44 {
46 return false;
47 }
48 }
49
50 return true;
51 }(),
52 "The list of supported protocols isn't properly sorted.");
53
56{
57 return "XRPL/" + std::to_string(p.first) + "." + std::to_string(p.second);
58}
59
61parseProtocolVersions(boost::beast::string_view const& value)
62{
63 static boost::regex re(
64 "^" // start of line
65 "XRPL/" // The string "XRPL/"
66 "([2-9]|(?:[1-9][0-9]+))" // a number (greater than 2 with no leading
67 // zeroes)
68 "\\." // a period
69 "(0|(?:[1-9][0-9]*))" // a number (no leading zeroes unless exactly
70 // zero)
71 "$" // The end of the string
72 ,
73 boost::regex_constants::optimize);
74
76
77 for (auto const& s : beast::rfc2616::split_commas(value))
78 {
79 boost::smatch m;
80
81 if (boost::regex_match(s, m, re))
82 {
83 std::uint16_t major;
84 std::uint16_t minor;
85 if (!beast::lexicalCastChecked(major, std::string(m[1])))
86 continue;
87
88 if (!beast::lexicalCastChecked(minor, std::string(m[2])))
89 continue;
90
91 auto const proto = make_protocol(major, minor);
92
93 // This is an extra sanity check: we check that the protocol we just
94 // decoded corresponds to the token we were parsing.
95 if (to_string(proto) == s)
96 result.push_back(make_protocol(major, minor));
97 }
98 }
99
100 // We guarantee that the returned list is sorted and contains no duplicates:
101 std::sort(result.begin(), result.end());
102 result.erase(std::unique(result.begin(), result.end()), result.end());
103
104 return result;
105}
106
109{
111
112 // The protocol version we want to negotiate is the largest item in the
113 // intersection of the versions supported by us and the peer. Since the
114 // output of std::set_intersection is sorted, that item is always going
115 // to be the last one. So we get a little clever and avoid the need for
116 // a container:
117 std::function<void(ProtocolVersion const&)> pickVersion = [&result](ProtocolVersion const& v) { result = v; };
118
120 std::begin(versions),
121 std::end(versions),
124 boost::make_function_output_iterator(pickVersion));
125
126 return result;
127}
128
130negotiateProtocolVersion(boost::beast::string_view const& versions)
131{
132 auto const them = parseProtocolVersions(versions);
133
134 return negotiateProtocolVersion(them);
135}
136
137std::string const&
139{
140 static std::string const supported = []() {
141 std::string ret;
142 for (auto const& v : supportedProtocolList)
143 {
144 if (!ret.empty())
145 ret += ", ";
146 ret += to_string(v);
147 }
148
149 return ret;
150 }();
151
152 return supported;
153}
154
155bool
161
162} // 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:175
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:6
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:598
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)