]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/dumresp.cc
Merge pull request #3172 from Whissi/complete-geobackend-removal
[thirdparty/pdns.git] / pdns / dumresp.cc
1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4 #include "iputils.hh"
5 #include "sstuff.hh"
6 #include "statbag.hh"
7 #include <atomic>
8 #include <sys/mman.h>
9 #include <thread>
10 StatBag S;
11
12 std::atomic<uint64_t>* g_counter;
13
14 void printStatus()
15 {
16 auto prev= g_counter->load();
17 for(;;) {
18 sleep(1);
19 cout<<g_counter->load()-prev<<"\t"<<g_counter->load()<<endl;
20 prev=g_counter->load();
21 }
22 }
23
24 int main(int argc, char** argv)
25 try
26 {
27 if(argc != 4) {
28 cerr<<"Syntax: dumresp local-address local-port number-of-processes "<<endl;
29 exit(EXIT_FAILURE);
30 }
31
32
33 auto ptr = mmap(NULL, sizeof(std::atomic<uint64_t>), PROT_READ | PROT_WRITE,
34 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
35
36 g_counter = new(ptr) std::atomic<uint64_t>();
37
38 int i=1;
39 for(; i < atoi(argv[3]); ++i) {
40 if(!fork())
41 break;
42 }
43 if(i==1) {
44 std::thread t(printStatus);
45 t.detach();
46 }
47
48 ComboAddress local(argv[1], atoi(argv[2]));
49 Socket s(local.sin4.sin_family, SOCK_DGRAM);
50 #ifdef SO_REUSEPORT
51 int one=1;
52 if(setsockopt(s.getHandle(), SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one)) < 0)
53 unixDie("setsockopt for REUSEPORT");
54 #endif
55
56 s.bind(local);
57 cout<<"Bound to "<<local.toStringWithPort()<<endl;
58 char buffer[1500];
59 struct dnsheader* dh = (struct dnsheader*)buffer;
60 int len;
61 ComboAddress rem=local;
62 socklen_t socklen = rem.getSocklen();
63 for(;;) {
64 len=recvfrom(s.getHandle(), buffer, sizeof(buffer), 0, (struct sockaddr*)&rem, &socklen);
65 (*g_counter)++;
66 if(len < 0)
67 unixDie("recvfrom");
68
69 if(dh->qr)
70 continue;
71 dh->qr=1;
72 dh->ad=0;
73 if(sendto(s.getHandle(), buffer, len, 0, (struct sockaddr*)&rem, socklen) < 0)
74 unixDie("sendto");
75
76 }
77 }
78 catch(std::exception& e)
79 {
80 cerr<<"Fatal error: "<<e.what()<<endl;
81 exit(EXIT_FAILURE);
82 }