rippled
PeerfinderManager.h
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 #ifndef RIPPLE_PEERFINDER_MANAGER_H_INCLUDED
21 #define RIPPLE_PEERFINDER_MANAGER_H_INCLUDED
22 
23 #include <ripple/beast/clock/abstract_clock.h>
24 #include <ripple/beast/utility/PropertyStream.h>
25 #include <ripple/core/Config.h>
26 #include <ripple/peerfinder/Slot.h>
27 #include <boost/asio/ip/tcp.hpp>
28 
29 namespace ripple {
30 namespace PeerFinder {
31 
33 
36 
37 //------------------------------------------------------------------------------
38 
40 struct Config
41 {
47 
53 
59 
61  bool peerPrivate = true;
62 
65 
68 
71 
74 
76  int ipLimit;
77 
78  //--------------------------------------------------------------------------
79 
81  Config();
82 
85  calcOutPeers() const;
86 
88  void
89  applyTuning();
90 
92  void
94 
102  static Config
103  makeConfig(
104  ripple::Config const& config,
105  std::uint16_t port,
106  bool validationPublicKey,
107  int ipLimit);
108 };
109 
110 //------------------------------------------------------------------------------
111 
113 struct Endpoint
114 {
115  Endpoint() = default;
116 
117  Endpoint(beast::IP::Endpoint const& ep, std::uint32_t hops_);
118 
121 };
122 
123 inline bool
124 operator<(Endpoint const& lhs, Endpoint const& rhs)
125 {
126  return lhs.address < rhs.address;
127 }
128 
131 
132 //------------------------------------------------------------------------------
133 
135 enum class Result { duplicate, full, success };
136 
139 {
140 protected:
141  Manager() noexcept;
142 
143 public:
149  virtual ~Manager() = default;
150 
156  virtual void
157  setConfig(Config const& config) = 0;
158 
160  virtual void
161  start() = 0;
162 
164  virtual void
165  stop() = 0;
166 
168  virtual Config
169  config() = 0;
170 
176  virtual void
177  addFixedPeer(
178  std::string const& name,
179  std::vector<beast::IP::Endpoint> const& addresses) = 0;
180 
184  virtual void
186  std::string const& name,
187  std::vector<std::string> const& strings) = 0;
188 
192  /* VFALCO NOTE Unimplemented
193  virtual void addFallbackURL (std::string const& name,
194  std::string const& url) = 0;
195  */
196 
197  //--------------------------------------------------------------------------
198 
203  virtual std::shared_ptr<Slot>
205  beast::IP::Endpoint const& local_endpoint,
206  beast::IP::Endpoint const& remote_endpoint) = 0;
207 
212  virtual std::shared_ptr<Slot>
213  new_outbound_slot(beast::IP::Endpoint const& remote_endpoint) = 0;
214 
216  virtual void
217  on_endpoints(
218  std::shared_ptr<Slot> const& slot,
219  Endpoints const& endpoints) = 0;
220 
225  virtual void
226  on_closed(std::shared_ptr<Slot> const& slot) = 0;
227 
229  virtual void
230  on_failure(std::shared_ptr<Slot> const& slot) = 0;
231 
233  virtual void
234  onRedirects(
235  boost::asio::ip::tcp::endpoint const& remote_address,
237 
238  //--------------------------------------------------------------------------
239 
247  virtual bool
248  onConnected(
249  std::shared_ptr<Slot> const& slot,
250  beast::IP::Endpoint const& local_endpoint) = 0;
251 
253  virtual Result
254  activate(
255  std::shared_ptr<Slot> const& slot,
256  PublicKey const& key,
257  bool reserved) = 0;
258 
260  virtual std::vector<Endpoint>
261  redirect(std::shared_ptr<Slot> const& slot) = 0;
262 
265  autoconnect() = 0;
266 
269 
273  virtual void
274  once_per_second() = 0;
275 };
276 
277 } // namespace PeerFinder
278 } // namespace ripple
279 
280 #endif
beast::PropertyStream::Source::name
std::string const & name() const
Returns the name of this source.
Definition: beast_PropertyStream.cpp:190
ripple::PeerFinder::Result::duplicate
@ duplicate
std::string
STL class.
std::shared_ptr
STL class.
beast::PropertyStream::Map
Definition: PropertyStream.h:224
ripple::PeerFinder::Manager::once_per_second
virtual void once_per_second()=0
Perform periodic activity.
ripple::PeerFinder::Manager::addFallbackStrings
virtual void addFallbackStrings(std::string const &name, std::vector< std::string > const &strings)=0
Add a set of strings as fallback IP::Endpoint sources.
ripple::PeerFinder::Manager::buildEndpointsForPeers
virtual std::vector< std::pair< std::shared_ptr< Slot >, std::vector< Endpoint > > > buildEndpointsForPeers()=0
beast::PropertyStream::Source
Subclasses can be called to write to a stream and have children.
Definition: PropertyStream.h:330
ripple::PeerFinder::Endpoint::Endpoint
Endpoint()=default
std::vector< beast::IP::Endpoint >
ripple::PeerFinder::Config::features
std::string features
The set of features we advertise.
Definition: PeerfinderManager.h:73
ripple::PeerFinder::operator<
bool operator<(Endpoint const &lhs, Endpoint const &rhs)
Definition: PeerfinderManager.h:124
ripple::PeerFinder::Manager::new_outbound_slot
virtual std::shared_ptr< Slot > new_outbound_slot(beast::IP::Endpoint const &remote_endpoint)=0
Create a new outbound slot with the specified remote endpoint.
ripple::PeerFinder::Manager::new_inbound_slot
virtual std::shared_ptr< Slot > new_inbound_slot(beast::IP::Endpoint const &local_endpoint, beast::IP::Endpoint const &remote_endpoint)=0
Add a URL as a fallback location to obtain IP::Endpoint sources.
ripple::PeerFinder::Config::peerPrivate
bool peerPrivate
true if we want our IP address kept private.
Definition: PeerfinderManager.h:61
ripple::PeerFinder::Manager::autoconnect
virtual std::vector< beast::IP::Endpoint > autoconnect()=0
Return a set of addresses we should connect to.
ripple::PeerFinder::Manager::on_endpoints
virtual void on_endpoints(std::shared_ptr< Slot > const &slot, Endpoints const &endpoints)=0
Called when mtENDPOINTS is received.
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::Manager::activate
virtual Result activate(std::shared_ptr< Slot > const &slot, PublicKey const &key, bool reserved)=0
Request an active slot type.
ripple::PeerFinder::Manager::redirect
virtual std::vector< Endpoint > redirect(std::shared_ptr< Slot > const &slot)=0
Returns a set of endpoints suitable for redirection.
ripple::PeerFinder::Manager::on_closed
virtual void on_closed(std::shared_ptr< Slot > const &slot)=0
Called when the slot is closed.
ripple::PeerFinder::Result::success
@ success
ripple::PeerFinder::Manager::stop
virtual void stop()=0
Transition to the stopped state, synchronously.
ripple::PeerFinder::Config::wantIncoming
bool wantIncoming
true if we want to accept incoming connections.
Definition: PeerfinderManager.h:64
ripple::PublicKey
A public key.
Definition: PublicKey.h:59
ripple::Config
Definition: Config.h:89
ripple::PeerFinder::Manager::setConfig
virtual void setConfig(Config const &config)=0
Set the configuration for the manager.
ripple::PeerFinder::Manager::start
virtual void start()=0
Transition to the started state, synchronously.
ripple::PeerFinder::Endpoint::hops
std::uint32_t hops
Definition: PeerfinderManager.h:119
ripple::PeerFinder::Config::outPeers
std::size_t outPeers
The number of automatic outbound connections to maintain.
Definition: PeerfinderManager.h:52
std::uint16_t
beast::abstract_clock
Abstract interface to a clock.
Definition: abstract_clock.h:57
ripple::PeerFinder::Config::maxPeers
std::size_t maxPeers
The largest number of public peer slots to allow.
Definition: PeerfinderManager.h:46
ripple::PeerFinder::Config::applyTuning
void applyTuning()
Adjusts the values so they follow the business rules.
Definition: PeerfinderConfig.cpp:46
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::PeerFinder::Manager::onConnected
virtual bool onConnected(std::shared_ptr< Slot > const &slot, beast::IP::Endpoint const &local_endpoint)=0
Called when an outbound connection attempt succeeds.
ripple::PeerFinder::Manager::~Manager
virtual ~Manager()=default
Destroy the object.
ripple::PeerFinder::Manager::onRedirects
virtual void onRedirects(boost::asio::ip::tcp::endpoint const &remote_address, std::vector< boost::asio::ip::tcp::endpoint > const &eps)=0
Called when we received redirect IPs from a busy peer.
ripple::PeerFinder::Manager::addFixedPeer
virtual void addFixedPeer(std::string const &name, std::vector< beast::IP::Endpoint > const &addresses)=0
Add a peer that should always be connected.
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
ripple::PeerFinder::Manager::config
virtual Config config()=0
Returns the configuration for the manager.
std::size_t
beast::IP::Endpoint
A version-independent IP address and port combination.
Definition: IPEndpoint.h:38
ripple::PeerFinder::Manager::on_failure
virtual void on_failure(std::shared_ptr< Slot > const &slot)=0
Called when an outbound connection is deemed to have failed.
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
ripple::PeerFinder::Endpoint
Describes a connectible peer address along with some metadata.
Definition: PeerfinderManager.h:113
ripple::PeerFinder::Config::Config
Config()
Create a configuration with default values.
Definition: PeerfinderConfig.cpp:26
ripple::PeerFinder::Result::full
@ full
ripple::PeerFinder::Config::onWrite
void onWrite(beast::PropertyStream::Map &map)
Write the configuration into a property stream.
Definition: PeerfinderConfig.cpp:66
ripple::PeerFinder::Endpoint::address
beast::IP::Endpoint address
Definition: PeerfinderManager.h:120
ripple::PeerFinder::Config::ipLimit
int ipLimit
Limit how many incoming connections we allow per IP.
Definition: PeerfinderManager.h:76
ripple::PeerFinder::Manager::Manager
Manager() noexcept
Definition: PeerfinderManager.cpp:268
ripple::PeerFinder::Manager
Maintains a set of IP addresses used for getting into the network.
Definition: PeerfinderManager.h:138
ripple::PeerFinder::Config::inPeers
std::size_t inPeers
The number of automatic inbound connections to maintain.
Definition: PeerfinderManager.h:58
ripple::PeerFinder::Result
Result
Possible results from activating a slot.
Definition: PeerfinderManager.h:135