Initial commit
[novacoin-seeder.git] / main.cpp
1 #include <algorithm>
2
3 #include <pthread.h>
4 #include <signal.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <getopt.h>
8
9 #include "bitcoin.h"
10 #include "db.h"
11
12 #define NTHREADS 24
13
14 using namespace std;
15
16 class CDnsSeedOpts {
17 public:
18   int nThreads;
19   int nPort;
20   int nP2Port;
21   int nDnsThreads;
22   int fWipeBan;
23   int fWipeIgnore;
24   const char *mbox;
25   const char *ns;
26   const char *host;
27   const char *tor;
28   const char *magic;
29   vector<string> vSeeds;
30   
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(); }
33   
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"
37                               "\n"
38                               "Options:\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"
52                               "\n";
53     bool showHelp = false;
54
55     while(1) {
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'},
70         {0, 0, 0, 0}
71       };
72       int option_index = 0;
73       int c = getopt_long(argc, argv, "s:h:n:m:t:p:d:o:b:k:", long_options, &option_index);
74       if (c == -1) break;
75       switch (c) {
76         case 's': {
77           vSeeds.push_back(optarg);
78           break;
79         }
80
81         case 'h': {
82           host = optarg;
83           break;
84         }
85         
86         case 'm': {
87           mbox = optarg;
88           break;
89         }
90         
91         case 'n': {
92           ns = optarg;
93           break;
94         }
95         
96         case 't': {
97           int n = strtol(optarg, NULL, 10);
98           if (n > 0 && n < 1000) nThreads = n;
99           break;
100         }
101
102         case 'd': {
103           int n = strtol(optarg, NULL, 10);
104           if (n > 0 && n < 1000) nDnsThreads = n;
105           break;
106         }
107
108         case 'p': {
109           int p = strtol(optarg, NULL, 10);
110           if (p > 0 && p < 65536) nPort = p;
111           break;
112         }
113         
114         case 'o': {
115           tor = optarg;
116           break;
117         }
118
119         case 'b': {
120           int p = strtol(optarg, NULL, 10);
121           if (p > 0 && p < 65536) nP2Port = p;
122           break;
123         }
124
125         case 'k': {
126           long int n;
127           unsigned int c;
128           magic = optarg;
129           if (strlen(magic)!=8)
130             break;
131           n = strtol(magic, NULL, 16);
132           if (n==0 && strcmp(magic, "00000000"))
133             break;
134           for (n=0; n<4; ++n) {
135             sscanf(&magic[n*2], "%2x", &c);
136             pchMessageStart[n] = (unsigned char) (c & 0xff);
137           }
138           break;
139         }
140
141         case '?': {
142           showHelp = true;
143           break;
144         }
145       }
146     }
147     if (host != NULL && ns == NULL) showHelp = true;
148     if (showHelp) fprintf(stderr, help, argv[0]);
149   }
150 };
151
152 extern "C" {
153 #include "dns.h"
154 }
155
156 CAddrDb db;
157
158 extern "C" void* ThreadCrawler(void* data) {
159   do {
160     CService ip;
161     int wait = 5;
162     if (!db.Get(ip, wait)) {
163       wait *= 1000;
164       wait += rand() % (500 * NTHREADS);
165       Sleep(wait);
166       continue;
167     }
168     int ban = 0;
169     vector<CAddress> addr;
170     int clientV = 0;
171     int blocks = 0;
172     std::string clientSV;
173     bool ret = TestNode(ip,ban,clientV,clientSV,blocks,addr);
174     db.Add(addr);
175     if (ret) {
176       db.Good(ip, clientV, clientSV, blocks);
177     } else {
178       db.Bad(ip, ban);
179     }
180   } while(1);
181 }
182
183 extern "C" int GetIPList(void *thread, addr_t *addr, int max, int ipv4, int ipv6);
184
185 class CDnsThread {
186 public:
187   dns_opt_t dns_opt; // must be first
188   const int id;
189   vector<addr_t> cache;
190   int nIPv4, nIPv6;
191   time_t cacheTime;
192   unsigned int cacheHits;
193   uint64_t dbQueries;
194
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;
200     }
201     time_t now = time(NULL);
202     cacheHits++;
203     if (force || cacheHits > (cache.size()*cache.size()/400) || (cacheHits*cacheHits > cache.size() / 20 && (now - cacheTime > 5))) {
204       set<CNetAddr> ips;
205       db.GetIPs(ips, 1000, nets);
206       dbQueries++;
207       cache.clear();
208       nIPv4 = 0;
209       nIPv6 = 0;
210       cache.reserve(ips.size());
211       for (set<CNetAddr>::iterator it = ips.begin(); it != ips.end(); it++) {
212         struct in_addr addr;
213         struct in6_addr addr6;
214         if ((*it).GetInAddr(&addr)) {
215           addr_t a;
216           a.v = 4;
217           memcpy(&a.data.v4, &addr, 4);
218           cache.push_back(a);
219           nIPv4++;
220 #ifdef USE_IPV6
221         } else if ((*it).GetIn6Addr(&addr6)) {
222           addr_t a;
223           a.v = 6;
224           memcpy(&a.data.v6, &addr6, 16);
225           cache.push_back(a);
226           nIPv6++;
227 #endif
228         }
229       }
230       cacheHits = 0;
231       cacheTime = now;
232     }
233   }
234
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;
244     cache.clear();
245     cache.reserve(1000);
246     cacheTime = 0;
247     cacheHits = 0;
248     dbQueries = 0;
249     nIPv4 = 0;
250     nIPv6 = 0;
251     cacheHit(true);
252   }
253
254   void run() {
255     dnsserver(&dns_opt);
256   }
257 };
258
259 extern "C" int GetIPList(void *data, addr_t* addr, int max, int ipv4, int ipv6) {
260   CDnsThread *thread = (CDnsThread*)data;
261   thread->cacheHit();
262   unsigned int size = thread->cache.size();
263   unsigned int maxmax = (ipv4 ? thread->nIPv4 : 0) + (ipv6 ? thread->nIPv6 : 0);
264   if (max > size)
265     max = size;
266   if (max > maxmax)
267     max = maxmax;
268   int i=0;
269   while (i<max) {
270     int j = i + (rand() % (size - i));
271     do {
272         bool ok = (ipv4 && thread->cache[j].v == 4) || 
273                   (ipv6 && thread->cache[j].v == 6);
274         if (ok) break;
275         j++;
276         if (j==size)
277             j=i;
278     } while(1);
279     addr[i] = thread->cache[j];
280     thread->cache[j] = thread->cache[i];
281     thread->cache[i] = addr[i];
282     i++;
283   }
284   return max;
285 }
286
287 vector<CDnsThread*> dnsThread;
288
289 extern "C" void* ThreadDNS(void* arg) {
290   CDnsThread *thread = (CDnsThread*)arg;
291   thread->run();
292 }
293
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;
298     } else {
299       return a.uptime[3] > b.uptime[3];
300     }
301   } else {
302     return a.uptime[4] > b.uptime[4];
303   }
304 }
305
306 extern "C" void* ThreadDumper(void*) {
307   do {
308     Sleep(100000);
309     {
310       FILE *f = fopen("dnsseed.dat.new","w+");
311       if (f) {
312         {
313           CAutoFile cf(f);
314           cf << db;
315         }
316         rename("dnsseed.dat.new", "dnsseed.dat");
317       }
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];
331       }
332       fclose(d);
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]);
335       fclose(ff);
336     }
337   } while(1);
338 }
339
340 extern "C" void* ThreadStats(void*) {
341   bool first = true;
342   do {
343     char c[256];
344     time_t tim = time(NULL);
345     struct tm *tmp = localtime(&tim);
346     strftime(c, 256, "[%y-%m-%d %H:%M:%S]", tmp);
347     CAddrDbStats stats;
348     db.GetStats(stats);
349     if (first)
350     {
351       first = false;
352       printf("\n\n\n\x1b[3A");
353     }
354     else
355       printf("\x1b[2K\x1b[u");
356     printf("\x1b[s");
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;
362     }
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);
364     Sleep(1000);
365   } while(1);
366 }
367
368 static vector<string> vSeeds;
369 unsigned short nP2Port;
370
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);
378     else
379       vDnsSeeds.push_back(*itr);
380   }
381   do {
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);
387       }
388     }
389     Sleep(1800000);
390   } while(1);
391 }
392
393 int main(int argc, char **argv) {
394   signal(SIGPIPE, SIG_IGN);
395   setbuf(stdout, NULL);
396   CDnsSeedOpts opts;
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");
404   }
405   if (opts.tor) {
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);
410     }
411   }
412   bool fDNS = true;
413   if (!opts.ns) {
414     printf("No nameserver set. Not starting DNS server.\n");
415     fDNS = false;
416   }
417   if (fDNS && !opts.host) {
418     fprintf(stderr, "No hostname set. Please use -h.\n");
419     exit(1);
420   }
421   FILE *f = fopen("dnsseed.dat","r");
422   if (f) {
423     printf("Loading dnsseed.dat...");
424     CAutoFile cf(f);
425     cf >> db;
426     if (opts.fWipeBan)
427         db.banned.clear();
428     if (opts.fWipeIgnore)
429         db.ResetIgnores();
430     printf("done\n");
431   }
432   pthread_t threadDns, threadSeed, threadDump, threadStats;
433   printf("Starting seeder...");
434   pthread_create(&threadSeed, NULL, ThreadSeeder, NULL);
435   printf("done\n");
436   printf("Starting %i crawler threads...", opts.nThreads);
437   for (int i=0; i<opts.nThreads; i++) {
438     pthread_t thread;
439     pthread_create(&thread, NULL, ThreadCrawler, NULL);
440   }
441   printf("done\n");
442   pthread_create(&threadDump, NULL, ThreadDumper, NULL);
443   if (fDNS) {
444     printf("Starting %i DNS threads for %s on %s (port %i)...", opts.nDnsThreads, opts.host, opts.ns, opts.nPort);
445     dnsThread.clear();
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]);
449       printf(".");
450       Sleep(20);
451     }
452     printf("done\n");
453   }
454   pthread_create(&threadStats, NULL, ThreadStats, NULL);
455   void* res;
456   pthread_join(threadDump, &res);
457   return 0;
458 }