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;
137 struct LocalServiceInfo {
142 extern CCriticalSection cs_mapLocalHost;
143 extern std::map<CNetAddr, LocalServiceInfo> mapLocalHost;
151 int64_t nTimeConnected;
152 std::string addrName;
154 std::string strSubVer;
156 int64_t nReleaseTime;
157 int32_t nStartingHeight;
158 int32_t nMisbehavior;
165 // Information about a peer
176 CCriticalSection cs_vSend;
177 CCriticalSection cs_vRecv;
180 int64_t nLastSendEmpty;
181 int64_t nTimeConnected;
182 int32_t nHeaderStart;
183 uint32_t nMessageStart;
185 std::string addrName;
188 std::string strSubVer;
193 bool fSuccessfullyConnected;
195 CSemaphoreGrant grantOutbound;
199 // Denial-of-service detection/prevention
200 // Key is IP address, value is banned-until-time
201 static std::map<CNetAddr, int64_t> setBanned;
202 static CCriticalSection cs_setBanned;
206 int64_t nReleaseTime;
207 std::map<uint256, CRequestTracker> mapRequests;
208 CCriticalSection cs_mapRequests;
209 uint256 hashContinue;
210 CBlockIndex* pindexLastGetBlocksBegin;
211 uint256 hashLastGetBlocksEnd;
212 int32_t nStartingHeight;
216 std::vector<CAddress> vAddrToSend;
217 std::set<CAddress> setAddrKnown;
219 std::set<uint256> setKnown;
220 uint256 hashCheckpointKnown; // ppcoin: known sent sync-checkpoint
221 int64_t nNextAddrSend;
222 int64_t nNextLocalAddrSend;
223 int64_t nNextInvSend;
225 // inventory based relay
226 mruset<CInv> setInventoryKnown;
227 std::vector<CInv> vInventoryToSend;
228 CCriticalSection cs_inventory;
229 std::multimap<int64_t, CInv> mapAskFor;
231 CNode(SOCKET hSocketIn, CAddress addrIn, std::string addrNameIn = "", bool fInboundIn=false);
235 // Network usage totals
236 static CCriticalSection cs_totalBytesRecv;
237 static CCriticalSection cs_totalBytesSent;
238 static uint64_t nTotalBytesRecv;
239 static uint64_t nTotalBytesSent;
241 void operator=(const CNode&);
246 CNode* AddRef(int64_t nTimeout=0);
248 void AddAddressKnown(const CAddress& addr);
249 void PushAddress(const CAddress& addr);
250 void AddInventoryKnown(const CInv& inv);
251 void PushInventory(const CInv& inv);
252 void AskFor(const CInv& inv);
253 void BeginMessage(const char* pszCommand);
256 void EndMessageAbortIfEmpty();
259 void PushMessage(const char* pszCommand)
263 BeginMessage(pszCommand);
273 template<typename T1>
274 void PushMessage(const char* pszCommand, const T1& a1)
278 BeginMessage(pszCommand);
289 template<typename T1, typename T2>
290 void PushMessage(const char* pszCommand, const T1& a1, const T2& a2)
294 BeginMessage(pszCommand);
305 template<typename T1, typename T2, typename T3>
306 void PushMessage(const char* pszCommand, const T1& a1, const T2& a2, const T3& a3)
310 BeginMessage(pszCommand);
311 vSend << a1 << a2 << a3;
321 template<typename T1, typename T2, typename T3, typename T4>
322 void PushMessage(const char* pszCommand, const T1& a1, const T2& a2, const T3& a3, const T4& a4)
326 BeginMessage(pszCommand);
327 vSend << a1 << a2 << a3 << a4;
337 template<typename T1, typename T2, typename T3, typename T4, typename T5>
338 void PushMessage(const char* pszCommand, const T1& a1, const T2& a2, const T3& a3, const T4& a4, const T5& a5)
342 BeginMessage(pszCommand);
343 vSend << a1 << a2 << a3 << a4 << a5;
353 template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
354 void PushMessage(const char* pszCommand, const T1& a1, const T2& a2, const T3& a3, const T4& a4, const T5& a5, const T6& a6)
358 BeginMessage(pszCommand);
359 vSend << a1 << a2 << a3 << a4 << a5 << a6;
369 template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7>
370 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)
374 BeginMessage(pszCommand);
375 vSend << a1 << a2 << a3 << a4 << a5 << a6 << a7;
385 template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8>
386 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)
390 BeginMessage(pszCommand);
391 vSend << a1 << a2 << a3 << a4 << a5 << a6 << a7 << a8;
401 template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9>
402 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)
406 BeginMessage(pszCommand);
407 vSend << a1 << a2 << a3 << a4 << a5 << a6 << a7 << a8 << a9;
418 void PushRequest(const char* pszCommand,
419 void (*fn)(void*, CDataStream&), void* param1)
422 RAND_bytes((unsigned char*)&hashReply, sizeof(hashReply));
425 LOCK(cs_mapRequests);
426 mapRequests[hashReply] = CRequestTracker(fn, param1);
429 PushMessage(pszCommand, hashReply);
432 template<typename T1>
433 void PushRequest(const char* pszCommand, const T1& a1,
434 void (*fn)(void*, CDataStream&), void* param1)
437 RAND_bytes((unsigned char*)&hashReply, sizeof(hashReply));
440 LOCK(cs_mapRequests);
441 mapRequests[hashReply] = CRequestTracker(fn, param1);
444 PushMessage(pszCommand, hashReply, a1);
447 template<typename T1, typename T2>
448 void PushRequest(const char* pszCommand, const T1& a1, const T2& a2,
449 void (*fn)(void*, CDataStream&), void* param1)
452 RAND_bytes((unsigned char*)&hashReply, sizeof(hashReply));
455 LOCK(cs_mapRequests);
456 mapRequests[hashReply] = CRequestTracker(fn, param1);
459 PushMessage(pszCommand, hashReply, a1, a2);
464 void PushGetBlocks(CBlockIndex* pindexBegin, uint256 hashEnd);
465 bool IsSubscribed(unsigned int nChannel);
466 void Subscribe(unsigned int nChannel, unsigned int nHops=0);
467 void CancelSubscribe(unsigned int nChannel);
468 void CloseSocketDisconnect();
472 // Denial-of-service detection/prevention
473 // The idea is to detect peers that are behaving
474 // badly and disconnect/ban them, but do it in a
475 // one-coding-mistake-won't-shatter-the-entire-network
477 // IMPORTANT: There should be nothing I can give a
478 // node that it will forward on that will make that
479 // node's peers drop it. If there is, an attacker
480 // can isolate a node and/or try to split the network.
481 // Dropping a node for sending stuff that is invalid
482 // now but might be valid in a later version is also
483 // dangerous, because it can cause a network split
484 // between nodes running old code and nodes running
486 static void ClearBanned(); // needed for unit testing
487 static bool IsBanned(const CNetAddr& ip);
488 bool Misbehaving(int howmuch); // 1 == a little, 100 == a lot
489 void copyStats(CNodeStats &stats);
491 static void RecordBytesRecv(uint64_t bytes);
492 static void RecordBytesSent(uint64_t bytes);
494 static uint64_t GetTotalBytesRecv();
495 static uint64_t GetTotalBytesSent();
499 void RelayTransaction(const CTransaction& tx, const uint256& hash);
500 void RelayTransaction(const CTransaction& tx, const uint256& hash, const CDataStream& ss);
503 /** Return a timestamp in the future (in microseconds) for exponentially distributed events. */
504 int64_t PoissonNextSend(int64_t nNow, int average_interval_seconds);