30 #include <condition_variable>
33 #include <arpa/inet.h>
36 #include <boost/filesystem/path.hpp>
37 #include <boost/foreach.hpp>
38 #include <boost/signals2/signal.hpp>
49 static const int PING_INTERVAL = 2 * 60;
51 static const int TIMEOUT_INTERVAL = 20 * 60;
53 static const int FEELER_INTERVAL = 120;
55 static const unsigned int MAX_INV_SZ = 50000;
57 static const unsigned int MAX_ADDR_TO_SEND = 1000;
59 static const unsigned int MAX_PROTOCOL_MESSAGE_LENGTH = 4 * 1000 * 1000;
61 static const unsigned int MAX_SUBVERSION_LENGTH = 256;
63 static const int MAX_OUTBOUND_CONNECTIONS = 8;
65 static const int MAX_ADDNODE_CONNECTIONS = 8;
67 static const bool DEFAULT_LISTEN =
true;
70 static const bool DEFAULT_UPNP = USE_UPNP;
72 static const bool DEFAULT_UPNP =
false;
75 static const size_t MAPASKFOR_MAX_SZ = MAX_INV_SZ;
77 static const size_t SETASKFOR_MAX_SZ = 2 * MAX_INV_SZ;
79 static const unsigned int DEFAULT_MAX_PEER_CONNECTIONS = 125;
81 static const uint64_t DEFAULT_MAX_UPLOAD_TARGET = 0;
83 static const uint64_t MAX_UPLOAD_TIMEFRAME = 60 * 60 * 24;
85 static const bool DEFAULT_BLOCKSONLY =
false;
87 static const bool DEFAULT_FORCEDNSSEED =
false;
88 static const size_t DEFAULT_MAXRECEIVEBUFFER = 5 * 1000;
89 static const size_t DEFAULT_MAXSENDBUFFER = 1 * 1000;
94 static const unsigned int DEFAULT_MISBEHAVING_BANTIME = 60 * 60 * 24;
119 std::vector<unsigned char>
data;
150 CConnman(uint64_t seed0, uint64_t seed1);
165 template<
typename Callable>
169 for (
auto&& node :
vNodes) {
175 template<
typename Callable>
179 for (
auto&& node :
vNodes) {
185 template<
typename Callable,
typename CallableAfter>
189 for (
auto&& node :
vNodes) {
196 template<
typename Callable,
typename CallableAfter>
200 for (
auto&& node :
vNodes) {
230 void Ban(
const CNetAddr& netAddr,
const BanReason& reason, int64_t bantimeoffset = 0,
bool sinceUnixEpoch =
false);
231 void Ban(
const CSubNet& subNet,
const BanReason& reason, int64_t bantimeoffset = 0,
bool sinceUnixEpoch =
false);
242 bool AddNode(
const std::string& node);
408 extern std::unique_ptr<CConnman>
g_connman;
409 void Discover(boost::thread_group& threadGroup);
421 while (first != last) {
422 if (!(*first))
return false;
484 extern std::map<CNetAddr, LocalServiceInfo>
mapLocalHost;
557 int readHeader(
const char *pch,
unsigned int nBytes);
558 int readData(
const char *pch,
unsigned int nBytes);
687 CNode(
NodeId id,
ServiceFlags nLocalServicesIn,
int nMyStartingHeightIn,
SOCKET hSocketIn,
const CAddress &addrIn, uint64_t nKeyedNetGroupIn, uint64_t nLocalHostNonceIn,
const std::string &addrNameIn =
"",
bool fInboundIn =
false);
727 bool ReceiveMsgBytes(
const char *pch,
unsigned int nBytes,
bool& complete);
std::map< CSubNet, CBanEntry > banmap_t
int64_t CAmount
Amount in satoshis (Can be negative)
Stochastical (IP) address manager.
A CService with information about it as peer.
An alert is a combination of a serialized CUnsignedAlert and a signature.
BloomFilter is a probabilistic filter which SPV clients provide so that we can filter the transaction...
Signals for UI communication.
std::condition_variable condMsgProc
void AddWhitelistedRange(const CSubNet &subnet)
uint64_t nMaxOutboundLimit
void SweepBanned()
clean unused entries (if bantime has expired)
std::thread threadMessageHandler
void ThreadOpenAddedConnections()
int GetBestHeight() const
bool ForNode(NodeId id, std::function< bool(CNode *pnode)> func)
size_t SocketSendData(CNode *pnode) const
CCriticalSection cs_vNodes
void DeleteNode(CNode *pnode)
bool AttemptToEvictConnection()
Try to find a connection to evict when the node is full.
bool GetNetworkActive() const
void ForEachNodeThen(Callable &&pre, CallableAfter &&post) const
ServiceFlags nLocalServices
Services this instance offers.
size_t GetNodeCount(NumConnections num)
std::atomic< bool > flagInterruptMsgProc
unsigned int GetReceiveFloodSize() const
std::list< CNode * > vNodesDisconnected
uint64_t GetTotalBytesRecv()
void SetBestHeight(int height)
CCriticalSection cs_totalBytesRecv
void ForEachNodeThen(Callable &&pre, CallableAfter &&post)
void AddNewAddress(const CAddress &addr, const CAddress &addrFrom, int64_t nTimePenalty=0)
CThreadInterrupt interruptNet
uint64_t GetMaxOutboundTimeframe()
std::atomic< NodeId > nLastNodeId
void SetMaxOutboundTimeframe(uint64_t timeframe)
set the timeframe for the max outbound target
void SetServices(const CService &addr, ServiceFlags nServices)
uint64_t GetOutboundTargetBytesLeft()
response the bytes left in the current max outbound cycle
size_t GetAddressCount() const
void AddNewAddresses(const std::vector< CAddress > &vAddr, const CAddress &addrFrom, int64_t nTimePenalty=0)
void RecordBytesSent(uint64_t bytes)
bool OpenNetworkConnection(const CAddress &addrConnect, bool fCountFailure, CSemaphoreGrant *grantOutbound=NULL, const char *strDest=NULL, bool fOneShot=false, bool fFeeler=false, bool fAddnode=false)
std::vector< CAddress > GetAddresses()
std::thread threadDNSAddressSeed
bool Unban(const CNetAddr &ip)
CCriticalSection cs_totalBytesSent
std::deque< std::string > vOneShots
void SetBannedSetDirty(bool dirty=true)
set the "dirty" flag for the banlist
ServiceFlags GetLocalServices() const
bool DisconnectNode(const std::string &node)
uint64_t nMaxOutboundTimeframe
ServiceFlags nRelevantServices
Services this instance cares about.
std::vector< ListenSocket > vhListenSocket
CClientUIInterface * clientInterface
CSipHasher GetDeterministicRandomizer(uint64_t id) const
Get a unique deterministic randomizer.
void GetBanned(banmap_t &banmap)
CCriticalSection cs_setBanned
void ThreadSocketHandler()
std::thread threadOpenConnections
void ForEachNode(Callable &&func) const
bool fMsgProcWake
flag for waking the message processor.
CNode * FindNode(const CNetAddr &ip)
bool Start(CScheduler &scheduler, std::string &strNodeError, Options options)
unsigned int GetSendBufferSize() const
void MarkAddressGood(const CAddress &addr)
const uint64_t nSeed0
SipHasher seeds for deterministic randomness.
unsigned int nReceiveFloodSize
uint64_t nMaxOutboundTotalBytesSentInCycle
uint64_t nMaxOutboundCycleStartTime
static bool NodeFullyConnected(const CNode *pnode)
void SetMaxOutboundTarget(uint64_t limit)
set the max outbound target in bytes
void WakeMessageHandler()
void SetNetworkActive(bool active)
uint64_t GetMaxOutboundTarget()
bool IsBanned(CNetAddr ip)
bool OutboundTargetReached(bool historicalBlockServingLimit)
check if the outbound target is reached
void ThreadDNSAddressSeed()
std::vector< CSubNet > vWhitelistedRange
void ThreadMessageHandler()
uint64_t CalculateKeyedNetGroup(const CAddress &ad) const
bool fAddressesInitialized
uint64_t GetTotalBytesSent()
uint64_t GetMaxOutboundTimeLeftInCycle()
response the time in second left in the current max outbound cycle
std::thread threadOpenAddedConnections
void ThreadOpenConnections()
void Ban(const CNetAddr &netAddr, const BanReason &reason, int64_t bantimeoffset=0, bool sinceUnixEpoch=false)
bool BannedSetIsDirty()
check is the banlist has unwritten changes
CNode * ConnectNode(CAddress addrConnect, const char *pszDest, bool fCountFailure)
std::atomic< int > nBestHeight
bool CheckIncomingNonce(uint64_t nonce)
bool BindListenPort(const CService &bindAddr, std::string &strError, bool fWhitelisted=false)
void RecordBytesRecv(uint64_t bytes)
std::vector< AddedNodeInfo > GetAddedNodeInfo()
void GetNodeStats(std::vector< CNodeStats > &vstats)
std::vector< std::string > vAddedNodes
void PushMessage(CNode *pnode, CSerializedNetMsg &&msg)
std::vector< CNode * > vNodes
CCriticalSection cs_vWhitelistedRange
unsigned int nSendBufferMaxSize
void SetBanned(const banmap_t &banmap)
std::atomic< bool > fNetworkActive
void ForEachNode(Callable &&func)
CConnman(uint64_t seed0, uint64_t seed1)
CCriticalSection cs_vOneShots
CCriticalSection cs_vAddedNodes
bool AddNode(const std::string &node)
std::thread threadSocketHandler
void AddressCurrentlyConnected(const CService &addr)
bool RemoveAddedNode(const std::string &node)
void AddOneShot(const std::string &strDest)
bool IsWhitelistedRange(const CNetAddr &addr)
void AcceptConnection(const ListenSocket &hListenSocket)
Wrapped boost mutex: supports recursive locking, but no waiting TODO: We should move away from using ...
Double ended buffer combining vector and stream-like interfaces.
void resize(size_type n, value_type c=0)
A hasher class for Bitcoin's 256-bit hash (double SHA-256).
IP address (IPv6, or IPv4 using mapped IPv6 range (::FFFF:0:0/96))
CNetMessage(const CMessageHeader::MessageStartChars &pchMessageStartIn, int nTypeIn, int nVersionIn)
int readHeader(const char *pch, unsigned int nBytes)
const uint256 & GetMessageHash() const
void SetVersion(int nVersionIn)
int readData(const char *pch, unsigned int nBytes)
Information about a peer.
std::atomic< int64_t > nLastSend
CCriticalSection cs_sendProcessing
CRollingBloomFilter filterInventoryKnown
std::atomic< int > nVersion
void SetSendVersion(int nVersionIn)
std::vector< uint256 > vInventoryBlockToSend
std::deque< std::vector< unsigned char > > vSendMsg
CCriticalSection cs_vRecv
std::atomic_bool fPauseRecv
std::atomic< int64_t > nTimeOffset
CCriticalSection cs_inventory
void PushAddress(const CAddress &_addr, FastRandomContext &insecure_rand)
void PushAlertHash(const uint256 &hash)
std::atomic< bool > fPingQueued
int GetSendVersion() const
CCriticalSection cs_feeFilter
std::list< CNetMessage > vRecvMsg
mapMsgCmdSize mapRecvBytesPerMsgCmd
void SetAddrLocal(const CService &addrLocalIn)
May not be called more than once.
std::vector< CAlert > vAlertToSend
std::atomic_bool fSuccessfullyConnected
ServiceFlags nServicesExpected
uint64_t GetLocalNonce() const
std::atomic< ServiceFlags > nServices
CRollingBloomFilter addrKnown
CCriticalSection cs_hSocket
CSemaphoreGrant grantOutbound
const int64_t nTimeConnected
std::string GetAddrName() const
mapMsgCmdSize mapSendBytesPerMsgCmd
const uint64_t nKeyedNetGroup
std::atomic< int > nRefCount
int GetMyStartingHeight() const
void CloseSocketDisconnect()
CCriticalSection cs_filter
CCriticalSection cs_addrName
std::atomic< int > nStartingHeight
void PushAlert(const CAlert &_alert)
std::atomic_bool fPauseSend
std::multimap< int64_t, CInv > mapAskFor
CCriticalSection cs_vSend
void PushInventory(const CInv &inv)
const ServiceFlags nLocalServices
bool ReceiveMsgBytes(const char *pch, unsigned int nBytes, bool &complete)
CCriticalSection cs_vProcessMsg
CCriticalSection cs_addrLocal
int64_t nextSendTimeFeeFilter
void MaybeSetAddrName(const std::string &addrNameIn)
Sets the addrName only if it was not previously set.
void operator=(const CNode &)
std::atomic< int64_t > timeLastMempoolReq
void AddAddressKnown(const CAddress &_addr)
std::atomic< int64_t > nLastRecv
void SetRecvVersion(int nVersionIn)
std::deque< CInv > vRecvGetData
std::atomic< int64_t > nLastTXTime
std::atomic< int64_t > nMinPingUsecTime
std::atomic< uint64_t > nPingNonceSent
std::vector< CAddress > vAddrToSend
std::set< uint256 > setAskFor
CAmount lastSentFeeFilter
const int nMyStartingHeight
const uint64_t nLocalHostNonce
std::atomic< int64_t > nPingUsecStart
void PushBlockHash(const uint256 &hash)
void copyStats(CNodeStats &stats)
ServiceFlags GetLocalServices() const
CNode(NodeId id, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn, SOCKET hSocketIn, const CAddress &addrIn, uint64_t nKeyedNetGroupIn, uint64_t nLocalHostNonceIn, const std::string &addrNameIn="", bool fInboundIn=false)
std::set< uint256 > setKnown
std::atomic< int64_t > nPingUsecTime
void AddInventoryKnown(const CInv &inv)
std::list< CNetMessage > vProcessMsg
CCriticalSection cs_SubVer
std::atomic< int64_t > nLastBlockTime
std::vector< uint256 > vBlockHashesToAnnounce
std::atomic_bool fDisconnect
void AskFor(const CInv &inv)
CService GetAddrLocal() const
std::set< uint256 > setInventoryTxToSend
std::atomic< int > nRecvVersion
int64_t nNextLocalAddrSend
mapMsgCmdSize mapSendBytesPerMsgCmd
mapMsgCmdSize mapRecvBytesPerMsgCmd
RollingBloomFilter is a probabilistic "keep track of most recently inserted" set.
void insert(const std::vector< unsigned char > &vKey)
bool contains(const std::vector< unsigned char > &vKey) const
RAII-style semaphore lock.
A combination of a network address (CNetAddr) and a (TCP) port.
std::vector< unsigned char > GetKey() const
The basic transaction that is broadcasted on the network and contained in blocks.
STL-like map container that only keeps the N elements with the highest value.
CAddress GetLocalAddress(const CNetAddr *paddrPeer, ServiceFlags nLocalServices)
bool RemoveLocal(const CService &addr)
bool IsPeerAddrLocalGood(CNode *pnode)
std::map< CNetAddr, LocalServiceInfo > mapLocalHost
void AdvertiseLocal(CNode *pnode)
std::unique_ptr< CConnman > g_connman
bool IsLocal(const CService &addr)
check whether a given address is potentially local
bool AddLocal(const CService &addr, int nScore=LOCAL_NONE)
CNodeSignals & GetNodeSignals()
CCriticalSection cs_mapLocalHost
int64_t PoissonNextSend(int64_t nNow, int average_interval_seconds)
Return a timestamp in the future (in microseconds) for exponentially distributed events.
std::string strSubVersion
Subversion as sent to the P2P network in version messages.
limitedmap< uint256, int64_t > mapAlreadyAskedFor
void Discover(boost::thread_group &threadGroup)
bool BindListenPort(const CService &bindAddr, std::string &strError, bool fWhitelisted=false)
bool IsLimited(enum Network net)
void MapPort(bool fUseUPnP)
std::map< std::string, uint64_t > mapMsgCmdSize
bool GetLocal(CService &addr, const CNetAddr *paddrPeer=NULL)
void SetLimited(enum Network net, bool fLimited=true)
Make a particular network entirely off-limits (no automatic connects to it)
unsigned short GetListenPort()
bool IsReachable(enum Network net)
check whether a given network is one we can probably connect to
bool SeenLocal(const CService &addr)
vote for a local address
ServiceFlags
nServices flags
ListenSocket(SOCKET socket_, bool whitelisted_)
unsigned int nReceiveFloodSize
uint64_t nMaxOutboundLimit
CClientUIInterface * uiInterface
uint64_t nMaxOutboundTimeframe
ServiceFlags nLocalServices
ServiceFlags nRelevantServices
unsigned int nSendBufferMaxSize
boost::signals2::signal< void(CNode *, CConnman &)> InitializeNode
boost::signals2::signal< bool(CNode *, CConnman &, std::atomic< bool > &), CombinerAll > ProcessMessages
boost::signals2::signal< bool(CNode *, CConnman &, std::atomic< bool > &), CombinerAll > SendMessages
boost::signals2::signal< void(NodeId, bool &)> FinalizeNode
CSerializedNetMsg(const CSerializedNetMsg &msg)=delete
CSerializedNetMsg & operator=(const CSerializedNetMsg &)=delete
CSerializedNetMsg()=default
CSerializedNetMsg(CSerializedNetMsg &&)=default
CSerializedNetMsg & operator=(CSerializedNetMsg &&)=default
std::vector< unsigned char > data
bool operator()(I first, I last) const