1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 The Bitcoin developers
3 // Distributed under the MIT/X11 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 "bitcoin-config.h"
13 #include "chainparams.h"
15 #include "ui_interface.h"
24 #include <miniupnpc/miniupnpc.h>
25 #include <miniupnpc/miniwget.h>
26 #include <miniupnpc/upnpcommands.h>
27 #include <miniupnpc/upnperrors.h>
30 #include <boost/filesystem.hpp>
32 // Dump addresses to peers.dat every 15 minutes (900s)
33 #define DUMP_ADDRESSES_INTERVAL 900
35 #if !defined(HAVE_MSG_NOSIGNAL) && !defined(MSG_NOSIGNAL)
36 #define MSG_NOSIGNAL 0
40 using namespace boost;
42 static const int MAX_OUTBOUND_CONNECTIONS = 8;
44 bool OpenNetworkConnection(const CAddress& addrConnect, CSemaphoreGrant *grantOutbound = NULL, const char *strDest = NULL, bool fOneShot = false);
47 struct LocalServiceInfo {
53 // Global state variables
55 bool fDiscover = true;
56 uint64_t nLocalServices = NODE_NETWORK;
57 static CCriticalSection cs_mapLocalHost;
58 static map<CNetAddr, LocalServiceInfo> mapLocalHost;
59 static bool vfReachable[NET_MAX] = {};
60 static bool vfLimited[NET_MAX] = {};
61 static CNode* pnodeLocalHost = NULL;
62 static CNode* pnodeSync = NULL;
63 uint64_t nLocalHostNonce = 0;
64 static std::vector<SOCKET> vhListenSocket;
66 int nMaxConnections = 125;
68 vector<CNode*> vNodes;
69 CCriticalSection cs_vNodes;
70 map<CInv, CDataStream> mapRelay;
71 deque<pair<int64_t, CInv> > vRelayExpiration;
72 CCriticalSection cs_mapRelay;
73 limitedmap<CInv, int64_t> mapAlreadyAskedFor(MAX_INV_SZ);
75 static deque<string> vOneShots;
76 CCriticalSection cs_vOneShots;
78 set<CNetAddr> setservAddNodeAddresses;
79 CCriticalSection cs_setservAddNodeAddresses;
81 vector<std::string> vAddedNodes;
82 CCriticalSection cs_vAddedNodes;
84 NodeId nLastNodeId = 0;
85 CCriticalSection cs_nLastNodeId;
87 static CSemaphore *semOutbound = NULL;
89 // Signals for message handling
90 static CNodeSignals g_signals;
91 CNodeSignals& GetNodeSignals() { return g_signals; }
93 void AddOneShot(string strDest)
96 vOneShots.push_back(strDest);
99 unsigned short GetListenPort()
101 return (unsigned short)(GetArg("-port", Params().GetDefaultPort()));
104 // find 'best' local address for a particular peer
105 bool GetLocal(CService& addr, const CNetAddr *paddrPeer)
111 int nBestReachability = -1;
113 LOCK(cs_mapLocalHost);
114 for (map<CNetAddr, LocalServiceInfo>::iterator it = mapLocalHost.begin(); it != mapLocalHost.end(); it++)
116 int nScore = (*it).second.nScore;
117 int nReachability = (*it).first.GetReachabilityFrom(paddrPeer);
118 if (nReachability > nBestReachability || (nReachability == nBestReachability && nScore > nBestScore))
120 addr = CService((*it).first, (*it).second.nPort);
121 nBestReachability = nReachability;
126 return nBestScore >= 0;
129 // get best local address for a particular peer as a CAddress
130 CAddress GetLocalAddress(const CNetAddr *paddrPeer)
132 CAddress ret(CService("0.0.0.0",0),0);
134 if (GetLocal(addr, paddrPeer))
136 ret = CAddress(addr);
137 ret.nServices = nLocalServices;
138 ret.nTime = GetAdjustedTime();
143 bool RecvLine(SOCKET hSocket, string& strLine)
149 int nBytes = recv(hSocket, &c, 1, 0);
157 if (strLine.size() >= 9000)
160 else if (nBytes <= 0)
162 boost::this_thread::interruption_point();
165 int nErr = WSAGetLastError();
166 if (nErr == WSAEMSGSIZE)
168 if (nErr == WSAEWOULDBLOCK || nErr == WSAEINTR || nErr == WSAEINPROGRESS)
174 if (!strLine.empty())
179 LogPrint("net", "socket closed\n");
185 int nErr = WSAGetLastError();
186 LogPrint("net", "recv failed: %d\n", nErr);
193 // used when scores of local addresses may have changed
194 // pushes better local address to peers
195 void static AdvertizeLocal()
198 BOOST_FOREACH(CNode* pnode, vNodes)
200 if (pnode->fSuccessfullyConnected)
202 CAddress addrLocal = GetLocalAddress(&pnode->addr);
203 if (addrLocal.IsRoutable() && (CService)addrLocal != (CService)pnode->addrLocal)
205 pnode->PushAddress(addrLocal);
206 pnode->addrLocal = addrLocal;
212 void SetReachable(enum Network net, bool fFlag)
214 LOCK(cs_mapLocalHost);
215 vfReachable[net] = fFlag;
216 if (net == NET_IPV6 && fFlag)
217 vfReachable[NET_IPV4] = true;
220 // learn a new local address
221 bool AddLocal(const CService& addr, int nScore)
223 if (!addr.IsRoutable())
226 if (!fDiscover && nScore < LOCAL_MANUAL)
232 LogPrintf("AddLocal(%s,%i)\n", addr.ToString(), nScore);
235 LOCK(cs_mapLocalHost);
236 bool fAlready = mapLocalHost.count(addr) > 0;
237 LocalServiceInfo &info = mapLocalHost[addr];
238 if (!fAlready || nScore >= info.nScore) {
239 info.nScore = nScore + (fAlready ? 1 : 0);
240 info.nPort = addr.GetPort();
242 SetReachable(addr.GetNetwork());
250 bool AddLocal(const CNetAddr &addr, int nScore)
252 return AddLocal(CService(addr, GetListenPort()), nScore);
255 /** Make a particular network entirely off-limits (no automatic connects to it) */
256 void SetLimited(enum Network net, bool fLimited)
258 if (net == NET_UNROUTABLE)
260 LOCK(cs_mapLocalHost);
261 vfLimited[net] = fLimited;
264 bool IsLimited(enum Network net)
266 LOCK(cs_mapLocalHost);
267 return vfLimited[net];
270 bool IsLimited(const CNetAddr &addr)
272 return IsLimited(addr.GetNetwork());
275 /** vote for a local address */
276 bool SeenLocal(const CService& addr)
279 LOCK(cs_mapLocalHost);
280 if (mapLocalHost.count(addr) == 0)
282 mapLocalHost[addr].nScore++;
290 /** check whether a given address is potentially local */
291 bool IsLocal(const CService& addr)
293 LOCK(cs_mapLocalHost);
294 return mapLocalHost.count(addr) > 0;
297 /** check whether a given address is in a network we can probably connect to */
298 bool IsReachable(const CNetAddr& addr)
300 LOCK(cs_mapLocalHost);
301 enum Network net = addr.GetNetwork();
302 return vfReachable[net] && !vfLimited[net];
305 bool GetMyExternalIP2(const CService& addrConnect, const char* pszGet, const char* pszKeyword, CNetAddr& ipRet)
308 if (!ConnectSocket(addrConnect, hSocket))
309 return error("GetMyExternalIP() : connection to %s failed", addrConnect.ToString());
311 send(hSocket, pszGet, strlen(pszGet), MSG_NOSIGNAL);
314 while (RecvLine(hSocket, strLine))
316 if (strLine.empty()) // HTTP response is separated from headers by blank line
320 if (!RecvLine(hSocket, strLine))
322 closesocket(hSocket);
325 if (pszKeyword == NULL)
327 if (strLine.find(pszKeyword) != string::npos)
329 strLine = strLine.substr(strLine.find(pszKeyword) + strlen(pszKeyword));
333 closesocket(hSocket);
334 if (strLine.find("<") != string::npos)
335 strLine = strLine.substr(0, strLine.find("<"));
336 strLine = strLine.substr(strspn(strLine.c_str(), " \t\n\r"));
337 while (strLine.size() > 0 && isspace(strLine[strLine.size()-1]))
338 strLine.resize(strLine.size()-1);
339 CService addr(strLine,0,true);
340 LogPrintf("GetMyExternalIP() received [%s] %s\n", strLine, addr.ToString());
341 if (!addr.IsValid() || !addr.IsRoutable())
347 closesocket(hSocket);
348 return error("GetMyExternalIP() : connection closed");
351 bool GetMyExternalIP(CNetAddr& ipRet)
353 CService addrConnect;
355 const char* pszKeyword;
357 for (int nLookup = 0; nLookup <= 1; nLookup++)
358 for (int nHost = 1; nHost <= 2; nHost++)
360 // We should be phasing out our use of sites like these. If we need
361 // replacements, we should ask for volunteers to put this simple
362 // php file on their web server that prints the client IP:
363 // <?php echo $_SERVER["REMOTE_ADDR"]; ?>
366 addrConnect = CService("91.198.22.70", 80); // checkip.dyndns.org
370 CService addrIP("checkip.dyndns.org", 80, true);
371 if (addrIP.IsValid())
372 addrConnect = addrIP;
375 pszGet = "GET / HTTP/1.1\r\n"
376 "Host: checkip.dyndns.org\r\n"
377 "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)\r\n"
378 "Connection: close\r\n"
381 pszKeyword = "Address:";
385 addrConnect = CService("74.208.43.192", 80); // www.showmyip.com
389 CService addrIP("www.showmyip.com", 80, true);
390 if (addrIP.IsValid())
391 addrConnect = addrIP;
394 pszGet = "GET /simple/ HTTP/1.1\r\n"
395 "Host: www.showmyip.com\r\n"
396 "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)\r\n"
397 "Connection: close\r\n"
400 pszKeyword = NULL; // Returns just IP address
403 if (GetMyExternalIP2(addrConnect, pszGet, pszKeyword, ipRet))
410 void ThreadGetMyExternalIP()
412 CNetAddr addrLocalHost;
413 if (GetMyExternalIP(addrLocalHost))
415 LogPrintf("GetMyExternalIP() returned %s\n", addrLocalHost.ToStringIP());
416 AddLocal(addrLocalHost, LOCAL_HTTP);
424 void AddressCurrentlyConnected(const CService& addr)
426 addrman.Connected(addr);
432 uint64_t CNode::nTotalBytesRecv = 0;
433 uint64_t CNode::nTotalBytesSent = 0;
434 CCriticalSection CNode::cs_totalBytesRecv;
435 CCriticalSection CNode::cs_totalBytesSent;
437 CNode* FindNode(const CNetAddr& ip)
440 BOOST_FOREACH(CNode* pnode, vNodes)
441 if ((CNetAddr)pnode->addr == ip)
446 CNode* FindNode(std::string addrName)
449 BOOST_FOREACH(CNode* pnode, vNodes)
450 if (pnode->addrName == addrName)
455 CNode* FindNode(const CService& addr)
458 BOOST_FOREACH(CNode* pnode, vNodes)
459 if ((CService)pnode->addr == addr)
464 CNode* ConnectNode(CAddress addrConnect, const char *pszDest)
466 if (pszDest == NULL) {
467 if (IsLocal(addrConnect))
470 // Look for an existing connection
471 CNode* pnode = FindNode((CService)addrConnect);
481 LogPrint("net", "trying connection %s lastseen=%.1fhrs\n",
482 pszDest ? pszDest : addrConnect.ToString(),
483 pszDest ? 0 : (double)(GetAdjustedTime() - addrConnect.nTime)/3600.0);
487 if (pszDest ? ConnectSocketByName(addrConnect, hSocket, pszDest, Params().GetDefaultPort()) : ConnectSocket(addrConnect, hSocket))
489 addrman.Attempt(addrConnect);
491 LogPrint("net", "connected %s\n", pszDest ? pszDest : addrConnect.ToString());
493 // Set to non-blocking
496 if (ioctlsocket(hSocket, FIONBIO, &nOne) == SOCKET_ERROR)
497 LogPrintf("ConnectSocket() : ioctlsocket non-blocking setting failed, error %d\n", WSAGetLastError());
499 if (fcntl(hSocket, F_SETFL, O_NONBLOCK) == SOCKET_ERROR)
500 LogPrintf("ConnectSocket() : fcntl non-blocking setting failed, error %d\n", errno);
504 CNode* pnode = new CNode(hSocket, addrConnect, pszDest ? pszDest : "", false);
509 vNodes.push_back(pnode);
512 pnode->nTimeConnected = GetTime();
521 void CNode::CloseSocketDisconnect()
524 if (hSocket != INVALID_SOCKET)
526 LogPrint("net", "disconnecting node %s\n", addrName);
527 closesocket(hSocket);
528 hSocket = INVALID_SOCKET;
531 // in case this fails, we'll empty the recv buffer when the CNode is deleted
532 TRY_LOCK(cs_vRecvMsg, lockRecv);
536 // if this was the sync node, we'll need a new one
537 if (this == pnodeSync)
541 void CNode::Cleanup()
546 void CNode::PushVersion()
548 int nBestHeight = g_signals.GetHeight().get_value_or(0);
550 /// when NTP implemented, change to just nTime = GetAdjustedTime()
551 int64_t nTime = (fInbound ? GetAdjustedTime() : GetTime());
552 CAddress addrYou = (addr.IsRoutable() && !IsProxy(addr) ? addr : CAddress(CService("0.0.0.0",0)));
553 CAddress addrMe = GetLocalAddress(&addr);
554 RAND_bytes((unsigned char*)&nLocalHostNonce, sizeof(nLocalHostNonce));
555 LogPrint("net", "send version message: version %d, blocks=%d, us=%s, them=%s, peer=%s\n", PROTOCOL_VERSION, nBestHeight, addrMe.ToString(), addrYou.ToString(), addr.ToString());
556 PushMessage("version", PROTOCOL_VERSION, nLocalServices, nTime, addrYou, addrMe,
557 nLocalHostNonce, FormatSubVersion(CLIENT_NAME, CLIENT_VERSION, std::vector<string>()), nBestHeight, true);
564 std::map<CNetAddr, int64_t> CNode::setBanned;
565 CCriticalSection CNode::cs_setBanned;
567 void CNode::ClearBanned()
572 bool CNode::IsBanned(CNetAddr ip)
574 bool fResult = false;
577 std::map<CNetAddr, int64_t>::iterator i = setBanned.find(ip);
578 if (i != setBanned.end())
580 int64_t t = (*i).second;
588 bool CNode::Ban(const CNetAddr &addr) {
589 int64_t banTime = GetTime()+GetArg("-bantime", 60*60*24); // Default 24-hour ban
592 if (setBanned[addr] < banTime)
593 setBanned[addr] = banTime;
599 #define X(name) stats.name = name
600 void CNode::copyStats(CNodeStats &stats)
602 stats.nodeid = this->GetId();
614 stats.fSyncNode = (this == pnodeSync);
616 // It is common for nodes with good ping times to suddenly become lagged,
617 // due to a new block arriving or other large transfer.
618 // Merely reporting pingtime might fool the caller into thinking the node was still responsive,
619 // since pingtime does not update until the ping is complete, which might take a while.
620 // So, if a ping is taking an unusually long time in flight,
621 // the caller can immediately detect that this is happening.
622 int64_t nPingUsecWait = 0;
623 if ((0 != nPingNonceSent) && (0 != nPingUsecStart)) {
624 nPingUsecWait = GetTimeMicros() - nPingUsecStart;
627 // 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 :)
628 stats.dPingTime = (((double)nPingUsecTime) / 1e6);
629 stats.dPingWait = (((double)nPingUsecWait) / 1e6);
631 // Leave string empty if addrLocal invalid (not filled in yet)
632 stats.addrLocal = addrLocal.IsValid() ? addrLocal.ToString() : "";
636 // requires LOCK(cs_vRecvMsg)
637 bool CNode::ReceiveMsgBytes(const char *pch, unsigned int nBytes)
641 // get current incomplete message, or create a new one
642 if (vRecvMsg.empty() ||
643 vRecvMsg.back().complete())
644 vRecvMsg.push_back(CNetMessage(SER_NETWORK, nRecvVersion));
646 CNetMessage& msg = vRecvMsg.back();
648 // absorb network data
651 handled = msg.readHeader(pch, nBytes);
653 handled = msg.readData(pch, nBytes);
665 int CNetMessage::readHeader(const char *pch, unsigned int nBytes)
667 // copy data to temporary parsing buffer
668 unsigned int nRemaining = 24 - nHdrPos;
669 unsigned int nCopy = std::min(nRemaining, nBytes);
671 memcpy(&hdrbuf[nHdrPos], pch, nCopy);
674 // if header incomplete, exit
678 // deserialize to CMessageHeader
682 catch (std::exception &e) {
686 // reject messages larger than MAX_SIZE
687 if (hdr.nMessageSize > MAX_SIZE)
690 // switch state to reading message data
692 vRecv.resize(hdr.nMessageSize);
697 int CNetMessage::readData(const char *pch, unsigned int nBytes)
699 unsigned int nRemaining = hdr.nMessageSize - nDataPos;
700 unsigned int nCopy = std::min(nRemaining, nBytes);
702 memcpy(&vRecv[nDataPos], pch, nCopy);
716 // requires LOCK(cs_vSend)
717 void SocketSendData(CNode *pnode)
719 std::deque<CSerializeData>::iterator it = pnode->vSendMsg.begin();
721 while (it != pnode->vSendMsg.end()) {
722 const CSerializeData &data = *it;
723 assert(data.size() > pnode->nSendOffset);
724 int nBytes = send(pnode->hSocket, &data[pnode->nSendOffset], data.size() - pnode->nSendOffset, MSG_NOSIGNAL | MSG_DONTWAIT);
726 pnode->nLastSend = GetTime();
727 pnode->nSendBytes += nBytes;
728 pnode->nSendOffset += nBytes;
729 pnode->RecordBytesSent(nBytes);
730 if (pnode->nSendOffset == data.size()) {
731 pnode->nSendOffset = 0;
732 pnode->nSendSize -= data.size();
735 // could not send full message; stop sending more
741 int nErr = WSAGetLastError();
742 if (nErr != WSAEWOULDBLOCK && nErr != WSAEMSGSIZE && nErr != WSAEINTR && nErr != WSAEINPROGRESS)
744 LogPrintf("socket send error %d\n", nErr);
745 pnode->CloseSocketDisconnect();
748 // couldn't send anything at all
753 if (it == pnode->vSendMsg.end()) {
754 assert(pnode->nSendOffset == 0);
755 assert(pnode->nSendSize == 0);
757 pnode->vSendMsg.erase(pnode->vSendMsg.begin(), it);
760 static list<CNode*> vNodesDisconnected;
762 void ThreadSocketHandler()
764 unsigned int nPrevNodeCount = 0;
772 // Disconnect unused nodes
773 vector<CNode*> vNodesCopy = vNodes;
774 BOOST_FOREACH(CNode* pnode, vNodesCopy)
776 if (pnode->fDisconnect ||
777 (pnode->GetRefCount() <= 0 && pnode->vRecvMsg.empty() && pnode->nSendSize == 0 && pnode->ssSend.empty()))
779 // remove from vNodes
780 vNodes.erase(remove(vNodes.begin(), vNodes.end(), pnode), vNodes.end());
782 // release outbound grant (if any)
783 pnode->grantOutbound.Release();
785 // close socket and cleanup
786 pnode->CloseSocketDisconnect();
789 // hold in disconnected pool until all refs are released
790 if (pnode->fNetworkNode || pnode->fInbound)
792 vNodesDisconnected.push_back(pnode);
797 // Delete disconnected nodes
798 list<CNode*> vNodesDisconnectedCopy = vNodesDisconnected;
799 BOOST_FOREACH(CNode* pnode, vNodesDisconnectedCopy)
801 // wait until threads are done using it
802 if (pnode->GetRefCount() <= 0)
804 bool fDelete = false;
806 TRY_LOCK(pnode->cs_vSend, lockSend);
809 TRY_LOCK(pnode->cs_vRecvMsg, lockRecv);
812 TRY_LOCK(pnode->cs_inventory, lockInv);
820 vNodesDisconnected.remove(pnode);
826 if(vNodes.size() != nPrevNodeCount) {
827 nPrevNodeCount = vNodes.size();
828 uiInterface.NotifyNumConnectionsChanged(nPrevNodeCount);
833 // Find which sockets have data to receive
835 struct timeval timeout;
837 timeout.tv_usec = 50000; // frequency to poll pnode->vSend
844 FD_ZERO(&fdsetError);
845 SOCKET hSocketMax = 0;
846 bool have_fds = false;
848 BOOST_FOREACH(SOCKET hListenSocket, vhListenSocket) {
849 FD_SET(hListenSocket, &fdsetRecv);
850 hSocketMax = max(hSocketMax, hListenSocket);
855 BOOST_FOREACH(CNode* pnode, vNodes)
857 if (pnode->hSocket == INVALID_SOCKET)
859 FD_SET(pnode->hSocket, &fdsetError);
860 hSocketMax = max(hSocketMax, pnode->hSocket);
863 // Implement the following logic:
864 // * If there is data to send, select() for sending data. As this only
865 // happens when optimistic write failed, we choose to first drain the
866 // write buffer in this case before receiving more. This avoids
867 // needlessly queueing received data, if the remote peer is not themselves
868 // receiving data. This means properly utilizing TCP flow control signalling.
869 // * Otherwise, if there is no (complete) message in the receive buffer,
870 // or there is space left in the buffer, select() for receiving data.
871 // * (if neither of the above applies, there is certainly one message
872 // in the receiver buffer ready to be processed).
873 // Together, that means that at least one of the following is always possible,
874 // so we don't deadlock:
875 // * We send some data.
876 // * We wait for data to be received (and disconnect after timeout).
877 // * We process a message in the buffer (message handler thread).
879 TRY_LOCK(pnode->cs_vSend, lockSend);
880 if (lockSend && !pnode->vSendMsg.empty()) {
881 FD_SET(pnode->hSocket, &fdsetSend);
886 TRY_LOCK(pnode->cs_vRecvMsg, lockRecv);
888 pnode->vRecvMsg.empty() || !pnode->vRecvMsg.front().complete() ||
889 pnode->GetTotalRecvSize() <= ReceiveFloodSize()))
890 FD_SET(pnode->hSocket, &fdsetRecv);
895 int nSelect = select(have_fds ? hSocketMax + 1 : 0,
896 &fdsetRecv, &fdsetSend, &fdsetError, &timeout);
897 boost::this_thread::interruption_point();
899 if (nSelect == SOCKET_ERROR)
903 int nErr = WSAGetLastError();
904 LogPrintf("socket select error %d\n", nErr);
905 for (unsigned int i = 0; i <= hSocketMax; i++)
906 FD_SET(i, &fdsetRecv);
909 FD_ZERO(&fdsetError);
910 MilliSleep(timeout.tv_usec/1000);
915 // Accept new connections
917 BOOST_FOREACH(SOCKET hListenSocket, vhListenSocket)
918 if (hListenSocket != INVALID_SOCKET && FD_ISSET(hListenSocket, &fdsetRecv))
920 struct sockaddr_storage sockaddr;
921 socklen_t len = sizeof(sockaddr);
922 SOCKET hSocket = accept(hListenSocket, (struct sockaddr*)&sockaddr, &len);
926 if (hSocket != INVALID_SOCKET)
927 if (!addr.SetSockAddr((const struct sockaddr*)&sockaddr))
928 LogPrintf("Warning: Unknown socket family\n");
932 BOOST_FOREACH(CNode* pnode, vNodes)
937 if (hSocket == INVALID_SOCKET)
939 int nErr = WSAGetLastError();
940 if (nErr != WSAEWOULDBLOCK)
941 LogPrintf("socket error accept failed: %d\n", nErr);
943 else if (nInbound >= nMaxConnections - MAX_OUTBOUND_CONNECTIONS)
945 closesocket(hSocket);
947 else if (CNode::IsBanned(addr))
949 LogPrintf("connection from %s dropped (banned)\n", addr.ToString());
950 closesocket(hSocket);
954 LogPrint("net", "accepted connection %s\n", addr.ToString());
955 CNode* pnode = new CNode(hSocket, addr, "", true);
959 vNodes.push_back(pnode);
966 // Service each socket
968 vector<CNode*> vNodesCopy;
972 BOOST_FOREACH(CNode* pnode, vNodesCopy)
975 BOOST_FOREACH(CNode* pnode, vNodesCopy)
977 boost::this_thread::interruption_point();
982 if (pnode->hSocket == INVALID_SOCKET)
984 if (FD_ISSET(pnode->hSocket, &fdsetRecv) || FD_ISSET(pnode->hSocket, &fdsetError))
986 TRY_LOCK(pnode->cs_vRecvMsg, lockRecv);
990 // typical socket buffer is 8K-64K
991 char pchBuf[0x10000];
992 int nBytes = recv(pnode->hSocket, pchBuf, sizeof(pchBuf), MSG_DONTWAIT);
995 if (!pnode->ReceiveMsgBytes(pchBuf, nBytes))
996 pnode->CloseSocketDisconnect();
997 pnode->nLastRecv = GetTime();
998 pnode->nRecvBytes += nBytes;
999 pnode->RecordBytesRecv(nBytes);
1001 else if (nBytes == 0)
1003 // socket closed gracefully
1004 if (!pnode->fDisconnect)
1005 LogPrint("net", "socket closed\n");
1006 pnode->CloseSocketDisconnect();
1008 else if (nBytes < 0)
1011 int nErr = WSAGetLastError();
1012 if (nErr != WSAEWOULDBLOCK && nErr != WSAEMSGSIZE && nErr != WSAEINTR && nErr != WSAEINPROGRESS)
1014 if (!pnode->fDisconnect)
1015 LogPrintf("socket recv error %d\n", nErr);
1016 pnode->CloseSocketDisconnect();
1026 if (pnode->hSocket == INVALID_SOCKET)
1028 if (FD_ISSET(pnode->hSocket, &fdsetSend))
1030 TRY_LOCK(pnode->cs_vSend, lockSend);
1032 SocketSendData(pnode);
1036 // Inactivity checking
1038 if (pnode->vSendMsg.empty())
1039 pnode->nLastSendEmpty = GetTime();
1040 if (GetTime() - pnode->nTimeConnected > 60)
1042 if (pnode->nLastRecv == 0 || pnode->nLastSend == 0)
1044 LogPrint("net", "socket no message in first 60 seconds, %d %d\n", pnode->nLastRecv != 0, pnode->nLastSend != 0);
1045 pnode->fDisconnect = true;
1047 else if (GetTime() - pnode->nLastSend > 90*60 && GetTime() - pnode->nLastSendEmpty > 90*60)
1049 LogPrintf("socket not sending\n");
1050 pnode->fDisconnect = true;
1052 else if (GetTime() - pnode->nLastRecv > 90*60)
1054 LogPrintf("socket inactivity timeout\n");
1055 pnode->fDisconnect = true;
1061 BOOST_FOREACH(CNode* pnode, vNodesCopy)
1078 void ThreadMapPort()
1080 std::string port = strprintf("%u", GetListenPort());
1081 const char * multicastif = 0;
1082 const char * minissdpdpath = 0;
1083 struct UPNPDev * devlist = 0;
1086 #ifndef UPNPDISCOVER_SUCCESS
1088 devlist = upnpDiscover(2000, multicastif, minissdpdpath, 0);
1092 devlist = upnpDiscover(2000, multicastif, minissdpdpath, 0, 0, &error);
1095 struct UPNPUrls urls;
1096 struct IGDdatas data;
1099 r = UPNP_GetValidIGD(devlist, &urls, &data, lanaddr, sizeof(lanaddr));
1103 char externalIPAddress[40];
1104 r = UPNP_GetExternalIPAddress(urls.controlURL, data.first.servicetype, externalIPAddress);
1105 if(r != UPNPCOMMAND_SUCCESS)
1106 LogPrintf("UPnP: GetExternalIPAddress() returned %d\n", r);
1109 if(externalIPAddress[0])
1111 LogPrintf("UPnP: ExternalIPAddress = %s\n", externalIPAddress);
1112 AddLocal(CNetAddr(externalIPAddress), LOCAL_UPNP);
1115 LogPrintf("UPnP: GetExternalIPAddress failed.\n");
1119 string strDesc = "Bitcoin " + FormatFullVersion();
1123 #ifndef UPNPDISCOVER_SUCCESS
1125 r = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype,
1126 port.c_str(), port.c_str(), lanaddr, strDesc.c_str(), "TCP", 0);
1129 r = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype,
1130 port.c_str(), port.c_str(), lanaddr, strDesc.c_str(), "TCP", 0, "0");
1133 if(r!=UPNPCOMMAND_SUCCESS)
1134 LogPrintf("AddPortMapping(%s, %s, %s) failed with code %d (%s)\n",
1135 port, port, lanaddr, r, strupnperror(r));
1137 LogPrintf("UPnP Port Mapping successful.\n");;
1139 MilliSleep(20*60*1000); // Refresh every 20 minutes
1142 catch (boost::thread_interrupted)
1144 r = UPNP_DeletePortMapping(urls.controlURL, data.first.servicetype, port.c_str(), "TCP", 0);
1145 LogPrintf("UPNP_DeletePortMapping() returned : %d\n", r);
1146 freeUPNPDevlist(devlist); devlist = 0;
1147 FreeUPNPUrls(&urls);
1151 LogPrintf("No valid UPnP IGDs found\n");
1152 freeUPNPDevlist(devlist); devlist = 0;
1154 FreeUPNPUrls(&urls);
1158 void MapPort(bool fUseUPnP)
1160 static boost::thread* upnp_thread = NULL;
1165 upnp_thread->interrupt();
1166 upnp_thread->join();
1169 upnp_thread = new boost::thread(boost::bind(&TraceThread<void (*)()>, "upnp", &ThreadMapPort));
1171 else if (upnp_thread) {
1172 upnp_thread->interrupt();
1173 upnp_thread->join();
1182 // Intentionally left blank.
1191 void ThreadDNSAddressSeed()
1193 const vector<CDNSSeedData> &vSeeds = Params().DNSSeeds();
1196 LogPrintf("Loading addresses from DNS seeds (could take a while)\n");
1198 BOOST_FOREACH(const CDNSSeedData &seed, vSeeds) {
1199 if (HaveNameProxy()) {
1200 AddOneShot(seed.host);
1202 vector<CNetAddr> vIPs;
1203 vector<CAddress> vAdd;
1204 if (LookupHost(seed.host.c_str(), vIPs))
1206 BOOST_FOREACH(CNetAddr& ip, vIPs)
1208 int nOneDay = 24*3600;
1209 CAddress addr = CAddress(CService(ip, Params().GetDefaultPort()));
1210 addr.nTime = GetTime() - 3*nOneDay - GetRand(4*nOneDay); // use a random age between 3 and 7 days old
1211 vAdd.push_back(addr);
1215 addrman.Add(vAdd, CNetAddr(seed.name, true));
1219 LogPrintf("%d addresses found from DNS seeds\n", found);
1233 void DumpAddresses()
1235 int64_t nStart = GetTimeMillis();
1240 LogPrint("net", "Flushed %d addresses to peers.dat %dms\n",
1241 addrman.size(), GetTimeMillis() - nStart);
1244 void static ProcessOneShot()
1249 if (vOneShots.empty())
1251 strDest = vOneShots.front();
1252 vOneShots.pop_front();
1255 CSemaphoreGrant grant(*semOutbound, true);
1257 if (!OpenNetworkConnection(addr, &grant, strDest.c_str(), true))
1258 AddOneShot(strDest);
1262 void ThreadOpenConnections()
1264 // Connect to specific addresses
1265 if (mapArgs.count("-connect") && mapMultiArgs["-connect"].size() > 0)
1267 for (int64_t nLoop = 0;; nLoop++)
1270 BOOST_FOREACH(string strAddr, mapMultiArgs["-connect"])
1273 OpenNetworkConnection(addr, NULL, strAddr.c_str());
1274 for (int i = 0; i < 10 && i < nLoop; i++)
1283 // Initiate network connections
1284 int64_t nStart = GetTime();
1291 CSemaphoreGrant grant(*semOutbound);
1292 boost::this_thread::interruption_point();
1294 // Add seed nodes if DNS seeds are all down (an infrastructure attack?).
1295 if (addrman.size() == 0 && (GetTime() - nStart > 60)) {
1296 static bool done = false;
1298 LogPrintf("Adding fixed seed nodes as DNS doesn't seem to be available.\n");
1299 addrman.Add(Params().FixedSeeds(), CNetAddr("127.0.0.1"));
1305 // Choose an address to connect to based on most recently seen
1307 CAddress addrConnect;
1309 // Only connect out to one peer per network group (/16 for IPv4).
1310 // Do this here so we don't have to critsect vNodes inside mapAddresses critsect.
1312 set<vector<unsigned char> > setConnected;
1315 BOOST_FOREACH(CNode* pnode, vNodes) {
1316 if (!pnode->fInbound) {
1317 setConnected.insert(pnode->addr.GetGroup());
1323 int64_t nANow = GetAdjustedTime();
1328 // use an nUnkBias between 10 (no outgoing connections) and 90 (8 outgoing connections)
1329 CAddress addr = addrman.Select(10 + min(nOutbound,8)*10);
1331 // if we selected an invalid address, restart
1332 if (!addr.IsValid() || setConnected.count(addr.GetGroup()) || IsLocal(addr))
1335 // If we didn't find an appropriate destination after trying 100 addresses fetched from addrman,
1336 // stop this loop, and let the outer loop run again (which sleeps, adds seed nodes, recalculates
1337 // already-connected network ranges, ...) before trying new addrman addresses.
1342 if (IsLimited(addr))
1345 // only consider very recently tried nodes after 30 failed attempts
1346 if (nANow - addr.nLastTry < 600 && nTries < 30)
1349 // do not allow non-default ports, unless after 50 invalid addresses selected already
1350 if (addr.GetPort() != Params().GetDefaultPort() && nTries < 50)
1357 if (addrConnect.IsValid())
1358 OpenNetworkConnection(addrConnect, &grant);
1362 void ThreadOpenAddedConnections()
1365 LOCK(cs_vAddedNodes);
1366 vAddedNodes = mapMultiArgs["-addnode"];
1369 if (HaveNameProxy()) {
1371 list<string> lAddresses(0);
1373 LOCK(cs_vAddedNodes);
1374 BOOST_FOREACH(string& strAddNode, vAddedNodes)
1375 lAddresses.push_back(strAddNode);
1377 BOOST_FOREACH(string& strAddNode, lAddresses) {
1379 CSemaphoreGrant grant(*semOutbound);
1380 OpenNetworkConnection(addr, &grant, strAddNode.c_str());
1383 MilliSleep(120000); // Retry every 2 minutes
1387 for (unsigned int i = 0; true; i++)
1389 list<string> lAddresses(0);
1391 LOCK(cs_vAddedNodes);
1392 BOOST_FOREACH(string& strAddNode, vAddedNodes)
1393 lAddresses.push_back(strAddNode);
1396 list<vector<CService> > lservAddressesToAdd(0);
1397 BOOST_FOREACH(string& strAddNode, lAddresses)
1399 vector<CService> vservNode(0);
1400 if(Lookup(strAddNode.c_str(), vservNode, Params().GetDefaultPort(), fNameLookup, 0))
1402 lservAddressesToAdd.push_back(vservNode);
1404 LOCK(cs_setservAddNodeAddresses);
1405 BOOST_FOREACH(CService& serv, vservNode)
1406 setservAddNodeAddresses.insert(serv);
1410 // Attempt to connect to each IP for each addnode entry until at least one is successful per addnode entry
1411 // (keeping in mind that addnode entries can have many IPs if fNameLookup)
1414 BOOST_FOREACH(CNode* pnode, vNodes)
1415 for (list<vector<CService> >::iterator it = lservAddressesToAdd.begin(); it != lservAddressesToAdd.end(); it++)
1416 BOOST_FOREACH(CService& addrNode, *(it))
1417 if (pnode->addr == addrNode)
1419 it = lservAddressesToAdd.erase(it);
1424 BOOST_FOREACH(vector<CService>& vserv, lservAddressesToAdd)
1426 CSemaphoreGrant grant(*semOutbound);
1427 OpenNetworkConnection(CAddress(vserv[i % vserv.size()]), &grant);
1430 MilliSleep(120000); // Retry every 2 minutes
1434 // if successful, this moves the passed grant to the constructed node
1435 bool OpenNetworkConnection(const CAddress& addrConnect, CSemaphoreGrant *grantOutbound, const char *strDest, bool fOneShot)
1438 // Initiate outbound network connection
1440 boost::this_thread::interruption_point();
1442 if (IsLocal(addrConnect) ||
1443 FindNode((CNetAddr)addrConnect) || CNode::IsBanned(addrConnect) ||
1444 FindNode(addrConnect.ToStringIPPort().c_str()))
1446 if (strDest && FindNode(strDest))
1449 CNode* pnode = ConnectNode(addrConnect, strDest);
1450 boost::this_thread::interruption_point();
1455 grantOutbound->MoveTo(pnode->grantOutbound);
1456 pnode->fNetworkNode = true;
1458 pnode->fOneShot = true;
1464 // for now, use a very simple selection metric: the node from which we received
1466 double static NodeSyncScore(const CNode *pnode) {
1467 return -pnode->nLastRecv;
1470 void static StartSync(const vector<CNode*> &vNodes) {
1471 CNode *pnodeNewSync = NULL;
1472 double dBestScore = 0;
1474 int nBestHeight = g_signals.GetHeight().get_value_or(0);
1476 // Iterate over all nodes
1477 BOOST_FOREACH(CNode* pnode, vNodes) {
1478 // check preconditions for allowing a sync
1479 if (!pnode->fClient && !pnode->fOneShot &&
1480 !pnode->fDisconnect && pnode->fSuccessfullyConnected &&
1481 (pnode->nStartingHeight > (nBestHeight - 144)) &&
1482 (pnode->nVersion < NOBLKS_VERSION_START || pnode->nVersion >= NOBLKS_VERSION_END)) {
1483 // if ok, compare node's score with the best so far
1484 double dScore = NodeSyncScore(pnode);
1485 if (pnodeNewSync == NULL || dScore > dBestScore) {
1486 pnodeNewSync = pnode;
1487 dBestScore = dScore;
1491 // if a new sync candidate was found, start sync!
1493 pnodeNewSync->fStartSync = true;
1494 pnodeSync = pnodeNewSync;
1498 void ThreadMessageHandler()
1500 SetThreadPriority(THREAD_PRIORITY_BELOW_NORMAL);
1503 bool fHaveSyncNode = false;
1505 vector<CNode*> vNodesCopy;
1508 vNodesCopy = vNodes;
1509 BOOST_FOREACH(CNode* pnode, vNodesCopy) {
1511 if (pnode == pnodeSync)
1512 fHaveSyncNode = true;
1517 StartSync(vNodesCopy);
1519 // Poll the connected nodes for messages
1520 CNode* pnodeTrickle = NULL;
1521 if (!vNodesCopy.empty())
1522 pnodeTrickle = vNodesCopy[GetRand(vNodesCopy.size())];
1526 BOOST_FOREACH(CNode* pnode, vNodesCopy)
1528 if (pnode->fDisconnect)
1533 TRY_LOCK(pnode->cs_vRecvMsg, lockRecv);
1536 if (!g_signals.ProcessMessages(pnode))
1537 pnode->CloseSocketDisconnect();
1539 if (pnode->nSendSize < SendBufferSize())
1541 if (!pnode->vRecvGetData.empty() || (!pnode->vRecvMsg.empty() && pnode->vRecvMsg[0].complete()))
1548 boost::this_thread::interruption_point();
1552 TRY_LOCK(pnode->cs_vSend, lockSend);
1554 g_signals.SendMessages(pnode, pnode == pnodeTrickle);
1556 boost::this_thread::interruption_point();
1561 BOOST_FOREACH(CNode* pnode, vNodesCopy)
1575 bool BindListenPort(const CService &addrBind, string& strError)
1580 // Create socket for listening for incoming connections
1581 struct sockaddr_storage sockaddr;
1582 socklen_t len = sizeof(sockaddr);
1583 if (!addrBind.GetSockAddr((struct sockaddr*)&sockaddr, &len))
1585 strError = strprintf("Error: bind address family for %s not supported", addrBind.ToString());
1586 LogPrintf("%s\n", strError);
1590 SOCKET hListenSocket = socket(((struct sockaddr*)&sockaddr)->sa_family, SOCK_STREAM, IPPROTO_TCP);
1591 if (hListenSocket == INVALID_SOCKET)
1593 strError = strprintf("Error: Couldn't open socket for incoming connections (socket returned error %d)", WSAGetLastError());
1594 LogPrintf("%s\n", strError);
1599 // Different way of disabling SIGPIPE on BSD
1600 setsockopt(hListenSocket, SOL_SOCKET, SO_NOSIGPIPE, (void*)&nOne, sizeof(int));
1604 // Allow binding if the port is still in TIME_WAIT state after
1605 // the program was closed and restarted. Not an issue on windows.
1606 setsockopt(hListenSocket, SOL_SOCKET, SO_REUSEADDR, (void*)&nOne, sizeof(int));
1611 // Set to non-blocking, incoming connections will also inherit this
1612 if (ioctlsocket(hListenSocket, FIONBIO, (u_long*)&nOne) == SOCKET_ERROR)
1614 if (fcntl(hListenSocket, F_SETFL, O_NONBLOCK) == SOCKET_ERROR)
1617 strError = strprintf("Error: Couldn't set properties on socket for incoming connections (error %d)", WSAGetLastError());
1618 LogPrintf("%s\n", strError);
1622 // some systems don't have IPV6_V6ONLY but are always v6only; others do have the option
1623 // and enable it by default or not. Try to enable it, if possible.
1624 if (addrBind.IsIPv6()) {
1627 setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_V6ONLY, (const char*)&nOne, sizeof(int));
1629 setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_V6ONLY, (void*)&nOne, sizeof(int));
1633 int nProtLevel = 10 /* PROTECTION_LEVEL_UNRESTRICTED */;
1634 int nParameterId = 23 /* IPV6_PROTECTION_LEVEl */;
1635 // this call is allowed to fail
1636 setsockopt(hListenSocket, IPPROTO_IPV6, nParameterId, (const char*)&nProtLevel, sizeof(int));
1640 if (::bind(hListenSocket, (struct sockaddr*)&sockaddr, len) == SOCKET_ERROR)
1642 int nErr = WSAGetLastError();
1643 if (nErr == WSAEADDRINUSE)
1644 strError = strprintf(_("Unable to bind to %s on this computer. Bitcoin Core is probably already running."), addrBind.ToString());
1646 strError = strprintf(_("Unable to bind to %s on this computer (bind returned error %d, %s)"), addrBind.ToString(), nErr, strerror(nErr));
1647 LogPrintf("%s\n", strError);
1650 LogPrintf("Bound to %s\n", addrBind.ToString());
1652 // Listen for incoming connections
1653 if (listen(hListenSocket, SOMAXCONN) == SOCKET_ERROR)
1655 strError = strprintf(_("Error: Listening for incoming connections failed (listen returned error %d)"), WSAGetLastError());
1656 LogPrintf("%s\n", strError);
1660 vhListenSocket.push_back(hListenSocket);
1662 if (addrBind.IsRoutable() && fDiscover)
1663 AddLocal(addrBind, LOCAL_BIND);
1668 void static Discover(boost::thread_group& threadGroup)
1674 // Get local host IP
1675 char pszHostName[1000] = "";
1676 if (gethostname(pszHostName, sizeof(pszHostName)) != SOCKET_ERROR)
1678 vector<CNetAddr> vaddr;
1679 if (LookupHost(pszHostName, vaddr))
1681 BOOST_FOREACH (const CNetAddr &addr, vaddr)
1683 AddLocal(addr, LOCAL_IF);
1688 // Get local host ip
1689 struct ifaddrs* myaddrs;
1690 if (getifaddrs(&myaddrs) == 0)
1692 for (struct ifaddrs* ifa = myaddrs; ifa != NULL; ifa = ifa->ifa_next)
1694 if (ifa->ifa_addr == NULL) continue;
1695 if ((ifa->ifa_flags & IFF_UP) == 0) continue;
1696 if (strcmp(ifa->ifa_name, "lo") == 0) continue;
1697 if (strcmp(ifa->ifa_name, "lo0") == 0) continue;
1698 if (ifa->ifa_addr->sa_family == AF_INET)
1700 struct sockaddr_in* s4 = (struct sockaddr_in*)(ifa->ifa_addr);
1701 CNetAddr addr(s4->sin_addr);
1702 if (AddLocal(addr, LOCAL_IF))
1703 LogPrintf("IPv4 %s: %s\n", ifa->ifa_name, addr.ToString());
1705 else if (ifa->ifa_addr->sa_family == AF_INET6)
1707 struct sockaddr_in6* s6 = (struct sockaddr_in6*)(ifa->ifa_addr);
1708 CNetAddr addr(s6->sin6_addr);
1709 if (AddLocal(addr, LOCAL_IF))
1710 LogPrintf("IPv6 %s: %s\n", ifa->ifa_name, addr.ToString());
1713 freeifaddrs(myaddrs);
1717 // Don't use external IPv4 discovery, when -onlynet="IPv6"
1718 if (!IsLimited(NET_IPV4))
1719 threadGroup.create_thread(boost::bind(&TraceThread<void (*)()>, "ext-ip", &ThreadGetMyExternalIP));
1722 void StartNode(boost::thread_group& threadGroup)
1724 if (semOutbound == NULL) {
1725 // initialize semaphore
1726 int nMaxOutbound = min(MAX_OUTBOUND_CONNECTIONS, nMaxConnections);
1727 semOutbound = new CSemaphore(nMaxOutbound);
1730 if (pnodeLocalHost == NULL)
1731 pnodeLocalHost = new CNode(INVALID_SOCKET, CAddress(CService("127.0.0.1", 0), nLocalServices));
1733 Discover(threadGroup);
1739 if (!GetBoolArg("-dnsseed", true))
1740 LogPrintf("DNS seeding disabled\n");
1742 threadGroup.create_thread(boost::bind(&TraceThread<void (*)()>, "dnsseed", &ThreadDNSAddressSeed));
1745 // Map ports with UPnP
1746 MapPort(GetBoolArg("-upnp", USE_UPNP));
1749 // Send and receive from sockets, accept connections
1750 threadGroup.create_thread(boost::bind(&TraceThread<void (*)()>, "net", &ThreadSocketHandler));
1752 // Initiate outbound connections from -addnode
1753 threadGroup.create_thread(boost::bind(&TraceThread<void (*)()>, "addcon", &ThreadOpenAddedConnections));
1755 // Initiate outbound connections
1756 threadGroup.create_thread(boost::bind(&TraceThread<void (*)()>, "opencon", &ThreadOpenConnections));
1759 threadGroup.create_thread(boost::bind(&TraceThread<void (*)()>, "msghand", &ThreadMessageHandler));
1761 // Dump network addresses
1762 threadGroup.create_thread(boost::bind(&LoopForever<void (*)()>, "dumpaddr", &DumpAddresses, DUMP_ADDRESSES_INTERVAL * 1000));
1767 LogPrintf("StopNode()\n");
1770 for (int i=0; i<MAX_OUTBOUND_CONNECTIONS; i++)
1771 semOutbound->post();
1787 BOOST_FOREACH(CNode* pnode, vNodes)
1788 if (pnode->hSocket != INVALID_SOCKET)
1789 closesocket(pnode->hSocket);
1790 BOOST_FOREACH(SOCKET hListenSocket, vhListenSocket)
1791 if (hListenSocket != INVALID_SOCKET)
1792 if (closesocket(hListenSocket) == SOCKET_ERROR)
1793 LogPrintf("closesocket(hListenSocket) failed with error %d\n", WSAGetLastError());
1795 // clean up some globals (to help leak detection)
1796 BOOST_FOREACH(CNode *pnode, vNodes)
1798 BOOST_FOREACH(CNode *pnode, vNodesDisconnected)
1801 vNodesDisconnected.clear();
1804 delete pnodeLocalHost;
1805 pnodeLocalHost = NULL;
1808 // Shutdown Windows Sockets
1813 instance_of_cnetcleanup;
1821 void RelayTransaction(const CTransaction& tx, const uint256& hash)
1823 CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
1826 RelayTransaction(tx, hash, ss);
1829 void RelayTransaction(const CTransaction& tx, const uint256& hash, const CDataStream& ss)
1831 CInv inv(MSG_TX, hash);
1834 // Expire old relay messages
1835 while (!vRelayExpiration.empty() && vRelayExpiration.front().first < GetTime())
1837 mapRelay.erase(vRelayExpiration.front().second);
1838 vRelayExpiration.pop_front();
1841 // Save original serialized message so newer versions are preserved
1842 mapRelay.insert(std::make_pair(inv, ss));
1843 vRelayExpiration.push_back(std::make_pair(GetTime() + 15 * 60, inv));
1846 BOOST_FOREACH(CNode* pnode, vNodes)
1848 if(!pnode->fRelayTxes)
1850 LOCK(pnode->cs_filter);
1853 if (pnode->pfilter->IsRelevantAndUpdate(tx, hash))
1854 pnode->PushInventory(inv);
1856 pnode->PushInventory(inv);
1860 void CNode::RecordBytesRecv(uint64_t bytes)
1862 LOCK(cs_totalBytesRecv);
1863 nTotalBytesRecv += bytes;
1866 void CNode::RecordBytesSent(uint64_t bytes)
1868 LOCK(cs_totalBytesSent);
1869 nTotalBytesSent += bytes;
1872 uint64_t CNode::GetTotalBytesRecv()
1874 LOCK(cs_totalBytesRecv);
1875 return nTotalBytesRecv;
1878 uint64_t CNode::GetTotalBytesSent()
1880 LOCK(cs_totalBytesSent);
1881 return nTotalBytesSent;
1884 void CNode::Fuzz(int nChance)
1886 if (!fSuccessfullyConnected) return; // Don't fuzz initial handshake
1887 if (GetRand(nChance) != 0) return; // Fuzz 1 of every nChance messages
1892 // xor a random byte with a random value:
1893 if (!ssSend.empty()) {
1894 CDataStream::size_type pos = GetRand(ssSend.size());
1895 ssSend[pos] ^= (unsigned char)(GetRand(256));
1899 // delete a random byte:
1900 if (!ssSend.empty()) {
1901 CDataStream::size_type pos = GetRand(ssSend.size());
1902 ssSend.erase(ssSend.begin()+pos);
1906 // insert a random byte at a random position
1908 CDataStream::size_type pos = GetRand(ssSend.size());
1909 char ch = (char)GetRand(256);
1910 ssSend.insert(ssSend.begin()+pos, ch);
1914 // Chance of more than one change half the time:
1915 // (more changes exponentially less likely):
1925 pathAddr = GetDataDir() / "peers.dat";
1928 bool CAddrDB::Write(const CAddrMan& addr)
1930 // Generate random temporary filename
1931 unsigned short randv = 0;
1932 RAND_bytes((unsigned char *)&randv, sizeof(randv));
1933 std::string tmpfn = strprintf("peers.dat.%04x", randv);
1935 // serialize addresses, checksum data up to that point, then append csum
1936 CDataStream ssPeers(SER_DISK, CLIENT_VERSION);
1937 ssPeers << FLATDATA(Params().MessageStart());
1939 uint256 hash = Hash(ssPeers.begin(), ssPeers.end());
1942 // open temp output file, and associate with CAutoFile
1943 boost::filesystem::path pathTmp = GetDataDir() / tmpfn;
1944 FILE *file = fopen(pathTmp.string().c_str(), "wb");
1945 CAutoFile fileout = CAutoFile(file, SER_DISK, CLIENT_VERSION);
1947 return error("%s : Failed to open file %s", __func__, pathTmp.string());
1949 // Write and commit header, data
1953 catch (std::exception &e) {
1954 return error("%s : Serialize or I/O error - %s", __func__, e.what());
1956 FileCommit(fileout);
1959 // replace existing peers.dat, if any, with new peers.dat.XXXX
1960 if (!RenameOver(pathTmp, pathAddr))
1961 return error("%s : Rename-into-place failed", __func__);
1966 bool CAddrDB::Read(CAddrMan& addr)
1968 // open input file, and associate with CAutoFile
1969 FILE *file = fopen(pathAddr.string().c_str(), "rb");
1970 CAutoFile filein = CAutoFile(file, SER_DISK, CLIENT_VERSION);
1972 return error("%s : Failed to open file %s", __func__, pathAddr.string());
1974 // use file size to size memory buffer
1975 int fileSize = boost::filesystem::file_size(pathAddr);
1976 int dataSize = fileSize - sizeof(uint256);
1977 // Don't try to resize to a negative number if file is small
1980 vector<unsigned char> vchData;
1981 vchData.resize(dataSize);
1984 // read data and checksum from file
1986 filein.read((char *)&vchData[0], dataSize);
1989 catch (std::exception &e) {
1990 return error("%s : Deserialize or I/O error - %s", __func__, e.what());
1994 CDataStream ssPeers(vchData, SER_DISK, CLIENT_VERSION);
1996 // verify stored checksum matches input data
1997 uint256 hashTmp = Hash(ssPeers.begin(), ssPeers.end());
1998 if (hashIn != hashTmp)
1999 return error("%s : Checksum mismatch, data corrupted", __func__);
2001 unsigned char pchMsgTmp[4];
2003 // de-serialize file header (network specific magic number) and ..
2004 ssPeers >> FLATDATA(pchMsgTmp);
2006 // ... verify the network matches ours
2007 if (memcmp(pchMsgTmp, Params().MessageStart(), sizeof(pchMsgTmp)))
2008 return error("%s : Invalid network magic number", __func__);
2010 // de-serialize address data into one CAddrMan object
2013 catch (std::exception &e) {
2014 return error("%s : Deserialize or I/O error - %s", __func__, e.what());