]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/dumresp.cc
Merge pull request #4289 from tuxis-ie/fix-api-documentation
[thirdparty/pdns.git] / pdns / dumresp.cc
1 /*
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * In addition, for the avoidance of any doubt, permission is granted to
10 * link this program with OpenSSL and to (re)distribute the binaries
11 * produced as the result of such linking.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 #include "iputils.hh"
26 #include "sstuff.hh"
27 #include "statbag.hh"
28 #include <atomic>
29 #include <sys/mman.h>
30 #include <thread>
31 StatBag S;
32
33 std::atomic<uint64_t>* g_counter;
34
35 void printStatus()
36 {
37 auto prev= g_counter->load();
38 for(;;) {
39 sleep(1);
40 cout<<g_counter->load()-prev<<"\t"<<g_counter->load()<<endl;
41 prev=g_counter->load();
42 }
43 }
44
45 void usage() {
46 cerr<<"Syntax: dumresp LOCAL-ADDRESS LOCAL-PORT NUMBER-OF-PROCESSES"<<endl;
47 }
48
49 int main(int argc, char** argv)
50 try
51 {
52 for(int i = 1; i < argc; i++) {
53 if((string) argv[i] == "--help"){
54 usage();
55 return(EXIT_SUCCESS);
56 }
57
58 if((string) argv[i] == "--version"){
59 cerr<<"dumresp "<<VERSION<<endl;
60 return(EXIT_SUCCESS);
61 }
62 }
63
64 if(argc != 4) {
65 usage();
66 exit(EXIT_FAILURE);
67 }
68
69 auto ptr = mmap(NULL, sizeof(std::atomic<uint64_t>), PROT_READ | PROT_WRITE,
70 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
71
72 g_counter = new(ptr) std::atomic<uint64_t>();
73
74 int i=1;
75 for(; i < atoi(argv[3]); ++i) {
76 if(!fork())
77 break;
78 }
79 if(i==1) {
80 std::thread t(printStatus);
81 t.detach();
82 }
83
84 ComboAddress local(argv[1], atoi(argv[2]));
85 Socket s(local.sin4.sin_family, SOCK_DGRAM);
86 #ifdef SO_REUSEPORT
87 int one=1;
88 if(setsockopt(s.getHandle(), SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one)) < 0)
89 unixDie("setsockopt for REUSEPORT");
90 #endif
91
92 s.bind(local);
93 cout<<"Bound to "<<local.toStringWithPort()<<endl;
94 char buffer[1500];
95 struct dnsheader* dh = (struct dnsheader*)buffer;
96 int len;
97 ComboAddress rem=local;
98 socklen_t socklen = rem.getSocklen();
99 for(;;) {
100 len=recvfrom(s.getHandle(), buffer, sizeof(buffer), 0, (struct sockaddr*)&rem, &socklen);
101 (*g_counter)++;
102 if(len < 0)
103 unixDie("recvfrom");
104
105 if(dh->qr)
106 continue;
107 dh->qr=1;
108 dh->ad=0;
109 if(sendto(s.getHandle(), buffer, len, 0, (struct sockaddr*)&rem, socklen) < 0)
110 unixDie("sendto");
111
112 }
113 }
114 catch(std::exception& e)
115 {
116 cerr<<"Fatal error: "<<e.what()<<endl;
117 exit(EXIT_FAILURE);
118 }