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 <openssl/rand.h>
13 #include <arpa/inet.h>
20 class CRequestTracker;
23 extern int nBestHeight;
24 extern int nConnectTimeout;
28 inline unsigned int ReceiveBufferSize() { return 1000*GetArg("-maxreceivebuffer", 10*1000); }
29 inline unsigned int SendBufferSize() { return 1000*GetArg("-maxsendbuffer", 10*1000); }
30 inline unsigned short GetDefaultPort() { return fTestNet ? 18333 : 8333; }
31 static const unsigned int PUBLISH_HOPS = 5;
34 NODE_NETWORK = (1 << 0),
40 bool ConnectSocket(const CAddress& addrConnect, SOCKET& hSocketRet, int nTimeout=nConnectTimeout);
41 bool Lookup(const char *pszName, std::vector<CAddress>& vaddr, int nServices, int nMaxSolutions, bool fAllowLookup = false, int portDefault = 0, bool fAllowPort = false);
42 bool Lookup(const char *pszName, CAddress& addr, int nServices, bool fAllowLookup = false, int portDefault = 0, bool fAllowPort = false);
43 bool GetMyExternalIP(unsigned int& ipRet);
44 bool AddAddress(CAddress addr, int64 nTimePenalty=0, CAddrDB *pAddrDB=NULL);
45 void AddressCurrentlyConnected(const CAddress& addr);
46 CNode* FindNode(unsigned int ip);
47 CNode* ConnectNode(CAddress addrConnect, int64 nTimeout=0);
48 void AbandonRequests(void (*fn)(void*, CDataStream&), void* param1);
49 bool AnySubscribed(unsigned int nChannel);
50 void MapPort(bool fMapPort);
51 void DNSAddressSeed();
52 bool BindListenPort(std::string& strError=REF(std::string()));
53 void StartNode(void* parg);
70 extern char pchMessageStart[4];
75 enum { COMMAND_SIZE=12 };
76 char pchMessageStart[sizeof(::pchMessageStart)];
77 char pchCommand[COMMAND_SIZE];
78 unsigned int nMessageSize;
79 unsigned int nChecksum;
83 memcpy(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart));
84 memset(pchCommand, 0, sizeof(pchCommand));
90 CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn)
92 memcpy(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart));
93 strncpy(pchCommand, pszCommand, COMMAND_SIZE);
94 nMessageSize = nMessageSizeIn;
100 READWRITE(FLATDATA(pchMessageStart));
101 READWRITE(FLATDATA(pchCommand));
102 READWRITE(nMessageSize);
104 READWRITE(nChecksum);
107 std::string GetCommand()
109 if (pchCommand[COMMAND_SIZE-1] == 0)
110 return std::string(pchCommand, pchCommand + strlen(pchCommand));
112 return std::string(pchCommand, pchCommand + COMMAND_SIZE);
117 // Check start string
118 if (memcmp(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart)) != 0)
121 // Check the command string for errors
122 for (char* p1 = pchCommand; p1 < pchCommand + COMMAND_SIZE; p1++)
126 // Must be all zeros after the first zero
127 for (; p1 < pchCommand + COMMAND_SIZE; p1++)
131 else if (*p1 < ' ' || *p1 > 0x7E)
136 if (nMessageSize > MAX_SIZE)
138 printf("CMessageHeader::IsValid() : (%s, %u bytes) nMessageSize > MAX_SIZE\n", GetCommand().c_str(), nMessageSize);
151 static const unsigned char pchIPv4[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff };
157 unsigned char pchReserved[12];
161 // disk and network only
165 unsigned int nLastTry;
172 CAddress(unsigned int ipIn, unsigned short portIn=0, uint64 nServicesIn=NODE_NETWORK)
176 port = htons(portIn == 0 ? GetDefaultPort() : portIn);
177 nServices = nServicesIn;
180 explicit CAddress(const struct sockaddr_in& sockaddr, uint64 nServicesIn=NODE_NETWORK)
183 ip = sockaddr.sin_addr.s_addr;
184 port = sockaddr.sin_port;
185 nServices = nServicesIn;
188 explicit CAddress(const char* pszIn, int portIn, bool fNameLookup = false, uint64 nServicesIn=NODE_NETWORK)
191 Lookup(pszIn, *this, nServicesIn, fNameLookup, portIn);
194 explicit CAddress(const char* pszIn, bool fNameLookup = false, uint64 nServicesIn=NODE_NETWORK)
197 Lookup(pszIn, *this, nServicesIn, fNameLookup, 0, true);
200 explicit CAddress(std::string strIn, int portIn, bool fNameLookup = false, uint64 nServicesIn=NODE_NETWORK)
203 Lookup(strIn.c_str(), *this, nServicesIn, fNameLookup, portIn);
206 explicit CAddress(std::string strIn, bool fNameLookup = false, uint64 nServicesIn=NODE_NETWORK)
209 Lookup(strIn.c_str(), *this, nServicesIn, fNameLookup, 0, true);
214 nServices = NODE_NETWORK;
215 memcpy(pchReserved, pchIPv4, sizeof(pchReserved));
217 port = htons(GetDefaultPort());
225 const_cast<CAddress*>(this)->Init();
226 if (nType & SER_DISK)
228 if ((nType & SER_DISK) || (nVersion >= 31402 && !(nType & SER_GETHASH)))
230 READWRITE(nServices);
231 READWRITE(FLATDATA(pchReserved)); // for IPv6
236 friend inline bool operator==(const CAddress& a, const CAddress& b)
238 return (memcmp(a.pchReserved, b.pchReserved, sizeof(a.pchReserved)) == 0 &&
243 friend inline bool operator!=(const CAddress& a, const CAddress& b)
248 friend inline bool operator<(const CAddress& a, const CAddress& b)
250 int ret = memcmp(a.pchReserved, b.pchReserved, sizeof(a.pchReserved));
255 if (ntohl(a.ip) < ntohl(b.ip))
257 else if (a.ip == b.ip)
258 return ntohs(a.port) < ntohs(b.port);
263 std::vector<unsigned char> GetKey() const
267 ss << FLATDATA(pchReserved) << ip << port;
269 #if defined(_MSC_VER) && _MSC_VER < 1300
270 return std::vector<unsigned char>((unsigned char*)&ss.begin()[0], (unsigned char*)&ss.end()[0]);
272 return std::vector<unsigned char>(ss.begin(), ss.end());
276 struct sockaddr_in GetSockAddr() const
278 struct sockaddr_in sockaddr;
279 memset(&sockaddr, 0, sizeof(sockaddr));
280 sockaddr.sin_family = AF_INET;
281 sockaddr.sin_addr.s_addr = ip;
282 sockaddr.sin_port = port;
288 return (memcmp(pchReserved, pchIPv4, sizeof(pchIPv4)) == 0);
291 bool IsRFC1918() const
293 return IsIPv4() && (GetByte(3) == 10 ||
294 (GetByte(3) == 192 && GetByte(2) == 168) ||
295 (GetByte(3) == 172 &&
296 (GetByte(2) >= 16 && GetByte(2) <= 31)));
299 bool IsRFC3927() const
301 return IsIPv4() && (GetByte(3) == 169 && GetByte(2) == 254);
306 return IsIPv4() && (GetByte(3) == 127 ||
310 bool IsRoutable() const
313 !(IsRFC1918() || IsRFC3927() || IsLocal());
318 // Clean up 3-byte shifted addresses caused by garbage in size field
319 // of addr messages from versions before 0.2.9 checksum.
320 // Two consecutive addr messages look like this:
321 // header20 vectorlen3 addr26 addr26 addr26 header20 vectorlen3 addr26 addr26 addr26...
322 // so if the first length field is garbled, it reads the second batch
323 // of addr misaligned by 3 bytes.
324 if (memcmp(pchReserved, pchIPv4+3, sizeof(pchIPv4)-3) == 0)
327 return (ip != 0 && ip != INADDR_NONE && port != htons(USHRT_MAX));
330 unsigned char GetByte(int n) const
332 return ((unsigned char*)&ip)[3-n];
335 std::string ToStringIPPort() const
337 return strprintf("%u.%u.%u.%u:%u", GetByte(3), GetByte(2), GetByte(1), GetByte(0), ntohs(port));
340 std::string ToStringIP() const
342 return strprintf("%u.%u.%u.%u", GetByte(3), GetByte(2), GetByte(1), GetByte(0));
345 std::string ToStringPort() const
347 return strprintf("%u", ntohs(port));
350 std::string ToString() const
352 return strprintf("%u.%u.%u.%u:%u", GetByte(3), GetByte(2), GetByte(1), GetByte(0), ntohs(port));
357 printf("CAddress(%s)\n", ToString().c_str());
373 static const char* ppszTypeName[] =
392 CInv(int typeIn, const uint256& hashIn)
398 CInv(const std::string& strType, const uint256& hashIn)
401 for (i = 1; i < ARRAYLEN(ppszTypeName); i++)
403 if (strType == ppszTypeName[i])
409 if (i == ARRAYLEN(ppszTypeName))
410 throw std::out_of_range(strprintf("CInv::CInv(string, uint256) : unknown type '%s'", strType.c_str()));
420 friend inline bool operator<(const CInv& a, const CInv& b)
422 return (a.type < b.type || (a.type == b.type && a.hash < b.hash));
425 bool IsKnownType() const
427 return (type >= 1 && type < ARRAYLEN(ppszTypeName));
430 const char* GetCommand() const
433 throw std::out_of_range(strprintf("CInv::GetCommand() : type=%d unknown type", type));
434 return ppszTypeName[type];
437 std::string ToString() const
439 return strprintf("%s %s", GetCommand(), hash.ToString().substr(0,20).c_str());
444 printf("CInv(%s)\n", ToString().c_str());
452 class CRequestTracker
455 void (*fn)(void*, CDataStream&);
458 explicit CRequestTracker(void (*fnIn)(void*, CDataStream&)=NULL, void* param1In=NULL)
475 extern bool fAllowDNS;
476 extern uint64 nLocalServices;
477 extern CAddress addrLocalHost;
478 extern CNode* pnodeLocalHost;
479 extern uint64 nLocalHostNonce;
480 extern boost::array<int, 10> vnThreadsRunning;
481 extern SOCKET hListenSocket;
483 extern std::vector<CNode*> vNodes;
484 extern CCriticalSection cs_vNodes;
485 extern std::map<std::vector<unsigned char>, CAddress> mapAddresses;
486 extern CCriticalSection cs_mapAddresses;
487 extern std::map<CInv, CDataStream> mapRelay;
488 extern std::deque<std::pair<int64, CInv> > vRelayExpiration;
489 extern CCriticalSection cs_mapRelay;
490 extern std::map<CInv, int64> mapAlreadyAskedFor;
493 extern int fUseProxy;
494 extern CAddress addrProxy;
509 CCriticalSection cs_vSend;
510 CCriticalSection cs_vRecv;
513 int64 nLastSendEmpty;
514 int64 nTimeConnected;
515 unsigned int nHeaderStart;
516 unsigned int nMessageStart;
519 std::string strSubVer;
523 bool fSuccessfullyConnected;
529 std::map<uint256, CRequestTracker> mapRequests;
530 CCriticalSection cs_mapRequests;
531 uint256 hashContinue;
532 CBlockIndex* pindexLastGetBlocksBegin;
533 uint256 hashLastGetBlocksEnd;
537 std::vector<CAddress> vAddrToSend;
538 std::set<CAddress> setAddrKnown;
540 std::set<uint256> setKnown;
542 // inventory based relay
543 std::set<CInv> setInventoryKnown;
544 std::vector<CInv> vInventoryToSend;
545 CCriticalSection cs_inventory;
546 std::multimap<int64, CInv> mapAskFor;
548 // publish and subscription
549 std::vector<char> vfSubscribe;
552 CNode(SOCKET hSocketIn, CAddress addrIn, bool fInboundIn=false)
556 vSend.SetType(SER_NETWORK);
558 vRecv.SetType(SER_NETWORK);
560 // Version 0.2 obsoletes 20 Feb 2012
561 if (GetTime() > 1329696000)
563 vSend.SetVersion(209);
564 vRecv.SetVersion(209);
568 nLastSendEmpty = GetTime();
569 nTimeConnected = GetTime();
575 fClient = false; // set by version message
576 fInbound = fInboundIn;
577 fNetworkNode = false;
578 fSuccessfullyConnected = false;
583 pindexLastGetBlocksBegin = 0;
584 hashLastGetBlocksEnd = 0;
585 nStartingHeight = -1;
587 vfSubscribe.assign(256, false);
589 // Be shy and don't send version until we hear
596 if (hSocket != INVALID_SOCKET)
598 closesocket(hSocket);
599 hSocket = INVALID_SOCKET;
605 void operator=(const CNode&);
611 return std::max(nRefCount, 0) + (GetTime() < nReleaseTime ? 1 : 0);
614 CNode* AddRef(int64 nTimeout=0)
617 nReleaseTime = std::max(nReleaseTime, GetTime() + nTimeout);
630 void AddAddressKnown(const CAddress& addr)
632 setAddrKnown.insert(addr);
635 void PushAddress(const CAddress& addr)
637 // Known checking here is only to save space from duplicates.
638 // SendMessages will filter it again for knowns that were added
639 // after addresses were pushed.
640 if (addr.IsValid() && !setAddrKnown.count(addr))
641 vAddrToSend.push_back(addr);
645 void AddInventoryKnown(const CInv& inv)
647 CRITICAL_BLOCK(cs_inventory)
648 setInventoryKnown.insert(inv);
651 void PushInventory(const CInv& inv)
653 CRITICAL_BLOCK(cs_inventory)
654 if (!setInventoryKnown.count(inv))
655 vInventoryToSend.push_back(inv);
658 void AskFor(const CInv& inv)
660 // We're using mapAskFor as a priority queue,
661 // the key is the earliest time the request can be sent
662 int64& nRequestTime = mapAlreadyAskedFor[inv];
663 printf("askfor %s %"PRI64d"\n", inv.ToString().c_str(), nRequestTime);
665 // Make sure not to reuse time indexes to keep things in the same order
666 int64 nNow = (GetTime() - 1) * 1000000;
667 static int64 nLastTime;
668 nLastTime = nNow = std::max(nNow, ++nLastTime);
670 // Each retry is 2 minutes after the last
671 nRequestTime = std::max(nRequestTime + 2 * 60 * 1000000, nNow);
672 mapAskFor.insert(std::make_pair(nRequestTime, inv));
677 void BeginMessage(const char* pszCommand)
680 if (nHeaderStart != -1)
682 nHeaderStart = vSend.size();
683 vSend << CMessageHeader(pszCommand, 0);
684 nMessageStart = vSend.size();
686 printf("%s ", DateTimeStrFormat("%x %H:%M:%S", GetTime()).c_str());
687 printf("sending: %s ", pszCommand);
692 if (nHeaderStart == -1)
694 vSend.resize(nHeaderStart);
698 printf("(aborted)\n");
703 if (mapArgs.count("-dropmessagestest") && GetRand(atoi(mapArgs["-dropmessagestest"])) == 0)
705 printf("dropmessages DROPPING SEND MESSAGE\n");
710 if (nHeaderStart == -1)
714 unsigned int nSize = vSend.size() - nMessageStart;
715 memcpy((char*)&vSend[nHeaderStart] + offsetof(CMessageHeader, nMessageSize), &nSize, sizeof(nSize));
718 if (vSend.GetVersion() >= 209)
720 uint256 hash = Hash(vSend.begin() + nMessageStart, vSend.end());
721 unsigned int nChecksum = 0;
722 memcpy(&nChecksum, &hash, sizeof(nChecksum));
723 assert(nMessageStart - nHeaderStart >= offsetof(CMessageHeader, nChecksum) + sizeof(nChecksum));
724 memcpy((char*)&vSend[nHeaderStart] + offsetof(CMessageHeader, nChecksum), &nChecksum, sizeof(nChecksum));
727 printf("(%d bytes) ", nSize);
735 void EndMessageAbortIfEmpty()
737 if (nHeaderStart == -1)
739 int nSize = vSend.size() - nMessageStart;
750 /// when NTP implemented, change to just nTime = GetAdjustedTime()
751 int64 nTime = (fInbound ? GetAdjustedTime() : GetTime());
752 CAddress addrYou = (fUseProxy ? CAddress("0.0.0.0") : addr);
753 CAddress addrMe = (fUseProxy ? CAddress("0.0.0.0") : addrLocalHost);
754 RAND_bytes((unsigned char*)&nLocalHostNonce, sizeof(nLocalHostNonce));
755 PushMessage("version", VERSION, nLocalServices, nTime, addrYou, addrMe,
756 nLocalHostNonce, std::string(pszSubVer), nBestHeight);
762 void PushMessage(const char* pszCommand)
766 BeginMessage(pszCommand);
776 template<typename T1>
777 void PushMessage(const char* pszCommand, const T1& a1)
781 BeginMessage(pszCommand);
792 template<typename T1, typename T2>
793 void PushMessage(const char* pszCommand, const T1& a1, const T2& a2)
797 BeginMessage(pszCommand);
808 template<typename T1, typename T2, typename T3>
809 void PushMessage(const char* pszCommand, const T1& a1, const T2& a2, const T3& a3)
813 BeginMessage(pszCommand);
814 vSend << a1 << a2 << a3;
824 template<typename T1, typename T2, typename T3, typename T4>
825 void PushMessage(const char* pszCommand, const T1& a1, const T2& a2, const T3& a3, const T4& a4)
829 BeginMessage(pszCommand);
830 vSend << a1 << a2 << a3 << a4;
840 template<typename T1, typename T2, typename T3, typename T4, typename T5>
841 void PushMessage(const char* pszCommand, const T1& a1, const T2& a2, const T3& a3, const T4& a4, const T5& a5)
845 BeginMessage(pszCommand);
846 vSend << a1 << a2 << a3 << a4 << a5;
856 template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
857 void PushMessage(const char* pszCommand, const T1& a1, const T2& a2, const T3& a3, const T4& a4, const T5& a5, const T6& a6)
861 BeginMessage(pszCommand);
862 vSend << a1 << a2 << a3 << a4 << a5 << a6;
872 template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7>
873 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)
877 BeginMessage(pszCommand);
878 vSend << a1 << a2 << a3 << a4 << a5 << a6 << a7;
888 template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8>
889 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)
893 BeginMessage(pszCommand);
894 vSend << a1 << a2 << a3 << a4 << a5 << a6 << a7 << a8;
904 template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9>
905 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)
909 BeginMessage(pszCommand);
910 vSend << a1 << a2 << a3 << a4 << a5 << a6 << a7 << a8 << a9;
921 void PushRequest(const char* pszCommand,
922 void (*fn)(void*, CDataStream&), void* param1)
925 RAND_bytes((unsigned char*)&hashReply, sizeof(hashReply));
927 CRITICAL_BLOCK(cs_mapRequests)
928 mapRequests[hashReply] = CRequestTracker(fn, param1);
930 PushMessage(pszCommand, hashReply);
933 template<typename T1>
934 void PushRequest(const char* pszCommand, const T1& a1,
935 void (*fn)(void*, CDataStream&), void* param1)
938 RAND_bytes((unsigned char*)&hashReply, sizeof(hashReply));
940 CRITICAL_BLOCK(cs_mapRequests)
941 mapRequests[hashReply] = CRequestTracker(fn, param1);
943 PushMessage(pszCommand, hashReply, a1);
946 template<typename T1, typename T2>
947 void PushRequest(const char* pszCommand, const T1& a1, const T2& a2,
948 void (*fn)(void*, CDataStream&), void* param1)
951 RAND_bytes((unsigned char*)&hashReply, sizeof(hashReply));
953 CRITICAL_BLOCK(cs_mapRequests)
954 mapRequests[hashReply] = CRequestTracker(fn, param1);
956 PushMessage(pszCommand, hashReply, a1, a2);
961 void PushGetBlocks(CBlockIndex* pindexBegin, uint256 hashEnd);
962 bool IsSubscribed(unsigned int nChannel);
963 void Subscribe(unsigned int nChannel, unsigned int nHops=0);
964 void CancelSubscribe(unsigned int nChannel);
965 void CloseSocketDisconnect();
978 inline void RelayInventory(const CInv& inv)
980 // Put on lists to offer to the other nodes
981 CRITICAL_BLOCK(cs_vNodes)
982 BOOST_FOREACH(CNode* pnode, vNodes)
983 pnode->PushInventory(inv);
987 void RelayMessage(const CInv& inv, const T& a)
989 CDataStream ss(SER_NETWORK);
992 RelayMessage(inv, ss);
996 inline void RelayMessage<>(const CInv& inv, const CDataStream& ss)
998 CRITICAL_BLOCK(cs_mapRelay)
1000 // Expire old relay messages
1001 while (!vRelayExpiration.empty() && vRelayExpiration.front().first < GetTime())
1003 mapRelay.erase(vRelayExpiration.front().second);
1004 vRelayExpiration.pop_front();
1007 // Save original serialized message so newer versions are preserved
1009 vRelayExpiration.push_back(std::make_pair(GetTime() + 15 * 60, inv));
1012 RelayInventory(inv);
1023 // Templates for the publish and subscription system.
1024 // The object being published as T& obj needs to have:
1025 // a set<unsigned int> setSources member
1026 // specializations of AdvertInsert and AdvertErase
1027 // Currently implemented for CTable and CProduct.
1030 template<typename T>
1031 void AdvertStartPublish(CNode* pfrom, unsigned int nChannel, unsigned int nHops, T& obj)
1034 obj.setSources.insert(pfrom->addr.ip);
1036 if (!AdvertInsert(obj))
1040 CRITICAL_BLOCK(cs_vNodes)
1041 BOOST_FOREACH(CNode* pnode, vNodes)
1042 if (pnode != pfrom && (nHops < PUBLISH_HOPS || pnode->IsSubscribed(nChannel)))
1043 pnode->PushMessage("publish", nChannel, nHops, obj);
1046 template<typename T>
1047 void AdvertStopPublish(CNode* pfrom, unsigned int nChannel, unsigned int nHops, T& obj)
1049 uint256 hash = obj.GetHash();
1051 CRITICAL_BLOCK(cs_vNodes)
1052 BOOST_FOREACH(CNode* pnode, vNodes)
1053 if (pnode != pfrom && (nHops < PUBLISH_HOPS || pnode->IsSubscribed(nChannel)))
1054 pnode->PushMessage("pub-cancel", nChannel, nHops, hash);
1059 template<typename T>
1060 void AdvertRemoveSource(CNode* pfrom, unsigned int nChannel, unsigned int nHops, T& obj)
1063 obj.setSources.erase(pfrom->addr.ip);
1065 // If no longer supported by any sources, cancel it
1066 if (obj.setSources.empty())
1067 AdvertStopPublish(pfrom, nChannel, nHops, obj);