1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2012 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
12 #include <openssl/rand.h>
20 class CRequestTracker;
23 extern int nBestHeight;
25 const uint16_t nSocksDefault = 9050;
26 const uint16_t nPortZero = 0;
29 inline uint64_t ReceiveBufferSize() { return 1000*GetArg("-maxreceivebuffer", 5*1000); }
30 inline uint64_t SendBufferSize() { return 1000*GetArg("-maxsendbuffer", 1*1000); }
32 void AddOneShot(std::string strDest);
33 bool RecvLine(SOCKET hSocket, std::string& strLine);
34 bool GetMyExternalIP(CNetAddr& ipRet);
35 void AddressCurrentlyConnected(const CService& addr);
36 CNode* FindNode(const CNetAddr& ip);
37 CNode* FindNode(const CService& ip);
38 CNode* ConnectNode(CAddress addrConnect, const char *strDest = NULL, int64_t nTimeout=0);
39 bool OpenNetworkConnection(const CAddress& addrConnect, CSemaphoreGrant *grantOutbound = NULL, const char *strDest = NULL, bool fOneShot = false);
40 uint16_t GetListenPort();
41 bool BindListenPort(const CService &bindAddr, std::string& strError=REF(std::string()));
42 void StartNode(void* parg);
47 LOCAL_NONE, // unknown
48 LOCAL_IF, // address a local interface listens on
49 LOCAL_BIND, // address explicit bound to
50 LOCAL_IRC, // address reported by IRC (deprecated)
51 LOCAL_HTTP, // address reported by whatismyip.com and similar
52 LOCAL_MANUAL, // address explicitly specified (-externalip=)
58 bool IsPeerAddrLocalGood(CNode *pnode);
59 void AdvertiseLocal(CNode *pnode);
60 void SetLimited(enum Network net, bool fLimited = true);
61 bool IsLimited(enum Network net);
62 bool IsLimited(const CNetAddr& addr);
63 bool AddLocal(const CService& addr, int nScore = LOCAL_NONE);
64 bool AddLocal(const CNetAddr& addr, int nScore = LOCAL_NONE);
65 bool SeenLocal(const CService& addr);
66 bool IsLocal(const CService& addr);
67 bool GetLocal(CService &addr, const CNetAddr *paddrPeer = NULL);
68 bool IsReachable(const CNetAddr &addr);
69 void SetReachable(enum Network net, bool fFlag = true);
70 CAddress GetLocalAddress(const CNetAddr *paddrPeer = NULL);
82 void (*fn)(void*, CDataStream&);
85 explicit CRequestTracker(void (*fnIn)(void*, CDataStream&)=NULL, void* param1In=NULL)
101 THREAD_SOCKETHANDLER,
102 THREAD_OPENCONNECTIONS,
103 THREAD_MESSAGEHANDLER,
106 THREAD_ADDEDCONNECTIONS,
118 extern bool fDiscover;
119 extern bool fNoListen;
121 extern bool fDiscover;
122 extern uint64_t nLocalServices;
123 extern uint64_t nLocalHostNonce;
124 extern CAddress addrSeenByPeer;
125 extern std::array<int, THREAD_MAX> vnThreadsRunning;
126 extern CAddrMan addrman;
128 extern std::vector<CNode*> vNodes;
129 extern CCriticalSection cs_vNodes;
130 extern std::vector<std::string> vAddedNodes;
131 extern CCriticalSection cs_vAddedNodes;
132 extern std::map<CInv, CDataStream> mapRelay;
133 extern std::deque<std::pair<int64_t, CInv> > vRelayExpiration;
134 extern CCriticalSection cs_mapRelay;
135 extern std::map<CInv, int64_t> mapAlreadyAskedFor;
144 int64_t nTimeConnected;
145 std::string addrName;
147 std::string strSubVer;
149 int64_t nReleaseTime;
150 int32_t nStartingHeight;
151 int32_t nMisbehavior;
158 // Information about a peer
169 CCriticalSection cs_vSend;
170 CCriticalSection cs_vRecv;
173 int64_t nLastSendEmpty;
174 int64_t nTimeConnected;
175 int32_t nHeaderStart;
176 uint32_t nMessageStart;
178 std::string addrName;
181 std::string strSubVer;
186 bool fSuccessfullyConnected;
188 CSemaphoreGrant grantOutbound;
192 // Denial-of-service detection/prevention
193 // Key is IP address, value is banned-until-time
194 static std::map<CNetAddr, int64_t> setBanned;
195 static CCriticalSection cs_setBanned;
199 int64_t nReleaseTime;
200 std::map<uint256, CRequestTracker> mapRequests;
201 CCriticalSection cs_mapRequests;
202 uint256 hashContinue;
203 CBlockIndex* pindexLastGetBlocksBegin;
204 uint256 hashLastGetBlocksEnd;
205 int32_t nStartingHeight;
209 std::vector<CAddress> vAddrToSend;
210 std::set<CAddress> setAddrKnown;
212 std::set<uint256> setKnown;
213 uint256 hashCheckpointKnown; // ppcoin: known sent sync-checkpoint
214 int64_t nNextAddrSend;
215 int64_t nNextLocalAddrSend;
216 int64_t nNextInvSend;
218 // inventory based relay
219 mruset<CInv> setInventoryKnown;
220 std::vector<CInv> vInventoryToSend;
221 CCriticalSection cs_inventory;
222 std::multimap<int64_t, CInv> mapAskFor;
224 CNode(SOCKET hSocketIn, CAddress addrIn, std::string addrNameIn = "", bool fInboundIn=false);
228 // Network usage totals
229 static CCriticalSection cs_totalBytesRecv;
230 static CCriticalSection cs_totalBytesSent;
231 static uint64_t nTotalBytesRecv;
232 static uint64_t nTotalBytesSent;
234 void operator=(const CNode&);
239 CNode* AddRef(int64_t nTimeout=0);
241 void AddAddressKnown(const CAddress& addr);
242 void PushAddress(const CAddress& addr);
243 void AddInventoryKnown(const CInv& inv);
244 void PushInventory(const CInv& inv);
245 void AskFor(const CInv& inv);
246 void BeginMessage(const char* pszCommand);
249 void EndMessageAbortIfEmpty();
252 void PushMessage(const char* pszCommand)
256 BeginMessage(pszCommand);
266 template<typename T1>
267 void PushMessage(const char* pszCommand, const T1& a1)
271 BeginMessage(pszCommand);
282 template<typename T1, typename T2>
283 void PushMessage(const char* pszCommand, const T1& a1, const T2& a2)
287 BeginMessage(pszCommand);
298 template<typename T1, typename T2, typename T3>
299 void PushMessage(const char* pszCommand, const T1& a1, const T2& a2, const T3& a3)
303 BeginMessage(pszCommand);
304 vSend << a1 << a2 << a3;
314 template<typename T1, typename T2, typename T3, typename T4>
315 void PushMessage(const char* pszCommand, const T1& a1, const T2& a2, const T3& a3, const T4& a4)
319 BeginMessage(pszCommand);
320 vSend << a1 << a2 << a3 << a4;
330 template<typename T1, typename T2, typename T3, typename T4, typename T5>
331 void PushMessage(const char* pszCommand, const T1& a1, const T2& a2, const T3& a3, const T4& a4, const T5& a5)
335 BeginMessage(pszCommand);
336 vSend << a1 << a2 << a3 << a4 << a5;
346 template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
347 void PushMessage(const char* pszCommand, const T1& a1, const T2& a2, const T3& a3, const T4& a4, const T5& a5, const T6& a6)
351 BeginMessage(pszCommand);
352 vSend << a1 << a2 << a3 << a4 << a5 << a6;
362 template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7>
363 void PushMessage(const char* pszCommand, const T1& a1, const T2& a2, const T3& a3, const T4& a4, const T5& a5, const T6& a6, const T7& a7)
367 BeginMessage(pszCommand);
368 vSend << a1 << a2 << a3 << a4 << a5 << a6 << a7;
378 template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8>
379 void PushMessage(const char* pszCommand, const T1& a1, const T2& a2, const T3& a3, const T4& a4, const T5& a5, const T6& a6, const T7& a7, const T8& a8)
383 BeginMessage(pszCommand);
384 vSend << a1 << a2 << a3 << a4 << a5 << a6 << a7 << a8;
394 template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9>
395 void PushMessage(const char* pszCommand, const T1& a1, const T2& a2, const T3& a3, const T4& a4, const T5& a5, const T6& a6, const T7& a7, const T8& a8, const T9& a9)
399 BeginMessage(pszCommand);
400 vSend << a1 << a2 << a3 << a4 << a5 << a6 << a7 << a8 << a9;
411 void PushRequest(const char* pszCommand,
412 void (*fn)(void*, CDataStream&), void* param1)
415 RAND_bytes((unsigned char*)&hashReply, sizeof(hashReply));
418 LOCK(cs_mapRequests);
419 mapRequests[hashReply] = CRequestTracker(fn, param1);
422 PushMessage(pszCommand, hashReply);
425 template<typename T1>
426 void PushRequest(const char* pszCommand, const T1& a1,
427 void (*fn)(void*, CDataStream&), void* param1)
430 RAND_bytes((unsigned char*)&hashReply, sizeof(hashReply));
433 LOCK(cs_mapRequests);
434 mapRequests[hashReply] = CRequestTracker(fn, param1);
437 PushMessage(pszCommand, hashReply, a1);
440 template<typename T1, typename T2>
441 void PushRequest(const char* pszCommand, const T1& a1, const T2& a2,
442 void (*fn)(void*, CDataStream&), void* param1)
445 RAND_bytes((unsigned char*)&hashReply, sizeof(hashReply));
448 LOCK(cs_mapRequests);
449 mapRequests[hashReply] = CRequestTracker(fn, param1);
452 PushMessage(pszCommand, hashReply, a1, a2);
457 void PushGetBlocks(CBlockIndex* pindexBegin, uint256 hashEnd);
458 bool IsSubscribed(unsigned int nChannel);
459 void Subscribe(unsigned int nChannel, unsigned int nHops=0);
460 void CancelSubscribe(unsigned int nChannel);
461 void CloseSocketDisconnect();
465 // Denial-of-service detection/prevention
466 // The idea is to detect peers that are behaving
467 // badly and disconnect/ban them, but do it in a
468 // one-coding-mistake-won't-shatter-the-entire-network
470 // IMPORTANT: There should be nothing I can give a
471 // node that it will forward on that will make that
472 // node's peers drop it. If there is, an attacker
473 // can isolate a node and/or try to split the network.
474 // Dropping a node for sending stuff that is invalid
475 // now but might be valid in a later version is also
476 // dangerous, because it can cause a network split
477 // between nodes running old code and nodes running
479 static void ClearBanned(); // needed for unit testing
480 static bool IsBanned(CNetAddr ip);
481 bool Misbehaving(int howmuch); // 1 == a little, 100 == a lot
482 void copyStats(CNodeStats &stats);
484 static void RecordBytesRecv(uint64_t bytes);
485 static void RecordBytesSent(uint64_t bytes);
487 static uint64_t GetTotalBytesRecv();
488 static uint64_t GetTotalBytesSent();
492 void RelayTransaction(const CTransaction& tx, const uint256& hash);
493 void RelayTransaction(const CTransaction& tx, const uint256& hash, const CDataStream& ss);
496 /** Return a timestamp in the future (in microseconds) for exponentially distributed events. */
497 int64_t PoissonNextSend(int64_t nNow, int average_interval_seconds);