X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=src%2Fnet.h;h=59587751866e7dcfcad1154adf6e762fdf0418fc;hb=0d97c9cdf504bb027941c8d443ce73ab232822fd;hp=369d4fd1707fc6959f2ded76036c0bff0376821b;hpb=77a43545b4491b9703d803765da9059d2bdd5aaa;p=novacoin.git diff --git a/src/net.h b/src/net.h index 369d4fd..5958775 100644 --- a/src/net.h +++ b/src/net.h @@ -5,6 +5,7 @@ #ifndef BITCOIN_NET_H #define BITCOIN_NET_H +#include #include #ifndef Q_MOC_RUN #include @@ -18,7 +19,6 @@ #include "mruset.h" #include "netbase.h" -#include "protocol.h" #include "addrman.h" #include "hash.h" @@ -27,10 +27,12 @@ class CNode; class CBlockIndex; extern int nBestHeight; +const uint16_t nSocksDefault = 9050; +const uint16_t nPortZero = 0; -inline unsigned int ReceiveBufferSize() { return 1000*GetArg("-maxreceivebuffer", 5*1000); } -inline unsigned int SendBufferSize() { return 1000*GetArg("-maxsendbuffer", 1*1000); } +inline uint64_t ReceiveBufferSize() { return 1000*GetArg("-maxreceivebuffer", 5*1000); } +inline uint64_t SendBufferSize() { return 1000*GetArg("-maxsendbuffer", 1*1000); } void AddOneShot(std::string strDest); bool RecvLine(SOCKET hSocket, std::string& strLine); @@ -39,6 +41,7 @@ void AddressCurrentlyConnected(const CService& addr); CNode* FindNode(const CNetAddr& ip); CNode* FindNode(const CService& ip); CNode* ConnectNode(CAddress addrConnect, const char *strDest = NULL, int64_t nTimeout=0); +bool OpenNetworkConnection(const CAddress& addrConnect, CSemaphoreGrant *grantOutbound = NULL, const char *strDest = NULL, bool fOneShot = false); void MapPort(); unsigned short GetListenPort(); bool BindListenPort(const CService &bindAddr, std::string& strError=REF(std::string())); @@ -50,7 +53,6 @@ enum LOCAL_NONE, // unknown LOCAL_IF, // address a local interface listens on LOCAL_BIND, // address explicit bound to - LOCAL_UPNP, // address reported by UPnP LOCAL_IRC, // address reported by IRC (deprecated) LOCAL_HTTP, // address reported by whatismyip.com and similar LOCAL_MANUAL, // address explicitly specified (-externalip=) @@ -58,6 +60,9 @@ enum LOCAL_MAX }; + +bool IsPeerAddrLocalGood(CNode *pnode); +void AdvertiseLocal(CNode *pnode); void SetLimited(enum Network net, bool fLimited = true); bool IsLimited(enum Network net); bool IsLimited(const CNetAddr& addr); @@ -74,7 +79,7 @@ CAddress GetLocalAddress(const CNetAddr *paddrPeer = NULL); enum { MSG_TX = 1, - MSG_BLOCK, + MSG_BLOCK }; class CRequestTracker @@ -103,20 +108,23 @@ enum threadId THREAD_OPENCONNECTIONS, THREAD_MESSAGEHANDLER, THREAD_RPCLISTENER, - THREAD_UPNP, THREAD_DNSSEED, THREAD_ADDEDCONNECTIONS, THREAD_DUMPADDRESS, THREAD_RPCHANDLER, THREAD_MINTER, THREAD_SCRIPTCHECK, + THREAD_NTP, + THREAD_IPCOLLECTOR, THREAD_MAX }; extern bool fClient; extern bool fDiscover; -extern bool fUseUPnP; +extern bool fNoListen; + +extern bool fDiscover; extern uint64_t nLocalServices; extern uint64_t nLocalHostNonce; extern CAddress addrSeenByPeer; @@ -125,6 +133,8 @@ extern CAddrMan addrman; extern std::vector vNodes; extern CCriticalSection cs_vNodes; +extern std::vector vAddedNodes; +extern CCriticalSection cs_vAddedNodes; extern std::map mapRelay; extern std::deque > vRelayExpiration; extern CCriticalSection cs_mapRelay; @@ -141,12 +151,12 @@ public: int64_t nLastRecv; int64_t nTimeConnected; std::string addrName; - int nVersion; + int32_t nVersion; std::string strSubVer; bool fInbound; int64_t nReleaseTime; - int nStartingHeight; - int nMisbehavior; + int32_t nStartingHeight; + int32_t nMisbehavior; uint64_t nSendBytes; uint64_t nRecvBytes; bool fSyncNode; @@ -173,12 +183,12 @@ public: int64_t nLastRecv; int64_t nLastSendEmpty; int64_t nTimeConnected; - int nHeaderStart; - unsigned int nMessageStart; + int32_t nHeaderStart; + uint32_t nMessageStart; CAddress addr; std::string addrName; CService addrLocal; - int nVersion; + int32_t nVersion; std::string strSubVer; bool fOneShot; bool fClient; @@ -203,7 +213,7 @@ public: uint256 hashContinue; CBlockIndex* pindexLastGetBlocksBegin; uint256 hashLastGetBlocksEnd; - int nStartingHeight; + int32_t nStartingHeight; bool fStartSync; // flood relay @@ -212,6 +222,9 @@ public: bool fGetAddr; std::set setKnown; uint256 hashCheckpointKnown; // ppcoin: known sent sync-checkpoint + int64_t nNextAddrSend; + int64_t nNextLocalAddrSend; + int64_t nNextInvSend; // inventory based relay mruset setInventoryKnown; @@ -230,11 +243,11 @@ public: nLastSendEmpty = GetTime(); nTimeConnected = GetTime(); nHeaderStart = -1; - nMessageStart = -1; + nMessageStart = std::numeric_limits::max(); addr = addrIn; - addrName = addrNameIn == "" ? addr.ToStringIPPort() : addrNameIn; + addrName = addrNameIn.empty() ? addr.ToStringIPPort() : addrNameIn; nVersion = 0; - strSubVer = ""; + strSubVer.clear(); fOneShot = false; fClient = false; // set by version message fInbound = fInboundIn; @@ -247,11 +260,14 @@ public: pindexLastGetBlocksBegin = 0; hashLastGetBlocksEnd = 0; nStartingHeight = -1; + nNextLocalAddrSend = 0; + nNextAddrSend = 0; + nNextInvSend = 0; fStartSync = false; fGetAddr = false; nMisbehavior = 0; hashCheckpointKnown = 0; - setInventoryKnown.max_size(SendBufferSize() / 1000); + setInventoryKnown.max_size((size_t)SendBufferSize() / 1000); // Be shy and don't send version until we hear if (hSocket != INVALID_SOCKET && !fInbound) @@ -262,8 +278,7 @@ public: { if (hSocket != INVALID_SOCKET) { - closesocket(hSocket); - hSocket = INVALID_SOCKET; + CloseSocket(hSocket); } } @@ -359,9 +374,9 @@ public: ENTER_CRITICAL_SECTION(cs_vSend); if (nHeaderStart != -1) AbortMessage(); - nHeaderStart = vSend.size(); + nHeaderStart = (int32_t)vSend.size(); vSend << CMessageHeader(pszCommand, 0); - nMessageStart = vSend.size(); + nMessageStart = (uint32_t)vSend.size(); if (fDebug) printf("sending: %s ", pszCommand); } @@ -372,7 +387,7 @@ public: return; vSend.resize(nHeaderStart); nHeaderStart = -1; - nMessageStart = -1; + nMessageStart = std::numeric_limits::max(); LEAVE_CRITICAL_SECTION(cs_vSend); if (fDebug) @@ -388,16 +403,18 @@ public: return; } - if (nHeaderStart < 0) + if (nHeaderStart < 0) { + LEAVE_CRITICAL_SECTION(cs_vSend); return; + } // Set the size - unsigned int nSize = vSend.size() - nMessageStart; + uint32_t nSize = (uint32_t) vSend.size() - nMessageStart; memcpy((char*)&vSend[nHeaderStart] + CMessageHeader::MESSAGE_SIZE_OFFSET, &nSize, sizeof(nSize)); // Set the checksum uint256 hash = Hash(vSend.begin() + nMessageStart, vSend.end()); - unsigned int nChecksum = 0; + uint32_t nChecksum = 0; memcpy(&nChecksum, &hash, sizeof(nChecksum)); assert(nMessageStart - nHeaderStart >= CMessageHeader::CHECKSUM_OFFSET + sizeof(nChecksum)); memcpy((char*)&vSend[nHeaderStart] + CMessageHeader::CHECKSUM_OFFSET, &nChecksum, sizeof(nChecksum)); @@ -407,7 +424,7 @@ public: } nHeaderStart = -1; - nMessageStart = -1; + nMessageStart = std::numeric_limits::max(); LEAVE_CRITICAL_SECTION(cs_vSend); } @@ -415,7 +432,7 @@ public: { if (nHeaderStart < 0) return; - int nSize = vSend.size() - nMessageStart; + int nSize = (int) vSend.size() - nMessageStart; if (nSize > 0) EndMessage(); else @@ -441,109 +458,13 @@ public: } } - template - void PushMessage(const char* pszCommand, const T1& a1) - { - try - { - BeginMessage(pszCommand); - vSend << a1; - EndMessage(); - } - catch (...) - { - AbortMessage(); - throw; - } - } - - template - void PushMessage(const char* pszCommand, const T1& a1, const T2& a2) - { - try - { - BeginMessage(pszCommand); - vSend << a1 << a2; - EndMessage(); - } - catch (...) - { - AbortMessage(); - throw; - } - } - - template - void PushMessage(const char* pszCommand, const T1& a1, const T2& a2, const T3& a3) - { - try - { - BeginMessage(pszCommand); - vSend << a1 << a2 << a3; - EndMessage(); - } - catch (...) - { - AbortMessage(); - throw; - } - } - - template - void PushMessage(const char* pszCommand, const T1& a1, const T2& a2, const T3& a3, const T4& a4) - { - try - { - BeginMessage(pszCommand); - vSend << a1 << a2 << a3 << a4; - EndMessage(); - } - catch (...) - { - AbortMessage(); - throw; - } - } - - template - void PushMessage(const char* pszCommand, const T1& a1, const T2& a2, const T3& a3, const T4& a4, const T5& a5) - { - try - { - BeginMessage(pszCommand); - vSend << a1 << a2 << a3 << a4 << a5; - EndMessage(); - } - catch (...) - { - AbortMessage(); - throw; - } - } - - template - 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); - vSend << a1 << a2 << a3 << a4 << a5 << a6; - EndMessage(); - } - catch (...) - { - AbortMessage(); - throw; - } - } - - template - 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) + template + void PushMessage(const char* pszCommand, const Args&... args) { try { BeginMessage(pszCommand); - vSend << a1 << a2 << a3 << a4 << a5 << a6 << a7; + (vSend << ... << args); EndMessage(); } catch (...) @@ -553,39 +474,6 @@ public: } } - template - 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); - vSend << a1 << a2 << a3 << a4 << a5 << a6 << a7 << a8; - EndMessage(); - } - catch (...) - { - AbortMessage(); - throw; - } - } - - template - 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); - vSend << a1 << a2 << a3 << a4 << a5 << a6 << a7 << a8 << a9; - EndMessage(); - } - catch (...) - { - AbortMessage(); - throw; - } - } - - void PushRequest(const char* pszCommand, void (*fn)(void*, CDataStream&), void* param1) { @@ -681,4 +569,7 @@ void RelayTransaction(const CTransaction& tx, const uint256& hash); void RelayTransaction(const CTransaction& tx, const uint256& hash, const CDataStream& ss); +/** Return a timestamp in the future (in microseconds) for exponentially distributed events. */ +int64_t PoissonNextSend(int64_t nNow, int average_interval_seconds); + #endif