31 #include <condition_variable> 34 #include <arpa/inet.h> 46 static const int PING_INTERVAL = 2 * 60;
48 static const int TIMEOUT_INTERVAL = 20 * 60;
50 static const int FEELER_INTERVAL = 120;
52 static const unsigned int MAX_INV_SZ = 50000;
54 static const unsigned int MAX_ASSET_INV_SZ = 1024;
56 static const unsigned int MAX_ADDR_TO_SEND = 1000;
58 static const unsigned int MAX_PROTOCOL_MESSAGE_LENGTH = 4 * 1000 * 1000;
60 static const unsigned int MAX_SUBVERSION_LENGTH = 256;
62 static const int MAX_OUTBOUND_CONNECTIONS = 8;
64 static const int MAX_ADDNODE_CONNECTIONS = 8;
66 static const bool DEFAULT_LISTEN =
true;
69 static const bool DEFAULT_UPNP =
USE_UPNP;
71 static const bool DEFAULT_UPNP =
false;
74 static const size_t MAPASKFOR_MAX_SZ = MAX_INV_SZ;
76 static const size_t SETASKFOR_MAX_SZ = 2 * MAX_INV_SZ;
78 static const unsigned int DEFAULT_MAX_PEER_CONNECTIONS = 125;
80 static const uint64_t DEFAULT_MAX_UPLOAD_TARGET = 0;
82 static const uint64_t MAX_UPLOAD_TIMEFRAME = 60 * 60 * 24;
84 static const bool DEFAULT_BLOCKSONLY =
false;
86 static const bool DEFAULT_FORCEDNSSEED =
true;
87 static const size_t DEFAULT_MAXRECEIVEBUFFER = 5 * 1000;
88 static const size_t DEFAULT_MAXSENDBUFFER = 1 * 1000;
91 static const unsigned int DEFAULT_MISBEHAVING_BANTIME = 60 * 60 * 24;
115 std::vector<unsigned char>
data;
134 int nMaxConnections = 0;
135 int nMaxOutbound = 0;
141 unsigned int nSendBufferMaxSize = 0;
142 unsigned int nReceiveFloodSize = 0;
143 uint64_t nMaxOutboundTimeframe = 0;
144 uint64_t nMaxOutboundLimit = 0;
148 bool m_use_addrman_outgoing =
true;
170 CConnman(uint64_t seed0, uint64_t seed1);
176 void SetNetworkActive(
bool active);
177 bool OpenNetworkConnection(
const CAddress& addrConnect,
bool fCountFailure,
CSemaphoreGrant *grantOutbound =
nullptr,
const char *strDest =
nullptr,
bool fOneShot =
false,
bool fFeeler =
false,
bool manual_connection =
false);
178 bool CheckIncomingNonce(uint64_t nonce);
180 bool ForNode(
NodeId id, std::function<
bool(
CNode* pnode)> func);
184 template<
typename Callable>
188 for (
auto&& node : vNodes) {
189 if (NodeFullyConnected(node))
194 template<
typename Callable>
198 for (
auto&& node : vNodes) {
199 if (NodeFullyConnected(node))
204 template<
typename Callable,
typename CallableAfter>
208 for (
auto&& node : vNodes) {
209 if (NodeFullyConnected(node))
215 template<
typename Callable,
typename CallableAfter>
219 for (
auto&& node : vNodes) {
220 if (NodeFullyConnected(node))
227 size_t GetAddressCount()
const;
229 void MarkAddressGood(
const CAddress& addr);
230 void AddNewAddresses(
const std::vector<CAddress>& vAddr,
const CAddress& addrFrom, int64_t nTimePenalty = 0);
231 std::vector<CAddress> GetAddresses();
247 void Ban(
const CNetAddr& netAddr,
const BanReason& reason, int64_t bantimeoffset = 0,
bool sinceUnixEpoch =
false);
248 void Ban(
const CSubNet& subNet,
const BanReason& reason, int64_t bantimeoffset = 0,
bool sinceUnixEpoch =
false);
255 void SetBanned(
const banmap_t &banmap);
259 void SetTryNewOutboundPeer(
bool flag);
260 bool GetTryNewOutboundPeer();
268 int GetExtraOutboundCount();
270 bool AddNode(
const std::string& node);
271 bool RemoveAddedNode(
const std::string& node);
272 std::vector<AddedNodeInfo> GetAddedNodeInfo();
275 void GetNodeStats(std::vector<CNodeStats>& vstats);
276 bool DisconnectNode(
const std::string& node);
277 bool DisconnectNode(
NodeId id);
282 void SetMaxOutboundTarget(uint64_t limit);
283 uint64_t GetMaxOutboundTarget();
286 void SetMaxOutboundTimeframe(uint64_t timeframe);
287 uint64_t GetMaxOutboundTimeframe();
292 bool OutboundTargetReached(
bool historicalBlockServingLimit);
296 uint64_t GetOutboundTargetBytesLeft();
300 uint64_t GetMaxOutboundTimeLeftInCycle();
302 uint64_t GetTotalBytesRecv();
303 uint64_t GetTotalBytesSent();
305 void SetBestHeight(
int height);
306 int GetBestHeight()
const;
309 CSipHasher GetDeterministicRandomizer(uint64_t
id)
const;
311 unsigned int GetReceiveFloodSize()
const;
313 void WakeMessageHandler();
324 bool InitBinds(
const std::vector<CService>& binds,
const std::vector<CService>& whiteBinds);
325 void ThreadOpenAddedConnections();
326 void AddOneShot(
const std::string& strDest);
327 void ProcessOneShot();
328 void ThreadOpenConnections();
329 void ThreadMessageHandler();
330 void AcceptConnection(
const ListenSocket& hListenSocket);
331 void ThreadSocketHandler();
332 void ThreadDNSAddressSeed();
334 uint64_t CalculateKeyedNetGroup(
const CAddress& ad)
const;
338 CNode* FindNode(
const std::string& addrName);
341 bool AttemptToEvictConnection();
342 CNode* ConnectNode(
CAddress addrConnect,
const char *pszDest,
bool fCountFailure);
343 bool IsWhitelistedRange(
const CNetAddr &addr);
345 void DeleteNode(
CNode* pnode);
349 size_t SocketSendData(
CNode *pnode)
const;
351 bool BannedSetIsDirty();
353 void SetBannedSetDirty(
bool dirty=
true);
356 void DumpAddresses();
361 void RecordBytesRecv(uint64_t bytes);
362 void RecordBytesSent(uint64_t bytes);
365 static bool NodeFullyConnected(
const CNode* pnode);
438 extern std::unique_ptr<CConnman>
g_connman;
439 void Discover(boost::thread_group& threadGroup);
451 while (first != last) {
452 if (!(*first))
return false;
465 virtual bool ProcessMessages(
CNode* pnode, std::atomic<bool>& interrupt) = 0;
466 virtual bool SendMessages(
CNode* pnode, std::atomic<bool>& interrupt) = 0;
467 virtual void InitializeNode(
CNode* pnode) = 0;
468 virtual void FinalizeNode(
NodeId id,
bool& update_connection_time) = 0;
513 extern std::map<CNetAddr, LocalServiceInfo>
mapLocalHost;
582 const uint256& GetMessageHash()
const;
590 int readHeader(
const char *pch,
unsigned int nBytes);
591 int readData(
const char *pch,
unsigned int nBytes);
722 CNode(
NodeId id,
ServiceFlags nLocalServicesIn,
int nMyStartingHeightIn,
SOCKET hSocketIn,
const CAddress &addrIn, uint64_t nKeyedNetGroupIn, uint64_t nLocalHostNonceIn,
const CAddress &addrBindIn,
const std::string &addrNameIn =
"",
bool fInboundIn =
false);
749 return nLocalHostNonce;
753 return nMyStartingHeight;
758 assert(nRefCount >= 0);
762 bool ReceiveMsgBytes(
const char *pch,
unsigned int nBytes,
bool& complete);
766 nRecvVersion = nVersionIn;
772 void SetSendVersion(
int nVersionIn);
773 int GetSendVersion()
const;
777 void SetAddrLocal(
const CService& addrLocalIn);
803 if (vAddrToSend.size() >= MAX_ADDR_TO_SEND) {
804 vAddrToSend[insecure_rand.
randrange(vAddrToSend.size())] = _addr;
806 vAddrToSend.push_back(_addr);
825 setInventoryTxToSend.insert(inv.
hash);
828 vInventoryBlockToSend.push_back(inv.
hash);
835 setInventoryAssetsSend.insert(name);
841 vBlockHashesToAnnounce.push_back(hash);
844 void AskFor(
const CInv& inv);
846 void CloseSocketDisconnect();
855 std::string GetAddrName()
const;
857 void MaybeSetAddrName(
const std::string& addrNameIn);
863 static void callCleanup();
872 #endif // RAVEN_NET_H std::atomic< bool > flagInterruptMsgProc
std::unique_ptr< CConnman > g_connman
std::vector< CSubNet > vWhitelistedRange
std::atomic< uint64_t > nPingNonceSent
bool IsReachable(enum Network net)
check whether a given network is one we can probably connect to
CCriticalSection cs_addrName
std::atomic_bool fPauseSend
int64_t nextSendTimeFeeFilter
mapMsgCmdSize mapRecvBytesPerMsgCmd
std::atomic< bool > fNetworkActive
bool fMsgProcWake
flag for waking the message processor.
ServiceFlags
nServices flags
CCriticalSection cs_filter
uint64_t nMaxOutboundTimeframe
STL-like map container that only keeps the N elements with the highest value.
std::atomic< int > nBestHeight
std::list< CNetMessage > vProcessMsg
const uint64_t nKeyedNetGroup
bool SeenLocal(const CService &addr)
vote for a local address
int GetRecvVersion() const
mapMsgCmdSize mapSendBytesPerMsgCmd
void insert(const std::vector< unsigned char > &vKey)
std::vector< unsigned char > data
unsigned int nReceiveFloodSize
mapMsgCmdSize mapSendBytesPerMsgCmd
CClientUIInterface * uiInterface
void resize(size_type n, value_type c=0)
std::vector< uint256 > vInventoryBlockToSend
CCriticalSection cs_hSocket
BloomFilter is a probabilistic filter which SPV clients provide so that we can filter the transaction...
uint64_t randrange(uint64_t range)
Generate a random integer in the range [0..range).
CCriticalSection cs_SubVer
CCriticalSection cs_addrLocal
RAII-style semaphore lock.
Interface for message handling.
void SetVersion(int nVersionIn)
uint64_t nMaxOutboundLimit
RollingBloomFilter is a probabilistic "keep track of most recently inserted" set. ...
NetEventsInterface * m_msgproc
std::atomic< int64_t > nPingUsecStart
CCriticalSection cs_vNodes
CCriticalSection cs_feeFilter
void AdvertiseLocal(CNode *pnode)
void MapPort(bool fUseUPnP)
Double ended buffer combining vector and stream-like interfaces.
CCriticalSection cs_vAddedNodes
A hasher class for Raven's 256-bit hash (double SHA-256).
ListenSocket(SOCKET socket_, bool whitelisted_)
CCriticalSection cs_inventory
int64_t PoissonNextSend(int64_t nNow, int average_interval_seconds)
Return a timestamp in the future (in microseconds) for exponentially distributed events.
uint64_t GetLocalNonce() const
std::map< CNetAddr, LocalServiceInfo > mapLocalHost
std::set< uint256 > setInventoryTxToSend
std::vector< CAddress > vAddrToSend
std::vector< CService > vWhiteBinds
std::atomic< int > nStartingHeight
void PushAddress(const CAddress &_addr, FastRandomContext &insecure_rand)
void PushAssetInventory(const std::string &name)
void SetRecvVersion(int nVersionIn)
std::atomic< int64_t > timeLastMempoolReq
Signals for UI communication.
std::list< CNetMessage > vRecvMsg
CNetMessage(const CMessageHeader::MessageStartChars &pchMessageStartIn, int nTypeIn, int nVersionIn)
void PushInventory(const CInv &inv)
std::atomic< ServiceFlags > nServices
int64_t nNextLocalAddrSend
std::deque< CInv > vRecvGetData
ServiceFlags nLocalServices
Services this instance offers.
bool contains(const std::vector< unsigned char > &vKey) const
std::set< uint256 > setKnown
bool operator()(I first, I last) const
Stochastical (IP) address manager.
int64_t CAmount
Amount in corbies (Can be negative)
std::atomic< int64_t > nLastSend
std::atomic< int64_t > nPingUsecTime
std::atomic< int64_t > nMinPingUsecTime
int GetMyStartingHeight() const
ServiceFlags GetLocalServices() const
std::map< CSubNet, CBanEntry > banmap_t
uint64_t nMaxOutboundCycleStartTime
bool GetLocal(CService &addr, const CNetAddr *paddrPeer=nullptr)
std::condition_variable condMsgProc
std::atomic< int64_t > nLastRecv
bool BindListenPort(const CService &bindAddr, std::string &strError, bool fWhitelisted=false)
std::thread threadOpenAddedConnections
void Interrupt(boost::thread_group &threadGroup)
Interrupt threads.
std::vector< CNode * > vNodes
bool IsPeerAddrLocalGood(CNode *pnode)
std::deque< std::string > vOneShots
NetEventsInterface * m_msgproc
A combination of a network address (CNetAddr) and a (TCP) port.
std::vector< std::string > vSeedNodes
std::vector< std::string > m_specified_outgoing
CRollingBloomFilter filterInventoryKnown
std::thread threadMessageHandler
void ForEachNodeThen(Callable &&pre, CallableAfter &&post)
bool RemoveLocal(const CService &addr)
A CService with information about it as peer.
std::vector< unsigned char > GetKey() const
std::vector< uint256 > vBlockHashesToAnnounce
std::atomic_bool m_try_another_outbound_peer
flag for deciding to connect to an extra outbound peer, in excess of nMaxOutbound This takes the plac...
bool GetNetworkActive() const
CClientUIInterface * clientInterface
std::atomic_bool fDisconnect
void SetLimited(enum Network net, bool fLimited=true)
Make a particular network entirely off-limits (no automatic connects to it)
void ForEachNode(Callable &&func)
limitedmap< uint256, int64_t > mapAlreadyAskedFor
CCriticalSection cs_vOneShots
CRollingBloomFilter addrKnown
CCriticalSection cs_setBanned
std::atomic< int > nRefCount
const int64_t nTimeConnected
uint64_t nMaxOutboundTimeframe
std::atomic< NodeId > nLastNodeId
IP address (IPv6, or IPv4 using mapped IPv6 range (::FFFF:0:0/96))
CCriticalSection cs_vRecv
std::list< CNode * > vNodesDisconnected
std::atomic< bool > fPingQueued
void AddInventoryKnown(const CInv &inv)
std::deque< CInvAsset > vRecvAssetGetData
void ForEachNode(Callable &&func) const
std::atomic< int64_t > nLastTXTime
std::vector< CSubNet > vWhitelistedRange
std::thread threadOpenConnections
CSemaphoreGrant grantOutbound
std::map< std::string, uint64_t > mapMsgCmdSize
bool fAddressesInitialized
std::vector< std::string > vAddedNodes
std::thread threadDNSAddressSeed
ServiceFlags nLocalServices
std::deque< std::vector< unsigned char > > vSendMsg
uint64_t nMaxOutboundLimit
void Discover(boost::thread_group &threadGroup)
std::vector< std::string > m_added_nodes
CCriticalSection cs_vProcessMsg
mapMsgCmdSize mapRecvBytesPerMsgCmd
std::vector< ListenSocket > vhListenSocket
CAmount lastSentFeeFilter
std::atomic< int64_t > nTimeOffset
void PushBlockHash(const uint256 &hash)
void ForEachNodeThen(Callable &&pre, CallableAfter &&post) const
CCriticalSection cs_totalBytesSent
std::atomic_bool fSuccessfullyConnected
std::set< std::string > setInventoryAssetsSend
std::atomic< int > nVersion
uint64_t nMaxOutboundTotalBytesSentInCycle
unsigned int nSendBufferMaxSize
const uint64_t nLocalHostNonce
unsigned int nSendBufferMaxSize
const ServiceFlags nLocalServices
std::string strSubVersion
Subversion as sent to the P2P network in version messages.
CClientUIInterface uiInterface
CCriticalSection cs_sendProcessing
Information about a peer.
std::thread threadSocketHandler
CCriticalSection cs_vSend
void AddAddressKnown(const CAddress &_addr)
std::atomic_bool fPauseRecv
void Init(const Options &connOptions)
CThreadInterrupt interruptNet
CCriticalSection cs_totalBytesRecv
CAddress GetLocalAddress(const CNetAddr *paddrPeer, ServiceFlags nLocalServices)
bool AddLocal(const CService &addr, int nScore=LOCAL_NONE)
std::atomic< int > nRecvVersion
std::atomic< int64_t > nLastBlockTime
bool IsLocal(const CService &addr)
check whether a given address is potentially local
std::multimap< int64_t, CInv > mapAskFor
CCriticalSection cs_mapLocalHost
unsigned short GetListenPort()
const int nMyStartingHeight
bool IsLimited(enum Network net)
Wrapped boost mutex: supports recursive locking, but no waiting TODO: We should move away from using ...
std::set< uint256 > setAskFor
unsigned int nReceiveFloodSize