rippled
SlotImp.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_SLOTIMP_H_INCLUDED
21 #define RIPPLE_PEERFINDER_SLOTIMP_H_INCLUDED
22 
23 #include <ripple/beast/container/aged_container_utility.h>
24 #include <ripple/beast/container/aged_unordered_map.h>
25 #include <ripple/peerfinder/PeerfinderManager.h>
26 #include <ripple/peerfinder/Slot.h>
27 #include <atomic>
28 #include <optional>
29 
30 namespace ripple {
31 namespace PeerFinder {
32 
33 class SlotImp : public Slot
34 {
35 public:
37 
38  // inbound
39  SlotImp(
42  bool fixed,
43  clock_type& clock);
44 
45  // outbound
46  SlotImp(
48  bool fixed,
49  clock_type& clock);
50 
51  bool
52  inbound() const override
53  {
54  return m_inbound;
55  }
56 
57  bool
58  fixed() const override
59  {
60  return m_fixed;
61  }
62 
63  bool
64  reserved() const override
65  {
66  return m_reserved;
67  }
68 
69  State
70  state() const override
71  {
72  return m_state;
73  }
74 
75  beast::IP::Endpoint const&
76  remote_endpoint() const override
77  {
78  return m_remote_endpoint;
79  }
80 
82  local_endpoint() const override
83  {
84  return m_local_endpoint;
85  }
86 
88  public_key() const override
89  {
90  return m_public_key;
91  }
92 
94  listening_port() const override
95  {
96  std::uint32_t const value = m_listening_port;
97  if (value == unknownPort)
98  return std::nullopt;
99  return value;
100  }
101 
102  void
104  {
105  m_listening_port = port;
106  }
107 
108  void
110  {
111  m_local_endpoint = endpoint;
112  }
113 
114  void
116  {
117  m_remote_endpoint = endpoint;
118  }
119 
120  void
121  public_key(PublicKey const& key)
122  {
123  m_public_key = key;
124  }
125 
126  void
127  reserved(bool reserved_)
128  {
129  m_reserved = reserved_;
130  }
131 
132  //--------------------------------------------------------------------------
133 
134  void
135  state(State state_);
136 
137  void
138  activate(clock_type::time_point const& now);
139 
140  // "Memberspace"
141  //
142  // The set of all recent addresses that we have seen from this peer.
143  // We try to avoid sending a peer the same addresses they gave us.
144  //
145  class recent_t
146  {
147  public:
148  explicit recent_t(clock_type& clock);
149 
154  void
155  insert(beast::IP::Endpoint const& ep, std::uint32_t hops);
156 
158  bool
159  filter(beast::IP::Endpoint const& ep, std::uint32_t hops);
160 
161  private:
162  void
163  expire();
164 
165  friend class SlotImp;
167  } recent;
168 
169  void
171  {
172  recent.expire();
173  }
174 
175 private:
176  bool const m_inbound;
177  bool const m_fixed;
183 
184  static std::int32_t constexpr unknownPort = -1;
186 
187 public:
188  // DEPRECATED public data members
189 
190  // Tells us if we checked the connection. Outbound connections
191  // are always considered checked since we successfuly connected.
192  bool checked;
193 
194  // Set to indicate if the connection can receive incoming at the
195  // address advertised in mtENDPOINTS. Only valid if checked is true.
196  bool canAccept;
197 
198  // Set to indicate that a connection check for this peer is in
199  // progress. Valid always.
201 
202  // The time after which we will accept mtENDPOINTS from the peer
203  // This is to prevent flooding or spamming. Receipt of mtENDPOINTS
204  // sooner than the allotted time should impose a load charge.
205  //
207 };
208 
209 } // namespace PeerFinder
210 } // namespace ripple
211 
212 #endif
ripple::PeerFinder::SlotImp::m_reserved
bool m_reserved
Definition: SlotImp.h:178
ripple::PeerFinder::SlotImp::inbound
bool inbound() const override
Returns true if this is an inbound connection.
Definition: SlotImp.h:52
std::shared_ptr< SlotImp >
ripple::PeerFinder::SlotImp::expire
void expire()
Definition: SlotImp.h:170
ripple::PeerFinder::SlotImp::recent_t::expire
void expire()
Definition: SlotImp.cpp:132
ripple::PeerFinder::SlotImp::checked
bool checked
Definition: SlotImp.h:192
ripple::PeerFinder::SlotImp::whenAcceptEndpoints
clock_type::time_point whenAcceptEndpoints
Definition: SlotImp.h:206
ripple::PeerFinder::SlotImp::m_remote_endpoint
beast::IP::Endpoint m_remote_endpoint
Definition: SlotImp.h:180
ripple::PeerFinder::SlotImp::public_key
std::optional< PublicKey > const & public_key() const override
The peer's public key, when known.
Definition: SlotImp.h:88
ripple::PeerFinder::SlotImp::recent_t::filter
bool filter(beast::IP::Endpoint const &ep, std::uint32_t hops)
Returns true if we should not send endpoint to the slot.
Definition: SlotImp.cpp:120
ripple::PeerFinder::SlotImp::recent_t
Definition: SlotImp.h:145
ripple::PeerFinder::SlotImp::m_fixed
const bool m_fixed
Definition: SlotImp.h:177
ripple::PeerFinder::SlotImp::remote_endpoint
void remote_endpoint(beast::IP::Endpoint const &endpoint)
Definition: SlotImp.h:115
ripple::PeerFinder::SlotImp::m_local_endpoint
std::optional< beast::IP::Endpoint > m_local_endpoint
Definition: SlotImp.h:181
ripple::PeerFinder::SlotImp::m_state
State m_state
Definition: SlotImp.h:179
ripple::PeerFinder::SlotImp::recent_t::recent_t
recent_t(clock_type &clock)
Definition: SlotImp.cpp:100
ripple::PublicKey
A public key.
Definition: PublicKey.h:59
ripple::PeerFinder::SlotImp::recent_t::cache
beast::aged_unordered_map< beast::IP::Endpoint, std::uint32_t > cache
Definition: SlotImp.h:166
ripple::PeerFinder::SlotImp::m_public_key
std::optional< PublicKey > m_public_key
Definition: SlotImp.h:182
ripple::PeerFinder::SlotImp::local_endpoint
void local_endpoint(beast::IP::Endpoint const &endpoint)
Definition: SlotImp.h:109
ripple::PeerFinder::SlotImp::canAccept
bool canAccept
Definition: SlotImp.h:196
ripple::PeerFinder::SlotImp::public_key
void public_key(PublicKey const &key)
Definition: SlotImp.h:121
ripple::PeerFinder::SlotImp::SlotImp
SlotImp(beast::IP::Endpoint const &local_endpoint, beast::IP::Endpoint const &remote_endpoint, bool fixed, clock_type &clock)
Definition: SlotImp.cpp:27
ripple::PeerFinder::SlotImp::recent_t::insert
void insert(beast::IP::Endpoint const &ep, std::uint32_t hops)
Called for each valid endpoint received for a slot.
Definition: SlotImp.cpp:105
ripple::PeerFinder::SlotImp::local_endpoint
std::optional< beast::IP::Endpoint > const & local_endpoint() const override
The local endpoint of the socket, when known.
Definition: SlotImp.h:82
std::uint32_t
atomic
ripple::PeerFinder::Slot::State
State
Definition: peerfinder/Slot.h:37
beast::abstract_clock< std::chrono::steady_clock >
ripple::PeerFinder::SlotImp::unknownPort
static constexpr std::int32_t unknownPort
Definition: SlotImp.h:184
ripple::PeerFinder::SlotImp::reserved
void reserved(bool reserved_)
Definition: SlotImp.h:127
beast::detail::aged_unordered_container
Associative container where each element is also indexed by time.
Definition: aged_unordered_container.h:85
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::PeerFinder::SlotImp::m_inbound
const bool m_inbound
Definition: SlotImp.h:176
ripple::PeerFinder::SlotImp::recent
class ripple::PeerFinder::SlotImp::recent_t recent
ripple::PeerFinder::SlotImp::m_listening_port
std::atomic< std::int32_t > m_listening_port
Definition: SlotImp.h:185
ripple::PeerFinder::SlotImp::set_listening_port
void set_listening_port(std::uint16_t port)
Definition: SlotImp.h:103
optional
beast::IP::Endpoint
A version-independent IP address and port combination.
Definition: IPEndpoint.h:38
ripple::PeerFinder::SlotImp::state
State state() const override
Returns the state of the connection.
Definition: SlotImp.h:70
ripple::PeerFinder::SlotImp
Definition: SlotImp.h:33
ripple::PeerFinder::SlotImp::listening_port
std::optional< std::uint16_t > listening_port() const override
Definition: SlotImp.h:94
ripple::PeerFinder::SlotImp::reserved
bool reserved() const override
Returns true if this is a reserved connection.
Definition: SlotImp.h:64
ripple::PeerFinder::SlotImp::activate
void activate(clock_type::time_point const &now)
Definition: SlotImp.cpp:85
beast::abstract_clock< std::chrono::steady_clock >::time_point
typename std::chrono::steady_clock ::time_point time_point
Definition: abstract_clock.h:63
ripple::PeerFinder::SlotImp::connectivityCheckInProgress
bool connectivityCheckInProgress
Definition: SlotImp.h:200
ripple::PeerFinder::SlotImp::fixed
bool fixed() const override
Returns true if this is a fixed connection.
Definition: SlotImp.h:58
ripple::PeerFinder::SlotImp::remote_endpoint
beast::IP::Endpoint const & remote_endpoint() const override
The remote endpoint of socket.
Definition: SlotImp.h:76
ripple::PeerFinder::Slot
Properties and state associated with a peer to peer overlay connection.
Definition: peerfinder/Slot.h:32