rippled
Loading...
Searching...
No Matches
PeerfinderConfig.cpp
1#include <xrpld/peerfinder/PeerfinderManager.h>
2#include <xrpld/peerfinder/detail/Tuning.h>
3
4namespace ripple {
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 &&
22 lhs.peerPrivate == rhs.peerPrivate &&
23 lhs.wantIncoming == rhs.wantIncoming && lhs.inPeers == rhs.inPeers &&
24 lhs.maxPeers == rhs.maxPeers && lhs.outPeers == rhs.outPeers &&
25 lhs.features == lhs.features && lhs.ipLimit == rhs.ipLimit &&
27}
28
31{
32 return std::max(
33 (maxPeers * Tuning::outPercent + 50) / 100,
35}
36
37void
39{
40 if (ipLimit == 0)
41 {
42 // Unless a limit is explicitly set, we allow between
43 // 2 and 5 connections from non RFC-1918 "private"
44 // IP addresses.
45 ipLimit = 2;
46
49 5, static_cast<int>(inPeers / Tuning::defaultMaxPeers));
50 }
51
52 // We don't allow a single IP to consume all incoming slots,
53 // unless we only have one incoming slot available.
54 ipLimit = std::max(1, std::min(ipLimit, static_cast<int>(inPeers / 2)));
55}
56
57void
59{
60 map["max_peers"] = maxPeers;
61 map["out_peers"] = outPeers;
62 map["want_incoming"] = wantIncoming;
63 map["auto_connect"] = autoConnect;
64 map["port"] = listeningPort;
65 map["features"] = features;
66 map["ip_limit"] = ipLimit;
67}
68
71 ripple::Config const& cfg,
72 std::uint16_t port,
73 bool validationPublicKey,
74 int ipLimit)
75{
76 PeerFinder::Config config;
77
78 config.peerPrivate = cfg.PEER_PRIVATE;
79
80 // Servers with peer privacy don't want to allow incoming connections
81 config.wantIncoming = (!config.peerPrivate) && (port != 0);
82
83 if (!cfg.PEERS_OUT_MAX && !cfg.PEERS_IN_MAX)
84 {
85 if (cfg.PEERS_MAX != 0)
86 config.maxPeers = cfg.PEERS_MAX;
87
88 if (config.maxPeers < Tuning::minOutCount)
90 config.outPeers = config.calcOutPeers();
91
92 // Calculate the number of outbound peers we want. If we dont want
93 // or can't accept incoming, this will simply be equal to maxPeers.
94 if (!config.wantIncoming)
95 config.outPeers = config.maxPeers;
96
97 // Calculate the largest number of inbound connections we could
98 // take.
99 if (config.maxPeers >= config.outPeers)
100 config.inPeers = config.maxPeers - config.outPeers;
101 else
102 config.inPeers = 0;
103 }
104 else
105 {
106 config.outPeers = cfg.PEERS_OUT_MAX;
107 config.inPeers = cfg.PEERS_IN_MAX;
108 config.maxPeers = 0;
109 }
110
111 // This will cause servers configured as validators to request that
112 // peers they connect to never report their IP address. We set this
113 // after we set the 'wantIncoming' because we want a "soft" version
114 // of peer privacy unless the operator explicitly asks for it.
115 if (validationPublicKey)
116 config.peerPrivate = true;
117
118 // if it's a private peer or we are running as standalone
119 // automatic connections would defeat the purpose.
120 config.autoConnect = !cfg.standalone() && !cfg.PEER_PRIVATE;
121 config.listeningPort = port;
122 config.features = "";
123 config.ipLimit = ipLimit;
124
125 // Enforce business rules
126 config.applyTuning();
127
128 return config;
129}
130
131} // namespace PeerFinder
132} // namespace ripple
std::size_t PEERS_IN_MAX
Definition Config.h:162
bool PEER_PRIVATE
Definition Config.h:154
bool standalone() const
Definition Config.h:317
std::size_t PEERS_OUT_MAX
Definition Config.h:161
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:6
PeerFinder configuration settings.
void onWrite(beast::PropertyStream::Map &map)
Write the configuration into a property stream.
Config()
Create a configuration with default values.
bool autoConnect
true if we want to establish connections automatically
int ipLimit
Limit how many incoming connections we allow per IP.
std::size_t inPeers
The number of automatic inbound connections to maintain.
void applyTuning()
Adjusts the values so they follow the business rules.
static Config makeConfig(ripple::Config const &config, std::uint16_t port, bool validationPublicKey, int ipLimit)
Make PeerFinder::Config from configuration parameters.
std::size_t maxPeers
The largest number of public peer slots to allow.
std::string features
The set of features we advertise.
bool wantIncoming
true if we want to accept incoming connections.
std::size_t outPeers
The number of automatic outbound connections to maintain.
std::size_t calcOutPeers() const
Returns a suitable value for outPeers according to the rules.
std::uint16_t listeningPort
The listening port number.
bool peerPrivate
true if we want our IP address kept private.