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.cpp 81KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2015 The Bitcoin Core developers
  3. // Distributed under the MIT software license, see the accompanying
  4. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  5. #if defined(HAVE_CONFIG_H)
  6. #include "config/bitcoin-config.h"
  7. #endif
  8. #include "net.h"
  9. #include "addrman.h"
  10. #include "chainparams.h"
  11. #include "clientversion.h"
  12. #include "consensus/consensus.h"
  13. #include "crypto/common.h"
  14. #include "hash.h"
  15. #include "primitives/transaction.h"
  16. #include "scheduler.h"
  17. #include "ui_interface.h"
  18. #include "utilstrencodings.h"
  19. #ifdef WIN32
  20. #include <string.h>
  21. #else
  22. #include <fcntl.h>
  23. #endif
  24. #ifdef USE_UPNP
  25. #include <miniupnpc/miniupnpc.h>
  26. #include <miniupnpc/miniwget.h>
  27. #include <miniupnpc/upnpcommands.h>
  28. #include <miniupnpc/upnperrors.h>
  29. #endif
  30. #include <boost/filesystem.hpp>
  31. #include <boost/thread.hpp>
  32. #include <math.h>
  33. // Dump addresses to peers.dat and banlist.dat every 15 minutes (900s)
  34. #define DUMP_ADDRESSES_INTERVAL 900
  35. #if !defined(HAVE_MSG_NOSIGNAL) && !defined(MSG_NOSIGNAL)
  36. #define MSG_NOSIGNAL 0
  37. #endif
  38. // Fix for ancient MinGW versions, that don't have defined these in ws2tcpip.h.
  39. // Todo: Can be removed when our pull-tester is upgraded to a modern MinGW version.
  40. #ifdef WIN32
  41. #ifndef PROTECTION_LEVEL_UNRESTRICTED
  42. #define PROTECTION_LEVEL_UNRESTRICTED 10
  43. #endif
  44. #ifndef IPV6_PROTECTION_LEVEL
  45. #define IPV6_PROTECTION_LEVEL 23
  46. #endif
  47. #endif
  48. namespace {
  49. const int MAX_OUTBOUND_CONNECTIONS = 8;
  50. struct ListenSocket {
  51. SOCKET socket;
  52. bool whitelisted;
  53. ListenSocket(SOCKET socket, bool whitelisted) : socket(socket), whitelisted(whitelisted) {}
  54. };
  55. }
  56. const static std::string NET_MESSAGE_COMMAND_OTHER = "*other*";
  57. //
  58. // Global state variables
  59. //
  60. bool fDiscover = true;
  61. bool fListen = true;
  62. uint64_t nLocalServices = NODE_NETWORK;
  63. CCriticalSection cs_mapLocalHost;
  64. std::map<CNetAddr, LocalServiceInfo> mapLocalHost;
  65. static bool vfLimited[NET_MAX] = {};
  66. static CNode* pnodeLocalHost = NULL;
  67. uint64_t nLocalHostNonce = 0;
  68. static std::vector<ListenSocket> vhListenSocket;
  69. CAddrMan addrman;
  70. int nMaxConnections = DEFAULT_MAX_PEER_CONNECTIONS;
  71. bool fAddressesInitialized = false;
  72. std::string strSubVersion;
  73. std::vector<CNode*> vNodes;
  74. CCriticalSection cs_vNodes;
  75. std::map<uint256, CTransaction> mapRelay;
  76. std::deque<std::pair<int64_t, uint256> > vRelayExpiration;
  77. CCriticalSection cs_mapRelay;
  78. limitedmap<uint256, int64_t> mapAlreadyAskedFor(MAX_INV_SZ);
  79. static std::deque<std::string> vOneShots;
  80. CCriticalSection cs_vOneShots;
  81. std::vector<std::string> vAddedNodes;
  82. CCriticalSection cs_vAddedNodes;
  83. NodeId nLastNodeId = 0;
  84. CCriticalSection cs_nLastNodeId;
  85. static CSemaphore *semOutbound = NULL;
  86. boost::condition_variable messageHandlerCondition;
  87. // Signals for message handling
  88. static CNodeSignals g_signals;
  89. CNodeSignals& GetNodeSignals() { return g_signals; }
  90. void AddOneShot(const std::string& strDest)
  91. {
  92. LOCK(cs_vOneShots);
  93. vOneShots.push_back(strDest);
  94. }
  95. unsigned short GetListenPort()
  96. {
  97. return (unsigned short)(GetArg("-port", Params().GetDefaultPort()));
  98. }
  99. // find 'best' local address for a particular peer
  100. bool GetLocal(CService& addr, const CNetAddr *paddrPeer)
  101. {
  102. if (!fListen)
  103. return false;
  104. int nBestScore = -1;
  105. int nBestReachability = -1;
  106. {
  107. LOCK(cs_mapLocalHost);
  108. for (std::map<CNetAddr, LocalServiceInfo>::iterator it = mapLocalHost.begin(); it != mapLocalHost.end(); it++)
  109. {
  110. int nScore = (*it).second.nScore;
  111. int nReachability = (*it).first.GetReachabilityFrom(paddrPeer);
  112. if (nReachability > nBestReachability || (nReachability == nBestReachability && nScore > nBestScore))
  113. {
  114. addr = CService((*it).first, (*it).second.nPort);
  115. nBestReachability = nReachability;
  116. nBestScore = nScore;
  117. }
  118. }
  119. }
  120. return nBestScore >= 0;
  121. }
  122. //! Convert the pnSeeds6 array into usable address objects.
  123. static std::vector<CAddress> convertSeed6(const std::vector<SeedSpec6> &vSeedsIn)
  124. {
  125. // It'll only connect to one or two seed nodes because once it connects,
  126. // it'll get a pile of addresses with newer timestamps.
  127. // Seed nodes are given a random 'last seen time' of between one and two
  128. // weeks ago.
  129. const int64_t nOneWeek = 7*24*60*60;
  130. std::vector<CAddress> vSeedsOut;
  131. vSeedsOut.reserve(vSeedsIn.size());
  132. for (std::vector<SeedSpec6>::const_iterator i(vSeedsIn.begin()); i != vSeedsIn.end(); ++i)
  133. {
  134. struct in6_addr ip;
  135. memcpy(&ip, i->addr, sizeof(ip));
  136. CAddress addr(CService(ip, i->port));
  137. addr.nTime = GetTime() - GetRand(nOneWeek) - nOneWeek;
  138. vSeedsOut.push_back(addr);
  139. }
  140. return vSeedsOut;
  141. }
  142. // get best local address for a particular peer as a CAddress
  143. // Otherwise, return the unroutable 0.0.0.0 but filled in with
  144. // the normal parameters, since the IP may be changed to a useful
  145. // one by discovery.
  146. CAddress GetLocalAddress(const CNetAddr *paddrPeer)
  147. {
  148. CAddress ret(CService("0.0.0.0",GetListenPort()),0);
  149. CService addr;
  150. if (GetLocal(addr, paddrPeer))
  151. {
  152. ret = CAddress(addr);
  153. }
  154. ret.nServices = nLocalServices;
  155. ret.nTime = GetAdjustedTime();
  156. return ret;
  157. }
  158. int GetnScore(const CService& addr)
  159. {
  160. LOCK(cs_mapLocalHost);
  161. if (mapLocalHost.count(addr) == LOCAL_NONE)
  162. return 0;
  163. return mapLocalHost[addr].nScore;
  164. }
  165. // Is our peer's addrLocal potentially useful as an external IP source?
  166. bool IsPeerAddrLocalGood(CNode *pnode)
  167. {
  168. return fDiscover && pnode->addr.IsRoutable() && pnode->addrLocal.IsRoutable() &&
  169. !IsLimited(pnode->addrLocal.GetNetwork());
  170. }
  171. // pushes our own address to a peer
  172. void AdvertiseLocal(CNode *pnode)
  173. {
  174. if (fListen && pnode->fSuccessfullyConnected)
  175. {
  176. CAddress addrLocal = GetLocalAddress(&pnode->addr);
  177. // If discovery is enabled, sometimes give our peer the address it
  178. // tells us that it sees us as in case it has a better idea of our
  179. // address than we do.
  180. if (IsPeerAddrLocalGood(pnode) && (!addrLocal.IsRoutable() ||
  181. GetRand((GetnScore(addrLocal) > LOCAL_MANUAL) ? 8:2) == 0))
  182. {
  183. addrLocal.SetIP(pnode->addrLocal);
  184. }
  185. if (addrLocal.IsRoutable())
  186. {
  187. LogPrintf("AdvertiseLocal: advertising address %s\n", addrLocal.ToString());
  188. pnode->PushAddress(addrLocal);
  189. }
  190. }
  191. }
  192. // learn a new local address
  193. bool AddLocal(const CService& addr, int nScore)
  194. {
  195. if (!addr.IsRoutable())
  196. return false;
  197. if (!fDiscover && nScore < LOCAL_MANUAL)
  198. return false;
  199. if (IsLimited(addr))
  200. return false;
  201. LogPrintf("AddLocal(%s,%i)\n", addr.ToString(), nScore);
  202. {
  203. LOCK(cs_mapLocalHost);
  204. bool fAlready = mapLocalHost.count(addr) > 0;
  205. LocalServiceInfo &info = mapLocalHost[addr];
  206. if (!fAlready || nScore >= info.nScore) {
  207. info.nScore = nScore + (fAlready ? 1 : 0);
  208. info.nPort = addr.GetPort();
  209. }
  210. }
  211. return true;
  212. }
  213. bool AddLocal(const CNetAddr &addr, int nScore)
  214. {
  215. return AddLocal(CService(addr, GetListenPort()), nScore);
  216. }
  217. bool RemoveLocal(const CService& addr)
  218. {
  219. LOCK(cs_mapLocalHost);
  220. LogPrintf("RemoveLocal(%s)\n", addr.ToString());
  221. mapLocalHost.erase(addr);
  222. return true;
  223. }
  224. /** Make a particular network entirely off-limits (no automatic connects to it) */
  225. void SetLimited(enum Network net, bool fLimited)
  226. {
  227. if (net == NET_UNROUTABLE)
  228. return;
  229. LOCK(cs_mapLocalHost);
  230. vfLimited[net] = fLimited;
  231. }
  232. bool IsLimited(enum Network net)
  233. {
  234. LOCK(cs_mapLocalHost);
  235. return vfLimited[net];
  236. }
  237. bool IsLimited(const CNetAddr &addr)
  238. {
  239. return IsLimited(addr.GetNetwork());
  240. }
  241. /** vote for a local address */
  242. bool SeenLocal(const CService& addr)
  243. {
  244. {
  245. LOCK(cs_mapLocalHost);
  246. if (mapLocalHost.count(addr) == 0)
  247. return false;
  248. mapLocalHost[addr].nScore++;
  249. }
  250. return true;
  251. }
  252. /** check whether a given address is potentially local */
  253. bool IsLocal(const CService& addr)
  254. {
  255. LOCK(cs_mapLocalHost);
  256. return mapLocalHost.count(addr) > 0;
  257. }
  258. /** check whether a given network is one we can probably connect to */
  259. bool IsReachable(enum Network net)
  260. {
  261. LOCK(cs_mapLocalHost);
  262. return !vfLimited[net];
  263. }
  264. /** check whether a given address is in a network we can probably connect to */
  265. bool IsReachable(const CNetAddr& addr)
  266. {
  267. enum Network net = addr.GetNetwork();
  268. return IsReachable(net);
  269. }
  270. void AddressCurrentlyConnected(const CService& addr)
  271. {
  272. addrman.Connected(addr);
  273. }
  274. uint64_t CNode::nTotalBytesRecv = 0;
  275. uint64_t CNode::nTotalBytesSent = 0;
  276. CCriticalSection CNode::cs_totalBytesRecv;
  277. CCriticalSection CNode::cs_totalBytesSent;
  278. uint64_t CNode::nMaxOutboundLimit = 0;
  279. uint64_t CNode::nMaxOutboundTotalBytesSentInCycle = 0;
  280. uint64_t CNode::nMaxOutboundTimeframe = 60*60*24; //1 day
  281. uint64_t CNode::nMaxOutboundCycleStartTime = 0;
  282. CNode* FindNode(const CNetAddr& ip)
  283. {
  284. LOCK(cs_vNodes);
  285. BOOST_FOREACH(CNode* pnode, vNodes)
  286. if ((CNetAddr)pnode->addr == ip)
  287. return (pnode);
  288. return NULL;
  289. }
  290. CNode* FindNode(const CSubNet& subNet)
  291. {
  292. LOCK(cs_vNodes);
  293. BOOST_FOREACH(CNode* pnode, vNodes)
  294. if (subNet.Match((CNetAddr)pnode->addr))
  295. return (pnode);
  296. return NULL;
  297. }
  298. CNode* FindNode(const std::string& addrName)
  299. {
  300. LOCK(cs_vNodes);
  301. BOOST_FOREACH(CNode* pnode, vNodes)
  302. if (pnode->addrName == addrName)
  303. return (pnode);
  304. return NULL;
  305. }
  306. CNode* FindNode(const CService& addr)
  307. {
  308. LOCK(cs_vNodes);
  309. BOOST_FOREACH(CNode* pnode, vNodes)
  310. if ((CService)pnode->addr == addr)
  311. return (pnode);
  312. return NULL;
  313. }
  314. CNode* ConnectNode(CAddress addrConnect, const char *pszDest)
  315. {
  316. if (pszDest == NULL) {
  317. if (IsLocal(addrConnect))
  318. return NULL;
  319. // Look for an existing connection
  320. CNode* pnode = FindNode((CService)addrConnect);
  321. if (pnode)
  322. {
  323. pnode->AddRef();
  324. return pnode;
  325. }
  326. }
  327. /// debug print
  328. LogPrint("net", "trying connection %s lastseen=%.1fhrs\n",
  329. pszDest ? pszDest : addrConnect.ToString(),
  330. pszDest ? 0.0 : (double)(GetAdjustedTime() - addrConnect.nTime)/3600.0);
  331. // Connect
  332. SOCKET hSocket;
  333. bool proxyConnectionFailed = false;
  334. if (pszDest ? ConnectSocketByName(addrConnect, hSocket, pszDest, Params().GetDefaultPort(), nConnectTimeout, &proxyConnectionFailed) :
  335. ConnectSocket(addrConnect, hSocket, nConnectTimeout, &proxyConnectionFailed))
  336. {
  337. if (!IsSelectableSocket(hSocket)) {
  338. LogPrintf("Cannot create connection: non-selectable socket created (fd >= FD_SETSIZE ?)\n");
  339. CloseSocket(hSocket);
  340. return NULL;
  341. }
  342. addrman.Attempt(addrConnect);
  343. // Add node
  344. CNode* pnode = new CNode(hSocket, addrConnect, pszDest ? pszDest : "", false);
  345. pnode->AddRef();
  346. {
  347. LOCK(cs_vNodes);
  348. vNodes.push_back(pnode);
  349. }
  350. pnode->nTimeConnected = GetTime();
  351. return pnode;
  352. } else if (!proxyConnectionFailed) {
  353. // If connecting to the node failed, and failure is not caused by a problem connecting to
  354. // the proxy, mark this as an attempt.
  355. addrman.Attempt(addrConnect);
  356. }
  357. return NULL;
  358. }
  359. static void DumpBanlist()
  360. {
  361. CNode::SweepBanned(); // clean unused entries (if bantime has expired)
  362. if (!CNode::BannedSetIsDirty())
  363. return;
  364. int64_t nStart = GetTimeMillis();
  365. CBanDB bandb;
  366. banmap_t banmap;
  367. CNode::SetBannedSetDirty(false);
  368. CNode::GetBanned(banmap);
  369. if (!bandb.Write(banmap))
  370. CNode::SetBannedSetDirty(true);
  371. LogPrint("net", "Flushed %d banned node ips/subnets to banlist.dat %dms\n",
  372. banmap.size(), GetTimeMillis() - nStart);
  373. }
  374. void CNode::CloseSocketDisconnect()
  375. {
  376. fDisconnect = true;
  377. if (hSocket != INVALID_SOCKET)
  378. {
  379. LogPrint("net", "disconnecting peer=%d\n", id);
  380. CloseSocket(hSocket);
  381. }
  382. // in case this fails, we'll empty the recv buffer when the CNode is deleted
  383. TRY_LOCK(cs_vRecvMsg, lockRecv);
  384. if (lockRecv)
  385. vRecvMsg.clear();
  386. }
  387. void CNode::PushVersion()
  388. {
  389. int nBestHeight = GetNodeSignals().GetHeight().get_value_or(0);
  390. int64_t nTime = (fInbound ? GetAdjustedTime() : GetTime());
  391. CAddress addrYou = (addr.IsRoutable() && !IsProxy(addr) ? addr : CAddress(CService("0.0.0.0",0)));
  392. CAddress addrMe = GetLocalAddress(&addr);
  393. GetRandBytes((unsigned char*)&nLocalHostNonce, sizeof(nLocalHostNonce));
  394. if (fLogIPs)
  395. LogPrint("net", "send version message: version %d, blocks=%d, us=%s, them=%s, peer=%d\n", PROTOCOL_VERSION, nBestHeight, addrMe.ToString(), addrYou.ToString(), id);
  396. else
  397. LogPrint("net", "send version message: version %d, blocks=%d, us=%s, peer=%d\n", PROTOCOL_VERSION, nBestHeight, addrMe.ToString(), id);
  398. PushMessage(NetMsgType::VERSION, PROTOCOL_VERSION, nLocalServices, nTime, addrYou, addrMe,
  399. nLocalHostNonce, strSubVersion, nBestHeight, !GetBoolArg("-blocksonly", DEFAULT_BLOCKSONLY));
  400. }
  401. banmap_t CNode::setBanned;
  402. CCriticalSection CNode::cs_setBanned;
  403. bool CNode::setBannedIsDirty;
  404. void CNode::ClearBanned()
  405. {
  406. {
  407. LOCK(cs_setBanned);
  408. setBanned.clear();
  409. setBannedIsDirty = true;
  410. }
  411. DumpBanlist(); //store banlist to disk
  412. uiInterface.BannedListChanged();
  413. }
  414. bool CNode::IsBanned(CNetAddr ip)
  415. {
  416. bool fResult = false;
  417. {
  418. LOCK(cs_setBanned);
  419. for (banmap_t::iterator it = setBanned.begin(); it != setBanned.end(); it++)
  420. {
  421. CSubNet subNet = (*it).first;
  422. CBanEntry banEntry = (*it).second;
  423. if(subNet.Match(ip) && GetTime() < banEntry.nBanUntil)
  424. fResult = true;
  425. }
  426. }
  427. return fResult;
  428. }
  429. bool CNode::IsBanned(CSubNet subnet)
  430. {
  431. bool fResult = false;
  432. {
  433. LOCK(cs_setBanned);
  434. banmap_t::iterator i = setBanned.find(subnet);
  435. if (i != setBanned.end())
  436. {
  437. CBanEntry banEntry = (*i).second;
  438. if (GetTime() < banEntry.nBanUntil)
  439. fResult = true;
  440. }
  441. }
  442. return fResult;
  443. }
  444. void CNode::Ban(const CNetAddr& addr, const BanReason &banReason, int64_t bantimeoffset, bool sinceUnixEpoch) {
  445. CSubNet subNet(addr);
  446. Ban(subNet, banReason, bantimeoffset, sinceUnixEpoch);
  447. }
  448. void CNode::Ban(const CSubNet& subNet, const BanReason &banReason, int64_t bantimeoffset, bool sinceUnixEpoch) {
  449. CBanEntry banEntry(GetTime());
  450. banEntry.banReason = banReason;
  451. if (bantimeoffset <= 0)
  452. {
  453. bantimeoffset = GetArg("-bantime", DEFAULT_MISBEHAVING_BANTIME);
  454. sinceUnixEpoch = false;
  455. }
  456. banEntry.nBanUntil = (sinceUnixEpoch ? 0 : GetTime() )+bantimeoffset;
  457. {
  458. LOCK(cs_setBanned);
  459. if (setBanned[subNet].nBanUntil < banEntry.nBanUntil) {
  460. setBanned[subNet] = banEntry;
  461. setBannedIsDirty = true;
  462. }
  463. else
  464. return;
  465. }
  466. uiInterface.BannedListChanged();
  467. {
  468. LOCK(cs_vNodes);
  469. BOOST_FOREACH(CNode* pnode, vNodes) {
  470. if (subNet.Match((CNetAddr)pnode->addr))
  471. pnode->fDisconnect = true;
  472. }
  473. }
  474. if(banReason == BanReasonManuallyAdded)
  475. DumpBanlist(); //store banlist to disk immediately if user requested ban
  476. }
  477. bool CNode::Unban(const CNetAddr &addr) {
  478. CSubNet subNet(addr);
  479. return Unban(subNet);
  480. }
  481. bool CNode::Unban(const CSubNet &subNet) {
  482. {
  483. LOCK(cs_setBanned);
  484. if (!setBanned.erase(subNet))
  485. return false;
  486. setBannedIsDirty = true;
  487. }
  488. uiInterface.BannedListChanged();
  489. DumpBanlist(); //store banlist to disk immediately
  490. return true;
  491. }
  492. void CNode::GetBanned(banmap_t &banMap)
  493. {
  494. LOCK(cs_setBanned);
  495. banMap = setBanned; //create a thread safe copy
  496. }
  497. void CNode::SetBanned(const banmap_t &banMap)
  498. {
  499. LOCK(cs_setBanned);
  500. setBanned = banMap;
  501. setBannedIsDirty = true;
  502. }
  503. void CNode::SweepBanned()
  504. {
  505. int64_t now = GetTime();
  506. LOCK(cs_setBanned);
  507. banmap_t::iterator it = setBanned.begin();
  508. while(it != setBanned.end())
  509. {
  510. CSubNet subNet = (*it).first;
  511. CBanEntry banEntry = (*it).second;
  512. if(now > banEntry.nBanUntil)
  513. {
  514. setBanned.erase(it++);
  515. setBannedIsDirty = true;
  516. LogPrint("net", "%s: Removed banned node ip/subnet from banlist.dat: %s\n", __func__, subNet.ToString());
  517. }
  518. else
  519. ++it;
  520. }
  521. }
  522. bool CNode::BannedSetIsDirty()
  523. {
  524. LOCK(cs_setBanned);
  525. return setBannedIsDirty;
  526. }
  527. void CNode::SetBannedSetDirty(bool dirty)
  528. {
  529. LOCK(cs_setBanned); //reuse setBanned lock for the isDirty flag
  530. setBannedIsDirty = dirty;
  531. }
  532. std::vector<CSubNet> CNode::vWhitelistedRange;
  533. CCriticalSection CNode::cs_vWhitelistedRange;
  534. bool CNode::IsWhitelistedRange(const CNetAddr &addr) {
  535. LOCK(cs_vWhitelistedRange);
  536. BOOST_FOREACH(const CSubNet& subnet, vWhitelistedRange) {
  537. if (subnet.Match(addr))
  538. return true;
  539. }
  540. return false;
  541. }
  542. void CNode::AddWhitelistedRange(const CSubNet &subnet) {
  543. LOCK(cs_vWhitelistedRange);
  544. vWhitelistedRange.push_back(subnet);
  545. }
  546. #undef X
  547. #define X(name) stats.name = name
  548. void CNode::copyStats(CNodeStats &stats)
  549. {
  550. stats.nodeid = this->GetId();
  551. X(nServices);
  552. X(fRelayTxes);
  553. X(nLastSend);
  554. X(nLastRecv);
  555. X(nTimeConnected);
  556. X(nTimeOffset);
  557. X(addrName);
  558. X(nVersion);
  559. X(cleanSubVer);
  560. X(fInbound);
  561. X(nStartingHeight);
  562. X(nSendBytes);
  563. X(mapSendBytesPerMsgCmd);
  564. X(nRecvBytes);
  565. X(mapRecvBytesPerMsgCmd);
  566. X(fWhitelisted);
  567. // It is common for nodes with good ping times to suddenly become lagged,
  568. // due to a new block arriving or other large transfer.
  569. // Merely reporting pingtime might fool the caller into thinking the node was still responsive,
  570. // since pingtime does not update until the ping is complete, which might take a while.
  571. // So, if a ping is taking an unusually long time in flight,
  572. // the caller can immediately detect that this is happening.
  573. int64_t nPingUsecWait = 0;
  574. if ((0 != nPingNonceSent) && (0 != nPingUsecStart)) {
  575. nPingUsecWait = GetTimeMicros() - nPingUsecStart;
  576. }
  577. // Raw ping time is in microseconds, but show it to user as whole seconds (Bitcoin users should be well used to small numbers with many decimal places by now :)
  578. stats.dPingTime = (((double)nPingUsecTime) / 1e6);
  579. stats.dPingMin = (((double)nMinPingUsecTime) / 1e6);
  580. stats.dPingWait = (((double)nPingUsecWait) / 1e6);
  581. // Leave string empty if addrLocal invalid (not filled in yet)
  582. stats.addrLocal = addrLocal.IsValid() ? addrLocal.ToString() : "";
  583. }
  584. #undef X
  585. // requires LOCK(cs_vRecvMsg)
  586. bool CNode::ReceiveMsgBytes(const char *pch, unsigned int nBytes)
  587. {
  588. while (nBytes > 0) {
  589. // get current incomplete message, or create a new one
  590. if (vRecvMsg.empty() ||
  591. vRecvMsg.back().complete())
  592. vRecvMsg.push_back(CNetMessage(Params().MessageStart(), SER_NETWORK, nRecvVersion));
  593. CNetMessage& msg = vRecvMsg.back();
  594. // absorb network data
  595. int handled;
  596. if (!msg.in_data)
  597. handled = msg.readHeader(pch, nBytes);
  598. else
  599. handled = msg.readData(pch, nBytes);
  600. if (handled < 0)
  601. return false;
  602. if (msg.in_data && msg.hdr.nMessageSize > MAX_PROTOCOL_MESSAGE_LENGTH) {
  603. LogPrint("net", "Oversized message from peer=%i, disconnecting\n", GetId());
  604. return false;
  605. }
  606. pch += handled;
  607. nBytes -= handled;
  608. if (msg.complete()) {
  609. //store received bytes per message command
  610. //to prevent a memory DOS, only allow valid commands
  611. mapMsgCmdSize::iterator i = mapRecvBytesPerMsgCmd.find(msg.hdr.pchCommand);
  612. if (i == mapRecvBytesPerMsgCmd.end())
  613. i = mapRecvBytesPerMsgCmd.find(NET_MESSAGE_COMMAND_OTHER);
  614. assert(i != mapRecvBytesPerMsgCmd.end());
  615. i->second += msg.hdr.nMessageSize + CMessageHeader::HEADER_SIZE;
  616. msg.nTime = GetTimeMicros();
  617. messageHandlerCondition.notify_one();
  618. }
  619. }
  620. return true;
  621. }
  622. int CNetMessage::readHeader(const char *pch, unsigned int nBytes)
  623. {
  624. // copy data to temporary parsing buffer
  625. unsigned int nRemaining = 24 - nHdrPos;
  626. unsigned int nCopy = std::min(nRemaining, nBytes);
  627. memcpy(&hdrbuf[nHdrPos], pch, nCopy);
  628. nHdrPos += nCopy;
  629. // if header incomplete, exit
  630. if (nHdrPos < 24)
  631. return nCopy;
  632. // deserialize to CMessageHeader
  633. try {
  634. hdrbuf >> hdr;
  635. }
  636. catch (const std::exception&) {
  637. return -1;
  638. }
  639. // reject messages larger than MAX_SIZE
  640. if (hdr.nMessageSize > MAX_SIZE)
  641. return -1;
  642. // switch state to reading message data
  643. in_data = true;
  644. return nCopy;
  645. }
  646. int CNetMessage::readData(const char *pch, unsigned int nBytes)
  647. {
  648. unsigned int nRemaining = hdr.nMessageSize - nDataPos;
  649. unsigned int nCopy = std::min(nRemaining, nBytes);
  650. if (vRecv.size() < nDataPos + nCopy) {
  651. // Allocate up to 256 KiB ahead, but never more than the total message size.
  652. vRecv.resize(std::min(hdr.nMessageSize, nDataPos + nCopy + 256 * 1024));
  653. }
  654. memcpy(&vRecv[nDataPos], pch, nCopy);
  655. nDataPos += nCopy;
  656. return nCopy;
  657. }
  658. // requires LOCK(cs_vSend)
  659. void SocketSendData(CNode *pnode)
  660. {
  661. std::deque<CSerializeData>::iterator it = pnode->vSendMsg.begin();
  662. while (it != pnode->vSendMsg.end()) {
  663. const CSerializeData &data = *it;
  664. assert(data.size() > pnode->nSendOffset);
  665. int nBytes = send(pnode->hSocket, &data[pnode->nSendOffset], data.size() - pnode->nSendOffset, MSG_NOSIGNAL | MSG_DONTWAIT);
  666. if (nBytes > 0) {
  667. pnode->nLastSend = GetTime();
  668. pnode->nSendBytes += nBytes;
  669. pnode->nSendOffset += nBytes;
  670. pnode->RecordBytesSent(nBytes);
  671. if (pnode->nSendOffset == data.size()) {
  672. pnode->nSendOffset = 0;
  673. pnode->nSendSize -= data.size();
  674. it++;
  675. } else {
  676. // could not send full message; stop sending more
  677. break;
  678. }
  679. } else {
  680. if (nBytes < 0) {
  681. // error
  682. int nErr = WSAGetLastError();
  683. if (nErr != WSAEWOULDBLOCK && nErr != WSAEMSGSIZE && nErr != WSAEINTR && nErr != WSAEINPROGRESS)
  684. {
  685. LogPrintf("socket send error %s\n", NetworkErrorString(nErr));
  686. pnode->CloseSocketDisconnect();
  687. }
  688. }
  689. // couldn't send anything at all
  690. break;
  691. }
  692. }
  693. if (it == pnode->vSendMsg.end()) {
  694. assert(pnode->nSendOffset == 0);
  695. assert(pnode->nSendSize == 0);
  696. }
  697. pnode->vSendMsg.erase(pnode->vSendMsg.begin(), it);
  698. }
  699. static std::list<CNode*> vNodesDisconnected;
  700. struct NodeEvictionCandidate
  701. {
  702. NodeId id;
  703. int64_t nTimeConnected;
  704. int64_t nMinPingUsecTime;
  705. CAddress addr;
  706. };
  707. static bool ReverseCompareNodeMinPingTime(const NodeEvictionCandidate &a, const NodeEvictionCandidate &b)
  708. {
  709. return a.nMinPingUsecTime > b.nMinPingUsecTime;
  710. }
  711. static bool ReverseCompareNodeTimeConnected(const NodeEvictionCandidate &a, const NodeEvictionCandidate &b)
  712. {
  713. return a.nTimeConnected > b.nTimeConnected;
  714. }
  715. class CompareNetGroupKeyed
  716. {
  717. std::vector<unsigned char> vchSecretKey;
  718. public:
  719. CompareNetGroupKeyed()
  720. {
  721. vchSecretKey.resize(32, 0);
  722. GetRandBytes(vchSecretKey.data(), vchSecretKey.size());
  723. }
  724. bool operator()(const NodeEvictionCandidate &a, const NodeEvictionCandidate &b)
  725. {
  726. std::vector<unsigned char> vchGroupA, vchGroupB;
  727. CSHA256 hashA, hashB;
  728. std::vector<unsigned char> vchA(32), vchB(32);
  729. vchGroupA = a.addr.GetGroup();
  730. vchGroupB = b.addr.GetGroup();
  731. hashA.Write(begin_ptr(vchGroupA), vchGroupA.size());
  732. hashB.Write(begin_ptr(vchGroupB), vchGroupB.size());
  733. hashA.Write(begin_ptr(vchSecretKey), vchSecretKey.size());
  734. hashB.Write(begin_ptr(vchSecretKey), vchSecretKey.size());
  735. hashA.Finalize(begin_ptr(vchA));
  736. hashB.Finalize(begin_ptr(vchB));
  737. return vchA < vchB;
  738. }
  739. };
  740. /** Try to find a connection to evict when the node is full.
  741. * Extreme care must be taken to avoid opening the node to attacker
  742. * triggered network partitioning.
  743. * The strategy used here is to protect a small number of peers
  744. * for each of several distinct characteristics which are difficult
  745. * to forge. In order to partition a node the attacker must be
  746. * simultaneously better at all of them than honest peers.
  747. */
  748. static bool AttemptToEvictConnection(bool fPreferNewConnection) {
  749. std::vector<NodeEvictionCandidate> vEvictionCandidates;
  750. {
  751. LOCK(cs_vNodes);
  752. BOOST_FOREACH(CNode *node, vNodes) {
  753. if (node->fWhitelisted)
  754. continue;
  755. if (!node->fInbound)
  756. continue;
  757. if (node->fDisconnect)
  758. continue;
  759. NodeEvictionCandidate candidate = {node->id, node->nTimeConnected, node->nMinPingUsecTime, node->addr};
  760. vEvictionCandidates.push_back(candidate);
  761. }
  762. }
  763. if (vEvictionCandidates.empty()) return false;
  764. // Protect connections with certain characteristics
  765. // Deterministically select 4 peers to protect by netgroup.
  766. // An attacker cannot predict which netgroups will be protected.
  767. static CompareNetGroupKeyed comparerNetGroupKeyed;
  768. std::sort(vEvictionCandidates.begin(), vEvictionCandidates.end(), comparerNetGroupKeyed);
  769. vEvictionCandidates.erase(vEvictionCandidates.end() - std::min(4, static_cast<int>(vEvictionCandidates.size())), vEvictionCandidates.end());
  770. if (vEvictionCandidates.empty()) return false;
  771. // Protect the 8 nodes with the lowest minimum ping time.
  772. // An attacker cannot manipulate this metric without physically moving nodes closer to the target.
  773. std::sort(vEvictionCandidates.begin(), vEvictionCandidates.end(), ReverseCompareNodeMinPingTime);
  774. vEvictionCandidates.erase(vEvictionCandidates.end() - std::min(8, static_cast<int>(vEvictionCandidates.size())), vEvictionCandidates.end());
  775. if (vEvictionCandidates.empty()) return false;
  776. // Protect the half of the remaining nodes which have been connected the longest.
  777. // This replicates the non-eviction implicit behavior, and precludes attacks that start later.
  778. std::sort(vEvictionCandidates.begin(), vEvictionCandidates.end(), ReverseCompareNodeTimeConnected);
  779. vEvictionCandidates.erase(vEvictionCandidates.end() - static_cast<int>(vEvictionCandidates.size() / 2), vEvictionCandidates.end());
  780. if (vEvictionCandidates.empty()) return false;
  781. // Identify the network group with the most connections and youngest member.
  782. // (vEvictionCandidates is already sorted by reverse connect time)
  783. std::vector<unsigned char> naMostConnections;
  784. unsigned int nMostConnections = 0;
  785. int64_t nMostConnectionsTime = 0;
  786. std::map<std::vector<unsigned char>, std::vector<NodeEvictionCandidate> > mapAddrCounts;
  787. BOOST_FOREACH(const NodeEvictionCandidate &node, vEvictionCandidates) {
  788. mapAddrCounts[node.addr.GetGroup()].push_back(node);
  789. int64_t grouptime = mapAddrCounts[node.addr.GetGroup()][0].nTimeConnected;
  790. size_t groupsize = mapAddrCounts[node.addr.GetGroup()].size();
  791. if (groupsize > nMostConnections || (groupsize == nMostConnections && grouptime > nMostConnectionsTime)) {
  792. nMostConnections = groupsize;
  793. nMostConnectionsTime = grouptime;
  794. naMostConnections = node.addr.GetGroup();
  795. }
  796. }
  797. // Reduce to the network group with the most connections
  798. vEvictionCandidates = mapAddrCounts[naMostConnections];
  799. // Do not disconnect peers if there is only one unprotected connection from their network group.
  800. // This step excessively favors netgroup diversity, and should be removed once more protective criteria are established.
  801. if (vEvictionCandidates.size() <= 1)
  802. // unless we prefer the new connection (for whitelisted peers)
  803. if (!fPreferNewConnection)
  804. return false;
  805. // Disconnect from the network group with the most connections
  806. NodeId evicted = vEvictionCandidates.front().id;
  807. LOCK(cs_vNodes);
  808. for(std::vector<CNode*>::const_iterator it(vNodes.begin()); it != vNodes.end(); ++it) {
  809. if ((*it)->GetId() == evicted) {
  810. (*it)->fDisconnect = true;
  811. return true;
  812. }
  813. }
  814. return false;
  815. }
  816. static void AcceptConnection(const ListenSocket& hListenSocket) {
  817. struct sockaddr_storage sockaddr;
  818. socklen_t len = sizeof(sockaddr);
  819. SOCKET hSocket = accept(hListenSocket.socket, (struct sockaddr*)&sockaddr, &len);
  820. CAddress addr;
  821. int nInbound = 0;
  822. int nMaxInbound = nMaxConnections - MAX_OUTBOUND_CONNECTIONS;
  823. if (hSocket != INVALID_SOCKET)
  824. if (!addr.SetSockAddr((const struct sockaddr*)&sockaddr))
  825. LogPrintf("Warning: Unknown socket family\n");
  826. bool whitelisted = hListenSocket.whitelisted || CNode::IsWhitelistedRange(addr);
  827. {
  828. LOCK(cs_vNodes);
  829. BOOST_FOREACH(CNode* pnode, vNodes)
  830. if (pnode->fInbound)
  831. nInbound++;
  832. }
  833. if (hSocket == INVALID_SOCKET)
  834. {
  835. int nErr = WSAGetLastError();
  836. if (nErr != WSAEWOULDBLOCK)
  837. LogPrintf("socket error accept failed: %s\n", NetworkErrorString(nErr));
  838. return;
  839. }
  840. if (!IsSelectableSocket(hSocket))
  841. {
  842. LogPrintf("connection from %s dropped: non-selectable socket\n", addr.ToString());
  843. CloseSocket(hSocket);
  844. return;
  845. }
  846. // According to the internet TCP_NODELAY is not carried into accepted sockets
  847. // on all platforms. Set it again here just to be sure.
  848. int set = 1;
  849. #ifdef WIN32
  850. setsockopt(hSocket, IPPROTO_TCP, TCP_NODELAY, (const char*)&set, sizeof(int));
  851. #else
  852. setsockopt(hSocket, IPPROTO_TCP, TCP_NODELAY, (void*)&set, sizeof(int));
  853. #endif
  854. if (CNode::IsBanned(addr) && !whitelisted)
  855. {
  856. LogPrintf("connection from %s dropped (banned)\n", addr.ToString());
  857. CloseSocket(hSocket);
  858. return;
  859. }
  860. if (nInbound >= nMaxInbound)
  861. {
  862. if (!AttemptToEvictConnection(whitelisted)) {
  863. // No connection to evict, disconnect the new connection
  864. LogPrint("net", "failed to find an eviction candidate - connection dropped (full)\n");
  865. CloseSocket(hSocket);
  866. return;
  867. }
  868. }
  869. CNode* pnode = new CNode(hSocket, addr, "", true);
  870. pnode->AddRef();
  871. pnode->fWhitelisted = whitelisted;
  872. LogPrint("net", "connection from %s accepted\n", addr.ToString());
  873. {
  874. LOCK(cs_vNodes);
  875. vNodes.push_back(pnode);
  876. }
  877. }
  878. void ThreadSocketHandler()
  879. {
  880. unsigned int nPrevNodeCount = 0;
  881. while (true)
  882. {
  883. //
  884. // Disconnect nodes
  885. //
  886. {
  887. LOCK(cs_vNodes);
  888. // Disconnect unused nodes
  889. std::vector<CNode*> vNodesCopy = vNodes;
  890. BOOST_FOREACH(CNode* pnode, vNodesCopy)
  891. {
  892. if (pnode->fDisconnect ||
  893. (pnode->GetRefCount() <= 0 && pnode->vRecvMsg.empty() && pnode->nSendSize == 0 && pnode->ssSend.empty()))
  894. {
  895. // remove from vNodes
  896. vNodes.erase(remove(vNodes.begin(), vNodes.end(), pnode), vNodes.end());
  897. // release outbound grant (if any)
  898. pnode->grantOutbound.Release();
  899. // close socket and cleanup
  900. pnode->CloseSocketDisconnect();
  901. // hold in disconnected pool until all refs are released
  902. if (pnode->fNetworkNode || pnode->fInbound)
  903. pnode->Release();
  904. vNodesDisconnected.push_back(pnode);
  905. }
  906. }
  907. }
  908. {
  909. // Delete disconnected nodes
  910. std::list<CNode*> vNodesDisconnectedCopy = vNodesDisconnected;
  911. BOOST_FOREACH(CNode* pnode, vNodesDisconnectedCopy)
  912. {
  913. // wait until threads are done using it
  914. if (pnode->GetRefCount() <= 0)
  915. {
  916. bool fDelete = false;
  917. {
  918. TRY_LOCK(pnode->cs_vSend, lockSend);
  919. if (lockSend)
  920. {
  921. TRY_LOCK(pnode->cs_vRecvMsg, lockRecv);
  922. if (lockRecv)
  923. {
  924. TRY_LOCK(pnode->cs_inventory, lockInv);
  925. if (lockInv)
  926. fDelete = true;
  927. }
  928. }
  929. }
  930. if (fDelete)
  931. {
  932. vNodesDisconnected.remove(pnode);
  933. delete pnode;
  934. }
  935. }
  936. }
  937. }
  938. if(vNodes.size() != nPrevNodeCount) {
  939. nPrevNodeCount = vNodes.size();
  940. uiInterface.NotifyNumConnectionsChanged(nPrevNodeCount);
  941. }
  942. //
  943. // Find which sockets have data to receive
  944. //
  945. struct timeval timeout;
  946. timeout.tv_sec = 0;
  947. timeout.tv_usec = 50000; // frequency to poll pnode->vSend
  948. fd_set fdsetRecv;
  949. fd_set fdsetSend;
  950. fd_set fdsetError;
  951. FD_ZERO(&fdsetRecv);
  952. FD_ZERO(&fdsetSend);
  953. FD_ZERO(&fdsetError);
  954. SOCKET hSocketMax = 0;
  955. bool have_fds = false;
  956. BOOST_FOREACH(const ListenSocket& hListenSocket, vhListenSocket) {
  957. FD_SET(hListenSocket.socket, &fdsetRecv);
  958. hSocketMax = std::max(hSocketMax, hListenSocket.socket);
  959. have_fds = true;
  960. }
  961. {
  962. LOCK(cs_vNodes);
  963. BOOST_FOREACH(CNode* pnode, vNodes)
  964. {
  965. if (pnode->hSocket == INVALID_SOCKET)
  966. continue;
  967. FD_SET(pnode->hSocket, &fdsetError);
  968. hSocketMax = std::max(hSocketMax, pnode->hSocket);
  969. have_fds = true;
  970. // Implement the following logic:
  971. // * If there is data to send, select() for sending data. As this only
  972. // happens when optimistic write failed, we choose to first drain the
  973. // write buffer in this case before receiving more. This avoids
  974. // needlessly queueing received data, if the remote peer is not themselves
  975. // receiving data. This means properly utilizing TCP flow control signalling.
  976. // * Otherwise, if there is no (complete) message in the receive buffer,
  977. // or there is space left in the buffer, select() for receiving data.
  978. // * (if neither of the above applies, there is certainly one message
  979. // in the receiver buffer ready to be processed).
  980. // Together, that means that at least one of the following is always possible,
  981. // so we don't deadlock:
  982. // * We send some data.
  983. // * We wait for data to be received (and disconnect after timeout).
  984. // * We process a message in the buffer (message handler thread).
  985. {
  986. TRY_LOCK(pnode->cs_vSend, lockSend);
  987. if (lockSend && !pnode->vSendMsg.empty()) {
  988. FD_SET(pnode->hSocket, &fdsetSend);
  989. continue;
  990. }
  991. }
  992. {
  993. TRY_LOCK(pnode->cs_vRecvMsg, lockRecv);
  994. if (lockRecv && (
  995. pnode->vRecvMsg.empty() || !pnode->vRecvMsg.front().complete() ||
  996. pnode->GetTotalRecvSize() <= ReceiveFloodSize()))
  997. FD_SET(pnode->hSocket, &fdsetRecv);
  998. }
  999. }
  1000. }
  1001. int nSelect = select(have_fds ? hSocketMax + 1 : 0,
  1002. &fdsetRecv, &fdsetSend, &fdsetError, &timeout);
  1003. boost::this_thread::interruption_point();
  1004. if (nSelect == SOCKET_ERROR)
  1005. {
  1006. if (have_fds)
  1007. {
  1008. int nErr = WSAGetLastError();
  1009. LogPrintf("socket select error %s\n", NetworkErrorString(nErr));
  1010. for (unsigned int i = 0; i <= hSocketMax; i++)
  1011. FD_SET(i, &fdsetRecv);
  1012. }
  1013. FD_ZERO(&fdsetSend);
  1014. FD_ZERO(&fdsetError);
  1015. MilliSleep(timeout.tv_usec/1000);
  1016. }
  1017. //
  1018. // Accept new connections
  1019. //
  1020. BOOST_FOREACH(const ListenSocket& hListenSocket, vhListenSocket)
  1021. {
  1022. if (hListenSocket.socket != INVALID_SOCKET && FD_ISSET(hListenSocket.socket, &fdsetRecv))
  1023. {
  1024. AcceptConnection(hListenSocket);
  1025. }
  1026. }
  1027. //
  1028. // Service each socket
  1029. //
  1030. std::vector<CNode*> vNodesCopy;
  1031. {
  1032. LOCK(cs_vNodes);
  1033. vNodesCopy = vNodes;
  1034. BOOST_FOREACH(CNode* pnode, vNodesCopy)
  1035. pnode->AddRef();
  1036. }
  1037. BOOST_FOREACH(CNode* pnode, vNodesCopy)
  1038. {
  1039. boost::this_thread::interruption_point();
  1040. //
  1041. // Receive
  1042. //
  1043. if (pnode->hSocket == INVALID_SOCKET)
  1044. continue;
  1045. if (FD_ISSET(pnode->hSocket, &fdsetRecv) || FD_ISSET(pnode->hSocket, &fdsetError))
  1046. {
  1047. TRY_LOCK(pnode->cs_vRecvMsg, lockRecv);
  1048. if (lockRecv)
  1049. {
  1050. {
  1051. // typical socket buffer is 8K-64K
  1052. char pchBuf[0x10000];
  1053. int nBytes = recv(pnode->hSocket, pchBuf, sizeof(pchBuf), MSG_DONTWAIT);
  1054. if (nBytes > 0)
  1055. {
  1056. if (!pnode->ReceiveMsgBytes(pchBuf, nBytes))
  1057. pnode->CloseSocketDisconnect();
  1058. pnode->nLastRecv = GetTime();
  1059. pnode->nRecvBytes += nBytes;
  1060. pnode->RecordBytesRecv(nBytes);
  1061. }
  1062. else if (nBytes == 0)
  1063. {
  1064. // socket closed gracefully
  1065. if (!pnode->fDisconnect)
  1066. LogPrint("net", "socket closed\n");
  1067. pnode->CloseSocketDisconnect();
  1068. }
  1069. else if (nBytes < 0)
  1070. {
  1071. // error
  1072. int nErr = WSAGetLastError();
  1073. if (nErr != WSAEWOULDBLOCK && nErr != WSAEMSGSIZE && nErr != WSAEINTR && nErr != WSAEINPROGRESS)
  1074. {
  1075. if (!pnode->fDisconnect)
  1076. LogPrintf("socket recv error %s\n", NetworkErrorString(nErr));
  1077. pnode->CloseSocketDisconnect();
  1078. }
  1079. }
  1080. }
  1081. }
  1082. }
  1083. //
  1084. // Send
  1085. //
  1086. if (pnode->hSocket == INVALID_SOCKET)
  1087. continue;
  1088. if (FD_ISSET(pnode->hSocket, &fdsetSend))
  1089. {
  1090. TRY_LOCK(pnode->cs_vSend, lockSend);
  1091. if (lockSend)
  1092. SocketSendData(pnode);
  1093. }
  1094. //
  1095. // Inactivity checking
  1096. //
  1097. int64_t nTime = GetTime();
  1098. if (nTime - pnode->nTimeConnected > 60)
  1099. {
  1100. if (pnode->nLastRecv == 0 || pnode->nLastSend == 0)
  1101. {
  1102. LogPrint("net", "socket no message in first 60 seconds, %d %d from %d\n", pnode->nLastRecv != 0, pnode->nLastSend != 0, pnode->id);
  1103. pnode->fDisconnect = true;
  1104. }
  1105. else if (nTime - pnode->nLastSend > TIMEOUT_INTERVAL)
  1106. {
  1107. LogPrintf("socket sending timeout: %is\n", nTime - pnode->nLastSend);
  1108. pnode->fDisconnect = true;
  1109. }
  1110. else if (nTime - pnode->nLastRecv > (pnode->nVersion > BIP0031_VERSION ? TIMEOUT_INTERVAL : 90*60))
  1111. {
  1112. LogPrintf("socket receive timeout: %is\n", nTime - pnode->nLastRecv);
  1113. pnode->fDisconnect = true;
  1114. }
  1115. else if (pnode->nPingNonceSent && pnode->nPingUsecStart + TIMEOUT_INTERVAL * 1000000 < GetTimeMicros())
  1116. {
  1117. LogPrintf("ping timeout: %fs\n", 0.000001 * (GetTimeMicros() - pnode->nPingUsecStart));
  1118. pnode->fDisconnect = true;
  1119. }
  1120. }
  1121. }
  1122. {
  1123. LOCK(cs_vNodes);
  1124. BOOST_FOREACH(CNode* pnode, vNodesCopy)
  1125. pnode->Release();
  1126. }
  1127. }
  1128. }
  1129. #ifdef USE_UPNP
  1130. void ThreadMapPort()
  1131. {
  1132. std::string port = strprintf("%u", GetListenPort());
  1133. const char * multicastif = 0;
  1134. const char * minissdpdpath = 0;
  1135. struct UPNPDev * devlist = 0;
  1136. char lanaddr[64];
  1137. #ifndef UPNPDISCOVER_SUCCESS
  1138. /* miniupnpc 1.5 */
  1139. devlist = upnpDiscover(2000, multicastif, minissdpdpath, 0);
  1140. #elif MINIUPNPC_API_VERSION < 14
  1141. /* miniupnpc 1.6 */
  1142. int error = 0;
  1143. devlist = upnpDiscover(2000, multicastif, minissdpdpath, 0, 0, &error);
  1144. #else
  1145. /* miniupnpc 1.9.20150730 */
  1146. int error = 0;
  1147. devlist = upnpDiscover(2000, multicastif, minissdpdpath, 0, 0, 2, &error);
  1148. #endif
  1149. struct UPNPUrls urls;
  1150. struct IGDdatas data;
  1151. int r;
  1152. r = UPNP_GetValidIGD(devlist, &urls, &data, lanaddr, sizeof(lanaddr));
  1153. if (r == 1)
  1154. {
  1155. if (fDiscover) {
  1156. char externalIPAddress[40];
  1157. r = UPNP_GetExternalIPAddress(urls.controlURL, data.first.servicetype, externalIPAddress);
  1158. if(r != UPNPCOMMAND_SUCCESS)
  1159. LogPrintf("UPnP: GetExternalIPAddress() returned %d\n", r);
  1160. else
  1161. {
  1162. if(externalIPAddress[0])
  1163. {
  1164. LogPrintf("UPnP: ExternalIPAddress = %s\n", externalIPAddress);
  1165. AddLocal(CNetAddr(externalIPAddress), LOCAL_UPNP);
  1166. }
  1167. else
  1168. LogPrintf("UPnP: GetExternalIPAddress failed.\n");
  1169. }
  1170. }
  1171. std::string strDesc = "Bitcoin " + FormatFullVersion();
  1172. try {
  1173. while (true) {
  1174. #ifndef UPNPDISCOVER_SUCCESS
  1175. /* miniupnpc 1.5 */
  1176. r = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype,
  1177. port.c_str(), port.c_str(), lanaddr, strDesc.c_str(), "TCP", 0);
  1178. #else
  1179. /* miniupnpc 1.6 */
  1180. r = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype,
  1181. port.c_str(), port.c_str(), lanaddr, strDesc.c_str(), "TCP", 0, "0");
  1182. #endif
  1183. if(r!=UPNPCOMMAND_SUCCESS)
  1184. LogPrintf("AddPortMapping(%s, %s, %s) failed with code %d (%s)\n",
  1185. port, port, lanaddr, r, strupnperror(r));
  1186. else
  1187. LogPrintf("UPnP Port Mapping successful.\n");
  1188. MilliSleep(20*60*1000); // Refresh every 20 minutes
  1189. }
  1190. }
  1191. catch (const boost::thread_interrupted&)
  1192. {
  1193. r = UPNP_DeletePortMapping(urls.controlURL, data.first.servicetype, port.c_str(), "TCP", 0);
  1194. LogPrintf("UPNP_DeletePortMapping() returned: %d\n", r);
  1195. freeUPNPDevlist(devlist); devlist = 0;
  1196. FreeUPNPUrls(&urls);
  1197. throw;
  1198. }
  1199. } else {
  1200. LogPrintf("No valid UPnP IGDs found\n");
  1201. freeUPNPDevlist(devlist); devlist = 0;
  1202. if (r != 0)
  1203. FreeUPNPUrls(&urls);
  1204. }
  1205. }
  1206. void MapPort(bool fUseUPnP)
  1207. {
  1208. static boost::thread* upnp_thread = NULL;
  1209. if (fUseUPnP)
  1210. {
  1211. if (upnp_thread) {
  1212. upnp_thread->interrupt();
  1213. upnp_thread->join();
  1214. delete upnp_thread;
  1215. }
  1216. upnp_thread = new boost::thread(boost::bind(&TraceThread<void (*)()>, "upnp", &ThreadMapPort));
  1217. }
  1218. else if (upnp_thread) {
  1219. upnp_thread->interrupt();
  1220. upnp_thread->join();
  1221. delete upnp_thread;
  1222. upnp_thread = NULL;
  1223. }
  1224. }
  1225. #else
  1226. void MapPort(bool)
  1227. {
  1228. // Intentionally left blank.
  1229. }
  1230. #endif
  1231. void ThreadDNSAddressSeed()
  1232. {
  1233. // goal: only query DNS seeds if address need is acute
  1234. if ((addrman.size() > 0) &&
  1235. (!GetBoolArg("-forcednsseed", DEFAULT_FORCEDNSSEED))) {
  1236. MilliSleep(11 * 1000);
  1237. LOCK(cs_vNodes);
  1238. if (vNodes.size() >= 2) {
  1239. LogPrintf("P2P peers available. Skipped DNS seeding.\n");
  1240. return;
  1241. }
  1242. }
  1243. const std::vector<CDNSSeedData> &vSeeds = Params().DNSSeeds();
  1244. int found = 0;
  1245. LogPrintf("Loading addresses from DNS seeds (could take a while)\n");
  1246. BOOST_FOREACH(const CDNSSeedData &seed, vSeeds) {
  1247. if (HaveNameProxy()) {
  1248. AddOneShot(seed.host);
  1249. } else {
  1250. std::vector<CNetAddr> vIPs;
  1251. std::vector<CAddress> vAdd;
  1252. uint64_t requiredServiceBits = NODE_NETWORK;
  1253. if (LookupHost(seed.getHost(requiredServiceBits).c_str(), vIPs, 0, true))
  1254. {
  1255. BOOST_FOREACH(const CNetAddr& ip, vIPs)
  1256. {
  1257. int nOneDay = 24*3600;
  1258. CAddress addr = CAddress(CService(ip, Params().GetDefaultPort()), requiredServiceBits);
  1259. addr.nTime = GetTime() - 3*nOneDay - GetRand(4*nOneDay); // use a random age between 3 and 7 days old
  1260. vAdd.push_back(addr);
  1261. found++;
  1262. }
  1263. }
  1264. // TODO: The seed name resolve may fail, yielding an IP of [::], which results in
  1265. // addrman assigning the same source to results from different seeds.
  1266. // This should switch to a hard-coded stable dummy IP for each seed name, so that the
  1267. // resolve is not required at all.
  1268. if (!vIPs.empty()) {
  1269. CService seedSource;
  1270. Lookup(seed.name.c_str(), seedSource, 0, true);
  1271. addrman.Add(vAdd, seedSource);
  1272. }
  1273. }
  1274. }
  1275. LogPrintf("%d addresses found from DNS seeds\n", found);
  1276. }
  1277. void DumpAddresses()
  1278. {
  1279. int64_t nStart = GetTimeMillis();
  1280. CAddrDB adb;
  1281. adb.Write(addrman);
  1282. LogPrint("net", "Flushed %d addresses to peers.dat %dms\n",
  1283. addrman.size(), GetTimeMillis() - nStart);
  1284. }
  1285. void DumpData()
  1286. {
  1287. DumpAddresses();
  1288. DumpBanlist();
  1289. }
  1290. void static ProcessOneShot()
  1291. {
  1292. std::string strDest;
  1293. {
  1294. LOCK(cs_vOneShots);
  1295. if (vOneShots.empty())
  1296. return;
  1297. strDest = vOneShots.front();
  1298. vOneShots.pop_front();
  1299. }
  1300. CAddress addr;
  1301. CSemaphoreGrant grant(*semOutbound, true);
  1302. if (grant) {
  1303. if (!OpenNetworkConnection(addr, &grant, strDest.c_str(), true))
  1304. AddOneShot(strDest);
  1305. }
  1306. }
  1307. void ThreadOpenConnections()
  1308. {
  1309. // Connect to specific addresses
  1310. if (mapArgs.count("-connect") && mapMultiArgs["-connect"].size() > 0)
  1311. {
  1312. for (int64_t nLoop = 0;; nLoop++)
  1313. {
  1314. ProcessOneShot();
  1315. BOOST_FOREACH(const std::string& strAddr, mapMultiArgs["-connect"])
  1316. {
  1317. CAddress addr;
  1318. OpenNetworkConnection(addr, NULL, strAddr.c_str());
  1319. for (int i = 0; i < 10 && i < nLoop; i++)
  1320. {
  1321. MilliSleep(500);
  1322. }
  1323. }
  1324. MilliSleep(500);
  1325. }
  1326. }
  1327. // Initiate network connections
  1328. int64_t nStart = GetTime();
  1329. while (true)
  1330. {
  1331. ProcessOneShot();
  1332. MilliSleep(500);
  1333. CSemaphoreGrant grant(*semOutbound);
  1334. boost::this_thread::interruption_point();
  1335. // Add seed nodes if DNS seeds are all down (an infrastructure attack?).
  1336. if (addrman.size() == 0 && (GetTime() - nStart > 60)) {
  1337. static bool done = false;
  1338. if (!done) {
  1339. LogPrintf("Adding fixed seed nodes as DNS doesn't seem to be available.\n");
  1340. addrman.Add(convertSeed6(Params().FixedSeeds()), CNetAddr("127.0.0.1"));
  1341. done = true;
  1342. }
  1343. }
  1344. //
  1345. // Choose an address to connect to based on most recently seen
  1346. //
  1347. CAddress addrConnect;
  1348. // Only connect out to one peer per network group (/16 for IPv4).
  1349. // Do this here so we don't have to critsect vNodes inside mapAddresses critsect.
  1350. int nOutbound = 0;
  1351. std::set<std::vector<unsigned char> > setConnected;
  1352. {
  1353. LOCK(cs_vNodes);
  1354. BOOST_FOREACH(CNode* pnode, vNodes) {
  1355. if (!pnode->fInbound) {
  1356. setConnected.insert(pnode->addr.GetGroup());
  1357. nOutbound++;
  1358. }
  1359. }
  1360. }
  1361. int64_t nANow = GetAdjustedTime();
  1362. int nTries = 0;
  1363. while (true)
  1364. {
  1365. CAddrInfo addr = addrman.Select();
  1366. // if we selected an invalid address, restart
  1367. if (!addr.IsValid() || setConnected.count(addr.GetGroup()) || IsLocal(addr))
  1368. break;
  1369. // If we didn't find an appropriate destination after trying 100 addresses fetched from addrman,
  1370. // stop this loop, and let the outer loop run again (which sleeps, adds seed nodes, recalculates
  1371. // already-connected network ranges, ...) before trying new addrman addresses.
  1372. nTries++;
  1373. if (nTries > 100)
  1374. break;
  1375. if (IsLimited(addr))
  1376. continue;
  1377. // only consider very recently tried nodes after 30 failed attempts
  1378. if (nANow - addr.nLastTry < 600 && nTries < 30)
  1379. continue;
  1380. // do not allow non-default ports, unless after 50 invalid addresses selected already
  1381. if (addr.GetPort() != Params().GetDefaultPort() && nTries < 50)
  1382. continue;
  1383. addrConnect = addr;
  1384. break;
  1385. }
  1386. if (addrConnect.IsValid())
  1387. OpenNetworkConnection(addrConnect, &grant);
  1388. }
  1389. }
  1390. void ThreadOpenAddedConnections()
  1391. {
  1392. {
  1393. LOCK(cs_vAddedNodes);
  1394. vAddedNodes = mapMultiArgs["-addnode"];
  1395. }
  1396. if (HaveNameProxy()) {
  1397. while(true) {
  1398. std::list<std::string> lAddresses(0);
  1399. {
  1400. LOCK(cs_vAddedNodes);
  1401. BOOST_FOREACH(const std::string& strAddNode, vAddedNodes)
  1402. lAddresses.push_back(strAddNode);
  1403. }
  1404. BOOST_FOREACH(const std::string& strAddNode, lAddresses) {
  1405. CAddress addr;
  1406. CSemaphoreGrant grant(*semOutbound);
  1407. OpenNetworkConnection(addr, &grant, strAddNode.c_str());
  1408. MilliSleep(500);
  1409. }
  1410. MilliSleep(120000); // Retry every 2 minutes
  1411. }
  1412. }
  1413. for (unsigned int i = 0; true; i++)
  1414. {
  1415. std::list<std::string> lAddresses(0);
  1416. {
  1417. LOCK(cs_vAddedNodes);
  1418. BOOST_FOREACH(const std::string& strAddNode, vAddedNodes)
  1419. lAddresses.push_back(strAddNode);
  1420. }
  1421. std::list<std::vector<CService> > lservAddressesToAdd(0);
  1422. BOOST_FOREACH(const std::string& strAddNode, lAddresses) {
  1423. std::vector<CService> vservNode(0);
  1424. if(Lookup(strAddNode.c_str(), vservNode, Params().GetDefaultPort(), fNameLookup, 0))
  1425. lservAddressesToAdd.push_back(vservNode);
  1426. }
  1427. // Attempt to connect to each IP for each addnode entry until at least one is successful per addnode entry
  1428. // (keeping in mind that addnode entries can have many IPs if fNameLookup)
  1429. {
  1430. LOCK(cs_vNodes);
  1431. BOOST_FOREACH(CNode* pnode, vNodes)
  1432. for (std::list<std::vector<CService> >::iterator it = lservAddressesToAdd.begin(); it != lservAddressesToAdd.end(); it++)
  1433. BOOST_FOREACH(const CService& addrNode, *(it))
  1434. if (pnode->addr == addrNode)
  1435. {
  1436. it = lservAddressesToAdd.erase(it);
  1437. it--;
  1438. break;
  1439. }
  1440. }
  1441. BOOST_FOREACH(std::vector<CService>& vserv, lservAddressesToAdd)
  1442. {
  1443. CSemaphoreGrant grant(*semOutbound);
  1444. OpenNetworkConnection(CAddress(vserv[i % vserv.size()]), &grant);
  1445. MilliSleep(500);
  1446. }
  1447. MilliSleep(120000); // Retry every 2 minutes
  1448. }
  1449. }
  1450. // if successful, this moves the passed grant to the constructed node
  1451. bool OpenNetworkConnection(const CAddress& addrConnect, CSemaphoreGrant *grantOutbound, const char *pszDest, bool fOneShot)
  1452. {
  1453. //
  1454. // Initiate outbound network connection
  1455. //
  1456. boost::this_thread::interruption_point();
  1457. if (!pszDest) {
  1458. if (IsLocal(addrConnect) ||
  1459. FindNode((CNetAddr)addrConnect) || CNode::IsBanned(addrConnect) ||
  1460. FindNode(addrConnect.ToStringIPPort()))
  1461. return false;
  1462. } else if (FindNode(std::string(pszDest)))
  1463. return false;
  1464. CNode* pnode = ConnectNode(addrConnect, pszDest);
  1465. boost::this_thread::interruption_point();
  1466. if (!pnode)
  1467. return false;
  1468. if (grantOutbound)
  1469. grantOutbound->MoveTo(pnode->grantOutbound);
  1470. pnode->fNetworkNode = true;
  1471. if (fOneShot)
  1472. pnode->fOneShot = true;
  1473. return true;
  1474. }
  1475. void ThreadMessageHandler()
  1476. {
  1477. boost::mutex condition_mutex;
  1478. boost::unique_lock<boost::mutex> lock(condition_mutex);
  1479. while (true)
  1480. {
  1481. std::vector<CNode*> vNodesCopy;
  1482. {
  1483. LOCK(cs_vNodes);
  1484. vNodesCopy = vNodes;
  1485. BOOST_FOREACH(CNode* pnode, vNodesCopy) {
  1486. pnode->AddRef();
  1487. }
  1488. }
  1489. bool fSleep = true;
  1490. BOOST_FOREACH(CNode* pnode, vNodesCopy)
  1491. {
  1492. if (pnode->fDisconnect)
  1493. continue;
  1494. // Receive messages
  1495. {
  1496. TRY_LOCK(pnode->cs_vRecvMsg, lockRecv);
  1497. if (lockRecv)
  1498. {
  1499. if (!GetNodeSignals().ProcessMessages(pnode))
  1500. pnode->CloseSocketDisconnect();
  1501. if (pnode->nSendSize < SendBufferSize())
  1502. {
  1503. if (!pnode->vRecvGetData.empty() || (!pnode->vRecvMsg.empty() && pnode->vRecvMsg[0].complete()))
  1504. {
  1505. fSleep = false;
  1506. }
  1507. }
  1508. }
  1509. }
  1510. boost::this_thread::interruption_point();
  1511. // Send messages
  1512. {
  1513. TRY_LOCK(pnode->cs_vSend, lockSend);
  1514. if (lockSend)
  1515. GetNodeSignals().SendMessages(pnode);
  1516. }
  1517. boost::this_thread::interruption_point();
  1518. }
  1519. {
  1520. LOCK(cs_vNodes);
  1521. BOOST_FOREACH(CNode* pnode, vNodesCopy)
  1522. pnode->Release();
  1523. }
  1524. if (fSleep)
  1525. messageHandlerCondition.timed_wait(lock, boost::posix_time::microsec_clock::universal_time() + boost::posix_time::milliseconds(100));
  1526. }
  1527. }
  1528. bool BindListenPort(const CService &addrBind, std::string& strError, bool fWhitelisted)
  1529. {
  1530. strError = "";
  1531. int nOne = 1;
  1532. // Create socket for listening for incoming connections
  1533. struct sockaddr_storage sockaddr;
  1534. socklen_t len = sizeof(sockaddr);
  1535. if (!addrBind.GetSockAddr((struct sockaddr*)&sockaddr, &len))
  1536. {
  1537. strError = strprintf("Error: Bind address family for %s not supported", addrBind.ToString());
  1538. LogPrintf("%s\n", strError);
  1539. return false;
  1540. }
  1541. SOCKET hListenSocket = socket(((struct sockaddr*)&sockaddr)->sa_family, SOCK_STREAM, IPPROTO_TCP);
  1542. if (hListenSocket == INVALID_SOCKET)
  1543. {
  1544. strError = strprintf("Error: Couldn't open socket for incoming connections (socket returned error %s)", NetworkErrorString(WSAGetLastError()));
  1545. LogPrintf("%s\n", strError);
  1546. return false;
  1547. }
  1548. if (!IsSelectableSocket(hListenSocket))
  1549. {
  1550. strError = "Error: Couldn't create a listenable socket for incoming connections";
  1551. LogPrintf("%s\n", strError);
  1552. return false;
  1553. }
  1554. #ifndef WIN32
  1555. #ifdef SO_NOSIGPIPE
  1556. // Different way of disabling SIGPIPE on BSD
  1557. setsockopt(hListenSocket, SOL_SOCKET, SO_NOSIGPIPE, (void*)&nOne, sizeof(int));
  1558. #endif
  1559. // Allow binding if the port is still in TIME_WAIT state after
  1560. // the program was closed and restarted.
  1561. setsockopt(hListenSocket, SOL_SOCKET, SO_REUSEADDR, (void*)&nOne, sizeof(int));
  1562. // Disable Nagle's algorithm
  1563. setsockopt(hListenSocket, IPPROTO_TCP, TCP_NODELAY, (void*)&nOne, sizeof(int));
  1564. #else
  1565. setsockopt(hListenSocket, SOL_SOCKET, SO_REUSEADDR, (const char*)&nOne, sizeof(int));
  1566. setsockopt(hListenSocket, IPPROTO_TCP, TCP_NODELAY, (const char*)&nOne, sizeof(int));
  1567. #endif
  1568. // Set to non-blocking, incoming connections will also inherit this
  1569. if (!SetSocketNonBlocking(hListenSocket, true)) {
  1570. strError = strprintf("BindListenPort: Setting listening socket to non-blocking failed, error %s\n", NetworkErrorString(WSAGetLastError()));
  1571. LogPrintf("%s\n", strError);
  1572. return false;
  1573. }
  1574. // some systems don't have IPV6_V6ONLY but are always v6only; others do have the option
  1575. // and enable it by default or not. Try to enable it, if possible.
  1576. if (addrBind.IsIPv6()) {
  1577. #ifdef IPV6_V6ONLY
  1578. #ifdef WIN32
  1579. setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_V6ONLY, (const char*)&nOne, sizeof(int));
  1580. #else
  1581. setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_V6ONLY, (void*)&nOne, sizeof(int));
  1582. #endif
  1583. #endif
  1584. #ifdef WIN32
  1585. int nProtLevel = PROTECTION_LEVEL_UNRESTRICTED;
  1586. setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_PROTECTION_LEVEL, (const char*)&nProtLevel, sizeof(int));
  1587. #endif
  1588. }
  1589. if (::bind(hListenSocket, (struct sockaddr*)&sockaddr, len) == SOCKET_ERROR)
  1590. {
  1591. int nErr = WSAGetLastError();
  1592. if (nErr == WSAEADDRINUSE)
  1593. strError = strprintf(_("Unable to bind to %s on this computer. %s is probably already running."), addrBind.ToString(), _(PACKAGE_NAME));
  1594. else
  1595. strError = strprintf(_("Unable to bind to %s on this computer (bind returned error %s)"), addrBind.ToString(), NetworkErrorString(nErr));
  1596. LogPrintf("%s\n", strError);
  1597. CloseSocket(hListenSocket);
  1598. return false;
  1599. }
  1600. LogPrintf("Bound to %s\n", addrBind.ToString());
  1601. // Listen for incoming connections
  1602. if (listen(hListenSocket, SOMAXCONN) == SOCKET_ERROR)
  1603. {
  1604. strError = strprintf(_("Error: Listening for incoming connections failed (listen returned error %s)"), NetworkErrorString(WSAGetLastError()));
  1605. LogPrintf("%s\n", strError);
  1606. CloseSocket(hListenSocket);
  1607. return false;
  1608. }
  1609. vhListenSocket.push_back(ListenSocket(hListenSocket, fWhitelisted));
  1610. if (addrBind.IsRoutable() && fDiscover && !fWhitelisted)
  1611. AddLocal(addrBind, LOCAL_BIND);
  1612. return true;
  1613. }
  1614. void static Discover(boost::thread_group& threadGroup)
  1615. {
  1616. if (!fDiscover)
  1617. return;
  1618. #ifdef WIN32
  1619. // Get local host IP
  1620. char pszHostName[256] = "";
  1621. if (gethostname(pszHostName, sizeof(pszHostName)) != SOCKET_ERROR)
  1622. {
  1623. std::vector<CNetAddr> vaddr;
  1624. if (LookupHost(pszHostName, vaddr, 0, true))
  1625. {
  1626. BOOST_FOREACH (const CNetAddr &addr, vaddr)
  1627. {
  1628. if (AddLocal(addr, LOCAL_IF))
  1629. LogPrintf("%s: %s - %s\n", __func__, pszHostName, addr.ToString());
  1630. }
  1631. }
  1632. }
  1633. #else
  1634. // Get local host ip
  1635. struct ifaddrs* myaddrs;
  1636. if (getifaddrs(&myaddrs) == 0)
  1637. {
  1638. for (struct ifaddrs* ifa = myaddrs; ifa != NULL; ifa = ifa->ifa_next)
  1639. {
  1640. if (ifa->ifa_addr == NULL) continue;
  1641. if ((ifa->ifa_flags & IFF_UP) == 0) continue;
  1642. if (strcmp(ifa->ifa_name, "lo") == 0) continue;
  1643. if (strcmp(ifa->ifa_name, "lo0") == 0) continue;
  1644. if (ifa->ifa_addr->sa_family == AF_INET)
  1645. {
  1646. struct sockaddr_in* s4 = (struct sockaddr_in*)(ifa->ifa_addr);
  1647. CNetAddr addr(s4->sin_addr);
  1648. if (AddLocal(addr, LOCAL_IF))
  1649. LogPrintf("%s: IPv4 %s: %s\n", __func__, ifa->ifa_name, addr.ToString());
  1650. }
  1651. else if (ifa->ifa_addr->sa_family == AF_INET6)
  1652. {
  1653. struct sockaddr_in6* s6 = (struct sockaddr_in6*)(ifa->ifa_addr);
  1654. CNetAddr addr(s6->sin6_addr);
  1655. if (AddLocal(addr, LOCAL_IF))
  1656. LogPrintf("%s: IPv6 %s: %s\n", __func__, ifa->ifa_name, addr.ToString());
  1657. }
  1658. }
  1659. freeifaddrs(myaddrs);
  1660. }
  1661. #endif
  1662. }
  1663. void StartNode(boost::thread_group& threadGroup, CScheduler& scheduler)
  1664. {
  1665. uiInterface.InitMessage(_("Loading addresses..."));
  1666. // Load addresses from peers.dat
  1667. int64_t nStart = GetTimeMillis();
  1668. {
  1669. CAddrDB adb;
  1670. if (adb.Read(addrman))
  1671. LogPrintf("Loaded %i addresses from peers.dat %dms\n", addrman.size(), GetTimeMillis() - nStart);
  1672. else {
  1673. addrman.Clear(); // Addrman can be in an inconsistent state after failure, reset it
  1674. LogPrintf("Invalid or missing peers.dat; recreating\n");
  1675. DumpAddresses();
  1676. }
  1677. }
  1678. uiInterface.InitMessage(_("Loading banlist..."));
  1679. // Load addresses from banlist.dat
  1680. nStart = GetTimeMillis();
  1681. CBanDB bandb;
  1682. banmap_t banmap;
  1683. if (bandb.Read(banmap)) {
  1684. CNode::SetBanned(banmap); // thread save setter
  1685. CNode::SetBannedSetDirty(false); // no need to write down, just read data
  1686. CNode::SweepBanned(); // sweep out unused entries
  1687. LogPrint("net", "Loaded %d banned node ips/subnets from banlist.dat %dms\n",
  1688. banmap.size(), GetTimeMillis() - nStart);
  1689. } else {
  1690. LogPrintf("Invalid or missing banlist.dat; recreating\n");
  1691. CNode::SetBannedSetDirty(true); // force write
  1692. DumpBanlist();
  1693. }
  1694. fAddressesInitialized = true;
  1695. if (semOutbound == NULL) {
  1696. // initialize semaphore
  1697. int nMaxOutbound = std::min(MAX_OUTBOUND_CONNECTIONS, nMaxConnections);
  1698. semOutbound = new CSemaphore(nMaxOutbound);
  1699. }
  1700. if (pnodeLocalHost == NULL)
  1701. pnodeLocalHost = new CNode(INVALID_SOCKET, CAddress(CService("127.0.0.1", 0), nLocalServices));
  1702. Discover(threadGroup);
  1703. //
  1704. // Start threads
  1705. //
  1706. if (!GetBoolArg("-dnsseed", true))
  1707. LogPrintf("DNS seeding disabled\n");
  1708. else
  1709. threadGroup.create_thread(boost::bind(&TraceThread<void (*)()>, "dnsseed", &ThreadDNSAddressSeed));
  1710. // Map ports with UPnP
  1711. MapPort(GetBoolArg("-upnp", DEFAULT_UPNP));
  1712. // Send and receive from sockets, accept connections
  1713. threadGroup.create_thread(boost::bind(&TraceThread<void (*)()>, "net", &ThreadSocketHandler));
  1714. // Initiate outbound connections from -addnode
  1715. threadGroup.create_thread(boost::bind(&TraceThread<void (*)()>, "addcon", &ThreadOpenAddedConnections));
  1716. // Initiate outbound connections
  1717. threadGroup.create_thread(boost::bind(&TraceThread<void (*)()>, "opencon", &ThreadOpenConnections));
  1718. // Process messages
  1719. threadGroup.create_thread(boost::bind(&TraceThread<void (*)()>, "msghand", &ThreadMessageHandler));
  1720. // Dump network addresses
  1721. scheduler.scheduleEvery(&DumpData, DUMP_ADDRESSES_INTERVAL);
  1722. }
  1723. bool StopNode()
  1724. {
  1725. LogPrintf("StopNode()\n");
  1726. MapPort(false);
  1727. if (semOutbound)
  1728. for (int i=0; i<MAX_OUTBOUND_CONNECTIONS; i++)
  1729. semOutbound->post();
  1730. if (fAddressesInitialized)
  1731. {
  1732. DumpData();
  1733. fAddressesInitialized = false;
  1734. }
  1735. return true;
  1736. }
  1737. class CNetCleanup
  1738. {
  1739. public:
  1740. CNetCleanup() {}
  1741. ~CNetCleanup()
  1742. {
  1743. // Close sockets
  1744. BOOST_FOREACH(CNode* pnode, vNodes)
  1745. if (pnode->hSocket != INVALID_SOCKET)
  1746. CloseSocket(pnode->hSocket);
  1747. BOOST_FOREACH(ListenSocket& hListenSocket, vhListenSocket)
  1748. if (hListenSocket.socket != INVALID_SOCKET)
  1749. if (!CloseSocket(hListenSocket.socket))
  1750. LogPrintf("CloseSocket(hListenSocket) failed with error %s\n", NetworkErrorString(WSAGetLastError()));
  1751. // clean up some globals (to help leak detection)
  1752. BOOST_FOREACH(CNode *pnode, vNodes)
  1753. delete pnode;
  1754. BOOST_FOREACH(CNode *pnode, vNodesDisconnected)
  1755. delete pnode;
  1756. vNodes.clear();
  1757. vNodesDisconnected.clear();
  1758. vhListenSocket.clear();
  1759. delete semOutbound;
  1760. semOutbound = NULL;
  1761. delete pnodeLocalHost;
  1762. pnodeLocalHost = NULL;
  1763. #ifdef WIN32
  1764. // Shutdown Windows Sockets
  1765. WSACleanup();
  1766. #endif
  1767. }
  1768. }
  1769. instance_of_cnetcleanup;
  1770. void RelayTransaction(const CTransaction& tx)
  1771. {
  1772. CInv inv(MSG_TX, tx.GetHash());
  1773. {
  1774. LOCK(cs_mapRelay);
  1775. // Expire old relay messages
  1776. while (!vRelayExpiration.empty() && vRelayExpiration.front().first < GetTime())
  1777. {
  1778. mapRelay.erase(vRelayExpiration.front().second);
  1779. vRelayExpiration.pop_front();
  1780. }
  1781. mapRelay.insert(std::make_pair(inv.hash, tx));
  1782. vRelayExpiration.push_back(std::make_pair(GetTime() + 15 * 60, inv.hash));
  1783. }
  1784. LOCK(cs_vNodes);
  1785. BOOST_FOREACH(CNode* pnode, vNodes)
  1786. {
  1787. pnode->PushInventory(inv);
  1788. }
  1789. }
  1790. void CNode::RecordBytesRecv(uint64_t bytes)
  1791. {
  1792. LOCK(cs_totalBytesRecv);
  1793. nTotalBytesRecv += bytes;
  1794. }
  1795. void CNode::RecordBytesSent(uint64_t bytes)
  1796. {
  1797. LOCK(cs_totalBytesSent);
  1798. nTotalBytesSent += bytes;
  1799. uint64_t now = GetTime();
  1800. if (nMaxOutboundCycleStartTime + nMaxOutboundTimeframe < now)
  1801. {
  1802. // timeframe expired, reset cycle
  1803. nMaxOutboundCycleStartTime = now;
  1804. nMaxOutboundTotalBytesSentInCycle = 0;
  1805. }
  1806. // TODO, exclude whitebind peers
  1807. nMaxOutboundTotalBytesSentInCycle += bytes;
  1808. }
  1809. void CNode::SetMaxOutboundTarget(uint64_t limit)
  1810. {
  1811. LOCK(cs_totalBytesSent);
  1812. uint64_t recommendedMinimum = (nMaxOutboundTimeframe / 600) * MAX_BLOCK_SIZE;
  1813. nMaxOutboundLimit = limit;
  1814. if (limit > 0 && limit < recommendedMinimum)
  1815. LogPrintf("Max outbound target is very small (%s bytes) and will be overshot. Recommended minimum is %s bytes.\n", nMaxOutboundLimit, recommendedMinimum);
  1816. }
  1817. uint64_t CNode::GetMaxOutboundTarget()
  1818. {
  1819. LOCK(cs_totalBytesSent);
  1820. return nMaxOutboundLimit;
  1821. }
  1822. uint64_t CNode::GetMaxOutboundTimeframe()
  1823. {
  1824. LOCK(cs_totalBytesSent);
  1825. return nMaxOutboundTimeframe;
  1826. }
  1827. uint64_t CNode::GetMaxOutboundTimeLeftInCycle()
  1828. {
  1829. LOCK(cs_totalBytesSent);
  1830. if (nMaxOutboundLimit == 0)
  1831. return 0;
  1832. if (nMaxOutboundCycleStartTime == 0)
  1833. return nMaxOutboundTimeframe;
  1834. uint64_t cycleEndTime = nMaxOutboundCycleStartTime + nMaxOutboundTimeframe;
  1835. uint64_t now = GetTime();
  1836. return (cycleEndTime < now) ? 0 : cycleEndTime - GetTime();
  1837. }
  1838. void CNode::SetMaxOutboundTimeframe(uint64_t timeframe)
  1839. {
  1840. LOCK(cs_totalBytesSent);
  1841. if (nMaxOutboundTimeframe != timeframe)
  1842. {
  1843. // reset measure-cycle in case of changing
  1844. // the timeframe
  1845. nMaxOutboundCycleStartTime = GetTime();
  1846. }
  1847. nMaxOutboundTimeframe = timeframe;
  1848. }
  1849. bool CNode::OutboundTargetReached(bool historicalBlockServingLimit)
  1850. {
  1851. LOCK(cs_totalBytesSent);
  1852. if (nMaxOutboundLimit == 0)
  1853. return false;
  1854. if (historicalBlockServingLimit)
  1855. {
  1856. // keep a large enough buffer to at least relay each block once
  1857. uint64_t timeLeftInCycle = GetMaxOutboundTimeLeftInCycle();
  1858. uint64_t buffer = timeLeftInCycle / 600 * MAX_BLOCK_SIZE;
  1859. if (buffer >= nMaxOutboundLimit || nMaxOutboundTotalBytesSentInCycle >= nMaxOutboundLimit - buffer)
  1860. return true;
  1861. }
  1862. else if (nMaxOutboundTotalBytesSentInCycle >= nMaxOutboundLimit)
  1863. return true;
  1864. return false;
  1865. }
  1866. uint64_t CNode::GetOutboundTargetBytesLeft()
  1867. {
  1868. LOCK(cs_totalBytesSent);
  1869. if (nMaxOutboundLimit == 0)
  1870. return 0;
  1871. return (nMaxOutboundTotalBytesSentInCycle >= nMaxOutboundLimit) ? 0 : nMaxOutboundLimit - nMaxOutboundTotalBytesSentInCycle;
  1872. }
  1873. uint64_t CNode::GetTotalBytesRecv()
  1874. {
  1875. LOCK(cs_totalBytesRecv);
  1876. return nTotalBytesRecv;
  1877. }
  1878. uint64_t CNode::GetTotalBytesSent()
  1879. {
  1880. LOCK(cs_totalBytesSent);
  1881. return nTotalBytesSent;
  1882. }
  1883. void CNode::Fuzz(int nChance)
  1884. {
  1885. if (!fSuccessfullyConnected) return; // Don't fuzz initial handshake
  1886. if (GetRand(nChance) != 0) return; // Fuzz 1 of every nChance messages
  1887. switch (GetRand(3))
  1888. {
  1889. case 0:
  1890. // xor a random byte with a random value:
  1891. if (!ssSend.empty()) {
  1892. CDataStream::size_type pos = GetRand(ssSend.size());
  1893. ssSend[pos] ^= (unsigned char)(GetRand(256));
  1894. }
  1895. break;
  1896. case 1:
  1897. // delete a random byte:
  1898. if (!ssSend.empty()) {
  1899. CDataStream::size_type pos = GetRand(ssSend.size());
  1900. ssSend.erase(ssSend.begin()+pos);
  1901. }
  1902. break;
  1903. case 2:
  1904. // insert a random byte at a random position
  1905. {
  1906. CDataStream::size_type pos = GetRand(ssSend.size());
  1907. char ch = (char)GetRand(256);
  1908. ssSend.insert(ssSend.begin()+pos, ch);
  1909. }
  1910. break;
  1911. }
  1912. // Chance of more than one change half the time:
  1913. // (more changes exponentially less likely):
  1914. Fuzz(2);
  1915. }
  1916. //
  1917. // CAddrDB
  1918. //
  1919. CAddrDB::CAddrDB()
  1920. {
  1921. pathAddr = GetDataDir() / "peers.dat";
  1922. }
  1923. bool CAddrDB::Write(const CAddrMan& addr)
  1924. {
  1925. // Generate random temporary filename
  1926. unsigned short randv = 0;
  1927. GetRandBytes((unsigned char*)&randv, sizeof(randv));
  1928. std::string tmpfn = strprintf("peers.dat.%04x", randv);
  1929. // serialize addresses, checksum data up to that point, then append csum
  1930. CDataStream ssPeers(SER_DISK, CLIENT_VERSION);
  1931. ssPeers << FLATDATA(Params().MessageStart());
  1932. ssPeers << addr;
  1933. uint256 hash = Hash(ssPeers.begin(), ssPeers.end());
  1934. ssPeers << hash;
  1935. // open temp output file, and associate with CAutoFile
  1936. boost::filesystem::path pathTmp = GetDataDir() / tmpfn;
  1937. FILE *file = fopen(pathTmp.string().c_str(), "wb");
  1938. CAutoFile fileout(file, SER_DISK, CLIENT_VERSION);
  1939. if (fileout.IsNull())
  1940. return error("%s: Failed to open file %s", __func__, pathTmp.string());
  1941. // Write and commit header, data
  1942. try {
  1943. fileout << ssPeers;
  1944. }
  1945. catch (const std::exception& e) {
  1946. return error("%s: Serialize or I/O error - %s", __func__, e.what());
  1947. }
  1948. FileCommit(fileout.Get());
  1949. fileout.fclose();
  1950. // replace existing peers.dat, if any, with new peers.dat.XXXX
  1951. if (!RenameOver(pathTmp, pathAddr))
  1952. return error("%s: Rename-into-place failed", __func__);
  1953. return true;
  1954. }
  1955. bool CAddrDB::Read(CAddrMan& addr)
  1956. {
  1957. // open input file, and associate with CAutoFile
  1958. FILE *file = fopen(pathAddr.string().c_str(), "rb");
  1959. CAutoFile filein(file, SER_DISK, CLIENT_VERSION);
  1960. if (filein.IsNull())
  1961. return error("%s: Failed to open file %s", __func__, pathAddr.string());
  1962. // use file size to size memory buffer
  1963. uint64_t fileSize = boost::filesystem::file_size(pathAddr);
  1964. uint64_t dataSize = 0;
  1965. // Don't try to resize to a negative number if file is small
  1966. if (fileSize >= sizeof(uint256))
  1967. dataSize = fileSize - sizeof(uint256);
  1968. std::vector<unsigned char> vchData;
  1969. vchData.resize(dataSize);
  1970. uint256 hashIn;
  1971. // read data and checksum from file
  1972. try {
  1973. filein.read((char *)&vchData[0], dataSize);
  1974. filein >> hashIn;
  1975. }
  1976. catch (const std::exception& e) {
  1977. return error("%s: Deserialize or I/O error - %s", __func__, e.what());
  1978. }
  1979. filein.fclose();
  1980. CDataStream ssPeers(vchData, SER_DISK, CLIENT_VERSION);
  1981. // verify stored checksum matches input data
  1982. uint256 hashTmp = Hash(ssPeers.begin(), ssPeers.end());
  1983. if (hashIn != hashTmp)
  1984. return error("%s: Checksum mismatch, data corrupted", __func__);
  1985. return Read(addr, ssPeers);
  1986. }
  1987. bool CAddrDB::Read(CAddrMan& addr, CDataStream& ssPeers)
  1988. {
  1989. unsigned char pchMsgTmp[4];
  1990. try {
  1991. // de-serialize file header (network specific magic number) and ..
  1992. ssPeers >> FLATDATA(pchMsgTmp);
  1993. // ... verify the network matches ours
  1994. if (memcmp(pchMsgTmp, Params().MessageStart(), sizeof(pchMsgTmp)))
  1995. return error("%s: Invalid network magic number", __func__);
  1996. // de-serialize address data into one CAddrMan object
  1997. ssPeers >> addr;
  1998. }
  1999. catch (const std::exception& e) {
  2000. // de-serialization has failed, ensure addrman is left in a clean state
  2001. addr.Clear();
  2002. return error("%s: Deserialize or I/O error - %s", __func__, e.what());
  2003. }
  2004. return true;
  2005. }
  2006. unsigned int ReceiveFloodSize() { return 1000*GetArg("-maxreceivebuffer", DEFAULT_MAXRECEIVEBUFFER); }
  2007. unsigned int SendBufferSize() { return 1000*GetArg("-maxsendbuffer", DEFAULT_MAXSENDBUFFER); }
  2008. CNode::CNode(SOCKET hSocketIn, const CAddress& addrIn, const std::string& addrNameIn, bool fInboundIn) :
  2009. ssSend(SER_NETWORK, INIT_PROTO_VERSION),
  2010. addrKnown(5000, 0.001),
  2011. filterInventoryKnown(50000, 0.000001)
  2012. {
  2013. nServices = 0;
  2014. hSocket = hSocketIn;
  2015. nRecvVersion = INIT_PROTO_VERSION;
  2016. nLastSend = 0;
  2017. nLastRecv = 0;
  2018. nSendBytes = 0;
  2019. nRecvBytes = 0;
  2020. nTimeConnected = GetTime();
  2021. nTimeOffset = 0;
  2022. addr = addrIn;
  2023. addrName = addrNameIn == "" ? addr.ToStringIPPort() : addrNameIn;
  2024. nVersion = 0;
  2025. strSubVer = "";
  2026. fWhitelisted = false;
  2027. fOneShot = false;
  2028. fClient = false; // set by version message
  2029. fInbound = fInboundIn;
  2030. fNetworkNode = false;
  2031. fSuccessfullyConnected = false;
  2032. fDisconnect = false;
  2033. nRefCount = 0;
  2034. nSendSize = 0;
  2035. nSendOffset = 0;
  2036. hashContinue = uint256();
  2037. nStartingHeight = -1;
  2038. filterInventoryKnown.reset();
  2039. fSendMempool = false;
  2040. fGetAddr = false;
  2041. nNextLocalAddrSend = 0;
  2042. nNextAddrSend = 0;
  2043. nNextInvSend = 0;
  2044. fRelayTxes = false;
  2045. fSentAddr = false;
  2046. pfilter = new CBloomFilter();
  2047. nPingNonceSent = 0;
  2048. nPingUsecStart = 0;
  2049. nPingUsecTime = 0;
  2050. fPingQueued = false;
  2051. nMinPingUsecTime = std::numeric_limits<int64_t>::max();
  2052. minFeeFilter = 0;
  2053. lastSentFeeFilter = 0;
  2054. nextSendTimeFeeFilter = 0;
  2055. BOOST_FOREACH(const std::string &msg, getAllNetMessageTypes())
  2056. mapRecvBytesPerMsgCmd[msg] = 0;
  2057. mapRecvBytesPerMsgCmd[NET_MESSAGE_COMMAND_OTHER] = 0;
  2058. {
  2059. LOCK(cs_nLastNodeId);
  2060. id = nLastNodeId++;
  2061. }
  2062. if (fLogIPs)
  2063. LogPrint("net", "Added connection to %s peer=%d\n", addrName, id);
  2064. else
  2065. LogPrint("net", "Added connection peer=%d\n", id);
  2066. // Be shy and don't send version until we hear
  2067. if (hSocket != INVALID_SOCKET && !fInbound)
  2068. PushVersion();
  2069. GetNodeSignals().InitializeNode(GetId(), this);
  2070. }
  2071. CNode::~CNode()
  2072. {
  2073. CloseSocket(hSocket);
  2074. if (pfilter)
  2075. delete pfilter;
  2076. GetNodeSignals().FinalizeNode(GetId());
  2077. }
  2078. void CNode::AskFor(const CInv& inv)
  2079. {
  2080. if (mapAskFor.size() > MAPASKFOR_MAX_SZ || setAskFor.size() > SETASKFOR_MAX_SZ)
  2081. return;
  2082. // a peer may not have multiple non-responded queue positions for a single inv item
  2083. if (!setAskFor.insert(inv.hash).second)
  2084. return;
  2085. // We're using mapAskFor as a priority queue,
  2086. // the key is the earliest time the request can be sent
  2087. int64_t nRequestTime;
  2088. limitedmap<uint256, int64_t>::const_iterator it = mapAlreadyAskedFor.find(inv.hash);
  2089. if (it != mapAlreadyAskedFor.end())
  2090. nRequestTime = it->second;
  2091. else
  2092. nRequestTime = 0;
  2093. LogPrint("net", "askfor %s %d (%s) peer=%d\n", inv.ToString(), nRequestTime, DateTimeStrFormat("%H:%M:%S", nRequestTime/1000000), id);
  2094. // Make sure not to reuse time indexes to keep things in the same order
  2095. int64_t nNow = GetTimeMicros() - 1000000;
  2096. static int64_t nLastTime;
  2097. ++nLastTime;
  2098. nNow = std::max(nNow, nLastTime);
  2099. nLastTime = nNow;
  2100. // Each retry is 2 minutes after the last
  2101. nRequestTime = std::max(nRequestTime + 2 * 60 * 1000000, nNow);
  2102. if (it != mapAlreadyAskedFor.end())
  2103. mapAlreadyAskedFor.update(it, nRequestTime);
  2104. else
  2105. mapAlreadyAskedFor.insert(std::make_pair(inv.hash, nRequestTime));
  2106. mapAskFor.insert(std::make_pair(nRequestTime, inv));
  2107. }
  2108. void CNode::BeginMessage(const char* pszCommand) EXCLUSIVE_LOCK_FUNCTION(cs_vSend)
  2109. {
  2110. ENTER_CRITICAL_SECTION(cs_vSend);
  2111. assert(ssSend.size() == 0);
  2112. ssSend << CMessageHeader(Params().MessageStart(), pszCommand, 0);
  2113. LogPrint("net", "sending: %s ", SanitizeString(pszCommand));
  2114. }
  2115. void CNode::AbortMessage() UNLOCK_FUNCTION(cs_vSend)
  2116. {
  2117. ssSend.clear();
  2118. LEAVE_CRITICAL_SECTION(cs_vSend);
  2119. LogPrint("net", "(aborted)\n");
  2120. }
  2121. void CNode::EndMessage(const char* pszCommand) UNLOCK_FUNCTION(cs_vSend)
  2122. {
  2123. // The -*messagestest options are intentionally not documented in the help message,
  2124. // since they are only used during development to debug the networking code and are
  2125. // not intended for end-users.
  2126. if (mapArgs.count("-dropmessagestest") && GetRand(GetArg("-dropmessagestest", 2)) == 0)
  2127. {
  2128. LogPrint("net", "dropmessages DROPPING SEND MESSAGE\n");
  2129. AbortMessage();
  2130. return;
  2131. }
  2132. if (mapArgs.count("-fuzzmessagestest"))
  2133. Fuzz(GetArg("-fuzzmessagestest", 10));
  2134. if (ssSend.size() == 0)
  2135. {
  2136. LEAVE_CRITICAL_SECTION(cs_vSend);
  2137. return;
  2138. }
  2139. // Set the size
  2140. unsigned int nSize = ssSend.size() - CMessageHeader::HEADER_SIZE;
  2141. WriteLE32((uint8_t*)&ssSend[CMessageHeader::MESSAGE_SIZE_OFFSET], nSize);
  2142. //log total amount of bytes per command
  2143. mapSendBytesPerMsgCmd[std::string(pszCommand)] += nSize + CMessageHeader::HEADER_SIZE;
  2144. // Set the checksum
  2145. uint256 hash = Hash(ssSend.begin() + CMessageHeader::HEADER_SIZE, ssSend.end());
  2146. unsigned int nChecksum = 0;
  2147. memcpy(&nChecksum, &hash, sizeof(nChecksum));
  2148. assert(ssSend.size () >= CMessageHeader::CHECKSUM_OFFSET + sizeof(nChecksum));
  2149. memcpy((char*)&ssSend[CMessageHeader::CHECKSUM_OFFSET], &nChecksum, sizeof(nChecksum));
  2150. LogPrint("net", "(%d bytes) peer=%d\n", nSize, id);
  2151. std::deque<CSerializeData>::iterator it = vSendMsg.insert(vSendMsg.end(), CSerializeData());
  2152. ssSend.GetAndClear(*it);
  2153. nSendSize += (*it).size();
  2154. // If write queue empty, attempt "optimistic write"
  2155. if (it == vSendMsg.begin())
  2156. SocketSendData(this);
  2157. LEAVE_CRITICAL_SECTION(cs_vSend);
  2158. }
  2159. //
  2160. // CBanDB
  2161. //
  2162. CBanDB::CBanDB()
  2163. {
  2164. pathBanlist = GetDataDir() / "banlist.dat";
  2165. }
  2166. bool CBanDB::Write(const banmap_t& banSet)
  2167. {
  2168. // Generate random temporary filename
  2169. unsigned short randv = 0;
  2170. GetRandBytes((unsigned char*)&randv, sizeof(randv));
  2171. std::string tmpfn = strprintf("banlist.dat.%04x", randv);
  2172. // serialize banlist, checksum data up to that point, then append csum
  2173. CDataStream ssBanlist(SER_DISK, CLIENT_VERSION);
  2174. ssBanlist << FLATDATA(Params().MessageStart());
  2175. ssBanlist << banSet;
  2176. uint256 hash = Hash(ssBanlist.begin(), ssBanlist.end());
  2177. ssBanlist << hash;
  2178. // open temp output file, and associate with CAutoFile
  2179. boost::filesystem::path pathTmp = GetDataDir() / tmpfn;
  2180. FILE *file = fopen(pathTmp.string().c_str(), "wb");
  2181. CAutoFile fileout(file, SER_DISK, CLIENT_VERSION);
  2182. if (fileout.IsNull())
  2183. return error("%s: Failed to open file %s", __func__, pathTmp.string());
  2184. // Write and commit header, data
  2185. try {
  2186. fileout << ssBanlist;
  2187. }
  2188. catch (const std::exception& e) {
  2189. return error("%s: Serialize or I/O error - %s", __func__, e.what());
  2190. }
  2191. FileCommit(fileout.Get());
  2192. fileout.fclose();
  2193. // replace existing banlist.dat, if any, with new banlist.dat.XXXX
  2194. if (!RenameOver(pathTmp, pathBanlist))
  2195. return error("%s: Rename-into-place failed", __func__);
  2196. return true;
  2197. }
  2198. bool CBanDB::Read(banmap_t& banSet)
  2199. {
  2200. // open input file, and associate with CAutoFile
  2201. FILE *file = fopen(pathBanlist.string().c_str(), "rb");
  2202. CAutoFile filein(file, SER_DISK, CLIENT_VERSION);
  2203. if (filein.IsNull())
  2204. return error("%s: Failed to open file %s", __func__, pathBanlist.string());
  2205. // use file size to size memory buffer
  2206. uint64_t fileSize = boost::filesystem::file_size(pathBanlist);
  2207. uint64_t dataSize = 0;
  2208. // Don't try to resize to a negative number if file is small
  2209. if (fileSize >= sizeof(uint256))
  2210. dataSize = fileSize - sizeof(uint256);
  2211. std::vector<unsigned char> vchData;
  2212. vchData.resize(dataSize);
  2213. uint256 hashIn;
  2214. // read data and checksum from file
  2215. try {
  2216. filein.read((char *)&vchData[0], dataSize);
  2217. filein >> hashIn;
  2218. }
  2219. catch (const std::exception& e) {
  2220. return error("%s: Deserialize or I/O error - %s", __func__, e.what());
  2221. }
  2222. filein.fclose();
  2223. CDataStream ssBanlist(vchData, SER_DISK, CLIENT_VERSION);
  2224. // verify stored checksum matches input data
  2225. uint256 hashTmp = Hash(ssBanlist.begin(), ssBanlist.end());
  2226. if (hashIn != hashTmp)
  2227. return error("%s: Checksum mismatch, data corrupted", __func__);
  2228. unsigned char pchMsgTmp[4];
  2229. try {
  2230. // de-serialize file header (network specific magic number) and ..
  2231. ssBanlist >> FLATDATA(pchMsgTmp);
  2232. // ... verify the network matches ours
  2233. if (memcmp(pchMsgTmp, Params().MessageStart(), sizeof(pchMsgTmp)))
  2234. return error("%s: Invalid network magic number", __func__);
  2235. // de-serialize address data into one CAddrMan object
  2236. ssBanlist >> banSet;
  2237. }
  2238. catch (const std::exception& e) {
  2239. return error("%s: Deserialize or I/O error - %s", __func__, e.what());
  2240. }
  2241. return true;
  2242. }
  2243. int64_t PoissonNextSend(int64_t nNow, int average_interval_seconds) {
  2244. return nNow + (int64_t)(log1p(GetRand(1ULL << 48) * -0.0000000000000035527136788 /* -1/2^48 */) * average_interval_seconds * -1000000.0 + 0.5);
  2245. }