1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2011 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
9 #include <boost/array.hpp>
10 #include <boost/foreach.hpp>
11 #include <openssl/rand.h>
14 #include <arpa/inet.h>
22 class CRequestTracker;
25 extern int nBestHeight;
26 extern int nConnectTimeout;
30 inline unsigned int ReceiveBufferSize() { return 1000*GetArg("-maxreceivebuffer", 10*1000); }
31 inline unsigned int SendBufferSize() { return 1000*GetArg("-maxsendbuffer", 10*1000); }
32 inline unsigned short GetDefaultPort() { return fTestNet ? 18333 : 8333; }
33 static const unsigned int PUBLISH_HOPS = 5;
36 NODE_NETWORK = (1 << 0),
42 bool ConnectSocket(const CAddress& addrConnect, SOCKET& hSocketRet, int nTimeout=nConnectTimeout);
43 bool Lookup(const char *pszName, std::vector<CAddress>& vaddr, int nServices, int nMaxSolutions, bool fAllowLookup = false, int portDefault = 0, bool fAllowPort = false);
44 bool Lookup(const char *pszName, CAddress& addr, int nServices, bool fAllowLookup = false, int portDefault = 0, bool fAllowPort = false);
45 bool GetMyExternalIP(unsigned int& ipRet);
46 bool AddAddress(CAddress addr, int64 nTimePenalty=0, CAddrDB *pAddrDB=NULL);
47 void AddressCurrentlyConnected(const CAddress& addr);
48 CNode* FindNode(unsigned int ip);
49 CNode* ConnectNode(CAddress addrConnect, int64 nTimeout=0);
50 void AbandonRequests(void (*fn)(void*, CDataStream&), void* param1);
51 bool AnySubscribed(unsigned int nChannel);
52 void MapPort(bool fMapPort);
53 void DNSAddressSeed();
54 bool BindListenPort(std::string& strError=REF(std::string()));
55 void StartNode(void* parg);
58 static const unsigned char pchIPv4[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff };
64 unsigned char pchReserved[12];
68 // disk and network only
72 unsigned int nLastTry;
79 CAddress(unsigned int ipIn, unsigned short portIn=0, uint64 nServicesIn=NODE_NETWORK)
83 port = htons(portIn == 0 ? GetDefaultPort() : portIn);
84 nServices = nServicesIn;
87 explicit CAddress(const struct sockaddr_in& sockaddr, uint64 nServicesIn=NODE_NETWORK)
90 ip = sockaddr.sin_addr.s_addr;
91 port = sockaddr.sin_port;
92 nServices = nServicesIn;
95 explicit CAddress(const char* pszIn, int portIn, bool fNameLookup = false, uint64 nServicesIn=NODE_NETWORK)
98 Lookup(pszIn, *this, nServicesIn, fNameLookup, portIn);
101 explicit CAddress(const char* pszIn, bool fNameLookup = false, uint64 nServicesIn=NODE_NETWORK)
104 Lookup(pszIn, *this, nServicesIn, fNameLookup, 0, true);
107 explicit CAddress(std::string strIn, int portIn, bool fNameLookup = false, uint64 nServicesIn=NODE_NETWORK)
110 Lookup(strIn.c_str(), *this, nServicesIn, fNameLookup, portIn);
113 explicit CAddress(std::string strIn, bool fNameLookup = false, uint64 nServicesIn=NODE_NETWORK)
116 Lookup(strIn.c_str(), *this, nServicesIn, fNameLookup, 0, true);
121 nServices = NODE_NETWORK;
122 memcpy(pchReserved, pchIPv4, sizeof(pchReserved));
124 port = htons(GetDefaultPort());
132 const_cast<CAddress*>(this)->Init();
133 if (nType & SER_DISK)
135 if ((nType & SER_DISK) || (nVersion >= 31402 && !(nType & SER_GETHASH)))
137 READWRITE(nServices);
138 READWRITE(FLATDATA(pchReserved)); // for IPv6
143 friend inline bool operator==(const CAddress& a, const CAddress& b)
145 return (memcmp(a.pchReserved, b.pchReserved, sizeof(a.pchReserved)) == 0 &&
150 friend inline bool operator!=(const CAddress& a, const CAddress& b)
155 friend inline bool operator<(const CAddress& a, const CAddress& b)
157 int ret = memcmp(a.pchReserved, b.pchReserved, sizeof(a.pchReserved));
162 if (ntohl(a.ip) < ntohl(b.ip))
164 else if (a.ip == b.ip)
165 return ntohs(a.port) < ntohs(b.port);
170 std::vector<unsigned char> GetKey() const
174 ss << FLATDATA(pchReserved) << ip << port;
176 #if defined(_MSC_VER) && _MSC_VER < 1300
177 return std::vector<unsigned char>((unsigned char*)&ss.begin()[0], (unsigned char*)&ss.end()[0]);
179 return std::vector<unsigned char>(ss.begin(), ss.end());
183 struct sockaddr_in GetSockAddr() const
185 struct sockaddr_in sockaddr;
186 memset(&sockaddr, 0, sizeof(sockaddr));
187 sockaddr.sin_family = AF_INET;
188 sockaddr.sin_addr.s_addr = ip;
189 sockaddr.sin_port = port;
195 return (memcmp(pchReserved, pchIPv4, sizeof(pchIPv4)) == 0);
198 bool IsRFC1918() const
200 return IsIPv4() && (GetByte(3) == 10 ||
201 (GetByte(3) == 192 && GetByte(2) == 168) ||
202 (GetByte(3) == 172 &&
203 (GetByte(2) >= 16 && GetByte(2) <= 31)));
206 bool IsRFC3927() const
208 return IsIPv4() && (GetByte(3) == 169 && GetByte(2) == 254);
213 return IsIPv4() && (GetByte(3) == 127 ||
217 bool IsRoutable() const
220 !(IsRFC1918() || IsRFC3927() || IsLocal());
225 // Clean up 3-byte shifted addresses caused by garbage in size field
226 // of addr messages from versions before 0.2.9 checksum.
227 // Two consecutive addr messages look like this:
228 // header20 vectorlen3 addr26 addr26 addr26 header20 vectorlen3 addr26 addr26 addr26...
229 // so if the first length field is garbled, it reads the second batch
230 // of addr misaligned by 3 bytes.
231 if (memcmp(pchReserved, pchIPv4+3, sizeof(pchIPv4)-3) == 0)
234 return (ip != 0 && ip != INADDR_NONE && port != htons(USHRT_MAX));
237 unsigned char GetByte(int n) const
239 return ((unsigned char*)&ip)[3-n];
242 std::string ToStringIPPort() const
244 return strprintf("%u.%u.%u.%u:%u", GetByte(3), GetByte(2), GetByte(1), GetByte(0), ntohs(port));
247 std::string ToStringIP() const
249 return strprintf("%u.%u.%u.%u", GetByte(3), GetByte(2), GetByte(1), GetByte(0));
252 std::string ToStringPort() const
254 return strprintf("%u", ntohs(port));
257 std::string ToString() const
259 return strprintf("%u.%u.%u.%u:%u", GetByte(3), GetByte(2), GetByte(1), GetByte(0), ntohs(port));
264 printf("CAddress(%s)\n", ToString().c_str());
280 static const char* ppszTypeName[] =
299 CInv(int typeIn, const uint256& hashIn)
305 CInv(const std::string& strType, const uint256& hashIn)
308 for (i = 1; i < ARRAYLEN(ppszTypeName); i++)
310 if (strType == ppszTypeName[i])
316 if (i == ARRAYLEN(ppszTypeName))
317 throw std::out_of_range(strprintf("CInv::CInv(string, uint256) : unknown type '%s'", strType.c_str()));
327 friend inline bool operator<(const CInv& a, const CInv& b)
329 return (a.type < b.type || (a.type == b.type && a.hash < b.hash));
332 bool IsKnownType() const
334 return (type >= 1 && type < ARRAYLEN(ppszTypeName));
337 const char* GetCommand() const
340 throw std::out_of_range(strprintf("CInv::GetCommand() : type=%d unknown type", type));
341 return ppszTypeName[type];
344 std::string ToString() const
346 return strprintf("%s %s", GetCommand(), hash.ToString().substr(0,20).c_str());
351 printf("CInv(%s)\n", ToString().c_str());
359 class CRequestTracker
362 void (*fn)(void*, CDataStream&);
365 explicit CRequestTracker(void (*fnIn)(void*, CDataStream&)=NULL, void* param1In=NULL)
382 extern bool fAllowDNS;
383 extern uint64 nLocalServices;
384 extern CAddress addrLocalHost;
385 extern uint64 nLocalHostNonce;
386 extern boost::array<int, 10> vnThreadsRunning;
388 extern std::vector<CNode*> vNodes;
389 extern CCriticalSection cs_vNodes;
390 extern std::map<std::vector<unsigned char>, CAddress> mapAddresses;
391 extern CCriticalSection cs_mapAddresses;
392 extern std::map<CInv, CDataStream> mapRelay;
393 extern std::deque<std::pair<int64, CInv> > vRelayExpiration;
394 extern CCriticalSection cs_mapRelay;
395 extern std::map<CInv, int64> mapAlreadyAskedFor;
398 extern int fUseProxy;
399 extern CAddress addrProxy;
414 CCriticalSection cs_vSend;
415 CCriticalSection cs_vRecv;
418 int64 nLastSendEmpty;
419 int64 nTimeConnected;
420 unsigned int nHeaderStart;
421 unsigned int nMessageStart;
424 std::string strSubVer;
428 bool fSuccessfullyConnected;
434 std::map<uint256, CRequestTracker> mapRequests;
435 CCriticalSection cs_mapRequests;
436 uint256 hashContinue;
437 CBlockIndex* pindexLastGetBlocksBegin;
438 uint256 hashLastGetBlocksEnd;
442 std::vector<CAddress> vAddrToSend;
443 std::set<CAddress> setAddrKnown;
445 std::set<uint256> setKnown;
447 // inventory based relay
448 std::set<CInv> setInventoryKnown;
449 std::vector<CInv> vInventoryToSend;
450 CCriticalSection cs_inventory;
451 std::multimap<int64, CInv> mapAskFor;
453 // publish and subscription
454 std::vector<char> vfSubscribe;
457 CNode(SOCKET hSocketIn, CAddress addrIn, bool fInboundIn=false)
461 vSend.SetType(SER_NETWORK);
463 vRecv.SetType(SER_NETWORK);
465 // Version 0.2 obsoletes 20 Feb 2012
466 if (GetTime() > 1329696000)
468 vSend.SetVersion(209);
469 vRecv.SetVersion(209);
473 nLastSendEmpty = GetTime();
474 nTimeConnected = GetTime();
480 fClient = false; // set by version message
481 fInbound = fInboundIn;
482 fNetworkNode = false;
483 fSuccessfullyConnected = false;
488 pindexLastGetBlocksBegin = 0;
489 hashLastGetBlocksEnd = 0;
490 nStartingHeight = -1;
492 vfSubscribe.assign(256, false);
494 // Be shy and don't send version until we hear
501 if (hSocket != INVALID_SOCKET)
503 closesocket(hSocket);
504 hSocket = INVALID_SOCKET;
510 void operator=(const CNode&);
516 return std::max(nRefCount, 0) + (GetTime() < nReleaseTime ? 1 : 0);
519 CNode* AddRef(int64 nTimeout=0)
522 nReleaseTime = std::max(nReleaseTime, GetTime() + nTimeout);
535 void AddAddressKnown(const CAddress& addr)
537 setAddrKnown.insert(addr);
540 void PushAddress(const CAddress& addr)
542 // Known checking here is only to save space from duplicates.
543 // SendMessages will filter it again for knowns that were added
544 // after addresses were pushed.
545 if (addr.IsValid() && !setAddrKnown.count(addr))
546 vAddrToSend.push_back(addr);
550 void AddInventoryKnown(const CInv& inv)
552 CRITICAL_BLOCK(cs_inventory)
553 setInventoryKnown.insert(inv);
556 void PushInventory(const CInv& inv)
558 CRITICAL_BLOCK(cs_inventory)
559 if (!setInventoryKnown.count(inv))
560 vInventoryToSend.push_back(inv);
563 void AskFor(const CInv& inv)
565 // We're using mapAskFor as a priority queue,
566 // the key is the earliest time the request can be sent
567 int64& nRequestTime = mapAlreadyAskedFor[inv];
568 printf("askfor %s %"PRI64d"\n", inv.ToString().c_str(), nRequestTime);
570 // Make sure not to reuse time indexes to keep things in the same order
571 int64 nNow = (GetTime() - 1) * 1000000;
572 static int64 nLastTime;
573 nLastTime = nNow = std::max(nNow, ++nLastTime);
575 // Each retry is 2 minutes after the last
576 nRequestTime = std::max(nRequestTime + 2 * 60 * 1000000, nNow);
577 mapAskFor.insert(std::make_pair(nRequestTime, inv));
582 void BeginMessage(const char* pszCommand)
584 cs_vSend.Enter("cs_vSend", __FILE__, __LINE__);
585 if (nHeaderStart != -1)
587 nHeaderStart = vSend.size();
588 vSend << CMessageHeader(pszCommand, 0);
589 nMessageStart = vSend.size();
591 printf("%s ", DateTimeStrFormat("%x %H:%M:%S", GetTime()).c_str());
592 printf("sending: %s ", pszCommand);
597 if (nHeaderStart == -1)
599 vSend.resize(nHeaderStart);
603 printf("(aborted)\n");
608 if (mapArgs.count("-dropmessagestest") && GetRand(atoi(mapArgs["-dropmessagestest"])) == 0)
610 printf("dropmessages DROPPING SEND MESSAGE\n");
615 if (nHeaderStart == -1)
619 unsigned int nSize = vSend.size() - nMessageStart;
620 memcpy((char*)&vSend[nHeaderStart] + offsetof(CMessageHeader, nMessageSize), &nSize, sizeof(nSize));
623 if (vSend.GetVersion() >= 209)
625 uint256 hash = Hash(vSend.begin() + nMessageStart, vSend.end());
626 unsigned int nChecksum = 0;
627 memcpy(&nChecksum, &hash, sizeof(nChecksum));
628 assert(nMessageStart - nHeaderStart >= offsetof(CMessageHeader, nChecksum) + sizeof(nChecksum));
629 memcpy((char*)&vSend[nHeaderStart] + offsetof(CMessageHeader, nChecksum), &nChecksum, sizeof(nChecksum));
632 printf("(%d bytes) ", nSize);
640 void EndMessageAbortIfEmpty()
642 if (nHeaderStart == -1)
644 int nSize = vSend.size() - nMessageStart;
655 /// when NTP implemented, change to just nTime = GetAdjustedTime()
656 int64 nTime = (fInbound ? GetAdjustedTime() : GetTime());
657 CAddress addrYou = (fUseProxy ? CAddress("0.0.0.0") : addr);
658 CAddress addrMe = (fUseProxy ? CAddress("0.0.0.0") : addrLocalHost);
659 RAND_bytes((unsigned char*)&nLocalHostNonce, sizeof(nLocalHostNonce));
660 PushMessage("version", VERSION, nLocalServices, nTime, addrYou, addrMe,
661 nLocalHostNonce, std::string(pszSubVer), nBestHeight);
667 void PushMessage(const char* pszCommand)
671 BeginMessage(pszCommand);
681 template<typename T1>
682 void PushMessage(const char* pszCommand, const T1& a1)
686 BeginMessage(pszCommand);
697 template<typename T1, typename T2>
698 void PushMessage(const char* pszCommand, const T1& a1, const T2& a2)
702 BeginMessage(pszCommand);
713 template<typename T1, typename T2, typename T3>
714 void PushMessage(const char* pszCommand, const T1& a1, const T2& a2, const T3& a3)
718 BeginMessage(pszCommand);
719 vSend << a1 << a2 << a3;
729 template<typename T1, typename T2, typename T3, typename T4>
730 void PushMessage(const char* pszCommand, const T1& a1, const T2& a2, const T3& a3, const T4& a4)
734 BeginMessage(pszCommand);
735 vSend << a1 << a2 << a3 << a4;
745 template<typename T1, typename T2, typename T3, typename T4, typename T5>
746 void PushMessage(const char* pszCommand, const T1& a1, const T2& a2, const T3& a3, const T4& a4, const T5& a5)
750 BeginMessage(pszCommand);
751 vSend << a1 << a2 << a3 << a4 << a5;
761 template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
762 void PushMessage(const char* pszCommand, const T1& a1, const T2& a2, const T3& a3, const T4& a4, const T5& a5, const T6& a6)
766 BeginMessage(pszCommand);
767 vSend << a1 << a2 << a3 << a4 << a5 << a6;
777 template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7>
778 void PushMessage(const char* pszCommand, const T1& a1, const T2& a2, const T3& a3, const T4& a4, const T5& a5, const T6& a6, const T7& a7)
782 BeginMessage(pszCommand);
783 vSend << a1 << a2 << a3 << a4 << a5 << a6 << a7;
793 template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8>
794 void PushMessage(const char* pszCommand, const T1& a1, const T2& a2, const T3& a3, const T4& a4, const T5& a5, const T6& a6, const T7& a7, const T8& a8)
798 BeginMessage(pszCommand);
799 vSend << a1 << a2 << a3 << a4 << a5 << a6 << a7 << a8;
809 template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9>
810 void PushMessage(const char* pszCommand, const T1& a1, const T2& a2, const T3& a3, const T4& a4, const T5& a5, const T6& a6, const T7& a7, const T8& a8, const T9& a9)
814 BeginMessage(pszCommand);
815 vSend << a1 << a2 << a3 << a4 << a5 << a6 << a7 << a8 << a9;
826 void PushRequest(const char* pszCommand,
827 void (*fn)(void*, CDataStream&), void* param1)
830 RAND_bytes((unsigned char*)&hashReply, sizeof(hashReply));
832 CRITICAL_BLOCK(cs_mapRequests)
833 mapRequests[hashReply] = CRequestTracker(fn, param1);
835 PushMessage(pszCommand, hashReply);
838 template<typename T1>
839 void PushRequest(const char* pszCommand, const T1& a1,
840 void (*fn)(void*, CDataStream&), void* param1)
843 RAND_bytes((unsigned char*)&hashReply, sizeof(hashReply));
845 CRITICAL_BLOCK(cs_mapRequests)
846 mapRequests[hashReply] = CRequestTracker(fn, param1);
848 PushMessage(pszCommand, hashReply, a1);
851 template<typename T1, typename T2>
852 void PushRequest(const char* pszCommand, const T1& a1, const T2& a2,
853 void (*fn)(void*, CDataStream&), void* param1)
856 RAND_bytes((unsigned char*)&hashReply, sizeof(hashReply));
858 CRITICAL_BLOCK(cs_mapRequests)
859 mapRequests[hashReply] = CRequestTracker(fn, param1);
861 PushMessage(pszCommand, hashReply, a1, a2);
866 void PushGetBlocks(CBlockIndex* pindexBegin, uint256 hashEnd);
867 bool IsSubscribed(unsigned int nChannel);
868 void Subscribe(unsigned int nChannel, unsigned int nHops=0);
869 void CancelSubscribe(unsigned int nChannel);
870 void CloseSocketDisconnect();
883 inline void RelayInventory(const CInv& inv)
885 // Put on lists to offer to the other nodes
886 CRITICAL_BLOCK(cs_vNodes)
887 BOOST_FOREACH(CNode* pnode, vNodes)
888 pnode->PushInventory(inv);
892 void RelayMessage(const CInv& inv, const T& a)
894 CDataStream ss(SER_NETWORK);
897 RelayMessage(inv, ss);
901 inline void RelayMessage<>(const CInv& inv, const CDataStream& ss)
903 CRITICAL_BLOCK(cs_mapRelay)
905 // Expire old relay messages
906 while (!vRelayExpiration.empty() && vRelayExpiration.front().first < GetTime())
908 mapRelay.erase(vRelayExpiration.front().second);
909 vRelayExpiration.pop_front();
912 // Save original serialized message so newer versions are preserved
914 vRelayExpiration.push_back(std::make_pair(GetTime() + 15 * 60, inv));
928 // Templates for the publish and subscription system.
929 // The object being published as T& obj needs to have:
930 // a set<unsigned int> setSources member
931 // specializations of AdvertInsert and AdvertErase
932 // Currently implemented for CTable and CProduct.
936 void AdvertStartPublish(CNode* pfrom, unsigned int nChannel, unsigned int nHops, T& obj)
939 obj.setSources.insert(pfrom->addr.ip);
941 if (!AdvertInsert(obj))
945 CRITICAL_BLOCK(cs_vNodes)
946 BOOST_FOREACH(CNode* pnode, vNodes)
947 if (pnode != pfrom && (nHops < PUBLISH_HOPS || pnode->IsSubscribed(nChannel)))
948 pnode->PushMessage("publish", nChannel, nHops, obj);
952 void AdvertStopPublish(CNode* pfrom, unsigned int nChannel, unsigned int nHops, T& obj)
954 uint256 hash = obj.GetHash();
956 CRITICAL_BLOCK(cs_vNodes)
957 BOOST_FOREACH(CNode* pnode, vNodes)
958 if (pnode != pfrom && (nHops < PUBLISH_HOPS || pnode->IsSubscribed(nChannel)))
959 pnode->PushMessage("pub-cancel", nChannel, nHops, hash);
965 void AdvertRemoveSource(CNode* pfrom, unsigned int nChannel, unsigned int nHops, T& obj)
968 obj.setSources.erase(pfrom->addr.ip);
970 // If no longer supported by any sources, cancel it
971 if (obj.setSources.empty())
972 AdvertStopPublish(pfrom, nChannel, nHops, obj);