You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

net.h 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2011 The Bitcoin developers
  3. // Distributed under the MIT/X11 software license, see the accompanying
  4. // file license.txt or http://www.opensource.org/licenses/mit-license.php.
  5. #ifndef BITCOIN_NET_H
  6. #define BITCOIN_NET_H
  7. #include <deque>
  8. #include <boost/array.hpp>
  9. #include <boost/foreach.hpp>
  10. #include <openssl/rand.h>
  11. #ifndef WIN32
  12. #include <arpa/inet.h>
  13. #endif
  14. #include "netbase.h"
  15. #include "protocol.h"
  16. class CAddrDB;
  17. class CRequestTracker;
  18. class CNode;
  19. class CBlockIndex;
  20. extern int nBestHeight;
  21. inline unsigned int ReceiveBufferSize() { return 1000*GetArg("-maxreceivebuffer", 10*1000); }
  22. inline unsigned int SendBufferSize() { return 1000*GetArg("-maxsendbuffer", 10*1000); }
  23. static const unsigned int PUBLISH_HOPS = 5;
  24. bool GetMyExternalIP(CNetAddr& ipRet);
  25. bool AddAddress(CAddress addr, int64 nTimePenalty=0, CAddrDB *pAddrDB=NULL);
  26. void AddressCurrentlyConnected(const CService& addr);
  27. CNode* FindNode(const CNetAddr& ip);
  28. CNode* FindNode(const CService& ip);
  29. CNode* ConnectNode(CAddress addrConnect, int64 nTimeout=0);
  30. void AbandonRequests(void (*fn)(void*, CDataStream&), void* param1);
  31. bool AnySubscribed(unsigned int nChannel);
  32. void MapPort(bool fMapPort);
  33. bool BindListenPort(std::string& strError=REF(std::string()));
  34. void StartNode(void* parg);
  35. bool StopNode();
  36. enum
  37. {
  38. MSG_TX = 1,
  39. MSG_BLOCK,
  40. };
  41. class CRequestTracker
  42. {
  43. public:
  44. void (*fn)(void*, CDataStream&);
  45. void* param1;
  46. explicit CRequestTracker(void (*fnIn)(void*, CDataStream&)=NULL, void* param1In=NULL)
  47. {
  48. fn = fnIn;
  49. param1 = param1In;
  50. }
  51. bool IsNull()
  52. {
  53. return fn == NULL;
  54. }
  55. };
  56. extern bool fClient;
  57. extern bool fAllowDNS;
  58. extern uint64 nLocalServices;
  59. extern CAddress addrLocalHost;
  60. extern uint64 nLocalHostNonce;
  61. extern boost::array<int, 10> vnThreadsRunning;
  62. extern std::vector<CNode*> vNodes;
  63. extern CCriticalSection cs_vNodes;
  64. extern std::map<std::vector<unsigned char>, CAddress> mapAddresses;
  65. extern CCriticalSection cs_mapAddresses;
  66. extern std::map<CInv, CDataStream> mapRelay;
  67. extern std::deque<std::pair<int64, CInv> > vRelayExpiration;
  68. extern CCriticalSection cs_mapRelay;
  69. extern std::map<CInv, int64> mapAlreadyAskedFor;
  70. class CNode
  71. {
  72. public:
  73. // socket
  74. uint64 nServices;
  75. SOCKET hSocket;
  76. CDataStream vSend;
  77. CDataStream vRecv;
  78. CCriticalSection cs_vSend;
  79. CCriticalSection cs_vRecv;
  80. int64 nLastSend;
  81. int64 nLastRecv;
  82. int64 nLastSendEmpty;
  83. int64 nTimeConnected;
  84. unsigned int nHeaderStart;
  85. unsigned int nMessageStart;
  86. CAddress addr;
  87. int nVersion;
  88. std::string strSubVer;
  89. bool fClient;
  90. bool fInbound;
  91. bool fNetworkNode;
  92. bool fSuccessfullyConnected;
  93. bool fDisconnect;
  94. protected:
  95. int nRefCount;
  96. // Denial-of-service detection/prevention
  97. // Key is ip address, value is banned-until-time
  98. static std::map<CNetAddr, int64> setBanned;
  99. static CCriticalSection cs_setBanned;
  100. int nMisbehavior;
  101. public:
  102. int64 nReleaseTime;
  103. std::map<uint256, CRequestTracker> mapRequests;
  104. CCriticalSection cs_mapRequests;
  105. uint256 hashContinue;
  106. CBlockIndex* pindexLastGetBlocksBegin;
  107. uint256 hashLastGetBlocksEnd;
  108. int nStartingHeight;
  109. // flood relay
  110. std::vector<CAddress> vAddrToSend;
  111. std::set<CAddress> setAddrKnown;
  112. bool fGetAddr;
  113. std::set<uint256> setKnown;
  114. // inventory based relay
  115. std::set<CInv> setInventoryKnown;
  116. std::vector<CInv> vInventoryToSend;
  117. CCriticalSection cs_inventory;
  118. std::multimap<int64, CInv> mapAskFor;
  119. // publish and subscription
  120. std::vector<char> vfSubscribe;
  121. CNode(SOCKET hSocketIn, CAddress addrIn, bool fInboundIn=false)
  122. {
  123. nServices = 0;
  124. hSocket = hSocketIn;
  125. vSend.SetType(SER_NETWORK);
  126. vSend.SetVersion(0);
  127. vRecv.SetType(SER_NETWORK);
  128. vRecv.SetVersion(0);
  129. // Version 0.2 obsoletes 20 Feb 2012
  130. if (GetTime() > 1329696000)
  131. {
  132. vSend.SetVersion(209);
  133. vRecv.SetVersion(209);
  134. }
  135. nLastSend = 0;
  136. nLastRecv = 0;
  137. nLastSendEmpty = GetTime();
  138. nTimeConnected = GetTime();
  139. nHeaderStart = -1;
  140. nMessageStart = -1;
  141. addr = addrIn;
  142. nVersion = 0;
  143. strSubVer = "";
  144. fClient = false; // set by version message
  145. fInbound = fInboundIn;
  146. fNetworkNode = false;
  147. fSuccessfullyConnected = false;
  148. fDisconnect = false;
  149. nRefCount = 0;
  150. nReleaseTime = 0;
  151. hashContinue = 0;
  152. pindexLastGetBlocksBegin = 0;
  153. hashLastGetBlocksEnd = 0;
  154. nStartingHeight = -1;
  155. fGetAddr = false;
  156. vfSubscribe.assign(256, false);
  157. nMisbehavior = 0;
  158. // Be shy and don't send version until we hear
  159. if (!fInbound)
  160. PushVersion();
  161. }
  162. ~CNode()
  163. {
  164. if (hSocket != INVALID_SOCKET)
  165. {
  166. closesocket(hSocket);
  167. hSocket = INVALID_SOCKET;
  168. }
  169. }
  170. private:
  171. CNode(const CNode&);
  172. void operator=(const CNode&);
  173. public:
  174. int GetRefCount()
  175. {
  176. return std::max(nRefCount, 0) + (GetTime() < nReleaseTime ? 1 : 0);
  177. }
  178. CNode* AddRef(int64 nTimeout=0)
  179. {
  180. if (nTimeout != 0)
  181. nReleaseTime = std::max(nReleaseTime, GetTime() + nTimeout);
  182. else
  183. nRefCount++;
  184. return this;
  185. }
  186. void Release()
  187. {
  188. nRefCount--;
  189. }
  190. void AddAddressKnown(const CAddress& addr)
  191. {
  192. setAddrKnown.insert(addr);
  193. }
  194. void PushAddress(const CAddress& addr)
  195. {
  196. // Known checking here is only to save space from duplicates.
  197. // SendMessages will filter it again for knowns that were added
  198. // after addresses were pushed.
  199. if (addr.IsValid() && !setAddrKnown.count(addr))
  200. vAddrToSend.push_back(addr);
  201. }
  202. void AddInventoryKnown(const CInv& inv)
  203. {
  204. CRITICAL_BLOCK(cs_inventory)
  205. setInventoryKnown.insert(inv);
  206. }
  207. void PushInventory(const CInv& inv)
  208. {
  209. CRITICAL_BLOCK(cs_inventory)
  210. if (!setInventoryKnown.count(inv))
  211. vInventoryToSend.push_back(inv);
  212. }
  213. void AskFor(const CInv& inv)
  214. {
  215. // We're using mapAskFor as a priority queue,
  216. // the key is the earliest time the request can be sent
  217. int64& nRequestTime = mapAlreadyAskedFor[inv];
  218. printf("askfor %s %"PRI64d"\n", inv.ToString().c_str(), nRequestTime);
  219. // Make sure not to reuse time indexes to keep things in the same order
  220. int64 nNow = (GetTime() - 1) * 1000000;
  221. static int64 nLastTime;
  222. ++nLastTime;
  223. nNow = std::max(nNow, nLastTime);
  224. nLastTime = nNow;
  225. // Each retry is 2 minutes after the last
  226. nRequestTime = std::max(nRequestTime + 2 * 60 * 1000000, nNow);
  227. mapAskFor.insert(std::make_pair(nRequestTime, inv));
  228. }
  229. void BeginMessage(const char* pszCommand)
  230. {
  231. cs_vSend.Enter("cs_vSend", __FILE__, __LINE__);
  232. if (nHeaderStart != -1)
  233. AbortMessage();
  234. nHeaderStart = vSend.size();
  235. vSend << CMessageHeader(pszCommand, 0);
  236. nMessageStart = vSend.size();
  237. if (fDebug) {
  238. printf("%s ", DateTimeStrFormat("%x %H:%M:%S", GetTime()).c_str());
  239. printf("sending: %s ", pszCommand);
  240. }
  241. }
  242. void AbortMessage()
  243. {
  244. if (nHeaderStart == -1)
  245. return;
  246. vSend.resize(nHeaderStart);
  247. nHeaderStart = -1;
  248. nMessageStart = -1;
  249. cs_vSend.Leave();
  250. if (fDebug)
  251. printf("(aborted)\n");
  252. }
  253. void EndMessage()
  254. {
  255. if (mapArgs.count("-dropmessagestest") && GetRand(atoi(mapArgs["-dropmessagestest"])) == 0)
  256. {
  257. printf("dropmessages DROPPING SEND MESSAGE\n");
  258. AbortMessage();
  259. return;
  260. }
  261. if (nHeaderStart == -1)
  262. return;
  263. // Set the size
  264. unsigned int nSize = vSend.size() - nMessageStart;
  265. memcpy((char*)&vSend[nHeaderStart] + offsetof(CMessageHeader, nMessageSize), &nSize, sizeof(nSize));
  266. // Set the checksum
  267. if (vSend.GetVersion() >= 209)
  268. {
  269. uint256 hash = Hash(vSend.begin() + nMessageStart, vSend.end());
  270. unsigned int nChecksum = 0;
  271. memcpy(&nChecksum, &hash, sizeof(nChecksum));
  272. assert(nMessageStart - nHeaderStart >= offsetof(CMessageHeader, nChecksum) + sizeof(nChecksum));
  273. memcpy((char*)&vSend[nHeaderStart] + offsetof(CMessageHeader, nChecksum), &nChecksum, sizeof(nChecksum));
  274. }
  275. if (fDebug) {
  276. printf("(%d bytes)\n", nSize);
  277. }
  278. nHeaderStart = -1;
  279. nMessageStart = -1;
  280. cs_vSend.Leave();
  281. }
  282. void EndMessageAbortIfEmpty()
  283. {
  284. if (nHeaderStart == -1)
  285. return;
  286. int nSize = vSend.size() - nMessageStart;
  287. if (nSize > 0)
  288. EndMessage();
  289. else
  290. AbortMessage();
  291. }
  292. void PushVersion();
  293. void PushMessage(const char* pszCommand)
  294. {
  295. try
  296. {
  297. BeginMessage(pszCommand);
  298. EndMessage();
  299. }
  300. catch (...)
  301. {
  302. AbortMessage();
  303. throw;
  304. }
  305. }
  306. template<typename T1>
  307. void PushMessage(const char* pszCommand, const T1& a1)
  308. {
  309. try
  310. {
  311. BeginMessage(pszCommand);
  312. vSend << a1;
  313. EndMessage();
  314. }
  315. catch (...)
  316. {
  317. AbortMessage();
  318. throw;
  319. }
  320. }
  321. template<typename T1, typename T2>
  322. void PushMessage(const char* pszCommand, const T1& a1, const T2& a2)
  323. {
  324. try
  325. {
  326. BeginMessage(pszCommand);
  327. vSend << a1 << a2;
  328. EndMessage();
  329. }
  330. catch (...)
  331. {
  332. AbortMessage();
  333. throw;
  334. }
  335. }
  336. template<typename T1, typename T2, typename T3>
  337. void PushMessage(const char* pszCommand, const T1& a1, const T2& a2, const T3& a3)
  338. {
  339. try
  340. {
  341. BeginMessage(pszCommand);
  342. vSend << a1 << a2 << a3;
  343. EndMessage();
  344. }
  345. catch (...)
  346. {
  347. AbortMessage();
  348. throw;
  349. }
  350. }
  351. template<typename T1, typename T2, typename T3, typename T4>
  352. void PushMessage(const char* pszCommand, const T1& a1, const T2& a2, const T3& a3, const T4& a4)
  353. {
  354. try
  355. {
  356. BeginMessage(pszCommand);
  357. vSend << a1 << a2 << a3 << a4;
  358. EndMessage();
  359. }
  360. catch (...)
  361. {
  362. AbortMessage();
  363. throw;
  364. }
  365. }
  366. template<typename T1, typename T2, typename T3, typename T4, typename T5>
  367. void PushMessage(const char* pszCommand, const T1& a1, const T2& a2, const T3& a3, const T4& a4, const T5& a5)
  368. {
  369. try
  370. {
  371. BeginMessage(pszCommand);
  372. vSend << a1 << a2 << a3 << a4 << a5;
  373. EndMessage();
  374. }
  375. catch (...)
  376. {
  377. AbortMessage();
  378. throw;
  379. }
  380. }
  381. template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
  382. void PushMessage(const char* pszCommand, const T1& a1, const T2& a2, const T3& a3, const T4& a4, const T5& a5, const T6& a6)
  383. {
  384. try
  385. {
  386. BeginMessage(pszCommand);
  387. vSend << a1 << a2 << a3 << a4 << a5 << a6;
  388. EndMessage();
  389. }
  390. catch (...)
  391. {
  392. AbortMessage();
  393. throw;
  394. }
  395. }
  396. template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7>
  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)
  398. {
  399. try
  400. {
  401. BeginMessage(pszCommand);
  402. vSend << a1 << a2 << a3 << a4 << a5 << a6 << a7;
  403. EndMessage();
  404. }
  405. catch (...)
  406. {
  407. AbortMessage();
  408. throw;
  409. }
  410. }
  411. template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8>
  412. 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)
  413. {
  414. try
  415. {
  416. BeginMessage(pszCommand);
  417. vSend << a1 << a2 << a3 << a4 << a5 << a6 << a7 << a8;
  418. EndMessage();
  419. }
  420. catch (...)
  421. {
  422. AbortMessage();
  423. throw;
  424. }
  425. }
  426. template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9>
  427. 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)
  428. {
  429. try
  430. {
  431. BeginMessage(pszCommand);
  432. vSend << a1 << a2 << a3 << a4 << a5 << a6 << a7 << a8 << a9;
  433. EndMessage();
  434. }
  435. catch (...)
  436. {
  437. AbortMessage();
  438. throw;
  439. }
  440. }
  441. void PushRequest(const char* pszCommand,
  442. void (*fn)(void*, CDataStream&), void* param1)
  443. {
  444. uint256 hashReply;
  445. RAND_bytes((unsigned char*)&hashReply, sizeof(hashReply));
  446. CRITICAL_BLOCK(cs_mapRequests)
  447. mapRequests[hashReply] = CRequestTracker(fn, param1);
  448. PushMessage(pszCommand, hashReply);
  449. }
  450. template<typename T1>
  451. void PushRequest(const char* pszCommand, const T1& a1,
  452. void (*fn)(void*, CDataStream&), void* param1)
  453. {
  454. uint256 hashReply;
  455. RAND_bytes((unsigned char*)&hashReply, sizeof(hashReply));
  456. CRITICAL_BLOCK(cs_mapRequests)
  457. mapRequests[hashReply] = CRequestTracker(fn, param1);
  458. PushMessage(pszCommand, hashReply, a1);
  459. }
  460. template<typename T1, typename T2>
  461. void PushRequest(const char* pszCommand, const T1& a1, const T2& a2,
  462. void (*fn)(void*, CDataStream&), void* param1)
  463. {
  464. uint256 hashReply;
  465. RAND_bytes((unsigned char*)&hashReply, sizeof(hashReply));
  466. CRITICAL_BLOCK(cs_mapRequests)
  467. mapRequests[hashReply] = CRequestTracker(fn, param1);
  468. PushMessage(pszCommand, hashReply, a1, a2);
  469. }
  470. void PushGetBlocks(CBlockIndex* pindexBegin, uint256 hashEnd);
  471. bool IsSubscribed(unsigned int nChannel);
  472. void Subscribe(unsigned int nChannel, unsigned int nHops=0);
  473. void CancelSubscribe(unsigned int nChannel);
  474. void CloseSocketDisconnect();
  475. void Cleanup();
  476. // Denial-of-service detection/prevention
  477. // The idea is to detect peers that are behaving
  478. // badly and disconnect/ban them, but do it in a
  479. // one-coding-mistake-won't-shatter-the-entire-network
  480. // way.
  481. // IMPORTANT: There should be nothing I can give a
  482. // node that it will forward on that will make that
  483. // node's peers drop it. If there is, an attacker
  484. // can isolate a node and/or try to split the network.
  485. // Dropping a node for sending stuff that is invalid
  486. // now but might be valid in a later version is also
  487. // dangerous, because it can cause a network split
  488. // between nodes running old code and nodes running
  489. // new code.
  490. static void ClearBanned(); // needed for unit testing
  491. static bool IsBanned(CNetAddr ip);
  492. bool Misbehaving(int howmuch); // 1 == a little, 100 == a lot
  493. };
  494. inline void RelayInventory(const CInv& inv)
  495. {
  496. // Put on lists to offer to the other nodes
  497. CRITICAL_BLOCK(cs_vNodes)
  498. BOOST_FOREACH(CNode* pnode, vNodes)
  499. pnode->PushInventory(inv);
  500. }
  501. template<typename T>
  502. void RelayMessage(const CInv& inv, const T& a)
  503. {
  504. CDataStream ss(SER_NETWORK);
  505. ss.reserve(10000);
  506. ss << a;
  507. RelayMessage(inv, ss);
  508. }
  509. template<>
  510. inline void RelayMessage<>(const CInv& inv, const CDataStream& ss)
  511. {
  512. CRITICAL_BLOCK(cs_mapRelay)
  513. {
  514. // Expire old relay messages
  515. while (!vRelayExpiration.empty() && vRelayExpiration.front().first < GetTime())
  516. {
  517. mapRelay.erase(vRelayExpiration.front().second);
  518. vRelayExpiration.pop_front();
  519. }
  520. // Save original serialized message so newer versions are preserved
  521. mapRelay[inv] = ss;
  522. vRelayExpiration.push_back(std::make_pair(GetTime() + 15 * 60, inv));
  523. }
  524. RelayInventory(inv);
  525. }
  526. //
  527. // Templates for the publish and subscription system.
  528. // The object being published as T& obj needs to have:
  529. // a set<unsigned int> setSources member
  530. // specializations of AdvertInsert and AdvertErase
  531. // Currently implemented for CTable and CProduct.
  532. //
  533. template<typename T>
  534. void AdvertStartPublish(CNode* pfrom, unsigned int nChannel, unsigned int nHops, T& obj)
  535. {
  536. // Add to sources
  537. obj.setSources.insert(pfrom->addr.ip);
  538. if (!AdvertInsert(obj))
  539. return;
  540. // Relay
  541. CRITICAL_BLOCK(cs_vNodes)
  542. BOOST_FOREACH(CNode* pnode, vNodes)
  543. if (pnode != pfrom && (nHops < PUBLISH_HOPS || pnode->IsSubscribed(nChannel)))
  544. pnode->PushMessage("publish", nChannel, nHops, obj);
  545. }
  546. template<typename T>
  547. void AdvertStopPublish(CNode* pfrom, unsigned int nChannel, unsigned int nHops, T& obj)
  548. {
  549. uint256 hash = obj.GetHash();
  550. CRITICAL_BLOCK(cs_vNodes)
  551. BOOST_FOREACH(CNode* pnode, vNodes)
  552. if (pnode != pfrom && (nHops < PUBLISH_HOPS || pnode->IsSubscribed(nChannel)))
  553. pnode->PushMessage("pub-cancel", nChannel, nHops, hash);
  554. AdvertErase(obj);
  555. }
  556. template<typename T>
  557. void AdvertRemoveSource(CNode* pfrom, unsigned int nChannel, unsigned int nHops, T& obj)
  558. {
  559. // Remove a source
  560. obj.setSources.erase(pfrom->addr.ip);
  561. // If no longer supported by any sources, cancel it
  562. if (obj.setSources.empty())
  563. AdvertStopPublish(pfrom, nChannel, nHops, obj);
  564. }
  565. #endif