rippled
Loading...
Searching...
No Matches
PeerfinderConfig.cpp
1#include <xrpld/peerfinder/PeerfinderManager.h>
2#include <xrpld/peerfinder/detail/Tuning.h>
3
4namespace xrpl {
5namespace PeerFinder {
6
8 : maxPeers(Tuning::defaultMaxPeers)
9 , outPeers(calcOutPeers())
10 , inPeers(0)
11 , wantIncoming(true)
12 , autoConnect(true)
13 , listeningPort(0)
14 , ipLimit(0)
15{
16}
17
18bool
19operator==(Config const& lhs, Config const& rhs)
20{
21 return lhs.autoConnect == rhs.autoConnect && lhs.peerPrivate == rhs.peerPrivate &&
22 lhs.wantIncoming == rhs.wantIncoming && lhs.inPeers == rhs.inPeers && lhs.maxPeers == rhs.maxPeers &&
23 lhs.outPeers == rhs.outPeers && lhs.features == lhs.features && lhs.ipLimit == rhs.ipLimit &&
25}
26
32
33void
35{
36 if (ipLimit == 0)
37 {
38 // Unless a limit is explicitly set, we allow between
39 // 2 and 5 connections from non RFC-1918 "private"
40 // IP addresses.
41 ipLimit = 2;
42
44 ipLimit += std::min(5, static_cast<int>(inPeers / Tuning::defaultMaxPeers));
45 }
46
47 // We don't allow a single IP to consume all incoming slots,
48 // unless we only have one incoming slot available.
49 ipLimit = std::max(1, std::min(ipLimit, static_cast<int>(inPeers / 2)));
50}
51
52void
54{
55 map["max_peers"] = maxPeers;
56 map["out_peers"] = outPeers;
57 map["want_incoming"] = wantIncoming;
58 map["auto_connect"] = autoConnect;
59 map["port"] = listeningPort;
60 map["features"] = features;
61 map["ip_limit"] = ipLimit;
62}
63
65Config::makeConfig(xrpl::Config const& cfg, std::uint16_t port, bool validationPublicKey, int ipLimit)
66{
67 PeerFinder::Config config;
68
69 config.peerPrivate = cfg.PEER_PRIVATE;
70
71 // Servers with peer privacy don't want to allow incoming connections
72 config.wantIncoming = (!config.peerPrivate) && (port != 0);
73
74 if (!cfg.PEERS_OUT_MAX && !cfg.PEERS_IN_MAX)
75 {
76 if (cfg.PEERS_MAX != 0)
77 config.maxPeers = cfg.PEERS_MAX;
78
79 if (config.maxPeers < Tuning::minOutCount)
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 config.inPeers = config.maxPeers - config.outPeers;
92 else
93 config.inPeers = 0;
94 }
95 else
96 {
97 config.outPeers = cfg.PEERS_OUT_MAX;
98 config.inPeers = cfg.PEERS_IN_MAX;
99 config.maxPeers = 0;
100 }
101
102 // This will cause servers configured as validators to request that
103 // peers they connect to never report their IP address. We set this
104 // after we set the 'wantIncoming' because we want a "soft" version
105 // of peer privacy unless the operator explicitly asks for it.
106 if (validationPublicKey)
107 config.peerPrivate = true;
108
109 // if it's a private peer or we are running as standalone
110 // automatic connections would defeat the purpose.
111 config.autoConnect = !cfg.standalone() && !cfg.PEER_PRIVATE;
112 config.listeningPort = port;
113 config.features = "";
114 config.ipLimit = ipLimit;
115
116 // Enforce business rules
117 config.applyTuning();
118
119 return config;
120}
121
122} // namespace PeerFinder
123} // namespace xrpl
std::size_t PEERS_IN_MAX
Definition Config.h:162
bool standalone() const
Definition Config.h:311
std::size_t PEERS_OUT_MAX
Definition Config.h:161
bool PEER_PRIVATE
Definition Config.h:154
std::size_t PEERS_MAX
Definition Config.h:160
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.
void onWrite(beast::PropertyStream::Map &map)
Write the configuration into a property stream.
Config()
Create a configuration with default values.
void applyTuning()
Adjusts the values so they follow the business rules.
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.