30 #include <condition_variable> 33 #include <arpa/inet.h> 45 static const int PING_INTERVAL = 2 * 60;
47 static const int TIMEOUT_INTERVAL = 20 * 60;
49 static const int FEELER_INTERVAL = 120;
51 static const unsigned int MAX_INV_SZ = 50000;
53 static const unsigned int MAX_ADDR_TO_SEND = 1000;
56 static const unsigned int MAX_PROTOCOL_MESSAGE_LENGTH = 8 * 1000 * 1000;
58 static const unsigned int MAX_SUBVERSION_LENGTH = 256;
60 static const int MAX_OUTBOUND_CONNECTIONS = 8;
62 static const int MAX_ADDNODE_CONNECTIONS = 8;
64 static const bool DEFAULT_LISTEN =
true;
67 static const bool DEFAULT_UPNP =
USE_UPNP;
69 static const bool DEFAULT_UPNP =
false;
72 static const size_t MAPASKFOR_MAX_SZ = MAX_INV_SZ;
74 static const size_t SETASKFOR_MAX_SZ = 2 * MAX_INV_SZ;
76 static const unsigned int DEFAULT_MAX_PEER_CONNECTIONS = 125;
78 static const uint64_t DEFAULT_MAX_UPLOAD_TARGET = 0;
80 static const uint64_t MAX_UPLOAD_TIMEFRAME = 60 * 60 * 24;
82 static const bool DEFAULT_BLOCKSONLY =
false;
84 static const bool DEFAULT_FORCEDNSSEED =
false;
85 static const size_t DEFAULT_MAXRECEIVEBUFFER = 5 * 1000;
86 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;
135 int nMaxConnections = 0;
136 int nMaxOutbound = 0;
142 unsigned int nSendBufferMaxSize = 0;
143 unsigned int nReceiveFloodSize = 0;
144 uint64_t nMaxOutboundTimeframe = 0;
145 uint64_t nMaxOutboundLimit = 0;
168 CConnman(uint64_t seed0, uint64_t seed1);
174 void SetNetworkActive(
bool active);
175 bool OpenNetworkConnection(
const CAddress& addrConnect,
bool fCountFailure,
CSemaphoreGrant *grantOutbound =
nullptr,
const char *strDest =
nullptr,
bool fOneShot =
false,
bool fFeeler =
false,
bool manual_connection =
false);
176 bool CheckIncomingNonce(uint64_t nonce);
182 template<
typename Callable>
186 for (
auto&&
node : vNodes) {
187 if (NodeFullyConnected(
node))
192 template<
typename Callable>
196 for (
auto&&
node : vNodes) {
197 if (NodeFullyConnected(
node))
202 template<
typename Callable,
typename CallableAfter>
206 for (
auto&&
node : vNodes) {
207 if (NodeFullyConnected(
node))
213 template<
typename Callable,
typename CallableAfter>
217 for (
auto&&
node : vNodes) {
218 if (NodeFullyConnected(
node))
225 size_t GetAddressCount()
const;
227 void MarkAddressGood(
const CAddress& addr);
228 void AddNewAddresses(
const std::vector<CAddress>& vAddr,
const CAddress& addrFrom, int64_t nTimePenalty = 0);
229 std::vector<CAddress> GetAddresses();
245 void Ban(
const CNetAddr& netAddr,
const BanReason& reason, int64_t bantimeoffset = 0,
bool sinceUnixEpoch =
false);
246 void Ban(
const CSubNet& subNet,
const BanReason& reason, int64_t bantimeoffset = 0,
bool sinceUnixEpoch =
false);
253 void SetBanned(
const banmap_t &banmap);
257 void SetTryNewOutboundPeer(
bool flag);
258 bool GetTryNewOutboundPeer();
266 int GetExtraOutboundCount();
268 bool AddNode(
const std::string&
node);
269 bool RemoveAddedNode(
const std::string& node);
270 std::vector<AddedNodeInfo> GetAddedNodeInfo();
273 void GetNodeStats(std::vector<CNodeStats>& vstats);
274 bool DisconnectNode(
const std::string& node);
275 bool DisconnectNode(
NodeId id);
280 void SetMaxOutboundTarget(uint64_t limit);
281 uint64_t GetMaxOutboundTarget();
284 void SetMaxOutboundTimeframe(uint64_t timeframe);
285 uint64_t GetMaxOutboundTimeframe();
290 bool OutboundTargetReached(
bool historicalBlockServingLimit);
294 uint64_t GetOutboundTargetBytesLeft();
298 uint64_t GetMaxOutboundTimeLeftInCycle();
300 uint64_t GetTotalBytesRecv();
301 uint64_t GetTotalBytesSent();
303 void SetBestHeight(
int height);
304 int GetBestHeight()
const;
307 CSipHasher GetDeterministicRandomizer(uint64_t
id)
const;
309 unsigned int GetReceiveFloodSize()
const;
311 void WakeMessageHandler();
321 bool Bind(
const CService &addr,
unsigned int flags);
322 bool InitBinds(
const std::vector<CService>& binds,
const std::vector<CService>& whiteBinds);
323 void ThreadOpenAddedConnections();
324 void AddOneShot(
const std::string& strDest);
325 void ProcessOneShot();
326 void ThreadOpenConnections();
327 void ThreadMessageHandler();
328 void AcceptConnection(
const ListenSocket& hListenSocket);
329 void ThreadSocketHandler();
330 void ThreadDNSAddressSeed();
332 uint64_t CalculateKeyedNetGroup(
const CAddress& ad)
const;
336 CNode* FindNode(
const std::string& addrName);
339 bool AttemptToEvictConnection();
340 CNode* ConnectNode(
CAddress addrConnect,
const char *pszDest,
bool fCountFailure);
341 bool IsWhitelistedRange(
const CNetAddr &addr);
343 void DeleteNode(
CNode* pnode);
347 size_t SocketSendData(
CNode *pnode)
const;
349 bool BannedSetIsDirty();
351 void SetBannedSetDirty(
bool dirty=
true);
354 void DumpAddresses();
359 void RecordBytesRecv(uint64_t
bytes);
360 void RecordBytesSent(uint64_t bytes);
363 static bool NodeFullyConnected(
const CNode* pnode);
439 friend struct CConnmanTest;
441 extern std::unique_ptr<CConnman>
g_connman;
442 void Discover(boost::thread_group& threadGroup);
454 while (first != last) {
455 if (!(*first))
return false;
468 virtual bool ProcessMessages(
CNode* pnode, std::atomic<bool>& interrupt) = 0;
469 virtual bool SendMessages(
CNode* pnode, std::atomic<bool>& interrupt) = 0;
470 virtual void InitializeNode(
CNode* pnode) = 0;
471 virtual void FinalizeNode(
NodeId id,
bool& update_connection_time) = 0;
516 extern std::map<CNetAddr, LocalServiceInfo>
mapLocalHost;
585 const uint256& GetMessageHash()
const;
593 int readHeader(
const char *pch,
unsigned int nBytes);
594 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);
727 void operator=(
const CNode&);
751 return nLocalHostNonce;
755 return nMyStartingHeight;
764 bool ReceiveMsgBytes(
const char *pch,
unsigned int nBytes,
bool& complete);
768 nRecvVersion = nVersionIn;
774 void SetSendVersion(
int nVersionIn);
775 int GetSendVersion()
const;
779 void SetAddrLocal(
const CService& addrLocalIn);
805 if (vAddrToSend.size() >= MAX_ADDR_TO_SEND) {
806 vAddrToSend[insecure_rand.
randrange(vAddrToSend.size())] = _addr;
808 vAddrToSend.push_back(_addr);
827 setInventoryTxToSend.insert(inv.
hash);
830 vInventoryBlockToSend.push_back(inv.
hash);
837 vBlockHashesToAnnounce.push_back(hash);
840 void AskFor(
const CInv& inv);
842 void CloseSocketDisconnect();
851 std::string GetAddrName()
const;
853 void MaybeSetAddrName(
const std::string& addrNameIn);
864 #endif // FABCOIN_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
#define function(a, b, c, d, k, s)
mapMsgCmdSize mapRecvBytesPerMsgCmd
std::atomic< bool > fNetworkActive
uint64_t GetLocalNonce() const
bool contains(const std::vector< unsigned char > &vKey) const
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
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).
void ForEachNodeThen(Callable &&pre, CallableAfter &&post) const
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
assert(len-trim+(2 *lenIndices)<=WIDTH)
void AdvertiseLocal(CNode *pnode)
void MapPort(bool fUseUPnP)
Double ended buffer combining vector and stream-like interfaces.
CCriticalSection cs_vAddedNodes
A hasher class for Fabcoin'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.
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 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
ServiceFlags nServicesExpected
int64_t nNextLocalAddrSend
std::deque< CInv > vRecvGetData
ServiceFlags nLocalServices
Services this instance offers.
std::set< uint256 > setKnown
Stochastical (IP) address manager.
int64_t CAmount
Amount in lius (Can be negative)
std::atomic< int64_t > nLastSend
std::atomic< int64_t > nPingUsecTime
std::atomic< int64_t > nMinPingUsecTime
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 operator()(I first, I last) const
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
CRollingBloomFilter filterInventoryKnown
ServiceFlags nRelevantServices
Services this instance cares about.
std::thread threadMessageHandler
void ForEachNodeThen(Callable &&pre, CallableAfter &&post)
std::vector< byte > bytes
bool RemoveLocal(const CService &addr)
A CService with information about it as peer.
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...
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)
int GetMyStartingHeight() const
ServiceFlags GetLocalServices() const
void ForEachNode(Callable &&func)
limitedmap< uint256, int64_t > mapAlreadyAskedFor
int GetRecvVersion() const
CCriticalSection cs_vOneShots
CRollingBloomFilter addrKnown
bool GetNetworkActive() const
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::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)
CCriticalSection cs_vProcessMsg
mapMsgCmdSize mapRecvBytesPerMsgCmd
std::vector< ListenSocket > vhListenSocket
CAmount lastSentFeeFilter
std::atomic< int64_t > nTimeOffset
void PushBlockHash(const uint256 &hash)
CCriticalSection cs_totalBytesSent
std::atomic_bool fSuccessfullyConnected
std::atomic< int > nVersion
uint64_t nMaxOutboundTotalBytesSentInCycle
unsigned int nSendBufferMaxSize
const uint64_t nLocalHostNonce
ServiceFlags nRelevantServices
unsigned int nSendBufferMaxSize
const ServiceFlags nLocalServices
void ForEachNode(Callable &&func) const
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
std::vector< unsigned char > GetKey() const