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