Merge remote-tracking branch 'bitcoin/master' into upstream-changes

This commit is contained in:
Jack Grigg 2021-06-24 00:18:33 +01:00
commit 0346150d92
13 changed files with 218 additions and 300 deletions

View File

@ -4,10 +4,5 @@ LDFLAGS = $(CXXFLAGS)
dnsseed: dns.o bitcoin.o netbase.o protocol.o db.o main.o util.o
g++ -pthread $(LDFLAGS) -o dnsseed dns.o bitcoin.o netbase.o protocol.o db.o main.o util.o -lcrypto
%.o: %.cpp bitcoin.h netbase.h protocol.h db.h serialize.h uint256.h util.h
g++ -pthread $(CXXFLAGS) -Wno-invalid-offsetof -c -o $@ $<
dns.o: dns.c
gcc -pthread -std=c99 $(CXXFLAGS) dns.c -c -o dns.o
%.o: %.cpp
%.o: %.cpp *.h
g++ -std=c++11 -pthread $(CXXFLAGS) -Wall -Wno-unused -Wno-sign-compare -Wno-reorder -Wno-comment -c -o $@ $<

4
README
View File

@ -60,3 +60,7 @@ $ iptables -t nat -A PREROUTING -p udp --dport 53 -j REDIRECT --to-port 5353
If properly configured, this will allow you to run dnsseed in userspace, using
the -p 5353 option.
Another solution is allowing a binary to bind to ports < 1024 with setcap (IPv6 access-safe)
$ setcap 'cap_net_bind_service=+ep' /path/to/dnsseed

View File

