1 // Copyright (c) 2009-2010 Satoshi Nakamoto
\r
2 // Distributed under the MIT/X11 software license, see the accompanying
\r
3 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
\r
5 class CMessageHeader;
\r
8 class CRequestTracker;
\r
11 extern int nBestHeight;
\r
15 static const unsigned short DEFAULT_PORT = 0x8d20; // htons(8333)
\r
16 static const unsigned int PUBLISH_HOPS = 5;
\r
19 NODE_NETWORK = (1 << 0),
\r
25 bool ConnectSocket(const CAddress& addrConnect, SOCKET& hSocketRet);
\r
26 bool GetMyExternalIP(unsigned int& ipRet);
\r
27 bool AddAddress(CAddress addr);
\r
28 void AddressCurrentlyConnected(const CAddress& addr);
\r
29 CNode* FindNode(unsigned int ip);
\r
30 CNode* ConnectNode(CAddress addrConnect, int64 nTimeout=0);
\r
31 void AbandonRequests(void (*fn)(void*, CDataStream&), void* param1);
\r
32 bool AnySubscribed(unsigned int nChannel);
\r
33 bool BindListenPort(string& strError=REF(string()));
\r
34 void StartNode(void* parg);
\r
46 // (4) message start
\r
51 // The message start string is designed to be unlikely to occur in normal data.
\r
52 // The characters are rarely used upper ascii, not valid as UTF-8, and produce
\r
53 // a large 4-byte int at any alignment.
\r
54 static const char pchMessageStart[4] = { 0xf9, 0xbe, 0xb4, 0xd9 };
\r
56 class CMessageHeader
\r
59 enum { COMMAND_SIZE=12 };
\r
60 char pchMessageStart[sizeof(::pchMessageStart)];
\r
61 char pchCommand[COMMAND_SIZE];
\r
62 unsigned int nMessageSize;
\r
63 unsigned int nChecksum;
\r
67 memcpy(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart));
\r
68 memset(pchCommand, 0, sizeof(pchCommand));
\r
74 CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn)
\r
76 memcpy(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart));
\r
77 strncpy(pchCommand, pszCommand, COMMAND_SIZE);
\r
78 nMessageSize = nMessageSizeIn;
\r
84 READWRITE(FLATDATA(pchMessageStart));
\r
85 READWRITE(FLATDATA(pchCommand));
\r
86 READWRITE(nMessageSize);
\r
87 if (nVersion >= 209)
\r
88 READWRITE(nChecksum);
\r
93 if (pchCommand[COMMAND_SIZE-1] == 0)
\r
94 return string(pchCommand, pchCommand + strlen(pchCommand));
\r
96 return string(pchCommand, pchCommand + COMMAND_SIZE);
\r
101 // Check start string
\r
102 if (memcmp(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart)) != 0)
\r
105 // Check the command string for errors
\r
106 for (char* p1 = pchCommand; p1 < pchCommand + COMMAND_SIZE; p1++)
\r
110 // Must be all zeros after the first zero
\r
111 for (; p1 < pchCommand + COMMAND_SIZE; p1++)
\r
115 else if (*p1 < ' ' || *p1 > 0x7E)
\r
120 if (nMessageSize > 0x10000000)
\r
122 printf("CMessageHeader::IsValid() : nMessageSize too large %u\n", nMessageSize);
\r
135 static const unsigned char pchIPv4[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff };
\r
141 unsigned char pchReserved[12];
\r
143 unsigned short port;
\r
146 unsigned int nTime;
\r
149 unsigned int nLastTry;
\r
156 CAddress(unsigned int ipIn, unsigned short portIn=DEFAULT_PORT, uint64 nServicesIn=NODE_NETWORK)
\r
161 nServices = nServicesIn;
\r
164 explicit CAddress(const struct sockaddr_in& sockaddr, uint64 nServicesIn=NODE_NETWORK)
\r
167 ip = sockaddr.sin_addr.s_addr;
\r
168 port = sockaddr.sin_port;
\r
169 nServices = nServicesIn;
\r
172 explicit CAddress(const char* pszIn, uint64 nServicesIn=NODE_NETWORK)
\r
176 nServices = nServicesIn;
\r
179 explicit CAddress(string strIn, uint64 nServicesIn=NODE_NETWORK)
\r
182 SetAddress(strIn.c_str());
\r
183 nServices = nServicesIn;
\r
188 nServices = NODE_NETWORK;
\r
189 memcpy(pchReserved, pchIPv4, sizeof(pchReserved));
\r
191 port = DEFAULT_PORT;
\r
192 nTime = GetAdjustedTime();
\r
196 bool SetAddress(const char* pszIn)
\r
199 port = DEFAULT_PORT;
\r
201 strlcpy(psz, pszIn, sizeof(psz));
\r
202 unsigned int a=0, b=0, c=0, d=0, e=0;
\r
203 if (sscanf(psz, "%u.%u.%u.%u:%u", &a, &b, &c, &d, &e) < 4)
\r
205 char* pszPort = strchr(psz, ':');
\r
209 port = htons(atoi(pszPort));
\r
210 if (atoi(pszPort) < 0 || atoi(pszPort) > USHRT_MAX)
\r
211 port = htons(USHRT_MAX);
\r
213 ip = inet_addr(psz);
\r
217 bool SetAddress(string strIn)
\r
219 return SetAddress(strIn.c_str());
\r
222 IMPLEMENT_SERIALIZE
\r
224 if (nType & SER_DISK)
\r
226 READWRITE(nVersion);
\r
229 READWRITE(nServices);
\r
230 READWRITE(FLATDATA(pchReserved)); // for IPv6
\r
235 friend inline bool operator==(const CAddress& a, const CAddress& b)
\r
237 return (memcmp(a.pchReserved, b.pchReserved, sizeof(a.pchReserved)) == 0 &&
\r
242 friend inline bool operator!=(const CAddress& a, const CAddress& b)
\r
244 return (!(a == b));
\r
247 friend inline bool operator<(const CAddress& a, const CAddress& b)
\r
249 int ret = memcmp(a.pchReserved, b.pchReserved, sizeof(a.pchReserved));
\r
254 if (ntohl(a.ip) < ntohl(b.ip))
\r
256 else if (a.ip == b.ip)
\r
257 return ntohs(a.port) < ntohs(b.port);
\r
262 vector<unsigned char> GetKey() const
\r
266 ss << FLATDATA(pchReserved) << ip << port;
\r
268 #if defined(_MSC_VER) && _MSC_VER < 1300
\r
269 return vector<unsigned char>((unsigned char*)&ss.begin()[0], (unsigned char*)&ss.end()[0]);
\r
271 return vector<unsigned char>(ss.begin(), ss.end());
\r
275 struct sockaddr_in GetSockAddr() const
\r
277 struct sockaddr_in sockaddr;
\r
278 memset(&sockaddr, 0, sizeof(sockaddr));
\r
279 sockaddr.sin_family = AF_INET;
\r
280 sockaddr.sin_addr.s_addr = ip;
\r
281 sockaddr.sin_port = port;
\r
285 bool IsIPv4() const
\r
287 return (memcmp(pchReserved, pchIPv4, sizeof(pchIPv4)) == 0);
\r
290 bool IsRoutable() const
\r
292 return IsValid() &&
\r
293 !(GetByte(3) == 10 ||
\r
294 (GetByte(3) == 192 && GetByte(2) == 168) ||
\r
295 GetByte(3) == 127 ||
\r
299 bool IsValid() const
\r
301 // Clean up 3-byte shifted addresses caused by garbage in size field
\r
302 // of addr messages from versions before 0.2.9 checksum.
\r
303 // Two consecutive addr messages look like this:
\r
304 // header20 vectorlen3 addr26 addr26 addr26 header20 vectorlen3 addr26 addr26 addr26...
\r
305 // so if the first length field is garbled, it reads the second batch
\r
306 // of addr misaligned by 3 bytes.
\r
307 if (memcmp(pchReserved, pchIPv4+3, sizeof(pchIPv4)-3) == 0)
\r
310 return (ip != 0 && ip != INADDR_NONE && port != htons(USHRT_MAX));
\r
313 unsigned char GetByte(int n) const
\r
315 return ((unsigned char*)&ip)[3-n];
\r
318 string ToStringIPPort() const
\r
320 return strprintf("%u.%u.%u.%u:%u", GetByte(3), GetByte(2), GetByte(1), GetByte(0), ntohs(port));
\r
323 string ToStringIP() const
\r
325 return strprintf("%u.%u.%u.%u", GetByte(3), GetByte(2), GetByte(1), GetByte(0));
\r
328 string ToStringPort() const
\r
330 return strprintf("%u", ntohs(port));
\r
333 string ToStringLog() const
\r
338 string ToString() const
\r
340 return strprintf("%u.%u.%u.%u:%u", GetByte(3), GetByte(2), GetByte(1), GetByte(0), ntohs(port));
\r
345 printf("CAddress(%s)\n", ToString().c_str());
\r
361 static const char* ppszTypeName[] =
\r
380 CInv(int typeIn, const uint256& hashIn)
\r
386 CInv(const string& strType, const uint256& hashIn)
\r
389 for (i = 1; i < ARRAYLEN(ppszTypeName); i++)
\r
391 if (strType == ppszTypeName[i])
\r
397 if (i == ARRAYLEN(ppszTypeName))
\r
398 throw std::out_of_range(strprintf("CInv::CInv(string, uint256) : unknown type '%s'", strType.c_str()));
\r
402 IMPLEMENT_SERIALIZE
\r
408 friend inline bool operator<(const CInv& a, const CInv& b)
\r
410 return (a.type < b.type || (a.type == b.type && a.hash < b.hash));
\r
413 bool IsKnownType() const
\r
415 return (type >= 1 && type < ARRAYLEN(ppszTypeName));
\r
418 const char* GetCommand() const
\r
420 if (!IsKnownType())
\r
421 throw std::out_of_range(strprintf("CInv::GetCommand() : type=% unknown type", type));
\r
422 return ppszTypeName[type];
\r
425 string ToString() const
\r
427 return strprintf("%s %s", GetCommand(), hash.ToString().substr(0,20).c_str());
\r
432 printf("CInv(%s)\n", ToString().c_str());
\r
440 class CRequestTracker
\r
443 void (*fn)(void*, CDataStream&);
\r
446 explicit CRequestTracker(void (*fnIn)(void*, CDataStream&)=NULL, void* param1In=NULL)
\r
462 extern bool fClient;
\r
463 extern uint64 nLocalServices;
\r
464 extern CAddress addrLocalHost;
\r
465 extern CNode* pnodeLocalHost;
\r
466 extern uint64 nLocalHostNonce;
\r
467 extern array<int, 10> vnThreadsRunning;
\r
468 extern SOCKET hListenSocket;
\r
469 extern int64 nThreadSocketHandlerHeartbeat;
\r
471 extern vector<CNode*> vNodes;
\r
472 extern CCriticalSection cs_vNodes;
\r
473 extern map<vector<unsigned char>, CAddress> mapAddresses;
\r
474 extern CCriticalSection cs_mapAddresses;
\r
475 extern map<CInv, CDataStream> mapRelay;
\r
476 extern deque<pair<int64, CInv> > vRelayExpiration;
\r
477 extern CCriticalSection cs_mapRelay;
\r
478 extern map<CInv, int64> mapAlreadyAskedFor;
\r
481 extern int fUseProxy;
\r
482 extern CAddress addrProxy;
\r
497 CCriticalSection cs_vSend;
\r
498 CCriticalSection cs_vRecv;
\r
501 int64 nLastSendEmpty;
\r
502 int64 nTimeConnected;
\r
503 unsigned int nHeaderStart;
\r
504 unsigned int nMessageStart;
\r
510 bool fSuccessfullyConnected;
\r
515 int64 nReleaseTime;
\r
516 map<uint256, CRequestTracker> mapRequests;
\r
517 CCriticalSection cs_mapRequests;
\r
518 uint256 hashContinue;
\r
519 CBlockIndex* pindexLastGetBlocksBegin;
\r
520 uint256 hashLastGetBlocksEnd;
\r
521 int nStartingHeight;
\r
524 vector<CAddress> vAddrToSend;
\r
525 set<CAddress> setAddrKnown;
\r
528 // inventory based relay
\r
529 set<CInv> setInventoryKnown;
\r
530 vector<CInv> vInventoryToSend;
\r
531 CCriticalSection cs_inventory;
\r
532 multimap<int64, CInv> mapAskFor;
\r
534 // publish and subscription
\r
535 vector<char> vfSubscribe;
\r
538 CNode(SOCKET hSocketIn, CAddress addrIn, bool fInboundIn=false)
\r
541 hSocket = hSocketIn;
\r
542 vSend.SetType(SER_NETWORK);
\r
543 vSend.SetVersion(0);
\r
544 vRecv.SetType(SER_NETWORK);
\r
545 vRecv.SetVersion(0);
\r
546 // Version 0.2 obsoletes 20 Feb 2012
\r
547 if (GetTime() > 1329696000)
\r
549 vSend.SetVersion(209);
\r
550 vRecv.SetVersion(209);
\r
554 nLastSendEmpty = GetTime();
\r
555 nTimeConnected = GetTime();
\r
557 nMessageStart = -1;
\r
560 fClient = false; // set by version message
\r
561 fInbound = fInboundIn;
\r
562 fNetworkNode = false;
\r
563 fSuccessfullyConnected = false;
\r
564 fDisconnect = false;
\r
568 pindexLastGetBlocksBegin = 0;
\r
569 hashLastGetBlocksEnd = 0;
\r
570 nStartingHeight = -1;
\r
572 vfSubscribe.assign(256, false);
\r
574 // Push a version message
\r
575 /// when NTP implemented, change to just nTime = GetAdjustedTime()
\r
576 int64 nTime = (fInbound ? GetAdjustedTime() : GetTime());
\r
577 CAddress addrYou = (fUseProxy ? CAddress("0.0.0.0") : addr);
\r
578 CAddress addrMe = (fUseProxy ? CAddress("0.0.0.0") : addrLocalHost);
\r
579 RAND_bytes((unsigned char*)&nLocalHostNonce, sizeof(nLocalHostNonce));
\r
580 PushMessage("version", VERSION, nLocalServices, nTime, addrYou, addrMe,
\r
581 nLocalHostNonce, string(pszSubVer), nBestHeight);
\r
586 if (hSocket != INVALID_SOCKET)
\r
588 closesocket(hSocket);
\r
589 hSocket = INVALID_SOCKET;
\r
594 CNode(const CNode&);
\r
595 void operator=(const CNode&);
\r
601 return max(nRefCount, 0) + (GetTime() < nReleaseTime ? 1 : 0);
\r
604 CNode* AddRef(int64 nTimeout=0)
\r
607 nReleaseTime = max(nReleaseTime, GetTime() + nTimeout);
\r
620 void AddAddressKnown(const CAddress& addr)
\r
622 setAddrKnown.insert(addr);
\r
625 void PushAddress(const CAddress& addr)
\r
627 // Known checking here is only to save space from duplicates.
\r
628 // SendMessages will filter it again for knowns that were added
\r
629 // after addresses were pushed.
\r
630 if (addr.IsValid() && !setAddrKnown.count(addr))
\r
631 vAddrToSend.push_back(addr);
\r
635 void AddInventoryKnown(const CInv& inv)
\r
637 CRITICAL_BLOCK(cs_inventory)
\r
638 setInventoryKnown.insert(inv);
\r
641 void PushInventory(const CInv& inv)
\r
643 CRITICAL_BLOCK(cs_inventory)
\r
644 if (!setInventoryKnown.count(inv))
\r
645 vInventoryToSend.push_back(inv);
\r
648 void AskFor(const CInv& inv)
\r
650 // We're using mapAskFor as a priority queue,
\r
651 // the key is the earliest time the request can be sent
\r
652 int64& nRequestTime = mapAlreadyAskedFor[inv];
\r
653 printf("askfor %s %"PRI64d"\n", inv.ToString().c_str(), nRequestTime);
\r
655 // Make sure not to reuse time indexes to keep things in the same order
\r
656 int64 nNow = (GetTime() - 1) * 1000000;
\r
657 static int64 nLastTime;
\r
658 nLastTime = nNow = max(nNow, ++nLastTime);
\r
660 // Each retry is 2 minutes after the last
\r
661 nRequestTime = max(nRequestTime + 2 * 60 * 1000000, nNow);
\r
662 mapAskFor.insert(make_pair(nRequestTime, inv));
\r
667 void BeginMessage(const char* pszCommand)
\r
670 if (nHeaderStart != -1)
\r
672 nHeaderStart = vSend.size();
\r
673 vSend << CMessageHeader(pszCommand, 0);
\r
674 nMessageStart = vSend.size();
\r
676 printf("%s ", DateTimeStrFormat("%x %H:%M:%S", GetTime()).c_str());
\r
677 printf("sending: %s ", pszCommand);
\r
680 void AbortMessage()
\r
682 if (nHeaderStart == -1)
\r
684 vSend.resize(nHeaderStart);
\r
686 nMessageStart = -1;
\r
688 printf("(aborted)\n");
\r
693 if (mapArgs.count("-dropmessagestest") && GetRand(atoi(mapArgs["-dropmessagestest"])) == 0)
\r
695 printf("dropmessages DROPPING SEND MESSAGE\n");
\r
700 if (nHeaderStart == -1)
\r
704 unsigned int nSize = vSend.size() - nMessageStart;
\r
705 memcpy((char*)&vSend[nHeaderStart] + offsetof(CMessageHeader, nMessageSize), &nSize, sizeof(nSize));
\r
707 // Set the checksum
\r
708 if (vSend.GetVersion() >= 209)
\r
710 uint256 hash = Hash(vSend.begin() + nMessageStart, vSend.end());
\r
711 unsigned int nChecksum = 0;
\r
712 memcpy(&nChecksum, &hash, sizeof(nChecksum));
\r
713 assert(nMessageStart - nHeaderStart >= offsetof(CMessageHeader, nChecksum) + sizeof(nChecksum));
\r
714 memcpy((char*)&vSend[nHeaderStart] + offsetof(CMessageHeader, nChecksum), &nChecksum, sizeof(nChecksum));
\r
717 printf("(%d bytes) ", nSize);
\r
721 nMessageStart = -1;
\r
725 void EndMessageAbortIfEmpty()
\r
727 if (nHeaderStart == -1)
\r
729 int nSize = vSend.size() - nMessageStart;
\r
736 const char* GetMessageCommand() const
\r
738 if (nHeaderStart == -1)
\r
740 return &vSend[nHeaderStart] + offsetof(CMessageHeader, pchCommand);
\r
746 void PushMessage(const char* pszCommand)
\r
750 BeginMessage(pszCommand);
\r
760 template<typename T1>
\r
761 void PushMessage(const char* pszCommand, const T1& a1)
\r
765 BeginMessage(pszCommand);
\r
776 template<typename T1, typename T2>
\r
777 void PushMessage(const char* pszCommand, const T1& a1, const T2& a2)
\r
781 BeginMessage(pszCommand);
\r
792 template<typename T1, typename T2, typename T3>
\r
793 void PushMessage(const char* pszCommand, const T1& a1, const T2& a2, const T3& a3)
\r
797 BeginMessage(pszCommand);
\r
798 vSend << a1 << a2 << a3;
\r
808 template<typename T1, typename T2, typename T3, typename T4>
\r
809 void PushMessage(const char* pszCommand, const T1& a1, const T2& a2, const T3& a3, const T4& a4)
\r
813 BeginMessage(pszCommand);
\r
814 vSend << a1 << a2 << a3 << a4;
\r
824 template<typename T1, typename T2, typename T3, typename T4, typename T5>
\r
825 void PushMessage(const char* pszCommand, const T1& a1, const T2& a2, const T3& a3, const T4& a4, const T5& a5)
\r
829 BeginMessage(pszCommand);
\r
830 vSend << a1 << a2 << a3 << a4 << a5;
\r
840 template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
\r
841 void PushMessage(const char* pszCommand, const T1& a1, const T2& a2, const T3& a3, const T4& a4, const T5& a5, const T6& a6)
\r
845 BeginMessage(pszCommand);
\r
846 vSend << a1 << a2 << a3 << a4 << a5 << a6;
\r
856 template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7>
\r
857 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)
\r
861 BeginMessage(pszCommand);
\r
862 vSend << a1 << a2 << a3 << a4 << a5 << a6 << a7;
\r
872 template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8>
\r
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, const T8& a8)
\r
877 BeginMessage(pszCommand);
\r
878 vSend << a1 << a2 << a3 << a4 << a5 << a6 << a7 << a8;
\r
888 template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9>
\r
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, const T9& a9)
\r
893 BeginMessage(pszCommand);
\r
894 vSend << a1 << a2 << a3 << a4 << a5 << a6 << a7 << a8 << a9;
\r
905 void PushRequest(const char* pszCommand,
\r
906 void (*fn)(void*, CDataStream&), void* param1)
\r
909 RAND_bytes((unsigned char*)&hashReply, sizeof(hashReply));
\r
911 CRITICAL_BLOCK(cs_mapRequests)
\r
912 mapRequests[hashReply] = CRequestTracker(fn, param1);
\r
914 PushMessage(pszCommand, hashReply);
\r
917 template<typename T1>
\r
918 void PushRequest(const char* pszCommand, const T1& a1,
\r
919 void (*fn)(void*, CDataStream&), void* param1)
\r
922 RAND_bytes((unsigned char*)&hashReply, sizeof(hashReply));
\r
924 CRITICAL_BLOCK(cs_mapRequests)
\r
925 mapRequests[hashReply] = CRequestTracker(fn, param1);
\r
927 PushMessage(pszCommand, hashReply, a1);
\r
930 template<typename T1, typename T2>
\r
931 void PushRequest(const char* pszCommand, const T1& a1, const T2& a2,
\r
932 void (*fn)(void*, CDataStream&), void* param1)
\r
935 RAND_bytes((unsigned char*)&hashReply, sizeof(hashReply));
\r
937 CRITICAL_BLOCK(cs_mapRequests)
\r
938 mapRequests[hashReply] = CRequestTracker(fn, param1);
\r
940 PushMessage(pszCommand, hashReply, a1, a2);
\r
945 void PushGetBlocks(CBlockIndex* pindexBegin, uint256 hashEnd);
\r
946 bool IsSubscribed(unsigned int nChannel);
\r
947 void Subscribe(unsigned int nChannel, unsigned int nHops=0);
\r
948 void CancelSubscribe(unsigned int nChannel);
\r
949 void CloseSocketDisconnect();
\r
962 inline void RelayInventory(const CInv& inv)
\r
964 // Put on lists to offer to the other nodes
\r
965 CRITICAL_BLOCK(cs_vNodes)
\r
966 foreach(CNode* pnode, vNodes)
\r
967 pnode->PushInventory(inv);
\r
970 template<typename T>
\r
971 void RelayMessage(const CInv& inv, const T& a)
\r
973 CDataStream ss(SER_NETWORK);
\r
976 RelayMessage(inv, ss);
\r
980 inline void RelayMessage<>(const CInv& inv, const CDataStream& ss)
\r
982 CRITICAL_BLOCK(cs_mapRelay)
\r
984 // Expire old relay messages
\r
985 while (!vRelayExpiration.empty() && vRelayExpiration.front().first < GetTime())
\r
987 mapRelay.erase(vRelayExpiration.front().second);
\r
988 vRelayExpiration.pop_front();
\r
991 // Save original serialized message so newer versions are preserved
\r
992 mapRelay[inv] = ss;
\r
993 vRelayExpiration.push_back(make_pair(GetTime() + 15 * 60, inv));
\r
996 RelayInventory(inv);
\r
1007 // Templates for the publish and subscription system.
\r
1008 // The object being published as T& obj needs to have:
\r
1009 // a set<unsigned int> setSources member
\r
1010 // specializations of AdvertInsert and AdvertErase
\r
1011 // Currently implemented for CTable and CProduct.
\r
1014 template<typename T>
\r
1015 void AdvertStartPublish(CNode* pfrom, unsigned int nChannel, unsigned int nHops, T& obj)
\r
1018 obj.setSources.insert(pfrom->addr.ip);
\r
1020 if (!AdvertInsert(obj))
\r
1024 CRITICAL_BLOCK(cs_vNodes)
\r
1025 foreach(CNode* pnode, vNodes)
\r
1026 if (pnode != pfrom && (nHops < PUBLISH_HOPS || pnode->IsSubscribed(nChannel)))
\r
1027 pnode->PushMessage("publish", nChannel, nHops, obj);
\r
1030 template<typename T>
\r
1031 void AdvertStopPublish(CNode* pfrom, unsigned int nChannel, unsigned int nHops, T& obj)
\r
1033 uint256 hash = obj.GetHash();
\r
1035 CRITICAL_BLOCK(cs_vNodes)
\r
1036 foreach(CNode* pnode, vNodes)
\r
1037 if (pnode != pfrom && (nHops < PUBLISH_HOPS || pnode->IsSubscribed(nChannel)))
\r
1038 pnode->PushMessage("pub-cancel", nChannel, nHops, hash);
\r
1043 template<typename T>
\r
1044 void AdvertRemoveSource(CNode* pfrom, unsigned int nChannel, unsigned int nHops, T& obj)
\r
1046 // Remove a source
\r
1047 obj.setSources.erase(pfrom->addr.ip);
\r
1049 // If no longer supported by any sources, cancel it
\r
1050 if (obj.setSources.empty())
\r
1051 AdvertStopPublish(pfrom, nChannel, nHops, obj);
\r