29 vector<string> vSeeds;
31 CDnsSeedOpts() : nThreads(24), nDnsThreads(24), nPort(53), fWipeBan(0), fWipeIgnore(0), mbox(NULL), ns(NULL), host(NULL), tor(NULL), magic(NULL), vSeeds()
32 { nP2Port = GetDefaultPort(); }
34 void ParseCommandLine(int argc, char **argv) {
35 static const char *help = "NovaCoin-seeder\n"
36 "Usage: %s -h <host> -n <ns> [-m <mbox>] [-t <threads>] [-p <port>]\n"
39 "-s <seed> Seed node to collect peers from (replaces default)\n"
40 "-h <host> Hostname of the DNS seed\n"
41 "-n <ns> Hostname of the nameserver\n"
42 "-m <mbox> E-Mail address reported in SOA records\n"
43 "-t <threads> Number of crawlers to run in parallel (default 24)\n"
44 "-d <threads> Number of DNS server threads (default 24)\n"
45 "-p <port> UDP port to listen on (default 53)\n"
46 "-o <ip:port> Tor proxy IP/Port\n"
47 "--p2port <port> P2P port to connect to\n"
48 "--magic <hex> Magic string/network prefix\n"
49 "--wipeban Wipe list of banned nodes\n"
50 "--wipeignore Wipe list of ignored nodes\n"
51 "-?, --help Show this text\n"
53 bool showHelp = false;
56 static struct option long_options[] = {
57 {"seed", required_argument, 0, 's'},
58 {"host", required_argument, 0, 'h'},
59 {"ns", required_argument, 0, 'n'},
60 {"mbox", required_argument, 0, 'm'},
61 {"threads", required_argument, 0, 't'},
62 {"dnsthreads", required_argument, 0, 'd'},
63 {"port", required_argument, 0, 'p'},
64 {"onion", required_argument, 0, 'o'},
65 {"p2port", required_argument, 0, 'b'},
66 {"magic", required_argument, 0, 'k'},
67 {"wipeban", no_argument, &fWipeBan, 1},
68 {"wipeignore", no_argument, &fWipeBan, 1},
69 {"help", no_argument, 0, 'h'},
73 int c = getopt_long(argc, argv, "s:h:n:m:t:p:d:o:b:k:", long_options, &option_index);
77 vSeeds.push_back(optarg);
97 int n = strtol(optarg, NULL, 10);
98 if (n > 0 && n < 1000) nThreads = n;
103 int n = strtol(optarg, NULL, 10);
104 if (n > 0 && n < 1000) nDnsThreads = n;
109 int p = strtol(optarg, NULL, 10);
110 if (p > 0 && p < 65536) nPort = p;
120 int p = strtol(optarg, NULL, 10);
121 if (p > 0 && p < 65536) nP2Port = p;
129 if (strlen(magic)!=8)
131 n = strtol(magic, NULL, 16);
132 if (n==0 && strcmp(magic, "00000000"))
134 for (n=0; n<4; ++n) {
135 sscanf(&magic[n*2], "%2x", &c);
136 pchMessageStart[n] = (unsigned char) (c & 0xff);
147 if (host != NULL && ns == NULL) showHelp = true;
148 if (showHelp) fprintf(stderr, help, argv[0]);
158 extern "C" void* ThreadCrawler(void* data) {
162 if (!db.Get(ip, wait)) {
164 wait += rand() % (500 * NTHREADS);
169 vector<CAddress> addr;
172 std::string clientSV;
173 bool ret = TestNode(ip,ban,clientV,clientSV,blocks,addr);
176 db.Good(ip, clientV, clientSV, blocks);
183 extern "C" int GetIPList(void *thread, addr_t *addr, int max, int ipv4, int ipv6);
187 dns_opt_t dns_opt; // must be first
189 vector<addr_t> cache;
192 unsigned int cacheHits;
195 void cacheHit(bool force = false) {
196 static bool nets[NET_MAX] = {};
197 if (!nets[NET_IPV4]) {
198 nets[NET_IPV4] = true;
199 nets[NET_IPV6] = true;
201 time_t now = time(NULL);
203 if (force || cacheHits > (cache.size()*cache.size()/400) || (cacheHits*cacheHits > cache.size() / 20 && (now - cacheTime > 5))) {
205 db.GetIPs(ips, 1000, nets);
210 cache.reserve(ips.size());
211 for (set<CNetAddr>::iterator it = ips.begin(); it != ips.end(); it++) {
213 struct in6_addr addr6;
214 if ((*it).GetInAddr(&addr)) {
217 memcpy(&a.data.v4, &addr, 4);
221 } else if ((*it).GetIn6Addr(&addr6)) {
224 memcpy(&a.data.v6, &addr6, 16);
235 CDnsThread(CDnsSeedOpts* opts, int idIn) : id(idIn) {
236 dns_opt.host = opts->host;
237 dns_opt.ns = opts->ns;
238 dns_opt.mbox = opts->mbox;
239 dns_opt.datattl = 60;
240 dns_opt.nsttl = 40000;
241 dns_opt.cb = GetIPList;
242 dns_opt.port = opts->nPort;
243 dns_opt.nRequests = 0;
259 extern "C" int GetIPList(void *data, addr_t* addr, int max, int ipv4, int ipv6) {
260 CDnsThread *thread = (CDnsThread*)data;
262 unsigned int size = thread->cache.size();
263 unsigned int maxmax = (ipv4 ? thread->nIPv4 : 0) + (ipv6 ? thread->nIPv6 : 0);
270 int j = i + (rand() % (size - i));
272 bool ok = (ipv4 && thread->cache[j].v == 4) ||
273 (ipv6 && thread->cache[j].v == 6);
279 addr[i] = thread->cache[j];
280 thread->cache[j] = thread->cache[i];
281 thread->cache[i] = addr[i];
287 vector<CDnsThread*> dnsThread;
289 extern "C" void* ThreadDNS(void* arg) {
290 CDnsThread *thread = (CDnsThread*)arg;
294 int StatCompare(const CAddrReport& a, const CAddrReport& b) {
295 if (a.uptime[4] == b.uptime[4]) {
296 if (a.uptime[3] == b.uptime[3]) {
297 return a.clientVersion > b.clientVersion;
299 return a.uptime[3] > b.uptime[3];
302 return a.uptime[4] > b.uptime[4];
306 extern "C" void* ThreadDumper(void*) {
310 FILE *f = fopen("dnsseed.dat.new","w+");
316 rename("dnsseed.dat.new", "dnsseed.dat");
318 FILE *d = fopen("dnsseed.dump", "w");
319 vector<CAddrReport> v = db.GetAll();
320 sort(v.begin(), v.end(), StatCompare);
321 fprintf(d, "# address \t%%(2h)\t%%(8h)\t%%(1d)\t%%(7d)\t%%(30d)\tblocks\tversion\n");
322 double stat[5]={0,0,0,0,0};
323 for (vector<CAddrReport>::const_iterator it = v.begin(); it < v.end(); it++) {
324 CAddrReport rep = *it;
325 fprintf(d, "%s\t%.2f%%\t%.2f%%\t%.2f%%\t%.2f%%\t%.2f%%\t%i\t%i \"%s\"\n", rep.ip.ToString().c_str(), 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.clientVersion, rep.clientSubVersion.c_str());
326 stat[0] += rep.uptime[0];
327 stat[1] += rep.uptime[1];
328 stat[2] += rep.uptime[2];
329 stat[3] += rep.uptime[3];
330 stat[4] += rep.uptime[4];
333 FILE *ff = fopen("dnsstats.log", "a");
334 fprintf(ff, "%llu %g %g %g %g %g\n", (unsigned long long)(time(NULL)), stat[0], stat[1], stat[2], stat[3], stat[4]);
340 extern "C" void* ThreadStats(void*) {
344 time_t tim = time(NULL);
345 struct tm *tmp = localtime(&tim);
346 strftime(c, 256, "[%y-%m-%d %H:%M:%S]", tmp);
352 printf("\n\n\n\x1b[3A");
355 printf("\x1b[2K\x1b[u");
357 uint64_t requests = 0;
358 uint64_t queries = 0;
359 for (unsigned int i=0; i<dnsThread.size(); i++) {
360 requests += dnsThread[i]->dns_opt.nRequests;
361 queries += dnsThread[i]->dbQueries;
363 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);
368 static vector<string> vSeeds;
369 unsigned short nP2Port;
371 extern "C" void* ThreadSeeder(void*) {
372 vector<string> vDnsSeeds;
373 vector<string>::iterator itr;
374 for (itr = vSeeds.begin(); itr != vSeeds.end(); itr++) {
375 size_t len = itr->length();
376 if (len>=6 && !itr->compare(len-6, 6, ".onion"))
377 db.Add(CService(itr->c_str(), nP2Port), true);
379 vDnsSeeds.push_back(*itr);
382 for (itr = vDnsSeeds.begin(); itr != vDnsSeeds.end(); itr++) {
383 vector<CNetAddr> ips;
384 LookupHost(itr->c_str(), ips);
385 for (vector<CNetAddr>::iterator it = ips.begin(); it != ips.end(); it++) {
386 db.Add(CService(*it, nP2Port), true);
393 int main(int argc, char **argv) {
394 signal(SIGPIPE, SIG_IGN);
395 setbuf(stdout, NULL);
397 opts.ParseCommandLine(argc, argv);
398 nP2Port = opts.nP2Port;
399 vSeeds.reserve(vSeeds.size() + opts.vSeeds.size());
400 vSeeds.insert(vSeeds.end(), opts.vSeeds.begin(), opts.vSeeds.end());
401 if (opts.vSeeds.empty()) {
402 vSeeds.push_back("novacoin.ru");
403 vSeeds.push_back("itzod.ru");
406 CService service(opts.tor, 9050);
407 if (service.IsValid()) {
408 printf("Using Tor proxy at %s\n", service.ToStringIPPort().c_str());
409 SetProxy(NET_TOR, service);
414 printf("No nameserver set. Not starting DNS server.\n");
417 if (fDNS && !opts.host) {
418 fprintf(stderr, "No hostname set. Please use -h.\n");
421 FILE *f = fopen("dnsseed.dat","r");
423 printf("Loading dnsseed.dat...");
428 if (opts.fWipeIgnore)
432 pthread_t threadDns, threadSeed, threadDump, threadStats;
433 printf("Starting seeder...");
434 pthread_create(&threadSeed, NULL, ThreadSeeder, NULL);
436 printf("Starting %i crawler threads...", opts.nThreads);
437 for (int i=0; i<opts.nThreads; i++) {
439 pthread_create(&thread, NULL, ThreadCrawler, NULL);
442 pthread_create(&threadDump, NULL, ThreadDumper, NULL);
444 printf("Starting %i DNS threads for %s on %s (port %i)...", opts.nDnsThreads, opts.host, opts.ns, opts.nPort);
446 for (int i=0; i<opts.nDnsThreads; i++) {
447 dnsThread.push_back(new CDnsThread(&opts, i));
448 pthread_create(&threadDns, NULL, ThreadDNS, dnsThread[i]);
454 pthread_create(&threadStats, NULL, ThreadStats, NULL);
456 pthread_join(threadDump, &res);