rippled
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 <ripple/peerfinder/PeerfinderManager.h>
21 #include <ripple/peerfinder/impl/Tuning.h>
22 
23 namespace ripple {
24 namespace 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 
45 void
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 
56  ipLimit += std::min(
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 
65 void
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 
77 Config
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
ripple::PeerFinder::Tuning::defaultMaxPeers
@ defaultMaxPeers
Definition: peerfinder/impl/Tuning.h:63
beast::PropertyStream::Map
Definition: PropertyStream.h:224
ripple::PeerFinder::Config::features
std::string features
The set of features we advertise.
Definition: PeerfinderManager.h:73
ripple::Config::PEER_PRIVATE
bool PEER_PRIVATE
Definition: Config.h:178
ripple::PeerFinder::Config::peerPrivate
bool peerPrivate
true if we want our IP address kept private.
Definition: PeerfinderManager.h:61
ripple::PeerFinder::Config::makeConfig
static Config makeConfig(ripple::Config const &config, std::uint16_t port, bool validationPublicKey, int ipLimit)
Make PeerFinder::Config from configuration parameters.
Definition: PeerfinderConfig.cpp:78
ripple::PeerFinder::Config::wantIncoming
bool wantIncoming
true if we want to accept incoming connections.
Definition: PeerfinderManager.h:64
ripple::Config
Definition: Config.h:89
ripple::Config::standalone
bool standalone() const
Definition: Config.h:332
ripple::PeerFinder::Tuning::outPercent
@ outPercent
Definition: peerfinder/impl/Tuning.h:52
ripple::PeerFinder::Config::outPeers
std::size_t outPeers
The number of automatic outbound connections to maintain.
Definition: PeerfinderManager.h:52
ripple::Config::PEERS_OUT_MAX
std::size_t PEERS_OUT_MAX
Definition: Config.h:185
std::uint16_t
ripple::Config::PEERS_IN_MAX
std::size_t PEERS_IN_MAX
Definition: Config.h:186
ripple::PeerFinder::Tuning::minOutCount
@ minOutCount
Definition: peerfinder/impl/Tuning.h:59
ripple::PeerFinder::Config::maxPeers
std::size_t maxPeers
The largest number of public peer slots to allow.
Definition: PeerfinderManager.h:46
std::min
T min(T... args)
ripple::PeerFinder::Config::applyTuning
void applyTuning()
Adjusts the values so they follow the business rules.
Definition: PeerfinderConfig.cpp:46
ripple::Config::PEERS_MAX
std::size_t PEERS_MAX
Definition: Config.h:184
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::PeerFinder::Config::listeningPort
std::uint16_t listeningPort
The listening port number.
Definition: PeerfinderManager.h:70
ripple::PeerFinder::Config::calcOutPeers
std::size_t calcOutPeers() const
Returns a suitable value for outPeers according to the rules.
Definition: PeerfinderConfig.cpp:38
std::size_t
ripple::PeerFinder::Config::autoConnect
bool autoConnect
true if we want to establish connections automatically
Definition: PeerfinderManager.h:67
ripple::PeerFinder::Config
PeerFinder configuration settings.
Definition: PeerfinderManager.h:40
std::max
T max(T... args)
ripple::PeerFinder::Config::Config
Config()
Create a configuration with default values.
Definition: PeerfinderConfig.cpp:26
ripple::PeerFinder::Config::onWrite
void onWrite(beast::PropertyStream::Map &map)
Write the configuration into a property stream.
Definition: PeerfinderConfig.cpp:66
ripple::PeerFinder::Config::ipLimit
int ipLimit
Limit how many incoming connections we allow per IP.
Definition: PeerfinderManager.h:76
ripple::PeerFinder::Config::inPeers
std::size_t inPeers
The number of automatic inbound connections to maintain.
Definition: PeerfinderManager.h:58