xrpld
Loading...
Searching...
No Matches
libxrpl/peerfinder/Config.cpp
1#include <xrpl/peerfinder/Config.h>
2
3#include <xrpl/beast/utility/PropertyStream.h>
4
5#include <algorithm>
6#include <cstddef>
7#include <cstdint>
8#include <stdexcept>
9
10namespace xrpl::peer_finder {
11
12std::size_t
18
19void
21{
22 if (ipLimit == 0)
23 {
24 // Unless a limit is explicitly set, we allow between
25 // 2 and 5 connections from non RFC-1918 "private"
26 // IP addresses.
27 ipLimit = 2;
28
30 ipLimit += std::min(5, static_cast<int>(inPeers / tuning::kDefaultMaxPeers));
31 }
32
33 // We don't allow a single IP to consume all incoming slots,
34 // unless we only have one incoming slot available.
35 ipLimit = std::max(1, std::min(ipLimit, static_cast<int>(inPeers / 2)));
36}
37
38void
40{
41 map["max_peers"] = maxPeers;
42 map["out_peers"] = outPeers;
43 map["want_incoming"] = wantIncoming;
44 map["auto_connect"] = autoConnect;
45 map["port"] = listeningPort;
46 map["features"] = features;
47 map["ip_limit"] = ipLimit;
48 map["verify_endpoints"] = verifyEndpoints;
49}
50
53 bool peerPrivate,
54 bool standalone,
55 PeerLimitConfig const& limits,
56 std::uint16_t port,
57 bool validationPublicKey,
58 int ipLimit,
59 bool verifyEndpoints)
60{
62
63 if (!limits.maxPeers)
64 {
65 if (limits.inPeers && !limits.outPeers)
66 throw std::runtime_error("Both inbound and outbound peer limits must be configured");
67
68 if (limits.outPeers && !limits.inPeers)
69 throw std::runtime_error("Both inbound and outbound peer limits must be configured");
70
71 if (limits.inPeers && *limits.inPeers > 1000)
72 throw std::runtime_error("Inbound peer limit must be less than or equal to 1000");
73
74 if (limits.outPeers && (*limits.outPeers < 10 || *limits.outPeers > 1000))
75 throw std::runtime_error("Outbound peer limit must be in the range 10-1000");
76 }
77
78 config.peerPrivate = peerPrivate;
79
80 // Servers with peer privacy don't want to allow incoming connections
81 config.wantIncoming = (!config.peerPrivate) && (port != 0);
82
83 if (limits.maxPeers || (!limits.inPeers && !limits.outPeers))
84 {
85 if (limits.maxPeers && *limits.maxPeers != 0)
86 config.maxPeers = *limits.maxPeers;
87
89 config.outPeers = config.calcOutPeers();
90
91 // Calculate the number of outbound peers we want. If we dont want
92 // or can't accept incoming, this will simply be equal to maxPeers.
93 if (!config.wantIncoming)
94 config.outPeers = config.maxPeers;
95
96 // Calculate the largest number of inbound connections we could
97 // take.
98 if (config.maxPeers >= config.outPeers)
99 {
100 config.inPeers = config.maxPeers - config.outPeers;
101 }
102 else
103 {
104 config.inPeers = 0;
105 }
106 }
107 else
108 {
109 config.outPeers = *limits.outPeers;
110 config.inPeers = *limits.inPeers;
111 config.maxPeers = 0;
112 }
113
114 // This will cause servers configured as validators to request that
115 // peers they connect to never report their IP address. We set this
116 // after we set the 'wantIncoming' because we want a "soft" version
117 // of peer privacy unless the operator explicitly asks for it.
118 if (validationPublicKey)
119 config.peerPrivate = true;
120
121 // if it's a private peer or we are running as standalone
122 // automatic connections would defeat the purpose.
123 config.autoConnect = !standalone && !peerPrivate;
124 config.listeningPort = port;
125 config.features = "";
126 config.ipLimit = ipLimit;
128
129 // Enforce business rules
130 config.applyTuning();
131
132 return config;
133}
134
135} // namespace xrpl::peer_finder
T max(T... args)
T min(T... args)
static constexpr auto kOutPercent
The percentage of total peer slots that are outbound.
static constexpr auto kDefaultMaxPeers
The default value of Config::maxPeers.
static constexpr auto kMinOutCount
A hard minimum on the number of outgoing connections.
PeerFinder configuration settings.
std::size_t outPeers
The number of automatic outbound connections to maintain.
void onWrite(beast::PropertyStream::Map &map) const
Write the configuration into a property stream.
bool wantIncoming
true if we want to accept incoming connections.
bool peerPrivate
true if we want our IP address kept private.
std::uint16_t listeningPort
The listening port number.
bool autoConnect
true if we want to establish connections automatically
void applyTuning()
Adjusts the values so they follow the business rules.
static Config makeConfig(bool peerPrivate, bool standalone, PeerLimitConfig const &limits, std::uint16_t port, bool validationPublicKey, int ipLimit, bool verifyEndpoints)
Make peer_finder::Config from peer limit and server mode parameters.
std::size_t inPeers
The number of automatic inbound connections to maintain.
std::size_t maxPeers
The largest number of public peer slots to allow.
int ipLimit
Limit how many incoming connections we allow per IP.
std::size_t calcOutPeers() const
Returns a suitable value for outPeers according to the rules.
bool verifyEndpoints
true if we want to verify endpoints in TMEndpoints messages
std::string features
The set of features we advertise.