9 #include <boost/array.hpp>
10 #include <boost/foreach.hpp>
11 #include <openssl/rand.h>
14 #include <arpa/inet.h>
44 void StartNode(boost::thread_group& threadGroup);
79 extern std::vector<CNode*>
vNodes;
81 extern std::map<CInv, CDataStream>
mapRelay;
125 CNetMessage(
int nTypeIn,
int nVersionIn) : hdrbuf(nTypeIn, nVersionIn), vRecv(nTypeIn, nVersionIn) {
145 int readHeader(
const char *pch,
unsigned int nBytes);
146 int readData(
const char *pch,
unsigned int nBytes);
234 nRecvVersion = INIT_PROTO_VERSION;
241 nBlocksRequested = 0;
248 fInbound = fInboundIn;
249 fNetworkNode =
false;
250 fSuccessfullyConnected =
false;
256 pindexLastGetBlocksBegin = 0;
257 hashLastGetBlocksEnd = 0;
258 nStartingHeight = -1;
262 hashCheckpointKnown = 0;
291 assert(nRefCount >= 0);
298 unsigned int total = 0;
310 nRecvVersion = nVersionIn;
330 setAddrKnown.insert(addr);
338 if (addr.
IsValid() && !setAddrKnown.count(addr))
339 vAddrToSend.push_back(addr);
347 setInventoryKnown.
insert(inv);
355 if (!setInventoryKnown.
count(inv))
356 vInventoryToSend.push_back(inv);
366 if (it != mapAlreadyAskedFor.
end())
367 nRequestTime = it->second;
375 static int64 nLastTime;
377 nNow = std::max(nNow, nLastTime);
381 nRequestTime = std::max(nRequestTime + 2 * 60 * 1000000, nNow);
382 if (it != mapAlreadyAskedFor.
end())
383 mapAlreadyAskedFor.
update(it, nRequestTime);
385 mapAlreadyAskedFor.
insert(std::make_pair(inv, nRequestTime));
386 mapAskFor.insert(std::make_pair(nRequestTime, inv));
395 assert(ssSend.
size() == 0);
398 printf(
"sending: %s ", pszCommand);
417 printf(
"dropmessages DROPPING SEND MESSAGE\n");
422 if (ssSend.
size() == 0)
426 unsigned int nSize = ssSend.
size() - CMessageHeader::HEADER_SIZE;
427 memcpy((
char*)&ssSend[CMessageHeader::MESSAGE_SIZE_OFFSET], &nSize,
sizeof(nSize));
431 unsigned int nChecksum = 0;
432 memcpy(&nChecksum, &hash,
sizeof(nChecksum));
433 assert(ssSend.
size () >= CMessageHeader::CHECKSUM_OFFSET +
sizeof(nChecksum));
434 memcpy((
char*)&ssSend[CMessageHeader::CHECKSUM_OFFSET], &nChecksum,
sizeof(nChecksum));
437 printf(
"(%d bytes)\n", nSize);
440 std::deque<CSerializeData>::iterator it = vSendMsg.insert(vSendMsg.end(),
CSerializeData());
442 nSendSize += (*it).size();
445 if (it == vSendMsg.begin())
468 template<
typename T1>
484 template<
typename T1,
typename T2>
485 void PushMessage(
const char* pszCommand,
const T1& a1,
const T2& a2)
500 template<
typename T1,
typename T2,
typename T3>
501 void PushMessage(
const char* pszCommand,
const T1& a1,
const T2& a2,
const T3& a3)
506 ssSend << a1 << a2 << a3;
516 template<
typename T1,
typename T2,
typename T3,
typename T4>
517 void PushMessage(
const char* pszCommand,
const T1& a1,
const T2& a2,
const T3& a3,
const T4& a4)
522 ssSend << a1 << a2 << a3 << a4;
532 template<
typename T1,
typename T2,
typename T3,
typename T4,
typename T5>
533 void PushMessage(
const char* pszCommand,
const T1& a1,
const T2& a2,
const T3& a3,
const T4& a4,
const T5& a5)
538 ssSend << a1 << a2 << a3 << a4 << a5;
548 template<
typename T1,
typename T2,
typename T3,
typename T4,
typename T5,
typename T6>
549 void PushMessage(
const char* pszCommand,
const T1& a1,
const T2& a2,
const T3& a3,
const T4& a4,
const T5& a5,
const T6& a6)
554 ssSend << a1 << a2 << a3 << a4 << a5 << a6;
564 template<
typename T1,
typename T2,
typename T3,
typename T4,
typename T5,
typename T6,
typename T7>
565 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)
570 ssSend << a1 << a2 << a3 << a4 << a5 << a6 << a7;
580 template<
typename T1,
typename T2,
typename T3,
typename T4,
typename T5,
typename T6,
typename T7,
typename T8>
581 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)
586 ssSend << a1 << a2 << a3 << a4 << a5 << a6 << a7 << a8;
596 template<
typename T1,
typename T2,
typename T3,
typename T4,
typename T5,
typename T6,
typename T7,
typename T8,
typename T9>
597 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)
602 ssSend << a1 << a2 << a3 << a4 << a5 << a6 << a7 << a8 << a9;
614 void Subscribe(
unsigned int nChannel,
unsigned int nHops=0);
std::multimap< int64, CInv > mapAskFor
std::map< K, V >::const_iterator const_iterator
CNetMessage(int nTypeIn, int nVersionIn)
void PushMessage(const char *pszCommand)
std::vector< CNode * > vNodes
#define EXCLUSIVE_LOCK_FUNCTION(...)
bool GetLocal(CService &addr, const CNetAddr *paddrPeer=NULL)
CNode(SOCKET hSocketIn, CAddress addrIn, std::string addrNameIn="", bool fInboundIn=false)
CCriticalSection cs_filter
const_iterator begin() const
void AddOneShot(std::string strDest)
STL-like map container that only keeps the N elements with the highest value.
void EndMessage() UNLOCK_FUNCTION(cs_vSend)
void GetAndClear(CSerializeData &data)
void insert(const value_type &x)
CCriticalSection cs_mapRelay
void PushMessage(const char *pszCommand, const T1 &a1)
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)
void CancelSubscribe(unsigned int nChannel)
bool SeenLocal(const CService &addr)
vote for a local address
CCriticalSection cs_vNodes
static void ClearBanned()
void update(const_iterator itIn, const mapped_type &v)
bool IsSubscribed(unsigned int nChannel)
void PushMessage(const char *pszCommand, const T1 &a1, const T2 &a2, const T3 &a3, const T4 &a4, const T5 &a5, const T6 &a6)
std::string ToStringIPPort() const
void resize(size_type n, value_type c=0)
std::pair< iterator, bool > insert(const key_type &x)
void AskFor(const CInv &inv)
BloomFilter is a probabilistic filter which SPV clients provide so that we can filter the transaction...
RAII-style semaphore lock.
uint256 hashLastGetBlocksEnd
void SetVersion(int nVersionIn)
bool RecvLine(SOCKET hSocket, std::string &strLine)
size_type max_size() const
void MapPort(bool fUseUPnP)
Double ended buffer combining vector and stream-like interfaces.
CCriticalSection cs_inventory
std::vector< CAddress > vAddrToSend
unsigned long long uint64
CCriticalSection cs_vAddedNodes
void AddAddressKnown(const CAddress &addr)
void PushMessage(const char *pszCommand, const T1 &a1, const T2 &a2, const T3 &a3, const T4 &a4, const T5 &a5)
void SetRecvVersion(int nVersionIn)
CNode * ConnectNode(CAddress addrConnect, const char *strDest=NULL)
size_type count(const key_type &k) const
void PushInventory(const CInv &inv)
CAddress GetLocalAddress(const CNetAddr *paddrPeer=NULL)
std::deque< CInv > vRecvGetData
void BeginMessage(const char *pszCommand) EXCLUSIVE_LOCK_FUNCTION(cs_vSend)
void PushMessage(const char *pszCommand, const T1 &a1, const T2 &a2, const T3 &a3, const T4 &a4)
std::set< uint256 > setKnown
Stochastical (IP) address manager.
void AddressCurrentlyConnected(const CService &addr)
std::deque< CNetMessage > vRecvMsg
unsigned int GetTotalRecvSize()
limitedmap< CInv, int64 > mapAlreadyAskedFor
void CloseSocketDisconnect()
std::map< CInv, CDataStream > mapRelay
bool IsReachable(const CNetAddr &addr)
check whether a given address is in a network we can probably connect to
#define LEAVE_CRITICAL_SECTION(cs)
void RelayTransaction(const CTransaction &tx, const uint256 &hash)
std::deque< std::pair< int64, CInv > > vRelayExpiration
uint64 GetRand(uint64 nMax)
unsigned int ReceiveFloodSize()
A combination of a network address (CNetAddr) and a (TCP) port.
std::vector< char, zero_after_free_allocator< char > > CSerializeData
CBlockIndex * pindexLastGetBlocksBegin
bool ReceiveMsgBytes(const char *pch, unsigned int nBytes)
mruset< CInv > setInventoryKnown
CNode * FindNode(const CNetAddr &ip)
A CService with information about it as peer.
unsigned int SendBufferSize()
bool fSuccessfullyConnected
bool GetMyExternalIP(CNetAddr &ipRet)
uint256 Hash(const T1 pbegin, const T1 pend)
std::string DateTimeStrFormat(const char *pszFormat, int64 nTime)
const_iterator end() const
void SetLimited(enum Network net, bool fLimited=true)
Make a particular network entirely off-limits (no automatic connects to it)
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)
int readData(const char *pch, unsigned int nBytes)
#define UNLOCK_FUNCTION(...)
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)
#define ENTER_CRITICAL_SECTION(cs)
void SocketSendData(CNode *pnode)
IP address (IPv6, or IPv4 using mapped IPv6 range (::FFFF:0:0/96))
void PushMessage(const char *pszCommand, const T1 &a1, const T2 &a2, const T3 &a3)
void StartNode(boost::thread_group &threadGroup)
void AddInventoryKnown(const CInv &inv)
CCriticalSection cs_vRecvMsg
The block chain is a tree shaped structure starting with the genesis block at the root...
CSemaphoreGrant grantOutbound
std::vector< CInv > vInventoryToSend
static bool IsBanned(CNetAddr ip)
void SetReachable(enum Network net, bool fFlag=true)
static CCriticalSection cs_setBanned
void operator=(const CNode &)
std::vector< std::string > vAddedNodes
void Subscribe(unsigned int nChannel, unsigned int nHops=0)
bool BindListenPort(const CService &bindAddr, std::string &strError=REF(std::string()))
void PushMessage(const char *pszCommand, const T1 &a1, const T2 &a2)
bool Misbehaving(int howmuch)
int readHeader(const char *pch, unsigned int nBytes)
std::string GetArg(const std::string &strArg, const std::string &strDefault)
Return string argument or default value.
The basic transaction that is broadcasted on the network and contained in blocks. ...
Information about a peer.
std::string ToString() const
CCriticalSection cs_vSend
void AbortMessage() UNLOCK_FUNCTION(cs_vSend)
void copyStats(CNodeStats &stats)
uint256 hashCheckpointKnown
bool AddLocal(const CService &addr, int nScore=LOCAL_NONE)
void PushGetBlocks(CBlockIndex *pindexBegin, uint256 hashEnd)
bool IsLocal(const CService &addr)
check whether a given address is potentially local
static std::map< CNetAddr, int64 > setBanned
const_iterator find(const key_type &k) const
unsigned short GetListenPort()
std::set< CAddress > setAddrKnown
bool IsLimited(enum Network net)
map< string, string > mapArgs
int atoi(const std::string &str)
std::deque< CSerializeData > vSendMsg
const_iterator end() const
void PushAddress(const CAddress &addr)