This repository was archived by the owner on Oct 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathCommon.h
More file actions
306 lines (246 loc) · 8.24 KB
/
Common.h
File metadata and controls
306 lines (246 loc) · 8.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// Aleth: Ethereum C++ client, tools and libraries.
// Copyright 2019 Aleth Authors.
// Licensed under the GNU General Public License, Version 3.
//
// Miscellanea required for the Host/Session/NodeTable classes.
//
#pragma once
#include <atomic>
#include <string>
#include <set>
#include <vector>
// Make sure boost/asio.hpp is included before windows.h.
#include <boost/asio.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/steady_timer.hpp>
#include <chrono>
#include <libdevcrypto/Common.h>
#include <libdevcore/Log.h>
#include <libdevcore/Exceptions.h>
#include <libdevcore/RLP.h>
#include <libdevcore/Guards.h>
namespace ba = boost::asio;
namespace bi = boost::asio::ip;
namespace dev
{
class RLP;
class RLPStream;
namespace p2p
{
/// Peer network protocol version.
constexpr unsigned c_protocolVersion = 4;
class NodeIPEndpoint;
class Node;
extern const NodeIPEndpoint UnspecifiedNodeIPEndpoint;
extern const Node UnspecifiedNode;
using NodeID = h512;
bool isPrivateAddress(bi::address const& _addressToCheck);
bool isPrivateAddress(std::string const& _addressToCheck);
bool isLocalHostAddress(bi::address const& _addressToCheck);
bool isLocalHostAddress(std::string const& _addressToCheck);
bool isPublicAddress(bi::address const& _addressToCheck);
bool isPublicAddress(std::string const& _addressToCheck);
bool isAllowedAddress(bool _allowLocalDiscovery, bi::address const& _addressToCheck);
bool isAllowedEndpoint(bool _allowLocalDiscovery, NodeIPEndpoint const& _endpointToCheck);
class UPnP;
class Host;
class Session;
struct NetworkStartRequired: virtual dev::Exception {};
struct InvalidPublicIPAddress: virtual dev::Exception {};
struct NetworkRestartNotSupported : virtual dev::Exception {};
/// The ECDHE agreement failed during RLPx handshake.
struct ECDHEError: virtual Exception {};
#define NET_GLOBAL_LOGGER(NAME, SEVERITY) \
BOOST_LOG_INLINE_GLOBAL_LOGGER_CTOR_ARGS(g_##NAME##Logger, \
boost::log::sources::severity_channel_logger_mt<>, \
(boost::log::keywords::severity = SEVERITY)(boost::log::keywords::channel = "net"))
NET_GLOBAL_LOGGER(netnote, VerbosityInfo)
#define cnetnote LOG(dev::p2p::g_netnoteLogger::get())
NET_GLOBAL_LOGGER(netlog, VerbosityDebug)
#define cnetlog LOG(dev::p2p::g_netlogLogger::get())
NET_GLOBAL_LOGGER(netdetails, VerbosityTrace)
#define cnetdetails LOG(dev::p2p::g_netdetailsLogger::get())
enum P2pPacketType
{
HelloPacket = 0,
DisconnectPacket,
PingPacket,
PongPacket,
UserPacket = 0x10
};
char const* p2pPacketTypeToString(P2pPacketType _packetType);
enum DisconnectReason
{
DisconnectRequested = 0,
TCPError,
BadProtocol,
UselessPeer,
TooManyPeers,
DuplicatePeer,
IncompatibleProtocol,
NullIdentity,
ClientQuit,
UnexpectedIdentity,
LocalIdentity,
PingTimeout,
UserReason = 0x10,
NoDisconnect = 0xffff
};
/// @returns the string form of the given disconnection reason.
std::string reasonOf(DisconnectReason _r);
using CapDesc = std::pair<std::string, unsigned>;
using CapDescSet = std::set<CapDesc>;
using CapDescs = std::vector<CapDesc>;
/*
* Used by Host to pass negotiated information about a connection to a
* new Peer Session; PeerSessionInfo is then maintained by Session and can
* be queried for point-in-time status information via Host.
*/
struct PeerSessionInfo
{
NodeID const id;
std::string const clientVersion;
std::string const host;
unsigned short const port;
std::chrono::steady_clock::duration lastPing;
std::set<CapDesc> const caps;
std::map<std::string, std::string> notes;
};
using PeerSessionInfos = std::vector<PeerSessionInfo>;
enum class PeerType
{
Optional,
Required
};
/**
* @brief IPv4,UDP/TCP endpoints.
*/
class NodeIPEndpoint
{
public:
enum RLPAppend
{
StreamList,
StreamInline
};
NodeIPEndpoint() = default;
NodeIPEndpoint(bi::address _addr, uint16_t _udp, uint16_t _tcp)
: m_address(std::move(_addr)), m_udpPort(_udp), m_tcpPort(_tcp)
{}
explicit NodeIPEndpoint(RLP const& _r) { interpretRLP(_r); }
operator bi::udp::endpoint() const { return {m_address, m_udpPort}; }
operator bi::tcp::endpoint() const { return {m_address, m_tcpPort}; }
explicit operator bool() const
{
return !m_address.is_unspecified() && m_udpPort > 0 && m_tcpPort > 0;
}
bool operator==(NodeIPEndpoint const& _cmp) const {
return m_address == _cmp.m_address && m_udpPort == _cmp.m_udpPort &&
m_tcpPort == _cmp.m_tcpPort;
}
bool operator!=(NodeIPEndpoint const& _cmp) const {
return !operator==(_cmp);
}
void streamRLP(RLPStream& _s, RLPAppend _append = StreamList) const;
void interpretRLP(RLP const& _r);
bi::address address() const { return m_address; }
void setAddress(bi::address _addr) { m_address = std::move(_addr); }
uint16_t udpPort() const { return m_udpPort; }
void setUdpPort(uint16_t _udp) { m_udpPort = _udp; }
uint16_t tcpPort() const { return m_tcpPort; }
void setTcpPort(uint16_t _tcp) { m_tcpPort = _tcp; }
private:
bi::address m_address;
uint16_t m_udpPort = 0;
uint16_t m_tcpPort = 0;
};
struct NodeSpec
{
NodeSpec() {}
/// Accepts user-readable strings in the form defined here: https://github.com/ethereum/wiki/wiki/enode-url-format
NodeSpec(std::string const& _user);
NodeSpec(std::string const& _addr, uint16_t _port, int _udpPort = -1):
m_address(_addr),
m_tcpPort(_port),
m_udpPort(_udpPort == -1 ? _port : (uint16_t)_udpPort)
{}
NodeID id() const { return m_id; }
NodeIPEndpoint nodeIPEndpoint() const;
std::string enode() const;
bool isValid() const;
private:
std::string m_address;
uint16_t m_tcpPort = 0;
uint16_t m_udpPort = 0;
NodeID m_id;
};
class Node
{
public:
Node() = default;
virtual ~Node() = default;
Node(Node const&);
Node(Public _publicKey, NodeIPEndpoint const& _ip, PeerType _peerType = PeerType::Optional): id(_publicKey), endpoint(_ip), peerType(_peerType) {}
Node(NodeSpec const& _s, PeerType _peerType = PeerType::Optional);
NodeID const& address() const { return id; }
explicit operator bool() const { return static_cast<bool>(id); }
// TODO: make private, give accessors and rename m_...
NodeID id;
/// Endpoints by which we expect to reach node.
// TODO: make private, give accessors and rename m_...
NodeIPEndpoint endpoint;
// TODO: p2p implement
std::atomic<PeerType> peerType{PeerType::Optional};
};
inline boost::log::formatting_ostream& operator<<(
boost::log::formatting_ostream& _strm, Node const& _node)
{
return _strm << _node.id << '@' << _node.endpoint;
}
inline boost::log::formatting_ostream& operator<<(
boost::log::formatting_ostream& _strm, Node& _node)
{
auto const& constValue = _node;
_strm << constValue;
return _strm;
}
inline std::ostream& operator<<(std::ostream& _strm, NodeID const& _id)
{
_strm << "##" << _id.abridged();
return _strm;
}
inline boost::log::formatting_ostream& operator<<(
boost::log::formatting_ostream& _strm, PeerSessionInfo const& _peerSessionInfo)
{
_strm << _peerSessionInfo.id << "|" << _peerSessionInfo.clientVersion << "|"
<< _peerSessionInfo.host << "|" << _peerSessionInfo.port << "|";
for (auto const& cap : _peerSessionInfo.caps)
_strm << "(" << cap.first << "," << cap.second << ")";
return _strm;
}
/// Simple stream output for a NodeIPEndpoint.
std::ostream& operator<<(std::ostream& _out, NodeIPEndpoint const& _ep);
/// Official Ethereum boot nodes
std::vector<std::pair<Public, const char*>> defaultBootNodes();
}
}
/// std::hash for asio::adress
namespace std
{
template <> struct hash<bi::address>
{
size_t operator()(bi::address const& _a) const
{
if (_a.is_v4())
return std::hash<unsigned long>()(_a.to_v4().to_ulong());
if (_a.is_v6())
{
auto const& range = _a.to_v6().to_bytes();
return boost::hash_range(range.begin(), range.end());
}
if (_a.is_unspecified())
return static_cast<size_t>(0x3487194039229152ull); // Chosen by fair dice roll, guaranteed to be random
return std::hash<std::string>()(_a.to_string());
}
};
} // namespace std