Files

818 lines
24 KiB
C++
Raw Permalink Normal View History

2017-11-09 19:34:51 +01:00
/*
* This file is part of the Flowee project
* Copyright (C) 2009-2010 Satoshi Nakamoto
* Copyright (C) 2009-2015 The Bitcoin Core developers
2021-06-20 22:44:44 +02:00
* Copyright (C) 2016 Tom Zander <tom@flowee.org>
2017-11-09 19:34:51 +01:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2013-04-13 00:13:08 -05:00
2018-01-16 10:47:52 +00:00
#ifndef FLOWEE_NET_H
#define FLOWEE_NET_H
2011-05-15 09:11:04 +02:00
2013-04-13 00:13:08 -05:00
#include "bloom.h"
#include <compat.h>
2013-04-13 00:13:08 -05:00
#include "limitedmap.h"
#include <netbase.h>
#include <primitives/MutableBlock.h>
2013-04-13 00:13:08 -05:00
#include "protocol.h"
#include <random.h>
#include <streaming/streams.h>
#include <sync.h>
#include <uint256.h>
2013-04-13 00:13:08 -05:00
2011-05-15 09:11:04 +02:00
#include <deque>
2011-10-07 11:02:21 -04:00
#ifndef WIN32
2011-05-15 09:11:04 +02:00
#include <arpa/inet.h>
#endif
2010-08-29 16:58:15 +00:00
2014-08-21 16:11:09 +02:00
#include <boost/filesystem/path.hpp>
2013-04-13 00:13:08 -05:00
#include <boost/signals2/signal.hpp>
2016-07-06 20:16:18 +02:00
// if this is enabled the node will hold an extra int to do reporting. Enable to see size and other stats.
// #define LOG_XTHINBLOCKS
2013-04-13 00:13:08 -05:00
class CAddrMan;
2015-04-02 12:04:59 -04:00
class CScheduler;
2013-04-13 00:13:08 -05:00
class CNode;
namespace boost {
class thread_group;
} // namespace boost
/** Time between pings automatically sent out for latency probing and keepalive (in seconds). */
static const int PING_INTERVAL = 2 * 60;
/** Time after which to disconnect, after waiting for a ping response (or inactivity). */
static const int TIMEOUT_INTERVAL = 20 * 60;
2013-06-06 00:04:33 -07:00
/** The maximum number of entries in an 'inv' protocol message */
static const unsigned int MAX_INV_SZ = 50000;
/** The maximum number of new addresses to accumulate before announcing. */
static const unsigned int MAX_ADDR_TO_SEND = 1000;
2015-07-31 18:05:42 +02:00
/** Maximum length of strSubVer in `version` message */
static const unsigned int MAX_SUBVERSION_LENGTH = 256;
2014-05-29 13:02:22 +02:00
/** -listen default */
static const bool DEFAULT_LISTEN = true;
2014-05-05 21:06:14 +02:00
/** -upnp default */
#ifdef USE_UPNP
static const bool DEFAULT_UPNP = USE_UPNP;
#else
static const bool DEFAULT_UPNP = false;
#endif
2014-09-09 09:18:05 +02:00
/** The maximum number of entries in mapAskFor */
static const size_t MAPASKFOR_MAX_SZ = MAX_INV_SZ;
/** The maximum number of entries in setAskFor (larger due to getdata latency)*/
static const size_t SETASKFOR_MAX_SZ = 2 * MAX_INV_SZ;
unsigned int ReceiveFloodSize();
unsigned int SendBufferSize();
2010-08-29 16:58:15 +00:00
2015-05-31 15:36:44 +02:00
void AddOneShot(const std::string& strDest);
2012-01-03 23:33:31 +01:00
void AddressCurrentlyConnected(const CService& addr);
CNode* FindNode(const CNetAddr& ip);
2015-05-25 20:03:51 +02:00
CNode* FindNode(const CSubNet& subNet);
2014-07-24 16:29:41 +02:00
CNode* FindNode(const std::string& addrName);
2012-01-03 23:33:31 +01:00
CNode* FindNode(const CService& ip);
2018-01-15 15:26:12 +00:00
CNode* FindNode(int nodeId);
2014-05-24 11:14:52 +02:00
CNode* ConnectNode(CAddress addrConnect, const char *pszDest = NULL);
bool OpenNetworkConnection(const CAddress& addrConnect, CSemaphoreGrant *grantOutbound = NULL, const char *strDest = NULL, bool fOneShot = false);
2013-03-06 22:31:26 -05:00
void MapPort(bool fUseUPnP);
unsigned short GetListenPort();
2014-06-21 13:34:36 +02:00
bool BindListenPort(const CService &bindAddr, std::string& strError, bool fWhitelisted = false);
2015-04-02 12:04:59 -04:00
void StartNode(boost::thread_group& threadGroup, CScheduler& scheduler);
2010-08-29 16:58:15 +00:00
bool StopNode();
2012-11-15 18:20:26 -05:00
void SocketSendData(CNode *pnode);
2010-08-29 16:58:15 +00:00
2013-11-18 01:25:17 +01:00
typedef int NodeId;
2015-03-05 04:01:01 -08:00
struct CombinerAll
{
typedef bool result_type;
template<typename I>
bool operator()(I first, I last) const
{
while (first != last) {
if (!(*first)) return false;
++first;
}
return true;
}
};
// Signals for message handling
struct CNodeSignals
{
boost::signals2::signal<int ()> GetHeight;
2015-03-05 04:01:01 -08:00
boost::signals2::signal<bool (CNode*), CombinerAll> ProcessMessages;
boost::signals2::signal<bool (CNode*), CombinerAll> SendMessages;
2013-11-18 01:25:17 +01:00
boost::signals2::signal<void (NodeId, const CNode*)> InitializeNode;
boost::signals2::signal<void (NodeId)> FinalizeNode;
};
2013-11-18 01:25:17 +01:00
CNodeSignals& GetNodeSignals();
2013-01-07 08:07:51 -08:00
2012-02-12 13:45:24 +01:00
enum
{
2012-05-10 20:35:13 +02:00
LOCAL_NONE, // unknown
LOCAL_IF, // address a local interface listens on
2012-05-11 15:28:59 +02:00
LOCAL_BIND, // address explicit bound to
2012-05-10 20:35:13 +02:00
LOCAL_UPNP, // address reported by UPnP
LOCAL_MANUAL, // address explicitly specified (-externalip=)
2012-02-19 20:44:35 +01:00
LOCAL_MAX
2012-02-12 13:45:24 +01:00
};
bool IsPeerAddrLocalGood(CNode *pnode);
2016-02-12 11:35:32 -07:00
void AdvertiseLocal(CNode *pnode);
2016-11-22 12:54:44 +01:00
void SetLimited(CNetAddr::Network net, bool fLimited = true);
bool IsLimited(CNetAddr::Network net);
bool IsLimited(const CNetAddr& addr);
2012-05-10 20:35:13 +02:00
bool AddLocal(const CService& addr, int nScore = LOCAL_NONE);
bool AddLocal(const CNetAddr& addr, int nScore = LOCAL_NONE);
2018-08-10 18:07:35 +03:00
void RemoveLocal(const CService& addr);
2012-05-10 20:35:13 +02:00
bool SeenLocal(const CService& addr);
bool IsLocal(const CService& addr);
bool GetLocal(CService &addr, const CNetAddr *paddrPeer = NULL);
2016-11-22 12:54:44 +01:00
bool IsReachable(CNetAddr::Network net);
bool IsReachable(const CNetAddr &addr);
2012-02-12 13:45:24 +01:00
CAddress GetLocalAddress(const CNetAddr *paddrPeer = NULL);
2012-05-24 19:02:21 +02:00
extern bool fDiscover;
2014-05-29 12:33:17 +02:00
extern bool fListen;
2013-04-13 00:13:08 -05:00
extern uint64_t nLocalServices;
extern uint64_t nLocalHostNonce;
2012-01-04 23:39:45 +01:00
extern CAddrMan addrman;
2014-11-16 03:19:23 -08:00
/** Maximum number of connections to simultaneously allow (aka connection slots) */
extern int nMaxConnections;
2010-08-29 16:58:15 +00:00
2011-05-15 09:11:04 +02:00
extern std::vector<CNode*> vNodes;
2010-08-29 16:58:15 +00:00
extern CCriticalSection cs_vNodes;
2011-05-15 09:11:04 +02:00
extern std::map<CInv, CDataStream> mapRelay;
2013-04-13 00:13:08 -05:00
extern std::deque<std::pair<int64_t, CInv> > vRelayExpiration;
2010-08-29 16:58:15 +00:00
extern CCriticalSection cs_mapRelay;
2016-04-11 12:52:29 -04:00
extern limitedmap<uint256, int64_t> mapAlreadyAskedFor;
2010-08-29 16:58:15 +00:00
extern std::vector<std::string> vAddedNodes;
extern CCriticalSection cs_vAddedNodes;
2013-11-18 01:25:17 +01:00
extern NodeId nLastNodeId;
extern CCriticalSection cs_nLastNodeId;
2010-08-29 16:58:15 +00:00
struct LocalServiceInfo {
int nScore;
int nPort;
};
extern CCriticalSection cs_mapLocalHost;
extern std::map<CNetAddr, LocalServiceInfo> mapLocalHost;
2010-08-29 16:58:15 +00:00
class CNodeStats
{
public:
2013-11-18 01:25:17 +01:00
NodeId nodeid;
2013-04-13 00:13:08 -05:00
uint64_t nServices;
2015-11-20 18:51:44 -05:00
bool fRelayTxes;
2013-04-13 00:13:08 -05:00
int64_t nLastSend;
int64_t nLastRecv;
int64_t nTimeConnected;
2014-12-15 11:06:15 +01:00
int64_t nTimeOffset;
std::string addrName;
int nVersion;
2013-11-26 12:52:21 +01:00
std::string cleanSubVer;
bool fInbound;
int nStartingHeight;
2013-04-13 00:13:08 -05:00
uint64_t nSendBytes;
uint64_t nRecvBytes;
2014-06-21 13:34:36 +02:00
bool fWhitelisted;
2013-08-22 04:34:33 -07:00
double dPingTime;
double dPingWait;
2015-09-03 13:06:13 -07:00
double dPingMin;
std::string addrLocal;
};
2010-08-29 16:58:15 +00:00
class CNetMessage {
public:
bool in_data; // parsing header (false) or data (true)
CDataStream hdrbuf; // partially received header
CMessageHeader hdr; // complete header
unsigned int nHdrPos;
CDataStream vRecv; // received message data
unsigned int nDataPos;
int64_t nTime; // time (in microseconds) of message receipt.
2014-10-27 20:24:31 -04:00
CNetMessage(const CMessageHeader::MessageStartChars& pchMessageStartIn, int nTypeIn, int nVersionIn) : hdrbuf(nTypeIn, nVersionIn), hdr(pchMessageStartIn), vRecv(nTypeIn, nVersionIn) {
hdrbuf.resize(24);
in_data = false;
nHdrPos = 0;
nDataPos = 0;
nTime = 0;
}
bool complete() const
{
if (!in_data)
return false;
return (hdr.nMessageSize == nDataPos);
}
void SetVersion(int nVersionIn)
{
hdrbuf.SetVersion(nVersionIn);
vRecv.SetVersion(nVersionIn);
}
int readHeader(const char *pch, unsigned int nBytes);
int readData(const char *pch, unsigned int nBytes);
};
typedef enum BanReason
{
BanReasonUnknown = 0,
BanReasonNodeMisbehaving = 1,
BanReasonManuallyAdded = 2
} BanReason;
class CBanEntry
{
public:
static const int CURRENT_VERSION=1;
int nVersion;
int64_t nCreateTime;
int64_t nBanUntil;
uint8_t banReason;
CBanEntry()
{
SetNull();
}
CBanEntry(int64_t nCreateTimeIn)
{
SetNull();
nCreateTime = nCreateTimeIn;
}
2017-01-19 21:40:34 +01:00
ADD_SERIALIZE_METHODS
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
READWRITE(this->nVersion);
nVersion = this->nVersion;
READWRITE(nCreateTime);
READWRITE(nBanUntil);
READWRITE(banReason);
}
void SetNull()
{
nVersion = CBanEntry::CURRENT_VERSION;
nCreateTime = 0;
nBanUntil = 0;
banReason = BanReasonUnknown;
}
std::string banReasonToString()
{
switch (banReason) {
case BanReasonNodeMisbehaving:
2016-02-04 17:15:20 -06:00
return "node misbehaving";
case BanReasonManuallyAdded:
return "manually added";
default:
return "unknown";
}
}
};
typedef std::map<CSubNet, CBanEntry> banmap_t;
2010-08-29 16:58:15 +00:00
2012-03-26 16:48:23 +02:00
/** Information about a peer */
2010-08-29 16:58:15 +00:00
class CNode
{
public:
// socket
2013-04-13 00:13:08 -05:00
uint64_t nServices;
2010-08-29 16:58:15 +00:00
SOCKET hSocket;
CDataStream ssSend;
size_t nSendSize; // total size of all vSendMsg entries
size_t nSendOffset; // offset inside the first vSendMsg already sent
2013-04-13 00:13:08 -05:00
uint64_t nSendBytes;
2019-08-24 15:01:22 +02:00
std::deque<std::vector<char>> vSendMsg;
2010-08-29 16:58:15 +00:00
CCriticalSection cs_vSend;
std::deque<CInv> vRecvGetData;
2013-03-01 01:41:28 +01:00
std::deque<CNetMessage> vRecvMsg;
CCriticalSection cs_vRecvMsg;
2013-04-13 00:13:08 -05:00
uint64_t nRecvBytes;
int nRecvVersion;
2013-04-13 00:13:08 -05:00
int64_t nLastSend;
int64_t nLastRecv;
int64_t nTimeConnected;
2014-12-15 11:06:15 +01:00
int64_t nTimeOffset;
2010-08-29 16:58:15 +00:00
CAddress addr;
std::string addrName;
2012-05-10 20:35:13 +02:00
CService addrLocal;
2010-08-29 16:58:15 +00:00
int nVersion;
2014-01-11 18:14:29 +01:00
// strSubVer is whatever byte array we read from the wire. However, this field is intended
2013-11-26 12:52:21 +01:00
// to be printed out, displayed to humans in various forms and so on. So we sanitize it and
// store the sanitized version in cleanSubVer. The original should be used when dealing with
// the network or wire types and the cleaned string used when displayed or logged.
std::string strSubVer, cleanSubVer;
2014-06-21 13:34:36 +02:00
bool fWhitelisted; // This peer can bypass DoS banning.
bool fOneShot;
2010-08-29 16:58:15 +00:00
bool fClient;
bool fInbound;
bool fAutoOutbound; // any outbound node not connected with -addnode, connect-thinblock or -connect
bool fNetworkNode; // any outbound node
2010-08-29 16:58:15 +00:00
bool fSuccessfullyConnected;
bool fDisconnect;
// We use fRelayTxes for two purposes -
// a) it allows us to not relay tx invs before receiving the peer's version message
2015-04-28 14:48:28 +00:00
// b) the peer may tell us in its version message that we should not relay tx invs
2015-11-14 05:12:31 -08:00
// unless it loads a bloom filter.
bool fRelayTxes;
bool fSentAddr;
CSemaphoreGrant grantOutbound;
CCriticalSection cs_filter;
CBloomFilter* pfilter;
2016-04-25 14:36:03 +01:00
CBloomFilter* pThinBlockFilter;
2010-08-29 16:58:15 +00:00
int nRefCount;
2013-11-18 01:25:17 +01:00
NodeId id;
2016-01-15 21:37:25 -08:00
2016-07-06 20:16:18 +02:00
// Xtreme Thinblocks: begin section
2021-11-02 09:36:09 +01:00
MutableBlock thinBlock;
2016-01-15 21:37:25 -08:00
std::vector<uint64_t> xThinBlockHashes;
2016-07-06 20:16:18 +02:00
#ifdef LOG_XTHINBLOCKS
int nSizeThinBlock; // Original on-wire size of the block. Just used for reporting
2016-07-06 20:16:18 +02:00
#endif
2016-01-15 21:37:25 -08:00
int thinBlockWaitingForTxns; // if -1 then not currently waiting
std::map<uint256, uint64_t> mapThinBlocksInFlight; // map of the hashes of thin blocks in flight with the time they were requested.
double nGetXBlockTxCount; // Count how many get_xblocktx requests are made
uint64_t nGetXBlockTxLastTime; // The last time a get_xblocktx request was made
2016-07-06 20:16:18 +02:00
// Xtreme Thinblocks: end section
2016-01-15 21:37:25 -08:00
2017-02-08 17:28:21 +01:00
uint16_t addrFromPort;
2013-03-29 00:43:31 +01:00
protected:
2011-09-06 16:09:04 -04:00
// Denial-of-service detection/prevention
2012-07-26 00:48:39 +00:00
// Key is IP address, value is banned-until-time
static banmap_t setBanned;
2011-09-06 16:09:04 -04:00
static CCriticalSection cs_setBanned;
2015-06-19 15:27:37 +02:00
static bool setBannedIsDirty;
2011-09-06 16:09:04 -04:00
2014-06-21 13:34:36 +02:00
// Whitelisted ranges. Any node connecting from these is automatically
// whitelisted (as well as those connecting to whitelisted binds).
static std::vector<CSubNet> vWhitelistedRange;
static CCriticalSection cs_vWhitelistedRange;
// Basic fuzz-testing
void Fuzz(int nChance); // modifies ssSend
2010-08-29 16:58:15 +00:00
public:
uint256 hashContinue;
int nStartingHeight;
// flood relay
2011-05-15 09:11:04 +02:00
std::vector<CAddress> vAddrToSend;
CRollingBloomFilter addrKnown;
2010-08-29 16:58:15 +00:00
bool fGetAddr;
2011-05-15 09:11:04 +02:00
std::set<uint256> setKnown;
int64_t nNextAddrSend;
int64_t nNextLocalAddrSend;
2010-08-29 16:58:15 +00:00
// inventory based relay
CRollingBloomFilter filterInventoryKnown;
2011-05-15 09:11:04 +02:00
std::vector<CInv> vInventoryToSend;
2010-08-29 16:58:15 +00:00
CCriticalSection cs_inventory;
std::set<uint256> setAskFor;
2013-04-13 00:13:08 -05:00
std::multimap<int64_t, CInv> mapAskFor;
int64_t nNextInvSend;
2014-11-18 22:16:32 +01:00
// Used for headers announcements - unfiltered blocks to relay
// Also protected by cs_inventory
std::vector<uint256> vBlockHashesToAnnounce;
2010-08-29 16:58:15 +00:00
// Ping time measurement:
// The pong reply we're expecting, or 0 if no pong expected.
2013-04-13 00:13:08 -05:00
uint64_t nPingNonceSent;
// Time (in usec) the last ping was sent, or 0 if no ping was ever sent.
2013-04-13 00:13:08 -05:00
int64_t nPingUsecStart;
// Last measured round-trip time.
2013-04-13 00:13:08 -05:00
int64_t nPingUsecTime;
2015-08-13 02:31:46 -07:00
// Best measured round-trip time.
int64_t nMinPingUsecTime;
// Whether a ping is requested.
2013-08-22 04:34:33 -07:00
bool fPingQueued;
2014-01-11 18:14:29 +01:00
2015-05-31 15:36:44 +02:00
CNode(SOCKET hSocketIn, const CAddress &addrIn, const std::string &addrNameIn = "", bool fInboundIn = false);
~CNode();
2010-08-29 16:58:15 +00:00
private:
2013-08-23 02:09:32 +10:00
// Network usage totals
static CCriticalSection cs_totalBytesRecv;
static CCriticalSection cs_totalBytesSent;
2013-04-13 00:13:08 -05:00
static uint64_t nTotalBytesRecv;
static uint64_t nTotalBytesSent;
2013-08-23 02:09:32 +10:00
2015-09-02 17:03:27 +02:00
// outbound limit & stats
static uint64_t nMaxOutboundTotalBytesSentInCycle;
static uint64_t nMaxOutboundCycleStartTime;
static uint64_t nMaxOutboundLimit;
static uint64_t nMaxOutboundTimeframe;
2010-08-29 16:58:15 +00:00
CNode(const CNode&);
void operator=(const CNode&);
2013-08-23 02:09:32 +10:00
2010-08-29 16:58:15 +00:00
public:
2013-11-18 01:25:17 +01:00
NodeId GetId() const {
return id;
}
2010-08-29 16:58:15 +00:00
int GetRefCount()
{
2013-03-29 00:43:31 +01:00
assert(nRefCount >= 0);
return nRefCount;
2010-08-29 16:58:15 +00:00
}
// requires LOCK(cs_vRecvMsg)
unsigned int GetTotalRecvSize()
{
unsigned int total = 0;
2018-12-30 15:33:11 +01:00
for (const CNetMessage &msg : vRecvMsg)
2013-03-01 01:41:28 +01:00
total += msg.vRecv.size() + 24;
return total;
}
// requires LOCK(cs_vRecvMsg)
bool ReceiveMsgBytes(const char *pch, unsigned int nBytes);
// requires LOCK(cs_vRecvMsg)
void SetRecvVersion(int nVersionIn)
{
nRecvVersion = nVersionIn;
2018-12-30 15:33:11 +01:00
for (CNetMessage &msg : vRecvMsg)
2013-03-01 01:41:28 +01:00
msg.SetVersion(nVersionIn);
}
2013-03-29 00:43:31 +01:00
CNode* AddRef()
2010-08-29 16:58:15 +00:00
{
2013-03-29 00:43:31 +01:00
nRefCount++;
2010-08-29 16:58:15 +00:00
return this;
}
void Release()
{
nRefCount--;
}
bool ThinBlockCapable()
{
2016-04-25 14:36:03 +01:00
return (nServices & NODE_XTHIN);
}
2010-08-29 16:58:15 +00:00
void AddAddressKnown(const CAddress& addr)
{
addrKnown.insert(addr.GetKey());
2010-08-29 16:58:15 +00:00
}
void PushAddress(const CAddress& addr)
{
// Known checking here is only to save space from duplicates.
// SendMessages will filter it again for knowns that were added
// after addresses were pushed.
if (addr.IsValid() && !addrKnown.contains(addr.GetKey())) {
if (vAddrToSend.size() >= MAX_ADDR_TO_SEND) {
vAddrToSend[insecure_rand() % vAddrToSend.size()] = addr;
} else {
vAddrToSend.push_back(addr);
}
}
2010-08-29 16:58:15 +00:00
}
void AddInventoryKnown(const CInv& inv)
{
2012-04-06 18:39:12 +02:00
{
LOCK(cs_inventory);
filterInventoryKnown.insert(inv.hash);
2012-04-06 18:39:12 +02:00
}
2010-08-29 16:58:15 +00:00
}
void PushInventory(const CInv& inv)
{
2012-04-06 18:39:12 +02:00
{
LOCK(cs_inventory);
if (inv.type == MSG_TX && filterInventoryKnown.contains(inv.hash))
return;
vInventoryToSend.push_back(inv);
2012-04-06 18:39:12 +02:00
}
2010-08-29 16:58:15 +00:00
}
2014-11-18 22:16:32 +01:00
void PushBlockHash(const uint256 &hash)
{
LOCK(cs_inventory);
vBlockHashesToAnnounce.push_back(hash);
}
void AskFor(const CInv& inv);
2010-08-29 16:58:15 +00:00
2012-11-10 23:51:50 -03:00
// TODO: Document the postcondition of this function. Is cs_vSend locked?
void BeginMessage(const char* pszCommand) EXCLUSIVE_LOCK_FUNCTION(cs_vSend);
2010-08-29 16:58:15 +00:00
2012-11-10 23:51:50 -03:00
// TODO: Document the precondition of this function. Is cs_vSend locked?
void AbortMessage() UNLOCK_FUNCTION(cs_vSend);
2010-08-29 16:58:15 +00:00
2012-11-10 23:51:50 -03:00
// TODO: Document the precondition of this function. Is cs_vSend locked?
void EndMessage() UNLOCK_FUNCTION(cs_vSend);
2010-08-29 16:58:15 +00:00
void PushVersion();
2011-01-24 10:42:17 -05:00
2010-08-29 16:58:15 +00:00
void PushMessage(const char* pszCommand)
{
try
{
BeginMessage(pszCommand);
EndMessage();
}
catch (...)
{
AbortMessage();
throw;
}
}
template<typename T1>
void PushMessage(const char* pszCommand, const T1& a1)
{
try
{
BeginMessage(pszCommand);
ssSend << a1;
2010-08-29 16:58:15 +00:00
EndMessage();
}
catch (...)
{
AbortMessage();
throw;
}
}
template<typename T1, typename T2>
void PushMessage(const char* pszCommand, const T1& a1, const T2& a2)
{
try
{
BeginMessage(pszCommand);
ssSend << a1 << a2;
2010-08-29 16:58:15 +00:00
EndMessage();
}
catch (...)
{
AbortMessage();
throw;
}
}
template<typename T1, typename T2, typename T3>
void PushMessage(const char* pszCommand, const T1& a1, const T2& a2, const T3& a3)
{
try
{
BeginMessage(pszCommand);
ssSend << a1 << a2 << a3;
2010-08-29 16:58:15 +00:00
EndMessage();
}
catch (...)
{
AbortMessage();
throw;
}
}
template<typename T1, typename T2, typename T3, typename T4>
void PushMessage(const char* pszCommand, const T1& a1, const T2& a2, const T3& a3, const T4& a4)
{
try
{
BeginMessage(pszCommand);
ssSend << a1 << a2 << a3 << a4;
2010-08-29 16:58:15 +00:00
EndMessage();
}
catch (...)
{
AbortMessage();
throw;
}
}
template<typename T1, typename T2, typename T3, typename T4, typename T5>
void PushMessage(const char* pszCommand, const T1& a1, const T2& a2, const T3& a3, const T4& a4, const T5& a5)
{
try
{
BeginMessage(pszCommand);
ssSend << a1 << a2 << a3 << a4 << a5;
2010-08-29 16:58:15 +00:00
EndMessage();
}
catch (...)
{
AbortMessage();
throw;
}
}
template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
void PushMessage(const char* pszCommand, const T1& a1, const T2& a2, const T3& a3, const T4& a4, const T5& a5, const T6& a6)
{
try
{
BeginMessage(pszCommand);
ssSend << a1 << a2 << a3 << a4 << a5 << a6;
2010-08-29 16:58:15 +00:00
EndMessage();
}
catch (...)
{
AbortMessage();
throw;
}
}
template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7>
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)
{
try
{
BeginMessage(pszCommand);
ssSend << a1 << a2 << a3 << a4 << a5 << a6 << a7;
2010-08-29 16:58:15 +00:00
EndMessage();
}
catch (...)
{
AbortMessage();
throw;
}
}
template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8>
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)
{
try
{
BeginMessage(pszCommand);
ssSend << a1 << a2 << a3 << a4 << a5 << a6 << a7 << a8;
2010-08-29 16:58:15 +00:00
EndMessage();
}
catch (...)
{
AbortMessage();
throw;
}
}
template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9>
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)
{
try
{
BeginMessage(pszCommand);
ssSend << a1 << a2 << a3 << a4 << a5 << a6 << a7 << a8 << a9;
2010-08-29 16:58:15 +00:00
EndMessage();
}
catch (...)
{
AbortMessage();
throw;
}
}
void CloseSocketDisconnect();
2011-09-06 16:09:04 -04:00
// Denial-of-service detection/prevention
// The idea is to detect peers that are behaving
// badly and disconnect/ban them, but do it in a
// one-coding-mistake-won't-shatter-the-entire-network
// way.
// IMPORTANT: There should be nothing I can give a
// node that it will forward on that will make that
// node's peers drop it. If there is, an attacker
// can isolate a node and/or try to split the network.
// Dropping a node for sending stuff that is invalid
// now but might be valid in a later version is also
// dangerous, because it can cause a network split
// between nodes running old code and nodes running
// new code.
static void ClearBanned(); // needed for unit testing
2012-01-03 23:33:31 +01:00
static bool IsBanned(CNetAddr ip);
2015-05-25 20:03:51 +02:00
static bool IsBanned(CSubNet subnet);
static void Ban(const CNetAddr &ip, const BanReason &banReason, int64_t bantimeoffset = 0, bool sinceUnixEpoch = false);
static void Ban(const CSubNet &subNet, const BanReason &banReason, int64_t bantimeoffset = 0, bool sinceUnixEpoch = false);
static bool Unban(const CNetAddr &ip);
2015-05-25 20:03:51 +02:00
static bool Unban(const CSubNet &ip);
static void GetBanned(banmap_t &banmap);
static void SetBanned(const banmap_t &banmap);
2015-06-19 15:27:37 +02:00
//!check is the banlist has unwritten changes
static bool BannedSetIsDirty();
//!set the "dirty" flag for the banlist
static void SetBannedSetDirty(bool dirty=true);
2015-08-09 00:17:27 +01:00
//!clean unused entries (if bantime has expired)
2015-06-19 15:27:37 +02:00
static void SweepBanned();
void copyStats(CNodeStats &stats);
2013-08-23 02:09:32 +10:00
2014-06-21 13:34:36 +02:00
static bool IsWhitelistedRange(const CNetAddr &ip);
static void AddWhitelistedRange(const CSubNet &subnet);
2013-08-23 02:09:32 +10:00
// Network stats
2013-04-13 00:13:08 -05:00
static void RecordBytesRecv(uint64_t bytes);
static void RecordBytesSent(uint64_t bytes);
2013-08-23 02:09:32 +10:00
2013-04-13 00:13:08 -05:00
static uint64_t GetTotalBytesRecv();
static uint64_t GetTotalBytesSent();
2015-09-02 17:03:27 +02:00
//!set the max outbound target in bytes
static void SetMaxOutboundTarget(uint64_t limit);
static uint64_t GetMaxOutboundTarget();
//!set the timeframe for the max outbound target
static void SetMaxOutboundTimeframe(uint64_t timeframe);
static uint64_t GetMaxOutboundTimeframe();
//!check if the outbound target is reached
// if param historicalBlockServingLimit is set true, the function will
// response true if the limit for serving historical blocks has been reached
static bool OutboundTargetReached(bool historicalBlockServingLimit);
//!response the bytes left in the current max outbound cycle
// in case of no limit, it will always response 0
static uint64_t GetOutboundTargetBytesLeft();
//!response the time in second left in the current max outbound cycle
// in case of no limit, it will always response 0
static uint64_t GetMaxOutboundTimeLeftInCycle();
2010-08-29 16:58:15 +00:00
};
2012-08-13 05:26:30 +02:00
class CTransaction;
void RelayTransaction(const CTransaction& tx);
void RelayTransaction(const CTransaction& tx, const CDataStream& ss);
2010-08-29 16:58:15 +00:00
2013-11-29 16:33:34 +01:00
/** Access to the (IP) address database (peers.dat) */
class CAddrDB
{
private:
boost::filesystem::path pathAddr;
public:
CAddrDB();
2017-08-01 13:07:18 +02:00
CAddrDB(const boost::filesystem::path &peersFilename);
2013-11-29 16:33:34 +01:00
bool Write(const CAddrMan& addr);
bool Read(CAddrMan& addr);
bool Read(CAddrMan& addr, CDataStream& ssPeers);
2013-11-29 16:33:34 +01:00
};
2015-06-19 15:27:37 +02:00
/** Access to the banlist database (banlist.dat) */
class CBanDB
{
private:
boost::filesystem::path pathBanlist;
public:
CBanDB();
2017-08-01 13:07:18 +02:00
CBanDB(const boost::filesystem::path &banlistFilename);
bool Write(const banmap_t& banSet);
bool Read(banmap_t& banSet);
2015-06-19 15:27:37 +02:00
};
void DumpBanlist();
/** Return a timestamp in the future (in microseconds) for exponentially distributed events. */
int64_t PoissonNextSend(int64_t nNow, int average_interval_seconds);
2018-01-16 10:47:52 +00:00
#endif