]>
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
);
110 if(setsockopt(tcpSocket
.getHandle(), SOL_SOCKET
, SO_REUSEPORT
, &one
, sizeof(one
)) < 0)
111 unixDie("setsockopt for REUSEPORT");
114 tcpSocket
.bind(local
);
115 tcpSocket
.listen(1024);
117 ComboAddress
rem("::1");
118 auto socklen
= rem
.getSocklen();
121 int sock
= accept(tcpSocket
.getHandle(), reinterpret_cast<struct sockaddr
*>(&rem
), &socklen
);
126 std::thread
connectionHandler(tcpConnectionHandler
, sock
);
127 connectionHandler
.detach();
131 int main(int argc
, char** argv
)
136 for(int i
= 1; i
< argc
; i
++) {
137 if(std::string(argv
[i
]) == "--help"){
139 return(EXIT_SUCCESS
);
142 if(std::string(argv
[i
]) == "--version"){
143 cerr
<<"dumresp "<<VERSION
<<endl
;
144 return(EXIT_SUCCESS
);
149 if (std::string(argv
[4]) == "tcp") {
162 auto ptr
= mmap(nullptr, sizeof(std::atomic
<uint64_t>), PROT_READ
| PROT_WRITE
,
163 MAP_SHARED
| MAP_ANONYMOUS
, -1, 0);
165 g_counter
= new(ptr
) std::atomic
<uint64_t>();
167 int numberOfListeners
= atoi(argv
[3]);
168 ComboAddress
local(argv
[1], atoi(argv
[2]));
171 for(; i
< numberOfListeners
; ++i
) {
177 std::thread
t(printStatus
);
181 for (int j
= 0; j
< numberOfListeners
; j
++) {
182 cout
<<"Listening to TCP "<<local
.toStringWithPort()<<endl
;
183 std::thread
tcpAcceptorThread(tcpAcceptor
, local
);
184 tcpAcceptorThread
.detach();
189 Socket
s(local
.sin4
.sin_family
, SOCK_DGRAM
);
192 if(setsockopt(s
.getHandle(), SOL_SOCKET
, SO_REUSEPORT
, &one
, sizeof(one
)) < 0)
193 unixDie("setsockopt for REUSEPORT");
197 cout
<<"Bound to UDP "<<local
.toStringWithPort()<<endl
;
199 ComboAddress rem
= local
;
200 socklen_t socklen
= rem
.getSocklen();
202 auto dh
= reinterpret_cast<struct dnsheader
*>(buffer
);
205 ssize_t len
= recvfrom(s
.getHandle(), buffer
, sizeof(buffer
), 0, reinterpret_cast<struct sockaddr
*>(&rem
), &socklen
);
210 if (static_cast<size_t>(len
) < sizeof(dnsheader
))
211 unixDie("too small " + std::to_string(len
));
216 turnQueryIntoResponse(dh
);
218 if(sendto(s
.getHandle(), buffer
, len
, 0, reinterpret_cast<const struct sockaddr
*>(&rem
), socklen
) < 0)
222 catch(const std::exception
& e
)
224 cerr
<<"Fatal error: "<<e
.what()<<endl
;