32 vector<string> vSeeds;
34 CDnsSeedOpts() : nThreads(24), nDnsThreads(24), nPort(53), fWipeBan(0), fWipeIgnore(0), mbox(NULL), ns(NULL), host(NULL), tor(NULL), magic(NULL), fNoStatsLog(0), vSeeds()
35 { nP2Port = GetDefaultPort(); }
37 void ParseCommandLine(int argc, char **argv) {
38 static const char *help = "NovaCoin-seeder\n"
39 "Usage: %s -h <host> -n <ns> [-m <mbox>] [-t <threads>] [-p <port>]\n"
42 "-s <seed> Seed node to collect peers from (replaces default)\n"
43 "-h <host> Hostname of the DNS seed\n"
44 "-n <ns> Hostname of the nameserver\n"
45 "-m <mbox> E-Mail address reported in SOA records\n"
46 "-t <threads> Number of crawlers to run in parallel (default 24)\n"
47 "-d <threads> Number of DNS server threads (default 24)\n"
48 "-p <port> UDP port to listen on (default 53)\n"
49 "-o <ip:port> Tor proxy IP/Port\n"
50 "--p2port <port> P2P port to connect to\n"
51 "--magic <hex> Magic string/network prefix\n"
52 "--wipeban Wipe list of banned nodes\n"
53 "--wipeignore Wipe list of ignored nodes\n"
54 "--nostatslog Do not write dnsstats.log file (default write)\n"
55 "-?, --help Show this text\n"
57 bool showHelp = false;
60 static struct option long_options[] = {
61 {"seed", required_argument, 0, 's'},
62 {"host", required_argument, 0, 'h'},
63 {"ns", required_argument, 0, 'n'},
64 {"mbox", required_argument, 0, 'm'},
65 {"threads", required_argument, 0, 't'},
66 {"dnsthreads", required_argument, 0, 'd'},
67 {"port", required_argument, 0, 'p'},
68 {"onion", required_argument, 0, 'o'},
69 {"p2port", required_argument, 0, 'b'},
70 {"magic", required_argument, 0, 'k'},
71 {"wipeban", no_argument, &fWipeBan, 1},
72 {"wipeignore", no_argument, &fWipeBan, 1},
73 {"nostatslog", no_argument, &fNoStatsLog, 1},
74 {"help", no_argument, 0, 'h'},
78 int c = getopt_long(argc, argv, "s:h:n:m:t:p:d:o:b:k:", long_options, &option_index);
82 vSeeds.push_back(optarg);
102 int n = strtol(optarg, NULL, 10);
103 if (n > 0 && n < 1000) nThreads = n;
108 int n = strtol(optarg, NULL, 10);
109 if (n > 0 && n < 1000) nDnsThreads = n;
114 int p = strtol(optarg, NULL, 10);
115 if (p > 0 && p < 65536) nPort = p;
125 int p = strtol(optarg, NULL, 10);
126 if (p > 0 && p < 65536) nP2Port = p;
134 if (strlen(magic)!=8)
136 n = strtol(magic, NULL, 16);
137 if (n==0 && strcmp(magic, "00000000"))
139 for (n=0; n<4; ++n) {
140 sscanf(&magic[n*2], "%2x", &c);
141 pchMessageStart[n] = (unsigned char) (c & 0xff);
152 if (host != NULL && ns == NULL) showHelp = true;
153 if (showHelp) fprintf(stderr, help, argv[0]);
163 extern "C" void* ThreadCrawler(void* data) {
167 if (!db.Get(ip, wait)) {
169 wait += rand() % (500 * NTHREADS);
174 vector<CAddress> addr;
177 std::string clientSV;
178 bool ret = TestNode(ip,ban,clientV,clientSV,blocks,addr);
181 db.Good(ip, clientV, clientSV, blocks);
188 extern "C" int GetIPList(void *thread, addr_t *addr, int max, int ipv4, int ipv6);
192 dns_opt_t dns_opt; // must be first
194 vector<addr_t> cache;
197 unsigned int cacheHits;
200 void cacheHit(bool force = false) {
201 static bool nets[NET_MAX] = {};
202 if (!nets[NET_IPV4]) {
203 nets[NET_IPV4] = true;
204 nets[NET_IPV6] = true;
206 time_t now = time(NULL);
208 if (force || cacheHits > (cache.size()*cache.size()/400) || (cacheHits*cacheHits > cache.size() / 20 && (now - cacheTime > 5))) {
210 db.GetIPs(ips, 1000, nets);
215 cache.reserve(ips.size());
216 for (set<CNetAddr>::iterator it = ips.begin(); it != ips.end(); it++) {
218 struct in6_addr addr6;
219 if ((*it).GetInAddr(&addr)) {
222 memcpy(&a.data.v4, &addr, 4);
226 } else if ((*it).GetIn6Addr(&addr6)) {
229 memcpy(&a.data.v6, &addr6, 16);
240 CDnsThread(CDnsSeedOpts* opts, int idIn) : id(idIn) {
241 dns_opt.host = opts->host;
242 dns_opt.ns = opts->ns;
243 dns_opt.mbox = opts->mbox;
244 dns_opt.datattl = 60;
245 dns_opt.nsttl = 40000;
246 dns_opt.cb = GetIPList;
247 dns_opt.port = opts->nPort;
248 dns_opt.nRequests = 0;
264 extern "C" int GetIPList(void *data, addr_t* addr, int max, int ipv4, int ipv6) {
265 CDnsThread *thread = (CDnsThread*)data;
267 unsigned int size = thread->cache.size();
268 unsigned int maxmax = (ipv4 ? thread->nIPv4 : 0) + (ipv6 ? thread->nIPv6 : 0);
275 int j = i + (rand() % (size - i));
277 bool ok = (ipv4 && thread->cache[j].v == 4) ||
278 (ipv6 && thread->cache[j].v == 6);
284 addr[i] = thread->cache[j];
285 thread->cache[j] = thread->cache[i];
286 thread->cache[i] = addr[i];
292 vector<CDnsThread*> dnsThread;
294 extern "C" void* ThreadDNS(void* arg) {
295 CDnsThread *thread = (CDnsThread*)arg;
299 int StatCompare(const CAddrReport& a, const CAddrReport& b) {
300 if (a.uptime[4] == b.uptime[4]) {
301 if (a.uptime[3] == b.uptime[3]) {
302 return a.clientVersion > b.clientVersion;
304 return a.uptime[3] > b.uptime[3];
307 return a.uptime[4] > b.uptime[4];
311 extern "C" void* ThreadDumper(void*) {
315 FILE *f = fopen("dnsseed.dat.new","w+");
321 rename("dnsseed.dat.new", "dnsseed.dat");
323 FILE *d = fopen("dnsseed.dump", "w");
324 vector<CAddrReport> v = db.GetAll();
325 sort(v.begin(), v.end(), StatCompare);
326 fprintf(d, "# address \t%%(2h)\t%%(8h)\t%%(1d)\t%%(7d)\t%%(30d)\tblocks\tversion\n");
327 double stat[5]={0,0,0,0,0};
328 for (vector<CAddrReport>::const_iterator it = v.begin(); it < v.end(); it++) {
329 CAddrReport rep = *it;
330 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());
331 stat[0] += rep.uptime[0];
332 stat[1] += rep.uptime[1];
333 stat[2] += rep.uptime[2];
334 stat[3] += rep.uptime[3];
335 stat[4] += rep.uptime[4];
340 FILE *ff = fopen("dnsstats.log", "a");
341 fprintf(ff, "%llu %g %g %g %g %g\n", (unsigned long long)(time(NULL)), stat[0], stat[1], stat[2], stat[3], stat[4]);
348 extern "C" void* ThreadStats(void*) {
352 time_t tim = time(NULL);
353 struct tm *tmp = localtime(&tim);
354 strftime(c, 256, "[%y-%m-%d %H:%M:%S]", tmp);
360 printf("\n\n\n\x1b[3A");
363 printf("\x1b[2K\x1b[u");
365 uint64_t requests = 0;
366 uint64_t queries = 0;
367 for (unsigned int i=0; i<dnsThread.size(); i++) {
368 requests += dnsThread[i]->dns_opt.nRequests;
369 queries += dnsThread[i]->dbQueries;
371 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);
376 static vector<string> vSeeds;
377 unsigned short nP2Port;
379 extern "C" void* ThreadSeeder(void*) {
380 vector<string> vDnsSeeds;
381 vector<string>::iterator itr;
382 for (itr = vSeeds.begin(); itr != vSeeds.end(); itr++) {
383 size_t len = itr->length();
384 if (len>=6 && !itr->compare(len-6, 6, ".onion"))
385 db.Add(CService(itr->c_str(), nP2Port), true);
387 vDnsSeeds.push_back(*itr);
390 for (itr = vDnsSeeds.begin(); itr != vDnsSeeds.end(); itr++) {
391 vector<CNetAddr> ips;
392 LookupHost(itr->c_str(), ips);
393 for (vector<CNetAddr>::iterator it = ips.begin(); it != ips.end(); it++) {
394 db.Add(CService(*it, nP2Port), true);
401 int main(int argc, char **argv) {
402 signal(SIGPIPE, SIG_IGN);
403 setbuf(stdout, NULL);
405 opts.ParseCommandLine(argc, argv);
406 nP2Port = opts.nP2Port;
407 vSeeds.reserve(vSeeds.size() + opts.vSeeds.size());
408 vSeeds.insert(vSeeds.end(), opts.vSeeds.begin(), opts.vSeeds.end());
409 if (opts.vSeeds.empty()) {
410 vSeeds.push_back("novacoin.ru");
411 vSeeds.push_back("itzod.ru");
414 CService service(opts.tor, 9050);
415 if (service.IsValid()) {
416 printf("Using Tor proxy at %s\n", service.ToStringIPPort().c_str());
417 SetProxy(NET_TOR, service);
422 printf("No nameserver set. Not starting DNS server.\n");
425 if (fDNS && !opts.host) {
426 fprintf(stderr, "No hostname set. Please use -h.\n");
429 FILE *f = fopen("dnsseed.dat","r");
431 printf("Loading dnsseed.dat...");
436 if (opts.fWipeIgnore)
440 if (opts.fNoStatsLog) NoLog = true;
441 pthread_t threadDns, threadSeed, threadDump, threadStats;
442 printf("Starting seeder...");
443 pthread_create(&threadSeed, NULL, ThreadSeeder, NULL);
445 printf("Starting %i crawler threads...", opts.nThreads);
446 for (int i=0; i<opts.nThreads; i++) {
448 pthread_create(&thread, NULL, ThreadCrawler, NULL);
451 pthread_create(&threadDump, NULL, ThreadDumper, NULL);
453 printf("Starting %i DNS threads for %s on %s (port %i)...", opts.nDnsThreads, opts.host, opts.ns, opts.nPort);
455 for (int i=0; i<opts.nDnsThreads; i++) {
456 dnsThread.push_back(new CDnsThread(&opts, i));
457 pthread_create(&threadDns, NULL, ThreadDNS, dnsThread[i]);
463 pthread_create(&threadStats, NULL, ThreadStats, NULL);
465 pthread_join(threadDump, &res);