TARAXA
Loading...
Searching...
No Matches
Common.h
Go to the documentation of this file.
1// Aleth: Ethereum C++ client, tools and libraries.
2// Copyright 2014-2019 Aleth Authors.
3// Licensed under the GNU General Public License, Version 3.
4
5//
6// Miscellanea required for the Host/Session/NodeTable classes.
7//
8
9#pragma once
10
11#include <atomic>
12#include <set>
13#include <shared_mutex>
14#include <string>
15#include <vector>
16
17// Make sure boost/asio.hpp is included before windows.h.
19#include <libdevcore/Guards.h>
20#include <libdevcore/Log.h>
21#include <libdevcore/RLP.h>
22#include <libdevcrypto/Common.h>
23
24#include <boost/asio.hpp>
25#include <boost/asio/ip/tcp.hpp>
26#include <boost/asio/steady_timer.hpp>
27#include <chrono>
28
29namespace ba = boost::asio;
30namespace bi = ba::ip;
31
32namespace dev {
33
34class RLP;
35class RLPStream;
36
37namespace p2p {
38
40constexpr unsigned c_protocolVersion = 1029;
41
42class NodeIPEndpoint;
43class Node;
45extern const Node UnspecifiedNode;
46
47using NodeID = h512;
48
49bool isPrivateAddress(bi::address const& _addressToCheck);
50bool isPrivateAddress(std::string const& _addressToCheck);
51bool isLocalHostAddress(bi::address const& _addressToCheck);
52bool isLocalHostAddress(std::string const& _addressToCheck);
53bool isPublicAddress(bi::address const& _addressToCheck);
54bool isPublicAddress(std::string const& _addressToCheck);
55bool isAllowedAddress(bool _allowLocalDiscovery, bi::address const& _addressToCheck);
57
58class UPnP;
59struct Host;
60struct Session;
61
65
67struct ECDHEError : virtual Exception {};
68
70
72
89
91std::string reasonOf(DisconnectReason _r);
92
103
104using CapDesc = std::pair<std::string, unsigned>;
105using CapDescs = std::vector<CapDesc>;
106
107/*
108 * Used by Host to pass negotiated information about a connection to a
109 * new Peer Session; PeerSessionInfo is then maintained by Session and can
110 * be queried for point-in-time status information via Host.
111 */
114 std::string clientVersion;
115 std::string host;
116 unsigned short port;
117 std::chrono::steady_clock::duration lastPing;
118 std::set<CapDesc> caps;
119};
120
121using PeerSessionInfos = std::vector<PeerSessionInfo>;
122
123enum class PeerType { Optional, Required };
124
129 public:
131
132 NodeIPEndpoint() = default;
135 explicit NodeIPEndpoint(RLP const& _r) { interpretRLP(_r); }
136
137 operator bi::udp::endpoint() const { return {m_address, m_udpPort}; }
138 operator bi::tcp::endpoint() const { return {m_address, m_tcpPort}; }
139
140 explicit operator bool() const { return !m_address.is_unspecified() && m_udpPort > 0 && m_tcpPort > 0; }
141
142 bool operator==(NodeIPEndpoint const& _cmp) const {
143 return m_address == _cmp.m_address && m_udpPort == _cmp.m_udpPort && m_tcpPort == _cmp.m_tcpPort;
144 }
145 bool operator!=(NodeIPEndpoint const& _cmp) const { return !operator==(_cmp); }
146
148 void interpretRLP(RLP const& _r);
149
150 bi::address address() const { return m_address; }
151
152 void setAddress(bi::address _addr) { m_address = std::move(_addr); }
153
154 uint16_t udpPort() const { return m_udpPort; }
155
157
158 uint16_t tcpPort() const { return m_tcpPort; }
159
161
162 private:
163 bi::address m_address;
166};
167
168struct NodeSpec {
170
173 NodeSpec(std::string const& _user);
174
175 NodeSpec(std::string const& _addr, uint16_t _port, int _udpPort = -1)
177
178 NodeID id() const { return m_id; }
179
181
182 std::string enode() const;
183
184 bool isValid() const;
185
186 private:
187 std::string m_address;
191};
192
193class Node {
194 public:
195 virtual ~Node() = default;
196 Node(Node const&);
200
201 NodeID const& address() const { return id; }
202
204 void set_endpoint(NodeIPEndpoint endpoint);
205
206 explicit operator bool() const { return static_cast<bool>(id); }
207
208 // TODO: make private, give accessors and rename m_...
209 NodeID const id;
210
211 private:
213 mutable std::shared_mutex endpoint_mu_;
214
215 public:
216 // TODO: p2p implement
217 std::atomic<PeerType> peerType{PeerType::Optional};
218 std::atomic<uint16_t> external_udp_port;
219};
220
221inline boost::log::formatting_ostream& operator<<(boost::log::formatting_ostream& _strm, Node const& _node) {
222 return _strm << _node.id << '@' << _node.get_endpoint();
223}
224
225inline boost::log::formatting_ostream& operator<<(boost::log::formatting_ostream& _strm, Node& _node) {
226 auto const& constValue = _node;
227 _strm << constValue;
228 return _strm;
229}
230
231inline std::ostream& operator<<(std::ostream& _strm, NodeID const& _id) {
232 _strm << "##" << _id.abridged();
233 return _strm;
234}
235
236inline boost::log::formatting_ostream& operator<<(boost::log::formatting_ostream& _strm,
238 _strm << _peerSessionInfo.id << "|" << _peerSessionInfo.clientVersion << "|" << _peerSessionInfo.host << "|"
239 << _peerSessionInfo.port << "|";
240 for (auto const& cap : _peerSessionInfo.caps) _strm << "(" << cap.first << "," << cap.second << ")";
241 return _strm;
242}
243
245std::ostream& operator<<(std::ostream& _out, NodeIPEndpoint const& _ep);
246
247} // namespace p2p
248} // namespace dev
249
251#if !defined(BOOST_ASIO_HAS_STD_HASH)
252namespace std {
253template <>
254struct hash<bi::address> {
255 size_t operator()(bi::address const& _a) const {
256 if (_a.is_v4()) return std::hash<unsigned long>()(_a.to_v4().to_ulong());
257 if (_a.is_v6()) {
258 auto const& range = _a.to_v6().to_bytes();
259 return boost::hash_range(range.begin(), range.end());
260 }
261 if (_a.is_unspecified())
262 return static_cast<size_t>(0x3487194039229152ull); // Chosen by fair dice roll, guaranteed to be
263 // random
264 return std::hash<std::string>()(_a.to_string());
265 }
266};
267} // namespace std
268#endif // !defined(BOOST_ASIO_HAS_STD_HASH)
Definition RLP.h:56
Class for writing to an RLP bytestream.
Definition RLP.h:484
Definition Common.h:193
virtual ~Node()=default
std::atomic< PeerType > peerType
Definition Common.h:217
std::atomic< uint16_t > external_udp_port
Definition Common.h:218
Node(Public _publicKey, NodeIPEndpoint const &_ip, PeerType _peerType=PeerType::Optional)
Definition Common.h:197
NodeID const id
Definition Common.h:209
std::shared_mutex endpoint_mu_
Definition Common.h:213
NodeID const & address() const
Definition Common.h:201
void set_endpoint(NodeIPEndpoint endpoint)
Definition Common.cpp:158
NodeIPEndpoint endpoint_
Definition Common.h:212
NodeIPEndpoint get_endpoint() const
Definition Common.cpp:153
IPv4,UDP/TCP endpoints.
Definition Common.h:128
void setUdpPort(uint16_t _udp)
Definition Common.h:156
void interpretRLP(RLP const &_r)
Definition Common.cpp:137
uint16_t tcpPort() const
Definition Common.h:158
void streamRLP(RLPStream &_s, RLPAppend _append=StreamList) const
Definition Common.cpp:126
void setAddress(bi::address _addr)
Definition Common.h:152
bool operator!=(NodeIPEndpoint const &_cmp) const
Definition Common.h:145
uint16_t m_udpPort
Definition Common.h:164
uint16_t udpPort() const
Definition Common.h:154
NodeIPEndpoint(RLP const &_r)
Definition Common.h:135
bi::address m_address
Definition Common.h:163
void setTcpPort(uint16_t _tcp)
Definition Common.h:160
RLPAppend
Definition Common.h:130
@ StreamList
Definition Common.h:130
@ StreamInline
Definition Common.h:130
bi::address address() const
Definition Common.h:150
NodeIPEndpoint(bi::address _addr, uint16_t _udp, uint16_t _tcp)
Definition Common.h:133
bool operator==(NodeIPEndpoint const &_cmp) const
Definition Common.h:142
uint16_t m_tcpPort
Definition Common.h:165
Definition UPnP.h:17
std::vector< CapDesc > CapDescs
Definition Common.h:105
std::string clientVersion
Definition Common.h:114
HandshakeFailureReason
Definition Common.h:93
std::set< CapDesc > caps
Definition Common.h:118
std::string host
Definition Common.h:115
NodeID id
Definition Common.h:113
std::pair< std::string, unsigned > CapDesc
Definition Common.h:104
std::chrono::steady_clock::duration lastPing
Definition Common.h:117
std::vector< PeerSessionInfo > PeerSessionInfos
Definition Common.h:121
ostream & operator<<(ostream &_out, NodeIPEndpoint const &_ep)
Definition Common.cpp:205
bool isLocalHostAddress(bi::address const &_addressToCheck)
Definition Common.cpp:77
const Node UnspecifiedNode
Definition Common.cpp:17
bool isPublicAddress(string const &_addressToCheck)
Definition Common.cpp:34
constexpr unsigned c_protocolVersion
Peer network protocol version.
Definition Common.h:40
string reasonOf(DisconnectReason _r)
Definition Common.cpp:93
char const * p2pPacketTypeToString(P2pPacketType _packetType)
Definition Common.cpp:19
bool isPrivateAddress(bi::address const &_addressToCheck)
Definition Common.cpp:52
unsigned short port
Definition Common.h:116
bool isAllowedEndpoint(bool _allowLocalDiscovery, NodeIPEndpoint const &_endpointToCheck)
Definition Common.cpp:46
DisconnectReason
Definition Common.h:73
@ NoDisconnect
Definition Common.h:87
@ UserReason
Definition Common.h:86
@ LocalIdentity
Definition Common.h:84
@ DisconnectRequested
Definition Common.h:74
@ DuplicatePeer
Definition Common.h:79
@ PingTimeout
Definition Common.h:85
@ IncompatibleProtocol
Definition Common.h:80
@ UnexpectedIdentity
Definition Common.h:83
@ ClientQuit
Definition Common.h:82
@ TCPError
Definition Common.h:75
@ TooManyPeers
Definition Common.h:78
@ NullIdentity
Definition Common.h:81
@ UselessPeer
Definition Common.h:77
@ BadProtocol
Definition Common.h:76
P2pPacketType
Definition Common.h:69
@ UserPacket
Definition Common.h:69
@ PongPacket
Definition Common.h:69
@ PingPacket
Definition Common.h:69
@ DisconnectPacket
Definition Common.h:69
@ HelloPacket
Definition Common.h:69
bool isAllowedAddress(bool _allowLocalDiscovery, bi::address const &_addressToCheck)
Definition Common.cpp:42
PeerType
Definition Common.h:123
const NodeIPEndpoint UnspecifiedNodeIPEndpoint
Definition Common.cpp:16
Definition Common.h:112
Definition Address.h:13
void assertThrowAux(bool _condition, ::std::string const &_errorDescription, unsigned _line, char const *_file, char const *_function)
Definition Assertions.h:53
FixedHash< 64 > h512
Definition FixedHash.h:452
std::hash for asio::adress
Definition FixedHash.h:483
Base class for all exceptions.
Definition Exceptions.h:21
The ECDHE agreement failed during RLPx handshake.
Definition Common.h:67
The Host class Capabilities should be registered prior to startNetwork, since m_capabilities is not t...
Definition Host.h:69
Definition Common.h:63
Definition Common.h:62
Definition Common.h:168
bool isValid() const
Definition Common.cpp:203
NodeID id() const
Definition Common.h:178
NodeIPEndpoint nodeIPEndpoint() const
Definition Common.cpp:185
NodeID m_id
Definition Common.h:190
uint16_t m_tcpPort
Definition Common.h:188
NodeSpec(std::string const &_user)
std::string enode() const
Definition Common.cpp:189
NodeSpec()
Definition Common.h:169
NodeSpec(std::string const &_addr, uint16_t _port, int _udpPort=-1)
Definition Common.h:175
std::string m_address
Definition Common.h:187
uint16_t m_udpPort
Definition Common.h:189
The Session class.
Definition Session.h:33
size_t operator()(bi::address const &_a) const
Definition Common.h:255