rippled
Loading...
Searching...
No Matches
PeerfinderConfig.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <xrpld/peerfinder/PeerfinderManager.h>
21#include <xrpld/peerfinder/detail/Tuning.h>
22
23namespace ripple {
24namespace PeerFinder {
25
27 : maxPeers(Tuning::defaultMaxPeers)
28 , outPeers(calcOutPeers())
29 , inPeers(0)
30 , wantIncoming(true)
31 , autoConnect(true)
32 , listeningPort(0)
33 , ipLimit(0)
34{
35}
36
39{
40 return std::max(
41 (maxPeers * Tuning::outPercent + 50) / 100,
43}
44
45void
47{
48 if (ipLimit == 0)
49 {
50 // Unless a limit is explicitly set, we allow between
51 // 2 and 5 connections from non RFC-1918 "private"
52 // IP addresses.
53 ipLimit = 2;
54
57 5, static_cast<int>(inPeers / Tuning::defaultMaxPeers));
58 }
59
60 // We don't allow a single IP to consume all incoming slots,
61 // unless we only have one incoming slot available.
62 ipLimit = std::max(1, std::min(ipLimit, static_cast<int>(inPeers / 2)));
63}
64
65void
67{
68 map["max_peers"] = maxPeers;
69 map["out_peers"] = outPeers;
70 map["want_incoming"] = wantIncoming;
71 map["auto_connect"] = autoConnect;
72 map["port"] = listeningPort;
73 map["features"] = features;
74 map["ip_limit"] = ipLimit;
75}
76
79 ripple::Config const& cfg,
80 std::uint16_t port,
81 bool validationPublicKey,
82 int ipLimit)
83{
84 PeerFinder::Config config;
85
86 config.peerPrivate = cfg.PEER_PRIVATE;
87
88 // Servers with peer privacy don't want to allow incoming connections
89 config.wantIncoming = (!config.peerPrivate) && (port != 0);
90
91 if (!cfg.PEERS_OUT_MAX && !cfg.PEERS_IN_MAX)
92 {
93 if (cfg.PEERS_MAX != 0)
94 config.maxPeers = cfg.PEERS_MAX;
95
96 if (config.maxPeers < Tuning::minOutCount)
98 config.outPeers = config.calcOutPeers();
99
100 // Calculate the number of outbound peers we want. If we dont want
101 // or can't accept incoming, this will simply be equal to maxPeers.
102 if (!config.wantIncoming)
103 config.outPeers = config.maxPeers;
104
105 // Calculate the largest number of inbound connections we could
106 // take.
107 if (config.maxPeers >= config.outPeers)
108 config.inPeers = config.maxPeers - config.outPeers;
109 else
110 config.inPeers = 0;
111 }
112 else
113 {
114 config.outPeers = cfg.PEERS_OUT_MAX;
115 config.inPeers = cfg.PEERS_IN_MAX;
116 config.maxPeers = 0;
117 }
118
119 // This will cause servers configured as validators to request that
120 // peers they connect to never report their IP address. We set this
121 // after we set the 'wantIncoming' because we want a "soft" version
122 // of peer privacy unless the operator explicitly asks for it.
123 if (validationPublicKey)
124 config.peerPrivate = true;
125
126 // if it's a private peer or we are running as standalone
127 // automatic connections would defeat the purpose.
128 config.autoConnect = !cfg.standalone() && !cfg.PEER_PRIVATE;
129 config.listeningPort = port;
130 config.features = "";
131 config.ipLimit = ipLimit;
132
133 // Enforce business rules
134 config.applyTuning();
135
136 return config;
137}
138
139} // namespace PeerFinder
140} // namespace ripple
std::size_t PEERS_IN_MAX
Definition: Config.h:181
bool PEER_PRIVATE
Definition: Config.h:173
bool standalone() const
Definition: Config.h:337
std::size_t PEERS_OUT_MAX
Definition: Config.h:180
std::size_t PEERS_MAX
Definition: Config.h:179
T max(T... args)
T min(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
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.