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 78KB

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