Changeset View
Changeset View
Standalone View
Standalone View
src/seeder/bitcoin.cpp
#include "bitcoin.h" | #include "bitcoin.h" | ||||
#include "db.h" | #include "db.h" | ||||
#include "hash.h" | |||||
#include "netbase.h" | #include "netbase.h" | ||||
#include "serialize.h" | #include "serialize.h" | ||||
#include "streams.h" | #include "streams.h" | ||||
#include "uint256.h" | #include "uint256.h" | ||||
#include <algorithm> | #include <algorithm> | ||||
// Weither we are on testnet or mainnet. | // Weither we are on testnet or mainnet. | ||||
bool fTestNet; | bool fTestNet; | ||||
// The network magic to use. | // The network magic to use. | ||||
CMessageHeader::MessageMagic netMagic = {0xe3, 0xe1, 0xf3, 0xe8}; | CMessageHeader::MessageMagic netMagic = {0xe3, 0xe1, 0xf3, 0xe8}; | ||||
#define BITCOIN_SEED_NONCE 0x0539a019ca550825ULL | #define BITCOIN_SEED_NONCE 0x0539a019ca550825ULL | ||||
static const uint32_t allones(-1); | static const uint32_t allones(-1); | ||||
class CNode { | class CSeederNode { | ||||
SOCKET sock; | SOCKET sock; | ||||
CDataStream vSend; | CDataStream vSend; | ||||
CDataStream vRecv; | CDataStream vRecv; | ||||
uint32_t nHeaderStart; | uint32_t nHeaderStart; | ||||
uint32_t nMessageStart; | uint32_t nMessageStart; | ||||
int nVersion; | int nVersion; | ||||
std::string strSubVer; | std::string strSubVer; | ||||
int nStartingHeight; | int nStartingHeight; | ||||
▲ Show 20 Lines • Show All 153 Lines • ▼ Show 20 Lines | class CSeederNode { | ||||
} | } | ||||
bool ProcessMessages() { | bool ProcessMessages() { | ||||
if (vRecv.empty()) { | if (vRecv.empty()) { | ||||
return false; | return false; | ||||
} | } | ||||
do { | do { | ||||
CDataStream::iterator pstart = search( | CDataStream::iterator pstart = | ||||
vRecv.begin(), vRecv.end(), BEGIN(netMagic), END(netMagic)); | search(vRecv.begin(), vRecv.end(), std::begin(netMagic), | ||||
std::end(netMagic)); | |||||
uint32_t nHeaderSize = GetSerializeSize( | uint32_t nHeaderSize = GetSerializeSize( | ||||
CMessageHeader(netMagic), vRecv.GetType(), vRecv.GetVersion()); | CMessageHeader(netMagic), vRecv.GetType(), vRecv.GetVersion()); | ||||
if (vRecv.end() - pstart < nHeaderSize) { | if (vRecv.end() - pstart < nHeaderSize) { | ||||
if (vRecv.size() > nHeaderSize) { | if (vRecv.size() > nHeaderSize) { | ||||
vRecv.erase(vRecv.begin(), vRecv.end() - nHeaderSize); | vRecv.erase(vRecv.begin(), vRecv.end() - nHeaderSize); | ||||
} | } | ||||
break; | break; | ||||
} | } | ||||
Show All 34 Lines | bool ProcessMessages() { | ||||
if (ProcessMessage(strCommand, vMsg)) return true; | if (ProcessMessage(strCommand, vMsg)) return true; | ||||
// printf("%s: done processing %s\n", ToString(you).c_str(), | // printf("%s: done processing %s\n", ToString(you).c_str(), | ||||
// strCommand.c_str()); | // strCommand.c_str()); | ||||
} while (1); | } while (1); | ||||
return false; | return false; | ||||
} | } | ||||
public: | public: | ||||
CNode(const CService &ip, std::vector<CAddress> *vAddrIn) | CSeederNode(const CService &ip, std::vector<CAddress> *vAddrIn) | ||||
: vSend(SER_NETWORK, 0), vRecv(SER_NETWORK, 0), nHeaderStart(-1), | : vSend(SER_NETWORK, 0), vRecv(SER_NETWORK, 0), nHeaderStart(-1), | ||||
nMessageStart(-1), nVersion(0), vAddr(vAddrIn), ban(0), doneAfter(0), | nMessageStart(-1), nVersion(0), vAddr(vAddrIn), ban(0), doneAfter(0), | ||||
you(ip, ServiceFlags(NODE_NETWORK | NODE_BITCOIN_CASH)) { | you(ip, ServiceFlags(NODE_NETWORK | NODE_BITCOIN_CASH)) { | ||||
if (time(nullptr) > 1329696000) { | if (time(nullptr) > 1329696000) { | ||||
vSend.SetVersion(209); | vSend.SetVersion(209); | ||||
vRecv.SetVersion(209); | vRecv.SetVersion(209); | ||||
} | } | ||||
} | } | ||||
▲ Show 20 Lines • Show All 62 Lines • ▼ Show 20 Lines | public: | ||||
int GetStartingHeight() { return nStartingHeight; } | int GetStartingHeight() { return nStartingHeight; } | ||||
}; | }; | ||||
bool TestNode(const CService &cip, int &ban, int &clientV, | bool TestNode(const CService &cip, int &ban, int &clientV, | ||||
std::string &clientSV, int &blocks, | std::string &clientSV, int &blocks, | ||||
std::vector<CAddress> *vAddr) { | std::vector<CAddress> *vAddr) { | ||||
try { | try { | ||||
CNode node(cip, vAddr); | CSeederNode node(cip, vAddr); | ||||
bool ret = node.Run(); | bool ret = node.Run(); | ||||
if (!ret) { | if (!ret) { | ||||
ban = node.GetBan(); | ban = node.GetBan(); | ||||
} else { | } else { | ||||
ban = 0; | ban = 0; | ||||
} | } | ||||
clientV = node.GetClientVersion(); | clientV = node.GetClientVersion(); | ||||
clientSV = node.GetClientSubVersion(); | clientSV = node.GetClientSubVersion(); | ||||
blocks = node.GetStartingHeight(); | blocks = node.GetStartingHeight(); | ||||
// printf("%s: %s!!!\n", cip.ToString().c_str(), ret ? "GOOD" : "BAD"); | // printf("%s: %s!!!\n", cip.ToString().c_str(), ret ? "GOOD" : "BAD"); | ||||
return ret; | return ret; | ||||
} catch (std::ios_base::failure &e) { | } catch (std::ios_base::failure &e) { | ||||
ban = 0; | ban = 0; | ||||
return false; | return false; | ||||
} | } | ||||
} | } | ||||
/* | |||||
int main(void) { | |||||
CService ip("bitcoin.sipa.be", 8333, true); | |||||
std::vector<CAddress> vAddr; | |||||
vAddr.clear(); | |||||
int ban = 0; | |||||
bool ret = TestNode(ip, ban, vAddr); | |||||
printf("ret=%s ban=%i vAddr.size()=%i\n", ret ? "good" : "bad", ban, | |||||
(int)vAddr.size()); | |||||
} | |||||
*/ |