Fabcoin Core  0.16.2
P2P Digital Currency
Common.h
Go to the documentation of this file.
1 /*
2  This file is part of cpp-ethereum.
3 
4  cpp-ethereum is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  cpp-ethereum is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16 */
25 #pragma once
26 
27 #include <string>
28 #include <set>
29 #include <vector>
30 
31 // Make sure boost/asio.hpp is included before windows.h.
32 #include <boost/asio.hpp>
33 #include <boost/asio/ip/tcp.hpp>
34 
35 #include <chrono>
36 #include <libdevcrypto/Common.h>
37 #include <libdevcore/Log.h>
38 #include <libdevcore/Exceptions.h>
39 #include <libdevcore/RLP.h>
40 #include <libdevcore/Guards.h>
41 namespace ba = boost::asio;
42 namespace bi = boost::asio::ip;
43 
44 namespace dev
45 {
46 
47 class RLP;
48 class RLPStream;
49 
50 namespace p2p
51 {
52 
54 extern const unsigned c_protocolVersion;
55 extern const unsigned c_defaultIPPort;
56 
57 class NodeIPEndpoint;
58 class Node;
59 extern const NodeIPEndpoint UnspecifiedNodeIPEndpoint;
60 extern const Node UnspecifiedNode;
61 
62 using NodeID = h512;
63 
64 bool isPrivateAddress(bi::address const& _addressToCheck);
65 bool isPrivateAddress(std::string const& _addressToCheck);
66 bool isLocalHostAddress(bi::address const& _addressToCheck);
67 bool isLocalHostAddress(std::string const& _addressToCheck);
68 bool isPublicAddress(bi::address const& _addressToCheck);
69 bool isPublicAddress(std::string const& _addressToCheck);
70 
71 class UPnP;
72 class Capability;
73 class Host;
74 class Session;
75 
79 
80 struct NetWarn: public LogChannel { static const char* name(); static const int verbosity = 0; };
81 struct NetNote: public LogChannel { static const char* name(); static const int verbosity = 2; };
82 struct NetImpolite: public LogChannel { static const char* name(); static const int verbosity = 3; };
83 struct NetMessageSummary: public LogChannel { static const char* name(); static const int verbosity = 4; };
84 struct NetConnect: public LogChannel { static const char* name(); static const int verbosity = 10; };
85 struct NetMessageDetail: public LogChannel { static const char* name(); static const int verbosity = 5; };
86 struct NetTriviaSummary: public LogChannel { static const char* name(); static const int verbosity = 10; };
87 struct NetTriviaDetail: public LogChannel { static const char* name(); static const int verbosity = 11; };
88 struct NetAllDetail: public LogChannel { static const char* name(); static const int verbosity = 13; };
89 struct NetRight: public LogChannel { static const char* name(); static const int verbosity = 14; };
90 struct NetLeft: public LogChannel { static const char* name(); static const int verbosity = 15; };
91 struct NetP2PWarn: public LogChannel { static const char* name(); static const int verbosity = 2; };
92 struct NetP2PNote: public LogChannel { static const char* name(); static const int verbosity = 6; };
93 struct NetP2PConnect: public LogChannel { static const char* name(); static const int verbosity = 10; };
94 
96 {
103  UserPacket = 0x10
104 };
105 
107 {
120  UserReason = 0x10,
121  NoDisconnect = 0xffff
122 };
123 
125 {
126  switch (_r)
127  {
128  case DuplicatePeer:
130  case NullIdentity:
131  case UnexpectedIdentity:
132  case LocalIdentity:
133  return true;
134  default:
135  return false;
136  }
137 }
138 
140 std::string reasonOf(DisconnectReason _r);
141 
142 using CapDesc = std::pair<std::string, u256>;
143 using CapDescSet = std::set<CapDesc>;
144 using CapDescs = std::vector<CapDesc>;
145 
146 /*
147  * Used by Host to pass negotiated information about a connection to a
148  * new Peer Session; PeerSessionInfo is then maintained by Session and can
149  * be queried for point-in-time status information via Host.
150  */
152 {
153  NodeID const id;
154  std::string const clientVersion;
155  std::string const host;
156  unsigned short const port;
158  std::set<CapDesc> const caps;
159  unsigned socketId;
160  std::map<std::string, std::string> notes;
161  unsigned const protocolVersion;
162 };
163 
164 using PeerSessionInfos = std::vector<PeerSessionInfo>;
165 
166 enum class PeerType
167 {
168  Optional,
169  Required
170 };
171 
176 {
177 public:
179  {
181  StreamInline
182  };
183 
185  static bool test_allowLocal;
186 
187  NodeIPEndpoint() = default;
188  NodeIPEndpoint(bi::address _addr, uint16_t _udp, uint16_t _tcp): address(_addr), udpPort(_udp), tcpPort(_tcp) {}
189  NodeIPEndpoint(RLP const& _r) { interpretRLP(_r); }
190 
191  operator bi::udp::endpoint() const { return bi::udp::endpoint(address, udpPort); }
192  operator bi::tcp::endpoint() const { return bi::tcp::endpoint(address, tcpPort); }
193 
194  operator bool() const { return !address.is_unspecified() && udpPort > 0 && tcpPort > 0; }
195 
196  bool isAllowed() const { return NodeIPEndpoint::test_allowLocal ? !address.is_unspecified() : isPublicAddress(address); }
197 
198  bool operator==(NodeIPEndpoint const& _cmp) const {
199  return address == _cmp.address && udpPort == _cmp.udpPort && tcpPort == _cmp.tcpPort;
200  }
201  bool operator!=(NodeIPEndpoint const& _cmp) const {
202  return !operator==(_cmp);
203  }
204 
205  void streamRLP(RLPStream& _s, RLPAppend _append = StreamList) const;
206  void interpretRLP(RLP const& _r);
207 
208  // TODO: make private, give accessors and rename m_...
210  uint16_t udpPort = 0;
211  uint16_t tcpPort = 0;
212 };
213 
214 struct NodeSpec
215 {
216  NodeSpec() {}
217 
219  NodeSpec(std::string const& _user);
220 
221  NodeSpec(std::string const& _addr, uint16_t _port, int _udpPort = -1):
222  m_address(_addr),
223  m_tcpPort(_port),
224  m_udpPort(_udpPort == -1 ? _port : (uint16_t)_udpPort)
225  {}
226 
227  NodeID id() const { return m_id; }
228 
229  NodeIPEndpoint nodeIPEndpoint() const;
230 
231  std::string enode() const;
232 
233 private:
234  std::string m_address;
235  uint16_t m_tcpPort = 0;
236  uint16_t m_udpPort = 0;
238 };
239 
240 class Node
241 {
242 public:
243  Node() = default;
244  Node(Node const&) = default;
245  Node(Public _publicKey, NodeIPEndpoint const& _ip, PeerType _peerType = PeerType::Optional): id(_publicKey), endpoint(_ip), peerType(_peerType) {}
246  Node(NodeSpec const& _s, PeerType _peerType = PeerType::Optional);
247 
248  virtual NodeID const& address() const { return id; }
249  virtual Public const& publicKey() const { return id; }
250 
251  virtual operator bool() const { return (bool)id; }
252 
253  // TODO: make private, give accessors and rename m_...
255 
257  // TODO: make private, give accessors and rename m_...
259 
260  // TODO: p2p implement
262 };
263 
265 {
267  {
268  public:
269  DeadlineOp(ba::io_service& _io, unsigned _msInFuture, std::function<void(boost::system::error_code const&)> const& _f): m_timer(new ba::deadline_timer(_io)) { m_timer->expires_from_now(boost::posix_time::milliseconds(_msInFuture)); m_timer->async_wait(_f); }
270  ~DeadlineOp() { if (m_timer) m_timer->cancel(); }
271 
272  DeadlineOp(DeadlineOp&& _s): m_timer(_s.m_timer.release()) {}
274  {
275  assert(&_s != this);
276 
277  m_timer.reset(_s.m_timer.release());
278  return *this;
279  }
280 
281  bool expired() { Guard l(x_timer); return m_timer->expires_from_now().total_nanoseconds() <= 0; }
282  void wait() { Guard l(x_timer); m_timer->wait(); }
283 
284  private:
285  std::unique_ptr<ba::deadline_timer> m_timer;
287  };
288 
289 public:
290  DeadlineOps(ba::io_service& _io, unsigned _reapIntervalMs = 100): m_io(_io), m_reapIntervalMs(_reapIntervalMs), m_stopped(false) { reap(); }
292 
293  void schedule(unsigned _msInFuture, std::function<void(boost::system::error_code const&)> const& _f) { if (m_stopped) return; DEV_GUARDED(x_timers) m_timers.emplace_back(m_io, _msInFuture, _f); }
294 
295  void stop() { m_stopped = true; DEV_GUARDED(x_timers) m_timers.clear(); }
296 
297  bool isStopped() const { return m_stopped; }
298 
299 protected:
300  void reap();
301 
302 private:
303  ba::io_service& m_io;
305 
306  std::vector<DeadlineOp> m_timers;
308 
309  std::atomic<bool> m_stopped;
310 };
311 
312 }
313 
315 std::ostream& operator<<(std::ostream& _out, dev::p2p::NodeIPEndpoint const& _ep);
316 }
317 
319 namespace std
320 {
321 
322 template <> struct hash<bi::address>
323 {
324  size_t operator()(bi::address const& _a) const
325  {
326  if (_a.is_v4())
327  return std::hash<unsigned long>()(_a.to_v4().to_ulong());
328  if (_a.is_v6())
329  {
330  auto const& range = _a.to_v6().to_bytes();
331  return boost::hash_range(range.begin(), range.end());
332  }
333  if (_a.is_unspecified())
334  return static_cast<size_t>(0x3487194039229152ull); // Chosen by fair dice roll, guaranteed to be random
335  return std::hash<std::string>()(_a.to_string());
336  }
337 };
338 
339 }
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
bool isAllowed() const
Definition: Common.h:196
#define function(a, b, c, d, k, s)
std::string const host
Definition: Common.h:155
bi::address address
Definition: Common.h:209
std::pair< std::string, u256 > CapDesc
Definition: Common.h:142
bool isPrivateAddress(bi::address const &_addressToCheck)
Definition: Common.cpp:84
unsigned const protocolVersion
Definition: Common.h:161
PacketType
Definition: Common.h:95
bool isLocalHostAddress(bi::address const &_addressToCheck)
Definition: Common.cpp:116
The Host class Capabilities should be registered prior to startNetwork, since m_capabilities is not t...
Definition: Host.h:129
std::vector< PeerSessionInfo > PeerSessionInfos
Definition: Common.h:164
std::map< std::string, std::string > notes
Definition: Common.h:160
bool isPublicAddress(bi::address const &_addressToCheck)
Definition: Common.cpp:76
NodeIPEndpoint(bi::address _addr, uint16_t _udp, uint16_t _tcp)
Definition: Common.h:188
bool operator!=(NodeIPEndpoint const &_cmp) const
Definition: Common.h:201
std::string m_address
Definition: Common.h:234
std::hash for asio::adress
Definition: Common.h:323
assert(len-trim+(2 *lenIndices)<=WIDTH)
const unsigned c_protocolVersion
Peer network protocol version.
Definition: Common.cpp:28
IPv4,UDP/TCP endpoints.
Definition: Common.h:175
FixedHash< 64 > h512
Definition: FixedHash.h:339
std::chrono::steady_clock::duration lastPing
Definition: Common.h:157
bool operator==(const ::CryptoPP::OID &lhs, const ::CryptoPP::OID &rhs)
Definition: asn.h:558
DisconnectReason
Definition: Common.h:106
bool isStopped() const
Definition: Common.h:297
Node(Public _publicKey, NodeIPEndpoint const &_ip, PeerType _peerType=PeerType::Optional)
Definition: Common.h:245
unsigned short const port
Definition: Common.h:156
#define DEV_GUARDED(MUTEX)
Simple block guard.
Definition: Guards.h:144
bool operator==(NodeIPEndpoint const &_cmp) const
Definition: Common.h:198
DeadlineOps(ba::io_service &_io, unsigned _reapIntervalMs=100)
Definition: Common.h:290
std::string reasonOf(DisconnectReason _r)
Definition: Common.cpp:134
Base class for all exceptions.
Definition: Exceptions.h:39
unsigned m_reapIntervalMs
Definition: Common.h:304
std::lock_guard< std::mutex > Guard
Definition: Guards.h:41
const char * name
Definition: rest.cpp:36
std::atomic< bool > m_stopped
Definition: Common.h:309
clock::duration duration
Definition: bench.h:50
NodeID id() const
Definition: Common.h:227
bool isPermanentProblem(DisconnectReason _r)
Definition: Common.h:124
DeadlineOp(ba::io_service &_io, unsigned _msInFuture, std::function< void(boost::system::error_code const &)> const &_f)
Definition: Common.h:269
NodeSpec(std::string const &_addr, uint16_t _port, int _udpPort=-1)
Definition: Common.h:221
const Node UnspecifiedNode
Definition: Common.cpp:33
ba::io_service & m_io
Definition: Common.h:303
NodeID id
Definition: Common.h:254
void schedule(unsigned _msInFuture, std::function< void(boost::system::error_code const &)> const &_f)
Definition: Common.h:293
DeadlineOp(DeadlineOp &&_s)
Definition: Common.h:272
std::vector< CapDesc > CapDescs
Definition: Common.h:144
std::unique_ptr< ba::deadline_timer > m_timer
Definition: Common.h:285
NodeIPEndpoint(RLP const &_r)
Definition: Common.h:189
std::set< CapDesc > const caps
Definition: Common.h:158
virtual NodeID const & address() const
Definition: Common.h:248
std::string const clientVersion
Definition: Common.h:154
PeerType
Definition: Common.h:166
const unsigned c_defaultIPPort
Definition: Common.cpp:29
std::set< CapDesc > CapDescSet
Definition: Common.h:143
virtual Public const & publicKey() const
Definition: Common.h:249
The default logging channels.
Definition: Log.h:130
std::vector< DeadlineOp > m_timers
Definition: Common.h:306
struct evm_uint160be address(struct evm_env *env)
Definition: capi.c:13
std::mutex Mutex
Definition: Guards.h:37
std::ostream & operator<<(std::ostream &_out, NodeTable const &_nodeTable)
Definition: NodeTable.h:270
Class for writing to an RLP bytestream.
Definition: RLP.h:383
DeadlineOp & operator=(DeadlineOp &&_s)
Definition: Common.h:273
const NodeIPEndpoint UnspecifiedNodeIPEndpoint
Definition: Common.cpp:32
Class for interpreting Recursive Linear-Prefix Data.
Definition: RLP.h:64
size_t operator()(bi::address const &_a) const
Definition: Common.h:324
static bool test_allowLocal
Setting true causes isAllowed to return true for all addresses. (Used by test fixtures) ...
Definition: Common.h:185
UniValue stop(const JSONRPCRequest &jsonRequest)
Definition: server.cpp:237
The Session class.
Definition: Session.h:90
NodeIPEndpoint endpoint
Endpoints by which we expect to reach node.
Definition: Common.h:258