xrpld
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/beast/core/string_type.hpp>
7#include <boost/iterator/function_output_iterator.hpp>
8#include <boost/regex/v5/regbase.hpp>
9#include <boost/regex/v5/regex.hpp>
10#include <boost/regex/v5/regex_match.hpp>
11
12#include <algorithm>
13#include <cstdint>
14#include <functional>
15#include <iterator>
16#include <optional>
17#include <ranges>
18#include <string>
19#include <vector>
20
21namespace xrpl {
22
29
31 {2, 2},
32 {2, 3},
33};
34
35// There should be at least one protocol we're willing to speak.
36static_assert(
37 !std::ranges::empty(kSupportedProtocolList),
38 "There must be at least one supported protocol.");
39
40// Searching for an adjacent pair where the first element is not less than the
41// second one proves the list is sorted in strictly ascending order, which in
42// turn means it holds no duplicates.
43static_assert(
45 std::ranges::end(kSupportedProtocolList),
46 "The list of supported protocols isn't properly sorted.");
47
48std::string
50{
51 return "XRPL/" + std::to_string(p.first) + "." + std::to_string(p.second);
52}
53
55parseProtocolVersions(boost::beast::string_view const& value)
56{
57 static boost::regex const kRE(
58 "^" // start of line
59 "XRPL/" // The string "XRPL/"
60 "([2-9]|(?:[1-9][0-9]+))" // a number (greater than 2 with no leading
61 // zeroes)
62 "\\." // a period
63 "(0|(?:[1-9][0-9]*))" // a number (no leading zeroes unless exactly
64 // zero)
65 "$" // The end of the string
66 ,
67 boost::regex_constants::optimize);
68
70
71 for (auto const& s : beast::rfc2616::splitCommas(value))
72 {
73 boost::smatch m;
74
75 if (boost::regex_match(s, m, kRE))
76 {
77 std::uint16_t major = 0;
78 std::uint16_t minor = 0;
79 if (!beast::lexicalCastChecked(major, std::string(m[1])))
80 continue;
81
82 if (!beast::lexicalCastChecked(minor, std::string(m[2])))
83 continue;
84
85 auto const proto = makeProtocol(major, minor);
86
87 // This is an extra sanity check: we check that the protocol we just
88 // decoded corresponds to the token we were parsing.
89 if (to_string(proto) == s)
90 result.push_back(makeProtocol(major, minor));
91 }
92 }
93
94 // We guarantee that the returned list is sorted and contains no duplicates:
95 std::ranges::sort(result);
96 auto const uniq = std::ranges::unique(result);
97 result.erase(uniq.begin(), uniq.end());
98
99 return result;
100}
101
104{
106
107 // The protocol version we want to negotiate is the largest item in the
108 // intersection of the versions supported by us and the peer. Since the
109 // output of std::set_intersection is sorted, that item is always going
110 // to be the last one. So we get a little clever and avoid the need for
111 // a container:
112 std::function<void(ProtocolVersion const&)> const pickVersion =
113 [&result](ProtocolVersion const& v) { result = v; };
114
116 versions, kSupportedProtocolList, boost::make_function_output_iterator(pickVersion));
117
118 return result;
119}
120
122negotiateProtocolVersion(boost::beast::string_view const& versions)
123{
124 auto const them = parseProtocolVersions(versions);
125
126 return negotiateProtocolVersion(them);
127}
128
129std::string const&
131{
132 static std::string const kSupported = []() {
133 std::string ret;
134 for (auto const& v : kSupportedProtocolList)
135 {
136 if (!ret.empty())
137 ret += ", ";
138 ret += to_string(v);
139 }
140
141 return ret;
142 }();
143
144 return kSupported;
145}
146
147bool
152
153} // namespace xrpl
T adjacent_find(T... args)
T empty(T... args)
T end(T... args)
T erase(T... args)
T find(T... args)
Result splitCommas(FwdIt first, FwdIt last)
Definition rfc2616.h:182
constexpr 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::optional< ProtocolVersion > negotiateProtocolVersion(std::vector< ProtocolVersion > const &versions)
Given a list of supported protocol versions, choose the one we prefer.
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:651
constexpr ProtocolVersion const kSupportedProtocolList[]
The list of protocol versions we speak and we prefer to use.
constexpr ProtocolVersion makeProtocol(std::uint16_t major, std::uint16_t minor)
std::string const & supportedProtocolVersions()
The list of all the protocol versions we support.
std::pair< std::uint16_t, std::uint16_t > ProtocolVersion
Represents a particular version of the peer-to-peer protocol.
T push_back(T... args)
T set_intersection(T... args)
T sort(T... args)
T to_string(T... args)
T unique(T... args)