rippled
Loading...
Searching...
No Matches
PeerfinderConfig.cpp
1#include <xrpld/peerfinder/PeerfinderManager.h>
2#include <xrpld/peerfinder/detail/Tuning.h>
3
4#include <algorithm>
5
6namespace xrpl {
7namespace PeerFinder {
8
9Config::Config() : outPeers(calcOutPeers())
10
11{
12}
13
14bool
15operator==(Config const& lhs, Config const& rhs)
16{
17 return lhs.autoConnect == rhs.autoConnect && lhs.peerPrivate == rhs.peerPrivate &&
18 lhs.wantIncoming == rhs.wantIncoming && lhs.inPeers == rhs.inPeers &&
19 lhs.maxPeers == rhs.maxPeers && lhs.outPeers == rhs.outPeers &&
20 lhs.features == rhs.features && lhs.ipLimit == rhs.ipLimit &&
22}
23
29
30void
32{
33 if (ipLimit == 0)
34 {
35 // Unless a limit is explicitly set, we allow between
36 // 2 and 5 connections from non RFC-1918 "private"
37 // IP addresses.
38 ipLimit = 2;
39
41 ipLimit += std::min(5, static_cast<int>(inPeers / Tuning::defaultMaxPeers));
42 }
43
44 // We don't allow a single IP to consume all incoming slots,
45 // unless we only have one incoming slot available.
46 ipLimit = std::max(1, std::min(ipLimit, static_cast<int>(inPeers / 2)));
47}
48
49void
51{
52 map["max_peers"] = maxPeers;
53 map["out_peers"] = outPeers;
54 map["want_incoming"] = wantIncoming;
55 map["auto_connect"] = autoConnect;
56 map["port"] = listeningPort;
57 map["features"] = features;
58 map["ip_limit"] = ipLimit;
59}
60
63 xrpl::Config const& cfg,
64 std::uint16_t port,
65 bool validationPublicKey,
66 int ipLimit)
67{
68 PeerFinder::Config config;
69
70 config.peerPrivate = cfg.PEER_PRIVATE;
71
72 // Servers with peer privacy don't want to allow incoming connections
73 config.wantIncoming = (!config.peerPrivate) && (port != 0);
74
75 if ((cfg.PEERS_OUT_MAX == 0u) && (cfg.PEERS_IN_MAX == 0u))
76 {
77 if (cfg.PEERS_MAX != 0)
78 config.maxPeers = cfg.PEERS_MAX;
79
81 config.outPeers = config.calcOutPeers();
82
83 // Calculate the number of outbound peers we want. If we dont want
84 // or can't accept incoming, this will simply be equal to maxPeers.
85 if (!config.wantIncoming)
86 config.outPeers = config.maxPeers;
87
88 // Calculate the largest number of inbound connections we could
89 // take.
90 if (config.maxPeers >= config.outPeers)
91 {
92 config.inPeers = config.maxPeers - config.outPeers;
93 }
94 else
95 {
96 config.inPeers = 0;
97 }
98 }
99 else
100 {
101 config.outPeers = cfg.PEERS_OUT_MAX;
102 config.inPeers = cfg.PEERS_IN_MAX;
103 config.maxPeers = 0;
104 }
105
106 // This will cause servers configured as validators to request that
107 // peers they connect to never report their IP address. We set this
108 // after we set the 'wantIncoming' because we want a "soft" version
109 // of peer privacy unless the operator explicitly asks for it.
110 if (validationPublicKey)
111 config.peerPrivate = true;
112
113 // if it's a private peer or we are running as standalone
114 // automatic connections would defeat the purpose.
115 config.autoConnect = !cfg.standalone() && !cfg.PEER_PRIVATE;
116 config.listeningPort = port;
117 config.features = "";
118 config.ipLimit = ipLimit;
119
120 // Enforce business rules
121 config.applyTuning();
122
123 return config;
124}
125
126} // namespace PeerFinder
127} // namespace xrpl
std::size_t PEERS_IN_MAX
Definition Config.h:167
bool standalone() const
Definition Config.h:316
std::size_t PEERS_OUT_MAX
Definition Config.h:166
bool PEER_PRIVATE
Definition Config.h:159
std::size_t PEERS_MAX
Definition Config.h:165
T is_same_v
T max(T... args)
T min(T... args)
bool operator==(Endpoint const &a, Endpoint const &b)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
PeerFinder configuration settings.
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 outPeers
The number of automatic outbound connections to maintain.
Config()
Create a configuration with default values.
void applyTuning()
Adjusts the values so they follow the business rules.
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 autoConnect
true if we want to establish connections automatically
std::string features
The set of features we advertise.
std::size_t inPeers
The number of automatic inbound connections to maintain.
static Config makeConfig(xrpl::Config const &config, std::uint16_t port, bool validationPublicKey, int ipLimit)
Make PeerFinder::Config from configuration parameters.
std::uint16_t listeningPort
The listening port number.
std::size_t calcOutPeers() const
Returns a suitable value for outPeers according to the rules.
bool peerPrivate
true if we want our IP address kept private.