1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
8 #include <boost/array.hpp>
9 #include <openssl/rand.h>
12 #include <arpa/inet.h>
18 class CRequestTracker;
21 extern int nBestHeight;
22 extern int nConnectTimeout;
26 inline unsigned short GetDefaultPort() { return fTestNet ? 18333 : 8333; }
27 static const unsigned int PUBLISH_HOPS = 5;
30 NODE_NETWORK = (1 << 0),
36 bool ConnectSocket(const CAddress& addrConnect, SOCKET& hSocketRet, int nTimeout=nConnectTimeout);
37 bool Lookup(const char *pszName, std::vector<CAddress>& vaddr, int nServices, int nMaxSolutions, bool fAllowLookup = false, int portDefault = 0, bool fAllowPort = false);
38 bool Lookup(const char *pszName, CAddress& addr, int nServices, bool fAllowLookup = false, int portDefault = 0, bool fAllowPort = false);
39 bool GetMyExternalIP(unsigned int& ipRet);
40 bool AddAddress(CAddress addr, int64 nTimePenalty=0);
41 void AddressCurrentlyConnected(const CAddress& addr);
42 CNode* FindNode(unsigned int ip);
43 CNode* ConnectNode(CAddress addrConnect, int64 nTimeout=0);
44 void AbandonRequests(void (*fn)(void*, CDataStream&), void* param1);
45 bool AnySubscribed(unsigned int nChannel);
46 void MapPort(bool fMapPort);
47 void DNSAddressSeed();
48 bool BindListenPort(std::string& strError=REF(std::string()));
49 void StartNode(void* parg);
66 extern char pchMessageStart[4];
71 enum { COMMAND_SIZE=12 };
72 char pchMessageStart[sizeof(::pchMessageStart)];
73 char pchCommand[COMMAND_SIZE];
74 unsigned int nMessageSize;
75 unsigned int nChecksum;
79 memcpy(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart));
80 memset(pchCommand, 0, sizeof(pchCommand));
86 CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn)
88 memcpy(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart));
89 strncpy(pchCommand, pszCommand, COMMAND_SIZE);
90 nMessageSize = nMessageSizeIn;
96 READWRITE(FLATDATA(pchMessageStart));
97 READWRITE(FLATDATA(pchCommand));
98 READWRITE(nMessageSize);
100 READWRITE(nChecksum);
103 std::string GetCommand()
105 if (pchCommand[COMMAND_SIZE-1] == 0)
106 return std::string(pchCommand, pchCommand + strlen(pchCommand));
108 return std::string(pchCommand, pchCommand + COMMAND_SIZE);
113 // Check start string
114 if (memcmp(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart)) != 0)
117 // Check the command string for errors
118 for (char* p1 = pchCommand; p1 < pchCommand + COMMAND_SIZE; p1++)
122 // Must be all zeros after the first zero
123 for (; p1 < pchCommand + COMMAND_SIZE; p1++)
127 else if (*p1 < ' ' || *p1 > 0x7E)
132 if (nMessageSize > MAX_SIZE)
134 printf("CMessageHeader::IsValid() : (%s, %u bytes) nMessageSize > MAX_SIZE\n", GetCommand().c_str(), nMessageSize);
147 static const unsigned char pchIPv4[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff };
153 unsigned char pchReserved[12];
157 // disk and network only
161 unsigned int nLastTry;
168 CAddress(unsigned int ipIn, unsigned short portIn=0, uint64 nServicesIn=NODE_NETWORK)
172 port = htons(portIn == 0 ? GetDefaultPort() : portIn);
173 nServices = nServicesIn;
176 explicit CAddress(const struct sockaddr_in& sockaddr, uint64 nServicesIn=NODE_NETWORK)
179 ip = sockaddr.sin_addr.s_addr;
180 port = sockaddr.sin_port;
181 nServices = nServicesIn;
184 explicit CAddress(const char* pszIn, int portIn, bool fNameLookup = false, uint64 nServicesIn=NODE_NETWORK)
187 Lookup(pszIn, *this, nServicesIn, fNameLookup, portIn);
190 explicit CAddress(const char* pszIn, bool fNameLookup = false, uint64 nServicesIn=NODE_NETWORK)
193 Lookup(pszIn, *this, nServicesIn, fNameLookup, 0, true);
196 explicit CAddress(std::string strIn, int portIn, bool fNameLookup = false, uint64 nServicesIn=NODE_NETWORK)
199 Lookup(strIn.c_str(), *this, nServicesIn, fNameLookup, portIn);
202 explicit CAddress(std::string strIn, bool fNameLookup = false, uint64 nServicesIn=NODE_NETWORK)
205 Lookup(strIn.c_str(), *this, nServicesIn, fNameLookup, 0, true);
210 nServices = NODE_NETWORK;
211 memcpy(pchReserved, pchIPv4, sizeof(pchReserved));
213 port = htons(GetDefaultPort());
221 const_cast<CAddress*>(this)->Init();
222 if (nType & SER_DISK)
224 if ((nType & SER_DISK) || (nVersion >= 31402 && !(nType & SER_GETHASH)))
226 READWRITE(nServices);
227 READWRITE(FLATDATA(pchReserved)); // for IPv6
232 friend inline bool operator==(const CAddress& a, const CAddress& b)
234 return (memcmp(a.pchReserved, b.pchReserved, sizeof(a.pchReserved)) == 0 &&
239 friend inline bool operator!=(const CAddress& a, const CAddress& b)
244 friend inline bool operator<(const CAddress& a, const CAddress& b)
246 int ret = memcmp(a.pchReserved, b.pchReserved, sizeof(a.pchReserved));
251 if (ntohl(a.ip) < ntohl(b.ip))
253 else if (a.ip == b.ip)
254 return ntohs(a.port) < ntohs(b.port);
259 std::vector<unsigned char> GetKey() const
263 ss << FLATDATA(pchReserved) << ip << port;
265 #if defined(_MSC_VER) && _MSC_VER < 1300
266 return std::vector<unsigned char>((unsigned char*)&ss.begin()[0], (unsigned char*)&ss.end()[0]);
268 return std::vector<unsigned char>(ss.begin(), ss.end());
272 struct sockaddr_in GetSockAddr() const
274 struct sockaddr_in sockaddr;
275 memset(&sockaddr, 0, sizeof(sockaddr));
276 sockaddr.sin_family = AF_INET;
277 sockaddr.sin_addr.s_addr = ip;
278 sockaddr.sin_port = port;
284 return (memcmp(pchReserved, pchIPv4, sizeof(pchIPv4)) == 0);
287 bool IsRFC1918() const
289 return IsIPv4() && (GetByte(3) == 10 ||
290 (GetByte(3) == 192 && GetByte(2) == 168) ||
291 (GetByte(3) == 172 &&
292 (GetByte(2) >= 16 && GetByte(2) <= 31)));
295 bool IsRFC3927() const
297 return IsIPv4() && (GetByte(3) == 169 && GetByte(2) == 254);
302 return IsIPv4() && (GetByte(3) == 127 ||
306 bool IsRoutable() const
309 !(IsRFC1918() || IsRFC3927() || IsLocal());
314 // Clean up 3-byte shifted addresses caused by garbage in size field
315 // of addr messages from versions before 0.2.9 checksum.
316 // Two consecutive addr messages look like this:
317 // header20 vectorlen3 addr26 addr26 addr26 header20 vectorlen3 addr26 addr26 addr26...
318 // so if the first length field is garbled, it reads the second batch
319 // of addr misaligned by 3 bytes.
320 if (memcmp(pchReserved, pchIPv4+3, sizeof(pchIPv4)-3) == 0)
323 return (ip != 0 && ip != INADDR_NONE && port != htons(USHRT_MAX));
326 unsigned char GetByte(int n) const
328 return ((unsigned char*)&ip)[3-n];
331 std::string ToStringIPPort() const
333 return strprintf("%u.%u.%u.%u:%u", GetByte(3), GetByte(2), GetByte(1), GetByte(0), ntohs(port));
336 std::string ToStringIP() const
338 return strprintf("%u.%u.%u.%u", GetByte(3), GetByte(2), GetByte(1), GetByte(0));
341 std::string ToStringPort() const
343 return strprintf("%u", ntohs(port));
346 std::string ToString() const
348 return strprintf("%u.%u.%u.%u:%u", GetByte(3), GetByte(2), GetByte(1), GetByte(0), ntohs(port));
353 printf("CAddress(%s)\n", ToString().c_str());
369 static const char* ppszTypeName[] =
388 CInv(int typeIn, const uint256& hashIn)
394 CInv(const std::string& strType, const uint256& hashIn)
397 for (i = 1; i < ARRAYLEN(ppszTypeName); i++)
399 if (strType == ppszTypeName[i])
405 if (i == ARRAYLEN(ppszTypeName))
406 throw std::out_of_range(strprintf("CInv::CInv(string, uint256) : unknown type '%s'", strType.c_str()));
416 friend inline bool operator<(const CInv& a, const CInv& b)
418 return (a.type < b.type || (a.type == b.type && a.hash < b.hash));
421 bool IsKnownType() const
423 return (type >= 1 && type < ARRAYLEN(ppszTypeName));
426 const char* GetCommand() const
429 throw std::out_of_range(strprintf("CInv::GetCommand() : type=%d unknown type", type));
430 return ppszTypeName[type];
433 std::string ToString() const
435 return strprintf("%s %s", GetCommand(), hash.ToString().substr(0,20).c_str());
440 printf("CInv(%s)\n", ToString().c_str());
448 class CRequestTracker
451 void (*fn)(void*, CDataStream&);
454 explicit CRequestTracker(void (*fnIn)(void*, CDataStream&)=NULL, void* param1In=NULL)
471 extern bool fAllowDNS;
472 extern uint64 nLocalServices;
473 extern CAddress addrLocalHost;
474 extern CNode* pnodeLocalHost;
475 extern uint64 nLocalHostNonce;
476 extern boost::array<int, 10> vnThreadsRunning;
477 extern SOCKET hListenSocket;
479 extern std::vector<CNode*> vNodes;
480 extern CCriticalSection cs_vNodes;
481 extern std::map<std::vector<unsigned char>, CAddress> mapAddresses;
482 extern CCriticalSection cs_mapAddresses;
483 extern std::map<CInv, CDataStream> mapRelay;
484 extern std::deque<std::pair<int64, CInv> > vRelayExpiration;
485 extern CCriticalSection cs_mapRelay;
486 extern std::map<CInv, int64> mapAlreadyAskedFor;
489 extern int fUseProxy;
490 extern CAddress addrProxy;
505 CCriticalSection cs_vSend;
506 CCriticalSection cs_vRecv;
509 int64 nLastSendEmpty;
510 int64 nTimeConnected;
511 unsigned int nHeaderStart;
512 unsigned int nMessageStart;
515 std::string strSubVer;
519 bool fSuccessfullyConnected;
525 std::map<uint256, CRequestTracker> mapRequests;
526 CCriticalSection cs_mapRequests;
527 uint256 hashContinue;
528 CBlockIndex* pindexLastGetBlocksBegin;
529 uint256 hashLastGetBlocksEnd;
533 std::vector<CAddress> vAddrToSend;
534 std::set<CAddress> setAddrKnown;
536 std::set<uint256> setKnown;
538 // inventory based relay
539 std::set<CInv> setInventoryKnown;
540 std::vector<CInv> vInventoryToSend;
541 CCriticalSection cs_inventory;
542 std::multimap<int64, CInv> mapAskFor;
544 // publish and subscription
545 std::vector<char> vfSubscribe;
548 CNode(SOCKET hSocketIn, CAddress addrIn, bool fInboundIn=false)
552 vSend.SetType(SER_NETWORK);
554 vRecv.SetType(SER_NETWORK);
556 // Version 0.2 obsoletes 20 Feb 2012
557 if (GetTime() > 1329696000)
559 vSend.SetVersion(209);
560 vRecv.SetVersion(209);
564 nLastSendEmpty = GetTime();
565 nTimeConnected = GetTime();
571 fClient = false; // set by version message
572 fInbound = fInboundIn;
573 fNetworkNode = false;
574 fSuccessfullyConnected = false;
579 pindexLastGetBlocksBegin = 0;
580 hashLastGetBlocksEnd = 0;
581 nStartingHeight = -1;
583 vfSubscribe.assign(256, false);
585 // Be shy and don't send version until we hear
592 if (hSocket != INVALID_SOCKET)
594 closesocket(hSocket);
595 hSocket = INVALID_SOCKET;
601 void operator=(const CNode&);
607 return std::max(nRefCount, 0) + (GetTime() < nReleaseTime ? 1 : 0);
610 CNode* AddRef(int64 nTimeout=0)
613 nReleaseTime = std::max(nReleaseTime, GetTime() + nTimeout);
626 void AddAddressKnown(const CAddress& addr)
628 setAddrKnown.insert(addr);
631 void PushAddress(const CAddress& addr)
633 // Known checking here is only to save space from duplicates.
634 // SendMessages will filter it again for knowns that were added
635 // after addresses were pushed.
636 if (addr.IsValid() && !setAddrKnown.count(addr))
637 vAddrToSend.push_back(addr);
641 void AddInventoryKnown(const CInv& inv)
643 CRITICAL_BLOCK(cs_inventory)
644 setInventoryKnown.insert(inv);
647 void PushInventory(const CInv& inv)
649 CRITICAL_BLOCK(cs_inventory)
650 if (!setInventoryKnown.count(inv))
651 vInventoryToSend.push_back(inv);
654 void AskFor(const CInv& inv)
656 // We're using mapAskFor as a priority queue,
657 // the key is the earliest time the request can be sent
658 int64& nRequestTime = mapAlreadyAskedFor[inv];
659 printf("askfor %s %"PRI64d"\n", inv.ToString().c_str(), nRequestTime);
661 // Make sure not to reuse time indexes to keep things in the same order
662 int64 nNow = (GetTime() - 1) * 1000000;
663 static int64 nLastTime;
664 nLastTime = nNow = std::max(nNow, ++nLastTime);
666 // Each retry is 2 minutes after the last
667 nRequestTime = std::max(nRequestTime + 2 * 60 * 1000000, nNow);
668 mapAskFor.insert(std::make_pair(nRequestTime, inv));
673 void BeginMessage(const char* pszCommand)
676 if (nHeaderStart != -1)
678 nHeaderStart = vSend.size();
679 vSend << CMessageHeader(pszCommand, 0);
680 nMessageStart = vSend.size();
682 printf("%s ", DateTimeStrFormat("%x %H:%M:%S", GetTime()).c_str());
683 printf("sending: %s ", pszCommand);
688 if (nHeaderStart == -1)
690 vSend.resize(nHeaderStart);
694 printf("(aborted)\n");
699 if (mapArgs.count("-dropmessagestest") && GetRand(atoi(mapArgs["-dropmessagestest"])) == 0)
701 printf("dropmessages DROPPING SEND MESSAGE\n");
706 if (nHeaderStart == -1)
710 unsigned int nSize = vSend.size() - nMessageStart;
711 memcpy((char*)&vSend[nHeaderStart] + offsetof(CMessageHeader, nMessageSize), &nSize, sizeof(nSize));
714 if (vSend.GetVersion() >= 209)
716 uint256 hash = Hash(vSend.begin() + nMessageStart, vSend.end());
717 unsigned int nChecksum = 0;
718 memcpy(&nChecksum, &hash, sizeof(nChecksum));
719 assert(nMessageStart - nHeaderStart >= offsetof(CMessageHeader, nChecksum) + sizeof(nChecksum));
720 memcpy((char*)&vSend[nHeaderStart] + offsetof(CMessageHeader, nChecksum), &nChecksum, sizeof(nChecksum));
723 printf("(%d bytes) ", nSize);
731 void EndMessageAbortIfEmpty()
733 if (nHeaderStart == -1)
735 int nSize = vSend.size() - nMessageStart;
746 /// when NTP implemented, change to just nTime = GetAdjustedTime()
747 int64 nTime = (fInbound ? GetAdjustedTime() : GetTime());
748 CAddress addrYou = (fUseProxy ? CAddress("0.0.0.0") : addr);
749 CAddress addrMe = (fUseProxy ? CAddress("0.0.0.0") : addrLocalHost);
750 RAND_bytes((unsigned char*)&nLocalHostNonce, sizeof(nLocalHostNonce));
751 PushMessage("version", VERSION, nLocalServices, nTime, addrYou, addrMe,
752 nLocalHostNonce, std::string(pszSubVer), nBestHeight);
758 void PushMessage(const char* pszCommand)
762 BeginMessage(pszCommand);
772 template<typename T1>
773 void PushMessage(const char* pszCommand, const T1& a1)
777 BeginMessage(pszCommand);
788 template<typename T1, typename T2>
789 void PushMessage(const char* pszCommand, const T1& a1, const T2& a2)
793 BeginMessage(pszCommand);
804 template<typename T1, typename T2, typename T3>
805 void PushMessage(const char* pszCommand, const T1& a1, const T2& a2, const T3& a3)
809 BeginMessage(pszCommand);
810 vSend << a1 << a2 << a3;
820 template<typename T1, typename T2, typename T3, typename T4>
821 void PushMessage(const char* pszCommand, const T1& a1, const T2& a2, const T3& a3, const T4& a4)
825 BeginMessage(pszCommand);
826 vSend << a1 << a2 << a3 << a4;
836 template<typename T1, typename T2, typename T3, typename T4, typename T5>
837 void PushMessage(const char* pszCommand, const T1& a1, const T2& a2, const T3& a3, const T4& a4, const T5& a5)
841 BeginMessage(pszCommand);
842 vSend << a1 << a2 << a3 << a4 << a5;
852 template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
853 void PushMessage(const char* pszCommand, const T1& a1, const T2& a2, const T3& a3, const T4& a4, const T5& a5, const T6& a6)
857 BeginMessage(pszCommand);
858 vSend << a1 << a2 << a3 << a4 << a5 << a6;
868 template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7>
869 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)
873 BeginMessage(pszCommand);
874 vSend << a1 << a2 << a3 << a4 << a5 << a6 << a7;
884 template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8>
885 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)
889 BeginMessage(pszCommand);
890 vSend << a1 << a2 << a3 << a4 << a5 << a6 << a7 << a8;
900 template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9>
901 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)
905 BeginMessage(pszCommand);
906 vSend << a1 << a2 << a3 << a4 << a5 << a6 << a7 << a8 << a9;
917 void PushRequest(const char* pszCommand,
918 void (*fn)(void*, CDataStream&), void* param1)
921 RAND_bytes((unsigned char*)&hashReply, sizeof(hashReply));
923 CRITICAL_BLOCK(cs_mapRequests)
924 mapRequests[hashReply] = CRequestTracker(fn, param1);
926 PushMessage(pszCommand, hashReply);
929 template<typename T1>
930 void PushRequest(const char* pszCommand, const T1& a1,
931 void (*fn)(void*, CDataStream&), void* param1)
934 RAND_bytes((unsigned char*)&hashReply, sizeof(hashReply));
936 CRITICAL_BLOCK(cs_mapRequests)
937 mapRequests[hashReply] = CRequestTracker(fn, param1);
939 PushMessage(pszCommand, hashReply, a1);
942 template<typename T1, typename T2>
943 void PushRequest(const char* pszCommand, const T1& a1, const T2& a2,
944 void (*fn)(void*, CDataStream&), void* param1)
947 RAND_bytes((unsigned char*)&hashReply, sizeof(hashReply));
949 CRITICAL_BLOCK(cs_mapRequests)
950 mapRequests[hashReply] = CRequestTracker(fn, param1);
952 PushMessage(pszCommand, hashReply, a1, a2);
957 void PushGetBlocks(CBlockIndex* pindexBegin, uint256 hashEnd);
958 bool IsSubscribed(unsigned int nChannel);
959 void Subscribe(unsigned int nChannel, unsigned int nHops=0);
960 void CancelSubscribe(unsigned int nChannel);
961 void CloseSocketDisconnect();
974 inline void RelayInventory(const CInv& inv)
976 // Put on lists to offer to the other nodes
977 CRITICAL_BLOCK(cs_vNodes)
978 BOOST_FOREACH(CNode* pnode, vNodes)
979 pnode->PushInventory(inv);
983 void RelayMessage(const CInv& inv, const T& a)
985 CDataStream ss(SER_NETWORK);
988 RelayMessage(inv, ss);
992 inline void RelayMessage<>(const CInv& inv, const CDataStream& ss)
994 CRITICAL_BLOCK(cs_mapRelay)
996 // Expire old relay messages
997 while (!vRelayExpiration.empty() && vRelayExpiration.front().first < GetTime())
999 mapRelay.erase(vRelayExpiration.front().second);
1000 vRelayExpiration.pop_front();
1003 // Save original serialized message so newer versions are preserved
1005 vRelayExpiration.push_back(std::make_pair(GetTime() + 15 * 60, inv));
1008 RelayInventory(inv);
1019 // Templates for the publish and subscription system.
1020 // The object being published as T& obj needs to have:
1021 // a set<unsigned int> setSources member
1022 // specializations of AdvertInsert and AdvertErase
1023 // Currently implemented for CTable and CProduct.
1026 template<typename T>
1027 void AdvertStartPublish(CNode* pfrom, unsigned int nChannel, unsigned int nHops, T& obj)
1030 obj.setSources.insert(pfrom->addr.ip);
1032 if (!AdvertInsert(obj))
1036 CRITICAL_BLOCK(cs_vNodes)
1037 BOOST_FOREACH(CNode* pnode, vNodes)
1038 if (pnode != pfrom && (nHops < PUBLISH_HOPS || pnode->IsSubscribed(nChannel)))
1039 pnode->PushMessage("publish", nChannel, nHops, obj);
1042 template<typename T>
1043 void AdvertStopPublish(CNode* pfrom, unsigned int nChannel, unsigned int nHops, T& obj)
1045 uint256 hash = obj.GetHash();
1047 CRITICAL_BLOCK(cs_vNodes)
1048 BOOST_FOREACH(CNode* pnode, vNodes)
1049 if (pnode != pfrom && (nHops < PUBLISH_HOPS || pnode->IsSubscribed(nChannel)))
1050 pnode->PushMessage("pub-cancel", nChannel, nHops, hash);
1055 template<typename T>
1056 void AdvertRemoveSource(CNode* pfrom, unsigned int nChannel, unsigned int nHops, T& obj)
1059 obj.setSources.erase(pfrom->addr.ip);
1061 // If no longer supported by any sources, cancel it
1062 if (obj.setSources.empty())
1063 AdvertStopPublish(pfrom, nChannel, nHops, obj);