1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 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.
6 #if defined(HAVE_CONFIG_H)
7 #include "config/bitcoin-config.h"
13 #include "chainparams.h"
14 #include "clientversion.h"
15 #include "primitives/transaction.h"
16 #include "ui_interface.h"
17 #include "crypto/common.h"
26 #include <miniupnpc/miniupnpc.h>
27 #include <miniupnpc/miniwget.h>
28 #include <miniupnpc/upnpcommands.h>
29 #include <miniupnpc/upnperrors.h>
32 #include <boost/filesystem.hpp>
33 #include <boost/thread.hpp>
35 // Dump addresses to peers.dat every 15 minutes (900s)
36 #define DUMP_ADDRESSES_INTERVAL 900
38 #if !defined(HAVE_MSG_NOSIGNAL) && !defined(MSG_NOSIGNAL)
39 #define MSG_NOSIGNAL 0
42 // Fix for ancient MinGW versions, that don't have defined these in ws2tcpip.h.
43 // Todo: Can be removed when our pull-tester is upgraded to a modern MinGW version.
45 #ifndef PROTECTION_LEVEL_UNRESTRICTED
46 #define PROTECTION_LEVEL_UNRESTRICTED 10
48 #ifndef IPV6_PROTECTION_LEVEL
49 #define IPV6_PROTECTION_LEVEL 23
56 const int MAX_OUTBOUND_CONNECTIONS = 8;
62 ListenSocket(SOCKET socket, bool whitelisted) : socket(socket), whitelisted(whitelisted) {}
67 // Global state variables
69 bool fDiscover = true;
71 uint64_t nLocalServices = NODE_NETWORK;
72 CCriticalSection cs_mapLocalHost;
73 map<CNetAddr, LocalServiceInfo> mapLocalHost;
74 static bool vfReachable[NET_MAX] = {};
75 static bool vfLimited[NET_MAX] = {};
76 static CNode* pnodeLocalHost = NULL;
77 uint64_t nLocalHostNonce = 0;
78 static std::vector<ListenSocket> vhListenSocket;
80 int nMaxConnections = 125;
81 bool fAddressesInitialized = false;
83 vector<CNode*> vNodes;
84 CCriticalSection cs_vNodes;
85 map<CInv, CDataStream> mapRelay;
86 deque<pair<int64_t, CInv> > vRelayExpiration;
87 CCriticalSection cs_mapRelay;
88 limitedmap<CInv, int64_t> mapAlreadyAskedFor(MAX_INV_SZ);
90 static deque<string> vOneShots;
91 CCriticalSection cs_vOneShots;
93 set<CNetAddr> setservAddNodeAddresses;
94 CCriticalSection cs_setservAddNodeAddresses;
96 vector<std::string> vAddedNodes;
97 CCriticalSection cs_vAddedNodes;
99 NodeId nLastNodeId = 0;
100 CCriticalSection cs_nLastNodeId;
102 static CSemaphore *semOutbound = NULL;
104 // Signals for message handling
105 static CNodeSignals g_signals;
106 CNodeSignals& GetNodeSignals() { return g_signals; }
108 void AddOneShot(string strDest)
111 vOneShots.push_back(strDest);
114 unsigned short GetListenPort()
116 return (unsigned short)(GetArg("-port", Params().GetDefaultPort()));
119 // find 'best' local address for a particular peer
120 bool GetLocal(CService& addr, const CNetAddr *paddrPeer)
126 int nBestReachability = -1;
128 LOCK(cs_mapLocalHost);
129 for (map<CNetAddr, LocalServiceInfo>::iterator it = mapLocalHost.begin(); it != mapLocalHost.end(); it++)
131 int nScore = (*it).second.nScore;
132 int nReachability = (*it).first.GetReachabilityFrom(paddrPeer);
133 if (nReachability > nBestReachability || (nReachability == nBestReachability && nScore > nBestScore))
135 addr = CService((*it).first, (*it).second.nPort);
136 nBestReachability = nReachability;
141 return nBestScore >= 0;
144 // get best local address for a particular peer as a CAddress
145 // Otherwise, return the unroutable 0.0.0.0 but filled in with
146 // the normal parameters, since the IP may be changed to a useful
148 CAddress GetLocalAddress(const CNetAddr *paddrPeer)
150 CAddress ret(CService("0.0.0.0",GetListenPort()),0);
152 if (GetLocal(addr, paddrPeer))
154 ret = CAddress(addr);
156 ret.nServices = nLocalServices;
157 ret.nTime = GetAdjustedTime();
161 int GetnScore(const CService& addr)
163 LOCK(cs_mapLocalHost);
164 if (mapLocalHost.count(addr) == LOCAL_NONE)
166 return mapLocalHost[addr].nScore;
169 // Is our peer's addrLocal potentially useful as an external IP source?
170 bool IsPeerAddrLocalGood(CNode *pnode)
172 return fDiscover && pnode->addr.IsRoutable() && pnode->addrLocal.IsRoutable() &&
173 !IsLimited(pnode->addrLocal.GetNetwork());
176 // pushes our own address to a peer
177 void AdvertizeLocal(CNode *pnode)
179 if (fListen && pnode->fSuccessfullyConnected)
181 CAddress addrLocal = GetLocalAddress(&pnode->addr);
182 // If discovery is enabled, sometimes give our peer the address it
183 // tells us that it sees us as in case it has a better idea of our
184 // address than we do.
185 if (IsPeerAddrLocalGood(pnode) && (!addrLocal.IsRoutable() ||
186 GetRand((GetnScore(addrLocal) > LOCAL_MANUAL) ? 8:2) == 0))
188 addrLocal.SetIP(pnode->addrLocal);
190 if (addrLocal.IsRoutable())
192 pnode->PushAddress(addrLocal);
197 void SetReachable(enum Network net, bool fFlag)
199 LOCK(cs_mapLocalHost);
200 vfReachable[net] = fFlag;
201 if (net == NET_IPV6 && fFlag)
202 vfReachable[NET_IPV4] = true;
205 // learn a new local address
206 bool AddLocal(const CService& addr, int nScore)
208 if (!addr.IsRoutable())
211 if (!fDiscover && nScore < LOCAL_MANUAL)
217 LogPrintf("AddLocal(%s,%i)\n", addr.ToString(), nScore);
220 LOCK(cs_mapLocalHost);
221 bool fAlready = mapLocalHost.count(addr) > 0;
222 LocalServiceInfo &info = mapLocalHost[addr];
223 if (!fAlready || nScore >= info.nScore) {
224 info.nScore = nScore + (fAlready ? 1 : 0);
225 info.nPort = addr.GetPort();
227 SetReachable(addr.GetNetwork());
233 bool AddLocal(const CNetAddr &addr, int nScore)
235 return AddLocal(CService(addr, GetListenPort()), nScore);
238 /** Make a particular network entirely off-limits (no automatic connects to it) */
239 void SetLimited(enum Network net, bool fLimited)
241 if (net == NET_UNROUTABLE)
243 LOCK(cs_mapLocalHost);
244 vfLimited[net] = fLimited;
247 bool IsLimited(enum Network net)
249 LOCK(cs_mapLocalHost);
250 return vfLimited[net];
253 bool IsLimited(const CNetAddr &addr)
255 return IsLimited(addr.GetNetwork());
258 /** vote for a local address */
259 bool SeenLocal(const CService& addr)
262 LOCK(cs_mapLocalHost);
263 if (mapLocalHost.count(addr) == 0)
265 mapLocalHost[addr].nScore++;
271 /** check whether a given address is potentially local */
272 bool IsLocal(const CService& addr)
274 LOCK(cs_mapLocalHost);
275 return mapLocalHost.count(addr) > 0;
278 /** check whether a given network is one we can probably connect to */
279 bool IsReachable(enum Network net)
281 LOCK(cs_mapLocalHost);
282 return vfReachable[net] && !vfLimited[net];
285 /** check whether a given address is in a network we can probably connect to */
286 bool IsReachable(const CNetAddr& addr)
288 enum Network net = addr.GetNetwork();
289 return IsReachable(net);
292 void AddressCurrentlyConnected(const CService& addr)
294 addrman.Connected(addr);
298 uint64_t CNode::nTotalBytesRecv = 0;
299 uint64_t CNode::nTotalBytesSent = 0;
300 CCriticalSection CNode::cs_totalBytesRecv;
301 CCriticalSection CNode::cs_totalBytesSent;
303 CNode* FindNode(const CNetAddr& ip)
306 BOOST_FOREACH(CNode* pnode, vNodes)
307 if ((CNetAddr)pnode->addr == ip)
312 CNode* FindNode(const std::string& addrName)
315 BOOST_FOREACH(CNode* pnode, vNodes)
316 if (pnode->addrName == addrName)
321 CNode* FindNode(const CService& addr)
324 BOOST_FOREACH(CNode* pnode, vNodes)
325 if ((CService)pnode->addr == addr)
330 CNode* ConnectNode(CAddress addrConnect, const char *pszDest)
332 if (pszDest == NULL) {
333 if (IsLocal(addrConnect))
336 // Look for an existing connection
337 CNode* pnode = FindNode((CService)addrConnect);
346 LogPrint("net", "trying connection %s lastseen=%.1fhrs\n",
347 pszDest ? pszDest : addrConnect.ToString(),
348 pszDest ? 0.0 : (double)(GetAdjustedTime() - addrConnect.nTime)/3600.0);
352 bool proxyConnectionFailed = false;
353 if (pszDest ? ConnectSocketByName(addrConnect, hSocket, pszDest, Params().GetDefaultPort(), nConnectTimeout, &proxyConnectionFailed) :
354 ConnectSocket(addrConnect, hSocket, nConnectTimeout, &proxyConnectionFailed))
356 addrman.Attempt(addrConnect);
359 CNode* pnode = new CNode(hSocket, addrConnect, pszDest ? pszDest : "", false);
364 vNodes.push_back(pnode);
367 pnode->nTimeConnected = GetTime();
370 } else if (!proxyConnectionFailed) {
371 // If connecting to the node failed, and failure is not caused by a problem connecting to
372 // the proxy, mark this as an attempt.
373 addrman.Attempt(addrConnect);
379 void CNode::CloseSocketDisconnect()
382 if (hSocket != INVALID_SOCKET)
384 LogPrint("net", "disconnecting peer=%d\n", id);
385 CloseSocket(hSocket);
388 // in case this fails, we'll empty the recv buffer when the CNode is deleted
389 TRY_LOCK(cs_vRecvMsg, lockRecv);
394 void CNode::PushVersion()
396 int nBestHeight = g_signals.GetHeight().get_value_or(0);
398 int64_t nTime = (fInbound ? GetAdjustedTime() : GetTime());
399 CAddress addrYou = (addr.IsRoutable() && !IsProxy(addr) ? addr : CAddress(CService("0.0.0.0",0)));
400 CAddress addrMe = GetLocalAddress(&addr);
401 GetRandBytes((unsigned char*)&nLocalHostNonce, sizeof(nLocalHostNonce));
403 LogPrint("net", "send version message: version %d, blocks=%d, us=%s, them=%s, peer=%d\n", PROTOCOL_VERSION, nBestHeight, addrMe.ToString(), addrYou.ToString(), id);
405 LogPrint("net", "send version message: version %d, blocks=%d, us=%s, peer=%d\n", PROTOCOL_VERSION, nBestHeight, addrMe.ToString(), id);
406 PushMessage("version", PROTOCOL_VERSION, nLocalServices, nTime, addrYou, addrMe,
407 nLocalHostNonce, FormatSubVersion(CLIENT_NAME, CLIENT_VERSION, std::vector<string>()), nBestHeight, true);
414 std::map<CNetAddr, int64_t> CNode::setBanned;
415 CCriticalSection CNode::cs_setBanned;
417 void CNode::ClearBanned()
422 bool CNode::IsBanned(CNetAddr ip)
424 bool fResult = false;
427 std::map<CNetAddr, int64_t>::iterator i = setBanned.find(ip);
428 if (i != setBanned.end())
430 int64_t t = (*i).second;
438 bool CNode::Ban(const CNetAddr &addr) {
439 int64_t banTime = GetTime()+GetArg("-bantime", 60*60*24); // Default 24-hour ban
442 if (setBanned[addr] < banTime)
443 setBanned[addr] = banTime;
449 std::vector<CSubNet> CNode::vWhitelistedRange;
450 CCriticalSection CNode::cs_vWhitelistedRange;
452 bool CNode::IsWhitelistedRange(const CNetAddr &addr) {
453 LOCK(cs_vWhitelistedRange);
454 BOOST_FOREACH(const CSubNet& subnet, vWhitelistedRange) {
455 if (subnet.Match(addr))
461 void CNode::AddWhitelistedRange(const CSubNet &subnet) {
462 LOCK(cs_vWhitelistedRange);
463 vWhitelistedRange.push_back(subnet);
467 #define X(name) stats.name = name
468 void CNode::copyStats(CNodeStats &stats)
470 stats.nodeid = this->GetId();
485 // It is common for nodes with good ping times to suddenly become lagged,
486 // due to a new block arriving or other large transfer.
487 // Merely reporting pingtime might fool the caller into thinking the node was still responsive,
488 // since pingtime does not update until the ping is complete, which might take a while.
489 // So, if a ping is taking an unusually long time in flight,
490 // the caller can immediately detect that this is happening.
491 int64_t nPingUsecWait = 0;
492 if ((0 != nPingNonceSent) && (0 != nPingUsecStart)) {
493 nPingUsecWait = GetTimeMicros() - nPingUsecStart;
496 // 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 :)
497 stats.dPingTime = (((double)nPingUsecTime) / 1e6);
498 stats.dPingWait = (((double)nPingUsecWait) / 1e6);
500 // Leave string empty if addrLocal invalid (not filled in yet)
501 stats.addrLocal = addrLocal.IsValid() ? addrLocal.ToString() : "";
505 // requires LOCK(cs_vRecvMsg)
506 bool CNode::ReceiveMsgBytes(const char *pch, unsigned int nBytes)
510 // get current incomplete message, or create a new one
511 if (vRecvMsg.empty() ||
512 vRecvMsg.back().complete())
513 vRecvMsg.push_back(CNetMessage(Params().MessageStart(), SER_NETWORK, nRecvVersion));
515 CNetMessage& msg = vRecvMsg.back();
517 // absorb network data
520 handled = msg.readHeader(pch, nBytes);
522 handled = msg.readData(pch, nBytes);
527 if (msg.in_data && msg.hdr.nMessageSize > MAX_PROTOCOL_MESSAGE_LENGTH) {
528 LogPrint("net", "Oversized message from peer=%i, disconnecting", GetId());
536 msg.nTime = GetTimeMicros();
542 int CNetMessage::readHeader(const char *pch, unsigned int nBytes)
544 // copy data to temporary parsing buffer
545 unsigned int nRemaining = 24 - nHdrPos;
546 unsigned int nCopy = std::min(nRemaining, nBytes);
548 memcpy(&hdrbuf[nHdrPos], pch, nCopy);
551 // if header incomplete, exit
555 // deserialize to CMessageHeader
559 catch (const std::exception&) {
563 // reject messages larger than MAX_SIZE
564 if (hdr.nMessageSize > MAX_SIZE)
567 // switch state to reading message data
573 int CNetMessage::readData(const char *pch, unsigned int nBytes)
575 unsigned int nRemaining = hdr.nMessageSize - nDataPos;
576 unsigned int nCopy = std::min(nRemaining, nBytes);
578 if (vRecv.size() < nDataPos + nCopy) {
579 // Allocate up to 256 KiB ahead, but never more than the total message size.
580 vRecv.resize(std::min(hdr.nMessageSize, nDataPos + nCopy + 256 * 1024));
583 memcpy(&vRecv[nDataPos], pch, nCopy);
597 // requires LOCK(cs_vSend)
598 void SocketSendData(CNode *pnode)
600 std::deque<CSerializeData>::iterator it = pnode->vSendMsg.begin();
602 while (it != pnode->vSendMsg.end()) {
603 const CSerializeData &data = *it;
604 assert(data.size() > pnode->nSendOffset);
605 int nBytes = send(pnode->hSocket, &data[pnode->nSendOffset], data.size() - pnode->nSendOffset, MSG_NOSIGNAL | MSG_DONTWAIT);
607 pnode->nLastSend = GetTime();
608 pnode->nSendBytes += nBytes;
609 pnode->nSendOffset += nBytes;
610 pnode->RecordBytesSent(nBytes);
611 if (pnode->nSendOffset == data.size()) {
612 pnode->nSendOffset = 0;
613 pnode->nSendSize -= data.size();
616 // could not send full message; stop sending more
622 int nErr = WSAGetLastError();
623 if (nErr != WSAEWOULDBLOCK && nErr != WSAEMSGSIZE && nErr != WSAEINTR && nErr != WSAEINPROGRESS)
625 LogPrintf("socket send error %s\n", NetworkErrorString(nErr));
626 pnode->CloseSocketDisconnect();
629 // couldn't send anything at all
634 if (it == pnode->vSendMsg.end()) {
635 assert(pnode->nSendOffset == 0);
636 assert(pnode->nSendSize == 0);
638 pnode->vSendMsg.erase(pnode->vSendMsg.begin(), it);
641 static list<CNode*> vNodesDisconnected;
643 void ThreadSocketHandler()
645 unsigned int nPrevNodeCount = 0;
653 // Disconnect unused nodes
654 vector<CNode*> vNodesCopy = vNodes;
655 BOOST_FOREACH(CNode* pnode, vNodesCopy)
657 if (pnode->fDisconnect ||
658 (pnode->GetRefCount() <= 0 && pnode->vRecvMsg.empty() && pnode->nSendSize == 0 && pnode->ssSend.empty()))
660 // remove from vNodes
661 vNodes.erase(remove(vNodes.begin(), vNodes.end(), pnode), vNodes.end());
663 // release outbound grant (if any)
664 pnode->grantOutbound.Release();
666 // close socket and cleanup
667 pnode->CloseSocketDisconnect();
669 // hold in disconnected pool until all refs are released
670 if (pnode->fNetworkNode || pnode->fInbound)
672 vNodesDisconnected.push_back(pnode);
677 // Delete disconnected nodes
678 list<CNode*> vNodesDisconnectedCopy = vNodesDisconnected;
679 BOOST_FOREACH(CNode* pnode, vNodesDisconnectedCopy)
681 // wait until threads are done using it
682 if (pnode->GetRefCount() <= 0)
684 bool fDelete = false;
686 TRY_LOCK(pnode->cs_vSend, lockSend);
689 TRY_LOCK(pnode->cs_vRecvMsg, lockRecv);
692 TRY_LOCK(pnode->cs_inventory, lockInv);
700 vNodesDisconnected.remove(pnode);
706 if(vNodes.size() != nPrevNodeCount) {
707 nPrevNodeCount = vNodes.size();
708 uiInterface.NotifyNumConnectionsChanged(nPrevNodeCount);
712 // Find which sockets have data to receive
714 struct timeval timeout;
716 timeout.tv_usec = 50000; // frequency to poll pnode->vSend
723 FD_ZERO(&fdsetError);
724 SOCKET hSocketMax = 0;
725 bool have_fds = false;
727 BOOST_FOREACH(const ListenSocket& hListenSocket, vhListenSocket) {
728 FD_SET(hListenSocket.socket, &fdsetRecv);
729 hSocketMax = max(hSocketMax, hListenSocket.socket);
735 BOOST_FOREACH(CNode* pnode, vNodes)
737 if (pnode->hSocket == INVALID_SOCKET)
739 FD_SET(pnode->hSocket, &fdsetError);
740 hSocketMax = max(hSocketMax, pnode->hSocket);
743 // Implement the following logic:
744 // * If there is data to send, select() for sending data. As this only
745 // happens when optimistic write failed, we choose to first drain the
746 // write buffer in this case before receiving more. This avoids
747 // needlessly queueing received data, if the remote peer is not themselves
748 // receiving data. This means properly utilizing TCP flow control signalling.
749 // * Otherwise, if there is no (complete) message in the receive buffer,
750 // or there is space left in the buffer, select() for receiving data.
751 // * (if neither of the above applies, there is certainly one message
752 // in the receiver buffer ready to be processed).
753 // Together, that means that at least one of the following is always possible,
754 // so we don't deadlock:
755 // * We send some data.
756 // * We wait for data to be received (and disconnect after timeout).
757 // * We process a message in the buffer (message handler thread).
759 TRY_LOCK(pnode->cs_vSend, lockSend);
760 if (lockSend && !pnode->vSendMsg.empty()) {
761 FD_SET(pnode->hSocket, &fdsetSend);
766 TRY_LOCK(pnode->cs_vRecvMsg, lockRecv);
768 pnode->vRecvMsg.empty() || !pnode->vRecvMsg.front().complete() ||
769 pnode->GetTotalRecvSize() <= ReceiveFloodSize()))
770 FD_SET(pnode->hSocket, &fdsetRecv);
775 int nSelect = select(have_fds ? hSocketMax + 1 : 0,
776 &fdsetRecv, &fdsetSend, &fdsetError, &timeout);
777 boost::this_thread::interruption_point();
779 if (nSelect == SOCKET_ERROR)
783 int nErr = WSAGetLastError();
784 LogPrintf("socket select error %s\n", NetworkErrorString(nErr));
785 for (unsigned int i = 0; i <= hSocketMax; i++)
786 FD_SET(i, &fdsetRecv);
789 FD_ZERO(&fdsetError);
790 MilliSleep(timeout.tv_usec/1000);
794 // Accept new connections
796 BOOST_FOREACH(const ListenSocket& hListenSocket, vhListenSocket)
798 if (hListenSocket.socket != INVALID_SOCKET && FD_ISSET(hListenSocket.socket, &fdsetRecv))
800 struct sockaddr_storage sockaddr;
801 socklen_t len = sizeof(sockaddr);
802 SOCKET hSocket = accept(hListenSocket.socket, (struct sockaddr*)&sockaddr, &len);
806 if (hSocket != INVALID_SOCKET)
807 if (!addr.SetSockAddr((const struct sockaddr*)&sockaddr))
808 LogPrintf("Warning: Unknown socket family\n");
810 bool whitelisted = hListenSocket.whitelisted || CNode::IsWhitelistedRange(addr);
813 BOOST_FOREACH(CNode* pnode, vNodes)
818 if (hSocket == INVALID_SOCKET)
820 int nErr = WSAGetLastError();
821 if (nErr != WSAEWOULDBLOCK)
822 LogPrintf("socket error accept failed: %s\n", NetworkErrorString(nErr));
824 else if (nInbound >= nMaxConnections - MAX_OUTBOUND_CONNECTIONS)
826 CloseSocket(hSocket);
828 else if (CNode::IsBanned(addr) && !whitelisted)
830 LogPrintf("connection from %s dropped (banned)\n", addr.ToString());
831 CloseSocket(hSocket);
835 CNode* pnode = new CNode(hSocket, addr, "", true);
837 pnode->fWhitelisted = whitelisted;
841 vNodes.push_back(pnode);
848 // Service each socket
850 vector<CNode*> vNodesCopy;
854 BOOST_FOREACH(CNode* pnode, vNodesCopy)
857 BOOST_FOREACH(CNode* pnode, vNodesCopy)
859 boost::this_thread::interruption_point();
864 if (pnode->hSocket == INVALID_SOCKET)
866 if (FD_ISSET(pnode->hSocket, &fdsetRecv) || FD_ISSET(pnode->hSocket, &fdsetError))
868 TRY_LOCK(pnode->cs_vRecvMsg, lockRecv);
872 // typical socket buffer is 8K-64K
873 char pchBuf[0x10000];
874 int nBytes = recv(pnode->hSocket, pchBuf, sizeof(pchBuf), MSG_DONTWAIT);
877 if (!pnode->ReceiveMsgBytes(pchBuf, nBytes))
878 pnode->CloseSocketDisconnect();
879 pnode->nLastRecv = GetTime();
880 pnode->nRecvBytes += nBytes;
881 pnode->RecordBytesRecv(nBytes);
883 else if (nBytes == 0)
885 // socket closed gracefully
886 if (!pnode->fDisconnect)
887 LogPrint("net", "socket closed\n");
888 pnode->CloseSocketDisconnect();
893 int nErr = WSAGetLastError();
894 if (nErr != WSAEWOULDBLOCK && nErr != WSAEMSGSIZE && nErr != WSAEINTR && nErr != WSAEINPROGRESS)
896 if (!pnode->fDisconnect)
897 LogPrintf("socket recv error %s\n", NetworkErrorString(nErr));
898 pnode->CloseSocketDisconnect();
908 if (pnode->hSocket == INVALID_SOCKET)
910 if (FD_ISSET(pnode->hSocket, &fdsetSend))
912 TRY_LOCK(pnode->cs_vSend, lockSend);
914 SocketSendData(pnode);
918 // Inactivity checking
920 int64_t nTime = GetTime();
921 if (nTime - pnode->nTimeConnected > 60)
923 if (pnode->nLastRecv == 0 || pnode->nLastSend == 0)
925 LogPrint("net", "socket no message in first 60 seconds, %d %d from %d\n", pnode->nLastRecv != 0, pnode->nLastSend != 0, pnode->id);
926 pnode->fDisconnect = true;
928 else if (nTime - pnode->nLastSend > TIMEOUT_INTERVAL)
930 LogPrintf("socket sending timeout: %is\n", nTime - pnode->nLastSend);
931 pnode->fDisconnect = true;
933 else if (nTime - pnode->nLastRecv > (pnode->nVersion > BIP0031_VERSION ? TIMEOUT_INTERVAL : 90*60))
935 LogPrintf("socket receive timeout: %is\n", nTime - pnode->nLastRecv);
936 pnode->fDisconnect = true;
938 else if (pnode->nPingNonceSent && pnode->nPingUsecStart + TIMEOUT_INTERVAL * 1000000 < GetTimeMicros())
940 LogPrintf("ping timeout: %fs\n", 0.000001 * (GetTimeMicros() - pnode->nPingUsecStart));
941 pnode->fDisconnect = true;
947 BOOST_FOREACH(CNode* pnode, vNodesCopy)
964 std::string port = strprintf("%u", GetListenPort());
965 const char * multicastif = 0;
966 const char * minissdpdpath = 0;
967 struct UPNPDev * devlist = 0;
970 #ifndef UPNPDISCOVER_SUCCESS
972 devlist = upnpDiscover(2000, multicastif, minissdpdpath, 0);
976 devlist = upnpDiscover(2000, multicastif, minissdpdpath, 0, 0, &error);
979 struct UPNPUrls urls;
980 struct IGDdatas data;
983 r = UPNP_GetValidIGD(devlist, &urls, &data, lanaddr, sizeof(lanaddr));
987 char externalIPAddress[40];
988 r = UPNP_GetExternalIPAddress(urls.controlURL, data.first.servicetype, externalIPAddress);
989 if(r != UPNPCOMMAND_SUCCESS)
990 LogPrintf("UPnP: GetExternalIPAddress() returned %d\n", r);
993 if(externalIPAddress[0])
995 LogPrintf("UPnP: ExternalIPAddress = %s\n", externalIPAddress);
996 AddLocal(CNetAddr(externalIPAddress), LOCAL_UPNP);
999 LogPrintf("UPnP: GetExternalIPAddress failed.\n");
1003 string strDesc = "Bitcoin " + FormatFullVersion();
1007 #ifndef UPNPDISCOVER_SUCCESS
1009 r = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype,
1010 port.c_str(), port.c_str(), lanaddr, strDesc.c_str(), "TCP", 0);
1013 r = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype,
1014 port.c_str(), port.c_str(), lanaddr, strDesc.c_str(), "TCP", 0, "0");
1017 if(r!=UPNPCOMMAND_SUCCESS)
1018 LogPrintf("AddPortMapping(%s, %s, %s) failed with code %d (%s)\n",
1019 port, port, lanaddr, r, strupnperror(r));
1021 LogPrintf("UPnP Port Mapping successful.\n");;
1023 MilliSleep(20*60*1000); // Refresh every 20 minutes
1026 catch (const boost::thread_interrupted&)
1028 r = UPNP_DeletePortMapping(urls.controlURL, data.first.servicetype, port.c_str(), "TCP", 0);
1029 LogPrintf("UPNP_DeletePortMapping() returned: %d\n", r);
1030 freeUPNPDevlist(devlist); devlist = 0;
1031 FreeUPNPUrls(&urls);
1035 LogPrintf("No valid UPnP IGDs found\n");
1036 freeUPNPDevlist(devlist); devlist = 0;
1038 FreeUPNPUrls(&urls);
1042 void MapPort(bool fUseUPnP)
1044 static boost::thread* upnp_thread = NULL;
1049 upnp_thread->interrupt();
1050 upnp_thread->join();
1053 upnp_thread = new boost::thread(boost::bind(&TraceThread<void (*)()>, "upnp", &ThreadMapPort));
1055 else if (upnp_thread) {
1056 upnp_thread->interrupt();
1057 upnp_thread->join();
1066 // Intentionally left blank.
1075 void ThreadDNSAddressSeed()
1077 // goal: only query DNS seeds if address need is acute
1078 if ((addrman.size() > 0) &&
1079 (!GetBoolArg("-forcednsseed", false))) {
1080 MilliSleep(11 * 1000);
1083 if (vNodes.size() >= 2) {
1084 LogPrintf("P2P peers available. Skipped DNS seeding.\n");
1089 const vector<CDNSSeedData> &vSeeds = Params().DNSSeeds();
1092 LogPrintf("Loading addresses from DNS seeds (could take a while)\n");
1094 BOOST_FOREACH(const CDNSSeedData &seed, vSeeds) {
1095 if (HaveNameProxy()) {
1096 AddOneShot(seed.host);
1098 vector<CNetAddr> vIPs;
1099 vector<CAddress> vAdd;
1100 if (LookupHost(seed.host.c_str(), vIPs))
1102 BOOST_FOREACH(CNetAddr& ip, vIPs)
1104 int nOneDay = 24*3600;
1105 CAddress addr = CAddress(CService(ip, Params().GetDefaultPort()));
1106 addr.nTime = GetTime() - 3*nOneDay - GetRand(4*nOneDay); // use a random age between 3 and 7 days old
1107 vAdd.push_back(addr);
1111 addrman.Add(vAdd, CNetAddr(seed.name, true));
1115 LogPrintf("%d addresses found from DNS seeds\n", found);
1129 void DumpAddresses()
1131 int64_t nStart = GetTimeMillis();
1136 LogPrint("net", "Flushed %d addresses to peers.dat %dms\n",
1137 addrman.size(), GetTimeMillis() - nStart);
1140 void static ProcessOneShot()
1145 if (vOneShots.empty())
1147 strDest = vOneShots.front();
1148 vOneShots.pop_front();
1151 CSemaphoreGrant grant(*semOutbound, true);
1153 if (!OpenNetworkConnection(addr, &grant, strDest.c_str(), true))
1154 AddOneShot(strDest);
1158 void ThreadOpenConnections()
1160 // Connect to specific addresses
1161 if (mapArgs.count("-connect") && mapMultiArgs["-connect"].size() > 0)
1163 for (int64_t nLoop = 0;; nLoop++)
1166 BOOST_FOREACH(string strAddr, mapMultiArgs["-connect"])
1169 OpenNetworkConnection(addr, NULL, strAddr.c_str());
1170 for (int i = 0; i < 10 && i < nLoop; i++)
1179 // Initiate network connections
1180 int64_t nStart = GetTime();
1187 CSemaphoreGrant grant(*semOutbound);
1188 boost::this_thread::interruption_point();
1190 // Add seed nodes if DNS seeds are all down (an infrastructure attack?).
1191 if (addrman.size() == 0 && (GetTime() - nStart > 60)) {
1192 static bool done = false;
1194 LogPrintf("Adding fixed seed nodes as DNS doesn't seem to be available.\n");
1195 addrman.Add(Params().FixedSeeds(), CNetAddr("127.0.0.1"));
1201 // Choose an address to connect to based on most recently seen
1203 CAddress addrConnect;
1205 // Only connect out to one peer per network group (/16 for IPv4).
1206 // Do this here so we don't have to critsect vNodes inside mapAddresses critsect.
1208 set<vector<unsigned char> > setConnected;
1211 BOOST_FOREACH(CNode* pnode, vNodes) {
1212 if (!pnode->fInbound) {
1213 setConnected.insert(pnode->addr.GetGroup());
1219 int64_t nANow = GetAdjustedTime();
1224 CAddrInfo addr = addrman.Select();
1226 // if we selected an invalid address, restart
1227 if (!addr.IsValid() || setConnected.count(addr.GetGroup()) || IsLocal(addr))
1230 // If we didn't find an appropriate destination after trying 100 addresses fetched from addrman,
1231 // stop this loop, and let the outer loop run again (which sleeps, adds seed nodes, recalculates
1232 // already-connected network ranges, ...) before trying new addrman addresses.
1237 if (IsLimited(addr))
1240 // only consider very recently tried nodes after 30 failed attempts
1241 if (nANow - addr.nLastTry < 600 && nTries < 30)
1244 // do not allow non-default ports, unless after 50 invalid addresses selected already
1245 if (addr.GetPort() != Params().GetDefaultPort() && nTries < 50)
1252 if (addrConnect.IsValid())
1253 OpenNetworkConnection(addrConnect, &grant);
1257 void ThreadOpenAddedConnections()
1260 LOCK(cs_vAddedNodes);
1261 vAddedNodes = mapMultiArgs["-addnode"];
1264 if (HaveNameProxy()) {
1266 list<string> lAddresses(0);
1268 LOCK(cs_vAddedNodes);
1269 BOOST_FOREACH(string& strAddNode, vAddedNodes)
1270 lAddresses.push_back(strAddNode);
1272 BOOST_FOREACH(string& strAddNode, lAddresses) {
1274 CSemaphoreGrant grant(*semOutbound);
1275 OpenNetworkConnection(addr, &grant, strAddNode.c_str());
1278 MilliSleep(120000); // Retry every 2 minutes
1282 for (unsigned int i = 0; true; i++)
1284 list<string> lAddresses(0);
1286 LOCK(cs_vAddedNodes);
1287 BOOST_FOREACH(string& strAddNode, vAddedNodes)
1288 lAddresses.push_back(strAddNode);
1291 list<vector<CService> > lservAddressesToAdd(0);
1292 BOOST_FOREACH(string& strAddNode, lAddresses)
1294 vector<CService> vservNode(0);
1295 if(Lookup(strAddNode.c_str(), vservNode, Params().GetDefaultPort(), fNameLookup, 0))
1297 lservAddressesToAdd.push_back(vservNode);
1299 LOCK(cs_setservAddNodeAddresses);
1300 BOOST_FOREACH(CService& serv, vservNode)
1301 setservAddNodeAddresses.insert(serv);
1305 // Attempt to connect to each IP for each addnode entry until at least one is successful per addnode entry
1306 // (keeping in mind that addnode entries can have many IPs if fNameLookup)
1309 BOOST_FOREACH(CNode* pnode, vNodes)
1310 for (list<vector<CService> >::iterator it = lservAddressesToAdd.begin(); it != lservAddressesToAdd.end(); it++)
1311 BOOST_FOREACH(CService& addrNode, *(it))
1312 if (pnode->addr == addrNode)
1314 it = lservAddressesToAdd.erase(it);
1319 BOOST_FOREACH(vector<CService>& vserv, lservAddressesToAdd)
1321 CSemaphoreGrant grant(*semOutbound);
1322 OpenNetworkConnection(CAddress(vserv[i % vserv.size()]), &grant);
1325 MilliSleep(120000); // Retry every 2 minutes
1329 // if successful, this moves the passed grant to the constructed node
1330 bool OpenNetworkConnection(const CAddress& addrConnect, CSemaphoreGrant *grantOutbound, const char *pszDest, bool fOneShot)
1333 // Initiate outbound network connection
1335 boost::this_thread::interruption_point();
1337 if (IsLocal(addrConnect) ||
1338 FindNode((CNetAddr)addrConnect) || CNode::IsBanned(addrConnect) ||
1339 FindNode(addrConnect.ToStringIPPort()))
1341 } else if (FindNode(pszDest))
1344 CNode* pnode = ConnectNode(addrConnect, pszDest);
1345 boost::this_thread::interruption_point();
1350 grantOutbound->MoveTo(pnode->grantOutbound);
1351 pnode->fNetworkNode = true;
1353 pnode->fOneShot = true;
1359 void ThreadMessageHandler()
1361 SetThreadPriority(THREAD_PRIORITY_BELOW_NORMAL);
1364 vector<CNode*> vNodesCopy;
1367 vNodesCopy = vNodes;
1368 BOOST_FOREACH(CNode* pnode, vNodesCopy) {
1373 // Poll the connected nodes for messages
1374 CNode* pnodeTrickle = NULL;
1375 if (!vNodesCopy.empty())
1376 pnodeTrickle = vNodesCopy[GetRand(vNodesCopy.size())];
1380 BOOST_FOREACH(CNode* pnode, vNodesCopy)
1382 if (pnode->fDisconnect)
1387 TRY_LOCK(pnode->cs_vRecvMsg, lockRecv);
1390 if (!g_signals.ProcessMessages(pnode))
1391 pnode->CloseSocketDisconnect();
1393 if (pnode->nSendSize < SendBufferSize())
1395 if (!pnode->vRecvGetData.empty() || (!pnode->vRecvMsg.empty() && pnode->vRecvMsg[0].complete()))
1402 boost::this_thread::interruption_point();
1406 TRY_LOCK(pnode->cs_vSend, lockSend);
1408 g_signals.SendMessages(pnode, pnode == pnodeTrickle || pnode->fWhitelisted);
1410 boost::this_thread::interruption_point();
1415 BOOST_FOREACH(CNode* pnode, vNodesCopy)
1429 bool BindListenPort(const CService &addrBind, string& strError, bool fWhitelisted)
1434 // Create socket for listening for incoming connections
1435 struct sockaddr_storage sockaddr;
1436 socklen_t len = sizeof(sockaddr);
1437 if (!addrBind.GetSockAddr((struct sockaddr*)&sockaddr, &len))
1439 strError = strprintf("Error: Bind address family for %s not supported", addrBind.ToString());
1440 LogPrintf("%s\n", strError);
1444 SOCKET hListenSocket = socket(((struct sockaddr*)&sockaddr)->sa_family, SOCK_STREAM, IPPROTO_TCP);
1445 if (hListenSocket == INVALID_SOCKET)
1447 strError = strprintf("Error: Couldn't open socket for incoming connections (socket returned error %s)", NetworkErrorString(WSAGetLastError()));
1448 LogPrintf("%s\n", strError);
1454 // Different way of disabling SIGPIPE on BSD
1455 setsockopt(hListenSocket, SOL_SOCKET, SO_NOSIGPIPE, (void*)&nOne, sizeof(int));
1457 // Allow binding if the port is still in TIME_WAIT state after
1458 // the program was closed and restarted. Not an issue on windows!
1459 setsockopt(hListenSocket, SOL_SOCKET, SO_REUSEADDR, (void*)&nOne, sizeof(int));
1462 // Set to non-blocking, incoming connections will also inherit this
1463 if (!SetSocketNonBlocking(hListenSocket, true)) {
1464 strError = strprintf("BindListenPort: Setting listening socket to non-blocking failed, error %s\n", NetworkErrorString(WSAGetLastError()));
1465 LogPrintf("%s\n", strError);
1469 // some systems don't have IPV6_V6ONLY but are always v6only; others do have the option
1470 // and enable it by default or not. Try to enable it, if possible.
1471 if (addrBind.IsIPv6()) {
1474 setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_V6ONLY, (const char*)&nOne, sizeof(int));
1476 setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_V6ONLY, (void*)&nOne, sizeof(int));
1480 int nProtLevel = PROTECTION_LEVEL_UNRESTRICTED;
1481 setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_PROTECTION_LEVEL, (const char*)&nProtLevel, sizeof(int));
1485 if (::bind(hListenSocket, (struct sockaddr*)&sockaddr, len) == SOCKET_ERROR)
1487 int nErr = WSAGetLastError();
1488 if (nErr == WSAEADDRINUSE)
1489 strError = strprintf(_("Unable to bind to %s on this computer. Bitcoin Core is probably already running."), addrBind.ToString());
1491 strError = strprintf(_("Unable to bind to %s on this computer (bind returned error %s)"), addrBind.ToString(), NetworkErrorString(nErr));
1492 LogPrintf("%s\n", strError);
1493 CloseSocket(hListenSocket);
1496 LogPrintf("Bound to %s\n", addrBind.ToString());
1498 // Listen for incoming connections
1499 if (listen(hListenSocket, SOMAXCONN) == SOCKET_ERROR)
1501 strError = strprintf(_("Error: Listening for incoming connections failed (listen returned error %s)"), NetworkErrorString(WSAGetLastError()));
1502 LogPrintf("%s\n", strError);
1503 CloseSocket(hListenSocket);
1507 vhListenSocket.push_back(ListenSocket(hListenSocket, fWhitelisted));
1509 if (addrBind.IsRoutable() && fDiscover && !fWhitelisted)
1510 AddLocal(addrBind, LOCAL_BIND);
1515 void static Discover(boost::thread_group& threadGroup)
1521 // Get local host IP
1522 char pszHostName[256] = "";
1523 if (gethostname(pszHostName, sizeof(pszHostName)) != SOCKET_ERROR)
1525 vector<CNetAddr> vaddr;
1526 if (LookupHost(pszHostName, vaddr))
1528 BOOST_FOREACH (const CNetAddr &addr, vaddr)
1530 if (AddLocal(addr, LOCAL_IF))
1531 LogPrintf("%s: %s - %s\n", __func__, pszHostName, addr.ToString());
1536 // Get local host ip
1537 struct ifaddrs* myaddrs;
1538 if (getifaddrs(&myaddrs) == 0)
1540 for (struct ifaddrs* ifa = myaddrs; ifa != NULL; ifa = ifa->ifa_next)
1542 if (ifa->ifa_addr == NULL) continue;
1543 if ((ifa->ifa_flags & IFF_UP) == 0) continue;
1544 if (strcmp(ifa->ifa_name, "lo") == 0) continue;
1545 if (strcmp(ifa->ifa_name, "lo0") == 0) continue;
1546 if (ifa->ifa_addr->sa_family == AF_INET)
1548 struct sockaddr_in* s4 = (struct sockaddr_in*)(ifa->ifa_addr);
1549 CNetAddr addr(s4->sin_addr);
1550 if (AddLocal(addr, LOCAL_IF))
1551 LogPrintf("%s: IPv4 %s: %s\n", __func__, ifa->ifa_name, addr.ToString());
1553 else if (ifa->ifa_addr->sa_family == AF_INET6)
1555 struct sockaddr_in6* s6 = (struct sockaddr_in6*)(ifa->ifa_addr);
1556 CNetAddr addr(s6->sin6_addr);
1557 if (AddLocal(addr, LOCAL_IF))
1558 LogPrintf("%s: IPv6 %s: %s\n", __func__, ifa->ifa_name, addr.ToString());
1561 freeifaddrs(myaddrs);
1566 void StartNode(boost::thread_group& threadGroup)
1568 uiInterface.InitMessage(_("Loading addresses..."));
1569 // Load addresses for peers.dat
1570 int64_t nStart = GetTimeMillis();
1573 if (!adb.Read(addrman))
1574 LogPrintf("Invalid or missing peers.dat; recreating\n");
1576 LogPrintf("Loaded %i addresses from peers.dat %dms\n",
1577 addrman.size(), GetTimeMillis() - nStart);
1578 fAddressesInitialized = true;
1580 if (semOutbound == NULL) {
1581 // initialize semaphore
1582 int nMaxOutbound = min(MAX_OUTBOUND_CONNECTIONS, nMaxConnections);
1583 semOutbound = new CSemaphore(nMaxOutbound);
1586 if (pnodeLocalHost == NULL)
1587 pnodeLocalHost = new CNode(INVALID_SOCKET, CAddress(CService("127.0.0.1", 0), nLocalServices));
1589 Discover(threadGroup);
1595 if (!GetBoolArg("-dnsseed", true))
1596 LogPrintf("DNS seeding disabled\n");
1598 threadGroup.create_thread(boost::bind(&TraceThread<void (*)()>, "dnsseed", &ThreadDNSAddressSeed));
1600 // Map ports with UPnP
1601 MapPort(GetBoolArg("-upnp", DEFAULT_UPNP));
1603 // Send and receive from sockets, accept connections
1604 threadGroup.create_thread(boost::bind(&TraceThread<void (*)()>, "net", &ThreadSocketHandler));
1606 // Initiate outbound connections from -addnode
1607 threadGroup.create_thread(boost::bind(&TraceThread<void (*)()>, "addcon", &ThreadOpenAddedConnections));
1609 // Initiate outbound connections
1610 threadGroup.create_thread(boost::bind(&TraceThread<void (*)()>, "opencon", &ThreadOpenConnections));
1613 threadGroup.create_thread(boost::bind(&TraceThread<void (*)()>, "msghand", &ThreadMessageHandler));
1615 // Dump network addresses
1616 threadGroup.create_thread(boost::bind(&LoopForever<void (*)()>, "dumpaddr", &DumpAddresses, DUMP_ADDRESSES_INTERVAL * 1000));
1621 LogPrintf("StopNode()\n");
1624 for (int i=0; i<MAX_OUTBOUND_CONNECTIONS; i++)
1625 semOutbound->post();
1627 if (fAddressesInitialized)
1630 fAddressesInitialized = false;
1644 BOOST_FOREACH(CNode* pnode, vNodes)
1645 if (pnode->hSocket != INVALID_SOCKET)
1646 CloseSocket(pnode->hSocket);
1647 BOOST_FOREACH(ListenSocket& hListenSocket, vhListenSocket)
1648 if (hListenSocket.socket != INVALID_SOCKET)
1649 if (!CloseSocket(hListenSocket.socket))
1650 LogPrintf("CloseSocket(hListenSocket) failed with error %s\n", NetworkErrorString(WSAGetLastError()));
1652 // clean up some globals (to help leak detection)
1653 BOOST_FOREACH(CNode *pnode, vNodes)
1655 BOOST_FOREACH(CNode *pnode, vNodesDisconnected)
1658 vNodesDisconnected.clear();
1659 vhListenSocket.clear();
1662 delete pnodeLocalHost;
1663 pnodeLocalHost = NULL;
1666 // Shutdown Windows Sockets
1671 instance_of_cnetcleanup;
1679 void RelayTransaction(const CTransaction& tx)
1681 CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
1684 RelayTransaction(tx, ss);
1687 void RelayTransaction(const CTransaction& tx, const CDataStream& ss)
1689 CInv inv(MSG_TX, tx.GetHash());
1692 // Expire old relay messages
1693 while (!vRelayExpiration.empty() && vRelayExpiration.front().first < GetTime())
1695 mapRelay.erase(vRelayExpiration.front().second);
1696 vRelayExpiration.pop_front();
1699 // Save original serialized message so newer versions are preserved
1700 mapRelay.insert(std::make_pair(inv, ss));
1701 vRelayExpiration.push_back(std::make_pair(GetTime() + 15 * 60, inv));
1704 BOOST_FOREACH(CNode* pnode, vNodes)
1706 if(!pnode->fRelayTxes)
1708 LOCK(pnode->cs_filter);
1711 if (pnode->pfilter->IsRelevantAndUpdate(tx))
1712 pnode->PushInventory(inv);
1714 pnode->PushInventory(inv);
1718 void CNode::RecordBytesRecv(uint64_t bytes)
1720 LOCK(cs_totalBytesRecv);
1721 nTotalBytesRecv += bytes;
1724 void CNode::RecordBytesSent(uint64_t bytes)
1726 LOCK(cs_totalBytesSent);
1727 nTotalBytesSent += bytes;
1730 uint64_t CNode::GetTotalBytesRecv()
1732 LOCK(cs_totalBytesRecv);
1733 return nTotalBytesRecv;
1736 uint64_t CNode::GetTotalBytesSent()
1738 LOCK(cs_totalBytesSent);
1739 return nTotalBytesSent;
1742 void CNode::Fuzz(int nChance)
1744 if (!fSuccessfullyConnected) return; // Don't fuzz initial handshake
1745 if (GetRand(nChance) != 0) return; // Fuzz 1 of every nChance messages
1750 // xor a random byte with a random value:
1751 if (!ssSend.empty()) {
1752 CDataStream::size_type pos = GetRand(ssSend.size());
1753 ssSend[pos] ^= (unsigned char)(GetRand(256));
1757 // delete a random byte:
1758 if (!ssSend.empty()) {
1759 CDataStream::size_type pos = GetRand(ssSend.size());
1760 ssSend.erase(ssSend.begin()+pos);
1764 // insert a random byte at a random position
1766 CDataStream::size_type pos = GetRand(ssSend.size());
1767 char ch = (char)GetRand(256);
1768 ssSend.insert(ssSend.begin()+pos, ch);
1772 // Chance of more than one change half the time:
1773 // (more changes exponentially less likely):
1783 pathAddr = GetDataDir() / "peers.dat";
1786 bool CAddrDB::Write(const CAddrMan& addr)
1788 // Generate random temporary filename
1789 unsigned short randv = 0;
1790 GetRandBytes((unsigned char*)&randv, sizeof(randv));
1791 std::string tmpfn = strprintf("peers.dat.%04x", randv);
1793 // serialize addresses, checksum data up to that point, then append csum
1794 CDataStream ssPeers(SER_DISK, CLIENT_VERSION);
1795 ssPeers << FLATDATA(Params().MessageStart());
1797 uint256 hash = Hash(ssPeers.begin(), ssPeers.end());
1800 // open temp output file, and associate with CAutoFile
1801 boost::filesystem::path pathTmp = GetDataDir() / tmpfn;
1802 FILE *file = fopen(pathTmp.string().c_str(), "wb");
1803 CAutoFile fileout(file, SER_DISK, CLIENT_VERSION);
1804 if (fileout.IsNull())
1805 return error("%s: Failed to open file %s", __func__, pathTmp.string());
1807 // Write and commit header, data
1811 catch (const std::exception& e) {
1812 return error("%s: Serialize or I/O error - %s", __func__, e.what());
1814 FileCommit(fileout.Get());
1817 // replace existing peers.dat, if any, with new peers.dat.XXXX
1818 if (!RenameOver(pathTmp, pathAddr))
1819 return error("%s: Rename-into-place failed", __func__);
1824 bool CAddrDB::Read(CAddrMan& addr)
1826 // open input file, and associate with CAutoFile
1827 FILE *file = fopen(pathAddr.string().c_str(), "rb");
1828 CAutoFile filein(file, SER_DISK, CLIENT_VERSION);
1829 if (filein.IsNull())
1830 return error("%s: Failed to open file %s", __func__, pathAddr.string());
1832 // use file size to size memory buffer
1833 int fileSize = boost::filesystem::file_size(pathAddr);
1834 int dataSize = fileSize - sizeof(uint256);
1835 // Don't try to resize to a negative number if file is small
1838 vector<unsigned char> vchData;
1839 vchData.resize(dataSize);
1842 // read data and checksum from file
1844 filein.read((char *)&vchData[0], dataSize);
1847 catch (const std::exception& e) {
1848 return error("%s: Deserialize or I/O error - %s", __func__, e.what());
1852 CDataStream ssPeers(vchData, SER_DISK, CLIENT_VERSION);
1854 // verify stored checksum matches input data
1855 uint256 hashTmp = Hash(ssPeers.begin(), ssPeers.end());
1856 if (hashIn != hashTmp)
1857 return error("%s: Checksum mismatch, data corrupted", __func__);
1859 unsigned char pchMsgTmp[4];
1861 // de-serialize file header (network specific magic number) and ..
1862 ssPeers >> FLATDATA(pchMsgTmp);
1864 // ... verify the network matches ours
1865 if (memcmp(pchMsgTmp, Params().MessageStart(), sizeof(pchMsgTmp)))
1866 return error("%s: Invalid network magic number", __func__);
1868 // de-serialize address data into one CAddrMan object
1871 catch (const std::exception& e) {
1872 return error("%s: Deserialize or I/O error - %s", __func__, e.what());
1878 unsigned int ReceiveFloodSize() { return 1000*GetArg("-maxreceivebuffer", 5*1000); }
1879 unsigned int SendBufferSize() { return 1000*GetArg("-maxsendbuffer", 1*1000); }
1881 CNode::CNode(SOCKET hSocketIn, CAddress addrIn, std::string addrNameIn, bool fInboundIn) : ssSend(SER_NETWORK, INIT_PROTO_VERSION), setAddrKnown(5000)
1884 hSocket = hSocketIn;
1885 nRecvVersion = INIT_PROTO_VERSION;
1890 nTimeConnected = GetTime();
1893 addrName = addrNameIn == "" ? addr.ToStringIPPort() : addrNameIn;
1896 fWhitelisted = false;
1898 fClient = false; // set by version message
1899 fInbound = fInboundIn;
1900 fNetworkNode = false;
1901 fSuccessfullyConnected = false;
1902 fDisconnect = false;
1906 hashContinue = uint256();
1907 nStartingHeight = -1;
1910 setInventoryKnown.max_size(SendBufferSize() / 1000);
1911 pfilter = new CBloomFilter();
1915 fPingQueued = false;
1918 LOCK(cs_nLastNodeId);
1923 LogPrint("net", "Added connection to %s peer=%d\n", addrName, id);
1925 LogPrint("net", "Added connection peer=%d\n", id);
1927 // Be shy and don't send version until we hear
1928 if (hSocket != INVALID_SOCKET && !fInbound)
1931 GetNodeSignals().InitializeNode(GetId(), this);
1936 CloseSocket(hSocket);
1941 GetNodeSignals().FinalizeNode(GetId());
1944 void CNode::AskFor(const CInv& inv)
1946 if (mapAskFor.size() > MAPASKFOR_MAX_SZ)
1948 // We're using mapAskFor as a priority queue,
1949 // the key is the earliest time the request can be sent
1950 int64_t nRequestTime;
1951 limitedmap<CInv, int64_t>::const_iterator it = mapAlreadyAskedFor.find(inv);
1952 if (it != mapAlreadyAskedFor.end())
1953 nRequestTime = it->second;
1956 LogPrint("net", "askfor %s %d (%s) peer=%d\n", inv.ToString(), nRequestTime, DateTimeStrFormat("%H:%M:%S", nRequestTime/1000000), id);
1958 // Make sure not to reuse time indexes to keep things in the same order
1959 int64_t nNow = GetTimeMicros() - 1000000;
1960 static int64_t nLastTime;
1962 nNow = std::max(nNow, nLastTime);
1965 // Each retry is 2 minutes after the last
1966 nRequestTime = std::max(nRequestTime + 2 * 60 * 1000000, nNow);
1967 if (it != mapAlreadyAskedFor.end())
1968 mapAlreadyAskedFor.update(it, nRequestTime);
1970 mapAlreadyAskedFor.insert(std::make_pair(inv, nRequestTime));
1971 mapAskFor.insert(std::make_pair(nRequestTime, inv));
1974 void CNode::BeginMessage(const char* pszCommand) EXCLUSIVE_LOCK_FUNCTION(cs_vSend)
1976 ENTER_CRITICAL_SECTION(cs_vSend);
1977 assert(ssSend.size() == 0);
1978 ssSend << CMessageHeader(Params().MessageStart(), pszCommand, 0);
1979 LogPrint("net", "sending: %s ", SanitizeString(pszCommand));
1982 void CNode::AbortMessage() UNLOCK_FUNCTION(cs_vSend)
1986 LEAVE_CRITICAL_SECTION(cs_vSend);
1988 LogPrint("net", "(aborted)\n");
1991 void CNode::EndMessage() UNLOCK_FUNCTION(cs_vSend)
1993 // The -*messagestest options are intentionally not documented in the help message,
1994 // since they are only used during development to debug the networking code and are
1995 // not intended for end-users.
1996 if (mapArgs.count("-dropmessagestest") && GetRand(GetArg("-dropmessagestest", 2)) == 0)
1998 LogPrint("net", "dropmessages DROPPING SEND MESSAGE\n");
2002 if (mapArgs.count("-fuzzmessagestest"))
2003 Fuzz(GetArg("-fuzzmessagestest", 10));
2005 if (ssSend.size() == 0)
2009 unsigned int nSize = ssSend.size() - CMessageHeader::HEADER_SIZE;
2010 WriteLE32((uint8_t*)&ssSend[CMessageHeader::MESSAGE_SIZE_OFFSET], nSize);
2013 uint256 hash = Hash(ssSend.begin() + CMessageHeader::HEADER_SIZE, ssSend.end());
2014 unsigned int nChecksum = 0;
2015 memcpy(&nChecksum, &hash, sizeof(nChecksum));
2016 assert(ssSend.size () >= CMessageHeader::CHECKSUM_OFFSET + sizeof(nChecksum));
2017 memcpy((char*)&ssSend[CMessageHeader::CHECKSUM_OFFSET], &nChecksum, sizeof(nChecksum));
2019 LogPrint("net", "(%d bytes) peer=%d\n", nSize, id);
2021 std::deque<CSerializeData>::iterator it = vSendMsg.insert(vSendMsg.end(), CSerializeData());
2022 ssSend.GetAndClear(*it);
2023 nSendSize += (*it).size();
2025 // If write queue empty, attempt "optimistic write"
2026 if (it == vSendMsg.begin())
2027 SocketSendData(this);
2029 LEAVE_CRITICAL_SECTION(cs_vSend);