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

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