#include "miner.h"
#include "ntp.h"
-#ifdef WIN32
-#include <string.h>
-#endif
-
using namespace std;
-using namespace boost;
static const int MAX_OUTBOUND_CONNECTIONS = 16;
static CNode* pnodeSync = NULL;
CAddress addrSeenByPeer(CService("0.0.0.0", nPortZero), nLocalServices);
uint64_t nLocalHostNonce = 0;
-boost::array<int, THREAD_MAX> vnThreadsRunning;
-static std::vector<SOCKET> vhListenSocket;
+array<int, THREAD_MAX> vnThreadsRunning;
+static vector<SOCKET> vhListenSocket;
CAddrMan addrman;
vector<CNode*> vNodes;
set<CNetAddr> setservAddNodeAddresses;
CCriticalSection cs_setservAddNodeAddresses;
-vector<std::string> vAddedNodes;
+vector<string> vAddedNodes;
CCriticalSection cs_vAddedNodes;
static CSemaphore *semOutbound = NULL;
// We now get our external IP from the IRC server first and only use this as a backup
bool GetMyExternalIP(CNetAddr& ipRet)
{
- struct sockaddr_in mapped;
- auto rnd = std::numeric_limits<uint64_t>::max();
+ struct sockaddr_in mapped = {};
+ auto rnd = GetRand(numeric_limits<uint64_t>::max());
const char *srv;
int rc = GetExternalIPbySTUN(rnd, &mapped, &srv);
if(rc >= 0) {
ipRet = CNetAddr(mapped.sin_addr);
- printf("GetExternalIPbySTUN(%" PRIu64 ") returned %s in attempt %d; Server=%s\n", rnd, ipRet.ToStringIP().c_str(), rc, srv);
+ if (fDebugNet) {
+ printf("GetExternalIPbySTUN(%" PRIu64 ") returned %s in attempt %d; Server=%s\n", rnd, ipRet.ToStringIP().c_str(), rc, srv);
+ }
return true;
}
return false;
}
}
-
-
-
-
void AddressCurrentlyConnected(const CService& addr)
{
addrman.Connected(addr);
return NULL;
}
-CNode* FindNode(std::string addrName)
+CNode* FindNode(string addrName)
{
LOCK(cs_vNodes);
for(CNode* pnode : vNodes)
}
}
+CNode::CNode(SOCKET hSocketIn, CAddress addrIn, string addrNameIn, bool fInboundIn) : vSend(SER_NETWORK, MIN_PROTO_VERSION), vRecv(SER_NETWORK, MIN_PROTO_VERSION)
+{
+ nServices = 0;
+ hSocket = hSocketIn;
+ nLastSend = 0;
+ nLastRecv = 0;
+ nSendBytes = 0;
+ nRecvBytes = 0;
+ nLastSendEmpty = GetTime();
+ nTimeConnected = GetTime();
+ nHeaderStart = -1;
+ nMessageStart = numeric_limits<uint32_t>::max();
+ addr = addrIn;
+ addrName = addrNameIn.empty() ? addr.ToStringIPPort() : addrNameIn;
+ nVersion = 0;
+ strSubVer.clear();
+ fOneShot = false;
+ fClient = false; // set by version message
+ fInbound = fInboundIn;
+ fNetworkNode = false;
+ fSuccessfullyConnected = false;
+ fDisconnect = false;
+ nRefCount = 0;
+ nReleaseTime = 0;
+ hashContinue = 0;
+ pindexLastGetBlocksBegin = 0;
+ hashLastGetBlocksEnd = 0;
+ nStartingHeight = -1;
+ nNextLocalAddrSend = 0;
+ nNextAddrSend = 0;
+ nNextInvSend = 0;
+ fStartSync = false;
+ fGetAddr = false;
+ nMisbehavior = 0;
+ hashCheckpointKnown = 0;
+ setInventoryKnown.max_size((size_t)SendBufferSize() / 1000);
+
+ // Be shy and don't send version until we hear
+ if (hSocket != INVALID_SOCKET && !fInbound)
+ PushVersion();
+}
+
+CNode::~CNode()
+{
+ if (hSocket != INVALID_SOCKET)
+ {
+ CloseSocket(hSocket);
+ }
+}
+
+int CNode::GetRefCount()
+{
+ return max(nRefCount, 0) + (GetTime() < nReleaseTime ? 1 : 0);
+}
+
+CNode* CNode::AddRef(int64_t nTimeout)
+{
+ if (nTimeout != 0)
+ nReleaseTime = max(nReleaseTime, GetTime() + nTimeout);
+ else
+ nRefCount++;
+ return this;
+}
+
+void CNode::Release()
+{
+ nRefCount--;
+}
+
+void CNode::AddAddressKnown(const CAddress& addr)
+{
+ setAddrKnown.insert(addr);
+}
+
+void CNode::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() && !setAddrKnown.count(addr))
+ vAddrToSend.push_back(addr);
+}
+
+void CNode::AddInventoryKnown(const CInv& inv)
+{
+ {
+ LOCK(cs_inventory);
+ setInventoryKnown.insert(inv);
+ }
+}
+
+void CNode::PushInventory(const CInv& inv)
+{
+ {
+ LOCK(cs_inventory);
+ if (!setInventoryKnown.count(inv))
+ vInventoryToSend.push_back(inv);
+ }
+}
+
+void CNode::AskFor(const CInv& inv)
+{
+ // We're using mapAskFor as a priority queue,
+ // the key is the earliest time the request can be sent
+ int64_t& nRequestTime = mapAlreadyAskedFor[inv];
+ if (fDebugNet)
+ printf("askfor %s %" PRId64 " (%s)\n", inv.ToString().c_str(), nRequestTime, DateTimeStrFormat("%H:%M:%S", nRequestTime/1000000).c_str());
+
+ // Make sure not to reuse time indexes to keep things in the same order
+ int64_t nNow = (GetTime() - 1) * 1000000;
+ static int64_t nLastTime;
+ ++nLastTime;
+ nNow = max(nNow, nLastTime);
+ nLastTime = nNow;
+
+ // Each retry is 2 minutes after the last
+ nRequestTime = max(nRequestTime + 2 * 60 * 1000000, nNow);
+ mapAskFor.insert({ nRequestTime, inv });
+}
+
+void CNode::BeginMessage(const char* pszCommand)
+{
+ ENTER_CRITICAL_SECTION(cs_vSend);
+ if (nHeaderStart != -1)
+ AbortMessage();
+ nHeaderStart = (int32_t)vSend.size();
+ vSend << CMessageHeader(pszCommand, 0);
+ nMessageStart = (uint32_t)vSend.size();
+ if (fDebug)
+ printf("sending: %s ", pszCommand);
+}
+
+void CNode::AbortMessage()
+{
+ if (nHeaderStart < 0)
+ return;
+ vSend.resize(nHeaderStart);
+ nHeaderStart = -1;
+ nMessageStart = numeric_limits<uint32_t>::max();
+ LEAVE_CRITICAL_SECTION(cs_vSend);
+
+ if (fDebug)
+ printf("(aborted)\n");
+}
+
+void CNode::EndMessage()
+{
+ if (mapArgs.count("-dropmessagestest") && GetRand(atoi(mapArgs["-dropmessagestest"])) == 0)
+ {
+ printf("dropmessages DROPPING SEND MESSAGE\n");
+ AbortMessage();
+ return;
+ }
+
+ if (nHeaderStart < 0)
+ return;
+
+ // Set the size
+ uint32_t nSize = (uint32_t) vSend.size() - nMessageStart;
+ memcpy((char*)&vSend[nHeaderStart] + CMessageHeader::MESSAGE_SIZE_OFFSET, &nSize, sizeof(nSize));
+
+ // Set the checksum
+ auto hash = Hash(vSend.begin() + nMessageStart, vSend.end());
+ 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));
+
+ if (fDebug) {
+ printf("(%d bytes)\n", nSize);
+ }
+
+ nHeaderStart = -1;
+ nMessageStart = numeric_limits<uint32_t>::max();
+ LEAVE_CRITICAL_SECTION(cs_vSend);
+}
+
+void CNode::EndMessageAbortIfEmpty()
+{
+ if (nHeaderStart < 0)
+ return;
+ int nSize = (int) vSend.size() - nMessageStart;
+ if (nSize > 0)
+ EndMessage();
+ else
+ AbortMessage();
+}
+
void CNode::CloseSocketDisconnect()
{
fDisconnect = true;
RAND_bytes((unsigned char*)&nLocalHostNonce, sizeof(nLocalHostNonce));
printf("send version message: version %d, blocks=%d, us=%s, them=%s, peer=%s\n", PROTOCOL_VERSION, nBestHeight, addrMe.ToString().c_str(), addrYou.ToString().c_str(), addr.ToString().c_str());
PushMessage("version", PROTOCOL_VERSION, nLocalServices, nTime, addrYou, addrMe,
- nLocalHostNonce, FormatSubVersion(CLIENT_NAME, CLIENT_VERSION, std::vector<string>()), nBestHeight);
+ nLocalHostNonce, FormatSubVersion(CLIENT_NAME, CLIENT_VERSION, vector<string>()), nBestHeight);
}
-std::map<CNetAddr, int64_t> CNode::setBanned;
+map<CNetAddr, int64_t> CNode::setBanned;
CCriticalSection CNode::cs_setBanned;
void CNode::ClearBanned()
ThreadSocketHandler2(parg);
vnThreadsRunning[THREAD_SOCKETHANDLER]--;
}
- catch (std::exception& e) {
+ catch (exception& e) {
vnThreadsRunning[THREAD_SOCKETHANDLER]--;
PrintException(&e, "ThreadSocketHandler()");
} catch (...) {
ThreadDNSAddressSeed2(parg);
vnThreadsRunning[THREAD_DNSSEED]--;
}
- catch (std::exception& e) {
+ catch (exception& e) {
vnThreadsRunning[THREAD_DNSSEED]--;
PrintException(&e, "ThreadDNSAddressSeed()");
} catch (...) {
// The first name is used as information source for addrman.
// The second name should resolve to a list of seed addresses.
static const vector<pair <string, string> > vstrDNSSeed = {
- { "novacoin.karelia.pro", "dnsseed.novacoin.karelia.pro" },
+ { "node.novacoin.karelia.pro", "dnsseed.novacoin.karelia.pro" },
{ "novacoin.ru", "dnsseed.novacoin.ru" },
{ "novacoin.ru", "testseed.novacoin.ru" },
{ "novaco.in", "dnsseed.novaco.in" },
{
ThreadDumpAddress2(parg);
}
- catch (std::exception& e) {
+ catch (exception& e) {
PrintException(&e, "ThreadDumpAddress()");
}
printf("ThreadDumpAddress exited\n");
ThreadOpenConnections2(parg);
vnThreadsRunning[THREAD_OPENCONNECTIONS]--;
}
- catch (std::exception& e) {
+ catch (exception& e) {
vnThreadsRunning[THREAD_OPENCONNECTIONS]--;
PrintException(&e, "ThreadOpenConnections()");
} catch (...) {
// Add seed nodes if IRC isn't working
if (!IsLimited(NET_IPV4) && addrman.size()==0 && (GetTime() - nStart > 60) && !fTestNet)
{
- std::vector<uint32_t> vnSeed =
+ vector<uint32_t> vnSeed =
{
0x1c542868, 0x3859dd6f, 0x203c2e68, 0xf145a6bc, 0x638a545f, 0x325da346, 0x385da346, 0xfb2b8d5f,
0x52568c5f, 0xa979e65b, 0x8de6485d, 0x9f79e65b, 0x048a861f, 0x3388b55f, 0x6ff0b45e, 0x17e81f5f,
0xbe5ef3bc, 0x4689344d, 0xb223895e, 0xfcebeaad, 0xb7c0e92e, 0x993c1760, 0xe1e171b0, 0xb857e75b,
0xbf10002e, 0xb55b2cb2, 0xa90e2cb2, 0x13d6f15e, 0xf8be9225, 0x14ddf15e, 0x06e90305, 0x82472cb2,
};
- std::vector<CAddress> vAdd;
+ vector<CAddress> vAdd;
for (unsigned int i = 0; i < vnSeed.size(); i++)
{
// It'll only connect to one or two seed nodes because once it connects,
// Add Tor nodes if we have connection with onion router
if (mapArgs.count("-tor"))
{
- const std::vector<std::string> vstrTorSeed =
+ const vector<string> vstrTorSeed =
{
"seedp4knqnoei57u.onion",
"seedr3hhlepyi7fd.onion",
"eqon4usunavt76m7.onion",
"seedd3aldwpslzl3.onion"
};
- std::vector<CAddress> vAdd;
+ vector<CAddress> vAdd;
for (unsigned int i = 0; i < vstrTorSeed.size(); i++)
{
CAddress addr(CService(vstrTorSeed[i], GetDefaultPort()));
ThreadOpenAddedConnections2(parg);
vnThreadsRunning[THREAD_ADDEDCONNECTIONS]--;
}
- catch (std::exception& e) {
+ catch (exception& e) {
vnThreadsRunning[THREAD_ADDEDCONNECTIONS]--;
PrintException(&e, "ThreadOpenAddedConnections()");
} catch (...) {
ThreadMessageHandler2(parg);
vnThreadsRunning[THREAD_MESSAGEHANDLER]--;
}
- catch (std::exception& e) {
+ catch (exception& e) {
vnThreadsRunning[THREAD_MESSAGEHANDLER]--;
PrintException(&e, "ThreadMessageHandler()");
} catch (...) {
#ifndef WIN32
#ifdef SO_NOSIGPIPE
// Different way of disabling SIGPIPE on BSD
- setsockopt(hListenSocket, SOL_SOCKET, SO_NOSIGPIPE, (void*)&nOne, sizeof(int));
+ if (setsockopt(hListenSocket, SOL_SOCKET, SO_NOSIGPIPE, (void*)&nOne, sizeof(int)) == SOCKET_ERROR)
+ {
+ printf("WARNING: setsockopt failed\n");
+ //TODO: work around problem - may be add CloseSocket and return false?
+ }
#endif
// Allow binding if the port is still in TIME_WAIT state after
// the program was closed and restarted. Not an issue on windows!
- setsockopt(hListenSocket, SOL_SOCKET, SO_REUSEADDR, (void*)&nOne, sizeof(int));
+ if (setsockopt(hListenSocket, SOL_SOCKET, SO_REUSEADDR, (void*)&nOne, sizeof(int)) == SOCKET_ERROR)
+ {
+ printf("WARNING: setsockopt failed\n");
+ //TODO: work around problem - may be add CloseSocket and return false?
+ }
#endif
#ifdef WIN32
{
strError = strprintf("Error: Couldn't set properties on socket for incoming connections (error %d)", WSAGetLastError());
printf("%s\n", strError.c_str());
+ CloseSocket(hSocket);
return false;
}
if (addrBind.IsIPv6()) {
#ifdef IPV6_V6ONLY
#ifdef WIN32
- setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_V6ONLY, (const char*)&nOne, sizeof(int));
+ if (setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_V6ONLY, (const char*)&nOne, sizeof(int)) == SOCKET_ERROR)
+ {
+ printf("WARNING: setsockopt failed\n");
+ //TODO: work around problem - may be add CloseSocket and return false?
+ }
#else
- setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_V6ONLY, (void*)&nOne, sizeof(int));
+ if (setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_V6ONLY, (void*)&nOne, sizeof(int)) == SOCKET_ERROR)
+ {
+ printf("WARNING: setsockopt failed\n");
+ //TODO: work around problem - may be add CloseSocket and return false?
+ }
#endif
#endif
#ifdef WIN32
- int nProtLevel = PROTECTION_LEVEL_UNRESTRICTED;
- setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_PROTECTION_LEVEL, (const char*)&nProtLevel, sizeof(int));
+ int nProtLevel = PROTECTION_LEVEL_UNRESTRICTED;
+ if (setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_PROTECTION_LEVEL, (const char*)&nProtLevel, sizeof(int)) == SOCKET_ERROR)
+ {
+ printf("WARNING: setsockopt failed\n");
+ //TODO: work around problem - may be add CloseSocket and return false?
+ }
#endif
}
#endif
}
instance_of_cnetcleanup;
+inline void RelayInventory(const CInv& inv)
+{
+ // Put on lists to offer to the other nodes
+ {
+ LOCK(cs_vNodes);
+ for(CNode* pnode : vNodes)
+ pnode->PushInventory(inv);
+ }
+}
+
void RelayTransaction(const CTransaction& tx, const uint256& hash)
{
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
}
// Save original serialized message so newer versions are preserved
- mapRelay.insert(std::make_pair(inv, ss));
- vRelayExpiration.push_back(std::make_pair(GetTime() + 15 * 60, inv));
+ mapRelay.insert({inv, ss});
+ vRelayExpiration.push_back({GetTime() + 15 * 60, inv});
}
RelayInventory(inv);