]>
git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/dumresp.cc
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
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.
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.
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.
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.
33 static std::atomic
<uint64_t>* g_counter
;
35 static void printStatus()
37 auto prev
= g_counter
->load();
40 cout
<<g_counter
->load()-prev
<<"\t"<<g_counter
->load()<<endl
;
41 prev
=g_counter
->load();
46 cerr
<<"Syntax: dumresp LOCAL-ADDRESS LOCAL-PORT NUMBER-OF-PROCESSES [tcp]"<<endl
;
49 static void turnQueryIntoResponse(dnsheader
* dh
)
57 static void tcpConnectionHandler(int sock
)
61 auto dh
= reinterpret_cast<struct dnsheader
*>(buffer
);
65 ssize_t got
= read(sock
, &len
, sizeof(len
));
71 if (got
!= sizeof(len
))
76 if (len
< sizeof(dnsheader
))
79 if (len
> sizeof(buffer
))
82 got
= read(sock
, buffer
, len
);
84 unixDie("read 2: " + std::to_string(got
) + " / " + std::to_string(len
));
89 turnQueryIntoResponse(dh
);
91 uint16_t wirelen
= htons(len
);
92 if (write(sock
, &wirelen
, sizeof(wirelen
)) != sizeof(wirelen
))
95 if (write(sock
, buffer
, len
) < 0)
101 catch(const std::exception
& e
) {
102 cerr
<<"TCP connection handler got an exception: "<<e
.what()<<endl
;
105 static void tcpAcceptor(const ComboAddress local
)
107 Socket
tcpSocket(local
.sin4
.sin_family
, SOCK_STREAM
);
108 setReusePort(tcpSocket
.getHandle());
109 tcpSocket
.bind(local
);
110 tcpSocket
.listen(1024);
112 ComboAddress
rem("::1");
113 auto socklen
= rem
.getSocklen();
116 int sock
= accept(tcpSocket
.getHandle(), reinterpret_cast<struct sockaddr
*>(&rem
), &socklen
);
121 std::thread
connectionHandler(tcpConnectionHandler
, sock
);
122 connectionHandler
.detach();
126 int main(int argc
, char** argv
)
131 for(int i
= 1; i
< argc
; i
++) {
132 if(std::string(argv
[i
]) == "--help"){
134 return(EXIT_SUCCESS
);
137 if(std::string(argv
[i
]) == "--version"){
138 cerr
<<"dumresp "<<VERSION
<<endl
;
139 return(EXIT_SUCCESS
);
144 if (std::string(argv
[4]) == "tcp") {
157 auto ptr
= mmap(nullptr, sizeof(std::atomic
<uint64_t>), PROT_READ
| PROT_WRITE
,
158 MAP_SHARED
| MAP_ANONYMOUS
, -1, 0);
160 g_counter
= new(ptr
) std::atomic
<uint64_t>();
162 int numberOfListeners
= atoi(argv
[3]);
163 ComboAddress
local(argv
[1], atoi(argv
[2]));
166 for(; i
< numberOfListeners
; ++i
) {
172 std::thread
t(printStatus
);
176 for (int j
= 0; j
< numberOfListeners
; j
++) {
177 cout
<<"Listening to TCP "<<local
.toStringWithPort()<<endl
;
178 std::thread
tcpAcceptorThread(tcpAcceptor
, local
);
179 tcpAcceptorThread
.detach();
184 Socket
s(local
.sin4
.sin_family
, SOCK_DGRAM
);
185 setReusePort(s
.getHandle());
187 cout
<<"Bound to UDP "<<local
.toStringWithPort()<<endl
;
189 ComboAddress rem
= local
;
190 socklen_t socklen
= rem
.getSocklen();
192 auto dh
= reinterpret_cast<struct dnsheader
*>(buffer
);
195 ssize_t len
= recvfrom(s
.getHandle(), buffer
, sizeof(buffer
), 0, reinterpret_cast<struct sockaddr
*>(&rem
), &socklen
);
200 if (static_cast<size_t>(len
) < sizeof(dnsheader
))
201 unixDie("too small " + std::to_string(len
));
206 turnQueryIntoResponse(dh
);
208 if(sendto(s
.getHandle(), buffer
, len
, 0, reinterpret_cast<const struct sockaddr
*>(&rem
), socklen
) < 0)
212 catch(const std::exception
& e
)
214 cerr
<<"Fatal error: "<<e
.what()<<endl
;