diff --git a/src/Makefile.am b/src/Makefile.am --- a/src/Makefile.am +++ b/src/Makefile.am @@ -383,6 +383,8 @@ libbitcoin_seeder_a_CPPFLAGS = $(AM_CPPFLAGS) $(PIE_FLAGS) $(BITCOIN_SEEDER_INCLUDES) libbitcoin_seeder_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS) libbitcoin_seeder_a_SOURCES = \ + seeder/address.cpp \ + seeder/address.h \ seeder/bitcoin.cpp \ seeder/bitcoin.h \ seeder/compat.h \ diff --git a/src/seeder/address.h b/src/seeder/address.h new file mode 100644 --- /dev/null +++ b/src/seeder/address.h @@ -0,0 +1,50 @@ +// Copyright (c) 2017 The Bitcoin developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#ifndef BITCOIN_SEEDER_ADDRESS_H +#define BITCOIN_SEEDER_ADDRESS_H + +#include "netbase.h" +#include "protocol.h" + +class CSeederAddress : public CSeederService { +public: + CSeederAddress(); + CSeederAddress(CSeederService ipIn, + uint64_t nServicesIn = NODE_NETWORK | NODE_BITCOIN_CASH); + + void Init(); + + ADD_SERIALIZE_METHODS; + + template + inline void SerializationOp(Stream &s, Operation ser_action) { + int nVersion = s.GetVersion(); + CSeederAddress *pthis = const_cast(this); + CSeederService *pip = (CSeederService *)pthis; + if (ser_action.ForRead()) { + pthis->Init(); + } + if (s.GetType() & SER_DISK) { + READWRITE(nVersion); + } + if ((s.GetType() & SER_DISK) || + (nVersion >= 31402 && !(s.GetType() & SER_GETHASH))) { + READWRITE(nTime); + } + READWRITE(nServices); + READWRITE(*pip); + } + + void print() const; + + // TODO: make private (improves encapsulation) +public: + uint64_t nServices; + + // disk and network only + unsigned int nTime; +}; + +#endif // BITCOIN_SEEDER_ADDRESS_H diff --git a/src/seeder/address.cpp b/src/seeder/address.cpp new file mode 100644 --- /dev/null +++ b/src/seeder/address.cpp @@ -0,0 +1,24 @@ +// Copyright (c) 2017 The Bitcoin developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#include "address.h" + +CSeederAddress::CSeederAddress() : CSeederService() { + Init(); +} + +CSeederAddress::CSeederAddress(CSeederService ipIn, uint64_t nServicesIn) + : CSeederService(ipIn) { + Init(); + nServices = nServicesIn; +} + +void CSeederAddress::Init() { + nServices = NODE_NETWORK | NODE_BITCOIN_CASH; + nTime = 100000000; +} + +void CSeederAddress::print() const { + printf("CSeederAddress(%s)\n", ToString().c_str()); +} diff --git a/src/seeder/bitcoin.h b/src/seeder/bitcoin.h --- a/src/seeder/bitcoin.h +++ b/src/seeder/bitcoin.h @@ -1,9 +1,27 @@ #ifndef BITCOIN_SEEDER_BITCOIN_H #define BITCOIN_SEEDER_BITCOIN_H +#include "address.h" #include "protocol.h" +#include +#include + +/** + * The seeder do not use the Params facility. + * + * While this is sorted out, we need a replacement. + */ +extern bool fTestNet; +static inline unsigned short GetDefaultPort(const bool testnet = fTestNet) { + return testnet ? 18333 : 8333; +} + +// The network magic to use. +extern CMessageHeader::MessageMagic netMagic; + bool TestNode(const CSeederService &cip, int &ban, int &client, - std::string &clientSV, int &blocks, std::vector *vAddr); + std::string &clientSV, int &blocks, + std::vector *vAddr); #endif diff --git a/src/seeder/bitcoin.cpp b/src/seeder/bitcoin.cpp --- a/src/seeder/bitcoin.cpp +++ b/src/seeder/bitcoin.cpp @@ -8,6 +8,12 @@ #include +// Weither we are on testnet or mainnet. +bool fTestNet; + +// The network magic to use. +CMessageHeader::MessageMagic netMagic = {0xe3, 0xe1, 0xf3, 0xe8}; + #define BITCOIN_SEED_NONCE 0x0539a019ca550825ULL static const uint32_t allones(-1); @@ -21,10 +27,10 @@ int nVersion; std::string strSubVer; int nStartingHeight; - std::vector *vAddr; + std::vector *vAddr; int ban; int64_t doneAfter; - CAddress you; + CSeederAddress you; int GetTimeout() { return you.IsTor() ? 120 : 30; } @@ -89,7 +95,7 @@ int64_t nTime = time(nullptr); uint64_t nLocalNonce = BITCOIN_SEED_NONCE; int64_t nLocalServices = 0; - CAddress me(CSeederService("0.0.0.0")); + CSeederAddress me(CSeederService("0.0.0.0")); BeginMessage("version"); int nBestHeight = GetRequireHeight(); std::string ver = "/bitcoin-cash-seeder:0.15/"; @@ -114,8 +120,8 @@ // strCommand.c_str()); if (strCommand == "version") { int64_t nTime; - CAddress addrMe; - CAddress addrFrom; + CSeederAddress addrMe; + CSeederAddress addrFrom; uint64_t nNonce = 1; vRecv >> nVersion >> you.nServices >> nTime >> addrMe; if (nVersion == 10300) nVersion = 300; @@ -142,17 +148,17 @@ } if (strCommand == "addr" && vAddr) { - std::vector vAddrNew; + std::vector vAddrNew; vRecv >> vAddrNew; // printf("%s: got %i addresses\n", ToString(you).c_str(), // (int)vAddrNew.size()); int64_t now = time(nullptr); - std::vector::iterator it = vAddrNew.begin(); + std::vector::iterator it = vAddrNew.begin(); if (vAddrNew.size() > 1) { if (doneAfter == 0 || doneAfter > now + 1) doneAfter = now + 1; } while (it != vAddrNew.end()) { - CAddress &addr = *it; + CSeederAddress &addr = *it; // printf("%s: got address %s\n", ToString(you).c_str(), // addr.ToString().c_str(), (int)(vAddr->size())); it++; @@ -179,9 +185,8 @@ } do { - CDataStream::iterator pstart = - search(vRecv.begin(), vRecv.end(), BEGIN(pchMessageStart), - END(pchMessageStart)); + CDataStream::iterator pstart = search( + vRecv.begin(), vRecv.end(), BEGIN(netMagic), END(netMagic)); uint32_t nHeaderSize = GetSerializeSize( CMessageHeader(), vRecv.GetType(), vRecv.GetVersion()); if (vRecv.end() - pstart < nHeaderSize) { @@ -231,7 +236,7 @@ } public: - CNode(const CSeederService &ip, std::vector *vAddrIn) + CNode(const CSeederService &ip, std::vector *vAddrIn) : vSend(SER_NETWORK, 0), vRecv(SER_NETWORK, 0), nHeaderStart(-1), nMessageStart(-1), nVersion(0), vAddr(vAddrIn), ban(0), doneAfter(0), you(ip) { @@ -303,7 +308,7 @@ bool TestNode(const CSeederService &cip, int &ban, int &clientV, std::string &clientSV, int &blocks, - std::vector *vAddr) { + std::vector *vAddr) { try { CNode node(cip, vAddr); bool ret = node.Run(); @@ -326,7 +331,7 @@ /* int main(void) { CSeederService ip("bitcoin.sipa.be", 8333, true); - std::vector vAddr; + std::vector vAddr; vAddr.clear(); int ban = 0; bool ret = TestNode(ip, ban, vAddr); diff --git a/src/seeder/db.h b/src/seeder/db.h --- a/src/seeder/db.h +++ b/src/seeder/db.h @@ -1,6 +1,8 @@ #ifndef BITCOIN_SEEDER_DB_H #define BITCOIN_SEEDER_DB_H +#include "address.h" +#include "bitcoin.h" #include "netbase.h" #include "protocol.h" #include "sync.h" @@ -262,7 +264,7 @@ protected: // internal routines that assume proper locks are acquired // add an address - void Add_(const CAddress &addr, bool force); + void Add_(const CSeederAddress &addr, bool force); // get an IP to test (must call Good_, Bad_, or Skipped_ on result // afterwards) bool Get_(CSeederServiceResult &ip, int &wait); @@ -376,12 +378,12 @@ s >> banned; } - void Add(const CAddress &addr, bool fForce = false) { + void Add(const CSeederAddress &addr, bool fForce = false) { LOCK(cs); Add_(addr, fForce); } - void Add(const std::vector &vAddr, bool fForce = false) { + void Add(const std::vector &vAddr, bool fForce = false) { LOCK(cs); for (size_t i = 0; i < vAddr.size(); i++) { Add_(vAddr[i], fForce); diff --git a/src/seeder/db.cpp b/src/seeder/db.cpp --- a/src/seeder/db.cpp +++ b/src/seeder/db.cpp @@ -135,7 +135,7 @@ nDirty++; } -void CAddrDb::Add_(const CAddress &addr, bool force) { +void CAddrDb::Add_(const CSeederAddress &addr, bool force) { if (!force && !addr.IsRoutable()) { return; } diff --git a/src/seeder/main.cpp b/src/seeder/main.cpp --- a/src/seeder/main.cpp +++ b/src/seeder/main.cpp @@ -1,3 +1,4 @@ +#include "address.h" #include "bitcoin.h" #include "clientversion.h" #include "db.h" @@ -13,8 +14,6 @@ #include #include -bool fTestNet = false; - class CDnsSeedOpts { public: int nThreads; @@ -183,7 +182,7 @@ continue; } - std::vector addr; + std::vector addr; for (size_t i = 0; i < ips.size(); i++) { CSeederServiceResult &res = ips[i]; res.nBanTime = 0; @@ -507,10 +506,10 @@ bool fDNS = true; if (opts.fUseTestNet) { printf("Using testnet.\n"); - pchMessageStart[0] = 0xf4; - pchMessageStart[1] = 0xe5; - pchMessageStart[2] = 0xf3; - pchMessageStart[3] = 0xf4; + netMagic[0] = 0xf4; + netMagic[1] = 0xe5; + netMagic[2] = 0xf3; + netMagic[3] = 0xf4; seeds = testnet_seeds; fTestNet = true; } diff --git a/src/seeder/protocol.h b/src/seeder/protocol.h --- a/src/seeder/protocol.h +++ b/src/seeder/protocol.h @@ -17,22 +17,10 @@ #include #include -extern bool fTestNet; -static inline unsigned short GetDefaultPort(const bool testnet = fTestNet) { - return testnet ? 18333 : 8333; -} - -// -// Message header -// (4) message start -// (12) command -// (4) size -// (4) checksum - -extern uint8_t pchMessageStart[4]; - class CMessageHeader { public: + typedef uint8_t MessageMagic[4]; + CMessageHeader(); CMessageHeader(const char *pszCommand, unsigned int nMessageSizeIn); @@ -54,7 +42,7 @@ // TODO: make private (improves encapsulation) public: enum { COMMAND_SIZE = 12 }; - char pchMessageStart[sizeof(::pchMessageStart)]; + MessageMagic pchMessageStart; char pchCommand[COMMAND_SIZE]; unsigned int nMessageSize; unsigned int nChecksum; @@ -67,45 +55,6 @@ NODE_BITCOIN_CASH = (1 << 5), }; -class CAddress : public CSeederService { -public: - CAddress(); - CAddress(CSeederService ipIn, - uint64_t nServicesIn = NODE_NETWORK | NODE_BITCOIN_CASH); - - void Init(); - - ADD_SERIALIZE_METHODS; - - template - inline void SerializationOp(Stream &s, Operation ser_action) { - int nVersion = s.GetVersion(); - CAddress *pthis = const_cast(this); - CSeederService *pip = (CSeederService *)pthis; - if (ser_action.ForRead()) { - pthis->Init(); - } - if (s.GetType() & SER_DISK) { - READWRITE(nVersion); - } - if ((s.GetType() & SER_DISK) || - (nVersion >= 31402 && !(s.GetType() & SER_GETHASH))) { - READWRITE(nTime); - } - READWRITE(nServices); - READWRITE(*pip); - } - - void print() const; - - // TODO: make private (improves encapsulation) -public: - uint64_t nServices; - - // disk and network only - unsigned int nTime; -}; - class CInv { public: CInv(); diff --git a/src/seeder/protocol.cpp b/src/seeder/protocol.cpp --- a/src/seeder/protocol.cpp +++ b/src/seeder/protocol.cpp @@ -70,25 +70,6 @@ return true; } -CAddress::CAddress() : CSeederService() { - Init(); -} - -CAddress::CAddress(CSeederService ipIn, uint64_t nServicesIn) - : CSeederService(ipIn) { - Init(); - nServices = nServicesIn; -} - -void CAddress::Init() { - nServices = NODE_NETWORK | NODE_BITCOIN_CASH; - nTime = 100000000; -} - -void CAddress::print() const { - printf("CAddress(%s)\n", ToString().c_str()); -} - CInv::CInv() { type = 0; hash.SetNull();