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  , wantIncoming(true)
30  , autoConnect(true)
31  , listeningPort(0)
32  , ipLimit(0)
33 {
34 }
35 
36 double
38 {
39  return std::max(
41 }
42 
43 void
45 {
49 
50  auto const inPeers = maxPeers - outPeers;
51 
52  if (ipLimit == 0)
53  {
54  // Unless a limit is explicitly set, we allow between
55  // 2 and 5 connections from non RFC-1918 "private"
56  // IP addresses.
57  ipLimit = 2;
58 
59  if (inPeers > Tuning::defaultMaxPeers)
60  ipLimit += std::min(
61  5, static_cast<int>(inPeers / Tuning::defaultMaxPeers));
62  }
63 
64  // We don't allow a single IP to consume all incoming slots,
65  // unless we only have one incoming slot available.
66  ipLimit = std::max(1, std::min(ipLimit, static_cast<int>(inPeers / 2)));
67 }
68 
69 void
71 {
72  map["max_peers"] = maxPeers;
73  map["out_peers"] = outPeers;
74  map["want_incoming"] = wantIncoming;
75  map["auto_connect"] = autoConnect;
76  map["port"] = listeningPort;
77  map["features"] = features;
78  map["ip_limit"] = ipLimit;
79 }
80 
81 } // namespace PeerFinder
82 } // namespace ripple
beast::PropertyStream::Map
Definition: PropertyStream.h:236
ripple::PeerFinder::Tuning::outPercent
@ outPercent
Definition: peerfinder/impl/Tuning.h:52
ripple::PeerFinder::Config::features
std::string features
The set of features we advertise.
Definition: PeerfinderManager.h:73
ripple::PeerFinder::Tuning::minOutCount
@ minOutCount
Definition: peerfinder/impl/Tuning.h:59
ripple::PeerFinder::Config::wantIncoming
bool wantIncoming
true if we want to accept incoming connections.
Definition: PeerfinderManager.h:64
ripple::PeerFinder::Config::outPeers
double outPeers
The number of automatic outbound connections to maintain.
Definition: PeerfinderManager.h:58
ripple::PeerFinder::Config::maxPeers
int 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:44
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
double calcOutPeers() const
Returns a suitable value for outPeers according to the rules.
Definition: PeerfinderConfig.cpp:37
ripple::PeerFinder::Tuning::defaultMaxPeers
@ defaultMaxPeers
Definition: peerfinder/impl/Tuning.h:63
ripple::PeerFinder::Config::autoConnect
bool autoConnect
true if we want to establish connections automatically
Definition: PeerfinderManager.h:67
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:70
ripple::PeerFinder::Config::ipLimit
int ipLimit
Limit how many incoming connections we allow per IP.
Definition: PeerfinderManager.h:76