twister dns seeder
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
3.0 KiB

13 years ago
#include <pthread.h>
#include "bitcoin.h"
#include "db.h"
#define NTHREADS 32
13 years ago
13 years ago
using namespace std;
extern "C" {
#include "dns.h"
13 years ago
}
CAddrDb db;
extern "C" void* ThreadCrawler(void* data) {
do {
CIPPort ip;
int wait = 5;
if (!db.Get(ip, wait)) {
13 years ago
wait *= 1000;
wait += rand() % (500 * NTHREADS);
Sleep(wait);
13 years ago
continue;
}
int ban = 0;
vector<CAddress> addr;
13 years ago
int clientV = 0;
bool ret = TestNode(ip,ban,clientV,addr);
13 years ago
db.Add(addr);
if (ret) {
13 years ago
db.Good(ip, clientV);
13 years ago
} else {
db.Bad(ip, ban);
}
} while(1);
}
extern "C" int GetIPList(struct in_addr *addr, int max, int ipv4only) {
set<CIP> ips;
db.GetIPs(ips, max, ipv4only);
int n = 0;
for (set<CIP>::iterator it = ips.begin(); it != ips.end(); it++) {
if ((*it).GetInAddr(&addr[n]))
n++;
}
// permute list
for (int i=0; i<n; i++) {
int k = i + (rand() % (n-i));
if (i != k) {
struct in_addr sw = addr[i];
addr[i] = addr[k];
addr[k] = sw;
}
}
13 years ago
return n;
}
static dns_opt_t dns_opt;
13 years ago
extern "C" void* ThreadDNS(void*) {
dns_opt.host = "seed.bitcoin.sipa.be";
dns_opt.ns = "vps.sipa.be";
dns_opt.mbox = "sipa.ulyssis.org";
dns_opt.datattl = 60;
dns_opt.nsttl = 40000;
dns_opt.cb = GetIPList;
dns_opt.port = 53;
dns_opt.nRequests = 0;
dnsserver(&dns_opt);
13 years ago
}
extern "C" void* ThreadDumper(void*) {
do {
Sleep(100000);
{
FILE *f = fopen("dnsseed.dat","w+");
if (f) {
CAutoFile cf(f);
cf << db;
}
}
} while(1);
}
extern "C" void* ThreadStats(void*) {
do {
CAddrDbStats stats;
db.GetStats(stats);
printf("*** %i available (%i tracked, %i new, %i active), %i banned; %i good; %llu DNS requests\n", stats.nAvail, stats.nTracked, stats.nNew, stats.nAvail - stats.nTracked - stats.nNew, stats.nBanned, stats.nGood, (unsigned long long)dns_opt.nRequests);
Sleep(10000);
} while(1);
}
static const string seeds[] = {"dnsseed.bluematt.me", "bitseed.xf2.org", "dnsseed.bitcoin.dashjr.org", "seed.bitcoin.sipa.be"};
extern "C" void* ThreadSeeder(void*) {
do {
for (int i=0; i<sizeof(seeds)/sizeof(seeds[0]); i++) {
vector<CIP> ips;
LookupHost(seeds[i].c_str(), ips);
for (vector<CIP>::iterator it = ips.begin(); it != ips.end(); it++) {
db.Add(CIPPort(*it, 8333), true);
}
}
Sleep(1800000);
} while(1);
}
13 years ago
int main(void) {
FILE *f = fopen("dnsseed.dat","r");
if (f) {
CAutoFile cf(f);
cf >> db;
}
pthread_t thread[NTHREADS+4];
13 years ago
for (int i=0; i<NTHREADS; i++) {
13 years ago
pthread_create(&thread[i], NULL, ThreadCrawler, NULL);
}
pthread_create(&thread[NTHREADS+0], NULL, ThreadSeeder, NULL);
pthread_create(&thread[NTHREADS+1], NULL, ThreadDumper, NULL);
pthread_create(&thread[NTHREADS+2], NULL, ThreadDNS, NULL);
pthread_create(&thread[NTHREADS+3], NULL, ThreadStats, NULL);
for (int i=0; i<NTHREADS+4; i++) {
13 years ago
void* res;
pthread_join(thread[i], &res);
}
return 0;
}