@ -81,7 +81,8 @@ class CNode {
BeginMessage("version");
int nBestHeight = GetRequireHeight();
string ver = "/zcash-seeder:0.01/";
vSend << PROTOCOL_VERSION << nLocalServices << nTime << you << me << nLocalNonce << ver << nBestHeight;
uint8_t fRelayTxs = 0;
vSend << PROTOCOL_VERSION << nLocalServices << nTime << you << me << nLocalNonce << ver << nBestHeight << fRelayTxs;
EndMessage();
}
@ -220,9 +221,11 @@ public:
int64 now;
while (now = time(NULL), ban == 0 && (doneAfter == 0 || doneAfter > now) && sock != INVALID_SOCKET) {
char pchBuf[0x10000];
fd_set set;
FD_ZERO(&set);
FD_SET(sock,&set);
fd_set read_set, except_set;
FD_ZERO(&read_set);
FD_ZERO(&except_set);
FD_SET(sock,&read_set);
FD_SET(sock,&except_set);
struct timeval wa;
if (doneAfter) {
wa.tv_sec = doneAfter - now;
@ -231,7 +234,7 @@ public:
wa.tv_sec = GetTimeout();
wa.tv_usec = 0;
}
int ret = select(sock+1, &set, NULL, &set, &wa);
int ret = select(sock+1, &read_set, NULL, &except_set, &wa);
if (ret != 1) {
if (!doneAfter) res = false;
break;
@ -274,9 +277,13 @@ public:
int GetStartingHeight() {
return nStartingHeight;
}
uint64_t GetServices() {
return you.nServices;
}
};
bool TestNode(const CService &cip, int &ban, int &clientV, std::string &clientSV, int &blocks, vector<CAddress>* vAddr) {
bool TestNode(const CService &cip, int &ban, int &clientV, std::string &clientSV, int &blocks, vector<CAddress>* vAddr, uint64_t& services) {
try {
CNode node(cip, vAddr);
bool ret = node.Run();
@ -288,6 +295,7 @@ bool TestNode(const CService &cip, int &ban, int &clientV, std::string &clientSV
clientV = node.GetClientVersion();
clientSV = node.GetClientSubVersion();
blocks = node.GetStartingHeight();
services = node.GetServices();
// printf("%s: %s!!!\n", cip.ToString().c_str(), ret ? "GOOD" : "BAD");
return ret;
} catch(std::ios_base::failure& e) {

View File

@ -3,6 +3,6 @@
#include "protocol.h"
bool TestNode(const CService &cip, int &ban, int &client, std::string &clientSV, int &blocks, std::vector<CAddress>* vAddr);
bool TestNode(const CService &cip, int &ban, int &client, std::string &clientSV, int &blocks, std::vector<CAddress>* vAddr, uint64_t& services);
#endif

11
db.cpp
View File

@ -69,7 +69,7 @@ int CAddrDb::Lookup_(const CService &ip) {
return -1;
}
void CAddrDb::Good_(const CService &addr, int clientV, std::string clientSV, int blocks) {
void CAddrDb::Good_(const CService &addr, int clientV, std::string clientSV, int blocks, uint64_t services) {
int id = Lookup_(addr);
if (id == -1) return;
unkId.erase(id);
@ -78,6 +78,7 @@ void CAddrDb::Good_(const CService &addr, int clientV, std::string clientSV, int
info.clientVersion = clientV;
info.clientSubVersion = clientSV;
info.blocks = blocks;
info.services = services;
info.Update(true);
if (info.IsGood() && goodId.count(id)==0) {
goodId.insert(id);
@ -140,12 +141,8 @@ void CAddrDb::Add_(const CAddress &addr, bool force) {
}
if (ipToId.count(ipp)) {
CAddrInfo &ai = idToInfo[ipToId[ipp]];
if (addr.nTime > ai.lastTry || ai.services != addr.nServices)
{
ai.lastTry = addr.nTime;
ai.services |= addr.nServices;
// printf("%s: updated\n", ToString(addr).c_str());
}
if (addr.nTime > ai.lastTry) ai.lastTry = addr.nTime;
// Do not update ai.nServices (data from VERSION from the peer itself is better than random ADDR rumours).
if (force) {
ai.ignoreTill = 0;
}

12
db.h
View File

@ -184,6 +184,7 @@ public:
struct CServiceResult {
CService service;
uint64_t services;
bool fGood;
int nBanTime;
int nHeight;
@ -216,7 +217,7 @@ protected:
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)
bool GetMany_(std::vector<CServiceResult> &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 Good_(const CService &ip, int clientV, std::string clientSV, int blocks, uint64_t services); // 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
@ -282,7 +283,7 @@ public:
} else {
CAddrDb *db = const_cast<CAddrDb*>(this);
db->nId = 0;
int n;
int n = 0;
READWRITE(n);
for (int i=0; i<n; i++) {
CAddrInfo info;
@ -314,9 +315,9 @@ public:
for (int i=0; i<vAddr.size(); i++)
Add_(vAddr[i], fForce);
}
void Good(const CService &addr, int clientVersion, std::string clientSubVersion, int blocks) {
void Good(const CService &addr, int clientVersion, std::string clientSubVersion, int blocks, uint64_t services) {
CRITICAL_BLOCK(cs)
Good_(addr, clientVersion, clientSubVersion, blocks);
Good_(addr, clientVersion, clientSubVersion, blocks, services);
}
void Skipped(const CService &addr) {
CRITICAL_BLOCK(cs)
@ -329,6 +330,7 @@ public:
bool Get(CServiceResult &ip, int& wait) {
CRITICAL_BLOCK(cs)
return Get_(ip, wait);
return false;
}
void GetMany(std::vector<CServiceResult> &ips, int max, int& wait) {
CRITICAL_BLOCK(cs) {
@ -345,7 +347,7 @@ public:
CRITICAL_BLOCK(cs) {
for (int i=0; i<ips.size(); i++) {
if (ips[i].fGood) {
Good_(ips[i].service, ips[i].nClientV, ips[i].strClientV, ips[i].nHeight);
Good_(ips[i].service, ips[i].nClientV, ips[i].strClientV, ips[i].nHeight, ips[i].services);
} else {
Bad_(ips[i].service, ips[i].nBanTime);
}

View File

@ -16,17 +16,11 @@
#define BUFLEN 512
#if defined IP_RECVDSTADDR
#if defined(IP_RECVDSTADDR)
# define DSTADDR_SOCKOPT IP_RECVDSTADDR
# define DSTADDR_DATASIZE (CMSG_SPACE(sizeof(struct in6_addr)))
# define dstaddr(x) (CMSG_DATA(x))
#elif defined IPV6_PKTINFO
struct in6_pktinfo
{
struct in6_addr ipi6_addr; /* src/dst IPv6 address */
unsigned int ipi6_ifindex; /* send/recv interface index */
};
#elif defined(IPV6_PKTINFO)
# define DSTADDR_SOCKOPT IPV6_PKTINFO
# define DSTADDR_DATASIZE (CMSG_SPACE(sizeof(struct in6_pktinfo)))
# define dstaddr(x) (&(((struct in6_pktinfo *)(CMSG_DATA(x)))->ipi6_addr))
@ -109,7 +103,7 @@ int static parse_name(const unsigned char **inpos, const unsigned char *inend, c
// -3: two subsequent dots
int static write_name(unsigned char** outpos, const unsigned char *outend, const char *name, int offset) {
while (*name != 0) {
char *dot = strchr(name, '.');
const char *dot = strchr(name, '.');
const char *fin = dot;
if (!dot) fin = name + strlen(name);
if (fin - name > 63) return -1;
@ -138,16 +132,21 @@ int static write_record(unsigned char** outpos, const unsigned char *outend, con
int error = 0;
// name
int ret = write_name(outpos, outend, name, offset);
if (ret) { error = ret; goto error; }
if (outend - *outpos < 8) { error = -4; goto error; }
// type
*((*outpos)++) = typ >> 8; *((*outpos)++) = typ & 0xFF;
// class
*((*outpos)++) = cls >> 8; *((*outpos)++) = cls & 0xFF;
// ttl
*((*outpos)++) = (ttl >> 24) & 0xFF; *((*outpos)++) = (ttl >> 16) & 0xFF; *((*outpos)++) = (ttl >> 8) & 0xFF; *((*outpos)++) = ttl & 0xFF;
return 0;
error:
if (ret) {
error = ret;
} else {
if (outend - *outpos < 8) {
error = -4;
} else {
// type
*((*outpos)++) = typ >> 8; *((*outpos)++) = typ & 0xFF;
// class
*((*outpos)++) = cls >> 8; *((*outpos)++) = cls & 0xFF;
// ttl
*((*outpos)++) = (ttl >> 24) & 0xFF; *((*outpos)++) = (ttl >> 16) & 0xFF; *((*outpos)++) = (ttl >> 8) & 0xFF; *((*outpos)++) = ttl & 0xFF;
return 0;
}
}
*outpos = oldpos;
return error;
}
@ -160,14 +159,16 @@ int static write_record_a(unsigned char** outpos, const unsigned char *outend, c
int error = 0;
int ret = write_record(outpos, outend, name, offset, TYPE_A, cls, ttl);
if (ret) return ret;
if (outend - *outpos < 6) { error = -5; goto error; }
// rdlength
*((*outpos)++) = 0; *((*outpos)++) = 4;
// rdata
for (int i=0; i<4; i++)
*((*outpos)++) = ip->data.v4[i];
return 0;
error:
if (outend - *outpos < 6) {
error = -5;
} else {
// rdlength
*((*outpos)++) = 0; *((*outpos)++) = 4;
// rdata
for (int i=0; i<4; i++)
*((*outpos)++) = ip->data.v4[i];
return 0;
}
*outpos = oldpos;
return error;
}
@ -179,63 +180,92 @@ int static write_record_aaaa(unsigned char** outpos, const unsigned char *outend
int error = 0;
int ret = write_record(outpos, outend, name, offset, TYPE_AAAA, cls, ttl);
if (ret) return ret;
if (outend - *outpos < 6) { error = -5; goto error; }
// rdlength
*((*outpos)++) = 0; *((*outpos)++) = 16;
// rdata
for (int i=0; i<16; i++)
*((*outpos)++) = ip->data.v6[i];
return 0;
error:
if (outend - *outpos < 18) {
error = -5;
} else {
// rdlength
*((*outpos)++) = 0; *((*outpos)++) = 16;
// rdata
for (int i=0; i<16; i++)
*((*outpos)++) = ip->data.v6[i];
return 0;
}
*outpos = oldpos;
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) {
int static write_record_ns(unsigned char** outpos, const unsigned char *outend, const char *name, int offset, dns_class cls, int ttl, const char *ns) {
unsigned char *oldpos = *outpos;
int ret = write_record(outpos, outend, name, offset, TYPE_NS, cls, ttl);
if (ret) return ret;
int error = 0;
if (outend - *outpos < 2) { error = -5; goto error; }
(*outpos) += 2;
unsigned char *curpos = *outpos;
ret = write_name(outpos, outend, ns, -1);
if (ret) { error = ret; goto error; }
curpos[-2] = (*outpos - curpos) >> 8;
curpos[-1] = (*outpos - curpos) & 0xFF;
return 0;
error:
if (outend - *outpos < 2) {
error = -5;
} else {
(*outpos) += 2;
unsigned char *curpos = *outpos;
ret = write_name(outpos, outend, ns, -1);
if (ret) {
error = ret;
} else {
curpos[-2] = (*outpos - curpos) >> 8;
curpos[-1] = (*outpos - curpos) & 0xFF;
return 0;
}
}
*outpos = oldpos;
return error;
}
int static write_record_soa(unsigned char** outpos, const unsigned char *outend, char *name, int offset, dns_class cls, int ttl, const char* mname, const char *rname,
int static write_record_soa(unsigned char** outpos, const unsigned char *outend, const 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;
int ret = write_record(outpos, outend, name, offset, TYPE_SOA, cls, ttl);
if (ret) return ret;
int error = 0;
if (outend - *outpos < 2) { error = -5; goto error; }
(*outpos) += 2;
unsigned char *curpos = *outpos;
ret = write_name(outpos, outend, mname, -1);
if (ret) { error = ret; goto error; }
ret = write_name(outpos, outend, rname, -1);
if (ret) { error = ret; goto error; }
if (outend - *outpos < 20) { error = -5; goto error; }
*((*outpos)++) = (serial >> 24) & 0xFF; *((*outpos)++) = (serial >> 16) & 0xFF; *((*outpos)++) = (serial >> 8) & 0xFF; *((*outpos)++) = serial & 0xFF;
*((*outpos)++) = (refresh >> 24) & 0xFF; *((*outpos)++) = (refresh >> 16) & 0xFF; *((*outpos)++) = (refresh >> 8) & 0xFF; *((*outpos)++) = refresh & 0xFF;
*((*outpos)++) = (retry >> 24) & 0xFF; *((*outpos)++) = (retry >> 16) & 0xFF; *((*outpos)++) = (retry >> 8) & 0xFF; *((*outpos)++) = retry & 0xFF;
*((*outpos)++) = (expire >> 24) & 0xFF; *((*outpos)++) = (expire >> 16) & 0xFF; *((*outpos)++) = (expire >> 8) & 0xFF; *((*outpos)++) = expire & 0xFF;
*((*outpos)++) = (minimum >> 24) & 0xFF; *((*outpos)++) = (minimum >> 16) & 0xFF; *((*outpos)++) = (minimum >> 8) & 0xFF; *((*outpos)++) = minimum & 0xFF;
curpos[-2] = (*outpos - curpos) >> 8;
curpos[-1] = (*outpos - curpos) & 0xFF;
return 0;
error:
if (outend - *outpos < 2) {
error = -5;
} else {
(*outpos) += 2;
unsigned char *curpos = *outpos;
ret = write_name(outpos, outend, mname, -1);
if (ret) {
error = ret;
} else {
ret = write_name(outpos, outend, rname, -1);
if (ret) {
error = ret;
} else {
if (outend - *outpos < 20) {
error = -5;
} else {
*((*outpos)++) = (serial >> 24) & 0xFF; *((*outpos)++) = (serial >> 16) & 0xFF; *((*outpos)++) = (serial >> 8) & 0xFF; *((*outpos)++) = serial & 0xFF;
*((*outpos)++) = (refresh >> 24) & 0xFF; *((*outpos)++) = (refresh >> 16) & 0xFF; *((*outpos)++) = (refresh >> 8) & 0xFF; *((*outpos)++) = refresh & 0xFF;
*((*outpos)++) = (retry >> 24) & 0xFF; *((*outpos)++) = (retry >> 16) & 0xFF; *((*outpos)++) = (retry >> 8) & 0xFF; *((*outpos)++) = retry & 0xFF;
*((*outpos)++) = (expire >> 24) & 0xFF; *((*outpos)++) = (expire >> 16) & 0xFF; *((*outpos)++) = (expire >> 8) & 0xFF; *((*outpos)++) = expire & 0xFF;
*((*outpos)++) = (minimum >> 24) & 0xFF; *((*outpos)++) = (minimum >> 16) & 0xFF; *((*outpos)++) = (minimum >> 8) & 0xFF; *((*outpos)++) = minimum & 0xFF;
curpos[-2] = (*outpos - curpos) >> 8;
curpos[-1] = (*outpos - curpos) & 0xFF;
return 0;
}
}
}
}
*outpos = oldpos;
return error;
}
static ssize_t set_error(unsigned char* outbuf, int error) {
// set error
outbuf[3] |= error & 0xF;
// set counts
outbuf[4] = 0; outbuf[5] = 0;
outbuf[6] = 0; outbuf[7] = 0;
outbuf[8] = 0; outbuf[9] = 0;
outbuf[10] = 0; outbuf[11] = 0;
return 12;
}
ssize_t static dnshandle(dns_opt_t *opt, const unsigned char *inbuf, size_t insize, unsigned char* outbuf) {
int error = 0;
if (insize < 12) // DNS header
@ -249,27 +279,27 @@ ssize_t static dnshandle(dns_opt_t *opt, const unsigned char *inbuf, size_t insi
// clear error
outbuf[3] &= ~15;
// check qr
if (inbuf[2] & 128) { /* printf("Got response?\n"); */ error = 1; goto error; }
if (inbuf[2] & 128) return set_error(outbuf, 1); /* printf("Got response?\n"); */
// check opcode
if (((inbuf[2] & 120) >> 3) != 0) { /* printf("Opcode nonzero?\n"); */ error = 4; goto error; }
if (((inbuf[2] & 120) >> 3) != 0) return set_error(outbuf, 1); /* printf("Opcode nonzero?\n"); */
// unset TC
outbuf[2] &= ~2;
// unset RA
outbuf[3] &= ~128;
// check questions
int nquestion = (inbuf[4] << 8) + inbuf[5];
if (nquestion == 0) { /* printf("No questions?\n"); */ error = 0; goto error; }
if (nquestion > 1) { /* printf("Multiple questions %i?\n", nquestion); */ error = 4; goto error; }
if (nquestion == 0) return set_error(outbuf, 0); /* printf("No questions?\n"); */
if (nquestion > 1) return set_error(outbuf, 4); /* printf("Multiple questions %i?\n", nquestion); */
const unsigned char *inpos = inbuf + 12;
const unsigned char *inend = inbuf + insize;
char name[256];
int offset = inpos - inbuf;
int ret = parse_name(&inpos, inend, inbuf, name, 256);
if (ret == -1) { error = 1; goto error; }
if (ret == -2) { error = 5; goto error; }
if (ret == -1) return set_error(outbuf, 1);
if (ret == -2) return set_error(outbuf, 5);
int namel = strlen(name), hostl = strlen(opt->host);
if (strcasecmp(name, opt->host) && (namel<hostl+2 || name[namel-hostl-1]!='.' || strcasecmp(name+namel-hostl,opt->host))) { error = 5; goto error; }
if (inend - inpos < 4) { error = 1; goto error; }
if (strcasecmp(name, opt->host) && (namel<hostl+2 || name[namel-hostl-1]!='.' || strcasecmp(name+namel-hostl,opt->host))) return set_error(outbuf, 5);
if (inend - inpos < 4) return set_error(outbuf, 1);
// copy question to output
memcpy(outbuf+12, inbuf+12, inpos+4 - (inbuf+12));
// set counts
@ -366,15 +396,6 @@ ssize_t static dnshandle(dns_opt_t *opt, const unsigned char *inbuf, size_t insi
outbuf[2] |= 4;
return outpos - outbuf;
error:
// set error
outbuf[3] |= error & 0xF;
// set counts
outbuf[4] = 0; outbuf[5] = 0;
outbuf[6] = 0; outbuf[7] = 0;
outbuf[8] = 0; outbuf[9] = 0;
outbuf[10] = 0; outbuf[11] = 0;
return 12;
}
static int listenSocket = -1;
@ -404,7 +425,7 @@ int dnsserver(dns_opt_t *opt) {
memset((char *) &si_me, 0, sizeof(si_me));
si_me.sin6_family = AF_INET6;
si_me.sin6_port = htons(opt->port);
si_me.sin6_addr = in6addr_any;
inet_pton(AF_INET6, opt->addr, &si_me.sin6_addr);
if (bind(listenSocket, (struct sockaddr*)&si_me, sizeof(si_me))==-1)
return -2;
}

11
dns.h
View File

@ -3,26 +3,27 @@
#include <stdint.h>
typedef struct {
struct addr_t {
int v;
union {
unsigned char v4[4];
unsigned char v6[16];
} data;
} addr_t;
};
typedef struct {
struct dns_opt_t {
int port;
int datattl;
int nsttl;
const char *host;
const char *addr;
const char *ns;
const char *mbox;
int (*cb)(void *opt, char *requested_hostname, addr_t *addr, int max, int ipv4, int ipv6);
// stats
uint64_t nRequests;
} dns_opt_t;
};
extern int dnsserver(dns_opt_t *opt);
int dnsserver(dns_opt_t *opt);
#endif

108
main.cpp
View File

@ -7,6 +7,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <atomic>
#include "bitcoin.h"
#include "db.h"
@ -27,11 +28,12 @@ public:
const char *ns;
const char *host;
const char *tor;
const char *ip_addr;
const char *ipv4_proxy;
const char *ipv6_proxy;
std::set<uint64_t> 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) {}
CDnsSeedOpts() : nThreads(96), nDnsThreads(4), ip_addr("::"), nPort(53), mbox(NULL), ns(NULL), host(NULL), tor(NULL), fUseTestNet(false), fWipeBan(false), fWipeIgnore(false), ipv4_proxy(NULL), ipv6_proxy(NULL) {}
void ParseCommandLine(int argc, char **argv) {
static const char *help = "Bitcoin-seeder\n"
@ -43,6 +45,7 @@ public:
"-m <mbox> E-Mail address reported in SOA records\n"
"-t <threads> Number of crawlers to run in parallel (default 96)\n"
"-d <threads> Number of DNS server threads (default 4)\n"
"-a <address> Address to listen on (default ::)\n"
"-p <port> UDP port to listen on (default 53)\n"
"-o <ip:port> Tor proxy IP/Port\n"
"-i <ip:port> IPV4 SOCKS5 proxy IP/Port\n"
@ -62,6 +65,7 @@ public:
{"mbox", required_argument, 0, 'm'},
{"threads", required_argument, 0, 't'},
{"dnsthreads", required_argument, 0, 'd'},
{"address", required_argument, 0, 'a'},
{"port", required_argument, 0, 'p'},
{"onion", required_argument, 0, 'o'},
{"proxyipv4", required_argument, 0, 'i'},
@ -74,7 +78,7 @@ public:
{0, 0, 0, 0}
};
int option_index = 0;
int c = getopt_long(argc, argv, "h:n:m:t:p:d:o:i:k:w:", long_options, &option_index);
int c = getopt_long(argc, argv, "h:n:m:t:a:p:d:o:i:k:w:", long_options, &option_index);
if (c == -1) break;
switch (c) {
case 'h': {
@ -104,6 +108,18 @@ public:
break;
}
case 'a': {
if (strchr(optarg, ':')==NULL) {
char* ip4_addr = (char*) malloc(strlen(optarg)+8);
strcpy(ip4_addr, "::FFFF:");
strcat(ip4_addr, optarg);
ip_addr = ip4_addr;
} else {
ip_addr = optarg;
}
break;
}
case 'p': {
int p = strtol(optarg, NULL, 10);
if (p > 0 && p < 65536) nPort = p;
@ -146,19 +162,23 @@ public:
}
}
if (filter_whitelist.empty()) {
filter_whitelist.insert(1);
filter_whitelist.insert(5);
filter_whitelist.insert(9);
filter_whitelist.insert(13);
filter_whitelist.insert(NODE_NETWORK); // x1
filter_whitelist.insert(NODE_NETWORK | NODE_BLOOM); // x5
filter_whitelist.insert(NODE_NETWORK | NODE_WITNESS); // x9
filter_whitelist.insert(NODE_NETWORK | NODE_WITNESS | NODE_COMPACT_FILTERS); // x49
filter_whitelist.insert(NODE_NETWORK | NODE_WITNESS | NODE_BLOOM); // xd
filter_whitelist.insert(NODE_NETWORK_LIMITED); // x400
filter_whitelist.insert(NODE_NETWORK_LIMITED | NODE_BLOOM); // x404
filter_whitelist.insert(NODE_NETWORK_LIMITED | NODE_WITNESS); // x408
filter_whitelist.insert(NODE_NETWORK_LIMITED | NODE_WITNESS | NODE_COMPACT_FILTERS); // x448
filter_whitelist.insert(NODE_NETWORK_LIMITED | NODE_WITNESS | NODE_BLOOM); // x40c
}
if (host != NULL && ns == NULL) showHelp = true;
if (showHelp) fprintf(stderr, help, argv[0]);
}
};
extern "C" {
#include "dns.h"
}
CAddrDb db;
@ -182,25 +202,32 @@ extern "C" void* ThreadCrawler(void* data) {
res.nClientV = 0;
res.nHeight = 0;
res.strClientV = "";
res.services = 0;
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 : NULL, res.services);
}
db.ResultMany(ips);
db.Add(addr);
} while(1);
return nullptr;
}
extern "C" int GetIPList(void *thread, char *requestedHostname, addr_t *addr, int max, int ipv4, int ipv6);
class CDnsThread {
public:
struct FlagSpecificData {
int nIPv4, nIPv6;
std::vector<addr_t> cache;
time_t cacheTime;
unsigned int cacheHits;
FlagSpecificData() : nIPv4(0), nIPv6(0), cacheTime(0), cacheHits(0) {}
};
dns_opt_t dns_opt; // must be first
const int id;
std::map<uint64_t, vector<addr_t> > cache;
int nIPv4, nIPv6;
std::map<uint64_t, time_t> cacheTime;
unsigned int cacheHits;
uint64_t dbQueries;
std::map<uint64_t, FlagSpecificData> perflag;
std::atomic<uint64_t> dbQueries;
std::set<uint64_t> filterWhitelist;
void cacheHit(uint64_t requestedFlags, bool force = false) {
@ -210,15 +237,16 @@ public:
nets[NET_IPV6] = true;
}
time_t now = time(NULL);
cacheHits++;
if (force || cacheHits > (cache[requestedFlags].size()*cache[requestedFlags].size()/400) || (cacheHits*cacheHits > cache[requestedFlags].size() / 20 && (now - cacheTime[requestedFlags] > 5))) {
FlagSpecificData& thisflag = perflag[requestedFlags];
thisflag.cacheHits++;
if (force || thisflag.cacheHits * 400 > (thisflag.cache.size()*thisflag.cache.size()) || (thisflag.cacheHits*thisflag.cacheHits * 20 > thisflag.cache.size() && (now - thisflag.cacheTime > 5))) {
set<CNetAddr> ips;
db.GetIPs(ips, requestedFlags, 1000, nets);
dbQueries++;
cache[requestedFlags].clear();
nIPv4 = 0;
nIPv6 = 0;
cache[requestedFlags].reserve(ips.size());
thisflag.cache.clear();
thisflag.nIPv4 = 0;
thisflag.nIPv6 = 0;
thisflag.cache.reserve(ips.size());
for (set<CNetAddr>::iterator it = ips.begin(); it != ips.end(); it++) {
struct in_addr addr;
struct in6_addr addr6;
@ -226,18 +254,18 @@ public:
addr_t a;
a.v = 4;
memcpy(&a.data.v4, &addr, 4);
cache[requestedFlags].push_back(a);
nIPv4++;
thisflag.cache.push_back(a);
thisflag.nIPv4++;
} else if ((*it).GetIn6Addr(&addr6)) {
addr_t a;
a.v = 6;
memcpy(&a.data.v6, &addr6, 16);
cache[requestedFlags].push_back(a);
nIPv6++;
thisflag.cache.push_back(a);
thisflag.nIPv6++;
}
}
cacheHits = 0;
cacheTime[requestedFlags] = now;
thisflag.cacheHits = 0;
thisflag.cacheTime = now;
}
}
@ -248,14 +276,11 @@ public:
dns_opt.datattl = 3600;
dns_opt.nsttl = 40000;
dns_opt.cb = GetIPList;
dns_opt.addr = opts->ip_addr;
dns_opt.port = opts->nPort;
dns_opt.nRequests = 0;
cache.clear();
cacheTime.clear();
cacheHits = 0;
dbQueries = 0;
nIPv4 = 0;
nIPv6 = 0;
perflag.clear();
filterWhitelist = opts->filter_whitelist;
}
@ -280,8 +305,9 @@ extern "C" int GetIPList(void *data, char *requestedHostname, addr_t* addr, int
else if (strcasecmp(requestedHostname, thread->dns_opt.host))
return 0;
thread->cacheHit(requestedFlags);
unsigned int size = thread->cache[requestedFlags].size();
unsigned int maxmax = (ipv4 ? thread->nIPv4 : 0) + (ipv6 ? thread->nIPv6 : 0);
auto& thisflag = thread->perflag[requestedFlags];
unsigned int size = thisflag.cache.size();
unsigned int maxmax = (ipv4 ? thisflag.nIPv4 : 0) + (ipv6 ? thisflag.nIPv6 : 0);
if (max > size)
max = size;
if (max > maxmax)
@ -290,16 +316,16 @@ extern "C" int GetIPList(void *data, char *requestedHostname, addr_t* addr, int
while (i<max) {
int j = i + (rand() % (size - i));
do {
bool ok = (ipv4 && thread->cache[requestedFlags][j].v == 4) ||
(ipv6 && thread->cache[requestedFlags][j].v == 6);
bool ok = (ipv4 && thisflag.cache[j].v == 4) ||
(ipv6 && thisflag.cache[j].v == 6);
if (ok) break;
j++;
if (j==size)
j=i;
} while(1);
addr[i] = thread->cache[requestedFlags][j];
thread->cache[requestedFlags][j] = thread->cache[requestedFlags][i];
thread->cache[requestedFlags][i] = addr[i];
addr[i] = thisflag.cache[j];
thisflag.cache[j] = thisflag.cache[i];
thisflag.cache[i] = addr[i];
i++;
}
return max;
@ -310,6 +336,7 @@ vector<CDnsThread*> dnsThread;
extern "C" void* ThreadDNS(void* arg) {
CDnsThread *thread = (CDnsThread*)arg;
thread->run();
return nullptr;
}
int StatCompare(const CAddrReport& a, const CAddrReport& b) {
@ -346,7 +373,7 @@ extern "C" void* ThreadDumper(void*) {
double stat[5]={0,0,0,0,0};
for (vector<CAddrReport>::const_iterator it = v.begin(); it < v.end(); it++) {
CAddrReport rep = *it;
fprintf(d, "%-47s %4d %11"PRId64" %6.2f%% %6.2f%% %6.2f%% %6.2f%% %6.2f%% %6i %08"PRIx64" %5i \"%s\"\n", rep.ip.ToString().c_str(), (int)rep.fGood, rep.lastSuccess, 100.0*rep.uptime[0], 100.0*rep.uptime[1], 100.0*rep.uptime[2], 100.0*rep.uptime[3], 100.0*rep.uptime[4], rep.blocks, rep.services, rep.clientVersion, rep.clientSubVersion.c_str());
fprintf(d, "%-47s %4d %11" PRId64 " %6.2f%% %6.2f%% %6.2f%% %6.2f%% %6.2f%% %6i %08" PRIx64 " %5i \"%s\"\n", rep.ip.ToString().c_str(), (int)rep.fGood, rep.lastSuccess, 100.0*rep.uptime[0], 100.0*rep.uptime[1], 100.0*rep.uptime[2], 100.0*rep.uptime[3], 100.0*rep.uptime[4], rep.blocks, rep.services, rep.clientVersion, rep.clientSubVersion.c_str());
stat[0] += rep.uptime[0];
stat[1] += rep.uptime[1];
stat[2] += rep.uptime[2];
@ -359,6 +386,7 @@ extern "C" void* ThreadDumper(void*) {
fclose(ff);
}
} while(1);
return nullptr;
}
extern "C" void* ThreadStats(void*) {
@ -387,6 +415,7 @@ extern "C" void* ThreadStats(void*) {
printf("%s %i/%i available (%i tried in %is, %i new, %i active), %i banned; %llu DNS requests, %llu db queries", c, stats.nGood, stats.nAvail, stats.nTracked, stats.nAge, stats.nNew, stats.nAvail - stats.nTracked - stats.nNew, stats.nBanned, (unsigned long long)requests, (unsigned long long)queries);
Sleep(1000);
} while(1);
return nullptr;
}
static const string mainnet_seeds[] = {"mainnet.z.cash", "dnsseed.str4d.xyz", "dnsseed.znodes.org", ""};
@ -407,6 +436,7 @@ extern "C" void* ThreadSeeder(void*) {
}
Sleep(1800000);
} while(1);
return nullptr;
}
int main(int argc, char **argv) {

View File

@ -36,7 +36,9 @@ CMessageHeader::CMessageHeader()
CMessageHeader::CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn)
{
memcpy(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart));
strncpy(pchCommand, pszCommand, COMMAND_SIZE);
size_t command_len = strnlen(pszCommand, COMMAND_SIZE);
memcpy(pchCommand, pszCommand, command_len);
memset(pchCommand + command_len, 0, COMMAND_SIZE - command_len);
nMessageSize = nMessageSizeIn;
nChecksum = 0;
}

View File

@ -60,6 +60,10 @@ class CMessageHeader
enum
{
NODE_NETWORK = (1 << 0),
NODE_BLOOM = (1 << 2),
NODE_WITNESS = (1 << 3),
NODE_COMPACT_FILTERS = (1 << 6),
NODE_NETWORK_LIMITED = (1 << 10),
};
class CAddress : public CService

148
uint256.h
View File

@ -22,11 +22,6 @@ typedef unsigned long long uint64;
#define for if (false) ; else for
#endif
inline int Testuint256AdHoc(std::vector<std::string> vArg);
// We have to keep a separate base class without constructors
// so the compiler will let us use it in a union
template<unsigned int BITS>
@ -322,7 +317,7 @@ public:
// hex string to uint
static char phexdigit[256] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0, 0,0xa,0xb,0xc,0xd,0xe,0xf,0,0,0,0,0,0,0,0,0, 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[*psz] || *psz == '0')
while (phexdigit[(unsigned char)*psz] || *psz == '0')
psz++;
psz--;
unsigned char* p1 = (unsigned char*)pn;
@ -622,145 +617,4 @@ inline const uint256 operator|(const uint256& a, const uint256& b) { return
inline const uint256 operator+(const uint256& a, const uint256& b) { return (base_uint256)a + (base_uint256)b; }
inline const uint256 operator-(const uint256& a, const uint256& b) { return (base_uint256)a - (base_uint256)b; }
inline int Testuint256AdHoc(std::vector<std::string> vArg)
{
uint256 g(0);
printf("%s\n", g.ToString().c_str());
g--; printf("g--\n");
printf("%s\n", g.ToString().c_str());
g--; printf("g--\n");
printf("%s\n", g.ToString().c_str());
g++; printf("g++\n");
printf("%s\n", g.ToString().c_str());
g++; printf("g++\n");
printf("%s\n", g.ToString().c_str());
g++; printf("g++\n");
printf("%s\n", g.ToString().c_str());
g++; printf("g++\n");
printf("%s\n", g.ToString().c_str());
uint256 a(7);
printf("a=7\n");
printf("%s\n", a.ToString().c_str());
uint256 b;
printf("b undefined\n");
printf("%s\n", b.ToString().c_str());
int c = 3;
a = c;
a.pn[3] = 15;
printf("%s\n", a.ToString().c_str());
uint256 k(c);
a = 5;
a.pn[3] = 15;
printf("%s\n", a.ToString().c_str());
b = 1;
b <<= 52;
a |= b;
a ^= 0x500;
printf("a %s\n", a.ToString().c_str());
a = a | b | (uint256)0x1000;
printf("a %s\n", a.ToString().c_str());
printf("b %s\n", b.ToString().c_str());
a = 0xfffffffe;
a.pn[4] = 9;
printf("%s\n", a.ToString().c_str());
a++;
printf("%s\n", a.ToString().c_str());
a++;
printf("%s\n", a.ToString().c_str());
a++;
printf("%s\n", a.ToString().c_str());
a++;
printf("%s\n", a.ToString().c_str());
a--;
printf("%s\n", a.ToString().c_str());
a--;
printf("%s\n", a.ToString().c_str());
a--;
printf("%s\n", a.ToString().c_str());
uint256 d = a--;
printf("%s\n", d.ToString().c_str());
printf("%s\n", a.ToString().c_str());
a--;
printf("%s\n", a.ToString().c_str());
a--;
printf("%s\n", a.ToString().c_str());
d = a;
printf("%s\n", d.ToString().c_str());
for (int i = uint256::WIDTH-1; i >= 0; i--) printf("%08x", d.pn[i]); printf("\n");
uint256 neg = d;
neg = ~neg;
printf("%s\n", neg.ToString().c_str());
uint256 e = uint256("0xABCDEF123abcdef12345678909832180000011111111");
printf("\n");
printf("%s\n", e.ToString().c_str());
printf("\n");
uint256 x1 = uint256("0xABCDEF123abcdef12345678909832180000011111111");
uint256 x2;
printf("%s\n", x1.ToString().c_str());
for (int i = 0; i < 270; i += 4)
{
x2 = x1 << i;
printf("%s\n", x2.ToString().c_str());
}
printf("\n");
printf("%s\n", x1.ToString().c_str());
for (int i = 0; i < 270; i += 4)
{
x2 = x1;
x2 >>= i;
printf("%s\n", x2.ToString().c_str());
}
for (int i = 0; i < 100; i++)
{
uint256 k = (~uint256(0) >> i);
printf("%s\n", k.ToString().c_str());
}
for (int i = 0; i < 100; i++)
{
uint256 k = (~uint256(0) << i);
printf("%s\n", k.ToString().c_str());
}
return (0);
}
#endif

2
util.h
View File

@ -82,7 +82,7 @@ void static inline Sleep(int nMilliSec) {
std::string vstrprintf(const std::string &format, va_list ap);
std::string static inline strprintf(const std::string &format, ...) {
std::string static inline strprintf(const std::string format, ...) {
va_list arg_ptr;
va_start(arg_ptr, format);
std::string ret = vstrprintf(format, arg_ptr);