Back to home page

Bitcoin sources

 
 

    


File indexing completed on 2020-06-26 04:59:17

0001 // Copyright (c) 2009-2010 Satoshi Nakamoto
0002 // Copyright (c) 2009-2014 The Bitcoin developers
0003 // Distributed under the MIT/X11 software license, see the accompanying
0004 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
0005 
0006 #if defined(HAVE_CONFIG_H)
0007 #include "config/bitcoin-config.h"
0008 #endif
0009 
0010 #include "net.h"
0011 
0012 #include "addrman.h"
0013 #include "chainparams.h"
0014 #include "clientversion.h"
0015 #include "primitives/transaction.h"
0016 #include "ui_interface.h"
0017 
0018 #ifdef WIN32
0019 #include <string.h>
0020 #else
0021 #include <fcntl.h>
0022 #endif
0023 
0024 #ifdef USE_UPNP
0025 #include <miniupnpc/miniupnpc.h>
0026 #include <miniupnpc/miniwget.h>
0027 #include <miniupnpc/upnpcommands.h>
0028 #include <miniupnpc/upnperrors.h>
0029 #endif
0030 
0031 #include <boost/filesystem.hpp>
0032 #include <boost/thread.hpp>
0033 
0034 // Dump addresses to peers.dat every 15 minutes (900s)
0035 #define DUMP_ADDRESSES_INTERVAL 900
0036 
0037 #if !defined(HAVE_MSG_NOSIGNAL) && !defined(MSG_NOSIGNAL)
0038 #define MSG_NOSIGNAL 0
0039 #endif
0040 
0041 // Fix for ancient MinGW versions, that don't have defined these in ws2tcpip.h.
0042 // Todo: Can be removed when our pull-tester is upgraded to a modern MinGW version.
0043 #ifdef WIN32
0044 #ifndef PROTECTION_LEVEL_UNRESTRICTED
0045 #define PROTECTION_LEVEL_UNRESTRICTED 10
0046 #endif
0047 #ifndef IPV6_PROTECTION_LEVEL
0048 #define IPV6_PROTECTION_LEVEL 23
0049 #endif
0050 #endif
0051 
0052 using namespace boost;
0053 using namespace std;
0054 
0055 namespace {
0056     const int MAX_OUTBOUND_CONNECTIONS = 8;
0057 
0058     struct ListenSocket {
0059         SOCKET socket;
0060         bool whitelisted;
0061 
0062         ListenSocket(SOCKET socket, bool whitelisted) : socket(socket), whitelisted(whitelisted) {}
0063     };
0064 }
0065 
0066 //
0067 // Global state variables
0068 //
0069 bool fDiscover = true;
0070 bool fListen = true;
0071 uint64_t nLocalServices = NODE_NETWORK;
0072 CCriticalSection cs_mapLocalHost;
0073 map<CNetAddr, LocalServiceInfo> mapLocalHost;
0074 static bool vfReachable[NET_MAX] = {};
0075 static bool vfLimited[NET_MAX] = {};
0076 static CNode* pnodeLocalHost = NULL;
0077 uint64_t nLocalHostNonce = 0;
0078 static std::vector<ListenSocket> vhListenSocket;
0079 CAddrMan addrman;
0080 int nMaxConnections = 125;
0081 bool fAddressesInitialized = false;
0082 
0083 vector<CNode*> vNodes;
0084 CCriticalSection cs_vNodes;
0085 map<CInv, CDataStream> mapRelay;
0086 deque<pair<int64_t, CInv> > vRelayExpiration;
0087 CCriticalSection cs_mapRelay;
0088 limitedmap<CInv, int64_t> mapAlreadyAskedFor(MAX_INV_SZ);
0089 
0090 static deque<string> vOneShots;
0091 CCriticalSection cs_vOneShots;
0092 
0093 set<CNetAddr> setservAddNodeAddresses;
0094 CCriticalSection cs_setservAddNodeAddresses;
0095 
0096 vector<std::string> vAddedNodes;
0097 CCriticalSection cs_vAddedNodes;
0098 
0099 NodeId nLastNodeId = 0;
0100 CCriticalSection cs_nLastNodeId;
0101 
0102 static CSemaphore *semOutbound = NULL;
0103 
0104 // Signals for message handling
0105 static CNodeSignals g_signals;
0106 CNodeSignals& GetNodeSignals() { return g_signals; }
0107 
0108 void AddOneShot(string strDest)
0109 {
0110     LOCK(cs_vOneShots);
0111     vOneShots.push_back(strDest);
0112 }
0113 
0114 unsigned short GetListenPort()
0115 {
0116     return (unsigned short)(GetArg("-port", Params().GetDefaultPort()));
0117 }
0118 
0119 // find 'best' local address for a particular peer
0120 bool GetLocal(CService& addr, const CNetAddr *paddrPeer)
0121 {
0122     if (!fListen)
0123         return false;
0124 
0125     int nBestScore = -1;
0126     int nBestReachability = -1;
0127     {
0128         LOCK(cs_mapLocalHost);
0129         for (map<CNetAddr, LocalServiceInfo>::iterator it = mapLocalHost.begin(); it != mapLocalHost.end(); it++)
0130         {
0131             int nScore = (*it).second.nScore;
0132             int nReachability = (*it).first.GetReachabilityFrom(paddrPeer);
0133             if (nReachability > nBestReachability || (nReachability == nBestReachability && nScore > nBestScore))
0134             {
0135                 addr = CService((*it).first, (*it).second.nPort);
0136                 nBestReachability = nReachability;
0137                 nBestScore = nScore;
0138             }
0139         }
0140     }
0141     return nBestScore >= 0;
0142 }
0143 
0144 // get best local address for a particular peer as a CAddress
0145 // Otherwise, return the unroutable 0.0.0.0 but filled in with
0146 // the normal parameters, since the IP may be changed to a useful
0147 // one by discovery.
0148 CAddress GetLocalAddress(const CNetAddr *paddrPeer)
0149 {
0150     CAddress ret(CService("0.0.0.0",GetListenPort()),0);
0151     CService addr;
0152     if (GetLocal(addr, paddrPeer))
0153     {
0154         ret = CAddress(addr);
0155     }
0156     ret.nServices = nLocalServices;
0157     ret.nTime = GetAdjustedTime();
0158     return ret;
0159 }
0160 
0161 bool RecvLine(SOCKET hSocket, string& strLine)
0162 {
0163     strLine = "";
0164     while (true)
0165     {
0166         char c;
0167         int nBytes = recv(hSocket, &c, 1, 0);
0168         if (nBytes > 0)
0169         {
0170             if (c == '\n')
0171                 continue;
0172             if (c == '\r')
0173                 return true;
0174             strLine += c;
0175             if (strLine.size() >= 9000)
0176                 return true;
0177         }
0178         else if (nBytes <= 0)
0179         {
0180             boost::this_thread::interruption_point();
0181             if (nBytes < 0)
0182             {
0183                 int nErr = WSAGetLastError();
0184                 if (nErr == WSAEMSGSIZE)
0185                     continue;
0186                 if (nErr == WSAEWOULDBLOCK || nErr == WSAEINTR || nErr == WSAEINPROGRESS)
0187                 {
0188                     MilliSleep(10);
0189                     continue;
0190                 }
0191             }
0192             if (!strLine.empty())
0193                 return true;
0194             if (nBytes == 0)
0195             {
0196                 // socket closed
0197                 LogPrint("net", "socket closed\n");
0198                 return false;
0199             }
0200             else
0201             {
0202                 // socket error
0203                 int nErr = WSAGetLastError();
0204                 LogPrint("net", "recv failed: %s\n", NetworkErrorString(nErr));
0205                 return false;
0206             }
0207         }
0208     }
0209 }
0210 
0211 int GetnScore(const CService& addr)
0212 {
0213     LOCK(cs_mapLocalHost);
0214     if (mapLocalHost.count(addr) == LOCAL_NONE)
0215         return 0;
0216     return mapLocalHost[addr].nScore;
0217 }
0218 
0219 // Is our peer's addrLocal potentially useful as an external IP source?
0220 bool IsPeerAddrLocalGood(CNode *pnode)
0221 {
0222     return fDiscover && pnode->addr.IsRoutable() && pnode->addrLocal.IsRoutable() &&
0223            !IsLimited(pnode->addrLocal.GetNetwork());
0224 }
0225 
0226 // pushes our own address to a peer
0227 void AdvertizeLocal(CNode *pnode)
0228 {
0229     if (fListen && pnode->fSuccessfullyConnected)
0230     {
0231         CAddress addrLocal = GetLocalAddress(&pnode->addr);
0232         // If discovery is enabled, sometimes give our peer the address it
0233         // tells us that it sees us as in case it has a better idea of our
0234         // address than we do.
0235         if (IsPeerAddrLocalGood(pnode) && (!addrLocal.IsRoutable() ||
0236              GetRand((GetnScore(addrLocal) > LOCAL_MANUAL) ? 8:2) == 0))
0237         {
0238             addrLocal.SetIP(pnode->addrLocal);
0239         }
0240         if (addrLocal.IsRoutable())
0241         {
0242             pnode->PushAddress(addrLocal);
0243         }
0244     }
0245 }
0246 
0247 void SetReachable(enum Network net, bool fFlag)
0248 {
0249     LOCK(cs_mapLocalHost);
0250     vfReachable[net] = fFlag;
0251     if (net == NET_IPV6 && fFlag)
0252         vfReachable[NET_IPV4] = true;
0253 }
0254 
0255 // learn a new local address
0256 bool AddLocal(const CService& addr, int nScore)
0257 {
0258     if (!addr.IsRoutable())
0259         return false;
0260 
0261     if (!fDiscover && nScore < LOCAL_MANUAL)
0262         return false;
0263 
0264     if (IsLimited(addr))
0265         return false;
0266 
0267     LogPrintf("AddLocal(%s,%i)\n", addr.ToString(), nScore);
0268 
0269     {
0270         LOCK(cs_mapLocalHost);
0271         bool fAlready = mapLocalHost.count(addr) > 0;
0272         LocalServiceInfo &info = mapLocalHost[addr];
0273         if (!fAlready || nScore >= info.nScore) {
0274             info.nScore = nScore + (fAlready ? 1 : 0);
0275             info.nPort = addr.GetPort();
0276         }
0277         SetReachable(addr.GetNetwork());
0278     }
0279 
0280     return true;
0281 }
0282 
0283 bool AddLocal(const CNetAddr &addr, int nScore)
0284 {
0285     return AddLocal(CService(addr, GetListenPort()), nScore);
0286 }
0287 
0288 /** Make a particular network entirely off-limits (no automatic connects to it) */
0289 void SetLimited(enum Network net, bool fLimited)
0290 {
0291     if (net == NET_UNROUTABLE)
0292         return;
0293     LOCK(cs_mapLocalHost);
0294     vfLimited[net] = fLimited;
0295 }
0296 
0297 bool IsLimited(enum Network net)
0298 {
0299     LOCK(cs_mapLocalHost);
0300     return vfLimited[net];
0301 }
0302 
0303 bool IsLimited(const CNetAddr &addr)
0304 {
0305     return IsLimited(addr.GetNetwork());
0306 }
0307 
0308 /** vote for a local address */
0309 bool SeenLocal(const CService& addr)
0310 {
0311     {
0312         LOCK(cs_mapLocalHost);
0313         if (mapLocalHost.count(addr) == 0)
0314             return false;
0315         mapLocalHost[addr].nScore++;
0316     }
0317     return true;
0318 }
0319 
0320 
0321 /** check whether a given address is potentially local */
0322 bool IsLocal(const CService& addr)
0323 {
0324     LOCK(cs_mapLocalHost);
0325     return mapLocalHost.count(addr) > 0;
0326 }
0327 
0328 /** check whether a given network is one we can probably connect to */
0329 bool IsReachable(enum Network net)
0330 {
0331     LOCK(cs_mapLocalHost);
0332     return vfReachable[net] && !vfLimited[net];
0333 }
0334 
0335 /** check whether a given address is in a network we can probably connect to */
0336 bool IsReachable(const CNetAddr& addr)
0337 {
0338     enum Network net = addr.GetNetwork();
0339     return IsReachable(net);
0340 }
0341 
0342 void AddressCurrentlyConnected(const CService& addr)
0343 {
0344     addrman.Connected(addr);
0345 }
0346 
0347 
0348 uint64_t CNode::nTotalBytesRecv = 0;
0349 uint64_t CNode::nTotalBytesSent = 0;
0350 CCriticalSection CNode::cs_totalBytesRecv;
0351 CCriticalSection CNode::cs_totalBytesSent;
0352 
0353 CNode* FindNode(const CNetAddr& ip)
0354 {
0355     LOCK(cs_vNodes);
0356     BOOST_FOREACH(CNode* pnode, vNodes)
0357         if ((CNetAddr)pnode->addr == ip)
0358             return (pnode);
0359     return NULL;
0360 }
0361 
0362 CNode* FindNode(const std::string& addrName)
0363 {
0364     LOCK(cs_vNodes);
0365     BOOST_FOREACH(CNode* pnode, vNodes)
0366         if (pnode->addrName == addrName)
0367             return (pnode);
0368     return NULL;
0369 }
0370 
0371 CNode* FindNode(const CService& addr)
0372 {
0373     LOCK(cs_vNodes);
0374     BOOST_FOREACH(CNode* pnode, vNodes)
0375         if ((CService)pnode->addr == addr)
0376             return (pnode);
0377     return NULL;
0378 }
0379 
0380 CNode* ConnectNode(CAddress addrConnect, const char *pszDest)
0381 {
0382     if (pszDest == NULL) {
0383         if (IsLocal(addrConnect))
0384             return NULL;
0385 
0386         // Look for an existing connection
0387         CNode* pnode = FindNode((CService)addrConnect);
0388         if (pnode)
0389         {
0390             pnode->AddRef();
0391             return pnode;
0392         }
0393     }
0394 
0395     /// debug print
0396     LogPrint("net", "trying connection %s lastseen=%.1fhrs\n",
0397         pszDest ? pszDest : addrConnect.ToString(),
0398         pszDest ? 0.0 : (double)(GetAdjustedTime() - addrConnect.nTime)/3600.0);
0399 
0400     // Connect
0401     SOCKET hSocket;
0402     bool proxyConnectionFailed = false;
0403     if (pszDest ? ConnectSocketByName(addrConnect, hSocket, pszDest, Params().GetDefaultPort(), nConnectTimeout, &proxyConnectionFailed) :
0404                   ConnectSocket(addrConnect, hSocket, nConnectTimeout, &proxyConnectionFailed))
0405     {
0406         addrman.Attempt(addrConnect);
0407 
0408         // Add node
0409         CNode* pnode = new CNode(hSocket, addrConnect, pszDest ? pszDest : "", false);
0410         pnode->AddRef();
0411 
0412         {
0413             LOCK(cs_vNodes);
0414             vNodes.push_back(pnode);
0415         }
0416 
0417         pnode->nTimeConnected = GetTime();
0418 
0419         return pnode;
0420     } else if (!proxyConnectionFailed) {
0421         // If connecting to the node failed, and failure is not caused by a problem connecting to
0422         // the proxy, mark this as an attempt.
0423         addrman.Attempt(addrConnect);
0424     }
0425 
0426     return NULL;
0427 }
0428 
0429 void CNode::CloseSocketDisconnect()
0430 {
0431     fDisconnect = true;
0432     if (hSocket != INVALID_SOCKET)
0433     {
0434         LogPrint("net", "disconnecting peer=%d\n", id);
0435         CloseSocket(hSocket);
0436     }
0437 
0438     // in case this fails, we'll empty the recv buffer when the CNode is deleted
0439     TRY_LOCK(cs_vRecvMsg, lockRecv);
0440     if (lockRecv)
0441         vRecvMsg.clear();
0442 }
0443 
0444 void CNode::PushVersion()
0445 {
0446     int nBestHeight = g_signals.GetHeight().get_value_or(0);
0447 
0448     /// when NTP implemented, change to just nTime = GetAdjustedTime()
0449     int64_t nTime = (fInbound ? GetAdjustedTime() : GetTime());
0450     CAddress addrYou = (addr.IsRoutable() && !IsProxy(addr) ? addr : CAddress(CService("0.0.0.0",0)));
0451     CAddress addrMe = GetLocalAddress(&addr);
0452     GetRandBytes((unsigned char*)&nLocalHostNonce, sizeof(nLocalHostNonce));
0453     if (fLogIPs)
0454         LogPrint("net", "send version message: version %d, blocks=%d, us=%s, them=%s, peer=%d\n", PROTOCOL_VERSION, nBestHeight, addrMe.ToString(), addrYou.ToString(), id);
0455     else
0456         LogPrint("net", "send version message: version %d, blocks=%d, us=%s, peer=%d\n", PROTOCOL_VERSION, nBestHeight, addrMe.ToString(), id);
0457     PushMessage("version", PROTOCOL_VERSION, nLocalServices, nTime, addrYou, addrMe,
0458                 nLocalHostNonce, FormatSubVersion(CLIENT_NAME, CLIENT_VERSION, std::vector<string>()), nBestHeight, true);
0459 }
0460 
0461 
0462 
0463 
0464 
0465 std::map<CNetAddr, int64_t> CNode::setBanned;
0466 CCriticalSection CNode::cs_setBanned;
0467 
0468 void CNode::ClearBanned()
0469 {
0470     setBanned.clear();
0471 }
0472 
0473 bool CNode::IsBanned(CNetAddr ip)
0474 {
0475     bool fResult = false;
0476     {
0477         LOCK(cs_setBanned);
0478         std::map<CNetAddr, int64_t>::iterator i = setBanned.find(ip);
0479         if (i != setBanned.end())
0480         {
0481             int64_t t = (*i).second;
0482             if (GetTime() < t)
0483                 fResult = true;
0484         }
0485     }
0486     return fResult;
0487 }
0488 
0489 bool CNode::Ban(const CNetAddr &addr) {
0490     int64_t banTime = GetTime()+GetArg("-bantime", 60*60*24);  // Default 24-hour ban
0491     {
0492         LOCK(cs_setBanned);
0493         if (setBanned[addr] < banTime)
0494             setBanned[addr] = banTime;
0495     }
0496     return true;
0497 }
0498 
0499 
0500 std::vector<CSubNet> CNode::vWhitelistedRange;
0501 CCriticalSection CNode::cs_vWhitelistedRange;
0502 
0503 bool CNode::IsWhitelistedRange(const CNetAddr &addr) {
0504     LOCK(cs_vWhitelistedRange);
0505     BOOST_FOREACH(const CSubNet& subnet, vWhitelistedRange) {
0506         if (subnet.Match(addr))
0507             return true;
0508     }
0509     return false;
0510 }
0511 
0512 void CNode::AddWhitelistedRange(const CSubNet &subnet) {
0513     LOCK(cs_vWhitelistedRange);
0514     vWhitelistedRange.push_back(subnet);
0515 }
0516 
0517 #undef X
0518 #define X(name) stats.name = name
0519 void CNode::copyStats(CNodeStats &stats)
0520 {
0521     stats.nodeid = this->GetId();
0522     X(nServices);
0523     X(nLastSend);
0524     X(nLastRecv);
0525     X(nTimeConnected);
0526     X(addrName);
0527     X(nVersion);
0528     X(cleanSubVer);
0529     X(fInbound);
0530     X(nStartingHeight);
0531     X(nSendBytes);
0532     X(nRecvBytes);
0533     X(fWhitelisted);
0534 
0535     // It is common for nodes with good ping times to suddenly become lagged,
0536     // due to a new block arriving or other large transfer.
0537     // Merely reporting pingtime might fool the caller into thinking the node was still responsive,
0538     // since pingtime does not update until the ping is complete, which might take a while.
0539     // So, if a ping is taking an unusually long time in flight,
0540     // the caller can immediately detect that this is happening.
0541     int64_t nPingUsecWait = 0;
0542     if ((0 != nPingNonceSent) && (0 != nPingUsecStart)) {
0543         nPingUsecWait = GetTimeMicros() - nPingUsecStart;
0544     }
0545 
0546     // 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 :)
0547     stats.dPingTime = (((double)nPingUsecTime) / 1e6);
0548     stats.dPingWait = (((double)nPingUsecWait) / 1e6);
0549 
0550     // Leave string empty if addrLocal invalid (not filled in yet)
0551     stats.addrLocal = addrLocal.IsValid() ? addrLocal.ToString() : "";
0552 }
0553 #undef X
0554 
0555 // requires LOCK(cs_vRecvMsg)
0556 bool CNode::ReceiveMsgBytes(const char *pch, unsigned int nBytes)
0557 {
0558     while (nBytes > 0) {
0559 
0560         // get current incomplete message, or create a new one
0561         if (vRecvMsg.empty() ||
0562             vRecvMsg.back().complete())
0563             vRecvMsg.push_back(CNetMessage(SER_NETWORK, nRecvVersion));
0564 
0565         CNetMessage& msg = vRecvMsg.back();
0566 
0567         // absorb network data
0568         int handled;
0569         if (!msg.in_data)
0570             handled = msg.readHeader(pch, nBytes);
0571         else
0572             handled = msg.readData(pch, nBytes);
0573 
0574         if (handled < 0)
0575                 return false;
0576 
0577         pch += handled;
0578         nBytes -= handled;
0579 
0580         if (msg.complete())
0581             msg.nTime = GetTimeMicros();
0582     }
0583 
0584     return true;
0585 }
0586 
0587 int CNetMessage::readHeader(const char *pch, unsigned int nBytes)
0588 {
0589     // copy data to temporary parsing buffer
0590     unsigned int nRemaining = 24 - nHdrPos;
0591     unsigned int nCopy = std::min(nRemaining, nBytes);
0592 
0593     memcpy(&hdrbuf[nHdrPos], pch, nCopy);
0594     nHdrPos += nCopy;
0595 
0596     // if header incomplete, exit
0597     if (nHdrPos < 24)
0598         return nCopy;
0599 
0600     // deserialize to CMessageHeader
0601     try {
0602         hdrbuf >> hdr;
0603     }
0604     catch (const std::exception &) {
0605         return -1;
0606     }
0607 
0608     // reject messages larger than MAX_SIZE
0609     if (hdr.nMessageSize > MAX_SIZE)
0610             return -1;
0611 
0612     // switch state to reading message data
0613     in_data = true;
0614 
0615     return nCopy;
0616 }
0617 
0618 int CNetMessage::readData(const char *pch, unsigned int nBytes)
0619 {
0620     unsigned int nRemaining = hdr.nMessageSize - nDataPos;
0621     unsigned int nCopy = std::min(nRemaining, nBytes);
0622 
0623     if (vRecv.size() < nDataPos + nCopy) {
0624         // Allocate up to 256 KiB ahead, but never more than the total message size.
0625         vRecv.resize(std::min(hdr.nMessageSize, nDataPos + nCopy + 256 * 1024));
0626     }
0627 
0628     memcpy(&vRecv[nDataPos], pch, nCopy);
0629     nDataPos += nCopy;
0630 
0631     return nCopy;
0632 }
0633 
0634 
0635 
0636 
0637 
0638 
0639 
0640 
0641 
0642 // requires LOCK(cs_vSend)
0643 void SocketSendData(CNode *pnode)
0644 {
0645     std::deque<CSerializeData>::iterator it = pnode->vSendMsg.begin();
0646 
0647     while (it != pnode->vSendMsg.end()) {
0648         const CSerializeData &data = *it;
0649         assert(data.size() > pnode->nSendOffset);
0650         int nBytes = send(pnode->hSocket, &data[pnode->nSendOffset], data.size() - pnode->nSendOffset, MSG_NOSIGNAL | MSG_DONTWAIT);
0651         if (nBytes > 0) {
0652             pnode->nLastSend = GetTime();
0653             pnode->nSendBytes += nBytes;
0654             pnode->nSendOffset += nBytes;
0655             pnode->RecordBytesSent(nBytes);
0656             if (pnode->nSendOffset == data.size()) {
0657                 pnode->nSendOffset = 0;
0658                 pnode->nSendSize -= data.size();
0659                 it++;
0660             } else {
0661                 // could not send full message; stop sending more
0662                 break;
0663             }
0664         } else {
0665             if (nBytes < 0) {
0666                 // error
0667                 int nErr = WSAGetLastError();
0668                 if (nErr != WSAEWOULDBLOCK && nErr != WSAEMSGSIZE && nErr != WSAEINTR && nErr != WSAEINPROGRESS)
0669                 {
0670                     LogPrintf("socket send error %s\n", NetworkErrorString(nErr));
0671                     pnode->CloseSocketDisconnect();
0672                 }
0673             }
0674             // couldn't send anything at all
0675             break;
0676         }
0677     }
0678 
0679     if (it == pnode->vSendMsg.end()) {
0680         assert(pnode->nSendOffset == 0);
0681         assert(pnode->nSendSize == 0);
0682     }
0683     pnode->vSendMsg.erase(pnode->vSendMsg.begin(), it);
0684 }
0685 
0686 static list<CNode*> vNodesDisconnected;
0687 
0688 void ThreadSocketHandler()
0689 {
0690     unsigned int nPrevNodeCount = 0;
0691     while (true)
0692     {
0693         //
0694         // Disconnect nodes
0695         //
0696         {
0697             LOCK(cs_vNodes);
0698             // Disconnect unused nodes
0699             vector<CNode*> vNodesCopy = vNodes;
0700             BOOST_FOREACH(CNode* pnode, vNodesCopy)
0701             {
0702                 if (pnode->fDisconnect ||
0703                     (pnode->GetRefCount() <= 0 && pnode->vRecvMsg.empty() && pnode->nSendSize == 0 && pnode->ssSend.empty()))
0704                 {
0705                     // remove from vNodes
0706                     vNodes.erase(remove(vNodes.begin(), vNodes.end(), pnode), vNodes.end());
0707 
0708                     // release outbound grant (if any)
0709                     pnode->grantOutbound.Release();
0710 
0711                     // close socket and cleanup
0712                     pnode->CloseSocketDisconnect();
0713 
0714                     // hold in disconnected pool until all refs are released
0715                     if (pnode->fNetworkNode || pnode->fInbound)
0716                         pnode->Release();
0717                     vNodesDisconnected.push_back(pnode);
0718                 }
0719             }
0720         }
0721         {
0722             // Delete disconnected nodes
0723             list<CNode*> vNodesDisconnectedCopy = vNodesDisconnected;
0724             BOOST_FOREACH(CNode* pnode, vNodesDisconnectedCopy)
0725             {
0726                 // wait until threads are done using it
0727                 if (pnode->GetRefCount() <= 0)
0728                 {
0729                     bool fDelete = false;
0730                     {
0731                         TRY_LOCK(pnode->cs_vSend, lockSend);
0732                         if (lockSend)
0733                         {
0734                             TRY_LOCK(pnode->cs_vRecvMsg, lockRecv);
0735                             if (lockRecv)
0736                             {
0737                                 TRY_LOCK(pnode->cs_inventory, lockInv);
0738                                 if (lockInv)
0739                                     fDelete = true;
0740                             }
0741                         }
0742                     }
0743                     if (fDelete)
0744                     {
0745                         vNodesDisconnected.remove(pnode);
0746                         delete pnode;
0747                     }
0748                 }
0749             }
0750         }
0751         if(vNodes.size() != nPrevNodeCount) {
0752             nPrevNodeCount = vNodes.size();
0753             uiInterface.NotifyNumConnectionsChanged(nPrevNodeCount);
0754         }
0755 
0756         //
0757         // Find which sockets have data to receive
0758         //
0759         struct timeval timeout;
0760         timeout.tv_sec  = 0;
0761         timeout.tv_usec = 50000; // frequency to poll pnode->vSend
0762 
0763         fd_set fdsetRecv;
0764         fd_set fdsetSend;
0765         fd_set fdsetError;
0766         FD_ZERO(&fdsetRecv);
0767         FD_ZERO(&fdsetSend);
0768         FD_ZERO(&fdsetError);
0769         SOCKET hSocketMax = 0;
0770         bool have_fds = false;
0771 
0772         BOOST_FOREACH(const ListenSocket& hListenSocket, vhListenSocket) {
0773             FD_SET(hListenSocket.socket, &fdsetRecv);
0774             hSocketMax = max(hSocketMax, hListenSocket.socket);
0775             have_fds = true;
0776         }
0777 
0778         {
0779             LOCK(cs_vNodes);
0780             BOOST_FOREACH(CNode* pnode, vNodes)
0781             {
0782                 if (pnode->hSocket == INVALID_SOCKET)
0783                     continue;
0784                 FD_SET(pnode->hSocket, &fdsetError);
0785                 hSocketMax = max(hSocketMax, pnode->hSocket);
0786                 have_fds = true;
0787 
0788                 // Implement the following logic:
0789                 // * If there is data to send, select() for sending data. As this only
0790                 //   happens when optimistic write failed, we choose to first drain the
0791                 //   write buffer in this case before receiving more. This avoids
0792                 //   needlessly queueing received data, if the remote peer is not themselves
0793                 //   receiving data. This means properly utilizing TCP flow control signalling.
0794                 // * Otherwise, if there is no (complete) message in the receive buffer,
0795                 //   or there is space left in the buffer, select() for receiving data.
0796                 // * (if neither of the above applies, there is certainly one message
0797                 //   in the receiver buffer ready to be processed).
0798                 // Together, that means that at least one of the following is always possible,
0799                 // so we don't deadlock:
0800                 // * We send some data.
0801                 // * We wait for data to be received (and disconnect after timeout).
0802                 // * We process a message in the buffer (message handler thread).
0803                 {
0804                     TRY_LOCK(pnode->cs_vSend, lockSend);
0805                     if (lockSend && !pnode->vSendMsg.empty()) {
0806                         FD_SET(pnode->hSocket, &fdsetSend);
0807                         continue;
0808                     }
0809                 }
0810                 {
0811                     TRY_LOCK(pnode->cs_vRecvMsg, lockRecv);
0812                     if (lockRecv && (
0813                         pnode->vRecvMsg.empty() || !pnode->vRecvMsg.front().complete() ||
0814                         pnode->GetTotalRecvSize() <= ReceiveFloodSize()))
0815                         FD_SET(pnode->hSocket, &fdsetRecv);
0816                 }
0817             }
0818         }
0819 
0820         int nSelect = select(have_fds ? hSocketMax + 1 : 0,
0821                              &fdsetRecv, &fdsetSend, &fdsetError, &timeout);
0822         boost::this_thread::interruption_point();
0823 
0824         if (nSelect == SOCKET_ERROR)
0825         {
0826             if (have_fds)
0827             {
0828                 int nErr = WSAGetLastError();
0829                 LogPrintf("socket select error %s\n", NetworkErrorString(nErr));
0830                 for (unsigned int i = 0; i <= hSocketMax; i++)
0831                     FD_SET(i, &fdsetRecv);
0832             }
0833             FD_ZERO(&fdsetSend);
0834             FD_ZERO(&fdsetError);
0835             MilliSleep(timeout.tv_usec/1000);
0836         }
0837 
0838         //
0839         // Accept new connections
0840         //
0841         BOOST_FOREACH(const ListenSocket& hListenSocket, vhListenSocket)
0842         {
0843             if (hListenSocket.socket != INVALID_SOCKET && FD_ISSET(hListenSocket.socket, &fdsetRecv))
0844             {
0845                 struct sockaddr_storage sockaddr;
0846                 socklen_t len = sizeof(sockaddr);
0847                 SOCKET hSocket = accept(hListenSocket.socket, (struct sockaddr*)&sockaddr, &len);
0848                 CAddress addr;
0849                 int nInbound = 0;
0850 
0851                 if (hSocket != INVALID_SOCKET)
0852                     if (!addr.SetSockAddr((const struct sockaddr*)&sockaddr))
0853                         LogPrintf("Warning: Unknown socket family\n");
0854 
0855                 bool whitelisted = hListenSocket.whitelisted || CNode::IsWhitelistedRange(addr);
0856                 {
0857                     LOCK(cs_vNodes);
0858                     BOOST_FOREACH(CNode* pnode, vNodes)
0859                         if (pnode->fInbound)
0860                             nInbound++;
0861                 }
0862 
0863                 if (hSocket == INVALID_SOCKET)
0864                 {
0865                     int nErr = WSAGetLastError();
0866                     if (nErr != WSAEWOULDBLOCK)
0867                         LogPrintf("socket error accept failed: %s\n", NetworkErrorString(nErr));
0868                 }
0869                 else if (nInbound >= nMaxConnections - MAX_OUTBOUND_CONNECTIONS)
0870                 {
0871                     CloseSocket(hSocket);
0872                 }
0873                 else if (CNode::IsBanned(addr) && !whitelisted)
0874                 {
0875                     LogPrintf("connection from %s dropped (banned)\n", addr.ToString());
0876                     CloseSocket(hSocket);
0877                 }
0878                 else
0879                 {
0880                     CNode* pnode = new CNode(hSocket, addr, "", true);
0881                     pnode->AddRef();
0882                     pnode->fWhitelisted = whitelisted;
0883 
0884                     {
0885                         LOCK(cs_vNodes);
0886                         vNodes.push_back(pnode);
0887                     }
0888                 }
0889             }
0890         }
0891 
0892         //
0893         // Service each socket
0894         //
0895         vector<CNode*> vNodesCopy;
0896         {
0897             LOCK(cs_vNodes);
0898             vNodesCopy = vNodes;
0899             BOOST_FOREACH(CNode* pnode, vNodesCopy)
0900                 pnode->AddRef();
0901         }
0902         BOOST_FOREACH(CNode* pnode, vNodesCopy)
0903         {
0904             boost::this_thread::interruption_point();
0905 
0906             //
0907             // Receive
0908             //
0909             if (pnode->hSocket == INVALID_SOCKET)
0910                 continue;
0911             if (FD_ISSET(pnode->hSocket, &fdsetRecv) || FD_ISSET(pnode->hSocket, &fdsetError))
0912             {
0913                 TRY_LOCK(pnode->cs_vRecvMsg, lockRecv);
0914                 if (lockRecv)
0915                 {
0916                     {
0917                         // typical socket buffer is 8K-64K
0918                         char pchBuf[0x10000];
0919                         int nBytes = recv(pnode->hSocket, pchBuf, sizeof(pchBuf), MSG_DONTWAIT);
0920                         if (nBytes > 0)
0921                         {
0922                             if (!pnode->ReceiveMsgBytes(pchBuf, nBytes))
0923                                 pnode->CloseSocketDisconnect();
0924                             pnode->nLastRecv = GetTime();
0925                             pnode->nRecvBytes += nBytes;
0926                             pnode->RecordBytesRecv(nBytes);
0927                         }
0928                         else if (nBytes == 0)
0929                         {
0930                             // socket closed gracefully
0931                             if (!pnode->fDisconnect)
0932                                 LogPrint("net", "socket closed\n");
0933                             pnode->CloseSocketDisconnect();
0934                         }
0935                         else if (nBytes < 0)
0936                         {
0937                             // error
0938                             int nErr = WSAGetLastError();
0939                             if (nErr != WSAEWOULDBLOCK && nErr != WSAEMSGSIZE && nErr != WSAEINTR && nErr != WSAEINPROGRESS)
0940                             {
0941                                 if (!pnode->fDisconnect)
0942                                     LogPrintf("socket recv error %s\n", NetworkErrorString(nErr));
0943                                 pnode->CloseSocketDisconnect();
0944                             }
0945                         }
0946                     }
0947                 }
0948             }
0949 
0950             //
0951             // Send
0952             //
0953             if (pnode->hSocket == INVALID_SOCKET)
0954                 continue;
0955             if (FD_ISSET(pnode->hSocket, &fdsetSend))
0956             {
0957                 TRY_LOCK(pnode->cs_vSend, lockSend);
0958                 if (lockSend)
0959                     SocketSendData(pnode);
0960             }
0961 
0962             //
0963             // Inactivity checking
0964             //
0965             int64_t nTime = GetTime();
0966             if (nTime - pnode->nTimeConnected > 60)
0967             {
0968                 if (pnode->nLastRecv == 0 || pnode->nLastSend == 0)
0969                 {
0970                     LogPrint("net", "socket no message in first 60 seconds, %d %d from %d\n", pnode->nLastRecv != 0, pnode->nLastSend != 0, pnode->id);
0971                     pnode->fDisconnect = true;
0972                 }
0973                 else if (nTime - pnode->nLastSend > TIMEOUT_INTERVAL)
0974                 {
0975                     LogPrintf("socket sending timeout: %is\n", nTime - pnode->nLastSend);
0976                     pnode->fDisconnect = true;
0977                 }
0978                 else if (nTime - pnode->nLastRecv > (pnode->nVersion > BIP0031_VERSION ? TIMEOUT_INTERVAL : 90*60))
0979                 {
0980                     LogPrintf("socket receive timeout: %is\n", nTime - pnode->nLastRecv);
0981                     pnode->fDisconnect = true;
0982                 }
0983                 else if (pnode->nPingNonceSent && pnode->nPingUsecStart + TIMEOUT_INTERVAL * 1000000 < GetTimeMicros())
0984                 {
0985                     LogPrintf("ping timeout: %fs\n", 0.000001 * (GetTimeMicros() - pnode->nPingUsecStart));
0986                     pnode->fDisconnect = true;
0987                 }
0988             }
0989         }
0990         {
0991             LOCK(cs_vNodes);
0992             BOOST_FOREACH(CNode* pnode, vNodesCopy)
0993                 pnode->Release();
0994         }
0995     }
0996 }
0997 
0998 
0999 
1000 
1001 
1002 
1003 
1004 
1005 
1006 #ifdef USE_UPNP
1007 void ThreadMapPort()
1008 {
1009     std::string port = strprintf("%u", GetListenPort());
1010     const char * multicastif = 0;
1011     const char * minissdpdpath = 0;
1012     struct UPNPDev * devlist = 0;
1013     char lanaddr[64];
1014 
1015 #ifndef UPNPDISCOVER_SUCCESS
1016     /* miniupnpc 1.5 */
1017     devlist = upnpDiscover(2000, multicastif, minissdpdpath, 0);
1018 #else
1019     /* miniupnpc 1.6 */
1020     int error = 0;
1021     devlist = upnpDiscover(2000, multicastif, minissdpdpath, 0, 0, &error);
1022 #endif
1023 
1024     struct UPNPUrls urls;
1025     struct IGDdatas data;
1026     int r;
1027 
1028     r = UPNP_GetValidIGD(devlist, &urls, &data, lanaddr, sizeof(lanaddr));
1029     if (r == 1)
1030     {
1031         if (fDiscover) {
1032             char externalIPAddress[40];
1033             r = UPNP_GetExternalIPAddress(urls.controlURL, data.first.servicetype, externalIPAddress);
1034             if(r != UPNPCOMMAND_SUCCESS)
1035                 LogPrintf("UPnP: GetExternalIPAddress() returned %d\n", r);
1036             else
1037             {
1038                 if(externalIPAddress[0])
1039                 {
1040                     LogPrintf("UPnP: ExternalIPAddress = %s\n", externalIPAddress);
1041                     AddLocal(CNetAddr(externalIPAddress), LOCAL_UPNP);
1042                 }
1043                 else
1044                     LogPrintf("UPnP: GetExternalIPAddress failed.\n");
1045             }
1046         }
1047 
1048         string strDesc = "Bitcoin " + FormatFullVersion();
1049 
1050         try {
1051             while (true) {
1052 #ifndef UPNPDISCOVER_SUCCESS
1053                 /* miniupnpc 1.5 */
1054                 r = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype,
1055                                     port.c_str(), port.c_str(), lanaddr, strDesc.c_str(), "TCP", 0);
1056 #else
1057                 /* miniupnpc 1.6 */
1058                 r = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype,
1059                                     port.c_str(), port.c_str(), lanaddr, strDesc.c_str(), "TCP", 0, "0");
1060 #endif
1061 
1062                 if(r!=UPNPCOMMAND_SUCCESS)
1063                     LogPrintf("AddPortMapping(%s, %s, %s) failed with code %d (%s)\n",
1064                         port, port, lanaddr, r, strupnperror(r));
1065                 else
1066                     LogPrintf("UPnP Port Mapping successful.\n");;
1067 
1068                 MilliSleep(20*60*1000); // Refresh every 20 minutes
1069             }
1070         }
1071         catch (boost::thread_interrupted)
1072         {
1073             r = UPNP_DeletePortMapping(urls.controlURL, data.first.servicetype, port.c_str(), "TCP", 0);
1074             LogPrintf("UPNP_DeletePortMapping() returned : %d\n", r);
1075             freeUPNPDevlist(devlist); devlist = 0;
1076             FreeUPNPUrls(&urls);
1077             throw;
1078         }
1079     } else {
1080         LogPrintf("No valid UPnP IGDs found\n");
1081         freeUPNPDevlist(devlist); devlist = 0;
1082         if (r != 0)
1083             FreeUPNPUrls(&urls);
1084     }
1085 }
1086 
1087 void MapPort(bool fUseUPnP)
1088 {
1089     static boost::thread* upnp_thread = NULL;
1090 
1091     if (fUseUPnP)
1092     {
1093         if (upnp_thread) {
1094             upnp_thread->interrupt();
1095             upnp_thread->join();
1096             delete upnp_thread;
1097         }
1098         upnp_thread = new boost::thread(boost::bind(&TraceThread<void (*)()>, "upnp", &ThreadMapPort));
1099     }
1100     else if (upnp_thread) {
1101         upnp_thread->interrupt();
1102         upnp_thread->join();
1103         delete upnp_thread;
1104         upnp_thread = NULL;
1105     }
1106 }
1107 
1108 #else
1109 void MapPort(bool)
1110 {
1111     // Intentionally left blank.
1112 }
1113 #endif
1114 
1115 
1116 
1117 
1118 
1119 
1120 void ThreadDNSAddressSeed()
1121 {
1122     // goal: only query DNS seeds if address need is acute
1123     if ((addrman.size() > 0) &&
1124         (!GetBoolArg("-forcednsseed", false))) {
1125         MilliSleep(11 * 1000);
1126 
1127         LOCK(cs_vNodes);
1128         if (vNodes.size() >= 2) {
1129             LogPrintf("P2P peers available. Skipped DNS seeding.\n");
1130             return;
1131         }
1132     }
1133 
1134     const vector<CDNSSeedData> &vSeeds = Params().DNSSeeds();
1135     int found = 0;
1136 
1137     LogPrintf("Loading addresses from DNS seeds (could take a while)\n");
1138 
1139     BOOST_FOREACH(const CDNSSeedData &seed, vSeeds) {
1140         if (HaveNameProxy()) {
1141             AddOneShot(seed.host);
1142         } else {
1143             vector<CNetAddr> vIPs;
1144             vector<CAddress> vAdd;
1145             if (LookupHost(seed.host.c_str(), vIPs))
1146             {
1147                 BOOST_FOREACH(CNetAddr& ip, vIPs)
1148                 {
1149                     int nOneDay = 24*3600;
1150                     CAddress addr = CAddress(CService(ip, Params().GetDefaultPort()));
1151                     addr.nTime = GetTime() - 3*nOneDay - GetRand(4*nOneDay); // use a random age between 3 and 7 days old
1152                     vAdd.push_back(addr);
1153                     found++;
1154                 }
1155             }
1156             addrman.Add(vAdd, CNetAddr(seed.name, true));
1157         }
1158     }
1159 
1160     LogPrintf("%d addresses found from DNS seeds\n", found);
1161 }
1162 
1163 
1164 
1165 
1166 
1167 
1168 
1169 
1170 
1171 
1172 
1173 
1174 void DumpAddresses()
1175 {
1176     int64_t nStart = GetTimeMillis();
1177 
1178     CAddrDB adb;
1179     adb.Write(addrman);
1180 
1181     LogPrint("net", "Flushed %d addresses to peers.dat  %dms\n",
1182            addrman.size(), GetTimeMillis() - nStart);
1183 }
1184 
1185 void static ProcessOneShot()
1186 {
1187     string strDest;
1188     {
1189         LOCK(cs_vOneShots);
1190         if (vOneShots.empty())
1191             return;
1192         strDest = vOneShots.front();
1193         vOneShots.pop_front();
1194     }
1195     CAddress addr;
1196     CSemaphoreGrant grant(*semOutbound, true);
1197     if (grant) {
1198         if (!OpenNetworkConnection(addr, &grant, strDest.c_str(), true))
1199             AddOneShot(strDest);
1200     }
1201 }
1202 
1203 void ThreadOpenConnections()
1204 {
1205     // Connect to specific addresses
1206     if (mapArgs.count("-connect") && mapMultiArgs["-connect"].size() > 0)
1207     {
1208         for (int64_t nLoop = 0;; nLoop++)
1209         {
1210             ProcessOneShot();
1211             BOOST_FOREACH(string strAddr, mapMultiArgs["-connect"])
1212             {
1213                 CAddress addr;
1214                 OpenNetworkConnection(addr, NULL, strAddr.c_str());
1215                 for (int i = 0; i < 10 && i < nLoop; i++)
1216                 {
1217                     MilliSleep(500);
1218                 }
1219             }
1220             MilliSleep(500);
1221         }
1222     }
1223 
1224     // Initiate network connections
1225     int64_t nStart = GetTime();
1226     while (true)
1227     {
1228         ProcessOneShot();
1229 
1230         MilliSleep(500);
1231 
1232         CSemaphoreGrant grant(*semOutbound);
1233         boost::this_thread::interruption_point();
1234 
1235         // Add seed nodes if DNS seeds are all down (an infrastructure attack?).
1236         if (addrman.size() == 0 && (GetTime() - nStart > 60)) {
1237             static bool done = false;
1238             if (!done) {
1239                 LogPrintf("Adding fixed seed nodes as DNS doesn't seem to be available.\n");
1240                 addrman.Add(Params().FixedSeeds(), CNetAddr("127.0.0.1"));
1241                 done = true;
1242             }
1243         }
1244 
1245         //
1246         // Choose an address to connect to based on most recently seen
1247         //
1248         CAddress addrConnect;
1249 
1250         // Only connect out to one peer per network group (/16 for IPv4).
1251         // Do this here so we don't have to critsect vNodes inside mapAddresses critsect.
1252         int nOutbound = 0;
1253         set<vector<unsigned char> > setConnected;
1254         {
1255             LOCK(cs_vNodes);
1256             BOOST_FOREACH(CNode* pnode, vNodes) {
1257                 if (!pnode->fInbound) {
1258                     setConnected.insert(pnode->addr.GetGroup());
1259                     nOutbound++;
1260                 }
1261             }
1262         }
1263 
1264         int64_t nANow = GetAdjustedTime();
1265 
1266         int nTries = 0;
1267         while (true)
1268         {
1269             // use an nUnkBias between 10 (no outgoing connections) and 90 (8 outgoing connections)
1270             CAddress addr = addrman.Select(10 + min(nOutbound,8)*10);
1271 
1272             // if we selected an invalid address, restart
1273             if (!addr.IsValid() || setConnected.count(addr.GetGroup()) || IsLocal(addr))
1274                 break;
1275 
1276             // If we didn't find an appropriate destination after trying 100 addresses fetched from addrman,
1277             // stop this loop, and let the outer loop run again (which sleeps, adds seed nodes, recalculates
1278             // already-connected network ranges, ...) before trying new addrman addresses.
1279             nTries++;
1280             if (nTries > 100)
1281                 break;
1282 
1283             if (IsLimited(addr))
1284                 continue;
1285 
1286             // only consider very recently tried nodes after 30 failed attempts
1287             if (nANow - addr.nLastTry < 600 && nTries < 30)
1288                 continue;
1289 
1290             // do not allow non-default ports, unless after 50 invalid addresses selected already
1291             if (addr.GetPort() != Params().GetDefaultPort() && nTries < 50)
1292                 continue;
1293 
1294             addrConnect = addr;
1295             break;
1296         }
1297 
1298         if (addrConnect.IsValid())
1299             OpenNetworkConnection(addrConnect, &grant);
1300     }
1301 }
1302 
1303 void ThreadOpenAddedConnections()
1304 {
1305     {
1306         LOCK(cs_vAddedNodes);
1307         vAddedNodes = mapMultiArgs["-addnode"];
1308     }
1309 
1310     if (HaveNameProxy()) {
1311         while(true) {
1312             list<string> lAddresses(0);
1313             {
1314                 LOCK(cs_vAddedNodes);
1315                 BOOST_FOREACH(string& strAddNode, vAddedNodes)
1316                     lAddresses.push_back(strAddNode);
1317             }
1318             BOOST_FOREACH(string& strAddNode, lAddresses) {
1319                 CAddress addr;
1320                 CSemaphoreGrant grant(*semOutbound);
1321                 OpenNetworkConnection(addr, &grant, strAddNode.c_str());
1322                 MilliSleep(500);
1323             }
1324             MilliSleep(120000); // Retry every 2 minutes
1325         }
1326     }
1327 
1328     for (unsigned int i = 0; true; i++)
1329     {
1330         list<string> lAddresses(0);
1331         {
1332             LOCK(cs_vAddedNodes);
1333             BOOST_FOREACH(string& strAddNode, vAddedNodes)
1334                 lAddresses.push_back(strAddNode);
1335         }
1336 
1337         list<vector<CService> > lservAddressesToAdd(0);
1338         BOOST_FOREACH(string& strAddNode, lAddresses)
1339         {
1340             vector<CService> vservNode(0);
1341             if(Lookup(strAddNode.c_str(), vservNode, Params().GetDefaultPort(), fNameLookup, 0))
1342             {
1343                 lservAddressesToAdd.push_back(vservNode);
1344                 {
1345                     LOCK(cs_setservAddNodeAddresses);
1346                     BOOST_FOREACH(CService& serv, vservNode)
1347                         setservAddNodeAddresses.insert(serv);
1348                 }
1349             }
1350         }
1351         // Attempt to connect to each IP for each addnode entry until at least one is successful per addnode entry
1352         // (keeping in mind that addnode entries can have many IPs if fNameLookup)
1353         {
1354             LOCK(cs_vNodes);
1355             BOOST_FOREACH(CNode* pnode, vNodes)
1356                 for (list<vector<CService> >::iterator it = lservAddressesToAdd.begin(); it != lservAddressesToAdd.end(); it++)
1357                     BOOST_FOREACH(CService& addrNode, *(it))
1358                         if (pnode->addr == addrNode)
1359                         {
1360                             it = lservAddressesToAdd.erase(it);
1361                             it--;
1362                             break;
1363                         }
1364         }
1365         BOOST_FOREACH(vector<CService>& vserv, lservAddressesToAdd)
1366         {
1367             CSemaphoreGrant grant(*semOutbound);
1368             OpenNetworkConnection(CAddress(vserv[i % vserv.size()]), &grant);
1369             MilliSleep(500);
1370         }
1371         MilliSleep(120000); // Retry every 2 minutes
1372     }
1373 }
1374 
1375 // if successful, this moves the passed grant to the constructed node
1376 bool OpenNetworkConnection(const CAddress& addrConnect, CSemaphoreGrant *grantOutbound, const char *pszDest, bool fOneShot)
1377 {
1378     //
1379     // Initiate outbound network connection
1380     //
1381     boost::this_thread::interruption_point();
1382     if (!pszDest) {
1383         if (IsLocal(addrConnect) ||
1384             FindNode((CNetAddr)addrConnect) || CNode::IsBanned(addrConnect) ||
1385             FindNode(addrConnect.ToStringIPPort()))
1386             return false;
1387     } else if (FindNode(pszDest))
1388         return false;
1389 
1390     CNode* pnode = ConnectNode(addrConnect, pszDest);
1391     boost::this_thread::interruption_point();
1392 
1393     if (!pnode)
1394         return false;
1395     if (grantOutbound)
1396         grantOutbound->MoveTo(pnode->grantOutbound);
1397     pnode->fNetworkNode = true;
1398     if (fOneShot)
1399         pnode->fOneShot = true;
1400 
1401     return true;
1402 }
1403 
1404 
1405 void ThreadMessageHandler()
1406 {
1407     SetThreadPriority(THREAD_PRIORITY_BELOW_NORMAL);
1408     while (true)
1409     {
1410         vector<CNode*> vNodesCopy;
1411         {
1412             LOCK(cs_vNodes);
1413             vNodesCopy = vNodes;
1414             BOOST_FOREACH(CNode* pnode, vNodesCopy) {
1415                 pnode->AddRef();
1416             }
1417         }
1418 
1419         // Poll the connected nodes for messages
1420         CNode* pnodeTrickle = NULL;
1421         if (!vNodesCopy.empty())
1422             pnodeTrickle = vNodesCopy[GetRand(vNodesCopy.size())];
1423 
1424         bool fSleep = true;
1425 
1426         BOOST_FOREACH(CNode* pnode, vNodesCopy)
1427         {
1428             if (pnode->fDisconnect)
1429                 continue;
1430 
1431             // Receive messages
1432             {
1433                 TRY_LOCK(pnode->cs_vRecvMsg, lockRecv);
1434                 if (lockRecv)
1435                 {
1436                     if (!g_signals.ProcessMessages(pnode))
1437                         pnode->CloseSocketDisconnect();
1438 
1439                     if (pnode->nSendSize < SendBufferSize())
1440                     {
1441                         if (!pnode->vRecvGetData.empty() || (!pnode->vRecvMsg.empty() && pnode->vRecvMsg[0].complete()))
1442                         {
1443                             fSleep = false;
1444                         }
1445                     }
1446                 }
1447             }
1448             boost::this_thread::interruption_point();
1449 
1450             // Send messages
1451             {
1452                 TRY_LOCK(pnode->cs_vSend, lockSend);
1453                 if (lockSend)
1454                     g_signals.SendMessages(pnode, pnode == pnodeTrickle);
1455             }
1456             boost::this_thread::interruption_point();
1457         }
1458 
1459         {
1460             LOCK(cs_vNodes);
1461             BOOST_FOREACH(CNode* pnode, vNodesCopy)
1462                 pnode->Release();
1463         }
1464 
1465         if (fSleep)
1466             MilliSleep(100);
1467     }
1468 }
1469 
1470 
1471 
1472 
1473 
1474 
1475 bool BindListenPort(const CService &addrBind, string& strError, bool fWhitelisted)
1476 {
1477     strError = "";
1478     int nOne = 1;
1479 
1480     // Create socket for listening for incoming connections
1481     struct sockaddr_storage sockaddr;
1482     socklen_t len = sizeof(sockaddr);
1483     if (!addrBind.GetSockAddr((struct sockaddr*)&sockaddr, &len))
1484     {
1485         strError = strprintf("Error: Bind address family for %s not supported", addrBind.ToString());
1486         LogPrintf("%s\n", strError);
1487         return false;
1488     }
1489 
1490     SOCKET hListenSocket = socket(((struct sockaddr*)&sockaddr)->sa_family, SOCK_STREAM, IPPROTO_TCP);
1491     if (hListenSocket == INVALID_SOCKET)
1492     {
1493         strError = strprintf("Error: Couldn't open socket for incoming connections (socket returned error %s)", NetworkErrorString(WSAGetLastError()));
1494         LogPrintf("%s\n", strError);
1495         return false;
1496     }
1497 
1498 #ifndef WIN32
1499 #ifdef SO_NOSIGPIPE
1500     // Different way of disabling SIGPIPE on BSD
1501     setsockopt(hListenSocket, SOL_SOCKET, SO_NOSIGPIPE, (void*)&nOne, sizeof(int));
1502 #endif
1503     // Allow binding if the port is still in TIME_WAIT state after
1504     // the program was closed and restarted. Not an issue on windows!
1505     setsockopt(hListenSocket, SOL_SOCKET, SO_REUSEADDR, (void*)&nOne, sizeof(int));
1506 #endif
1507 
1508     // Set to non-blocking, incoming connections will also inherit this
1509     if (!SetSocketNonBlocking(hListenSocket, true)) {
1510         strError = strprintf("BindListenPort: Setting listening socket to non-blocking failed, error %s\n", NetworkErrorString(WSAGetLastError()));
1511         LogPrintf("%s\n", strError);
1512         return false;
1513     }
1514 
1515     // some systems don't have IPV6_V6ONLY but are always v6only; others do have the option
1516     // and enable it by default or not. Try to enable it, if possible.
1517     if (addrBind.IsIPv6()) {
1518 #ifdef IPV6_V6ONLY
1519 #ifdef WIN32
1520         setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_V6ONLY, (const char*)&nOne, sizeof(int));
1521 #else
1522         setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_V6ONLY, (void*)&nOne, sizeof(int));
1523 #endif
1524 #endif
1525 #ifdef WIN32
1526         int nProtLevel = PROTECTION_LEVEL_UNRESTRICTED;
1527         setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_PROTECTION_LEVEL, (const char*)&nProtLevel, sizeof(int));
1528 #endif
1529     }
1530 
1531     if (::bind(hListenSocket, (struct sockaddr*)&sockaddr, len) == SOCKET_ERROR)
1532     {
1533         int nErr = WSAGetLastError();
1534         if (nErr == WSAEADDRINUSE)
1535             strError = strprintf(_("Unable to bind to %s on this computer. Bitcoin Core is probably already running."), addrBind.ToString());
1536         else
1537             strError = strprintf(_("Unable to bind to %s on this computer (bind returned error %s)"), addrBind.ToString(), NetworkErrorString(nErr));
1538         LogPrintf("%s\n", strError);
1539         CloseSocket(hListenSocket);
1540         return false;
1541     }
1542     LogPrintf("Bound to %s\n", addrBind.ToString());
1543 
1544     // Listen for incoming connections
1545     if (listen(hListenSocket, SOMAXCONN) == SOCKET_ERROR)
1546     {
1547         strError = strprintf(_("Error: Listening for incoming connections failed (listen returned error %s)"), NetworkErrorString(WSAGetLastError()));
1548         LogPrintf("%s\n", strError);
1549         CloseSocket(hListenSocket);
1550         return false;
1551     }
1552 
1553     vhListenSocket.push_back(ListenSocket(hListenSocket, fWhitelisted));
1554 
1555     if (addrBind.IsRoutable() && fDiscover && !fWhitelisted)
1556         AddLocal(addrBind, LOCAL_BIND);
1557 
1558     return true;
1559 }
1560 
1561 void static Discover(boost::thread_group& threadGroup)
1562 {
1563     if (!fDiscover)
1564         return;
1565 
1566 #ifdef WIN32
1567     // Get local host IP
1568     char pszHostName[256] = "";
1569     if (gethostname(pszHostName, sizeof(pszHostName)) != SOCKET_ERROR)
1570     {
1571         vector<CNetAddr> vaddr;
1572         if (LookupHost(pszHostName, vaddr))
1573         {
1574             BOOST_FOREACH (const CNetAddr &addr, vaddr)
1575             {
1576                 if (AddLocal(addr, LOCAL_IF))
1577                     LogPrintf("%s: %s - %s\n", __func__, pszHostName, addr.ToString());
1578             }
1579         }
1580     }
1581 #else
1582     // Get local host ip
1583     struct ifaddrs* myaddrs;
1584     if (getifaddrs(&myaddrs) == 0)
1585     {
1586         for (struct ifaddrs* ifa = myaddrs; ifa != NULL; ifa = ifa->ifa_next)
1587         {
1588             if (ifa->ifa_addr == NULL) continue;
1589             if ((ifa->ifa_flags & IFF_UP) == 0) continue;
1590             if (strcmp(ifa->ifa_name, "lo") == 0) continue;
1591             if (strcmp(ifa->ifa_name, "lo0") == 0) continue;
1592             if (ifa->ifa_addr->sa_family == AF_INET)
1593             {
1594                 struct sockaddr_in* s4 = (struct sockaddr_in*)(ifa->ifa_addr);
1595                 CNetAddr addr(s4->sin_addr);
1596                 if (AddLocal(addr, LOCAL_IF))
1597                     LogPrintf("%s: IPv4 %s: %s\n", __func__, ifa->ifa_name, addr.ToString());
1598             }
1599             else if (ifa->ifa_addr->sa_family == AF_INET6)
1600             {
1601                 struct sockaddr_in6* s6 = (struct sockaddr_in6*)(ifa->ifa_addr);
1602                 CNetAddr addr(s6->sin6_addr);
1603                 if (AddLocal(addr, LOCAL_IF))
1604                     LogPrintf("%s: IPv6 %s: %s\n", __func__, ifa->ifa_name, addr.ToString());
1605             }
1606         }
1607         freeifaddrs(myaddrs);
1608     }
1609 #endif
1610 }
1611 
1612 void StartNode(boost::thread_group& threadGroup)
1613 {
1614     uiInterface.InitMessage(_("Loading addresses..."));
1615     // Load addresses for peers.dat
1616     int64_t nStart = GetTimeMillis();
1617     {
1618         CAddrDB adb;
1619         if (!adb.Read(addrman))
1620             LogPrintf("Invalid or missing peers.dat; recreating\n");
1621     }
1622     LogPrintf("Loaded %i addresses from peers.dat  %dms\n",
1623            addrman.size(), GetTimeMillis() - nStart);
1624     fAddressesInitialized = true;
1625 
1626     if (semOutbound == NULL) {
1627         // initialize semaphore
1628         int nMaxOutbound = min(MAX_OUTBOUND_CONNECTIONS, nMaxConnections);
1629         semOutbound = new CSemaphore(nMaxOutbound);
1630     }
1631 
1632     if (pnodeLocalHost == NULL)
1633         pnodeLocalHost = new CNode(INVALID_SOCKET, CAddress(CService("127.0.0.1", 0), nLocalServices));
1634 
1635     Discover(threadGroup);
1636 
1637     //
1638     // Start threads
1639     //
1640 
1641     if (!GetBoolArg("-dnsseed", true))
1642         LogPrintf("DNS seeding disabled\n");
1643     else
1644         threadGroup.create_thread(boost::bind(&TraceThread<void (*)()>, "dnsseed", &ThreadDNSAddressSeed));
1645 
1646     // Map ports with UPnP
1647     MapPort(GetBoolArg("-upnp", DEFAULT_UPNP));
1648 
1649     // Send and receive from sockets, accept connections
1650     threadGroup.create_thread(boost::bind(&TraceThread<void (*)()>, "net", &ThreadSocketHandler));
1651 
1652     // Initiate outbound connections from -addnode
1653     threadGroup.create_thread(boost::bind(&TraceThread<void (*)()>, "addcon", &ThreadOpenAddedConnections));
1654 
1655     // Initiate outbound connections
1656     threadGroup.create_thread(boost::bind(&TraceThread<void (*)()>, "opencon", &ThreadOpenConnections));
1657 
1658     // Process messages
1659     threadGroup.create_thread(boost::bind(&TraceThread<void (*)()>, "msghand", &ThreadMessageHandler));
1660 
1661     // Dump network addresses
1662     threadGroup.create_thread(boost::bind(&LoopForever<void (*)()>, "dumpaddr", &DumpAddresses, DUMP_ADDRESSES_INTERVAL * 1000));
1663 }
1664 
1665 bool StopNode()
1666 {
1667     LogPrintf("StopNode()\n");
1668     MapPort(false);
1669     if (semOutbound)
1670         for (int i=0; i<MAX_OUTBOUND_CONNECTIONS; i++)
1671             semOutbound->post();
1672 
1673     if (fAddressesInitialized)
1674     {
1675         DumpAddresses();
1676         fAddressesInitialized = false;
1677     }
1678 
1679     return true;
1680 }
1681 
1682 class CNetCleanup
1683 {
1684 public:
1685     CNetCleanup() {}
1686 
1687     ~CNetCleanup()
1688     {
1689         // Close sockets
1690         BOOST_FOREACH(CNode* pnode, vNodes)
1691             if (pnode->hSocket != INVALID_SOCKET)
1692                 CloseSocket(pnode->hSocket);
1693         BOOST_FOREACH(ListenSocket& hListenSocket, vhListenSocket)
1694             if (hListenSocket.socket != INVALID_SOCKET)
1695                 if (!CloseSocket(hListenSocket.socket))
1696                     LogPrintf("CloseSocket(hListenSocket) failed with error %s\n", NetworkErrorString(WSAGetLastError()));
1697 
1698         // clean up some globals (to help leak detection)
1699         BOOST_FOREACH(CNode *pnode, vNodes)
1700             delete pnode;
1701         BOOST_FOREACH(CNode *pnode, vNodesDisconnected)
1702             delete pnode;
1703         vNodes.clear();
1704         vNodesDisconnected.clear();
1705         vhListenSocket.clear();
1706         delete semOutbound;
1707         semOutbound = NULL;
1708         delete pnodeLocalHost;
1709         pnodeLocalHost = NULL;
1710 
1711 #ifdef WIN32
1712         // Shutdown Windows Sockets
1713         WSACleanup();
1714 #endif
1715     }
1716 }
1717 instance_of_cnetcleanup;
1718 
1719 
1720 
1721 
1722 
1723 
1724 
1725 void RelayTransaction(const CTransaction& tx)
1726 {
1727     CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
1728     ss.reserve(10000);
1729     ss << tx;
1730     RelayTransaction(tx, ss);
1731 }
1732 
1733 void RelayTransaction(const CTransaction& tx, const CDataStream& ss)
1734 {
1735     CInv inv(MSG_TX, tx.GetHash());
1736     {
1737         LOCK(cs_mapRelay);
1738         // Expire old relay messages
1739         while (!vRelayExpiration.empty() && vRelayExpiration.front().first < GetTime())
1740         {
1741             mapRelay.erase(vRelayExpiration.front().second);
1742             vRelayExpiration.pop_front();
1743         }
1744 
1745         // Save original serialized message so newer versions are preserved
1746         mapRelay.insert(std::make_pair(inv, ss));
1747         vRelayExpiration.push_back(std::make_pair(GetTime() + 15 * 60, inv));
1748     }
1749     LOCK(cs_vNodes);
1750     BOOST_FOREACH(CNode* pnode, vNodes)
1751     {
1752         if(!pnode->fRelayTxes)
1753             continue;
1754         LOCK(pnode->cs_filter);
1755         if (pnode->pfilter)
1756         {
1757             if (pnode->pfilter->IsRelevantAndUpdate(tx))
1758                 pnode->PushInventory(inv);
1759         } else
1760             pnode->PushInventory(inv);
1761     }
1762 }
1763 
1764 void CNode::RecordBytesRecv(uint64_t bytes)
1765 {
1766     LOCK(cs_totalBytesRecv);
1767     nTotalBytesRecv += bytes;
1768 }
1769 
1770 void CNode::RecordBytesSent(uint64_t bytes)
1771 {
1772     LOCK(cs_totalBytesSent);
1773     nTotalBytesSent += bytes;
1774 }
1775 
1776 uint64_t CNode::GetTotalBytesRecv()
1777 {
1778     LOCK(cs_totalBytesRecv);
1779     return nTotalBytesRecv;
1780 }
1781 
1782 uint64_t CNode::GetTotalBytesSent()
1783 {
1784     LOCK(cs_totalBytesSent);
1785     return nTotalBytesSent;
1786 }
1787 
1788 void CNode::Fuzz(int nChance)
1789 {
1790     if (!fSuccessfullyConnected) return; // Don't fuzz initial handshake
1791     if (GetRand(nChance) != 0) return; // Fuzz 1 of every nChance messages
1792 
1793     switch (GetRand(3))
1794     {
1795     case 0:
1796         // xor a random byte with a random value:
1797         if (!ssSend.empty()) {
1798             CDataStream::size_type pos = GetRand(ssSend.size());
1799             ssSend[pos] ^= (unsigned char)(GetRand(256));
1800         }
1801         break;
1802     case 1:
1803         // delete a random byte:
1804         if (!ssSend.empty()) {
1805             CDataStream::size_type pos = GetRand(ssSend.size());
1806             ssSend.erase(ssSend.begin()+pos);
1807         }
1808         break;
1809     case 2:
1810         // insert a random byte at a random position
1811         {
1812             CDataStream::size_type pos = GetRand(ssSend.size());
1813             char ch = (char)GetRand(256);
1814             ssSend.insert(ssSend.begin()+pos, ch);
1815         }
1816         break;
1817     }
1818     // Chance of more than one change half the time:
1819     // (more changes exponentially less likely):
1820     Fuzz(2);
1821 }
1822 
1823 //
1824 // CAddrDB
1825 //
1826 
1827 CAddrDB::CAddrDB()
1828 {
1829     pathAddr = GetDataDir() / "peers.dat";
1830 }
1831 
1832 bool CAddrDB::Write(const CAddrMan& addr)
1833 {
1834     // Generate random temporary filename
1835     unsigned short randv = 0;
1836     GetRandBytes((unsigned char*)&randv, sizeof(randv));
1837     std::string tmpfn = strprintf("peers.dat.%04x", randv);
1838 
1839     // serialize addresses, checksum data up to that point, then append csum
1840     CDataStream ssPeers(SER_DISK, CLIENT_VERSION);
1841     ssPeers << FLATDATA(Params().MessageStart());
1842     ssPeers << addr;
1843     uint256 hash = Hash(ssPeers.begin(), ssPeers.end());
1844     ssPeers << hash;
1845 
1846     // open temp output file, and associate with CAutoFile
1847     boost::filesystem::path pathTmp = GetDataDir() / tmpfn;
1848     FILE *file = fopen(pathTmp.string().c_str(), "wb");
1849     CAutoFile fileout(file, SER_DISK, CLIENT_VERSION);
1850     if (fileout.IsNull())
1851         return error("%s : Failed to open file %s", __func__, pathTmp.string());
1852 
1853     // Write and commit header, data
1854     try {
1855         fileout << ssPeers;
1856     }
1857     catch (std::exception &e) {
1858         return error("%s : Serialize or I/O error - %s", __func__, e.what());
1859     }
1860     FileCommit(fileout.Get());
1861     fileout.fclose();
1862 
1863     // replace existing peers.dat, if any, with new peers.dat.XXXX
1864     if (!RenameOver(pathTmp, pathAddr))
1865         return error("%s : Rename-into-place failed", __func__);
1866 
1867     return true;
1868 }
1869 
1870 bool CAddrDB::Read(CAddrMan& addr)
1871 {
1872     // open input file, and associate with CAutoFile
1873     FILE *file = fopen(pathAddr.string().c_str(), "rb");
1874     CAutoFile filein(file, SER_DISK, CLIENT_VERSION);
1875     if (filein.IsNull())
1876         return error("%s : Failed to open file %s", __func__, pathAddr.string());
1877 
1878     // use file size to size memory buffer
1879     int fileSize = boost::filesystem::file_size(pathAddr);
1880     int dataSize = fileSize - sizeof(uint256);
1881     // Don't try to resize to a negative number if file is small
1882     if (dataSize < 0)
1883         dataSize = 0;
1884     vector<unsigned char> vchData;
1885     vchData.resize(dataSize);
1886     uint256 hashIn;
1887 
1888     // read data and checksum from file
1889     try {
1890         filein.read((char *)&vchData[0], dataSize);
1891         filein >> hashIn;
1892     }
1893     catch (std::exception &e) {
1894         return error("%s : Deserialize or I/O error - %s", __func__, e.what());
1895     }
1896     filein.fclose();
1897 
1898     CDataStream ssPeers(vchData, SER_DISK, CLIENT_VERSION);
1899 
1900     // verify stored checksum matches input data
1901     uint256 hashTmp = Hash(ssPeers.begin(), ssPeers.end());
1902     if (hashIn != hashTmp)
1903         return error("%s : Checksum mismatch, data corrupted", __func__);
1904 
1905     unsigned char pchMsgTmp[4];
1906     try {
1907         // de-serialize file header (network specific magic number) and ..
1908         ssPeers >> FLATDATA(pchMsgTmp);
1909 
1910         // ... verify the network matches ours
1911         if (memcmp(pchMsgTmp, Params().MessageStart(), sizeof(pchMsgTmp)))
1912             return error("%s : Invalid network magic number", __func__);
1913 
1914         // de-serialize address data into one CAddrMan object
1915         ssPeers >> addr;
1916     }
1917     catch (std::exception &e) {
1918         return error("%s : Deserialize or I/O error - %s", __func__, e.what());
1919     }
1920 
1921     return true;
1922 }
1923 
1924 unsigned int ReceiveFloodSize() { return 1000*GetArg("-maxreceivebuffer", 5*1000); }
1925 unsigned int SendBufferSize() { return 1000*GetArg("-maxsendbuffer", 1*1000); }
1926 
1927 CNode::CNode(SOCKET hSocketIn, CAddress addrIn, std::string addrNameIn, bool fInboundIn) : ssSend(SER_NETWORK, INIT_PROTO_VERSION), setAddrKnown(5000)
1928 {
1929     nServices = 0;
1930     hSocket = hSocketIn;
1931     nRecvVersion = INIT_PROTO_VERSION;
1932     nLastSend = 0;
1933     nLastRecv = 0;
1934     nSendBytes = 0;
1935     nRecvBytes = 0;
1936     nTimeConnected = GetTime();
1937     addr = addrIn;
1938     addrName = addrNameIn == "" ? addr.ToStringIPPort() : addrNameIn;
1939     nVersion = 0;
1940     strSubVer = "";
1941     fWhitelisted = false;
1942     fOneShot = false;
1943     fClient = false; // set by version message
1944     fInbound = fInboundIn;
1945     fNetworkNode = false;
1946     fSuccessfullyConnected = false;
1947     fDisconnect = false;
1948     nRefCount = 0;
1949     nSendSize = 0;
1950     nSendOffset = 0;
1951     hashContinue = 0;
1952     nStartingHeight = -1;
1953     fGetAddr = false;
1954     fRelayTxes = false;
1955     setInventoryKnown.max_size(SendBufferSize() / 1000);
1956     pfilter = new CBloomFilter();
1957     nPingNonceSent = 0;
1958     nPingUsecStart = 0;
1959     nPingUsecTime = 0;
1960     fPingQueued = false;
1961 
1962     {
1963         LOCK(cs_nLastNodeId);
1964         id = nLastNodeId++;
1965     }
1966 
1967     if (fLogIPs)
1968         LogPrint("net", "Added connection to %s peer=%d\n", addrName, id);
1969     else
1970         LogPrint("net", "Added connection peer=%d\n", id);
1971 
1972     // Be shy and don't send version until we hear
1973     if (hSocket != INVALID_SOCKET && !fInbound)
1974         PushVersion();
1975 
1976     GetNodeSignals().InitializeNode(GetId(), this);
1977 }
1978 
1979 CNode::~CNode()
1980 {
1981     CloseSocket(hSocket);
1982 
1983     if (pfilter)
1984         delete pfilter;
1985 
1986     GetNodeSignals().FinalizeNode(GetId());
1987 }
1988 
1989 void CNode::AskFor(const CInv& inv)
1990 {
1991     if (mapAskFor.size() > MAPASKFOR_MAX_SZ)
1992         return;
1993     // We're using mapAskFor as a priority queue,
1994     // the key is the earliest time the request can be sent
1995     int64_t nRequestTime;
1996     limitedmap<CInv, int64_t>::const_iterator it = mapAlreadyAskedFor.find(inv);
1997     if (it != mapAlreadyAskedFor.end())
1998         nRequestTime = it->second;
1999     else
2000         nRequestTime = 0;
2001     LogPrint("net", "askfor %s  %d (%s) peer=%d\n", inv.ToString(), nRequestTime, DateTimeStrFormat("%H:%M:%S", nRequestTime/1000000), id);
2002 
2003     // Make sure not to reuse time indexes to keep things in the same order
2004     int64_t nNow = GetTimeMicros() - 1000000;
2005     static int64_t nLastTime;
2006     ++nLastTime;
2007     nNow = std::max(nNow, nLastTime);
2008     nLastTime = nNow;
2009 
2010     // Each retry is 2 minutes after the last
2011     nRequestTime = std::max(nRequestTime + 2 * 60 * 1000000, nNow);
2012     if (it != mapAlreadyAskedFor.end())
2013         mapAlreadyAskedFor.update(it, nRequestTime);
2014     else
2015         mapAlreadyAskedFor.insert(std::make_pair(inv, nRequestTime));
2016     mapAskFor.insert(std::make_pair(nRequestTime, inv));
2017 }
2018 
2019 void CNode::BeginMessage(const char* pszCommand) EXCLUSIVE_LOCK_FUNCTION(cs_vSend)
2020 {
2021     ENTER_CRITICAL_SECTION(cs_vSend);
2022     assert(ssSend.size() == 0);
2023     ssSend << CMessageHeader(pszCommand, 0);
2024     LogPrint("net", "sending: %s ", pszCommand);
2025 }
2026 
2027 void CNode::AbortMessage() UNLOCK_FUNCTION(cs_vSend)
2028 {
2029     ssSend.clear();
2030 
2031     LEAVE_CRITICAL_SECTION(cs_vSend);
2032 
2033     LogPrint("net", "(aborted)\n");
2034 }
2035 
2036 void CNode::EndMessage() UNLOCK_FUNCTION(cs_vSend)
2037 {
2038     // The -*messagestest options are intentionally not documented in the help message,
2039     // since they are only used during development to debug the networking code and are
2040     // not intended for end-users.
2041     if (mapArgs.count("-dropmessagestest") && GetRand(GetArg("-dropmessagestest", 2)) == 0)
2042     {
2043         LogPrint("net", "dropmessages DROPPING SEND MESSAGE\n");
2044         AbortMessage();
2045         return;
2046     }
2047     if (mapArgs.count("-fuzzmessagestest"))
2048         Fuzz(GetArg("-fuzzmessagestest", 10));
2049 
2050     if (ssSend.size() == 0)
2051         return;
2052 
2053     // Set the size
2054     unsigned int nSize = ssSend.size() - CMessageHeader::HEADER_SIZE;
2055     memcpy((char*)&ssSend[CMessageHeader::MESSAGE_SIZE_OFFSET], &nSize, sizeof(nSize));
2056 
2057     // Set the checksum
2058     uint256 hash = Hash(ssSend.begin() + CMessageHeader::HEADER_SIZE, ssSend.end());
2059     unsigned int nChecksum = 0;
2060     memcpy(&nChecksum, &hash, sizeof(nChecksum));
2061     assert(ssSend.size () >= CMessageHeader::CHECKSUM_OFFSET + sizeof(nChecksum));
2062     memcpy((char*)&ssSend[CMessageHeader::CHECKSUM_OFFSET], &nChecksum, sizeof(nChecksum));
2063 
2064     LogPrint("net", "(%d bytes) peer=%d\n", nSize, id);
2065 
2066     std::deque<CSerializeData>::iterator it = vSendMsg.insert(vSendMsg.end(), CSerializeData());
2067     ssSend.GetAndClear(*it);
2068     nSendSize += (*it).size();
2069 
2070     // If write queue empty, attempt "optimistic write"
2071     if (it == vSendMsg.begin())
2072         SocketSendData(this);
2073 
2074     LEAVE_CRITICAL_SECTION(cs_vSend);
2075 }