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>
21 class CRequestTracker;
24 extern int nBestHeight;
26 const uint16_t nSocksDefault = 9050;
27 const uint16_t nPortZero = 0;
30 inline uint64_t ReceiveBufferSize() { return 1000*GetArg("-maxreceivebuffer", 5*1000); }
31 inline uint64_t SendBufferSize() { return 1000*GetArg("-maxsendbuffer", 1*1000); }
33 void AddOneShot(string strDest);
34 bool RecvLine(SOCKET hSocket, string& strLine);
35 bool GetMyExternalIP(CNetAddr& ipRet);
36 void AddressCurrentlyConnected(const CService& addr);
37 CNode* FindNode(const CNetAddr& ip);
38 CNode* FindNode(const CService& ip);
39 CNode* ConnectNode(CAddress addrConnect, const char *strDest = NULL, int64_t nTimeout=0);
40 bool OpenNetworkConnection(const CAddress& addrConnect, CSemaphoreGrant *grantOutbound = NULL, const char *strDest = NULL, bool fOneShot = false);
42 uint16_t GetListenPort();
43 bool BindListenPort(const CService &bindAddr, string& strError=REF(string()));
44 void StartNode(void* parg);
49 LOCAL_NONE, // unknown
50 LOCAL_IF, // address a local interface listens on
51 LOCAL_BIND, // address explicit bound to
52 LOCAL_IRC, // address reported by IRC (deprecated)
53 LOCAL_HTTP, // address reported by whatismyip.com and similar
54 LOCAL_MANUAL, // address explicitly specified (-externalip=)
60 bool IsPeerAddrLocalGood(CNode *pnode);
61 void AdvertiseLocal(CNode *pnode);
62 void SetLimited(enum Network net, bool fLimited = true);
63 bool IsLimited(enum Network net);
64 bool IsLimited(const CNetAddr& addr);
65 bool AddLocal(const CService& addr, int nScore = LOCAL_NONE);
66 bool AddLocal(const CNetAddr& addr, int nScore = LOCAL_NONE);
67 bool SeenLocal(const CService& addr);
68 bool IsLocal(const CService& addr);
69 bool GetLocal(CService &addr, const CNetAddr *paddrPeer = NULL);
70 bool IsReachable(const CNetAddr &addr);
71 void SetReachable(enum Network net, bool fFlag = true);
72 CAddress GetLocalAddress(const CNetAddr *paddrPeer = NULL);
84 void (*fn)(void*, CDataStream&);
87 explicit CRequestTracker(void (*fnIn)(void*, CDataStream&)=NULL, void* param1In=NULL)
103 THREAD_SOCKETHANDLER,
104 THREAD_OPENCONNECTIONS,
105 THREAD_MESSAGEHANDLER,
108 THREAD_ADDEDCONNECTIONS,
120 extern bool fDiscover;
121 extern bool fNoListen;
123 extern bool fDiscover;
124 extern uint64_t nLocalServices;
125 extern uint64_t nLocalHostNonce;
126 extern CAddress addrSeenByPeer;
127 extern array<int, THREAD_MAX> vnThreadsRunning;
128 extern CAddrMan addrman;
130 extern vector<CNode*> vNodes;
131 extern CCriticalSection cs_vNodes;
132 extern vector<string> vAddedNodes;
133 extern CCriticalSection cs_vAddedNodes;
134 extern map<CInv, CDataStream> mapRelay;
135 extern deque<pair<int64_t, CInv> > vRelayExpiration;
136 extern CCriticalSection cs_mapRelay;
137 extern map<CInv, int64_t> mapAlreadyAskedFor;
146 int64_t nTimeConnected;
151 int64_t nReleaseTime;
152 int32_t nStartingHeight;
153 int32_t nMisbehavior;
160 // Information about a peer
171 CCriticalSection cs_vSend;
172 CCriticalSection cs_vRecv;
175 int64_t nLastSendEmpty;
176 int64_t nTimeConnected;
177 int32_t nHeaderStart;
178 uint32_t nMessageStart;
188 bool fSuccessfullyConnected;
190 CSemaphoreGrant grantOutbound;
194 // Denial-of-service detection/prevention
195 // Key is IP address, value is banned-until-time
196 static map<CNetAddr, int64_t> setBanned;
197 static CCriticalSection cs_setBanned;
201 int64_t nReleaseTime;
202 map<uint256, CRequestTracker> mapRequests;
203 CCriticalSection cs_mapRequests;
204 uint256 hashContinue;
205 CBlockIndex* pindexLastGetBlocksBegin;
206 uint256 hashLastGetBlocksEnd;
207 int32_t nStartingHeight;
211 vector<CAddress> vAddrToSend;
212 set<CAddress> setAddrKnown;
214 set<uint256> setKnown;
215 uint256 hashCheckpointKnown; // ppcoin: known sent sync-checkpoint
216 int64_t nNextAddrSend;
217 int64_t nNextLocalAddrSend;
218 int64_t nNextInvSend;
220 // inventory based relay
221 mruset<CInv> setInventoryKnown;
222 vector<CInv> vInventoryToSend;
223 CCriticalSection cs_inventory;
224 multimap<int64_t, CInv> mapAskFor;
226 CNode(SOCKET hSocketIn, CAddress addrIn, string addrNameIn = "", bool fInboundIn=false);
230 // Network usage totals
231 static CCriticalSection cs_totalBytesRecv;
232 static CCriticalSection cs_totalBytesSent;
233 static uint64_t nTotalBytesRecv;
234 static uint64_t nTotalBytesSent;
236 void operator=(const CNode&);
241 CNode* AddRef(int64_t nTimeout=0);
243 void AddAddressKnown(const CAddress& addr);
244 void PushAddress(const CAddress& addr);
245 void AddInventoryKnown(const CInv& inv);
246 void PushInventory(const CInv& inv);
247 void AskFor(const CInv& inv);
248 void BeginMessage(const char* pszCommand);
251 void EndMessageAbortIfEmpty();
254 void PushMessage(const char* pszCommand)
258 BeginMessage(pszCommand);
268 template<typename T1>
269 void PushMessage(const char* pszCommand, const T1& a1)
273 BeginMessage(pszCommand);
284 template<typename T1, typename T2>
285 void PushMessage(const char* pszCommand, const T1& a1, const T2& a2)
289 BeginMessage(pszCommand);
300 template<typename T1, typename T2, typename T3>
301 void PushMessage(const char* pszCommand, const T1& a1, const T2& a2, const T3& a3)
305 BeginMessage(pszCommand);
306 vSend << a1 << a2 << a3;
316 template<typename T1, typename T2, typename T3, typename T4>
317 void PushMessage(const char* pszCommand, const T1& a1, const T2& a2, const T3& a3, const T4& a4)
321 BeginMessage(pszCommand);
322 vSend << a1 << a2 << a3 << a4;
332 template<typename T1, typename T2, typename T3, typename T4, typename T5>
333 void PushMessage(const char* pszCommand, const T1& a1, const T2& a2, const T3& a3, const T4& a4, const T5& a5)
337 BeginMessage(pszCommand);
338 vSend << a1 << a2 << a3 << a4 << a5;
348 template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
349 void PushMessage(const char* pszCommand, const T1& a1, const T2& a2, const T3& a3, const T4& a4, const T5& a5, const T6& a6)
353 BeginMessage(pszCommand);
354 vSend << a1 << a2 << a3 << a4 << a5 << a6;
364 template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7>
365 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)
369 BeginMessage(pszCommand);
370 vSend << a1 << a2 << a3 << a4 << a5 << a6 << a7;
380 template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8>
381 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)
385 BeginMessage(pszCommand);
386 vSend << a1 << a2 << a3 << a4 << a5 << a6 << a7 << a8;
396 template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9>
397 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)
401 BeginMessage(pszCommand);
402 vSend << a1 << a2 << a3 << a4 << a5 << a6 << a7 << a8 << a9;
413 void PushRequest(const char* pszCommand,
414 void (*fn)(void*, CDataStream&), void* param1)
417 RAND_bytes((unsigned char*)&hashReply, sizeof(hashReply));
420 LOCK(cs_mapRequests);
421 mapRequests[hashReply] = CRequestTracker(fn, param1);
424 PushMessage(pszCommand, hashReply);
427 template<typename T1>
428 void PushRequest(const char* pszCommand, const T1& a1,
429 void (*fn)(void*, CDataStream&), void* param1)
432 RAND_bytes((unsigned char*)&hashReply, sizeof(hashReply));
435 LOCK(cs_mapRequests);
436 mapRequests[hashReply] = CRequestTracker(fn, param1);
439 PushMessage(pszCommand, hashReply, a1);
442 template<typename T1, typename T2>
443 void PushRequest(const char* pszCommand, const T1& a1, const T2& a2,
444 void (*fn)(void*, CDataStream&), void* param1)
447 RAND_bytes((unsigned char*)&hashReply, sizeof(hashReply));
450 LOCK(cs_mapRequests);
451 mapRequests[hashReply] = CRequestTracker(fn, param1);
454 PushMessage(pszCommand, hashReply, a1, a2);
459 void PushGetBlocks(CBlockIndex* pindexBegin, uint256 hashEnd);
460 bool IsSubscribed(unsigned int nChannel);
461 void Subscribe(unsigned int nChannel, unsigned int nHops=0);
462 void CancelSubscribe(unsigned int nChannel);
463 void CloseSocketDisconnect();
467 // Denial-of-service detection/prevention
468 // The idea is to detect peers that are behaving
469 // badly and disconnect/ban them, but do it in a
470 // one-coding-mistake-won't-shatter-the-entire-network
472 // IMPORTANT: There should be nothing I can give a
473 // node that it will forward on that will make that
474 // node's peers drop it. If there is, an attacker
475 // can isolate a node and/or try to split the network.
476 // Dropping a node for sending stuff that is invalid
477 // now but might be valid in a later version is also
478 // dangerous, because it can cause a network split
479 // between nodes running old code and nodes running
481 static void ClearBanned(); // needed for unit testing
482 static bool IsBanned(CNetAddr ip);
483 bool Misbehaving(int howmuch); // 1 == a little, 100 == a lot
484 void copyStats(CNodeStats &stats);
486 static void RecordBytesRecv(uint64_t bytes);
487 static void RecordBytesSent(uint64_t bytes);
489 static uint64_t GetTotalBytesRecv();
490 static uint64_t GetTotalBytesSent();
494 void RelayTransaction(const CTransaction& tx, const uint256& hash);
495 void RelayTransaction(const CTransaction& tx, const uint256& hash, const CDataStream& ss);
498 /** Return a timestamp in the future (in microseconds) for exponentially distributed events. */
499 int64_t PoissonNextSend(int64_t nNow, int average_interval_seconds);