]>
Commit | Line | Data |
---|---|---|
12471842 PL |
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 | */ | |
870a0fe4 AT |
22 | #ifdef HAVE_CONFIG_H |
23 | #include "config.h" | |
24 | #endif | |
c1e527e3 | 25 | #include "iputils.hh" |
26 | #include "sstuff.hh" | |
27 | #include "statbag.hh" | |
7b85069a | 28 | #include <atomic> |
29 | #include <sys/mman.h> | |
30 | #include <thread> | |
c1e527e3 | 31 | StatBag S; |
32 | ||
7b85069a | 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 | ||
e06df890 PL |
45 | void usage() { |
46 | cerr<<"Syntax: dumresp LOCAL-ADDRESS LOCAL-PORT NUMBER-OF-PROCESSES"<<endl; | |
47 | } | |
48 | ||
c1e527e3 | 49 | int main(int argc, char** argv) |
50 | try | |
51 | { | |
e06df890 PL |
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 | ||
4465e941 | 64 | if(argc != 4) { |
e06df890 | 65 | usage(); |
c1e527e3 | 66 | exit(EXIT_FAILURE); |
67 | } | |
68 | ||
7b85069a | 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) { | |
c1e527e3 | 76 | if(!fork()) |
77 | break; | |
78 | } | |
7b85069a | 79 | if(i==1) { |
80 | std::thread t(printStatus); | |
81 | t.detach(); | |
82 | } | |
83 | ||
4465e941 | 84 | ComboAddress local(argv[1], atoi(argv[2])); |
85 | Socket s(local.sin4.sin_family, SOCK_DGRAM); | |
c648bdce | 86 | #ifdef SO_REUSEPORT |
c1e527e3 | 87 | int one=1; |
88 | if(setsockopt(s.getHandle(), SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one)) < 0) | |
89 | unixDie("setsockopt for REUSEPORT"); | |
c648bdce | 90 | #endif |
c1e527e3 | 91 | |
92 | s.bind(local); | |
4465e941 | 93 | cout<<"Bound to "<<local.toStringWithPort()<<endl; |
c1e527e3 | 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); | |
7b85069a | 101 | (*g_counter)++; |
c1e527e3 | 102 | if(len < 0) |
103 | unixDie("recvfrom"); | |
786d6f33 | 104 | |
c1e527e3 | 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 | } |