rippled
Loading...
Searching...
No Matches
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 <xrpld/core/Config.h>
24#include <xrpld/peerfinder/Slot.h>
25
26#include <xrpl/beast/clock/abstract_clock.h>
27#include <xrpl/beast/utility/PropertyStream.h>
28
29#include <boost/asio/ip/tcp.hpp>
30
31#include <string_view>
32
33namespace ripple {
34namespace PeerFinder {
35
37
40
41//------------------------------------------------------------------------------
42
44struct Config
45{
51
57
63
65 bool peerPrivate = true;
66
69
72
75
78
81
82 //--------------------------------------------------------------------------
83
85 Config();
86
89 calcOutPeers() const;
90
92 void
94
96 void
98
106 static Config
108 ripple::Config const& config,
109 std::uint16_t port,
110 bool validationPublicKey,
111 int ipLimit);
112
113 friend bool
114 operator==(Config const& lhs, Config const& rhs);
115};
116
117//------------------------------------------------------------------------------
118
121{
122 Endpoint() = default;
123
125
128};
129
130inline bool
131operator<(Endpoint const& lhs, Endpoint const& rhs)
132{
133 return lhs.address < rhs.address;
134}
135
138
139//------------------------------------------------------------------------------
140
142enum class Result {
146 full,
147 success
148};
149
164inline std::string_view
165to_string(Result result) noexcept
166{
167 switch (result)
168 {
169 case Result::inboundDisabled:
170 return "inbound disabled";
171 case Result::duplicatePeer:
172 return "peer already connected";
173 case Result::ipLimitExceeded:
174 return "ip limit exceeded";
175 case Result::full:
176 return "slots full";
177 case Result::success:
178 return "success";
179 }
180
181 return "unknown";
182}
183
186{
187protected:
188 Manager() noexcept;
189
190public:
196 virtual ~Manager() = default;
197
203 virtual void
205
207 virtual void
208 start() = 0;
209
211 virtual void
212 stop() = 0;
213
215 virtual Config
216 config() = 0;
217
223 virtual void
225 std::string const& name,
226 std::vector<beast::IP::Endpoint> const& addresses) = 0;
227
231 virtual void
233 std::string const& name,
234 std::vector<std::string> const& strings) = 0;
235
239 /* VFALCO NOTE Unimplemented
240 virtual void addFallbackURL (std::string const& name,
241 std::string const& url) = 0;
242 */
243
244 //--------------------------------------------------------------------------
245
252 beast::IP::Endpoint const& local_endpoint,
253 beast::IP::Endpoint const& remote_endpoint) = 0;
254
260 new_outbound_slot(beast::IP::Endpoint const& remote_endpoint) = 0;
261
263 virtual void
265 std::shared_ptr<Slot> const& slot,
266 Endpoints const& endpoints) = 0;
267
272 virtual void
274
276 virtual void
278
280 virtual void
282 boost::asio::ip::tcp::endpoint const& remote_address,
284
285 //--------------------------------------------------------------------------
286
294 virtual bool
296 std::shared_ptr<Slot> const& slot,
297 beast::IP::Endpoint const& local_endpoint) = 0;
298
300 virtual Result
302 std::shared_ptr<Slot> const& slot,
303 PublicKey const& key,
304 bool reserved) = 0;
305
309
313
316
320 virtual void
322};
323
324} // namespace PeerFinder
325} // namespace ripple
326
327#endif
A version-independent IP address and port combination.
Definition IPEndpoint.h:38
Subclasses can be called to write to a stream and have children.
std::string const & name() const
Returns the name of this source.
Maintains a set of IP addresses used for getting into the network.
virtual void once_per_second()=0
Perform periodic activity.
virtual void on_endpoints(std::shared_ptr< Slot > const &slot, Endpoints const &endpoints)=0
Called when mtENDPOINTS is received.
virtual std::pair< std::shared_ptr< Slot >, Result > new_outbound_slot(beast::IP::Endpoint const &remote_endpoint)=0
Create a new outbound slot with the specified remote endpoint.
virtual void start()=0
Transition to the started state, synchronously.
virtual void setConfig(Config const &config)=0
Set the configuration for the manager.
virtual void addFixedPeer(std::string const &name, std::vector< beast::IP::Endpoint > const &addresses)=0
Add a peer that should always be connected.
virtual std::vector< beast::IP::Endpoint > autoconnect()=0
Return a set of addresses we should connect to.
virtual ~Manager()=default
Destroy the object.
virtual bool onConnected(std::shared_ptr< Slot > const &slot, beast::IP::Endpoint const &local_endpoint)=0
Called when an outbound connection attempt succeeds.
virtual Config config()=0
Returns the configuration for the manager.
virtual void on_closed(std::shared_ptr< Slot > const &slot)=0
Called when the slot is closed.
virtual void stop()=0
Transition to the stopped state, synchronously.
virtual std::vector< std::pair< std::shared_ptr< Slot >, std::vector< Endpoint > > > buildEndpointsForPeers()=0
virtual std::vector< Endpoint > redirect(std::shared_ptr< Slot > const &slot)=0
Returns a set of endpoints suitable for redirection.
virtual void on_failure(std::shared_ptr< Slot > const &slot)=0
Called when an outbound connection is deemed to have failed.
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.
virtual void addFallbackStrings(std::string const &name, std::vector< std::string > const &strings)=0
Add a set of strings as fallback IP::Endpoint sources.
virtual Result activate(std::shared_ptr< Slot > const &slot, PublicKey const &key, bool reserved)=0
Request an active slot type.
virtual std::pair< std::shared_ptr< Slot >, Result > 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.
A public key.
Definition PublicKey.h:62
bool operator<(Endpoint const &lhs, Endpoint const &rhs)
std::string_view to_string(Result result) noexcept
Converts a Result enum value to its string representation.
Result
Possible results from activating a slot.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
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.
friend bool operator==(Config const &lhs, Config const &rhs)
Describes a connectible peer address along with some metadata.