diff --git a/src/seeder/bitcoin.h b/src/seeder/bitcoin.h --- a/src/seeder/bitcoin.h +++ b/src/seeder/bitcoin.h @@ -1,5 +1,5 @@ -#ifndef _BITCOIN_H_ -#define _BITCOIN_H_ 1 +#ifndef BITCOIN_SEEDER_BITCOIN_H +#define BITCOIN_SEEDER_BITCOIN_H #include "protocol.h" diff --git a/src/seeder/bitcoin.cpp b/src/seeder/bitcoin.cpp --- a/src/seeder/bitcoin.cpp +++ b/src/seeder/bitcoin.cpp @@ -1,14 +1,13 @@ -#include +#include "bitcoin.h" #include "db.h" #include "netbase.h" -#include "protocol.h" #include "serialize.h" #include "uint256.h" -#define BITCOIN_SEED_NONCE 0x0539a019ca550825ULL +#include -using namespace std; +#define BITCOIN_SEED_NONCE 0x0539a019ca550825ULL class CNode { SOCKET sock; @@ -17,19 +16,14 @@ unsigned int nHeaderStart; unsigned int nMessageStart; int nVersion; - string strSubVer; + std::string strSubVer; int nStartingHeight; - vector *vAddr; + std::vector *vAddr; int ban; int64 doneAfter; CAddress you; - int GetTimeout() { - if (you.IsTor()) - return 120; - else - return 30; - } + int GetTimeout() { return you.IsTor() ? 120 : 30; } void BeginMessage(const char *pszCommand) { if (nHeaderStart != -1) AbortMessage(); @@ -79,13 +73,13 @@ } void PushVersion() { - int64 nTime = time(NULL); + int64 nTime = time(nullptr); uint64 nLocalNonce = BITCOIN_SEED_NONCE; int64 nLocalServices = 0; CAddress me(CService("0.0.0.0")); BeginMessage("version"); int nBestHeight = GetRequireHeight(); - string ver = "/bitcoin-seeder:0.01/"; + std::string ver = "/bitcoin-seeder:0.01/"; vSend << PROTOCOL_VERSION << nLocalServices << nTime << you << me << nLocalNonce << ver << nBestHeight; EndMessage(); @@ -96,13 +90,13 @@ if (vAddr) { BeginMessage("getaddr"); EndMessage(); - doneAfter = time(NULL) + GetTimeout(); + doneAfter = time(nullptr) + GetTimeout(); } else { - doneAfter = time(NULL) + 1; + doneAfter = time(nullptr) + 1; } } - bool ProcessMessage(string strCommand, CDataStream &vRecv) { + bool ProcessMessage(std::string strCommand, CDataStream &vRecv) { // printf("%s: RECV %s\n", ToString(you).c_str(), // strCommand.c_str()); if (strCommand == "version") { @@ -120,27 +114,27 @@ BeginMessage("verack"); EndMessage(); } - vSend.SetVersion(min(nVersion, PROTOCOL_VERSION)); + vSend.SetVersion(std::min(nVersion, PROTOCOL_VERSION)); if (nVersion < 209) { - this->vRecv.SetVersion(min(nVersion, PROTOCOL_VERSION)); + this->vRecv.SetVersion(std::min(nVersion, PROTOCOL_VERSION)); GotVersion(); } return false; } if (strCommand == "verack") { - this->vRecv.SetVersion(min(nVersion, PROTOCOL_VERSION)); + this->vRecv.SetVersion(std::min(nVersion, PROTOCOL_VERSION)); GotVersion(); return false; } if (strCommand == "addr" && vAddr) { - vector vAddrNew; + std::vector vAddrNew; vRecv >> vAddrNew; // printf("%s: got %i addresses\n", ToString(you).c_str(), // (int)vAddrNew.size()); - int64 now = time(NULL); - vector::iterator it = vAddrNew.begin(); + int64 now = time(nullptr); + std::vector::iterator it = vAddrNew.begin(); if (vAddrNew.size() > 1) { if (doneAfter == 0 || doneAfter > now + 1) doneAfter = now + 1; } @@ -180,8 +174,8 @@ break; } vRecv.erase(vRecv.begin(), pstart); - vector vHeaderSave(vRecv.begin(), - vRecv.begin() + nHeaderSize); + std::vector vHeaderSave(vRecv.begin(), + vRecv.begin() + nHeaderSize); CMessageHeader hdr; vRecv >> hdr; if (!hdr.IsValid()) { @@ -189,7 +183,7 @@ ban = 100000; return true; } - string strCommand = hdr.GetCommand(); + std::string strCommand = hdr.GetCommand(); unsigned int nMessageSize = hdr.nMessageSize; if (nMessageSize > MAX_SIZE) { // printf("%s: BAD (message too large)\n", @@ -220,14 +214,14 @@ } public: - CNode(const CService &ip, vector *vAddrIn) + CNode(const CService &ip, std::vector *vAddrIn) : you(ip), nHeaderStart(-1), nMessageStart(-1), vAddr(vAddrIn), ban(0), doneAfter(0), nVersion(0) { vSend.SetType(SER_NETWORK); vSend.SetVersion(0); vRecv.SetType(SER_NETWORK); vRecv.SetVersion(0); - if (time(NULL) > 1329696000) { + if (time(nullptr) > 1329696000) { vSend.SetVersion(209); vRecv.SetVersion(209); } @@ -238,9 +232,9 @@ PushVersion(); Send(); int64 now; - while (now = time(NULL), ban == 0 && - (doneAfter == 0 || doneAfter > now) && - sock != INVALID_SOCKET) { + while (now = time(nullptr), ban == 0 && + (doneAfter == 0 || doneAfter > now) && + sock != INVALID_SOCKET) { char pchBuf[0x10000]; fd_set set; FD_ZERO(&set); @@ -253,7 +247,7 @@ wa.tv_sec = GetTimeout(); wa.tv_usec = 0; } - int ret = select(sock + 1, &set, NULL, &set, &wa); + int ret = select(sock + 1, &set, nullptr, &set, &wa); if (ret != 1) { if (!doneAfter) res = false; break; @@ -293,7 +287,8 @@ }; bool TestNode(const CService &cip, int &ban, int &clientV, - std::string &clientSV, int &blocks, vector *vAddr) { + std::string &clientSV, int &blocks, + std::vector *vAddr) { try { CNode node(cip, vAddr); bool ret = node.Run(); @@ -316,7 +311,7 @@ /* int main(void) { CService ip("bitcoin.sipa.be", 8333, true); - vector vAddr; + std::vector vAddr; vAddr.clear(); int ban = 0; bool ret = TestNode(ip, ban, vAddr); diff --git a/src/seeder/compat.h b/src/seeder/compat.h --- a/src/seeder/compat.h +++ b/src/seeder/compat.h @@ -2,8 +2,8 @@ // Copyright (c) 2009-2012 The Bitcoin developers // Distributed under the MIT/X11 software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. -#ifndef _BITCOIN_COMPAT_H -#define _BITCOIN_COMPAT_H 1 +#ifndef BITCOIN_SEEDER_COMPAT_H +#define BITCOIN_SEEDER_COMPAT_H #ifdef WIN32 #define _WIN32_WINNT 0x0501 diff --git a/src/seeder/db.h b/src/seeder/db.h --- a/src/seeder/db.h +++ b/src/seeder/db.h @@ -1,15 +1,17 @@ -#include -#include +#ifndef BITCOIN_SEEDER_DB_H +#define BITCOIN_SEEDER_DB_H +#include "netbase.h" +#include "protocol.h" +#include "util.h" + +#include +#include #include #include #include #include -#include "netbase.h" -#include "protocol.h" -#include "util.h" - #define MIN_RETRY 1000 #define REQUIRE_VERSION 70001 @@ -18,7 +20,7 @@ return testnet ? 500000 : 350000; } -std::string static inline ToString(const CService &ip) { +static inline std::string ToString(const CService &ip) { std::string str = ip.ToString(); while (str.size() < 22) str += ' '; @@ -162,9 +164,9 @@ friend class CAddrDb; - IMPLEMENT_SERIALIZE(unsigned char version = 4; READWRITE(version); - READWRITE(ip); READWRITE(services); READWRITE(lastTry); - unsigned char tried = ourLastTry != 0; READWRITE(tried); + IMPLEMENT_SERIALIZE(uint8_t version = 4; READWRITE(version); READWRITE(ip); + READWRITE(services); READWRITE(lastTry); + uint8_t tried = ourLastTry != 0; READWRITE(tried); if (tried) { READWRITE(ourLastTry); READWRITE(ignoreTill); @@ -216,39 +218,45 @@ class CAddrDb { private: mutable CCriticalSection cs; - int nId; // number of address id's - std::map - idToInfo; // map address id to address info (b,c,d,e) - std::map ipToId; // map ip to id (b,c,d,e) - std::deque ourId; // sequence of tried nodes, in order we have tried - // connecting to them (c,d) - std::set unkId; // set of nodes not yet tried (b) - std::set goodId; // set of good nodes (d, good e) + // number of address id's + int nId; + // map address id to address info (b,c,d,e) + std::map idToInfo; + // map ip to id (b,c,d,e) + std::map ipToId; + // sequence of tried nodes, in order we have tried connecting to them (c,d) + std::deque ourId; + // set of nodes not yet tried (b) + std::set unkId; + // set of good nodes (d, good e) + std::set goodId; int nDirty; protected: // internal routines that assume proper locks are acquired - void Add_(const CAddress &addr, bool force); // add an address - bool Get_(CServiceResult &ip, int &wait); // get an IP to test (must call - // Good_, Bad_, or Skipped_ on - // result afterwards) + // add an address + void Add_(const CAddress &addr, bool force); + // get an IP to test (must call Good_, Bad_, or Skipped_ on result + // afterwards) + bool Get_(CServiceResult &ip, int &wait); bool GetMany_(std::vector &ips, int max, int &wait); - void - Good_(const CService &ip, int clientV, std::string clientSV, - int blocks); // mark an IP as good (must have been returned by Get_) - void Bad_(const CService &ip, int ban); // mark an IP as bad (and optionally - // ban it) (must have been returned - // by Get_) - void Skipped_(const CService &ip); // mark an IP as skipped (must have been - // returned by Get_) - int Lookup_(const CService &ip); // look up id of an IP - void - GetIPs_(std::set &ips, uint64_t requestedFlags, int max, - const bool *nets); // get a random set of IPs (shared lock only) + // mark an IP as good (must have been returned by Get_) + void Good_(const CService &ip, int clientV, std::string clientSV, + int blocks); + // mark an IP as bad (and optionally ban it) (must have been returned by + // Get_) + void Bad_(const CService &ip, int ban); + // mark an IP as skipped (must have been returned by Get_) + void Skipped_(const CService &ip); + // look up id of an IP + int Lookup_(const CService &ip); + // get a random set of IPs (shared lock only) + void GetIPs_(std::set &ips, uint64_t requestedFlags, int max, + const bool *nets); public: - std::map - banned; // nodes that are banned, with their unban time (a) + // nodes that are banned, with their unban time (a) + std::map banned; void GetStats(CAddrDbStats &stats) { SHARED_CRITICAL_BLOCK(cs) { @@ -257,7 +265,7 @@ stats.nTracked = ourId.size(); stats.nGood = goodId.size(); stats.nNew = unkId.size(); - stats.nAge = time(NULL) - idToInfo[ourId[0]].ourLastTry; + stats.nAge = time(nullptr) - idToInfo[ourId[0]].ourLastTry; } } @@ -395,3 +403,5 @@ GetIPs_(ips, requestedFlags, max, nets); } }; + +#endif diff --git a/src/seeder/db.cpp b/src/seeder/db.cpp --- a/src/seeder/db.cpp +++ b/src/seeder/db.cpp @@ -1,10 +1,9 @@ #include "db.h" -#include -using namespace std; +#include void CAddrInfo::Update(bool good) { - uint32_t now = time(NULL); + uint32_t now = time(nullptr); if (ourLastTry == 0) ourLastTry = now - MIN_RETRY; int age = now - ourLastTry; lastTry = now; @@ -36,7 +35,7 @@ } bool CAddrDb::Get_(CServiceResult &ip, int &wait) { - int64 now = time(NULL); + int64 now = time(nullptr); int cont = 0; int tot = unkId.size() + ourId.size(); if (tot == 0) { @@ -47,13 +46,14 @@ int rnd = rand() % tot; int ret; if (rnd < unkId.size()) { - set::iterator it = unkId.end(); + std::set::iterator it = unkId.end(); it--; ret = *it; unkId.erase(it); } else { ret = ourId.front(); - if (time(NULL) - idToInfo[ret].ourLastTry < MIN_RETRY) return false; + if (time(nullptr) - idToInfo[ret].ourLastTry < MIN_RETRY) + return false; ourId.pop_front(); } if (idToInfo[ret].ignoreTill && idToInfo[ret].ignoreTill < now) { @@ -100,7 +100,7 @@ unkId.erase(id); CAddrInfo &info = idToInfo[id]; info.Update(false); - uint32_t now = time(NULL); + uint32_t now = time(nullptr); int ter = info.GetBanTime(); if (ter) { // printf("%s: terrible\n", ToString(addr).c_str()); @@ -137,7 +137,7 @@ CService ipp(addr); if (banned.count(ipp)) { time_t bantime = banned[ipp]; - if (force || (bantime < time(NULL) && addr.nTime > bantime)) + if (force || (bantime < time(nullptr) && addr.nTime > bantime)) banned.erase(ipp); else return; @@ -169,7 +169,7 @@ nDirty++; } -void CAddrDb::GetIPs_(set &ips, uint64_t requestedFlags, int max, +void CAddrDb::GetIPs_(std::set &ips, uint64_t requestedFlags, int max, const bool *nets) { if (goodId.size() == 0) { int id = -1; @@ -197,11 +197,12 @@ if (max > goodIdFiltered.size() / 2) max = goodIdFiltered.size() / 2; if (max < 1) max = 1; - set ids; + std::set ids; while (ids.size() < max) { ids.insert(goodIdFiltered[rand() % goodIdFiltered.size()]); } - for (set::const_iterator it = ids.begin(); it != ids.end(); it++) { + for (std::set::const_iterator it = ids.begin(); it != ids.end(); + it++) { CService &ip = idToInfo[*it].ip; if (nets[ip.GetNetwork()]) ips.insert(ip); } diff --git a/src/seeder/dns.h b/src/seeder/dns.h --- a/src/seeder/dns.h +++ b/src/seeder/dns.h @@ -1,13 +1,13 @@ -#ifndef _DNS_H_ -#define _DNS_H_ 1 +#ifndef BITCOIN_SEEDER_DNS_H +#define BITCOIN_SEEDER_DNS_H 1 #include typedef struct { int v; union { - unsigned char v4[4]; - unsigned char v6[16]; + uint8_t v4[4]; + uint8_t v6[16]; } data; } addr_t; diff --git a/src/seeder/dns.c b/src/seeder/dns.c --- a/src/seeder/dns.c +++ b/src/seeder/dns.c @@ -1,8 +1,9 @@ +#include "dns.h" + #include #include #include #include -#include #include #include #include @@ -12,8 +13,6 @@ #include #include -#include "dns.h" - #define BUFLEN 512 #if defined IP_RECVDSTADDR @@ -35,7 +34,7 @@ union control_data { struct cmsghdr cmsg; - unsigned char data[DSTADDR_DATASIZE]; + uint8_t data[DSTADDR_DATASIZE]; }; typedef enum { CLASS_IN = 1, QCLASS_ANY = 255 } dns_class; @@ -55,8 +54,8 @@ // -1: premature end of input, forward reference, component > 63 char, invalid // character // -2: insufficient space in output -int static parse_name(const unsigned char **inpos, const unsigned char *inend, - const unsigned char *inbuf, char *buf, size_t bufsize) { +static int parse_name(const uint8_t **inpos, const uint8_t *inend, + const uint8_t *inbuf, char *buf, size_t bufsize) { size_t bufused = 0; int init = 1; do { @@ -78,7 +77,7 @@ if (*inpos == inend) return -1; int ref = ((octet - 0xC0) << 8) + *((*inpos)++); if (ref < 0 || ref >= (*inpos) - inbuf - 2) return -1; - const unsigned char *newbuf = inbuf + ref; + const uint8_t *newbuf = inbuf + ref; return parse_name(&newbuf, (*inpos) - 2, inbuf, buf + bufused, bufsize - bufused); } @@ -99,8 +98,8 @@ // -1: component > 63 characters // -2: insufficent space in output // -3: two subsequent dots -int static write_name(unsigned char **outpos, const unsigned char *outend, - const char *name, int offset) { +static int write_name(uint8_t **outpos, const uint8_t *outend, const char *name, + int offset) { while (*name != 0) { char *dot = strchr(name, '.'); const char *fin = dot; @@ -126,10 +125,10 @@ return 0; } -int static write_record(unsigned char **outpos, const unsigned char *outend, +static int write_record(uint8_t **outpos, const uint8_t *outend, const char *name, int offset, dns_type typ, dns_class cls, int ttl) { - unsigned char *oldpos = *outpos; + uint8_t *oldpos = *outpos; int error = 0; // name int ret = write_name(outpos, outend, name, offset); @@ -158,11 +157,11 @@ return error; } -int static write_record_a(unsigned char **outpos, const unsigned char *outend, +static int write_record_a(uint8_t **outpos, const uint8_t *outend, const char *name, int offset, dns_class cls, int ttl, const addr_t *ip) { if (ip->v != 4) return -6; - unsigned char *oldpos = *outpos; + uint8_t *oldpos = *outpos; int error = 0; int ret = write_record(outpos, outend, name, offset, TYPE_A, cls, ttl); if (ret) return ret; @@ -182,12 +181,11 @@ return error; } -int static write_record_aaaa(unsigned char **outpos, - const unsigned char *outend, const char *name, - int offset, dns_class cls, int ttl, - const addr_t *ip) { +static int write_record_aaaa(uint8_t **outpos, const uint8_t *outend, + const char *name, int offset, dns_class cls, + int ttl, const addr_t *ip) { if (ip->v != 6) return -6; - unsigned char *oldpos = *outpos; + uint8_t *oldpos = *outpos; int error = 0; int ret = write_record(outpos, outend, name, offset, TYPE_AAAA, cls, ttl); if (ret) return ret; @@ -207,10 +205,9 @@ return error; } -int static write_record_ns(unsigned char **outpos, const unsigned char *outend, - char *name, int offset, dns_class cls, int ttl, - const char *ns) { - unsigned char *oldpos = *outpos; +static int write_record_ns(uint8_t **outpos, const uint8_t *outend, char *name, + int offset, dns_class cls, int ttl, const char *ns) { + uint8_t *oldpos = *outpos; int ret = write_record(outpos, outend, name, offset, TYPE_NS, cls, ttl); if (ret) return ret; int error = 0; @@ -219,7 +216,7 @@ goto error; } (*outpos) += 2; - unsigned char *curpos = *outpos; + uint8_t *curpos = *outpos; ret = write_name(outpos, outend, ns, -1); if (ret) { error = ret; @@ -233,12 +230,12 @@ return error; } -int static write_record_soa(unsigned char **outpos, const unsigned char *outend, - char *name, int offset, dns_class cls, int ttl, +static int write_record_soa(uint8_t **outpos, const uint8_t *outend, char *name, + int offset, dns_class cls, int ttl, const char *mname, const char *rname, uint32_t serial, uint32_t refresh, uint32_t retry, uint32_t expire, uint32_t minimum) { - unsigned char *oldpos = *outpos; + uint8_t *oldpos = *outpos; int ret = write_record(outpos, outend, name, offset, TYPE_SOA, cls, ttl); if (ret) return ret; int error = 0; @@ -247,7 +244,7 @@ goto error; } (*outpos) += 2; - unsigned char *curpos = *outpos; + uint8_t *curpos = *outpos; ret = write_name(outpos, outend, mname, -1); if (ret) { error = ret; @@ -290,8 +287,8 @@ return error; } -ssize_t static dnshandle(dns_opt_t *opt, const unsigned char *inbuf, - size_t insize, unsigned char *outbuf) { +static ssize_t dnshandle(dns_opt_t *opt, const uint8_t *inbuf, size_t insize, + uint8_t *outbuf) { int error = 0; if (insize < 12) // DNS header return -1; @@ -327,8 +324,8 @@ error = 4; goto error; } - const unsigned char *inpos = inbuf + 12; - const unsigned char *inend = inbuf + insize; + const uint8_t *inpos = inbuf + 12; + const uint8_t *inend = inbuf + insize; char name[256]; int offset = inpos - inbuf; int ret = parse_name(&inpos, inend, inbuf, name, 256); @@ -369,8 +366,8 @@ int cls = (inpos[2] << 8) + inpos[3]; inpos += 4; - unsigned char *outpos = outbuf + (inpos - inbuf); - unsigned char *outend = outbuf + BUFLEN; + uint8_t *outpos = outbuf + (inpos - inbuf); + uint8_t *outend = outbuf + BUFLEN; // printf("DNS: Request host='%s' type=%i class=%i\n", name, typ, cls); @@ -381,7 +378,7 @@ if (!((typ == TYPE_NS || typ == QTYPE_ANY) && (cls == CLASS_IN || cls == QCLASS_ANY))) { // authority section will be necessary, either NS or SOA - unsigned char *newpos = outpos; + uint8_t *newpos = outpos; write_record_ns(&newpos, outend, "", offset, CLASS_IN, 0, opt->ns); max_auth_size = newpos - outpos; @@ -520,7 +517,7 @@ return -2; } - unsigned char inbuf[BUFLEN], outbuf[BUFLEN]; + uint8_t inbuf[BUFLEN], outbuf[BUFLEN]; struct iovec iov[1] = { { .iov_base = inbuf, .iov_len = sizeof(inbuf), @@ -537,7 +534,7 @@ }; for (; 1; ++(opt->nRequests)) { ssize_t insize = recvmsg(listenSocket, &msg, 0); - // unsigned char *addr = (unsigned char*)&si_other.sin_addr.s_addr; + // uint8_t *addr = (uint8_t*)&si_other.sin_addr.s_addr; // printf("DNS: Request %llu from %i.%i.%i.%i:%i of %i bytes\n", // (unsigned long long)(opt->nRequests), addr[0], addr[1], addr[2], // addr[3], ntohs(si_other.sin_port), (int)insize); diff --git a/src/seeder/main.cpp b/src/seeder/main.cpp --- a/src/seeder/main.cpp +++ b/src/seeder/main.cpp @@ -1,18 +1,16 @@ -#include +#include "bitcoin.h" +#include "db.h" -#define __STDC_FORMAT_MACROS +#include #include +#include +#include #include -#include #include #include -#include -#include -#include "bitcoin.h" -#include "db.h" - -using namespace std; +#define __STDC_FORMAT_MACROS +#include bool fTestNet = false; @@ -33,9 +31,9 @@ std::set filter_whitelist; CDnsSeedOpts() - : nThreads(96), nDnsThreads(4), nPort(53), mbox(NULL), ns(NULL), - host(NULL), tor(NULL), fUseTestNet(false), fWipeBan(false), - fWipeIgnore(false), ipv4_proxy(NULL), ipv6_proxy(NULL) {} + : nThreads(96), nDnsThreads(4), nPort(53), mbox(nullptr), ns(nullptr), + host(nullptr), tor(nullptr), fUseTestNet(false), fWipeBan(false), + fWipeIgnore(false), ipv4_proxy(nullptr), ipv6_proxy(nullptr) {} void ParseCommandLine(int argc, char **argv) { static const char *help = @@ -100,19 +98,19 @@ } case 't': { - int n = strtol(optarg, NULL, 10); + int n = strtol(optarg, nullptr, 10); if (n > 0 && n < 1000) nThreads = n; break; } case 'd': { - int n = strtol(optarg, NULL, 10); + int n = strtol(optarg, nullptr, 10); if (n > 0 && n < 1000) nDnsThreads = n; break; } case 'p': { - int p = strtol(optarg, NULL, 10); + int p = strtol(optarg, nullptr, 10); if (p > 0 && p < 65536) nPort = p; break; } @@ -161,7 +159,7 @@ filter_whitelist.insert(NODE_NETWORK | NODE_BITCOIN_CASH | NODE_BLOOM | NODE_XTHIN); } - if (host != NULL && ns == NULL) showHelp = true; + if (host != nullptr && ns == nullptr) showHelp = true; if (showHelp) fprintf(stderr, help, argv[0]); } }; @@ -178,14 +176,14 @@ std::vector ips; int wait = 5; db.GetMany(ips, 16, wait); - int64 now = time(NULL); + int64 now = time(nullptr); if (ips.empty()) { wait *= 1000; wait += rand() % (500 * *nThreads); Sleep(wait); continue; } - vector addr; + std::vector addr; for (int i = 0; i < ips.size(); i++) { CServiceResult &res = ips[i]; res.nBanTime = 0; @@ -193,9 +191,9 @@ res.nHeight = 0; res.strClientV = ""; bool getaddr = res.ourLastSuccess + 86400 < now; - res.fGood = - TestNode(res.service, res.nBanTime, res.nClientV, - res.strClientV, res.nHeight, getaddr ? &addr : NULL); + res.fGood = TestNode(res.service, res.nBanTime, res.nClientV, + res.strClientV, res.nHeight, + getaddr ? &addr : nullptr); } db.ResultMany(ips); db.Add(addr); @@ -228,7 +226,7 @@ nets[NET_IPV4] = true; nets[NET_IPV6] = true; } - time_t now = time(NULL); + time_t now = time(nullptr); FlagSpecificData &thisflag = perflag[requestedFlags]; thisflag.cacheHits++; if (force || @@ -237,24 +235,23 @@ (thisflag.cacheHits * thisflag.cacheHits * 20 > thisflag.cache.size() && (now - thisflag.cacheTime > 5))) { - set ips; + std::set ips; db.GetIPs(ips, requestedFlags, 1000, nets); dbQueries++; thisflag.cache.clear(); thisflag.nIPv4 = 0; thisflag.nIPv6 = 0; thisflag.cache.reserve(ips.size()); - for (set::iterator it = ips.begin(); it != ips.end(); - it++) { + for (auto &ip : ips) { struct in_addr addr; struct in6_addr addr6; - if ((*it).GetInAddr(&addr)) { + if (ip.GetInAddr(&addr)) { addr_t a; a.v = 4; memcpy(&a.data.v4, &addr, 4); thisflag.cache.push_back(a); thisflag.nIPv4++; - } else if ((*it).GetIn6Addr(&addr6)) { + } else if (ip.GetIn6Addr(&addr6)) { addr_t a; a.v = 6; memcpy(&a.data.v6, &addr6, 16); @@ -328,7 +325,7 @@ return max; } -vector dnsThread; +std::vector dnsThread; extern "C" void *ThreadDNS(void *arg) { CDnsThread *thread = (CDnsThread *)arg; @@ -351,11 +348,14 @@ extern "C" void *ThreadDumper(void *) { int count = 0; do { - Sleep(100000 << count); // First 100s, than 200s, 400s, 800s, 1600s, and - // then 3200s forever - if (count < 5) count++; + // First 100s, than 200s, 400s, 800s, 1600s, and then 3200s forever + Sleep(100000 << count); + if (count < 5) { + count++; + } + { - vector v = db.GetAll(); + std::vector v = db.GetAll(); sort(v.begin(), v.end(), StatCompare); FILE *f = fopen("dnsseed.dat.new", "w+"); if (f) { @@ -370,9 +370,7 @@ "lastSuccess %%(2h) %%(8h) %%(1d) %%(7d) " "%%(30d) blocks svcs version\n"); double stat[5] = {0, 0, 0, 0, 0}; - for (vector::const_iterator it = v.begin(); - it < v.end(); it++) { - CAddrReport rep = *it; + for (CAddrReport rep : v) { fprintf( d, "%-47s %4d %11" PRId64 @@ -392,8 +390,8 @@ fclose(d); FILE *ff = fopen("dnsstats.log", "a"); fprintf(ff, "%llu %g %g %g %g %g\n", - (unsigned long long)(time(NULL)), stat[0], stat[1], stat[2], - stat[3], stat[4]); + (unsigned long long)(time(nullptr)), stat[0], stat[1], + stat[2], stat[3], stat[4]); fclose(ff); } } while (1); @@ -404,7 +402,7 @@ bool first = true; do { char c[256]; - time_t tim = time(NULL); + time_t tim = time(nullptr); struct tm *tmp = localtime(&tim); strftime(c, 256, "[%y-%m-%d %H:%M:%S]", tmp); CAddrDbStats stats; @@ -432,17 +430,18 @@ return nullptr; } -static const string mainnet_seeds[] = { +static const std::string mainnet_seeds[] = { "seed.bitcoinabc.org", "seed-abc.bitcoinforks.org", "seed.bitprim.org", "seed.deadalnix.me", "seeder.criptolayer.net", ""}; -static const string testnet_seeds[] = {"testnet-seed.bitcoinabc.org", - "testnet-seed-abc.bitcoinforks.org", - "testnet-seed.bitcoinunlimited.info", - "testnet-seed.bitprim.org", - "testnet-seed.deadalnix.me", - "testnet-seeder.criptolayer.net", - ""}; -static const string *seeds = mainnet_seeds; +static const std::string testnet_seeds[] = { + "testnet-seed.bitcoinabc.org", + "testnet-seed-abc.bitcoinforks.org", + "testnet-seed.bitcoinunlimited.info", + "testnet-seed.bitprim.org", + "testnet-seed.deadalnix.me", + "testnet-seeder.criptolayer.net", + ""}; +static const std::string *seeds = mainnet_seeds; extern "C" void *ThreadSeeder(void *) { if (!fTestNet) { @@ -450,11 +449,10 @@ } do { for (int i = 0; seeds[i] != ""; i++) { - vector ips; + std::vector ips; LookupHost(seeds[i].c_str(), ips); - for (vector::iterator it = ips.begin(); it != ips.end(); - it++) { - db.Add(CService(*it, GetDefaultPort()), true); + for (auto &ip : ips) { + db.Add(CService(ip, GetDefaultPort()), true); } } Sleep(1800000); @@ -464,7 +462,7 @@ int main(int argc, char **argv) { signal(SIGPIPE, SIG_IGN); - setbuf(stdout, NULL); + setbuf(stdout, nullptr); CDnsSeedOpts opts; opts.ParseCommandLine(argc, argv); printf("Supporting whitelisted filters: "); @@ -537,14 +535,14 @@ dnsThread.clear(); for (int i = 0; i < opts.nDnsThreads; i++) { dnsThread.push_back(new CDnsThread(&opts, i)); - pthread_create(&threadDns, NULL, ThreadDNS, dnsThread[i]); + pthread_create(&threadDns, nullptr, ThreadDNS, dnsThread[i]); printf("."); Sleep(20); } printf("done\n"); } printf("Starting seeder..."); - pthread_create(&threadSeed, NULL, ThreadSeeder, NULL); + pthread_create(&threadSeed, nullptr, ThreadSeeder, nullptr); printf("done\n"); printf("Starting %i crawler threads...", opts.nThreads); pthread_attr_t attr_crawler; @@ -556,8 +554,8 @@ } pthread_attr_destroy(&attr_crawler); printf("done\n"); - pthread_create(&threadStats, NULL, ThreadStats, NULL); - pthread_create(&threadDump, NULL, ThreadDumper, NULL); + pthread_create(&threadStats, nullptr, ThreadStats, nullptr); + pthread_create(&threadDump, nullptr, ThreadDumper, nullptr); void *res; pthread_join(threadDump, &res); return 0; diff --git a/src/seeder/netbase.h b/src/seeder/netbase.h --- a/src/seeder/netbase.h +++ b/src/seeder/netbase.h @@ -1,15 +1,15 @@ // Copyright (c) 2009-2012 The Bitcoin developers // Distributed under the MIT/X11 software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. -#ifndef BITCOIN_NETBASE_H -#define BITCOIN_NETBASE_H - -#include -#include +#ifndef BITCOIN_SEEDER_NETBASE_H +#define BITCOIN_SEEDER_NETBASE_H #include "compat.h" #include "serialize.h" +#include +#include + extern int nConnectTimeout; #ifdef WIN32 @@ -34,7 +34,7 @@ /** IP address (IPv6, or IPv4 using mapped IPv6 range (::FFFF:0:0/96)) */ class CNetAddr { protected: - unsigned char ip[16]; // in network byte order + uint8_t ip[16]; // in network byte order public: CNetAddr(); @@ -70,8 +70,8 @@ unsigned int GetByte(int n) const; uint64 GetHash() const; bool GetInAddr(struct in_addr *pipv4Addr) const; - std::vector GetGroup() const; - int GetReachabilityFrom(const CNetAddr *paddrPartner = NULL) const; + std::vector GetGroup() const; + int GetReachabilityFrom(const CNetAddr *paddrPartner = nullptr) const; void print() const; CNetAddr(const struct in6_addr &pipv6Addr); @@ -108,7 +108,7 @@ friend bool operator==(const CService &a, const CService &b); friend bool operator!=(const CService &a, const CService &b); friend bool operator<(const CService &a, const CService &b); - std::vector GetKey() const; + std::vector GetKey() const; std::string ToString() const; std::string ToStringPort() const; std::string ToStringIPPort() const; diff --git a/src/seeder/netbase.cpp b/src/seeder/netbase.cpp --- a/src/seeder/netbase.cpp +++ b/src/seeder/netbase.cpp @@ -4,19 +4,17 @@ // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include "netbase.h" +#include "strlcpy.h" #include "util.h" #ifndef WIN32 #include #endif -#include "strlcpy.h" #include // for to_lower() #define printf my_printf -using namespace std; - // Settings typedef std::pair proxyType; static proxyType proxyInfo[NET_MAX]; @@ -24,8 +22,7 @@ int nConnectTimeout = 5000; bool fNameLookup = false; -static const unsigned char pchIPv4[12] = {0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0xff, 0xff}; +static const uint8_t pchIPv4[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff}; enum Network ParseNetwork(std::string net) { boost::to_lower(net); @@ -41,15 +38,13 @@ // if a : is found, and it either follows a [...], or no other : is in the // string, treat it as port separator bool fHaveColon = colon != in.npos; - bool fBracketed = - fHaveColon && - (in[0] == '[' && in[colon - 1] == ']'); // if there is a colon, and - // in[0]=='[', colon is not 0, - // so in[colon-1] is safe + // if there is a colon, and in[0]=='[', colon is not 0, so in[colon-1] is + // safe + bool fBracketed = fHaveColon && (in[0] == '[' && in[colon - 1] == ']'); bool fMultiColon = fHaveColon && (in.find_last_of(':', colon - 1) != in.npos); if (fHaveColon && (colon == 0 || fBracketed || !fMultiColon)) { - char *endp = NULL; + char *endp = nullptr; int n = strtol(in.c_str() + colon + 1, &endp, 10); if (endp && *endp == 0 && n >= 0) { in = in.substr(0, colon); @@ -62,7 +57,7 @@ hostOut = in; } -bool static LookupIntern(const char *pszName, std::vector &vIP, +static bool LookupIntern(const char *pszName, std::vector &vIP, unsigned int nMaxSolutions, bool fAllowLookup) { vIP.clear(); @@ -86,12 +81,12 @@ aiHint.ai_family = AF_UNSPEC; aiHint.ai_flags = fAllowLookup ? AI_ADDRCONFIG : AI_NUMERICHOST; #endif - struct addrinfo *aiRes = NULL; - int nErr = getaddrinfo(pszName, NULL, &aiHint, &aiRes); + struct addrinfo *aiRes = nullptr; + int nErr = getaddrinfo(pszName, nullptr, &aiHint, &aiRes); if (nErr) return false; struct addrinfo *aiTrav = aiRes; - while (aiTrav != NULL && + while (aiTrav != nullptr && (nMaxSolutions == 0 || vIP.size() < nMaxSolutions)) { if (aiTrav->ai_family == AF_INET) { assert(aiTrav->ai_addrlen >= sizeof(sockaddr_in)); @@ -162,7 +157,7 @@ return Lookup(pszName, addr, portDefault, false); } -bool static Socks4(const CService &addrDest, SOCKET &hSocket) { +static bool Socks4(const CService &addrDest, SOCKET &hSocket) { printf("SOCKS4 connecting %s\n", addrDest.ToString().c_str()); if (!addrDest.IsIPv4()) { closesocket(hSocket); @@ -201,7 +196,7 @@ return true; } -bool static Socks5(string strDest, int port, SOCKET &hSocket) { +static bool Socks5(std::string strDest, int port, SOCKET &hSocket) { printf("SOCKS5 connecting %s\n", strDest.c_str()); if (strDest.size() > 255) { closesocket(hSocket); @@ -225,7 +220,7 @@ closesocket(hSocket); return error("Proxy failed to initialize"); } - string strSocks5("\5\1"); + std::string strSocks5("\5\1"); strSocks5 += '\000'; strSocks5 += '\003'; strSocks5 += static_cast(std::min((int)strDest.size(), 255)); @@ -304,7 +299,7 @@ return true; } -bool static ConnectSocketDirectly(const CService &addrConnect, +static bool ConnectSocketDirectly(const CService &addrConnect, SOCKET &hSocketRet, int nTimeout) { hSocketRet = INVALID_SOCKET; @@ -348,7 +343,7 @@ fd_set fdset; FD_ZERO(&fdset); FD_SET(hSocket, &fdset); - int nRet = select(hSocket + 1, NULL, &fdset, NULL, &timeout); + int nRet = select(hSocket + 1, nullptr, &fdset, nullptr, &timeout); if (nRet == 0) { printf("connection timeout\n"); closesocket(hSocket); @@ -477,9 +472,9 @@ bool ConnectSocketByName(CService &addr, SOCKET &hSocketRet, const char *pszDest, int portDefault, int nTimeout) { - string strDest; + std::string strDest; int port = portDefault; - SplitHostPort(string(pszDest), port, strDest); + SplitHostPort(std::string(pszDest), port, strDest); SOCKET hSocket = INVALID_SOCKET; CService addrResolved( @@ -514,13 +509,13 @@ memcpy(ip, ipIn.ip, sizeof(ip)); } -static const unsigned char pchOnionCat[] = {0xFD, 0x87, 0xD8, 0x7E, 0xEB, 0x43}; -static const unsigned char pchGarliCat[] = {0xFD, 0x60, 0xDB, 0x4D, 0xDD, 0xB5}; +static const uint8_t pchOnionCat[] = {0xFD, 0x87, 0xD8, 0x7E, 0xEB, 0x43}; +static const uint8_t pchGarliCat[] = {0xFD, 0x60, 0xDB, 0x4D, 0xDD, 0xB5}; bool CNetAddr::SetSpecial(const std::string &strName) { if (strName.size() > 6 && strName.substr(strName.size() - 6, 6) == ".onion") { - std::vector vchAddr = + std::vector vchAddr = DecodeBase32(strName.substr(0, strName.size() - 6).c_str()); if (vchAddr.size() != 16 - sizeof(pchOnionCat)) return false; memcpy(ip, pchOnionCat, sizeof(pchOnionCat)); @@ -530,7 +525,7 @@ } if (strName.size() > 11 && strName.substr(strName.size() - 11, 11) == ".oc.b32.i2p") { - std::vector vchAddr = + std::vector vchAddr = DecodeBase32(strName.substr(0, strName.size() - 11).c_str()); if (vchAddr.size() != 16 - sizeof(pchGarliCat)) return false; memcpy(ip, pchOnionCat, sizeof(pchGarliCat)); @@ -602,8 +597,8 @@ } bool CNetAddr::IsRFC6052() const { - static const unsigned char pchRFC6052[] = {0, 0x64, 0xFF, 0x9B, 0, 0, - 0, 0, 0, 0, 0, 0}; + static const uint8_t pchRFC6052[] = {0, 0x64, 0xFF, 0x9B, 0, 0, + 0, 0, 0, 0, 0, 0}; return (memcmp(ip, pchRFC6052, sizeof(pchRFC6052)) == 0); } @@ -613,7 +608,7 @@ } bool CNetAddr::IsRFC4862() const { - static const unsigned char pchRFC4862[] = {0xFE, 0x80, 0, 0, 0, 0, 0, 0}; + static const uint8_t pchRFC4862[] = {0xFE, 0x80, 0, 0, 0, 0, 0, 0}; return (memcmp(ip, pchRFC4862, sizeof(pchRFC4862)) == 0); } @@ -622,8 +617,8 @@ } bool CNetAddr::IsRFC6145() const { - static const unsigned char pchRFC6145[] = {0, 0, 0, 0, 0, 0, - 0, 0, 0xFF, 0xFF, 0, 0}; + static const uint8_t pchRFC6145[] = {0, 0, 0, 0, 0, 0, + 0, 0, 0xFF, 0xFF, 0, 0}; return (memcmp(ip, pchRFC6145, sizeof(pchRFC6145)) == 0); } @@ -645,8 +640,8 @@ if (IsIPv4() && (GetByte(3) == 127 || GetByte(3) == 0)) return true; // IPv6 loopback (::1/128) - static const unsigned char pchLocal[16] = {0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 1}; + static const uint8_t pchLocal[16] = {0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 1}; if (memcmp(ip, pchLocal, 16) == 0) return true; return false; @@ -667,7 +662,7 @@ if (memcmp(ip, pchIPv4 + 3, sizeof(pchIPv4) - 3) == 0) return false; // unspecified IPv6 address (::/128) - unsigned char ipNone[16] = {}; + uint8_t ipNone[16] = {}; if (memcmp(ip, ipNone, 16) == 0) return false; // documentation IPv6 address @@ -713,19 +708,21 @@ if (serv.GetSockAddr((struct sockaddr *)&sockaddr, &socklen)) { char name[1025] = ""; if (!getnameinfo((const struct sockaddr *)&sockaddr, socklen, name, - sizeof(name), NULL, 0, NI_NUMERICHOST)) + sizeof(name), nullptr, 0, NI_NUMERICHOST)) return std::string(name); } - if (IsIPv4()) + + if (IsIPv4()) { return strprintf("%u.%u.%u.%u", GetByte(3), GetByte(2), GetByte(1), GetByte(0)); - else - return strprintf( - "%x:%x:%x:%x:%x:%x:%x:%x", GetByte(15) << 8 | GetByte(14), - GetByte(13) << 8 | GetByte(12), GetByte(11) << 8 | GetByte(10), - GetByte(9) << 8 | GetByte(8), GetByte(7) << 8 | GetByte(6), - GetByte(5) << 8 | GetByte(4), GetByte(3) << 8 | GetByte(2), - GetByte(1) << 8 | GetByte(0)); + } + + return strprintf("%x:%x:%x:%x:%x:%x:%x:%x", GetByte(15) << 8 | GetByte(14), + GetByte(13) << 8 | GetByte(12), + GetByte(11) << 8 | GetByte(10), + GetByte(9) << 8 | GetByte(8), GetByte(7) << 8 | GetByte(6), + GetByte(5) << 8 | GetByte(4), GetByte(3) << 8 | GetByte(2), + GetByte(1) << 8 | GetByte(0)); } std::string CNetAddr::ToString() const { @@ -757,8 +754,8 @@ // get canonical identifier of an address' group // no two connections will be attempted to addresses with the same group -std::vector CNetAddr::GetGroup() const { - std::vector vchRet; +std::vector CNetAddr::GetGroup() const { + std::vector vchRet; int nClass = NET_IPV6; int nStartByte = 0; int nBits = 16; @@ -799,14 +796,14 @@ nClass = NET_I2P; nStartByte = 6; nBits = 4; - } - // for he.net, use /36 groups - else if (GetByte(15) == 0x20 && GetByte(14) == 0x11 && - GetByte(13) == 0x04 && GetByte(12) == 0x70) + } else if (GetByte(15) == 0x20 && GetByte(14) == 0x11 && + GetByte(13) == 0x04 && GetByte(12) == 0x70) { + // for he.net, use /36 groups nBits = 36; - // for the rest of the IPv6 network, use /32 groups - else + } else { + // for the rest of the IPv6 network, use /32 groups nBits = 32; + } vchRet.push_back(nClass); while (nBits >= 8) { @@ -835,8 +832,8 @@ // and only used in GetReachabilityFrom static const int NET_UNKNOWN = NET_MAX + 0; static const int NET_TEREDO = NET_MAX + 1; -int static GetExtNetwork(const CNetAddr *addr) { - if (addr == NULL) return NET_UNKNOWN; +static int GetExtNetwork(const CNetAddr *addr) { + if (addr == nullptr) return NET_UNKNOWN; if (addr->IsRFC4380()) return NET_TEREDO; return addr->GetNetwork(); } @@ -876,17 +873,16 @@ case NET_IPV4: return REACH_IPV4; case NET_IPV6: - return fTunnel ? REACH_IPV6_WEAK - : REACH_IPV6_STRONG; // only prefer giving - // our IPv6 address if - // it's not tunnelled + // only prefer giving our IPv6 address if it's not tunnelled + return fTunnel ? REACH_IPV6_WEAK : REACH_IPV6_STRONG; } case NET_TOR: switch (ourNet) { default: return REACH_DEFAULT; case NET_IPV4: - return REACH_IPV4; // Tor users can connect to IPv4 as well + // Tor users can connect to IPv4 as well + return REACH_IPV4; case NET_TOR: return REACH_PRIVATE; } @@ -921,11 +917,11 @@ case NET_IPV4: return REACH_IPV4; case NET_I2P: - return REACH_PRIVATE; // assume connections from unroutable - // addresses are + // assume connections from unroutable addresses are + return REACH_PRIVATE; case NET_TOR: - return REACH_PRIVATE; // either from Tor/I2P, or don't care - // about our address + // either from Tor/I2P, or don't care about our address + return REACH_PRIVATE; } } } @@ -1036,8 +1032,8 @@ return false; } -std::vector CService::GetKey() const { - std::vector vKey; +std::vector CService::GetKey() const { + std::vector vKey; vKey.resize(18); memcpy(&vKey[0], ip, 16); vKey[16] = port / 0x100; diff --git a/src/seeder/protocol.h b/src/seeder/protocol.h --- a/src/seeder/protocol.h +++ b/src/seeder/protocol.h @@ -7,8 +7,8 @@ #error This header can only be compiled as C++. #endif -#ifndef __INCLUDED_PROTOCOL_H__ -#define __INCLUDED_PROTOCOL_H__ +#ifndef BITCOIN_SEEDER_PROTOCOL_H +#define BITCOIN_SEEDER_PROTOCOL_H #include "netbase.h" #include "serialize.h" @@ -29,7 +29,7 @@ // (4) size // (4) checksum -extern unsigned char pchMessageStart[4]; +extern uint8_t pchMessageStart[4]; class CMessageHeader { public: diff --git a/src/seeder/protocol.cpp b/src/seeder/protocol.cpp --- a/src/seeder/protocol.cpp +++ b/src/seeder/protocol.cpp @@ -3,13 +3,13 @@ // Distributed under the MIT/X11 software license, see the accompanying // file license.txt or http://www.opensource.org/licenses/mit-license.php. -#include -#include - -#include "netbase.h" #include "protocol.h" +#include "netbase.h" #include "util.h" +#include +#include + #ifndef WIN32 #include #endif diff --git a/src/seeder/serialize.h b/src/seeder/serialize.h --- a/src/seeder/serialize.h +++ b/src/seeder/serialize.h @@ -2,8 +2,8 @@ // Copyright (c) 2011 The Bitcoin developers // Distributed under the MIT/X11 software license, see the accompanying // file license.txt or http://www.opensource.org/licenses/mit-license.php. -#ifndef BITCOIN_SERIALIZE_H -#define BITCOIN_SERIALIZE_H +#ifndef BITCOIN_SEEDER_SERIALIZE_H +#define BITCOIN_SEEDER_SERIALIZE_H #include #include @@ -41,7 +41,7 @@ #define mlock(p, n) VirtualLock((p), (n)); #define munlock(p, n) VirtualUnlock((p), (n)); #else -#include +#include #include /* This comes from limits.h if it's not defined there set a sane default */ #ifndef PAGESIZE @@ -138,7 +138,7 @@ inline unsigned int GetSerializeSize(signed char a, int, int = 0) { return sizeof(a); } -inline unsigned int GetSerializeSize(unsigned char a, int, int = 0) { +inline unsigned int GetSerializeSize(uint8_t a, int, int = 0) { return sizeof(a); } inline unsigned int GetSerializeSize(signed short a, int, int = 0) { @@ -181,7 +181,7 @@ WRITEDATA(s, a); } template -inline void Serialize(Stream &s, unsigned char a, int, int = 0) { +inline void Serialize(Stream &s, uint8_t a, int, int = 0) { WRITEDATA(s, a); } template @@ -234,7 +234,7 @@ READDATA(s, a); } template -inline void Unserialize(Stream &s, unsigned char &a, int, int = 0) { +inline void Unserialize(Stream &s, uint8_t &a, int, int = 0) { READDATA(s, a); } template @@ -302,31 +302,31 @@ // inline unsigned int GetSizeOfCompactSize(uint64 nSize) { if (nSize < 253) - return sizeof(unsigned char); + return sizeof(uint8_t); else if (nSize <= USHRT_MAX) - return sizeof(unsigned char) + sizeof(unsigned short); + return sizeof(uint8_t) + sizeof(unsigned short); else if (nSize <= UINT_MAX) - return sizeof(unsigned char) + sizeof(unsigned int); + return sizeof(uint8_t) + sizeof(unsigned int); else - return sizeof(unsigned char) + sizeof(uint64); + return sizeof(uint8_t) + sizeof(uint64); } template void WriteCompactSize(Stream &os, uint64 nSize) { if (nSize < 253) { - unsigned char chSize = nSize; + uint8_t chSize = nSize; WRITEDATA(os, chSize); } else if (nSize <= USHRT_MAX) { - unsigned char chSize = 253; + uint8_t chSize = 253; unsigned short xSize = nSize; WRITEDATA(os, chSize); WRITEDATA(os, xSize); } else if (nSize <= UINT_MAX) { - unsigned char chSize = 254; + uint8_t chSize = 254; unsigned int xSize = nSize; WRITEDATA(os, chSize); WRITEDATA(os, xSize); } else { - unsigned char chSize = 255; + uint8_t chSize = 255; uint64 xSize = nSize; WRITEDATA(os, chSize); WRITEDATA(os, xSize); @@ -335,7 +335,7 @@ } template uint64 ReadCompactSize(Stream &is) { - unsigned char chSize; + uint8_t chSize; READDATA(is, chSize); uint64 nSizeRet = 0; if (chSize < 253) { @@ -399,7 +399,7 @@ public: explicit CFixedFieldString(const std::string &str) - : pcstr(&str), pstr(NULL) {} + : pcstr(&str), pstr(nullptr) {} explicit CFixedFieldString(std::string &str) : pcstr(&str), pstr(&str) {} unsigned int GetSerializeSize(int, int = 0) const { return LEN; } @@ -411,7 +411,7 @@ } template void Unserialize(Stream &s, int, int = 0) { - if (pstr == NULL) + if (pstr == nullptr) throw std::ios_base::failure("CFixedFieldString::Unserialize : " "trying to unserialize to const " "string"); @@ -676,18 +676,17 @@ // inline unsigned int GetSerializeSize(const CScript &v, int nType, int nVersion) { - return GetSerializeSize((const std::vector &)v, nType, - nVersion); + return GetSerializeSize((const std::vector &)v, nType, nVersion); } template void Serialize(Stream &os, const CScript &v, int nType, int nVersion) { - Serialize(os, (const std::vector &)v, nType, nVersion); + Serialize(os, (const std::vector &)v, nType, nVersion); } template void Unserialize(Stream &is, CScript &v, int nType, int nVersion) { - Unserialize(is, (std::vector &)v, nType, nVersion); + Unserialize(is, (std::vector &)v, nType, nVersion); } // @@ -902,12 +901,12 @@ T *allocate(std::size_t n, const void *hint = 0) { T *p; p = std::allocator::allocate(n, hint); - if (p != NULL) mlock(p, sizeof(T) * n); + if (p != nullptr) mlock(p, sizeof(T) * n); return p; } void deallocate(T *p, std::size_t n) { - if (p != NULL) { + if (p != nullptr) { memset(p, 0, sizeof(T) * n); munlock(p, sizeof(T) * n); } @@ -975,8 +974,8 @@ Init(nTypeIn, nVersionIn); } - CDataStream(const std::vector &vchIn, - int nTypeIn = SER_NETWORK, int nVersionIn = PROTOCOL_VERSION) + CDataStream(const std::vector &vchIn, int nTypeIn = SER_NETWORK, + int nVersionIn = PROTOCOL_VERSION) : vch((char *)&vchIn.begin()[0], (char *)&vchIn.end()[0]) { Init(nTypeIn, nVersionIn); } @@ -1217,22 +1216,22 @@ // n=256000 109804 seconds #include int main(int argc, char *argv[]) { - vector vch(0xcc, 250); + vector vch(0xcc, 250); printf("CDataStream:\n"); for (int n = 1000; n <= 4500000; n *= 2) { CDataStream ss; - time_t nStart = time(NULL); + time_t nStart = time(nullptr); for (int i = 0; i < n; i++) ss.write((char *)&vch[0], vch.size()); - printf("n=%-10d %d seconds\n", n, time(NULL) - nStart); + printf("n=%-10d %d seconds\n", n, time(nullptr) - nStart); } printf("stringstream:\n"); for (int n = 1000; n <= 4500000; n *= 2) { stringstream ss; - time_t nStart = time(NULL); + time_t nStart = time(nullptr); for (int i = 0; i < n; i++) ss.write((char *)&vch[0], vch.size()); - printf("n=%-10d %d seconds\n", n, time(NULL) - nStart); + printf("n=%-10d %d seconds\n", n, time(nullptr) - nStart); } } #endif @@ -1256,7 +1255,7 @@ typedef FILE element_type; - CAutoFile(FILE *filenew = NULL, int nTypeIn = SER_DISK, + CAutoFile(FILE *filenew = nullptr, int nTypeIn = SER_DISK, int nVersionIn = PROTOCOL_VERSION) { file = filenew; nType = nTypeIn; @@ -1268,14 +1267,15 @@ ~CAutoFile() { fclose(); } void fclose() { - if (file != NULL && file != stdin && file != stdout && file != stderr) + if (file != nullptr && file != stdin && file != stdout && + file != stderr) ::fclose(file); - file = NULL; + file = nullptr; } FILE *release() { FILE *ret = file; - file = NULL; + file = nullptr; return ret; } operator FILE *() { return file; } @@ -1283,7 +1283,7 @@ FILE &operator*() { return *file; } FILE **operator&() { return &file; } FILE *operator=(FILE *pnew) { return file = pnew; } - bool operator!() { return (file == NULL); } + bool operator!() { return (file == nullptr); } // // Stream subset @@ -1314,7 +1314,7 @@ CAutoFile &read(char *pch, int nSize) { if (!file) throw std::ios_base::failure( - "CAutoFile::read : file handle is NULL"); + "CAutoFile::read : file handle is nullptr"); if (fread(pch, 1, nSize, file) != nSize) setstate(std::ios::failbit, feof(file) ? "CAutoFile::read : end of file" @@ -1325,7 +1325,7 @@ CAutoFile &write(const char *pch, int nSize) { if (!file) throw std::ios_base::failure( - "CAutoFile::write : file handle is NULL"); + "CAutoFile::write : file handle is nullptr"); if (fwrite(pch, 1, nSize, file) != nSize) setstate(std::ios::failbit, "CAutoFile::write : write failed"); return (*this); @@ -1340,7 +1340,7 @@ // Serialize to this stream if (!file) throw std::ios_base::failure( - "CAutoFile::operator<< : file handle is NULL"); + "CAutoFile::operator<< : file handle is nullptr"); ::Serialize(*this, obj, nType, nVersion); return (*this); } @@ -1349,7 +1349,7 @@ // Unserialize from this stream if (!file) throw std::ios_base::failure( - "CAutoFile::operator>> : file handle is NULL"); + "CAutoFile::operator>> : file handle is nullptr"); ::Unserialize(*this, obj, nType, nVersion); return (*this); } diff --git a/src/seeder/strlcpy.h b/src/seeder/strlcpy.h --- a/src/seeder/strlcpy.h +++ b/src/seeder/strlcpy.h @@ -13,11 +13,11 @@ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#ifndef BITCOIN_STRLCPY_H -#define BITCOIN_STRLCPY_H +#ifndef BITCOIN_SEEDER_STRLCPY_H +#define BITCOIN_SEEDER_STRLCPY_H -#include -#include +#include +#include /* * Copy src to string dst of size siz. At most siz-1 characters diff --git a/src/seeder/uint256.h b/src/seeder/uint256.h --- a/src/seeder/uint256.h +++ b/src/seeder/uint256.h @@ -2,12 +2,12 @@ // Copyright (c) 2011 The Bitcoin developers // Distributed under the MIT/X11 software license, see the accompanying // file license.txt or http://www.opensource.org/licenses/mit-license.php. -#ifndef BITCOIN_UINT256_H -#define BITCOIN_UINT256_H +#ifndef BITCOIN_SEEDER_UINT256_H +#define BITCOIN_SEEDER_UINT256_H #include "serialize.h" -#include +#include #include #include @@ -249,8 +249,7 @@ std::string GetHex() const { char psz[sizeof(pn) * 2 + 1]; for (int i = 0; i < sizeof(pn); i++) - sprintf(psz + i * 2, "%02x", - ((unsigned char *)pn)[sizeof(pn) - i - 1]); + sprintf(psz + i * 2, "%02x", ((uint8_t *)pn)[sizeof(pn) - i - 1]); return std::string(psz, psz + sizeof(pn) * 2); } @@ -275,15 +274,15 @@ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0, 0, 0, 0, 0, 0, 0, 0, 0}; const char *pbegin = psz; - while (phexdigit[(unsigned char)*psz] || *psz == '0') + while (phexdigit[(uint8_t)*psz] || *psz == '0') psz++; psz--; - unsigned char *p1 = (unsigned char *)pn; - unsigned char *pend = p1 + WIDTH * 4; + uint8_t *p1 = (uint8_t *)pn; + uint8_t *pend = p1 + WIDTH * 4; while (psz >= pbegin && p1 < pend) { - *p1 = phexdigit[(unsigned char)*psz--]; + *p1 = phexdigit[(uint8_t)*psz--]; if (psz >= pbegin) { - *p1 |= (phexdigit[(unsigned char)*psz--] << 4); + *p1 |= (phexdigit[(uint8_t)*psz--] << 4); p1++; } } @@ -293,9 +292,9 @@ std::string ToString() const { return (GetHex()); } - unsigned char *begin() { return (unsigned char *)&pn[0]; } + uint8_t *begin() { return (uint8_t *)&pn[0]; } - unsigned char *end() { return (unsigned char *)&pn[WIDTH]; } + uint8_t *end() { return (uint8_t *)&pn[WIDTH]; } unsigned int size() { return sizeof(pn); } @@ -371,7 +370,7 @@ explicit uint160(const std::string &str) { SetHex(str); } - explicit uint160(const std::vector &vch) { + explicit uint160(const std::vector &vch) { if (vch.size() == sizeof(pn)) memcpy(pn, &vch[0], sizeof(pn)); else @@ -558,7 +557,7 @@ explicit uint256(const std::string &str) { SetHex(str); } - explicit uint256(const std::vector &vch) { + explicit uint256(const std::vector &vch) { if (vch.size() == sizeof(pn)) memcpy(pn, &vch[0], sizeof(pn)); else diff --git a/src/seeder/util.h b/src/seeder/util.h --- a/src/seeder/util.h +++ b/src/seeder/util.h @@ -1,18 +1,18 @@ -#ifndef _UTIL_H_ -#define _UTIL_H_ 1 +#ifndef BITCOIN_SEEDER_UTIL_H +#define BITCOIN_SEEDER_UTIL_H +#include #include #include #include -#include #include "uint256.h" #define loop for (;;) #define BEGIN(a) ((char *)&(a)) #define END(a) ((char *)&((&(a))[1])) -#define UBEGIN(a) ((unsigned char *)&(a)) -#define UEND(a) ((unsigned char *)&((&(a))[1])) +#define UBEGIN(a) ((uint8_t *)&(a)) +#define UEND(a) ((uint8_t *)&((&(a))[1])) #define ARRAYLEN(array) (sizeof(array) / sizeof((array)[0])) #define WSAGetLastError() errno @@ -33,7 +33,7 @@ pthread_rwlock_t mutex; public: - explicit CCriticalSection() { pthread_rwlock_init(&mutex, NULL); } + explicit CCriticalSection() { pthread_rwlock_init(&mutex, nullptr); } ~CCriticalSection() { pthread_rwlock_destroy(&mutex); } void Enter(bool fShared = false) { if (fShared) { @@ -66,25 +66,25 @@ if (CCriticalBlock criticalblock = CCriticalBlock(cs, true)) template inline uint256 Hash(const T1 pbegin, const T1 pend) { - static unsigned char pblank[1]; + static uint8_t pblank[1]; uint256 hash1; - SHA256((pbegin == pend ? pblank : (unsigned char *)&pbegin[0]), - (pend - pbegin) * sizeof(pbegin[0]), (unsigned char *)&hash1); + SHA256((pbegin == pend ? pblank : (uint8_t *)&pbegin[0]), + (pend - pbegin) * sizeof(pbegin[0]), (uint8_t *)&hash1); uint256 hash2; - SHA256((unsigned char *)&hash1, sizeof(hash1), (unsigned char *)&hash2); + SHA256((uint8_t *)&hash1, sizeof(hash1), (uint8_t *)&hash2); return hash2; } -void static inline Sleep(int nMilliSec) { +static inline void Sleep(int nMilliSec) { struct timespec wa; wa.tv_sec = nMilliSec / 1000; wa.tv_nsec = (nMilliSec % 1000) * 1000000; - nanosleep(&wa, NULL); + nanosleep(&wa, nullptr); } std::string vstrprintf(const std::string &format, va_list ap); -std::string static inline strprintf(const std::string &format, ...) { +static inline std::string strprintf(const std::string &format, ...) { va_list arg_ptr; va_start(arg_ptr, format); std::string ret = vstrprintf(format, arg_ptr); @@ -92,17 +92,17 @@ return ret; } -bool static inline error(std::string err, ...) { +static inline bool error(std::string err, ...) { return false; } -bool static inline my_printf(std::string err, ...) { +static inline bool my_printf(std::string err, ...) { return true; } -std::vector DecodeBase32(const char *p, bool *pfInvalid = NULL); +std::vector DecodeBase32(const char *p, bool *pfInvalid = nullptr); std::string DecodeBase32(const std::string &str); -std::string EncodeBase32(const unsigned char *pch, size_t len); +std::string EncodeBase32(const uint8_t *pch, size_t len); std::string EncodeBase32(const std::string &str); #endif diff --git a/src/seeder/util.cpp b/src/seeder/util.cpp --- a/src/seeder/util.cpp +++ b/src/seeder/util.cpp @@ -1,9 +1,8 @@ #include "util.h" -#include -using namespace std; +#include -string vstrprintf(const std::string &format, va_list ap) { +std::string vstrprintf(const std::string &format, va_list ap) { char buffer[50000]; char *p = buffer; int limit = sizeof(buffer); @@ -17,21 +16,21 @@ if (p != buffer) delete[] p; limit *= 2; p = new char[limit]; - if (p == NULL) throw std::bad_alloc(); + if (p == nullptr) throw std::bad_alloc(); } - string str(p, p + ret); + std::string str(p, p + ret); if (p != buffer) delete[] p; return str; } -string EncodeBase32(const unsigned char *pch, size_t len) { +std::string EncodeBase32(const uint8_t *pch, size_t len) { static const char *pbase32 = "abcdefghijklmnopqrstuvwxyz234567"; - string strRet = ""; + std::string strRet = ""; strRet.reserve((len + 4) / 5 * 8); int mode = 0, left = 0; - const unsigned char *pchEnd = pch + len; + const uint8_t *pchEnd = pch + len; while (pch < pchEnd) { int enc = *(pch++); @@ -79,11 +78,11 @@ return strRet; } -string EncodeBase32(const string &str) { - return EncodeBase32((const unsigned char *)str.c_str(), str.size()); +std::string EncodeBase32(const std::string &str) { + return EncodeBase32((const uint8_t *)str.c_str(), str.size()); } -vector DecodeBase32(const char *p, bool *pfInvalid) { +std::vector DecodeBase32(const char *p, bool *pfInvalid) { static const int decode32_table[256] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, @@ -103,14 +102,14 @@ if (pfInvalid) *pfInvalid = false; - vector vchRet; + std::vector vchRet; vchRet.reserve((strlen(p)) * 5 / 8); int mode = 0; int left = 0; while (1) { - int dec = decode32_table[(unsigned char)*p]; + int dec = decode32_table[(uint8_t)*p]; if (dec == -1) break; p++; switch (mode) { @@ -173,25 +172,24 @@ case 2: // 8n+2 base32 characters processed: require '======' if (left || p[0] != '=' || p[1] != '=' || p[2] != '=' || p[3] != '=' || p[4] != '=' || p[5] != '=' || - decode32_table[(unsigned char)p[6]] != -1) + decode32_table[(uint8_t)p[6]] != -1) *pfInvalid = true; break; case 4: // 8n+4 base32 characters processed: require '====' if (left || p[0] != '=' || p[1] != '=' || p[2] != '=' || - p[3] != '=' || decode32_table[(unsigned char)p[4]] != -1) + p[3] != '=' || decode32_table[(uint8_t)p[4]] != -1) *pfInvalid = true; break; case 5: // 8n+5 base32 characters processed: require '===' if (left || p[0] != '=' || p[1] != '=' || p[2] != '=' || - decode32_table[(unsigned char)p[3]] != -1) + decode32_table[(uint8_t)p[3]] != -1) *pfInvalid = true; break; case 7: // 8n+7 base32 characters processed: require '=' - if (left || p[0] != '=' || - decode32_table[(unsigned char)p[1]] != -1) + if (left || p[0] != '=' || decode32_table[(uint8_t)p[1]] != -1) *pfInvalid = true; break; } @@ -199,7 +197,7 @@ return vchRet; } -string DecodeBase32(const string &str) { - vector vchRet = DecodeBase32(str.c_str()); - return string((const char *)&vchRet[0], vchRet.size()); +std::string DecodeBase32(const std::string &str) { + std::vector vchRet = DecodeBase32(str.c_str()); + return std::string((const char *)&vchRet[0], vchRet.size()); }