]>
Commit | Line | Data |
---|---|---|
288f4aa9 | 1 | /* |
6edbf68a PL |
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 | |
3e61e7f7 | 25 | |
76473b92 KM |
26 | #include <netdb.h> |
27 | #include <sys/stat.h> | |
28 | #include <unistd.h> | |
fa8fd4d2 | 29 | |
2470b36e | 30 | #include "ws-recursor.hh" |
49a699c4 | 31 | #include <pthread.h> |
3ea54bf0 | 32 | #include "recpacketcache.hh" |
3ddb9247 | 33 | #include "utility.hh" |
51e2144e | 34 | #include "dns_random.hh" |
d1b28475 KM |
35 | #ifdef HAVE_LIBSODIUM |
36 | #include <sodium.h> | |
37 | #endif | |
3afde9b2 | 38 | #include "opensslsigners.hh" |
288f4aa9 BH |
39 | #include <iostream> |
40 | #include <errno.h> | |
81859ba5 | 41 | #include <boost/static_assert.hpp> |
288f4aa9 BH |
42 | #include <map> |
43 | #include <set> | |
97bb160b | 44 | #include "recursor_cache.hh" |
38c9ceaa | 45 | #include "cachecleaner.hh" |
288f4aa9 | 46 | #include <stdio.h> |
c75a6a9e | 47 | #include <signal.h> |
288f4aa9 | 48 | #include <stdlib.h> |
bb4bdbaf | 49 | #include "misc.hh" |
288f4aa9 BH |
50 | #include "mtasker.hh" |
51 | #include <utility> | |
288f4aa9 BH |
52 | #include "arguments.hh" |
53 | #include "syncres.hh" | |
88def049 BH |
54 | #include <fcntl.h> |
55 | #include <fstream> | |
3e61e7f7 | 56 | #include "sortlist.hh" |
57 | extern SortList g_sortlist; | |
5c633640 BH |
58 | #include "sstuff.hh" |
59 | #include <boost/tuple/tuple.hpp> | |
60 | #include <boost/tuple/tuple_comparison.hpp> | |
72df400f | 61 | #include <boost/shared_array.hpp> |
7f1fa77d | 62 | #include <boost/function.hpp> |
5605c067 | 63 | #include <boost/algorithm/string.hpp> |
8f7473d7 | 64 | #ifdef MALLOC_TRACE |
65 | #include "malloctrace.hh" | |
66 | #endif | |
40a3dd64 | 67 | #include <netinet/tcp.h> |
ea634573 BH |
68 | #include "dnsparser.hh" |
69 | #include "dnswriter.hh" | |
70 | #include "dnsrecords.hh" | |
f814d7c8 | 71 | #include "zoneparser-tng.hh" |
1d5b3ce6 | 72 | #include "rec_channel.hh" |
aaacf7f2 | 73 | #include "logger.hh" |
c8ddb7c2 | 74 | #include "iputils.hh" |
09e6702a | 75 | #include "mplexer.hh" |
c038218b | 76 | #include "config.h" |
808c5ef7 | 77 | #include "lua-recursor4.hh" |
ba1a571d | 78 | #include "version.hh" |
79332bff | 79 | #include "responsestats.hh" |
d67620e4 | 80 | #include "secpoll-recursor.hh" |
c5c066bf | 81 | #include "dnsname.hh" |
644dd1da | 82 | #include "filterpo.hh" |
83 | #include "rpzloader.hh" | |
b3f0ed10 | 84 | #include "validate-recursor.hh" |
f3c18728 | 85 | #include "rec-lua-conf.hh" |
5c3b5e7f | 86 | #include "ednsoptions.hh" |
85c7ca75 | 87 | #include "gettime.hh" |
f3c18728 | 88 | |
d9d3f9c1 | 89 | #include "rec-protobuf.hh" |
d705aad9 | 90 | #include "rec-snmp.hh" |
aa7929a3 | 91 | |
6b6720de PL |
92 | #ifdef HAVE_SYSTEMD |
93 | #include <systemd/sd-daemon.h> | |
94 | #endif | |
95 | ||
bb4bdbaf | 96 | __thread FDMultiplexer* t_fdm; |
b4015453 | 97 | static __thread unsigned int t_id; |
09e6702a | 98 | unsigned int g_maxTCPPerClient; |
fde296a3 | 99 | size_t g_tcpMaxQueriesPerConn; |
5b0ddd18 | 100 | unsigned int g_networkTimeoutMsec; |
08f3f638 | 101 | uint64_t g_latencyStatSize; |
09e6702a | 102 | bool g_logCommonErrors; |
e661a20b | 103 | bool g_anyToTcp; |
b33c2462 | 104 | uint16_t g_udpTruncationThreshold, g_outgoingEDNSBufsize; |
a3e7b735 | 105 | __thread shared_ptr<RecursorLua4>* t_pdl; |
b3adda56 | 106 | bool g_lowercaseOutgoing; |
60c8afa8 | 107 | |
108 | __thread addrringbuf_t* t_remotes, *t_servfailremotes, *t_largeanswerremotes; | |
109 | ||
c5c066bf | 110 | __thread boost::circular_buffer<pair<DNSName, uint16_t> >* t_queryring, *t_servfailqueryring; |
77499b05 | 111 | __thread shared_ptr<Regex>* t_traceRegex; |
674cf0f6 | 112 | |
aa7929a3 RG |
113 | #ifdef HAVE_PROTOBUF |
114 | __thread boost::uuids::random_generator* t_uuidGenerator; | |
115 | #endif | |
116 | ||
376effcf | 117 | |
d7dae798 BH |
118 | RecursorControlChannel s_rcc; // only active in thread 0 |
119 | ||
120 | // for communicating with our threads | |
49a699c4 BH |
121 | struct ThreadPipeSet |
122 | { | |
123 | int writeToThread; | |
124 | int readToThread; | |
125 | int writeFromThread; | |
126 | int readFromThread; | |
127 | }; | |
3ea54bf0 | 128 | |
d7dae798 | 129 | vector<ThreadPipeSet> g_pipes; // effectively readonly after startup |
5c633640 | 130 | |
d7dae798 | 131 | SyncRes::domainmap_t* g_initialDomainMap; // new threads needs this to be setup |
49a699c4 BH |
132 | |
133 | #include "namespaces.hh" | |
3ea54bf0 | 134 | |
49a699c4 | 135 | __thread MemRecursorCache* t_RC; |
16beeaa4 | 136 | __thread RecursorPacketCache* t_packetCache; |
1d5b3ce6 BH |
137 | RecursorStats g_stats; |
138 | bool g_quiet; | |
49a699c4 | 139 | |
1bc3c142 BH |
140 | bool g_weDistributeQueries; // if true, only 1 thread listens on the incoming query sockets |
141 | ||
41942bb3 | 142 | __thread NetmaskGroup* t_allowFrom; |
49a699c4 BH |
143 | static NetmaskGroup* g_initialAllowFrom; // new thread needs to be setup with this |
144 | ||
eb5bae86 | 145 | NetmaskGroup* g_dontQuery; |
2d733c0f | 146 | string s_programname="pdns_recursor"; |
49a699c4 | 147 | |
40a3dd64 BH |
148 | typedef vector<int> tcpListenSockets_t; |
149 | tcpListenSockets_t g_tcpListenSockets; // shared across threads, but this is fine, never written to from a thread. All threads listen on all sockets | |
3159c9ef | 150 | int g_tcpTimeout; |
85c32340 | 151 | unsigned int g_maxMThreads; |
183eb877 | 152 | __thread struct timeval g_now; // timestamp, updated (too) frequently |
84433b79 | 153 | typedef map<int, ComboAddress> listenSocketsAddresses_t; // is shared across all threads right now |
154 | listenSocketsAddresses_t g_listenSocketsAddresses; // is shared across all threads right now | |
cbc03320 | 155 | set<int> g_fromtosockets; // listen sockets that use 'sendfromto()' mechanism |
18af64a8 | 156 | |
d7dae798 BH |
157 | __thread MT_t* MT; // the big MTasker |
158 | ||
f8f243b0 | 159 | unsigned int g_numThreads, g_numWorkerThreads; |
c3828c03 | 160 | |
12cd44ee | 161 | #define LOCAL_NETS "127.0.0.0/8, 10.0.0.0/8, 100.64.0.0/10, 169.254.0.0/16, 192.168.0.0/16, 172.16.0.0/12, ::1/128, fc00::/7, fe80::/10" |
162 | // Bad Nets taken from both: | |
3ddb9247 | 163 | // http://www.iana.org/assignments/iana-ipv4-special-registry/iana-ipv4-special-registry.xhtml |
12cd44ee | 164 | // and |
165 | // http://www.iana.org/assignments/iana-ipv6-special-registry/iana-ipv6-special-registry.xhtml | |
166 | // where such a network may not be considered a valid destination | |
167 | #define BAD_NETS "0.0.0.0/8, 192.0.0.0/24, 192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24, 240.0.0.0/4, ::/96, ::ffff:0:0/96, 100::/64, 2001:db8::/32" | |
168 | #define DONT_QUERY LOCAL_NETS ", " BAD_NETS | |
49a699c4 | 169 | |
d7dae798 | 170 | //! used to send information to a newborn mthread |
ea634573 | 171 | struct DNSComboWriter { |
27c0050c | 172 | DNSComboWriter(const char* data, uint16_t len, const struct timeval& now) : d_mdp(true, data, len), d_now(now), |
232f0877 | 173 | d_tcp(false), d_socket(-1) |
ea634573 BH |
174 | {} |
175 | MOADNSParser d_mdp; | |
00c9b8c1 | 176 | void setRemote(const ComboAddress* sa) |
ea634573 | 177 | { |
37d3f960 | 178 | d_remote=*sa; |
ea634573 BH |
179 | } |
180 | ||
b71b60ee | 181 | void setLocal(const ComboAddress& sa) |
182 | { | |
183 | d_local=sa; | |
184 | } | |
185 | ||
186 | ||
ea634573 BH |
187 | void setSocket(int sock) |
188 | { | |
189 | d_socket=sock; | |
190 | } | |
a1754c6a BH |
191 | |
192 | string getRemote() const | |
193 | { | |
37d3f960 | 194 | return d_remote.toString(); |
a1754c6a BH |
195 | } |
196 | ||
c9e9e5e0 | 197 | struct timeval d_now; |
b71b60ee | 198 | ComboAddress d_remote, d_local; |
aa7929a3 RG |
199 | #ifdef HAVE_PROTOBUF |
200 | boost::uuids::uuid d_uuid; | |
201 | #endif | |
b40562da RG |
202 | EDNSSubnetOpts d_ednssubnet; |
203 | bool d_ecsFound{false}; | |
204 | bool d_ecsParsed{false}; | |
ea634573 BH |
205 | bool d_tcp; |
206 | int d_socket; | |
a82f68f0 | 207 | int d_tag{0}; |
49a3500d | 208 | string d_query; |
cd989c87 | 209 | shared_ptr<TCPConnection> d_tcpConnection; |
e8340d27 | 210 | vector<pair<uint16_t, string> > d_ednsOpts; |
02b47f43 | 211 | std::vector<std::string> d_policyTags; |
05c74122 | 212 | std::unordered_map<string,string> d_data; |
ea634573 BH |
213 | }; |
214 | ||
215 | ||
288f4aa9 BH |
216 | ArgvMap &arg() |
217 | { | |
218 | static ArgvMap theArg; | |
219 | return theArg; | |
220 | } | |
4ef015cd | 221 | |
b4015453 RG |
222 | unsigned int getRecursorThreadId() |
223 | { | |
224 | return t_id; | |
225 | } | |
09e6702a | 226 | |
d8f6d49f | 227 | void handleTCPClientWritable(int fd, FDMultiplexer::funcparam_t& var); |
09e6702a | 228 | |
50c81227 | 229 | // -1 is error, 0 is timeout, 1 is success |
3ddb9247 | 230 | int asendtcp(const string& data, Socket* sock) |
5c633640 BH |
231 | { |
232 | PacketID pident; | |
233 | pident.sock=sock; | |
234 | pident.outMSG=data; | |
3ddb9247 | 235 | |
bb4bdbaf | 236 | t_fdm->addWriteFD(sock->getHandle(), handleTCPClientWritable, pident); |
50c81227 | 237 | string packet; |
5c633640 | 238 | |
5b0ddd18 | 239 | int ret=MT->waitEvent(pident, &packet, g_networkTimeoutMsec); |
23db0a09 | 240 | |
9170fbaf | 241 | if(!ret || ret==-1) { // timeout |
bb4bdbaf | 242 | t_fdm->removeWriteFD(sock->getHandle()); |
5c633640 | 243 | } |
50c81227 BH |
244 | else if(packet.size() !=data.size()) { // main loop tells us what it sent out, or empty in case of an error |
245 | return -1; | |
246 | } | |
9170fbaf | 247 | return ret; |
5c633640 BH |
248 | } |
249 | ||
d8f6d49f | 250 | void handleTCPClientReadable(int fd, FDMultiplexer::funcparam_t& var); |
09e6702a | 251 | |
9170fbaf | 252 | // -1 is error, 0 is timeout, 1 is success |
a683e8bd | 253 | int arecvtcp(string& data, size_t len, Socket* sock, bool incompleteOkay) |
288f4aa9 | 254 | { |
50c81227 | 255 | data.clear(); |
5c633640 BH |
256 | PacketID pident; |
257 | pident.sock=sock; | |
258 | pident.inNeeded=len; | |
825fa717 | 259 | pident.inIncompleteOkay=incompleteOkay; |
bb4bdbaf | 260 | t_fdm->addReadFD(sock->getHandle(), handleTCPClientReadable, pident); |
5c633640 | 261 | |
bb4bdbaf | 262 | int ret=MT->waitEvent(pident,&data, g_networkTimeoutMsec); |
9170fbaf | 263 | if(!ret || ret==-1) { // timeout |
bb4bdbaf | 264 | t_fdm->removeReadFD(sock->getHandle()); |
288f4aa9 | 265 | } |
50c81227 BH |
266 | else if(data.empty()) {// error, EOF or other |
267 | return -1; | |
268 | } | |
269 | ||
9170fbaf | 270 | return ret; |
288f4aa9 BH |
271 | } |
272 | ||
fba1e944 | 273 | void handleGenUDPQueryResponse(int fd, FDMultiplexer::funcparam_t& var) |
4465e941 | 274 | { |
fba1e944 | 275 | PacketID pident=*any_cast<PacketID>(&var); |
4465e941 | 276 | char resp[512]; |
a683e8bd | 277 | ssize_t ret=recv(fd, resp, sizeof(resp), 0); |
4465e941 | 278 | t_fdm->removeReadFD(fd); |
279 | if(ret >= 0) { | |
a683e8bd | 280 | string data(resp, (size_t) ret); |
fba1e944 | 281 | MT->sendEvent(pident, &data); |
4465e941 | 282 | } |
283 | else { | |
fba1e944 | 284 | string empty; |
285 | MT->sendEvent(pident, &empty); | |
286 | // cerr<<"Had some kind of error: "<<ret<<", "<<strerror(errno)<<endl; | |
4465e941 | 287 | } |
288 | } | |
fba1e944 | 289 | string GenUDPQueryResponse(const ComboAddress& dest, const string& query) |
4465e941 | 290 | { |
4465e941 | 291 | Socket s(dest.sin4.sin_family, SOCK_DGRAM); |
292 | s.setNonBlocking(); | |
293 | ComboAddress local = getQueryLocalAddress(dest.sin4.sin_family, 0); | |
294 | ||
295 | s.bind(local); | |
296 | s.connect(dest); | |
4465e941 | 297 | s.send(query); |
298 | ||
299 | PacketID pident; | |
300 | pident.sock=&s; | |
301 | pident.type=0; | |
fba1e944 | 302 | t_fdm->addReadFD(s.getHandle(), handleGenUDPQueryResponse, pident); |
4465e941 | 303 | |
304 | string data; | |
fba1e944 | 305 | |
4465e941 | 306 | int ret=MT->waitEvent(pident,&data, g_networkTimeoutMsec); |
fba1e944 | 307 | |
4465e941 | 308 | if(!ret || ret==-1) { // timeout |
4465e941 | 309 | t_fdm->removeReadFD(s.getHandle()); |
310 | } | |
311 | else if(data.empty()) {// error, EOF or other | |
fba1e944 | 312 | // we could special case this |
4465e941 | 313 | return data; |
314 | } | |
4465e941 | 315 | return data; |
316 | } | |
317 | ||
318 | ||
3ddb9247 | 319 | vector<ComboAddress> g_localQueryAddresses4, g_localQueryAddresses6; |
046c5a5d | 320 | const ComboAddress g_local4("0.0.0.0"), g_local6("::"); |
1652a63e | 321 | |
d7dae798 | 322 | //! pick a random query local address |
1652a63e | 323 | ComboAddress getQueryLocalAddress(int family, uint16_t port) |
5a38281c | 324 | { |
1652a63e | 325 | ComboAddress ret; |
5a38281c | 326 | if(family==AF_INET) { |
3ddb9247 | 327 | if(g_localQueryAddresses4.empty()) |
1652a63e | 328 | ret = g_local4; |
3ddb9247 | 329 | else |
1652a63e BH |
330 | ret = g_localQueryAddresses4[dns_random(g_localQueryAddresses4.size())]; |
331 | ret.sin4.sin_port = htons(port); | |
5a38281c BH |
332 | } |
333 | else { | |
334 | if(g_localQueryAddresses6.empty()) | |
1652a63e BH |
335 | ret = g_local6; |
336 | else | |
337 | ret = g_localQueryAddresses6[dns_random(g_localQueryAddresses6.size())]; | |
3ddb9247 | 338 | |
1652a63e | 339 | ret.sin6.sin6_port = htons(port); |
5a38281c | 340 | } |
1652a63e | 341 | return ret; |
5a38281c | 342 | } |
4ef015cd | 343 | |
d8f6d49f | 344 | void handleUDPServerResponse(int fd, FDMultiplexer::funcparam_t&); |
09e6702a | 345 | |
d7dae798 BH |
346 | void setSocketBuffer(int fd, int optname, uint32_t size) |
347 | { | |
348 | uint32_t psize=0; | |
349 | socklen_t len=sizeof(psize); | |
3ddb9247 | 350 | |
d7dae798 BH |
351 | if(!getsockopt(fd, SOL_SOCKET, optname, (char*)&psize, &len) && psize > size) { |
352 | L<<Logger::Error<<"Not decreasing socket buffer size from "<<psize<<" to "<<size<<endl; | |
3ddb9247 | 353 | return; |
d7dae798 BH |
354 | } |
355 | ||
356 | if (setsockopt(fd, SOL_SOCKET, optname, (char*)&size, sizeof(size)) < 0 ) | |
c057bfaa | 357 | L<<Logger::Error<<"Unable to raise socket buffer size to "<<size<<": "<<strerror(errno)<<endl; |
d7dae798 BH |
358 | } |
359 | ||
360 | ||
361 | static void setSocketReceiveBuffer(int fd, uint32_t size) | |
362 | { | |
363 | setSocketBuffer(fd, SO_RCVBUF, size); | |
364 | } | |
365 | ||
366 | static void setSocketSendBuffer(int fd, uint32_t size) | |
367 | { | |
368 | setSocketBuffer(fd, SO_SNDBUF, size); | |
369 | } | |
370 | ||
371 | ||
4ef015cd BH |
372 | // you can ask this class for a UDP socket to send a query from |
373 | // this socket is not yours, don't even think about deleting it | |
374 | // but after you call 'returnSocket' on it, don't assume anything anymore | |
375 | class UDPClientSocks | |
376 | { | |
4ef015cd | 377 | unsigned int d_numsocks; |
4ef015cd | 378 | public: |
e2642526 | 379 | UDPClientSocks() : d_numsocks(0) |
4ef015cd BH |
380 | { |
381 | } | |
382 | ||
996c89cc | 383 | typedef set<int> socks_t; |
4ef015cd BH |
384 | socks_t d_socks; |
385 | ||
2ee280cf | 386 | // returning -2 means: temporary OS error (ie, out of files), -1 means error related to remote |
d8f6d49f | 387 | int getSocket(const ComboAddress& toaddr, int* fd) |
4ef015cd | 388 | { |
d8f6d49f BH |
389 | *fd=makeClientSocket(toaddr.sin4.sin_family); |
390 | if(*fd < 0) // temporary error - receive exception otherwise | |
2ee280cf | 391 | return -2; |
d8f6d49f BH |
392 | |
393 | if(connect(*fd, (struct sockaddr*)(&toaddr), toaddr.getSocklen()) < 0) { | |
394 | int err = errno; | |
41ff43f8 | 395 | // returnSocket(*fd); |
a7b68ae7 RG |
396 | try { |
397 | closesocket(*fd); | |
398 | } | |
399 | catch(const PDNSException& e) { | |
400 | L<<Logger::Error<<"Error closing UDP socket after connect() failed: "<<e.reason<<endl; | |
401 | } | |
402 | ||
d8f6d49f | 403 | if(err==ENETUNREACH) // Seth "My Interfaces Are Like A Yo Yo" Arnold special |
4957a608 | 404 | return -2; |
998a4334 | 405 | return -1; |
d8f6d49f | 406 | } |
998a4334 | 407 | |
d8f6d49f | 408 | d_socks.insert(*fd); |
998a4334 | 409 | d_numsocks++; |
d8f6d49f | 410 | return 0; |
4ef015cd BH |
411 | } |
412 | ||
095c3045 BH |
413 | void returnSocket(int fd) |
414 | { | |
415 | socks_t::iterator i=d_socks.find(fd); | |
34801ab1 | 416 | if(i==d_socks.end()) { |
335da0ba | 417 | throw PDNSException("Trying to return a socket (fd="+std::to_string(fd)+") not in the pool"); |
34801ab1 | 418 | } |
bb4bdbaf | 419 | returnSocketLocked(i); |
095c3045 BH |
420 | } |
421 | ||
4ef015cd | 422 | // return a socket to the pool, or simply erase it |
bb4bdbaf | 423 | void returnSocketLocked(socks_t::iterator& i) |
4ef015cd | 424 | { |
600fc20b | 425 | if(i==d_socks.end()) { |
3f81d239 | 426 | throw PDNSException("Trying to return a socket not in the pool"); |
600fc20b | 427 | } |
80baf329 | 428 | try { |
bb4bdbaf | 429 | t_fdm->removeReadFD(*i); |
80baf329 BH |
430 | } |
431 | catch(FDMultiplexerException& e) { | |
bb4bdbaf | 432 | // we sometimes return a socket that has not yet been assigned to t_fdm |
80baf329 | 433 | } |
a7b68ae7 RG |
434 | try { |
435 | closesocket(*i); | |
436 | } | |
437 | catch(const PDNSException& e) { | |
438 | L<<Logger::Error<<"Error closing returned UDP socket: "<<e.reason<<endl; | |
439 | } | |
3ddb9247 | 440 | |
998a4334 BH |
441 | d_socks.erase(i++); |
442 | --d_numsocks; | |
4ef015cd | 443 | } |
d8f6d49f BH |
444 | |
445 | // returns -1 for errors which might go away, throws for ones that won't | |
bb4bdbaf | 446 | static int makeClientSocket(int family) |
d8f6d49f | 447 | { |
a683e8bd | 448 | int ret=socket(family, SOCK_DGRAM, 0 ); // turns out that setting CLO_EXEC and NONBLOCK from here is not a performance win on Linux (oddly enough) |
42c235e5 | 449 | |
d8f6d49f BH |
450 | if(ret < 0 && errno==EMFILE) // this is not a catastrophic error |
451 | return ret; | |
3ddb9247 PD |
452 | |
453 | if(ret<0) | |
335da0ba | 454 | throw PDNSException("Making a socket for resolver (family = "+std::to_string(family)+"): "+stringerror()); |
36855b53 | 455 | |
7eb73ffa | 456 | // setCloseOnExec(ret); // we're not going to exec |
5a38281c | 457 | |
d8f6d49f | 458 | int tries=10; |
3aa91c3e | 459 | ComboAddress sin; |
d8f6d49f | 460 | while(--tries) { |
1652a63e | 461 | uint16_t port; |
3ddb9247 | 462 | |
d8f6d49f | 463 | if(tries==1) // fall back to kernel 'random' |
4957a608 | 464 | port = 0; |
1652a63e BH |
465 | else |
466 | port = 1025 + dns_random(64510); | |
5a38281c | 467 | |
3aa91c3e | 468 | sin=getQueryLocalAddress(family, port); // does htons for us |
5a38281c | 469 | |
3ddb9247 | 470 | if (::bind(ret, (struct sockaddr *)&sin, sin.getSocklen()) >= 0) |
4957a608 | 471 | break; |
d8f6d49f BH |
472 | } |
473 | if(!tries) | |
3aa91c3e | 474 | throw PDNSException("Resolver binding to local query client socket on "+sin.toString()+": "+stringerror()); |
3ddb9247 | 475 | |
3897b9e1 | 476 | setNonBlocking(ret); |
d8f6d49f BH |
477 | return ret; |
478 | } | |
49a699c4 BH |
479 | }; |
480 | ||
481 | static __thread UDPClientSocks* t_udpclientsocks; | |
4ef015cd | 482 | |
288f4aa9 | 483 | /* these two functions are used by LWRes */ |
34801ab1 | 484 | // -2 is OS error, -1 is error that depends on the remote, > 0 is success |
a683e8bd | 485 | int asendto(const char *data, size_t len, int flags, |
3ddb9247 | 486 | const ComboAddress& toaddr, uint16_t id, const DNSName& domain, uint16_t qtype, int* fd) |
288f4aa9 | 487 | { |
34801ab1 BH |
488 | |
489 | PacketID pident; | |
787e5eab BH |
490 | pident.domain = domain; |
491 | pident.remote = toaddr; | |
492 | pident.type = qtype; | |
34801ab1 BH |
493 | |
494 | // see if there is an existing outstanding request we can chain on to, using partial equivalence function | |
495 | pair<MT_t::waiters_t::iterator, MT_t::waiters_t::iterator> chain=MT->d_waiters.equal_range(pident, PacketIDBirthdayCompare()); | |
496 | ||
497 | for(; chain.first != chain.second; chain.first++) { | |
498 | if(chain.first->key.fd > -1) { // don't chain onto existing chained waiter! | |
e27e91a8 | 499 | /* |
4665c31e BH |
500 | cerr<<"Orig: "<<pident.domain<<", "<<pident.remote.toString()<<", id="<<id<<endl; |
501 | cerr<<"Had hit: "<< chain.first->key.domain<<", "<<chain.first->key.remote.toString()<<", id="<<chain.first->key.id | |
4957a608 | 502 | <<", count="<<chain.first->key.chain.size()<<", origfd: "<<chain.first->key.fd<<endl; |
e27e91a8 | 503 | */ |
34801ab1 BH |
504 | chain.first->key.chain.insert(id); // we can chain |
505 | *fd=-1; // gets used in waitEvent / sendEvent later on | |
506 | return 1; | |
507 | } | |
508 | } | |
509 | ||
49a699c4 | 510 | int ret=t_udpclientsocks->getSocket(toaddr, fd); |
d8f6d49f BH |
511 | if(ret < 0) |
512 | return ret; | |
34801ab1 | 513 | |
998a4334 BH |
514 | pident.fd=*fd; |
515 | pident.id=id; | |
3ddb9247 | 516 | |
bb4bdbaf BH |
517 | t_fdm->addReadFD(*fd, handleUDPServerResponse, pident); |
518 | ret = send(*fd, data, len, 0); | |
519 | ||
5b0ddd18 | 520 | int tmp = errno; |
bb4bdbaf | 521 | |
7302ed0a | 522 | if(ret < 0) |
49a699c4 | 523 | t_udpclientsocks->returnSocket(*fd); |
bb4bdbaf | 524 | |
5b0ddd18 | 525 | errno = tmp; // this is for logging purposes only |
7302ed0a | 526 | return ret; |
288f4aa9 BH |
527 | } |
528 | ||
9170fbaf | 529 | // -1 is error, 0 is timeout, 1 is success |
a683e8bd | 530 | int arecvfrom(char *data, size_t len, int flags, const ComboAddress& fromaddr, size_t *d_len, |
c5c066bf | 531 | uint16_t id, const DNSName& domain, uint16_t qtype, int fd, struct timeval* now) |
288f4aa9 | 532 | { |
0d5f0a9f | 533 | static optional<unsigned int> nearMissLimit; |
3ddb9247 | 534 | if(!nearMissLimit) |
0d5f0a9f BH |
535 | nearMissLimit=::arg().asNum("spoof-nearmiss-max"); |
536 | ||
288f4aa9 | 537 | PacketID pident; |
4ef015cd | 538 | pident.fd=fd; |
288f4aa9 | 539 | pident.id=id; |
0d5f0a9f | 540 | pident.domain=domain; |
787e5eab | 541 | pident.type = qtype; |
996c89cc | 542 | pident.remote=fromaddr; |
b636533b | 543 | |
288f4aa9 | 544 | string packet; |
5b0ddd18 | 545 | int ret=MT->waitEvent(pident, &packet, g_networkTimeoutMsec, now); |
34801ab1 | 546 | |
9170fbaf | 547 | if(ret > 0) { |
996c89cc | 548 | if(packet.empty()) // means "error" |
3ddb9247 | 549 | return -1; |
998a4334 | 550 | |
a683e8bd | 551 | *d_len=packet.size(); |
9170fbaf | 552 | memcpy(data,packet.c_str(),min(len,*d_len)); |
0d5f0a9f | 553 | if(*nearMissLimit && pident.nearMisses > *nearMissLimit) { |
996c89cc | 554 | L<<Logger::Error<<"Too many ("<<pident.nearMisses<<" > "<<*nearMissLimit<<") bogus answers for '"<<domain<<"' from "<<fromaddr.toString()<<", assuming spoof attempt."<<endl; |
0d5f0a9f | 555 | g_stats.spoofCount++; |
35ce8576 BH |
556 | return -1; |
557 | } | |
288f4aa9 | 558 | } |
09e6702a | 559 | else { |
34801ab1 | 560 | if(fd >= 0) |
49a699c4 | 561 | t_udpclientsocks->returnSocket(fd); |
09e6702a | 562 | } |
9170fbaf | 563 | return ret; |
288f4aa9 BH |
564 | } |
565 | ||
aa4e4cbf | 566 | |
87a5ea63 | 567 | string s_pidfname; |
88def049 BH |
568 | static void writePid(void) |
569 | { | |
191f2e47 | 570 | if(!::arg().mustDo("write-pid")) |
571 | return; | |
18e7758c | 572 | ofstream of(s_pidfname.c_str(), std::ios_base::app); |
88def049 | 573 | if(of) |
705f31ae | 574 | of<< Utility::getpid() <<endl; |
88def049 | 575 | else |
c057bfaa | 576 | L<<Logger::Error<<"Writing pid for "<<Utility::getpid()<<" to "<<s_pidfname<<" failed: "<<strerror(errno)<<endl; |
88def049 BH |
577 | } |
578 | ||
bd0289fc BH |
579 | typedef map<ComboAddress, uint32_t, ComboAddress::addressOnlyLessThan> tcpClientCounts_t; |
580 | tcpClientCounts_t __thread* t_tcpClientCounts; | |
0e9d9ce2 | 581 | |
cd989c87 | 582 | TCPConnection::TCPConnection(int fd, const ComboAddress& addr) : d_remote(addr), d_fd(fd) |
3ddb9247 PD |
583 | { |
584 | ++s_currentConnections; | |
cd989c87 | 585 | (*t_tcpClientCounts)[d_remote]++; |
0e408828 | 586 | } |
cd989c87 BH |
587 | |
588 | TCPConnection::~TCPConnection() | |
0e408828 | 589 | { |
a7b68ae7 RG |
590 | try { |
591 | if(closesocket(d_fd) < 0) | |
592 | L<<Logger::Error<<"Error closing socket for TCPConnection"<<endl; | |
593 | } | |
594 | catch(const PDNSException& e) { | |
595 | L<<Logger::Error<<"Error closing TCPConnection socket: "<<e.reason<<endl; | |
596 | } | |
597 | ||
3ddb9247 | 598 | if(t_tcpClientCounts->count(d_remote) && !(*t_tcpClientCounts)[d_remote]--) |
cd989c87 | 599 | t_tcpClientCounts->erase(d_remote); |
1bc9e6bd | 600 | --s_currentConnections; |
0e408828 | 601 | } |
0e9d9ce2 | 602 | |
3ddb9247 | 603 | AtomicCounter TCPConnection::s_currentConnections; |
d8f6d49f | 604 | void handleRunningTCPQuestion(int fd, FDMultiplexer::funcparam_t& var); |
6dcd28c3 | 605 | |
92011b8f | 606 | // the idea is, only do things that depend on the *response* here. Incoming accounting is on incoming. |
c5c066bf | 607 | void updateResponseStats(int res, const ComboAddress& remote, unsigned int packetsize, const DNSName* query, uint16_t qtype) |
2cc13433 | 608 | { |
92011b8f | 609 | if(packetsize > 1000 && t_largeanswerremotes) |
610 | t_largeanswerremotes->push_back(remote); | |
2cc13433 BH |
611 | switch(res) { |
612 | case RCode::ServFail: | |
92011b8f | 613 | if(t_servfailremotes) { |
614 | t_servfailremotes->push_back(remote); | |
5af86fdc | 615 | if(query && t_servfailqueryring) // packet cache |
92011b8f | 616 | t_servfailqueryring->push_back(make_pair(*query, qtype)); |
617 | } | |
2cc13433 BH |
618 | g_stats.servFails++; |
619 | break; | |
620 | case RCode::NXDomain: | |
621 | g_stats.nxDomains++; | |
622 | break; | |
623 | case RCode::NoError: | |
624 | g_stats.noErrors++; | |
625 | break; | |
626 | } | |
627 | } | |
628 | ||
a903b39c | 629 | static string makeLoginfo(DNSComboWriter* dc) |
630 | try | |
631 | { | |
5ad5bb7d | 632 | return "("+dc->d_mdp.d_qname.toLogString()+"/"+DNSRecordContent::NumberToType(dc->d_mdp.d_qtype)+" from "+(dc->d_remote.toString())+")"; |
a903b39c | 633 | } |
634 | catch(...) | |
635 | { | |
636 | return "Exception making error message for exception"; | |
637 | } | |
638 | ||
aa7929a3 | 639 | #ifdef HAVE_PROTOBUF |
07ebe7c6 | 640 | static void protobufLogQuery(const std::shared_ptr<RemoteLogger>& logger, uint8_t maskV4, uint8_t maskV6, const boost::uuids::uuid& uniqueId, const ComboAddress& remote, const ComboAddress& local, const Netmask& ednssubnet, bool tcp, uint16_t id, size_t len, const DNSName& qname, uint16_t qtype, uint16_t qclass, const std::vector<std::string>& policyTags) |
aa7929a3 | 641 | { |
e1c8a4bb RG |
642 | Netmask requestorNM(remote, remote.sin4.sin_family == AF_INET ? maskV4 : maskV6); |
643 | const ComboAddress& requestor = requestorNM.getMaskedNetwork(); | |
644 | RecProtoBufMessage message(DNSProtoBufMessage::Query, uniqueId, &requestor, &local, qname, qtype, qclass, id, tcp, len); | |
a94bc5d7 | 645 | message.setEDNSSubnet(ednssubnet, ednssubnet.isIpv4() ? maskV4 : maskV6); |
02b47f43 | 646 | |
02b47f43 | 647 | if (!policyTags.empty()) { |
d9d3f9c1 | 648 | message.setPolicyTags(policyTags); |
02b47f43 | 649 | } |
aa7929a3 | 650 | |
d9d3f9c1 | 651 | // cerr <<message.toDebugString()<<endl; |
aa7929a3 | 652 | std::string str; |
d9d3f9c1 | 653 | message.serialize(str); |
aa7929a3 | 654 | logger->queueData(str); |
aa7929a3 RG |
655 | } |
656 | ||
d9d3f9c1 | 657 | static void protobufLogResponse(const std::shared_ptr<RemoteLogger>& logger, const RecProtoBufMessage& message) |
aa7929a3 | 658 | { |
d9d3f9c1 | 659 | // cerr <<message.toDebugString()<<endl; |
aa7929a3 | 660 | std::string str; |
d9d3f9c1 | 661 | message.serialize(str); |
aa7929a3 | 662 | logger->queueData(str); |
aa7929a3 RG |
663 | } |
664 | #endif | |
665 | ||
53508135 PL |
666 | /** |
667 | * Chases the CNAME provided by the PolicyCustom RPZ policy. | |
668 | * | |
669 | * @param spoofed: The DNSRecord that was created by the policy, should already be added to ret | |
670 | * @param qtype: The QType of the original query | |
671 | * @param sr: A SyncRes | |
672 | * @param res: An integer that will contain the RCODE of the lookup we do | |
673 | * @param ret: A vector of DNSRecords where the result of the CNAME chase should be appended to | |
674 | */ | |
675 | void handleRPZCustom(const DNSRecord& spoofed, const QType& qtype, SyncRes& sr, int& res, vector<DNSRecord>& ret) | |
676 | { | |
677 | if (spoofed.d_type == QType::CNAME) { | |
678 | bool oldWantsRPZ = sr.d_wantsRPZ; | |
679 | sr.d_wantsRPZ = false; | |
680 | vector<DNSRecord> ans; | |
681 | res = sr.beginResolve(DNSName(spoofed.d_content->getZoneRepresentation()), qtype, 1, ans); | |
682 | for (const auto& rec : ans) { | |
683 | if(rec.d_place == DNSResourceRecord::ANSWER) { | |
684 | ret.push_back(rec); | |
685 | } | |
686 | } | |
687 | // Reset the RPZ state of the SyncRes | |
688 | sr.d_wantsRPZ = oldWantsRPZ; | |
689 | } | |
690 | } | |
691 | ||
288f4aa9 BH |
692 | void startDoResolve(void *p) |
693 | { | |
7b1469bb | 694 | DNSComboWriter* dc=(DNSComboWriter *)p; |
288f4aa9 | 695 | try { |
5af86fdc RG |
696 | if (t_queryring) |
697 | t_queryring->push_back(make_pair(dc->d_mdp.d_qname, dc->d_mdp.d_qtype)); | |
92011b8f | 698 | |
b18ace73 | 699 | uint32_t maxanswersize= dc->d_tcp ? 65535 : min((uint16_t) 512, g_udpTruncationThreshold); |
7f7b8d55 | 700 | EDNSOpts edo; |
8e079f3a | 701 | bool haveEDNS=false; |
702 | if(getEDNSOpts(dc->d_mdp, &edo)) { | |
703 | if(!dc->d_tcp) | |
704 | maxanswersize = min(edo.d_packetsize, g_udpTruncationThreshold); | |
e8340d27 | 705 | dc->d_ednsOpts = edo.d_options; |
8e079f3a | 706 | haveEDNS=true; |
b40562da RG |
707 | |
708 | if (g_useIncomingECS && !dc->d_ecsParsed) { | |
709 | for (const auto& o : edo.d_options) { | |
710 | if (o.first == EDNSOptionCode::ECS) { | |
711 | dc->d_ecsFound = getEDNSSubnetOptsFromString(o.second, &dc->d_ednssubnet); | |
712 | break; | |
713 | } | |
714 | } | |
715 | } | |
10321a98 | 716 | } |
b40562da RG |
717 | /* perhaps there was no EDNS or no ECS but by now we looked */ |
718 | dc->d_ecsParsed = true; | |
e325f20c | 719 | vector<DNSRecord> ret; |
ea634573 | 720 | vector<uint8_t> packet; |
b23b8614 | 721 | |
ad42489c | 722 | auto luaconfsLocal = g_luaconfs.getLocal(); |
b8470add PL |
723 | // Used to tell syncres later on if we should apply NSDNAME and NSIP RPZ triggers for this query |
724 | bool wantsRPZ(true); | |
d9d3f9c1 | 725 | RecProtoBufMessage pbMessage(RecProtoBufMessage::Response); |
aa7929a3 | 726 | #ifdef HAVE_PROTOBUF |
d9d3f9c1 | 727 | if (luaconfsLocal->protobufServer) { |
e1c8a4bb RG |
728 | Netmask requestorNM(dc->d_remote, dc->d_remote.sin4.sin_family == AF_INET ? luaconfsLocal->protobufMaskV4 : luaconfsLocal->protobufMaskV6); |
729 | const ComboAddress& requestor = requestorNM.getMaskedNetwork(); | |
730 | pbMessage.update(dc->d_uuid, &requestor, &dc->d_local, dc->d_tcp, dc->d_mdp.d_header.id); | |
b40562da | 731 | pbMessage.setEDNSSubnet(dc->d_ednssubnet.source, dc->d_ednssubnet.source.isIpv4() ? luaconfsLocal->protobufMaskV4 : luaconfsLocal->protobufMaskV6); |
d9d3f9c1 RG |
732 | pbMessage.setQuestion(dc->d_mdp.d_qname, dc->d_mdp.d_qtype, dc->d_mdp.d_qclass); |
733 | } | |
734 | #endif /* HAVE_PROTOBUF */ | |
ad42489c | 735 | |
3ddb9247 | 736 | DNSPacketWriter pw(packet, dc->d_mdp.d_qname, dc->d_mdp.d_qtype, dc->d_mdp.d_qclass); |
ea634573 BH |
737 | |
738 | pw.getHeader()->aa=0; | |
739 | pw.getHeader()->ra=1; | |
c154c8a4 | 740 | pw.getHeader()->qr=1; |
bb4bdbaf | 741 | pw.getHeader()->tc=0; |
ea634573 | 742 | pw.getHeader()->id=dc->d_mdp.d_header.id; |
10321a98 | 743 | pw.getHeader()->rd=dc->d_mdp.d_header.rd; |
57769f13 | 744 | pw.getHeader()->cd=dc->d_mdp.d_header.cd; |
ea634573 | 745 | |
1059837e | 746 | // DO NOT MOVE THIS CODE UP - DNSPacketWriter needs to get the original-cased version |
b3adda56 | 747 | if (g_lowercaseOutgoing) |
3ebc80ce | 748 | dc->d_mdp.d_qname = dc->d_mdp.d_qname.makeLowerCase(); |
b3adda56 | 749 | |
904d3219 PD |
750 | uint32_t minTTL=std::numeric_limits<uint32_t>::max(); |
751 | ||
752 | SyncRes sr(dc->d_now); | |
2e921ec6 | 753 | bool DNSSECOK=false; |
3457a2a0 | 754 | if(t_pdl) { |
755 | sr.setLuaEngine(*t_pdl); | |
4ea94941 | 756 | sr.d_requestor=dc->d_remote; |
3457a2a0 | 757 | } |
2e921ec6 | 758 | |
9eec8c98 | 759 | if(g_dnssecmode != DNSSECMode::Off) { |
2e921ec6 | 760 | sr.d_doDNSSEC=true; |
9eec8c98 PL |
761 | |
762 | // Does the requestor want DNSSEC records? | |
763 | if(edo.d_Z & EDNSOpts::DNSSECOK) { | |
764 | DNSSECOK=true; | |
765 | g_stats.dnssecQueries++; | |
766 | } | |
767 | } else { | |
768 | // Ignore the client-set CD flag | |
769 | pw.getHeader()->cd=0; | |
5b9853c9 | 770 | } |
4898a348 RG |
771 | #ifdef HAVE_PROTOBUF |
772 | sr.d_initialRequestId = dc->d_uuid; | |
773 | #endif | |
b40562da RG |
774 | if (g_useIncomingECS) { |
775 | sr.d_incomingECSFound = dc->d_ecsFound; | |
776 | if (dc->d_ecsFound) { | |
777 | sr.d_incomingECS = dc->d_ednssubnet; | |
778 | } | |
779 | } | |
57769f13 | 780 | |
904d3219 PD |
781 | bool tracedQuery=false; // we could consider letting Lua know about this too |
782 | bool variableAnswer = false; | |
9fc36e90 | 783 | bool shouldNotValidate = false; |
904d3219 | 784 | |
ef3b6cd7 RG |
785 | /* preresolve expects res (dq.rcode) to be set to RCode::NoError by default */ |
786 | int res = RCode::NoError; | |
1f1ca368 | 787 | DNSFilterEngine::Policy appliedPolicy; |
39ec5d29 | 788 | DNSRecord spoofed; |
6e505c5e RG |
789 | RecursorLua4::DNSQuestion dq(dc->d_remote, dc->d_local, dc->d_mdp.d_qname, dc->d_mdp.d_qtype, dc->d_tcp, variableAnswer, wantsRPZ); |
790 | dq.ednsFlags = &edo.d_Z; | |
791 | dq.ednsOptions = &dc->d_ednsOpts; | |
792 | dq.tag = dc->d_tag; | |
793 | dq.discardedPolicies = &sr.d_discardedPolicies; | |
794 | dq.policyTags = &dc->d_policyTags; | |
795 | dq.appliedPolicy = &appliedPolicy; | |
796 | dq.currentRecords = &ret; | |
797 | dq.dh = &dc->d_mdp.d_header; | |
05c74122 | 798 | dq.data = dc->d_data; |
ba21fcfe | 799 | |
e661a20b | 800 | if(dc->d_mdp.d_qtype==QType::ANY && !dc->d_tcp && g_anyToTcp) { |
56b4d21b PD |
801 | pw.getHeader()->tc = 1; |
802 | res = 0; | |
803 | variableAnswer = true; | |
e661a20b PD |
804 | goto sendit; |
805 | } | |
806 | ||
c5c066bf | 807 | if(t_traceRegex->get() && (*t_traceRegex)->match(dc->d_mdp.d_qname.toString())) { |
77499b05 BH |
808 | sr.setLogMode(SyncRes::Store); |
809 | tracedQuery=true; | |
810 | } | |
3ddb9247 | 811 | |
8f7473d7 | 812 | |
976ec823 | 813 | if(!g_quiet || tracedQuery) { |
461df9d2 | 814 | L<<Logger::Warning<<t_id<<" ["<<MT->getTid()<<"/"<<MT->numProcesses()<<"] " << (dc->d_tcp ? "TCP " : "") << "question for '"<<dc->d_mdp.d_qname<<"|" |
976ec823 | 815 | <<DNSRecordContent::NumberToType(dc->d_mdp.d_qtype)<<"' from "<<dc->getRemote(); |
b40562da RG |
816 | if(!dc->d_ednssubnet.source.empty()) { |
817 | L<<" (ecs "<<dc->d_ednssubnet.source.toString()<<")"; | |
6e986f5e | 818 | } |
976ec823 | 819 | L<<endl; |
820 | } | |
c75a6a9e | 821 | |
fededf47 | 822 | sr.setId(MT->getTid()); |
67828389 | 823 | if(!dc->d_mdp.d_header.rd) |
c836dc19 BH |
824 | sr.setCacheOnly(); |
825 | ||
0a273054 | 826 | if (t_pdl->get()) { |
ba21fcfe | 827 | (*t_pdl)->prerpz(dq, res); |
0a273054 RG |
828 | } |
829 | ||
db486de5 | 830 | // Check if the query has a policy attached to it |
0a273054 | 831 | if (wantsRPZ) { |
1f1ca368 | 832 | appliedPolicy = luaconfsLocal->dfe.getQueryPolicy(dc->d_mdp.d_qname, dc->d_remote, sr.d_discardedPolicies); |
0a273054 | 833 | } |
644dd1da | 834 | |
54be222b | 835 | // if there is a RecursorLua active, and it 'took' the query in preResolve, we don't launch beginResolve |
ba21fcfe | 836 | if(!t_pdl->get() || !(*t_pdl)->preresolve(dq, res)) { |
b8470add PL |
837 | |
838 | sr.d_wantsRPZ = wantsRPZ; | |
839 | if(wantsRPZ) { | |
840 | switch(appliedPolicy.d_kind) { | |
841 | case DNSFilterEngine::PolicyKind::NoAction: | |
842 | break; | |
843 | case DNSFilterEngine::PolicyKind::Drop: | |
844 | g_stats.policyDrops++; | |
7a25883a | 845 | g_stats.policyResults[appliedPolicy.d_kind]++; |
b8470add PL |
846 | delete dc; |
847 | dc=0; | |
848 | return; | |
849 | case DNSFilterEngine::PolicyKind::NXDOMAIN: | |
850 | g_stats.policyResults[appliedPolicy.d_kind]++; | |
851 | res=RCode::NXDomain; | |
852 | goto haveAnswer; | |
853 | case DNSFilterEngine::PolicyKind::NODATA: | |
854 | g_stats.policyResults[appliedPolicy.d_kind]++; | |
855 | res=RCode::NoError; | |
db486de5 | 856 | goto haveAnswer; |
b8470add PL |
857 | case DNSFilterEngine::PolicyKind::Custom: |
858 | g_stats.policyResults[appliedPolicy.d_kind]++; | |
859 | res=RCode::NoError; | |
860 | spoofed.d_name=dc->d_mdp.d_qname; | |
861 | spoofed.d_type=appliedPolicy.d_custom->getType(); | |
862 | spoofed.d_ttl = appliedPolicy.d_ttl; | |
863 | spoofed.d_class = 1; | |
864 | spoofed.d_content = appliedPolicy.d_custom; | |
865 | spoofed.d_place = DNSResourceRecord::ANSWER; | |
866 | ret.push_back(spoofed); | |
53508135 | 867 | handleRPZCustom(spoofed, QType(dc->d_mdp.d_qtype), sr, res, ret); |
b8470add PL |
868 | goto haveAnswer; |
869 | case DNSFilterEngine::PolicyKind::Truncate: | |
870 | if(!dc->d_tcp) { | |
871 | g_stats.policyResults[appliedPolicy.d_kind]++; | |
872 | res=RCode::NoError; | |
873 | pw.getHeader()->tc=1; | |
874 | goto haveAnswer; | |
875 | } | |
876 | break; | |
877 | } | |
db486de5 PL |
878 | } |
879 | ||
b8470add | 880 | // Query got not handled for QNAME Policy reasons, now actually go out to find an answer |
44971ca0 PD |
881 | try { |
882 | res = sr.beginResolve(dc->d_mdp.d_qname, QType(dc->d_mdp.d_qtype), dc->d_mdp.d_qclass, ret); | |
9fc36e90 | 883 | shouldNotValidate = sr.wasOutOfBand(); |
44971ca0 PD |
884 | } |
885 | catch(ImmediateServFailException &e) { | |
854d44e3 | 886 | if(g_logCommonErrors) |
887 | L<<Logger::Notice<<"Sending SERVFAIL to "<<dc->getRemote()<<" during resolve of '"<<dc->d_mdp.d_qname<<"' because: "<<e.reason<<endl; | |
44971ca0 PD |
888 | res = RCode::ServFail; |
889 | } | |
4485aa35 | 890 | |
b8470add PL |
891 | // During lookup, an NSDNAME or NSIP trigger was hit in RPZ |
892 | if (res == -2) { // XXX This block should be macro'd, it is repeated post-resolve. | |
893 | appliedPolicy = sr.d_appliedPolicy; | |
894 | g_stats.policyResults[appliedPolicy.d_kind]++; | |
895 | switch(appliedPolicy.d_kind) { | |
896 | case DNSFilterEngine::PolicyKind::NoAction: // This can never happen | |
897 | throw PDNSException("NoAction policy returned while a NSDNAME or NSIP trigger was hit"); | |
898 | case DNSFilterEngine::PolicyKind::Drop: | |
899 | g_stats.policyDrops++; | |
900 | delete dc; | |
901 | dc=0; | |
902 | return; | |
903 | case DNSFilterEngine::PolicyKind::NXDOMAIN: | |
904 | ret.clear(); | |
905 | res=RCode::NXDomain; | |
906 | goto haveAnswer; | |
907 | ||
908 | case DNSFilterEngine::PolicyKind::NODATA: | |
909 | ret.clear(); | |
910 | res=RCode::NoError; | |
911 | goto haveAnswer; | |
912 | ||
913 | case DNSFilterEngine::PolicyKind::Truncate: | |
914 | if(!dc->d_tcp) { | |
915 | ret.clear(); | |
916 | res=RCode::NoError; | |
917 | pw.getHeader()->tc=1; | |
918 | goto haveAnswer; | |
919 | } | |
920 | break; | |
921 | ||
922 | case DNSFilterEngine::PolicyKind::Custom: | |
923 | ret.clear(); | |
924 | res=RCode::NoError; | |
925 | spoofed.d_name=dc->d_mdp.d_qname; | |
926 | spoofed.d_type=appliedPolicy.d_custom->getType(); | |
927 | spoofed.d_ttl = appliedPolicy.d_ttl; | |
928 | spoofed.d_class = 1; | |
929 | spoofed.d_content = appliedPolicy.d_custom; | |
930 | spoofed.d_place = DNSResourceRecord::ANSWER; | |
931 | ret.push_back(spoofed); | |
53508135 | 932 | handleRPZCustom(spoofed, QType(dc->d_mdp.d_qtype), sr, res, ret); |
b8470add PL |
933 | goto haveAnswer; |
934 | } | |
935 | } | |
936 | ||
937 | if (wantsRPZ) { | |
1f1ca368 | 938 | appliedPolicy = luaconfsLocal->dfe.getPostPolicy(ret, sr.d_discardedPolicies); |
b8470add | 939 | } |
db486de5 PL |
940 | |
941 | if(t_pdl->get()) { | |
942 | if(res == RCode::NoError) { | |
943 | auto i=ret.cbegin(); | |
944 | for(; i!= ret.cend(); ++i) | |
945 | if(i->d_type == dc->d_mdp.d_qtype && i->d_place == DNSResourceRecord::ANSWER) | |
946 | break; | |
ba21fcfe | 947 | if(i == ret.cend() && (*t_pdl)->nodata(dq, res)) |
3ca4e735 PL |
948 | shouldNotValidate = true; |
949 | ||
db486de5 | 950 | } |
ba21fcfe | 951 | else if(res == RCode::NXDomain && (*t_pdl)->nxdomain(dq, res)) |
3ca4e735 | 952 | shouldNotValidate = true; |
db486de5 | 953 | |
ba21fcfe | 954 | if((*t_pdl)->postresolve(dq, res)) |
3ca4e735 | 955 | shouldNotValidate = true; |
db486de5 PL |
956 | } |
957 | ||
b8470add PL |
958 | if (wantsRPZ) { //XXX This block is repeated, see above |
959 | g_stats.policyResults[appliedPolicy.d_kind]++; | |
960 | switch(appliedPolicy.d_kind) { | |
961 | case DNSFilterEngine::PolicyKind::NoAction: | |
962 | break; | |
963 | case DNSFilterEngine::PolicyKind::Drop: | |
964 | g_stats.policyDrops++; | |
965 | delete dc; | |
966 | dc=0; | |
967 | return; | |
968 | case DNSFilterEngine::PolicyKind::NXDOMAIN: | |
969 | ret.clear(); | |
970 | res=RCode::NXDomain; | |
971 | goto haveAnswer; | |
972 | ||
973 | case DNSFilterEngine::PolicyKind::NODATA: | |
974 | ret.clear(); | |
975 | res=RCode::NoError; | |
976 | goto haveAnswer; | |
977 | ||
978 | case DNSFilterEngine::PolicyKind::Truncate: | |
979 | if(!dc->d_tcp) { | |
980 | ret.clear(); | |
981 | res=RCode::NoError; | |
982 | pw.getHeader()->tc=1; | |
983 | goto haveAnswer; | |
984 | } | |
985 | break; | |
986 | ||
987 | case DNSFilterEngine::PolicyKind::Custom: | |
988 | ret.clear(); | |
989 | res=RCode::NoError; | |
990 | spoofed.d_name=dc->d_mdp.d_qname; | |
991 | spoofed.d_type=appliedPolicy.d_custom->getType(); | |
992 | spoofed.d_ttl = appliedPolicy.d_ttl; | |
993 | spoofed.d_class = 1; | |
994 | spoofed.d_content = appliedPolicy.d_custom; | |
995 | spoofed.d_place = DNSResourceRecord::ANSWER; | |
996 | ret.push_back(spoofed); | |
53508135 | 997 | handleRPZCustom(spoofed, QType(dc->d_mdp.d_qtype), sr, res, ret); |
b8470add PL |
998 | goto haveAnswer; |
999 | } | |
644dd1da | 1000 | } |
4485aa35 | 1001 | } |
644dd1da | 1002 | haveAnswer:; |
3e8216c8 | 1003 | if(res == PolicyDecision::DROP) { |
e9c2ad3a | 1004 | g_stats.policyDrops++; |
ae7e77ad | 1005 | delete dc; |
1006 | dc=0; | |
1007 | return; | |
3ddb9247 | 1008 | } |
3e8216c8 | 1009 | if(tracedQuery || res == PolicyDecision::PASS || res == RCode::ServFail || pw.getHeader()->rcode == RCode::ServFail) |
1dc8f4d0 | 1010 | { |
85ffbc53 PD |
1011 | string trace(sr.getTrace()); |
1012 | if(!trace.empty()) { | |
1013 | vector<string> lines; | |
1014 | boost::split(lines, trace, boost::is_any_of("\n")); | |
1dc8f4d0 | 1015 | for(const string& line : lines) { |
85ffbc53 PD |
1016 | if(!line.empty()) |
1017 | L<<Logger::Warning<< line << endl; | |
1018 | } | |
1019 | } | |
1020 | } | |
3ddb9247 | 1021 | |
b3f0ed10 | 1022 | if(res == PolicyDecision::PASS) { // XXX what does this MEAN? Why servfail on PASS? |
0fe1d080 PD |
1023 | pw.getHeader()->rcode=RCode::ServFail; |
1024 | // no commit here, because no record | |
1025 | g_stats.servFails++; | |
1026 | } | |
288f4aa9 | 1027 | else { |
ea634573 | 1028 | pw.getHeader()->rcode=res; |
92011b8f | 1029 | |
f3fe4ae6 | 1030 | // Does the validation mode or query demand validation? |
9fc36e90 | 1031 | if(!shouldNotValidate && (g_dnssecmode == DNSSECMode::ValidateAll || g_dnssecmode==DNSSECMode::ValidateForLog || ((dc->d_mdp.d_header.ad || DNSSECOK) && g_dnssecmode==DNSSECMode::Process))) { |
b25cae9a | 1032 | try { |
f3fe4ae6 | 1033 | if(sr.doLog()) { |
5fc44cd2 | 1034 | L<<Logger::Warning<<"Starting validation of answer to "<<dc->d_mdp.d_qname<<"|"<<QType(dc->d_mdp.d_qtype).getName()<<" for "<<dc->d_remote.toStringWithPort()<<endl; |
2e921ec6 | 1035 | } |
b25cae9a | 1036 | |
4898a348 RG |
1037 | ResolveContext ctx; |
1038 | #ifdef HAVE_PROTOBUF | |
1039 | ctx.d_initialRequestId = dc->d_uuid; | |
1040 | #endif | |
1041 | auto state=validateRecords(ctx, ret); | |
b25cae9a | 1042 | if(state == Secure) { |
2e921ec6 | 1043 | if(sr.doLog()) { |
5fc44cd2 | 1044 | L<<Logger::Warning<<"Answer to "<<dc->d_mdp.d_qname<<"|"<<QType(dc->d_mdp.d_qtype).getName()<<" for "<<dc->d_remote.toStringWithPort()<<" validates correctly"<<endl; |
2e921ec6 | 1045 | } |
b25cae9a | 1046 | |
1047 | // Is the query source interested in the value of the ad-bit? | |
885c8881 | 1048 | if (dc->d_mdp.d_header.ad || DNSSECOK) |
b25cae9a | 1049 | pw.getHeader()->ad=1; |
1050 | } | |
1051 | else if(state == Insecure) { | |
f3fe4ae6 | 1052 | if(sr.doLog()) { |
5fc44cd2 | 1053 | L<<Logger::Warning<<"Answer to "<<dc->d_mdp.d_qname<<"|"<<QType(dc->d_mdp.d_qtype).getName()<<" for "<<dc->d_remote.toStringWithPort()<<" validates as Insecure"<<endl; |
12ce523e | 1054 | } |
b25cae9a | 1055 | |
1056 | pw.getHeader()->ad=0; | |
f3fe4ae6 | 1057 | } |
b25cae9a | 1058 | else if(state == Bogus) { |
c87e1876 | 1059 | if(g_dnssecLogBogus || sr.doLog() || g_dnssecmode == DNSSECMode::ValidateForLog) { |
5fc44cd2 | 1060 | L<<Logger::Warning<<"Answer to "<<dc->d_mdp.d_qname<<"|"<<QType(dc->d_mdp.d_qtype).getName()<<" for "<<dc->d_remote.toStringWithPort()<<" validates as Bogus"<<endl; |
b25cae9a | 1061 | } |
1062 | ||
1063 | // Does the query or validation mode sending out a SERVFAIL on validation errors? | |
885c8881 | 1064 | if(!pw.getHeader()->cd && (g_dnssecmode == DNSSECMode::ValidateAll || dc->d_mdp.d_header.ad || DNSSECOK)) { |
b25cae9a | 1065 | if(sr.doLog()) { |
5fc44cd2 | 1066 | L<<Logger::Warning<<"Sending out SERVFAIL for "<<dc->d_mdp.d_qname<<"|"<<QType(dc->d_mdp.d_qtype).getName()<<" because recursor or query demands it for Bogus results"<<endl; |
b25cae9a | 1067 | } |
1068 | ||
1069 | pw.getHeader()->rcode=RCode::ServFail; | |
1070 | goto sendit; | |
1071 | } else { | |
1072 | if(sr.doLog()) { | |
5fc44cd2 | 1073 | L<<Logger::Warning<<"Not sending out SERVFAIL for "<<dc->d_mdp.d_qname<<"|"<<QType(dc->d_mdp.d_qtype).getName()<<" Bogus validation since neither config nor query demands this"<<endl; |
b25cae9a | 1074 | } |
1075 | } | |
1076 | } | |
1077 | } | |
1078 | catch(ImmediateServFailException &e) { | |
1079 | if(g_logCommonErrors) | |
5fc44cd2 | 1080 | L<<Logger::Notice<<"Sending SERVFAIL to "<<dc->getRemote()<<" during validation of '"<<dc->d_mdp.d_qname<<"|"<<QType(dc->d_mdp.d_qtype).getName()<<"' because: "<<e.reason<<endl; |
b25cae9a | 1081 | pw.getHeader()->rcode=RCode::ServFail; |
1082 | goto sendit; | |
f3fe4ae6 | 1083 | } |
b3f0ed10 | 1084 | } |
1085 | ||
c154c8a4 | 1086 | if(ret.size()) { |
92476c8b | 1087 | orderAndShuffle(ret); |
ad42489c | 1088 | if(auto sl = luaconfsLocal->sortlist.getOrderCmp(dc->d_remote)) { |
3e61e7f7 | 1089 | sort(ret.begin(), ret.end(), *sl); |
1090 | variableAnswer=true; | |
1091 | } | |
8e079f3a | 1092 | } |
1093 | if(haveEDNS) { | |
1094 | ret.push_back(makeOpt(edo.d_packetsize, 0, edo.d_Z)); | |
1095 | } | |
0afa32d4 RG |
1096 | |
1097 | bool needCommit = false; | |
8e079f3a | 1098 | for(auto i=ret.cbegin(); i!=ret.cend(); ++i) { |
3e80ebce KM |
1099 | if( ! DNSSECOK && |
1100 | ( i->d_type == QType::NSEC3 || | |
1101 | ( | |
1102 | ( i->d_type == QType::RRSIG || i->d_type==QType::NSEC ) && | |
1103 | ( | |
1104 | ( dc->d_mdp.d_qtype != i->d_type && dc->d_mdp.d_qtype != QType::ANY ) || | |
1105 | i->d_place != DNSResourceRecord::ANSWER | |
1106 | ) | |
1107 | ) | |
1108 | ) | |
1109 | ) { | |
2e921ec6 | 1110 | continue; |
3e80ebce KM |
1111 | } |
1112 | ||
8e079f3a | 1113 | pw.startRecord(i->d_name, i->d_type, i->d_ttl, i->d_class, i->d_place); |
1114 | if(i->d_type != QType::OPT) // their TTL ain't real | |
1115 | minTTL = min(minTTL, i->d_ttl); | |
1116 | i->d_content->toPacket(pw); | |
1117 | if(pw.size() > maxanswersize) { | |
1118 | pw.rollback(); | |
1119 | if(i->d_place==DNSResourceRecord::ANSWER) // only truncate if we actually omitted parts of the answer | |
add935a2 | 1120 | { |
4957a608 | 1121 | pw.getHeader()->tc=1; |
add935a2 PD |
1122 | pw.truncate(); |
1123 | } | |
8e079f3a | 1124 | goto sendit; // need to jump over pw.commit |
1125 | } | |
0afa32d4 | 1126 | needCommit = true; |
aa7929a3 | 1127 | #ifdef HAVE_PROTOBUF |
d9d3f9c1 RG |
1128 | if(luaconfsLocal->protobufServer && (i->d_type == QType::A || i->d_type == QType::AAAA || i->d_type == QType::CNAME)) { |
1129 | pbMessage.addRR(*i); | |
aa7929a3 RG |
1130 | } |
1131 | #endif | |
ea634573 | 1132 | } |
0afa32d4 | 1133 | if(needCommit) |
8e079f3a | 1134 | pw.commit(); |
288f4aa9 | 1135 | } |
10321a98 | 1136 | sendit:; |
b3f0ed10 | 1137 | |
79332bff | 1138 | g_rs.submitResponse(dc->d_mdp.d_qtype, packet.size(), !dc->d_tcp); |
92011b8f | 1139 | updateResponseStats(res, dc->d_remote, packet.size(), &dc->d_mdp.d_qname, dc->d_mdp.d_qtype); |
aa7929a3 | 1140 | #ifdef HAVE_PROTOBUF |
b790ef3d | 1141 | if (luaconfsLocal->protobufServer && (!luaconfsLocal->protobufTaggedOnly || (appliedPolicy.d_name && !appliedPolicy.d_name->empty()) || !dc->d_policyTags.empty())) { |
d9d3f9c1 RG |
1142 | pbMessage.setBytes(packet.size()); |
1143 | pbMessage.setResponseCode(pw.getHeader()->rcode); | |
0a273054 RG |
1144 | if (appliedPolicy.d_name) { |
1145 | pbMessage.setAppliedPolicy(*appliedPolicy.d_name); | |
1146 | } | |
d9d3f9c1 | 1147 | pbMessage.setPolicyTags(dc->d_policyTags); |
58307a85 | 1148 | pbMessage.setQueryTime(dc->d_now.tv_sec, dc->d_now.tv_usec); |
02b47f43 | 1149 | protobufLogResponse(luaconfsLocal->protobufServer, pbMessage); |
aa7929a3 RG |
1150 | } |
1151 | #endif | |
ea634573 | 1152 | if(!dc->d_tcp) { |
b71b60ee | 1153 | struct msghdr msgh; |
1154 | struct iovec iov; | |
1155 | char cbuf[256]; | |
1156 | fillMSGHdr(&msgh, &iov, cbuf, 0, (char*)&*packet.begin(), packet.size(), &dc->d_remote); | |
2c0af54f PD |
1157 | msgh.msg_control=NULL; |
1158 | ||
cbc03320 | 1159 | if(g_fromtosockets.count(dc->d_socket)) { |
fbe2a2e0 | 1160 | addCMsgSrcAddr(&msgh, cbuf, &dc->d_local, 0); |
2c0af54f | 1161 | } |
cbc03320 | 1162 | if(sendmsg(dc->d_socket, &msgh, 0) < 0 && g_logCommonErrors) |
1163 | L<<Logger::Warning<<"Sending UDP reply to client "<<dc->d_remote.toStringWithPort()<<" failed with: "<<strerror(errno)<<endl; | |
3762e821 | 1164 | if(!SyncRes::s_nopacketcache && !variableAnswer && !sr.wasVariable() ) { |
d9d3f9c1 | 1165 | t_packetCache->insertResponsePacket(dc->d_tag, dc->d_mdp.d_qname, dc->d_mdp.d_qtype, dc->d_query, |
76e2b9e3 | 1166 | string((const char*)&*packet.begin(), packet.size()), |
3ddb9247 | 1167 | g_now.tv_sec, |
76e2b9e3 | 1168 | pw.getHeader()->rcode == RCode::ServFail ? SyncRes::s_packetcacheservfailttl : |
d9d3f9c1 RG |
1169 | min(minTTL,SyncRes::s_packetcachettl), |
1170 | &pbMessage); | |
1051f8a9 | 1171 | } |
3762e821 | 1172 | // else cerr<<"Not putting in packet cache: "<<sr.wasVariable()<<endl; |
feccc9fc | 1173 | } |
9c495589 BH |
1174 | else { |
1175 | char buf[2]; | |
ea634573 BH |
1176 | buf[0]=packet.size()/256; |
1177 | buf[1]=packet.size()%256; | |
feccc9fc | 1178 | |
c038218b | 1179 | Utility::iovec iov[2]; |
feccc9fc | 1180 | |
ea634573 BH |
1181 | iov[0].iov_base=(void*)buf; iov[0].iov_len=2; |
1182 | iov[1].iov_base=(void*)&*packet.begin(); iov[1].iov_len = packet.size(); | |
feccc9fc | 1183 | |
dd079764 | 1184 | int wret=Utility::writev(dc->d_socket, iov, 2); |
0e9d9ce2 | 1185 | bool hadError=true; |
feccc9fc | 1186 | |
dd079764 | 1187 | if(wret == 0) |
18af64a8 | 1188 | L<<Logger::Error<<"EOF writing TCP answer to "<<dc->getRemote()<<endl; |
dd079764 | 1189 | else if(wret < 0 ) |
18af64a8 | 1190 | L<<Logger::Error<<"Error writing TCP answer to "<<dc->getRemote()<<": "<< strerror(errno) <<endl; |
dd079764 RG |
1191 | else if((unsigned int)wret != 2 + packet.size()) |
1192 | L<<Logger::Error<<"Oops, partial answer sent to "<<dc->getRemote()<<" for "<<dc->d_mdp.d_qname<<" (size="<< (2 + packet.size()) <<", sent "<<wret<<")"<<endl; | |
0e9d9ce2 | 1193 | else |
18af64a8 | 1194 | hadError=false; |
3ddb9247 | 1195 | |
09e6702a | 1196 | // update tcp connection status, either by closing or moving to 'BYTE0' |
3ddb9247 | 1197 | |
09e6702a | 1198 | if(hadError) { |
18af64a8 | 1199 | // no need to remove us from FDM, we weren't there |
c36bc97a | 1200 | dc->d_socket = -1; |
09e6702a | 1201 | } |
a6ae6414 | 1202 | else { |
fde296a3 RG |
1203 | dc->d_tcpConnection->queriesCount++; |
1204 | if (g_tcpMaxQueriesPerConn && dc->d_tcpConnection->queriesCount >= g_tcpMaxQueriesPerConn) { | |
1205 | dc->d_socket = -1; | |
1206 | } | |
1207 | else { | |
1208 | dc->d_tcpConnection->state=TCPConnection::BYTE0; | |
1209 | Utility::gettimeofday(&g_now, 0); // needs to be updated | |
1210 | t_fdm->addReadFD(dc->d_socket, handleRunningTCPQuestion, dc->d_tcpConnection); | |
1211 | t_fdm->setReadTTD(dc->d_socket, g_now, g_tcpTimeout); | |
1212 | } | |
0e9d9ce2 | 1213 | } |
9c495589 | 1214 | } |
3ddb9247 | 1215 | |
1d5b3ce6 | 1216 | if(!g_quiet) { |
461df9d2 | 1217 | L<<Logger::Error<<t_id<<" ["<<MT->getTid()<<"/"<<MT->numProcesses()<<"] answer to "<<(dc->d_mdp.d_header.rd?"":"non-rd ")<<"question '"<<dc->d_mdp.d_qname<<"|"<<DNSRecordContent::NumberToType(dc->d_mdp.d_qtype); |
ea634573 | 1218 | L<<"': "<<ntohs(pw.getHeader()->ancount)<<" answers, "<<ntohs(pw.getHeader()->arcount)<<" additional, took "<<sr.d_outqueries<<" packets, "<< |
9de3e034 | 1219 | sr.d_totUsec/1000.0<<" ms, "<< |
1220 | sr.d_throttledqueries<<" throttled, "<<sr.d_timeouts<<" timeouts, "<<sr.d_tcpoutqueries<<" tcp connections, rcode="<<res<<endl; | |
c75a6a9e | 1221 | } |
b23b8614 | 1222 | |
3ddb9247 | 1223 | sr.d_outqueries ? t_RC->cacheMisses++ : t_RC->cacheHits++; |
fe213470 BH |
1224 | float spent=makeFloat(sr.d_now-dc->d_now); |
1225 | if(spent < 0.001) | |
1226 | g_stats.answers0_1++; | |
1227 | else if(spent < 0.010) | |
1228 | g_stats.answers1_10++; | |
1229 | else if(spent < 0.1) | |
1230 | g_stats.answers10_100++; | |
1231 | else if(spent < 1.0) | |
1232 | g_stats.answers100_1000++; | |
1233 | else | |
1234 | g_stats.answersSlow++; | |
1235 | ||
574af7ea | 1236 | uint64_t newLat=(uint64_t)(spent*1000000); |
b841314c | 1237 | newLat = min(newLat,(uint64_t)(((uint64_t) g_networkTimeoutMsec)*1000)); // outliers of several minutes exist.. |
08f3f638 | 1238 | g_stats.avgLatencyUsec=(1-1.0/g_latencyStatSize)*g_stats.avgLatencyUsec + (float)newLat/g_latencyStatSize; |
0a6b1027 | 1239 | // no worries, we do this for packet cache hits elsewhere |
c6d04bdc | 1240 | // cout<<dc->d_mdp.d_qname<<"\t"<<MT->getUsec()<<"\t"<<sr.d_outqueries<<endl; |
ea634573 | 1241 | delete dc; |
c36bc97a | 1242 | dc=0; |
288f4aa9 | 1243 | } |
3f81d239 | 1244 | catch(PDNSException &ae) { |
a903b39c | 1245 | L<<Logger::Error<<"startDoResolve problem "<<makeLoginfo(dc)<<": "<<ae.reason<<endl; |
c36bc97a | 1246 | delete dc; |
288f4aa9 | 1247 | } |
7b1469bb | 1248 | catch(MOADNSException& e) { |
a903b39c | 1249 | L<<Logger::Error<<"DNS parser error "<<makeLoginfo(dc) <<": "<<dc->d_mdp.d_qname<<", "<<e.what()<<endl; |
c36bc97a | 1250 | delete dc; |
7b1469bb | 1251 | } |
fdbf35ac | 1252 | catch(std::exception& e) { |
068c7634 PD |
1253 | L<<Logger::Error<<"STL error "<< makeLoginfo(dc)<<": "<<e.what(); |
1254 | ||
1255 | // Luawrapper nests the exception from Lua, so we unnest it here | |
1256 | try { | |
1257 | std::rethrow_if_nested(e); | |
1258 | } catch(const std::exception& e) { | |
1259 | L<<". Extra info: "<<e.what(); | |
1260 | } catch(...) {} | |
1261 | ||
1262 | L<<endl; | |
c36bc97a | 1263 | delete dc; |
c154c8a4 | 1264 | } |
288f4aa9 | 1265 | catch(...) { |
a903b39c | 1266 | L<<Logger::Error<<"Any other exception in a resolver context "<< makeLoginfo(dc) <<endl; |
288f4aa9 | 1267 | } |
3ddb9247 | 1268 | |
ec6eacbc | 1269 | g_stats.maxMThreadStackUsage = max(MT->getMaxStackUsage(), g_stats.maxMThreadStackUsage); |
288f4aa9 BH |
1270 | } |
1271 | ||
677e2a46 | 1272 | void makeControlChannelSocket(int processNum=-1) |
1d5b3ce6 | 1273 | { |
2d733c0f | 1274 | string sockname=::arg()["socket-dir"]+"/"+s_programname; |
677e2a46 | 1275 | if(processNum >= 0) |
335da0ba | 1276 | sockname += "."+std::to_string(processNum); |
677e2a46 | 1277 | sockname+=".controlsocket"; |
41f7a068 | 1278 | s_rcc.listen(sockname); |
3ddb9247 | 1279 | |
387de317 BH |
1280 | int sockowner = -1; |
1281 | int sockgroup = -1; | |
1282 | ||
1283 | if (!::arg().isEmpty("socket-group")) | |
1284 | sockgroup=::arg().asGid("socket-group"); | |
1285 | if (!::arg().isEmpty("socket-owner")) | |
1286 | sockowner=::arg().asUid("socket-owner"); | |
3ddb9247 | 1287 | |
f838ad8d BH |
1288 | if (sockgroup > -1 || sockowner > -1) { |
1289 | if(chown(sockname.c_str(), sockowner, sockgroup) < 0) { | |
1290 | unixDie("Failed to chown control socket"); | |
1291 | } | |
1292 | } | |
387de317 BH |
1293 | |
1294 | // do mode change if socket-mode is given | |
1295 | if(!::arg().isEmpty("socket-mode")) { | |
1296 | mode_t sockmode=::arg().asMode("socket-mode"); | |
34c513f9 RG |
1297 | if(chmod(sockname.c_str(), sockmode) < 0) { |
1298 | unixDie("Failed to chmod control socket"); | |
1299 | } | |
387de317 | 1300 | } |
1d5b3ce6 BH |
1301 | } |
1302 | ||
b40562da | 1303 | static bool getQNameAndSubnet(const std::string& question, DNSName* dnsname, uint16_t* qtype, uint16_t* qclass, EDNSSubnetOpts* ednssubnet) |
02b47f43 | 1304 | { |
b40562da | 1305 | bool found = false; |
02b47f43 RG |
1306 | const struct dnsheader* dh = (struct dnsheader*)question.c_str(); |
1307 | size_t questionLen = question.length(); | |
1308 | unsigned int consumed=0; | |
1309 | *dnsname=DNSName(question.c_str(), questionLen, sizeof(dnsheader), false, qtype, qclass, &consumed); | |
1310 | ||
1311 | size_t pos= sizeof(dnsheader)+consumed+4; | |
1312 | /* at least OPT root label (1), type (2), class (2) and ttl (4) + OPT RR rdlen (2) | |
1313 | = 11 */ | |
1314 | if(ntohs(dh->arcount) == 1 && questionLen > pos + 11) { // this code can extract one (1) EDNS Subnet option | |
1315 | /* OPT root label (1) followed by type (2) */ | |
1316 | if(question.at(pos)==0 && question.at(pos+1)==0 && question.at(pos+2)==QType::OPT) { | |
1317 | char* ecsStart = nullptr; | |
1318 | size_t ecsLen = 0; | |
1319 | int res = getEDNSOption((char*)question.c_str()+pos+9, questionLen - pos - 9, EDNSOptionCode::ECS, &ecsStart, &ecsLen); | |
1320 | if (res == 0 && ecsLen > 4) { | |
1321 | EDNSSubnetOpts eso; | |
1322 | if(getEDNSSubnetOptsFromString(ecsStart + 4, ecsLen - 4, &eso)) { | |
b40562da RG |
1323 | *ednssubnet=eso; |
1324 | found = true; | |
02b47f43 RG |
1325 | } |
1326 | } | |
1327 | } | |
1328 | } | |
b40562da | 1329 | return found; |
02b47f43 RG |
1330 | } |
1331 | ||
d8f6d49f | 1332 | void handleRunningTCPQuestion(int fd, FDMultiplexer::funcparam_t& var) |
09e6702a | 1333 | { |
cd989c87 | 1334 | shared_ptr<TCPConnection> conn=any_cast<shared_ptr<TCPConnection> >(var); |
c038218b | 1335 | |
879b3f70 | 1336 | if(conn->state==TCPConnection::BYTE0) { |
b841314c | 1337 | ssize_t bytes=recv(conn->getFD(), conn->data, 2, 0); |
09e6702a | 1338 | if(bytes==1) |
667f7e60 | 1339 | conn->state=TCPConnection::BYTE1; |
3ddb9247 | 1340 | if(bytes==2) { |
a0aa4f64 | 1341 | conn->qlen=(((unsigned char)conn->data[0]) << 8)+ (unsigned char)conn->data[1]; |
667f7e60 BH |
1342 | conn->bytesread=0; |
1343 | conn->state=TCPConnection::GETQUESTION; | |
09e6702a BH |
1344 | } |
1345 | if(!bytes || bytes < 0) { | |
bb4bdbaf | 1346 | t_fdm->removeReadFD(fd); |
09e6702a BH |
1347 | return; |
1348 | } | |
1349 | } | |
667f7e60 | 1350 | else if(conn->state==TCPConnection::BYTE1) { |
b841314c | 1351 | ssize_t bytes=recv(conn->getFD(), conn->data+1, 1, 0); |
09e6702a | 1352 | if(bytes==1) { |
667f7e60 | 1353 | conn->state=TCPConnection::GETQUESTION; |
a0aa4f64 | 1354 | conn->qlen=(((unsigned char)conn->data[0]) << 8)+ (unsigned char)conn->data[1]; |
667f7e60 | 1355 | conn->bytesread=0; |
09e6702a BH |
1356 | } |
1357 | if(!bytes || bytes < 0) { | |
1358 | if(g_logCommonErrors) | |
cd989c87 | 1359 | L<<Logger::Error<<"TCP client "<< conn->d_remote.toString() <<" disconnected after first byte"<<endl; |
bb4bdbaf | 1360 | t_fdm->removeReadFD(fd); |
09e6702a BH |
1361 | return; |
1362 | } | |
1363 | } | |
667f7e60 | 1364 | else if(conn->state==TCPConnection::GETQUESTION) { |
b841314c | 1365 | ssize_t bytes=recv(conn->getFD(), conn->data + conn->bytesread, conn->qlen - conn->bytesread, 0); |
f9d67b41 | 1366 | if(!bytes || bytes < 0 || bytes > std::numeric_limits<std::uint16_t>::max()) { |
cd989c87 | 1367 | L<<Logger::Error<<"TCP client "<< conn->d_remote.toString() <<" disconnected while reading question body"<<endl; |
bb4bdbaf | 1368 | t_fdm->removeReadFD(fd); |
09e6702a BH |
1369 | return; |
1370 | } | |
b841314c | 1371 | conn->bytesread+=(uint16_t)bytes; |
667f7e60 | 1372 | if(conn->bytesread==conn->qlen) { |
bb4bdbaf | 1373 | t_fdm->removeReadFD(fd); // should no longer awake ourselves when there is data to read |
879b3f70 | 1374 | |
09e6702a BH |
1375 | DNSComboWriter* dc=0; |
1376 | try { | |
cd989c87 | 1377 | dc=new DNSComboWriter(conn->data, conn->qlen, g_now); |
09e6702a BH |
1378 | } |
1379 | catch(MOADNSException &mde) { | |
3ddb9247 | 1380 | g_stats.clientParseError++; |
4957a608 | 1381 | if(g_logCommonErrors) |
cd989c87 | 1382 | L<<Logger::Error<<"Unable to parse packet from TCP client "<< conn->d_remote.toString() <<endl; |
4957a608 | 1383 | return; |
09e6702a | 1384 | } |
cd989c87 BH |
1385 | dc->d_tcpConnection = conn; // carry the torch |
1386 | dc->setSocket(conn->getFD()); // this is the only time a copy is made of the actual fd | |
09e6702a | 1387 | dc->d_tcp=true; |
cd989c87 | 1388 | dc->setRemote(&conn->d_remote); |
a6147cd2 | 1389 | ComboAddress dest; |
1390 | memset(&dest, 0, sizeof(dest)); | |
1391 | dest.sin4.sin_family = conn->d_remote.sin4.sin_family; | |
1392 | socklen_t len = dest.getSocklen(); | |
1393 | getsockname(conn->getFD(), (sockaddr*)&dest, &len); // if this fails, we're ok with it | |
1394 | dc->setLocal(dest); | |
33dcceba RG |
1395 | DNSName qname; |
1396 | uint16_t qtype=0; | |
1397 | uint16_t qclass=0; | |
1398 | bool needECS = false; | |
aa7929a3 | 1399 | #ifdef HAVE_PROTOBUF |
02b47f43 | 1400 | auto luaconfsLocal = g_luaconfs.getLocal(); |
33dcceba RG |
1401 | if (luaconfsLocal->protobufServer) { |
1402 | needECS = true; | |
1403 | } | |
1404 | #endif | |
1405 | ||
1406 | if(needECS || (t_pdl->get() && (*t_pdl)->d_gettag)) { | |
1407 | ||
1408 | try { | |
b40562da RG |
1409 | dc->d_ecsParsed = true; |
1410 | dc->d_ecsFound = getQNameAndSubnet(std::string(conn->data, conn->qlen), &qname, &qtype, &qclass, &dc->d_ednssubnet); | |
02b47f43 | 1411 | |
33dcceba RG |
1412 | if(t_pdl->get() && (*t_pdl)->d_gettag) { |
1413 | try { | |
b40562da | 1414 | dc->d_tag = (*t_pdl)->gettag(conn->d_remote, dc->d_ednssubnet.source, dest, qname, qtype, &dc->d_policyTags, dc->d_data); |
33dcceba RG |
1415 | } |
1416 | catch(std::exception& e) { | |
1417 | if(g_logCommonErrors) | |
1418 | L<<Logger::Warning<<"Error parsing a query packet qname='"<<qname<<"' for tag determination, setting tag=0: "<<e.what()<<endl; | |
1419 | } | |
1420 | } | |
1421 | } | |
1422 | catch(std::exception& e) | |
1423 | { | |
1424 | if(g_logCommonErrors) | |
1425 | L<<Logger::Warning<<"Error parsing a query packet for tag determination, setting tag=0: "<<e.what()<<endl; | |
1426 | } | |
1427 | } | |
1428 | #ifdef HAVE_PROTOBUF | |
4898a348 | 1429 | if(luaconfsLocal->protobufServer || luaconfsLocal->outgoingProtobufServer) { |
02b47f43 | 1430 | dc->d_uuid = (*t_uuidGenerator)(); |
4898a348 | 1431 | } |
02b47f43 | 1432 | |
4898a348 | 1433 | if(luaconfsLocal->protobufServer) { |
02b47f43 | 1434 | try { |
02b47f43 | 1435 | const struct dnsheader* dh = (const struct dnsheader*) conn->data; |
02b47f43 | 1436 | |
b790ef3d | 1437 | if (!luaconfsLocal->protobufTaggedOnly) { |
b40562da | 1438 | protobufLogQuery(luaconfsLocal->protobufServer, luaconfsLocal->protobufMaskV4, luaconfsLocal->protobufMaskV6, dc->d_uuid, conn->d_remote, dest, dc->d_ednssubnet.source, true, dh->id, conn->qlen, qname, qtype, qclass, dc->d_policyTags); |
b790ef3d | 1439 | } |
02b47f43 RG |
1440 | } |
1441 | catch(std::exception& e) { | |
1442 | if(g_logCommonErrors) | |
1443 | L<<Logger::Warning<<"Error parsing a TCP query packet for edns subnet: "<<e.what()<<endl; | |
1444 | } | |
1445 | } | |
aa7929a3 | 1446 | #endif |
879b3f70 | 1447 | if(dc->d_mdp.d_header.qr) { |
4957a608 | 1448 | delete dc; |
048f5db6 | 1449 | g_stats.ignoredCount++; |
4328f463 | 1450 | L<<Logger::Error<<"Ignoring answer from TCP client "<< conn->d_remote.toString() <<" on server socket!"<<endl; |
4957a608 | 1451 | return; |
879b3f70 | 1452 | } |
3abcdab2 PD |
1453 | if(dc->d_mdp.d_header.opcode) { |
1454 | delete dc; | |
048f5db6 | 1455 | g_stats.ignoredCount++; |
4328f463 | 1456 | L<<Logger::Error<<"Ignoring non-query opcode from TCP client "<< conn->d_remote.toString() <<" on server socket!"<<endl; |
3abcdab2 PD |
1457 | return; |
1458 | } | |
09e6702a | 1459 | else { |
4957a608 BH |
1460 | ++g_stats.qcounter; |
1461 | ++g_stats.tcpqcounter; | |
50a5ef72 | 1462 | MT->makeThread(startDoResolve, dc); // deletes dc, will set state to BYTE0 again |
4957a608 | 1463 | return; |
09e6702a BH |
1464 | } |
1465 | } | |
1466 | } | |
1467 | } | |
1468 | ||
6dcd28c3 | 1469 | //! Handle new incoming TCP connection |
d8f6d49f | 1470 | void handleNewTCPQuestion(int fd, FDMultiplexer::funcparam_t& ) |
09e6702a | 1471 | { |
37d3f960 | 1472 | ComboAddress addr; |
09e6702a | 1473 | socklen_t addrlen=sizeof(addr); |
a683e8bd | 1474 | int newsock=accept(fd, (struct sockaddr*)&addr, &addrlen); |
b841314c | 1475 | if(newsock>=0) { |
85c32340 BH |
1476 | if(MT->numProcesses() > g_maxMThreads) { |
1477 | g_stats.overCapacityDrops++; | |
a7b68ae7 RG |
1478 | try { |
1479 | closesocket(newsock); | |
1480 | } | |
1481 | catch(const PDNSException& e) { | |
1482 | L<<Logger::Error<<"Error closing TCP socket after an over capacity drop: "<<e.reason<<endl; | |
1483 | } | |
85c32340 BH |
1484 | return; |
1485 | } | |
1486 | ||
92011b8f | 1487 | if(t_remotes) |
1488 | t_remotes->push_back(addr); | |
49a699c4 | 1489 | if(t_allowFrom && !t_allowFrom->match(&addr)) { |
3ddb9247 | 1490 | if(!g_quiet) |
4957a608 | 1491 | L<<Logger::Error<<"["<<MT->getTid()<<"] dropping TCP query from "<<addr.toString()<<", address not matched by allow-from"<<endl; |
2914b022 | 1492 | |
09e6702a | 1493 | g_stats.unauthorizedTCP++; |
a7b68ae7 RG |
1494 | try { |
1495 | closesocket(newsock); | |
1496 | } | |
1497 | catch(const PDNSException& e) { | |
1498 | L<<Logger::Error<<"Error closing TCP socket after an ACL drop: "<<e.reason<<endl; | |
1499 | } | |
09e6702a BH |
1500 | return; |
1501 | } | |
bd0289fc | 1502 | if(g_maxTCPPerClient && t_tcpClientCounts->count(addr) && (*t_tcpClientCounts)[addr] >= g_maxTCPPerClient) { |
09e6702a | 1503 | g_stats.tcpClientOverflow++; |
a7b68ae7 RG |
1504 | try { |
1505 | closesocket(newsock); // don't call TCPConnection::closeAndCleanup here - did not enter it in the counts yet! | |
1506 | } | |
1507 | catch(const PDNSException& e) { | |
1508 | L<<Logger::Error<<"Error closing TCP socket after an overflow drop: "<<e.reason<<endl; | |
1509 | } | |
09e6702a BH |
1510 | return; |
1511 | } | |
3ddb9247 | 1512 | |
3897b9e1 | 1513 | setNonBlocking(newsock); |
cd989c87 BH |
1514 | shared_ptr<TCPConnection> tc(new TCPConnection(newsock, addr)); |
1515 | tc->state=TCPConnection::BYTE0; | |
3ddb9247 | 1516 | |
cd989c87 | 1517 | t_fdm->addReadFD(tc->getFD(), handleRunningTCPQuestion, tc); |
c038218b | 1518 | |
0bff046b | 1519 | struct timeval now; |
c038218b | 1520 | Utility::gettimeofday(&now, 0); |
cd989c87 | 1521 | t_fdm->setReadTTD(tc->getFD(), now, g_tcpTimeout); |
09e6702a BH |
1522 | } |
1523 | } | |
3ddb9247 | 1524 | |
b71b60ee | 1525 | string* doProcessUDPQuestion(const std::string& question, const ComboAddress& fromaddr, const ComboAddress& destaddr, struct timeval tv, int fd) |
1bc3c142 | 1526 | { |
183eb877 | 1527 | gettimeofday(&g_now, 0); |
b71b60ee | 1528 | struct timeval diff = g_now - tv; |
1529 | double delta=(diff.tv_sec*1000 + diff.tv_usec/1000.0); | |
183eb877 | 1530 | |
22cf1fda | 1531 | if(tv.tv_sec && delta > 1000.0) { |
b71b60ee | 1532 | g_stats.tooOldDrops++; |
1533 | return 0; | |
1534 | } | |
1535 | ||
1bc3c142 | 1536 | ++g_stats.qcounter; |
d7f10541 BH |
1537 | if(fromaddr.sin4.sin_family==AF_INET6) |
1538 | g_stats.ipv6qcounter++; | |
1bc3c142 BH |
1539 | |
1540 | string response; | |
93f0da94 | 1541 | const struct dnsheader* dh = (struct dnsheader*)question.c_str(); |
49a3500d | 1542 | unsigned int ctag=0; |
12aff2e5 | 1543 | bool needECS = false; |
02b47f43 | 1544 | std::vector<std::string> policyTags; |
05c74122 | 1545 | std::unordered_map<string,string> data; |
12aff2e5 | 1546 | #ifdef HAVE_PROTOBUF |
02b47f43 | 1547 | boost::uuids::uuid uniqueId; |
02b47f43 RG |
1548 | auto luaconfsLocal = g_luaconfs.getLocal(); |
1549 | if (luaconfsLocal->protobufServer) { | |
4898a348 | 1550 | uniqueId = (*t_uuidGenerator)(); |
02b47f43 | 1551 | needECS = true; |
4898a348 | 1552 | } else if (luaconfsLocal->outgoingProtobufServer) { |
02b47f43 RG |
1553 | uniqueId = (*t_uuidGenerator)(); |
1554 | } | |
12aff2e5 | 1555 | #endif |
b40562da RG |
1556 | EDNSSubnetOpts ednssubnet; |
1557 | bool ecsFound = false; | |
1558 | bool ecsParsed = false; | |
1bc3c142 | 1559 | try { |
02b47f43 RG |
1560 | DNSName qname; |
1561 | uint16_t qtype=0; | |
1562 | uint16_t qclass=0; | |
1bc3c142 | 1563 | uint32_t age; |
8f7473d7 | 1564 | #ifdef MALLOC_TRACE |
1565 | /* | |
1566 | static uint64_t last=0; | |
1567 | if(!last) | |
1568 | g_mtracer->clearAllocators(); | |
1569 | cout<<g_mtracer->getAllocs()-last<<" "<<g_mtracer->getNumOut()<<" -- BEGIN TRACE"<<endl; | |
1570 | last=g_mtracer->getAllocs(); | |
1571 | cout<<g_mtracer->topAllocatorsString()<<endl; | |
1572 | g_mtracer->clearAllocators(); | |
1573 | */ | |
1574 | #endif | |
55a1378f | 1575 | |
12aff2e5 | 1576 | if(needECS || (t_pdl->get() && (*t_pdl)->d_gettag)) { |
b2eacd67 | 1577 | try { |
b40562da RG |
1578 | ecsParsed = true; |
1579 | ecsFound = getQNameAndSubnet(question, &qname, &qtype, &qclass, &ednssubnet); | |
12aff2e5 RG |
1580 | |
1581 | if(t_pdl->get() && (*t_pdl)->d_gettag) { | |
1582 | try { | |
b40562da | 1583 | ctag=(*t_pdl)->gettag(fromaddr, ednssubnet.source, destaddr, qname, qtype, &policyTags, data); |
12aff2e5 RG |
1584 | } |
1585 | catch(std::exception& e) { | |
1586 | if(g_logCommonErrors) | |
1587 | L<<Logger::Warning<<"Error parsing a query packet qname='"<<qname<<"' for tag determination, setting tag=0: "<<e.what()<<endl; | |
1588 | } | |
8ea8c302 | 1589 | } |
b2eacd67 | 1590 | } |
1591 | catch(std::exception& e) | |
1592 | { | |
1593 | if(g_logCommonErrors) | |
1594 | L<<Logger::Warning<<"Error parsing a query packet for tag determination, setting tag=0: "<<e.what()<<endl; | |
12aff2e5 | 1595 | } |
12ce523e | 1596 | } |
3ddb9247 | 1597 | |
02b47f43 | 1598 | bool cacheHit = false; |
d9d3f9c1 | 1599 | RecProtoBufMessage pbMessage(DNSProtoBufMessage::DNSProtoBufMessageType::Response); |
02b47f43 RG |
1600 | #ifdef HAVE_PROTOBUF |
1601 | if(luaconfsLocal->protobufServer) { | |
b790ef3d | 1602 | if (!luaconfsLocal->protobufTaggedOnly || !policyTags.empty()) { |
b40562da | 1603 | protobufLogQuery(luaconfsLocal->protobufServer, luaconfsLocal->protobufMaskV4, luaconfsLocal->protobufMaskV6, uniqueId, fromaddr, destaddr, ednssubnet.source, false, dh->id, question.size(), qname, qtype, qclass, policyTags); |
b790ef3d | 1604 | } |
d9d3f9c1 RG |
1605 | } |
1606 | #endif /* HAVE_PROTOBUF */ | |
02b47f43 | 1607 | |
d9d3f9c1 RG |
1608 | cacheHit = (!SyncRes::s_nopacketcache && t_packetCache->getResponsePacket(ctag, question, g_now.tv_sec, &response, &age, &pbMessage)); |
1609 | if (cacheHit) { | |
1610 | #ifdef HAVE_PROTOBUF | |
b790ef3d | 1611 | if(luaconfsLocal->protobufServer && (!luaconfsLocal->protobufTaggedOnly || !pbMessage.getAppliedPolicy().empty() || !pbMessage.getPolicyTags().empty())) { |
e1c8a4bb RG |
1612 | Netmask requestorNM(fromaddr, fromaddr.sin4.sin_family == AF_INET ? luaconfsLocal->protobufMaskV4 : luaconfsLocal->protobufMaskV6); |
1613 | const ComboAddress& requestor = requestorNM.getMaskedNetwork(); | |
1614 | pbMessage.update(uniqueId, &requestor, &destaddr, false, dh->id); | |
b40562da | 1615 | pbMessage.setEDNSSubnet(ednssubnet.source, ednssubnet.source.isIpv4() ? luaconfsLocal->protobufMaskV4 : luaconfsLocal->protobufMaskV6); |
58307a85 | 1616 | pbMessage.setQueryTime(g_now.tv_sec, g_now.tv_usec); |
02b47f43 RG |
1617 | protobufLogResponse(luaconfsLocal->protobufServer, pbMessage); |
1618 | } | |
d9d3f9c1 | 1619 | #endif /* HAVE_PROTOBUF */ |
49a3500d | 1620 | if(!g_quiet) |
1621 | L<<Logger::Notice<<t_id<< " question answered from packet cache tag="<<ctag<<" from "<<fromaddr.toString()<<endl; | |
8f7473d7 | 1622 | |
1bc3c142 BH |
1623 | g_stats.packetCacheHits++; |
1624 | SyncRes::s_queries++; | |
1625 | ageDNSPacket(response, age); | |
b71b60ee | 1626 | struct msghdr msgh; |
1627 | struct iovec iov; | |
1628 | char cbuf[256]; | |
1629 | fillMSGHdr(&msgh, &iov, cbuf, 0, (char*)response.c_str(), response.length(), const_cast<ComboAddress*>(&fromaddr)); | |
2c0af54f PD |
1630 | msgh.msg_control=NULL; |
1631 | ||
cbc03320 | 1632 | if(g_fromtosockets.count(fd)) { |
fbe2a2e0 | 1633 | addCMsgSrcAddr(&msgh, cbuf, &destaddr, 0); |
b71b60ee | 1634 | } |
cbc03320 | 1635 | if(sendmsg(fd, &msgh, 0) < 0 && g_logCommonErrors) |
1636 | L<<Logger::Warning<<"Sending UDP reply to client "<<fromaddr.toStringWithPort()<<" failed with: "<<strerror(errno)<<endl; | |
b71b60ee | 1637 | |
97bee66d | 1638 | if(response.length() >= sizeof(struct dnsheader)) { |
dd079764 RG |
1639 | struct dnsheader tmpdh; |
1640 | memcpy(&tmpdh, response.c_str(), sizeof(tmpdh)); | |
1641 | updateResponseStats(tmpdh.rcode, fromaddr, response.length(), 0, 0); | |
97bee66d | 1642 | } |
08f3f638 | 1643 | g_stats.avgLatencyUsec=(1-1.0/g_latencyStatSize)*g_stats.avgLatencyUsec + 0.0; // we assume 0 usec |
1bc3c142 BH |
1644 | return 0; |
1645 | } | |
3ddb9247 | 1646 | } |
1bc3c142 BH |
1647 | catch(std::exception& e) { |
1648 | L<<Logger::Error<<"Error processing or aging answer packet: "<<e.what()<<endl; | |
1649 | return 0; | |
1650 | } | |
3ddb9247 | 1651 | |
4ea94941 | 1652 | if(t_pdl->get()) { |
93f0da94 | 1653 | if((*t_pdl)->ipfilter(fromaddr, destaddr, *dh)) { |
4ea94941 | 1654 | if(!g_quiet) |
1655 | L<<Logger::Notice<<t_id<<" ["<<MT->getTid()<<"/"<<MT->numProcesses()<<"] DROPPED question from "<<fromaddr.toStringWithPort()<<" based on policy"<<endl; | |
1656 | g_stats.policyDrops++; | |
1657 | return 0; | |
1658 | } | |
1659 | } | |
1660 | ||
1bc3c142 | 1661 | if(MT->numProcesses() > g_maxMThreads) { |
461df9d2 | 1662 | if(!g_quiet) |
854d44e3 | 1663 | L<<Logger::Notice<<t_id<<" ["<<MT->getTid()<<"/"<<MT->numProcesses()<<"] DROPPED question from "<<fromaddr.toStringWithPort()<<", over capacity"<<endl; |
461df9d2 | 1664 | |
1bc3c142 BH |
1665 | g_stats.overCapacityDrops++; |
1666 | return 0; | |
1667 | } | |
3ddb9247 | 1668 | |
1bc3c142 BH |
1669 | DNSComboWriter* dc = new DNSComboWriter(question.c_str(), question.size(), g_now); |
1670 | dc->setSocket(fd); | |
49a3500d | 1671 | dc->d_tag=ctag; |
1672 | dc->d_query = question; | |
1bc3c142 | 1673 | dc->setRemote(&fromaddr); |
b71b60ee | 1674 | dc->setLocal(destaddr); |
1bc3c142 | 1675 | dc->d_tcp=false; |
02b47f43 | 1676 | dc->d_policyTags = policyTags; |
05c74122 | 1677 | dc->d_data = data; |
b40562da RG |
1678 | dc->d_ecsFound = ecsFound; |
1679 | dc->d_ecsParsed = ecsParsed; | |
1680 | dc->d_ednssubnet = ednssubnet; | |
aa7929a3 | 1681 | #ifdef HAVE_PROTOBUF |
4898a348 | 1682 | if (luaconfsLocal->protobufServer || luaconfsLocal->outgoingProtobufServer) { |
d9d3f9c1 RG |
1683 | dc->d_uuid = uniqueId; |
1684 | } | |
aa7929a3 RG |
1685 | #endif |
1686 | ||
1bc3c142 BH |
1687 | MT->makeThread(startDoResolve, (void*) dc); // deletes dc |
1688 | return 0; | |
3ddb9247 PD |
1689 | } |
1690 | ||
b71b60ee | 1691 | |
d8f6d49f | 1692 | void handleNewUDPQuestion(int fd, FDMultiplexer::funcparam_t& var) |
5db529f8 | 1693 | { |
a683e8bd | 1694 | ssize_t len; |
5db529f8 BH |
1695 | char data[1500]; |
1696 | ComboAddress fromaddr; | |
b71b60ee | 1697 | struct msghdr msgh; |
1698 | struct iovec iov; | |
1699 | char cbuf[256]; | |
1700 | ||
1701 | fromaddr.sin6.sin6_family=AF_INET6; // this makes sure fromaddr is big enough | |
1702 | fillMSGHdr(&msgh, &iov, cbuf, sizeof(cbuf), data, sizeof(data), &fromaddr); | |
1703 | ||
3ddb9247 | 1704 | for(;;) |
b71b60ee | 1705 | if((len=recvmsg(fd, &msgh, 0)) >= 0) { |
92011b8f | 1706 | if(t_remotes) |
1707 | t_remotes->push_back(fromaddr); | |
b23b8614 | 1708 | |
49a699c4 | 1709 | if(t_allowFrom && !t_allowFrom->match(&fromaddr)) { |
3ddb9247 | 1710 | if(!g_quiet) |
4957a608 | 1711 | L<<Logger::Error<<"["<<MT->getTid()<<"] dropping UDP query from "<<fromaddr.toString()<<", address not matched by allow-from"<<endl; |
2914b022 | 1712 | |
5db529f8 | 1713 | g_stats.unauthorizedUDP++; |
a9af3782 | 1714 | return; |
5db529f8 | 1715 | } |
15c01deb | 1716 | BOOST_STATIC_ASSERT(offsetof(sockaddr_in, sin_port) == offsetof(sockaddr_in6, sin6_port)); |
81859ba5 | 1717 | if(!fromaddr.sin4.sin_port) { // also works for IPv6 |
3ddb9247 | 1718 | if(!g_quiet) |
81859ba5 | 1719 | L<<Logger::Error<<"["<<MT->getTid()<<"] dropping UDP query from "<<fromaddr.toStringWithPort()<<", can't deal with port 0"<<endl; |
1720 | ||
1721 | g_stats.clientParseError++; // not quite the best place to put it, but needs to go somewhere | |
1722 | return; | |
1723 | } | |
5db529f8 | 1724 | try { |
b23b8614 | 1725 | dnsheader* dh=(dnsheader*)data; |
3ddb9247 | 1726 | |
b23b8614 | 1727 | if(dh->qr) { |
048f5db6 | 1728 | g_stats.ignoredCount++; |
4957a608 BH |
1729 | if(g_logCommonErrors) |
1730 | L<<Logger::Error<<"Ignoring answer from "<<fromaddr.toString()<<" on server socket!"<<endl; | |
5db529f8 | 1731 | } |
3abcdab2 | 1732 | else if(dh->opcode) { |
048f5db6 | 1733 | g_stats.ignoredCount++; |
3abcdab2 PD |
1734 | if(g_logCommonErrors) |
1735 | L<<Logger::Error<<"Ignoring non-query opcode "<<dh->opcode<<" from "<<fromaddr.toString()<<" on server socket!"<<endl; | |
1736 | } | |
5db529f8 | 1737 | else { |
a683e8bd | 1738 | string question(data, (size_t)len); |
b71b60ee | 1739 | struct timeval tv={0,0}; |
1740 | HarvestTimestamp(&msgh, &tv); | |
1741 | ComboAddress dest; | |
c3cecd36 | 1742 | memset(&dest, 0, sizeof(dest)); // this makes sure we ignore this address if not returned by recvmsg above |
a6147cd2 | 1743 | auto loc = rplookup(g_listenSocketsAddresses, fd); |
1744 | if(HarvestDestinationAddress(&msgh, &dest)) { | |
1745 | // but.. need to get port too | |
1746 | if(loc) | |
1747 | dest.sin4.sin_port = loc->sin4.sin_port; | |
1748 | } | |
1749 | else { | |
1750 | if(loc) { | |
1751 | dest = *loc; | |
1752 | } | |
1753 | else { | |
1754 | dest.sin4.sin_family = fromaddr.sin4.sin_family; | |
a683e8bd RG |
1755 | socklen_t slen = dest.getSocklen(); |
1756 | getsockname(fd, (sockaddr*)&dest, &slen); // if this fails, we're ok with it | |
a6147cd2 | 1757 | } |
1758 | } | |
232f0877 | 1759 | if(g_weDistributeQueries) |
b71b60ee | 1760 | distributeAsyncFunction(question, boost::bind(doProcessUDPQuestion, question, fromaddr, dest, tv, fd)); |
232f0877 | 1761 | else |
b71b60ee | 1762 | doProcessUDPQuestion(question, fromaddr, dest, tv, fd); |
5db529f8 BH |
1763 | } |
1764 | } | |
1765 | catch(MOADNSException& mde) { | |
3ddb9247 | 1766 | g_stats.clientParseError++; |
84e66a59 | 1767 | if(g_logCommonErrors) |
4957a608 | 1768 | L<<Logger::Error<<"Unable to parse packet from remote UDP client "<<fromaddr.toString() <<": "<<mde.what()<<endl; |
5db529f8 | 1769 | } |
0b602819 KM |
1770 | catch(std::runtime_error& e) { |
1771 | g_stats.clientParseError++; | |
1772 | if(g_logCommonErrors) | |
1773 | L<<Logger::Error<<"Unable to parse packet from remote UDP client "<<fromaddr.toString() <<": "<<e.what()<<endl; | |
5db529f8 BH |
1774 | } |
1775 | } | |
ac0e821b BH |
1776 | else { |
1777 | // cerr<<t_id<<" had error: "<<stringerror()<<endl; | |
3ddb9247 | 1778 | if(errno == EAGAIN) |
9326cae1 | 1779 | g_stats.noPacketError++; |
bf3b0cec | 1780 | break; |
ac0e821b | 1781 | } |
5db529f8 BH |
1782 | } |
1783 | ||
1bc3c142 | 1784 | |
5db529f8 BH |
1785 | typedef vector<pair<int, function< void(int, any&) > > > deferredAdd_t; |
1786 | deferredAdd_t deferredAdd; | |
1787 | ||
f28307ad | 1788 | void makeTCPServerSockets() |
9c495589 | 1789 | { |
37d3f960 | 1790 | int fd; |
f28307ad | 1791 | vector<string>locals; |
2e3d8a19 | 1792 | stringtok(locals,::arg()["local-address"]," ,"); |
9c495589 | 1793 | |
f28307ad | 1794 | if(locals.empty()) |
3f81d239 | 1795 | throw PDNSException("No local address specified"); |
3ddb9247 | 1796 | |
f28307ad | 1797 | for(vector<string>::const_iterator i=locals.begin();i!=locals.end();++i) { |
32252594 BH |
1798 | ServiceTuple st; |
1799 | st.port=::arg().asNum("local-port"); | |
1800 | parseService(*i, st); | |
3ddb9247 | 1801 | |
32252594 BH |
1802 | ComboAddress sin; |
1803 | ||
f28307ad | 1804 | memset((char *)&sin,0, sizeof(sin)); |
37d3f960 | 1805 | sin.sin4.sin_family = AF_INET; |
32252594 | 1806 | if(!IpToU32(st.host, (uint32_t*)&sin.sin4.sin_addr.s_addr)) { |
37d3f960 | 1807 | sin.sin6.sin6_family = AF_INET6; |
f71bc087 | 1808 | if(makeIPv6sockaddr(st.host, &sin.sin6) < 0) |
3ddb9247 | 1809 | throw PDNSException("Unable to resolve local address for TCP server on '"+ st.host +"'"); |
37d3f960 BH |
1810 | } |
1811 | ||
1812 | fd=socket(sin.sin6.sin6_family, SOCK_STREAM, 0); | |
3ddb9247 | 1813 | if(fd<0) |
3f81d239 | 1814 | throw PDNSException("Making a TCP server socket for resolver: "+stringerror()); |
f28307ad | 1815 | |
3897b9e1 | 1816 | setCloseOnExec(fd); |
a903b39c | 1817 | |
f28307ad | 1818 | int tmp=1; |
37d3f960 | 1819 | if(setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,(char*)&tmp,sizeof tmp)<0) { |
f28307ad | 1820 | L<<Logger::Error<<"Setsockopt failed for TCP listening socket"<<endl; |
c8ddb7c2 | 1821 | exit(1); |
f28307ad | 1822 | } |
0dfa94ab | 1823 | if(sin.sin6.sin6_family == AF_INET6 && setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &tmp, sizeof(tmp)) < 0) { |
1824 | L<<Logger::Error<<"Failed to set IPv6 socket to IPv6 only, continuing anyhow: "<<strerror(errno)<<endl; | |
1825 | } | |
1826 | ||
c8ddb7c2 | 1827 | #ifdef TCP_DEFER_ACCEPT |
37d3f960 BH |
1828 | if(setsockopt(fd, SOL_TCP,TCP_DEFER_ACCEPT,(char*)&tmp,sizeof tmp) >= 0) { |
1829 | if(i==locals.begin()) | |
4957a608 | 1830 | L<<Logger::Error<<"Enabled TCP data-ready filter for (slight) DoS protection"<<endl; |
c8ddb7c2 BH |
1831 | } |
1832 | #endif | |
1833 | ||
fec7dd5a SS |
1834 | if( ::arg().mustDo("non-local-bind") ) |
1835 | Utility::setBindAny(AF_INET, fd); | |
1836 | ||
2332f42d | 1837 | #ifdef SO_REUSEPORT |
1838 | if(::arg().mustDo("reuseport")) { | |
1839 | int one=1; | |
1840 | if(setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one)) < 0) | |
1841 | throw PDNSException("SO_REUSEPORT: "+stringerror()); | |
1842 | } | |
1843 | #endif | |
1844 | ||
32252594 | 1845 | sin.sin4.sin_port = htons(st.port); |
a683e8bd | 1846 | socklen_t socklen=sin.sin4.sin_family==AF_INET ? sizeof(sin.sin4) : sizeof(sin.sin6); |
3ddb9247 | 1847 | if (::bind(fd, (struct sockaddr *)&sin, socklen )<0) |
3f81d239 | 1848 | throw PDNSException("Binding TCP server socket for "+ st.host +": "+stringerror()); |
3ddb9247 | 1849 | |
3897b9e1 | 1850 | setNonBlocking(fd); |
49a699c4 | 1851 | setSocketSendBuffer(fd, 65000); |
37d3f960 | 1852 | listen(fd, 128); |
5db529f8 | 1853 | deferredAdd.push_back(make_pair(fd, handleNewTCPQuestion)); |
c2136bf0 | 1854 | g_tcpListenSockets.push_back(fd); |
84433b79 | 1855 | // we don't need to update g_listenSocketsAddresses since it doesn't work for TCP/IP: |
1856 | // - fd is not that which we know here, but returned from accept() | |
3ddb9247 | 1857 | if(sin.sin4.sin_family == AF_INET) |
32252594 | 1858 | L<<Logger::Error<<"Listening for TCP queries on "<< sin.toString() <<":"<<st.port<<endl; |
aa136564 | 1859 | else |
32252594 | 1860 | L<<Logger::Error<<"Listening for TCP queries on ["<< sin.toString() <<"]:"<<st.port<<endl; |
f28307ad | 1861 | } |
9c495589 BH |
1862 | } |
1863 | ||
f28307ad | 1864 | void makeUDPServerSockets() |
288f4aa9 | 1865 | { |
fec7dd5a | 1866 | int one=1; |
f28307ad | 1867 | vector<string>locals; |
2e3d8a19 | 1868 | stringtok(locals,::arg()["local-address"]," ,"); |
288f4aa9 | 1869 | |
f28307ad | 1870 | if(locals.empty()) |
3f81d239 | 1871 | throw PDNSException("No local address specified"); |
3ddb9247 | 1872 | |
f28307ad | 1873 | for(vector<string>::const_iterator i=locals.begin();i!=locals.end();++i) { |
32252594 BH |
1874 | ServiceTuple st; |
1875 | st.port=::arg().asNum("local-port"); | |
1876 | parseService(*i, st); | |
1877 | ||
37d3f960 | 1878 | ComboAddress sin; |
996c89cc | 1879 | |
37d3f960 BH |
1880 | memset(&sin, 0, sizeof(sin)); |
1881 | sin.sin4.sin_family = AF_INET; | |
32252594 | 1882 | if(!IpToU32(st.host.c_str() , (uint32_t*)&sin.sin4.sin_addr.s_addr)) { |
37d3f960 | 1883 | sin.sin6.sin6_family = AF_INET6; |
f71bc087 | 1884 | if(makeIPv6sockaddr(st.host, &sin.sin6) < 0) |
3ddb9247 | 1885 | throw PDNSException("Unable to resolve local address for UDP server on '"+ st.host +"'"); |
37d3f960 | 1886 | } |
3ddb9247 | 1887 | |
bb4bdbaf | 1888 | int fd=socket(sin.sin4.sin_family, SOCK_DGRAM, 0); |
d3b4137e | 1889 | if(fd < 0) { |
3f81d239 | 1890 | throw PDNSException("Making a UDP server socket for resolver: "+netstringerror()); |
d3b4137e | 1891 | } |
915b0c39 AT |
1892 | if (!setSocketTimestamps(fd)) |
1893 | L<<Logger::Warning<<"Unable to enable timestamp reporting for socket"<<endl; | |
0dfa94ab | 1894 | |
b71b60ee | 1895 | if(IsAnyAddress(sin)) { |
cbc03320 | 1896 | if(sin.sin4.sin_family == AF_INET) |
1897 | if(!setsockopt(fd, IPPROTO_IP, GEN_IP_PKTINFO, &one, sizeof(one))) // linux supports this, so why not - might fail on other systems | |
1898 | g_fromtosockets.insert(fd); | |
757d3179 | 1899 | #ifdef IPV6_RECVPKTINFO |
cbc03320 | 1900 | if(sin.sin4.sin_family == AF_INET6) |
1901 | if(!setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &one, sizeof(one))) | |
1902 | g_fromtosockets.insert(fd); | |
757d3179 | 1903 | #endif |
0dfa94ab | 1904 | if(sin.sin6.sin6_family == AF_INET6 && setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one)) < 0) { |
1905 | L<<Logger::Error<<"Failed to set IPv6 socket to IPv6 only, continuing anyhow: "<<strerror(errno)<<endl; | |
1906 | } | |
b71b60ee | 1907 | } |
fec7dd5a SS |
1908 | if( ::arg().mustDo("non-local-bind") ) |
1909 | Utility::setBindAny(AF_INET6, fd); | |
1910 | ||
3897b9e1 | 1911 | setCloseOnExec(fd); |
a903b39c | 1912 | |
4e9a20e6 | 1913 | setSocketReceiveBuffer(fd, 250000); |
32252594 | 1914 | sin.sin4.sin_port = htons(st.port); |
37d3f960 | 1915 | |
2332f42d | 1916 | |
1917 | #ifdef SO_REUSEPORT | |
1918 | if(::arg().mustDo("reuseport")) { | |
2332f42d | 1919 | if(setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one)) < 0) |
1920 | throw PDNSException("SO_REUSEPORT: "+stringerror()); | |
1921 | } | |
1922 | #endif | |
a683e8bd | 1923 | socklen_t socklen=sin.getSocklen(); |
3ddb9247 | 1924 | if (::bind(fd, (struct sockaddr *)&sin, socklen)<0) |
335da0ba | 1925 | throw PDNSException("Resolver binding to server socket on port "+ std::to_string(st.port) +" for "+ st.host+": "+stringerror()); |
3ddb9247 | 1926 | |
3897b9e1 | 1927 | setNonBlocking(fd); |
c2136bf0 | 1928 | |
0aaecd50 | 1929 | deferredAdd.push_back(make_pair(fd, handleNewUDPQuestion)); |
40a3dd64 | 1930 | g_listenSocketsAddresses[fd]=sin; // this is written to only from the startup thread, not from the workers |
3ddb9247 | 1931 | if(sin.sin4.sin_family == AF_INET) |
32252594 | 1932 | L<<Logger::Error<<"Listening for UDP queries on "<< sin.toString() <<":"<<st.port<<endl; |
aa136564 | 1933 | else |
32252594 | 1934 | L<<Logger::Error<<"Listening for UDP queries on ["<< sin.toString() <<"]:"<<st.port<<endl; |
f28307ad | 1935 | } |
c836dc19 | 1936 | } |
caa6eefa | 1937 | |
9c495589 | 1938 | |
c836dc19 BH |
1939 | void daemonize(void) |
1940 | { | |
1941 | if(fork()) | |
1942 | exit(0); // bye bye | |
3ddb9247 PD |
1943 | |
1944 | setsid(); | |
c836dc19 | 1945 | |
27a5ead5 | 1946 | int i=open("/dev/null",O_RDWR); /* open stdin */ |
3ddb9247 | 1947 | if(i < 0) |
27a5ead5 BH |
1948 | L<<Logger::Critical<<"Unable to open /dev/null: "<<stringerror()<<endl; |
1949 | else { | |
1950 | dup2(i,0); /* stdin */ | |
1951 | dup2(i,1); /* stderr */ | |
1952 | dup2(i,2); /* stderr */ | |
1953 | close(i); | |
1954 | } | |
288f4aa9 | 1955 | } |
caa6eefa | 1956 | |
cc59bce6 | 1957 | AtomicCounter counter; |
c75a6a9e BH |
1958 | bool statsWanted; |
1959 | ||
1960 | void usr1Handler(int) | |
1961 | { | |
1962 | statsWanted=true; | |
1963 | } | |
ae1b2e98 | 1964 | |
9170fbaf BH |
1965 | void usr2Handler(int) |
1966 | { | |
f1f34cc2 | 1967 | g_quiet= !g_quiet; |
1968 | SyncRes::setDefaultLogMode(g_quiet ? SyncRes::LogNone : SyncRes::Log); | |
1969 | ::arg().set("quiet")=g_quiet ? "" : "no"; | |
9170fbaf BH |
1970 | } |
1971 | ||
c75a6a9e BH |
1972 | void doStats(void) |
1973 | { | |
16beeaa4 BH |
1974 | static time_t lastOutputTime; |
1975 | static uint64_t lastQueryCount; | |
d299d4f5 | 1976 | |
1977 | uint64_t cacheHits = broadcastAccFunction<uint64_t>(pleaseGetCacheHits); | |
1978 | uint64_t cacheMisses = broadcastAccFunction<uint64_t>(pleaseGetCacheMisses); | |
3ddb9247 | 1979 | |
d299d4f5 | 1980 | if(g_stats.qcounter && (cacheHits + cacheMisses) && SyncRes::s_queries && SyncRes::s_outqueries) { |
bd301954 | 1981 | L<<Logger::Notice<<"stats: "<<g_stats.qcounter<<" questions, "<< |
3427fa8a BH |
1982 | broadcastAccFunction<uint64_t>(pleaseGetCacheSize)<< " cache entries, "<< |
1983 | broadcastAccFunction<uint64_t>(pleaseGetNegCacheSize)<<" negative entries, "<< | |
3ddb9247 PD |
1984 | (int)((cacheHits*100.0)/(cacheHits+cacheMisses))<<"% cache hits"<<endl; |
1985 | ||
bd301954 | 1986 | L<<Logger::Notice<<"stats: throttle map: " |
3427fa8a | 1987 | << broadcastAccFunction<uint64_t>(pleaseGetThrottleSize) <<", ns speeds: " |
3ddb9247 | 1988 | << broadcastAccFunction<uint64_t>(pleaseGetNsSpeedsSize)<<endl; |
bd301954 JB |
1989 | L<<Logger::Notice<<"stats: outpacket/query ratio "<<(int)(SyncRes::s_outqueries*100.0/SyncRes::s_queries)<<"%"; |
1990 | L<<Logger::Notice<<", "<<(int)(SyncRes::s_throttledqueries*100.0/(SyncRes::s_outqueries+SyncRes::s_throttledqueries))<<"% throttled, " | |
525b8a7c | 1991 | <<SyncRes::s_nodelegated<<" no-delegation drops"<<endl; |
bd301954 | 1992 | L<<Logger::Notice<<"stats: "<<SyncRes::s_tcpoutqueries<<" outgoing tcp connections, "<< |
3427fa8a | 1993 | broadcastAccFunction<uint64_t>(pleaseGetConcurrentQueries)<<" queries running, "<<SyncRes::s_outgoingtimeouts<<" outgoing timeouts"<<endl; |
81883dcc | 1994 | |
bd301954 | 1995 | //L<<Logger::Notice<<"stats: "<<g_stats.ednsPingMatches<<" ping matches, "<<g_stats.ednsPingMismatches<<" mismatches, "<< |
16beeaa4 | 1996 | //g_stats.noPingOutQueries<<" outqueries w/o ping, "<< g_stats.noEdnsOutQueries<<" w/o EDNS"<<endl; |
3ddb9247 | 1997 | |
bd301954 | 1998 | L<<Logger::Notice<<"stats: " << broadcastAccFunction<uint64_t>(pleaseGetPacketCacheSize) << |
16beeaa4 | 1999 | " packet cache entries, "<<(int)(100.0*broadcastAccFunction<uint64_t>(pleaseGetPacketCacheHits)/SyncRes::s_queries) << "% packet cache hits"<<endl; |
3ddb9247 | 2000 | |
16beeaa4 BH |
2001 | time_t now = time(0); |
2002 | if(lastOutputTime && lastQueryCount && now != lastOutputTime) { | |
bd301954 | 2003 | L<<Logger::Notice<<"stats: "<< (SyncRes::s_queries - lastQueryCount) / (now - lastOutputTime) <<" qps (average over "<< (now - lastOutputTime) << " seconds)"<<endl; |
16beeaa4 BH |
2004 | } |
2005 | lastOutputTime = now; | |
2006 | lastQueryCount = SyncRes::s_queries; | |
c75a6a9e | 2007 | } |
3ddb9247 | 2008 | else if(statsWanted) |
bd301954 | 2009 | L<<Logger::Notice<<"stats: no stats yet!"<<endl; |
7becf07f | 2010 | |
c75a6a9e BH |
2011 | statsWanted=false; |
2012 | } | |
c836dc19 | 2013 | |
29f0b1ce | 2014 | static void houseKeeping(void *) |
c836dc19 | 2015 | { |
d67620e4 | 2016 | static __thread time_t last_stat, last_rootupdate, last_prune, last_secpoll; |
8baca3fa | 2017 | static __thread int cleanCounter=0; |
cc59bce6 | 2018 | static __thread bool s_running; // houseKeeping can get suspended in secpoll, and be restarted, which makes us do duplicate work |
2019 | try { | |
2020 | if(s_running) | |
2021 | return; | |
2022 | s_running=true; | |
3ddb9247 | 2023 | |
cc59bce6 | 2024 | struct timeval now; |
2025 | Utility::gettimeofday(&now, 0); | |
3ddb9247 PD |
2026 | |
2027 | if(now.tv_sec - last_prune > (time_t)(5 + t_id)) { | |
cc59bce6 | 2028 | DTime dt; |
2029 | dt.setTimeval(now); | |
2030 | t_RC->doPrune(); // this function is local to a thread, so fine anyhow | |
f8f243b0 | 2031 | t_packetCache->doPruneTo(::arg().asNum("max-packetcache-entries") / g_numWorkerThreads); |
3ddb9247 | 2032 | |
f8f243b0 | 2033 | pruneCollection(t_sstorage->negcache, ::arg().asNum("max-cache-entries") / (g_numWorkerThreads * 10), 200); |
3ddb9247 | 2034 | |
cc59bce6 | 2035 | if(!((cleanCounter++)%40)) { // this is a full scan! |
2036 | time_t limit=now.tv_sec-300; | |
2037 | for(SyncRes::nsspeeds_t::iterator i = t_sstorage->nsSpeeds.begin() ; i!= t_sstorage->nsSpeeds.end(); ) | |
2038 | if(i->second.stale(limit)) | |
2039 | t_sstorage->nsSpeeds.erase(i++); | |
2040 | else | |
2041 | ++i; | |
2042 | } | |
2043 | last_prune=time(0); | |
d67620e4 | 2044 | } |
3ddb9247 | 2045 | |
cc59bce6 | 2046 | if(now.tv_sec - last_rootupdate > 7200) { |
7836f7b4 PL |
2047 | int res = getRootNS(); |
2048 | if (!res) | |
2049 | last_rootupdate=now.tv_sec; | |
cc59bce6 | 2050 | } |
3ddb9247 | 2051 | |
cc59bce6 | 2052 | if(!t_id) { |
3ddb9247 | 2053 | if(now.tv_sec - last_stat >= 1800) { |
cc59bce6 | 2054 | doStats(); |
2055 | last_stat=time(0); | |
2056 | } | |
3ddb9247 | 2057 | |
cc59bce6 | 2058 | if(now.tv_sec - last_secpoll >= 3600) { |
2059 | try { | |
2060 | doSecPoll(&last_secpoll); | |
2061 | } | |
2062 | catch(...) {} | |
18b73338 | 2063 | } |
d67620e4 | 2064 | } |
cc59bce6 | 2065 | s_running=false; |
d67620e4 | 2066 | } |
cc59bce6 | 2067 | catch(PDNSException& ae) |
2068 | { | |
2069 | s_running=false; | |
2070 | L<<Logger::Error<<"Fatal error in housekeeping thread: "<<ae.reason<<endl; | |
2071 | throw; | |
2072 | } | |
779828c4 | 2073 | } |
d6d5dea7 | 2074 | |
49a699c4 BH |
2075 | void makeThreadPipes() |
2076 | { | |
c3828c03 | 2077 | for(unsigned int n=0; n < g_numThreads; ++n) { |
49a699c4 BH |
2078 | struct ThreadPipeSet tps; |
2079 | int fd[2]; | |
2080 | if(pipe(fd) < 0) | |
2081 | unixDie("Creating pipe for inter-thread communications"); | |
3ddb9247 | 2082 | |
49a699c4 BH |
2083 | tps.readToThread = fd[0]; |
2084 | tps.writeToThread = fd[1]; | |
3ddb9247 | 2085 | |
49a699c4 BH |
2086 | if(pipe(fd) < 0) |
2087 | unixDie("Creating pipe for inter-thread communications"); | |
2088 | tps.readFromThread = fd[0]; | |
2089 | tps.writeFromThread = fd[1]; | |
3ddb9247 | 2090 | |
49a699c4 BH |
2091 | g_pipes.push_back(tps); |
2092 | } | |
2093 | } | |
2094 | ||
00c9b8c1 BH |
2095 | struct ThreadMSG |
2096 | { | |
2097 | pipefunc_t func; | |
2098 | bool wantAnswer; | |
2099 | }; | |
2100 | ||
49a699c4 BH |
2101 | void broadcastFunction(const pipefunc_t& func, bool skipSelf) |
2102 | { | |
49a699c4 | 2103 | unsigned int n = 0; |
1dc8f4d0 | 2104 | for(ThreadPipeSet& tps : g_pipes) |
49a699c4 BH |
2105 | { |
2106 | if(n++ == t_id) { | |
2107 | if(!skipSelf) | |
2108 | func(); // don't write to ourselves! | |
2109 | continue; | |
2110 | } | |
3ddb9247 | 2111 | |
00c9b8c1 BH |
2112 | ThreadMSG* tmsg = new ThreadMSG(); |
2113 | tmsg->func = func; | |
2114 | tmsg->wantAnswer = true; | |
b841314c RG |
2115 | if(write(tps.writeToThread, &tmsg, sizeof(tmsg)) != sizeof(tmsg)) { |
2116 | delete tmsg; | |
49a699c4 | 2117 | unixDie("write to thread pipe returned wrong size or error"); |
b841314c | 2118 | } |
3ddb9247 | 2119 | |
49a699c4 BH |
2120 | string* resp; |
2121 | if(read(tps.readFromThread, &resp, sizeof(resp)) != sizeof(resp)) | |
2122 | unixDie("read from thread pipe returned wrong size or error"); | |
3ddb9247 | 2123 | |
49a699c4 BH |
2124 | if(resp) { |
2125 | // cerr <<"got response: " << *resp << endl; | |
2126 | delete resp; | |
2127 | } | |
2128 | } | |
2129 | } | |
06ea9015 | 2130 | |
2fafb640 | 2131 | static uint32_t g_disthashseed; |
8171ab83 | 2132 | void distributeAsyncFunction(const string& packet, const pipefunc_t& func) |
00c9b8c1 | 2133 | { |
8171ab83 | 2134 | unsigned int hash = hashQuestion(packet.c_str(), packet.length(), g_disthashseed); |
06ea9015 | 2135 | unsigned int target = 1 + (hash % (g_pipes.size()-1)); |
2136 | ||
00c9b8c1 BH |
2137 | if(target == t_id) { |
2138 | func(); | |
2139 | return; | |
2140 | } | |
3ddb9247 | 2141 | ThreadPipeSet& tps = g_pipes[target]; |
00c9b8c1 BH |
2142 | ThreadMSG* tmsg = new ThreadMSG(); |
2143 | tmsg->func = func; | |
2144 | tmsg->wantAnswer = false; | |
3ddb9247 | 2145 | |
b841314c RG |
2146 | if(write(tps.writeToThread, &tmsg, sizeof(tmsg)) != sizeof(tmsg)) { |
2147 | delete tmsg; | |
3ddb9247 | 2148 | unixDie("write to thread pipe returned wrong size or error"); |
b841314c | 2149 | } |
00c9b8c1 | 2150 | } |
3427fa8a | 2151 | |
49a699c4 BH |
2152 | void handlePipeRequest(int fd, FDMultiplexer::funcparam_t& var) |
2153 | { | |
00c9b8c1 | 2154 | ThreadMSG* tmsg; |
3ddb9247 PD |
2155 | |
2156 | if(read(fd, &tmsg, sizeof(tmsg)) != sizeof(tmsg)) { // fd == readToThread | |
49a699c4 BH |
2157 | unixDie("read from thread pipe returned wrong size or error"); |
2158 | } | |
3ddb9247 | 2159 | |
2f22827a | 2160 | void *resp=0; |
2161 | try { | |
2162 | resp = tmsg->func(); | |
2163 | } | |
2164 | catch(std::exception& e) { | |
6d2010a8 | 2165 | if(g_logCommonErrors) |
2166 | L<<Logger::Error<<"PIPE function we executed created exception: "<<e.what()<<endl; // but what if they wanted an answer.. we send 0 | |
2f22827a | 2167 | } |
2168 | catch(PDNSException& e) { | |
6d2010a8 | 2169 | if(g_logCommonErrors) |
2170 | L<<Logger::Error<<"PIPE function we executed created PDNS exception: "<<e.reason<<endl; // but what if they wanted an answer.. we send 0 | |
2f22827a | 2171 | } |
00c9b8c1 BH |
2172 | if(tmsg->wantAnswer) |
2173 | if(write(g_pipes[t_id].writeFromThread, &resp, sizeof(resp)) != sizeof(resp)) | |
2174 | unixDie("write to thread pipe returned wrong size or error"); | |
3ddb9247 | 2175 | |
00c9b8c1 | 2176 | delete tmsg; |
49a699c4 | 2177 | } |
09e6702a | 2178 | |
13034931 BH |
2179 | template<class T> void *voider(const boost::function<T*()>& func) |
2180 | { | |
2181 | return func(); | |
2182 | } | |
2183 | ||
b3b5459d BH |
2184 | vector<ComboAddress>& operator+=(vector<ComboAddress>&a, const vector<ComboAddress>& b) |
2185 | { | |
2186 | a.insert(a.end(), b.begin(), b.end()); | |
2187 | return a; | |
2188 | } | |
2189 | ||
92011b8f | 2190 | vector<pair<string, uint16_t> >& operator+=(vector<pair<string, uint16_t> >&a, const vector<pair<string, uint16_t> >& b) |
2191 | { | |
2192 | a.insert(a.end(), b.begin(), b.end()); | |
2193 | return a; | |
2194 | } | |
2195 | ||
3ddb9247 PD |
2196 | vector<pair<DNSName, uint16_t> >& operator+=(vector<pair<DNSName, uint16_t> >&a, const vector<pair<DNSName, uint16_t> >& b) |
2197 | { | |
2198 | a.insert(a.end(), b.begin(), b.end()); | |
2199 | return a; | |
2200 | } | |
2201 | ||
92011b8f | 2202 | |
13034931 | 2203 | template<class T> T broadcastAccFunction(const boost::function<T*()>& func, bool skipSelf) |
3427fa8a BH |
2204 | { |
2205 | unsigned int n = 0; | |
2206 | T ret=T(); | |
1dc8f4d0 | 2207 | for(ThreadPipeSet& tps : g_pipes) |
3427fa8a BH |
2208 | { |
2209 | if(n++ == t_id) { | |
2210 | if(!skipSelf) { | |
2211 | T* resp = (T*)func(); // don't write to ourselves! | |
2212 | if(resp) { | |
2213 | //~ cerr <<"got direct: " << *resp << endl; | |
2214 | ret += *resp; | |
2215 | delete resp; | |
2216 | } | |
2217 | } | |
2218 | continue; | |
2219 | } | |
3ddb9247 | 2220 | |
00c9b8c1 BH |
2221 | ThreadMSG* tmsg = new ThreadMSG(); |
2222 | tmsg->func = boost::bind(voider<T>, func); | |
2223 | tmsg->wantAnswer = true; | |
3ddb9247 | 2224 | |
b841314c RG |
2225 | if(write(tps.writeToThread, &tmsg, sizeof(tmsg)) != sizeof(tmsg)) { |
2226 | delete tmsg; | |
3427fa8a | 2227 | unixDie("write to thread pipe returned wrong size or error"); |
b841314c | 2228 | } |
3ddb9247 | 2229 | |
3427fa8a BH |
2230 | T* resp; |
2231 | if(read(tps.readFromThread, &resp, sizeof(resp)) != sizeof(resp)) | |
2232 | unixDie("read from thread pipe returned wrong size or error"); | |
3ddb9247 | 2233 | |
3427fa8a BH |
2234 | if(resp) { |
2235 | //~ cerr <<"got response: " << *resp << endl; | |
2236 | ret += *resp; | |
2237 | delete resp; | |
2238 | } | |
2239 | } | |
2240 | return ret; | |
2241 | } | |
2242 | ||
13034931 BH |
2243 | template string broadcastAccFunction(const boost::function<string*()>& fun, bool skipSelf); // explicit instantiation |
2244 | template uint64_t broadcastAccFunction(const boost::function<uint64_t*()>& fun, bool skipSelf); // explicit instantiation | |
b3b5459d | 2245 | template vector<ComboAddress> broadcastAccFunction(const boost::function<vector<ComboAddress> *()>& fun, bool skipSelf); // explicit instantiation |
3ddb9247 | 2246 | template vector<pair<DNSName,uint16_t> > broadcastAccFunction(const boost::function<vector<pair<DNSName, uint16_t> > *()>& fun, bool skipSelf); // explicit instantiation |
3427fa8a | 2247 | |
d8f6d49f | 2248 | void handleRCC(int fd, FDMultiplexer::funcparam_t& var) |
09e6702a BH |
2249 | { |
2250 | string remote; | |
2251 | string msg=s_rcc.recv(&remote); | |
2252 | RecursorControlParser rcp; | |
2253 | RecursorControlParser::func_t* command; | |
3ddb9247 | 2254 | |
09e6702a | 2255 | string answer=rcp.getAnswer(msg, &command); |
f0f3f0b0 PL |
2256 | |
2257 | // If we are inside a chroot, we need to strip | |
2258 | if (!arg()["chroot"].empty()) { | |
a683e8bd | 2259 | size_t len = arg()["chroot"].length(); |
f0f3f0b0 PL |
2260 | remote = remote.substr(len); |
2261 | } | |
2262 | ||
ab5c053d BH |
2263 | try { |
2264 | s_rcc.send(answer, &remote); | |
2265 | command(); | |
2266 | } | |
fdbf35ac | 2267 | catch(std::exception& e) { |
ab5c053d BH |
2268 | L<<Logger::Error<<"Error dealing with control socket request: "<<e.what()<<endl; |
2269 | } | |
3f81d239 | 2270 | catch(PDNSException& ae) { |
ab5c053d BH |
2271 | L<<Logger::Error<<"Error dealing with control socket request: "<<ae.reason<<endl; |
2272 | } | |
09e6702a BH |
2273 | } |
2274 | ||
d8f6d49f | 2275 | void handleTCPClientReadable(int fd, FDMultiplexer::funcparam_t& var) |
09e6702a | 2276 | { |
0b18b22e | 2277 | PacketID* pident=any_cast<PacketID>(&var); |
667f7e60 | 2278 | // cerr<<"handleTCPClientReadable called for fd "<<fd<<", pident->inNeeded: "<<pident->inNeeded<<", "<<pident->sock->getHandle()<<endl; |
09e6702a | 2279 | |
667f7e60 | 2280 | shared_array<char> buffer(new char[pident->inNeeded]); |
09e6702a | 2281 | |
a683e8bd | 2282 | ssize_t ret=recv(fd, buffer.get(), pident->inNeeded,0); |
09e6702a | 2283 | if(ret > 0) { |
667f7e60 | 2284 | pident->inMSG.append(&buffer[0], &buffer[ret]); |
a683e8bd | 2285 | pident->inNeeded-=(size_t)ret; |
825fa717 | 2286 | if(!pident->inNeeded || pident->inIncompleteOkay) { |
667f7e60 BH |
2287 | // cerr<<"Got entire load of "<<pident->inMSG.size()<<" bytes"<<endl; |
2288 | PacketID pid=*pident; | |
2289 | string msg=pident->inMSG; | |
3ddb9247 | 2290 | |
bb4bdbaf | 2291 | t_fdm->removeReadFD(fd); |
3ddb9247 | 2292 | MT->sendEvent(pid, &msg); |
09e6702a BH |
2293 | } |
2294 | else { | |
667f7e60 | 2295 | // cerr<<"Still have "<<pident->inNeeded<<" left to go"<<endl; |
09e6702a BH |
2296 | } |
2297 | } | |
2298 | else { | |
667f7e60 | 2299 | PacketID tmp=*pident; |
bb4bdbaf | 2300 | t_fdm->removeReadFD(fd); // pident might now be invalid (it isn't, but still) |
09e6702a BH |
2301 | string empty; |
2302 | MT->sendEvent(tmp, &empty); // this conveys error status | |
2303 | } | |
2304 | } | |
2305 | ||
d8f6d49f | 2306 | void handleTCPClientWritable(int fd, FDMultiplexer::funcparam_t& var) |
09e6702a | 2307 | { |
0b18b22e | 2308 | PacketID* pid=any_cast<PacketID>(&var); |
a683e8bd | 2309 | ssize_t ret=send(fd, pid->outMSG.c_str() + pid->outPos, pid->outMSG.size() - pid->outPos,0); |
09e6702a | 2310 | if(ret > 0) { |
a683e8bd | 2311 | pid->outPos+=(ssize_t)ret; |
667f7e60 BH |
2312 | if(pid->outPos==pid->outMSG.size()) { |
2313 | PacketID tmp=*pid; | |
bb4bdbaf | 2314 | t_fdm->removeWriteFD(fd); |
09e6702a BH |
2315 | MT->sendEvent(tmp, &tmp.outMSG); // send back what we sent to convey everything is ok |
2316 | } | |
2317 | } | |
2318 | else { // error or EOF | |
667f7e60 | 2319 | PacketID tmp(*pid); |
bb4bdbaf | 2320 | t_fdm->removeWriteFD(fd); |
09e6702a | 2321 | string sent; |
998a4334 | 2322 | MT->sendEvent(tmp, &sent); // we convey error status by sending empty string |
09e6702a BH |
2323 | } |
2324 | } | |
2325 | ||
34801ab1 BH |
2326 | // resend event to everybody chained onto it |
2327 | void doResends(MT_t::waiters_t::iterator& iter, PacketID resend, const string& content) | |
2328 | { | |
2329 | if(iter->key.chain.empty()) | |
2330 | return; | |
e27e91a8 | 2331 | // cerr<<"doResends called!\n"; |
34801ab1 BH |
2332 | for(PacketID::chain_t::iterator i=iter->key.chain.begin(); i != iter->key.chain.end() ; ++i) { |
2333 | resend.fd=-1; | |
2334 | resend.id=*i; | |
e27e91a8 | 2335 | // cerr<<"\tResending "<<content.size()<<" bytes for fd="<<resend.fd<<" and id="<<resend.id<<endl; |
4665c31e | 2336 | |
34801ab1 BH |
2337 | MT->sendEvent(resend, &content); |
2338 | g_stats.chainResends++; | |
34801ab1 BH |
2339 | } |
2340 | } | |
2341 | ||
d8f6d49f | 2342 | void handleUDPServerResponse(int fd, FDMultiplexer::funcparam_t& var) |
09e6702a | 2343 | { |
600fc20b | 2344 | PacketID pid=any_cast<PacketID>(var); |
a683e8bd | 2345 | ssize_t len; |
e45beeda | 2346 | char data[g_outgoingEDNSBufsize]; |
996c89cc | 2347 | ComboAddress fromaddr; |
09e6702a BH |
2348 | socklen_t addrlen=sizeof(fromaddr); |
2349 | ||
998a4334 | 2350 | len=recvfrom(fd, data, sizeof(data), 0, (sockaddr *)&fromaddr, &addrlen); |
c1da7976 | 2351 | |
a683e8bd | 2352 | if(len < (ssize_t) sizeof(dnsheader)) { |
998a4334 | 2353 | if(len < 0) |
996c89cc | 2354 | ; // cerr<<"Error on fd "<<fd<<": "<<stringerror()<<"\n"; |
09e6702a | 2355 | else { |
3ddb9247 | 2356 | g_stats.serverParseError++; |
09e6702a | 2357 | if(g_logCommonErrors) |
85db02c5 | 2358 | L<<Logger::Error<<"Unable to parse packet from remote UDP server "<< fromaddr.toString() << |
e44d9fa7 | 2359 | ": packet smaller than DNS header"<<endl; |
998a4334 | 2360 | } |
34801ab1 | 2361 | |
49a699c4 | 2362 | t_udpclientsocks->returnSocket(fd); |
34801ab1 BH |
2363 | string empty; |
2364 | ||
2365 | MT_t::waiters_t::iterator iter=MT->d_waiters.find(pid); | |
3ddb9247 | 2366 | if(iter != MT->d_waiters.end()) |
34801ab1 | 2367 | doResends(iter, pid, empty); |
3ddb9247 | 2368 | |
34801ab1 | 2369 | MT->sendEvent(pid, &empty); // this denotes error (does lookup again.. at least L1 will be hot) |
998a4334 | 2370 | return; |
3ddb9247 | 2371 | } |
998a4334 BH |
2372 | |
2373 | dnsheader dh; | |
2374 | memcpy(&dh, data, sizeof(dh)); | |
3ddb9247 | 2375 | |
6da3b3ad PD |
2376 | PacketID pident; |
2377 | pident.remote=fromaddr; | |
2378 | pident.id=dh.id; | |
2379 | pident.fd=fd; | |
34801ab1 | 2380 | |
33a928af | 2381 | if(!dh.qr && g_logCommonErrors) { |
854d44e3 | 2382 | L<<Logger::Notice<<"Not taking data from question on outgoing socket from "<< fromaddr.toStringWithPort() <<endl; |
6da3b3ad PD |
2383 | } |
2384 | ||
2385 | if(!dh.qdcount || // UPC, Nominum, very old BIND on FormErr, NSD | |
2386 | !dh.qr) { // one weird server | |
2387 | pident.domain.clear(); | |
2388 | pident.type = 0; | |
2389 | } | |
2390 | else { | |
2391 | try { | |
0b31e67e | 2392 | if(len > 12) |
2393 | pident.domain=DNSName(data, len, 12, false, &pident.type); // don't copy this from above - we need to do the actual read | |
6da3b3ad PD |
2394 | } |
2395 | catch(std::exception& e) { | |
2396 | g_stats.serverParseError++; // won't be fed to lwres.cc, so we have to increment | |
0b31e67e | 2397 | L<<Logger::Warning<<"Error in packet from remote nameserver "<< fromaddr.toStringWithPort() << ": "<<e.what() << endl; |
6da3b3ad | 2398 | return; |
34801ab1 | 2399 | } |
6da3b3ad PD |
2400 | } |
2401 | string packet; | |
2402 | packet.assign(data, len); | |
34801ab1 | 2403 | |
6da3b3ad PD |
2404 | MT_t::waiters_t::iterator iter=MT->d_waiters.find(pident); |
2405 | if(iter != MT->d_waiters.end()) { | |
2406 | doResends(iter, pident, packet); | |
2407 | } | |
c1da7976 | 2408 | |
6da3b3ad | 2409 | retryWithName: |
4957a608 | 2410 | |
6da3b3ad PD |
2411 | if(!MT->sendEvent(pident, &packet)) { |
2412 | // we do a full scan for outstanding queries on unexpected answers. not too bad since we only accept them on the right port number, which is hard enough to guess | |
2413 | for(MT_t::waiters_t::iterator mthread=MT->d_waiters.begin(); mthread!=MT->d_waiters.end(); ++mthread) { | |
2414 | if(pident.fd==mthread->key.fd && mthread->key.remote==pident.remote && mthread->key.type == pident.type && | |
e325f20c | 2415 | pident.domain == mthread->key.domain) { |
6da3b3ad | 2416 | mthread->key.nearMisses++; |
998a4334 | 2417 | } |
6da3b3ad PD |
2418 | |
2419 | // be a bit paranoid here since we're weakening our matching | |
3ddb9247 | 2420 | if(pident.domain.empty() && !mthread->key.domain.empty() && !pident.type && mthread->key.type && |
6da3b3ad PD |
2421 | pident.id == mthread->key.id && mthread->key.remote == pident.remote) { |
2422 | // cerr<<"Empty response, rest matches though, sending to a waiter"<<endl; | |
2423 | pident.domain = mthread->key.domain; | |
2424 | pident.type = mthread->key.type; | |
2425 | goto retryWithName; // note that this only passes on an error, lwres will still reject the packet | |
d4fb76e9 | 2426 | } |
09e6702a | 2427 | } |
6da3b3ad PD |
2428 | g_stats.unexpectedCount++; // if we made it here, it really is an unexpected answer |
2429 | if(g_logCommonErrors) { | |
8a464ee3 | 2430 | L<<Logger::Warning<<"Discarding unexpected packet from "<<fromaddr.toStringWithPort()<<": "<< (pident.domain.empty() ? "<empty>" : pident.domain.toString())<<", "<<pident.type<<", "<<MT->d_waiters.size()<<" waiters"<<endl; |
d8f6d49f | 2431 | } |
09e6702a | 2432 | } |
6da3b3ad PD |
2433 | else if(fd >= 0) { |
2434 | t_udpclientsocks->returnSocket(fd); | |
2435 | } | |
09e6702a BH |
2436 | } |
2437 | ||
1f4abb20 BH |
2438 | FDMultiplexer* getMultiplexer() |
2439 | { | |
2440 | FDMultiplexer* ret; | |
2441 | for(FDMultiplexer::FDMultiplexermap_t::const_iterator i = FDMultiplexer::getMultiplexerMap().begin(); | |
2442 | i != FDMultiplexer::getMultiplexerMap().end(); ++i) { | |
2443 | try { | |
2444 | ret=i->second(); | |
1f4abb20 BH |
2445 | return ret; |
2446 | } | |
98d0ee4a | 2447 | catch(FDMultiplexerException &fe) { |
0a7f24cb | 2448 | L<<Logger::Error<<"Non-fatal error initializing possible multiplexer ("<<fe.what()<<"), falling back"<<endl; |
98d0ee4a BH |
2449 | } |
2450 | catch(...) { | |
2451 | L<<Logger::Error<<"Non-fatal error initializing possible multiplexer"<<endl; | |
2452 | } | |
1f4abb20 BH |
2453 | } |
2454 | L<<Logger::Error<<"No working multiplexer found!"<<endl; | |
2455 | exit(1); | |
2456 | } | |
2457 | ||
3ddb9247 | 2458 | |
0f39c1a3 | 2459 | string* doReloadLuaScript() |
4485aa35 | 2460 | { |
674cf0f6 | 2461 | string fname= ::arg()["lua-dns-script"]; |
4485aa35 | 2462 | try { |
674cf0f6 BH |
2463 | if(fname.empty()) { |
2464 | t_pdl->reset(); | |
2465 | L<<Logger::Error<<t_id<<" Unloaded current lua script"<<endl; | |
0f39c1a3 | 2466 | return new string("unloaded\n"); |
4485aa35 BH |
2467 | } |
2468 | else { | |
a3e7b735 | 2469 | *t_pdl = shared_ptr<RecursorLua4>(new RecursorLua4(fname)); |
4485aa35 BH |
2470 | } |
2471 | } | |
fdbf35ac | 2472 | catch(std::exception& e) { |
674cf0f6 | 2473 | L<<Logger::Error<<t_id<<" Retaining current script, error from '"<<fname<<"': "<< e.what() <<endl; |
0f39c1a3 | 2474 | return new string("retaining current script, error from '"+fname+"': "+e.what()+"\n"); |
4485aa35 | 2475 | } |
3ddb9247 | 2476 | |
674cf0f6 | 2477 | L<<Logger::Warning<<t_id<<" (Re)loaded lua script from '"<<fname<<"'"<<endl; |
0f39c1a3 | 2478 | return new string("(re)loaded '"+fname+"'\n"); |
4485aa35 BH |
2479 | } |
2480 | ||
49a699c4 BH |
2481 | string doQueueReloadLuaScript(vector<string>::const_iterator begin, vector<string>::const_iterator end) |
2482 | { | |
3ddb9247 | 2483 | if(begin != end) |
49a699c4 | 2484 | ::arg().set("lua-dns-script") = *begin; |
3ddb9247 | 2485 | |
0f39c1a3 | 2486 | return broadcastAccFunction<string>(doReloadLuaScript); |
3ddb9247 | 2487 | } |
49a699c4 | 2488 | |
77499b05 BH |
2489 | string* pleaseUseNewTraceRegex(const std::string& newRegex) |
2490 | try | |
2491 | { | |
2492 | if(newRegex.empty()) { | |
2493 | t_traceRegex->reset(); | |
2494 | return new string("unset\n"); | |
2495 | } | |
2496 | else { | |
2497 | (*t_traceRegex) = shared_ptr<Regex>(new Regex(newRegex)); | |
2498 | return new string("ok\n"); | |
2499 | } | |
2500 | } | |
3f81d239 | 2501 | catch(PDNSException& ae) |
77499b05 BH |
2502 | { |
2503 | return new string(ae.reason+"\n"); | |
2504 | } | |
2505 | ||
2506 | string doTraceRegex(vector<string>::const_iterator begin, vector<string>::const_iterator end) | |
2507 | { | |
2508 | return broadcastAccFunction<string>(boost::bind(pleaseUseNewTraceRegex, begin!=end ? *begin : "")); | |
2509 | } | |
2510 | ||
4e9a20e6 | 2511 | static void checkLinuxIPv6Limits() |
2512 | { | |
2513 | #ifdef __linux__ | |
2514 | string line; | |
2515 | if(readFileIfThere("/proc/sys/net/ipv6/route/max_size", &line)) { | |
335da0ba | 2516 | int lim=std::stoi(line); |
4e9a20e6 | 2517 | if(lim < 16384) { |
36849ff2 | 2518 | L<<Logger::Error<<"If using IPv6, please raise sysctl net.ipv6.route.max_size, currently set to "<<lim<<" which is < 16384"<<endl; |
4e9a20e6 | 2519 | } |
2520 | } | |
2521 | #endif | |
2522 | } | |
36849ff2 | 2523 | static void checkOrFixFDS() |
4e9a20e6 | 2524 | { |
c0063e60 | 2525 | unsigned int availFDs=getFilenumLimit(); |
2526 | unsigned int wantFDs = g_maxMThreads * g_numWorkerThreads +25; // even healthier margin then before | |
2527 | ||
2528 | if(wantFDs > availFDs) { | |
067ad20e | 2529 | unsigned int hardlimit= getFilenumLimit(true); |
2530 | if(hardlimit >= wantFDs) { | |
c0063e60 | 2531 | setFilenumLimit(wantFDs); |
2532 | L<<Logger::Warning<<"Raised soft limit on number of filedescriptors to "<<wantFDs<<" to match max-mthreads and threads settings"<<endl; | |
36849ff2 | 2533 | } |
2534 | else { | |
067ad20e | 2535 | int newval = (hardlimit - 25) / g_numWorkerThreads; |
2536 | L<<Logger::Warning<<"Insufficient number of filedescriptors available for max-mthreads*threads setting! ("<<hardlimit<<" < "<<wantFDs<<"), reducing max-mthreads to "<<newval<<endl; | |
36849ff2 | 2537 | g_maxMThreads = newval; |
067ad20e | 2538 | setFilenumLimit(hardlimit); |
36849ff2 | 2539 | } |
2540 | } | |
4e9a20e6 | 2541 | } |
77499b05 | 2542 | |
bb4bdbaf | 2543 | void* recursorThread(void*); |
51e2144e | 2544 | |
3427fa8a | 2545 | void* pleaseSupplantACLs(NetmaskGroup *ng) |
49a699c4 BH |
2546 | { |
2547 | t_allowFrom = ng; | |
3427fa8a | 2548 | return 0; |
49a699c4 BH |
2549 | } |
2550 | ||
dbd23fc2 BH |
2551 | int g_argc; |
2552 | char** g_argv; | |
2553 | ||
18af64a8 | 2554 | void parseACLs() |
f7c1d4e3 | 2555 | { |
18af64a8 | 2556 | static bool l_initialized; |
3ddb9247 | 2557 | |
49a699c4 | 2558 | if(l_initialized) { // only reload configuration file on second call |
18af64a8 BH |
2559 | string configname=::arg()["config-dir"]+"/recursor.conf"; |
2560 | cleanSlashes(configname); | |
3ddb9247 PD |
2561 | |
2562 | if(!::arg().preParseFile(configname.c_str(), "allow-from-file")) | |
7e818521 | 2563 | throw runtime_error("Unable to re-parse configuration file '"+configname+"'"); |
49a699c4 | 2564 | ::arg().preParseFile(configname.c_str(), "allow-from", LOCAL_NETS); |
242b90e1 | 2565 | ::arg().preParseFile(configname.c_str(), "include-dir"); |
829849d6 AT |
2566 | ::arg().preParse(g_argc, g_argv, "include-dir"); |
2567 | ||
2568 | // then process includes | |
2569 | std::vector<std::string> extraConfigs; | |
242b90e1 AT |
2570 | ::arg().gatherIncludes(extraConfigs); |
2571 | ||
1dc8f4d0 | 2572 | for(const std::string& fn : extraConfigs) { |
7e818521 | 2573 | if(!::arg().preParseFile(fn.c_str(), "allow-from-file", ::arg()["allow-from-file"])) |
2574 | throw runtime_error("Unable to re-parse configuration file include '"+fn+"'"); | |
2575 | if(!::arg().preParseFile(fn.c_str(), "allow-from", ::arg()["allow-from"])) | |
2576 | throw runtime_error("Unable to re-parse configuration file include '"+fn+"'"); | |
829849d6 | 2577 | } |
ca2c884c AT |
2578 | |
2579 | ::arg().preParse(g_argc, g_argv, "allow-from-file"); | |
2580 | ::arg().preParse(g_argc, g_argv, "allow-from"); | |
f27e6356 | 2581 | } |
49a699c4 BH |
2582 | |
2583 | NetmaskGroup* oldAllowFrom = t_allowFrom, *allowFrom=new NetmaskGroup; | |
3ddb9247 | 2584 | |
2c95fc65 BH |
2585 | if(!::arg()["allow-from-file"].empty()) { |
2586 | string line; | |
2c95fc65 BH |
2587 | ifstream ifs(::arg()["allow-from-file"].c_str()); |
2588 | if(!ifs) { | |
3ddb9247 | 2589 | delete allowFrom; |
9c61b9d0 | 2590 | throw runtime_error("Could not open '"+::arg()["allow-from-file"]+"': "+stringerror()); |
2c95fc65 BH |
2591 | } |
2592 | ||
2593 | string::size_type pos; | |
2594 | while(getline(ifs,line)) { | |
2595 | pos=line.find('#'); | |
2596 | if(pos!=string::npos) | |
2597 | line.resize(pos); | |
2598 | trim(line); | |
2599 | if(line.empty()) | |
2600 | continue; | |
2601 | ||
18af64a8 | 2602 | allowFrom->addMask(line); |
2c95fc65 | 2603 | } |
49a699c4 | 2604 | L<<Logger::Warning<<"Done parsing " << allowFrom->size() <<" allow-from ranges from file '"<<::arg()["allow-from-file"]<<"' - overriding 'allow-from' setting"<<endl; |
2c95fc65 BH |
2605 | } |
2606 | else if(!::arg()["allow-from"].empty()) { | |
f7c1d4e3 BH |
2607 | vector<string> ips; |
2608 | stringtok(ips, ::arg()["allow-from"], ", "); | |
3ddb9247 | 2609 | |
f7c1d4e3 BH |
2610 | L<<Logger::Warning<<"Only allowing queries from: "; |
2611 | for(vector<string>::const_iterator i = ips.begin(); i!= ips.end(); ++i) { | |
18af64a8 | 2612 | allowFrom->addMask(*i); |
f7c1d4e3 | 2613 | if(i!=ips.begin()) |
674cf0f6 | 2614 | L<<Logger::Warning<<", "; |
f7c1d4e3 BH |
2615 | L<<Logger::Warning<<*i; |
2616 | } | |
2617 | L<<Logger::Warning<<endl; | |
2618 | } | |
49a699c4 | 2619 | else { |
3ddb9247 | 2620 | if(::arg()["local-address"]!="127.0.0.1" && ::arg().asNum("local-port")==53) |
49a699c4 BH |
2621 | L<<Logger::Error<<"WARNING: Allowing queries from all IP addresses - this can be a security risk!"<<endl; |
2622 | delete allowFrom; | |
2623 | allowFrom = 0; | |
2624 | } | |
3ddb9247 | 2625 | |
49a699c4 | 2626 | g_initialAllowFrom = allowFrom; |
d7dae798 | 2627 | broadcastFunction(boost::bind(pleaseSupplantACLs, allowFrom)); |
49a699c4 | 2628 | delete oldAllowFrom; |
3ddb9247 | 2629 | |
49a699c4 | 2630 | l_initialized = true; |
18af64a8 BH |
2631 | } |
2632 | ||
795215f2 | 2633 | |
9ea28e46 | 2634 | std::unordered_set<DNSName> g_delegationOnly; |
756e82cf | 2635 | static void setupDelegationOnly() |
2636 | { | |
2637 | vector<string> parts; | |
2638 | stringtok(parts, ::arg()["delegation-only"], ", \t"); | |
2639 | for(const auto& p : parts) { | |
9ea28e46 | 2640 | g_delegationOnly.insert(DNSName(p)); |
756e82cf | 2641 | } |
2642 | } | |
795215f2 | 2643 | |
18af64a8 BH |
2644 | int serviceMain(int argc, char*argv[]) |
2645 | { | |
5124de27 | 2646 | L.setName(s_programname); |
18af64a8 | 2647 | L.setLoglevel((Logger::Urgency)(6)); // info and up |
b6cfa948 | 2648 | L.disableSyslog(::arg().mustDo("disable-syslog")); |
18af64a8 BH |
2649 | |
2650 | if(!::arg()["logging-facility"].empty()) { | |
f8499e52 BH |
2651 | int val=logFacilityToLOG(::arg().asNum("logging-facility") ); |
2652 | if(val >= 0) | |
2653 | theL().setFacility(val); | |
18af64a8 BH |
2654 | else |
2655 | L<<Logger::Error<<"Unknown logging facility "<<::arg().asNum("logging-facility") <<endl; | |
2656 | } | |
2657 | ||
ba1a571d | 2658 | showProductVersion(); |
18af64a8 | 2659 | seedRandom(::arg()["entropy-source"]); |
3afde9b2 | 2660 | |
06ea9015 | 2661 | g_disthashseed=dns_random(0xffffffff); |
2662 | ||
b7ef5828 PL |
2663 | checkLinuxIPv6Limits(); |
2664 | try { | |
2665 | vector<string> addrs; | |
2666 | if(!::arg()["query-local-address6"].empty()) { | |
2667 | SyncRes::s_doIPv6=true; | |
2668 | L<<Logger::Warning<<"Enabling IPv6 transport for outgoing queries"<<endl; | |
2669 | ||
2670 | stringtok(addrs, ::arg()["query-local-address6"], ", ;"); | |
2671 | for(const string& addr : addrs) { | |
2672 | g_localQueryAddresses6.push_back(ComboAddress(addr)); | |
2673 | } | |
2674 | } | |
2675 | else { | |
2676 | L<<Logger::Warning<<"NOT using IPv6 for outgoing queries - set 'query-local-address6=::' to enable"<<endl; | |
2677 | } | |
2678 | addrs.clear(); | |
2679 | stringtok(addrs, ::arg()["query-local-address"], ", ;"); | |
2680 | for(const string& addr : addrs) { | |
2681 | g_localQueryAddresses4.push_back(ComboAddress(addr)); | |
2682 | } | |
2683 | } | |
2684 | catch(std::exception& e) { | |
2685 | L<<Logger::Error<<"Assigning local query addresses: "<<e.what(); | |
2686 | exit(99); | |
2687 | } | |
2688 | ||
e48c6b8a PL |
2689 | // keep this ABOVE loadRecursorLuaConfig! |
2690 | if(::arg()["dnssec"]=="off") | |
2691 | g_dnssecmode=DNSSECMode::Off; | |
2692 | else if(::arg()["dnssec"]=="process-no-validate") | |
2693 | g_dnssecmode=DNSSECMode::ProcessNoValidate; | |
2694 | else if(::arg()["dnssec"]=="process") | |
2695 | g_dnssecmode=DNSSECMode::Process; | |
2696 | else if(::arg()["dnssec"]=="validate") | |
2697 | g_dnssecmode=DNSSECMode::ValidateAll; | |
2698 | else if(::arg()["dnssec"]=="log-fail") | |
2699 | g_dnssecmode=DNSSECMode::ValidateForLog; | |
2700 | else { | |
2701 | L<<Logger::Error<<"Unknown DNSSEC mode "<<::arg()["dnssec"]<<endl; | |
2702 | exit(1); | |
2703 | } | |
2704 | ||
2705 | g_dnssecLogBogus = ::arg().mustDo("dnssec-log-bogus"); | |
2706 | ||
a4241908 | 2707 | loadRecursorLuaConfig(::arg()["lua-config-file"], ::arg().mustDo("daemon")); |
ad42489c | 2708 | |
18af64a8 | 2709 | parseACLs(); |
92011b8f | 2710 | sortPublicSuffixList(); |
2711 | ||
eb5bae86 BH |
2712 | if(!::arg()["dont-query"].empty()) { |
2713 | g_dontQuery=new NetmaskGroup; | |
2714 | vector<string> ips; | |
2715 | stringtok(ips, ::arg()["dont-query"], ", "); | |
66e0b6ea BH |
2716 | ips.push_back("0.0.0.0"); |
2717 | ips.push_back("::"); | |
c36bc97a | 2718 | |
eb5bae86 BH |
2719 | L<<Logger::Warning<<"Will not send queries to: "; |
2720 | for(vector<string>::const_iterator i = ips.begin(); i!= ips.end(); ++i) { | |
2721 | g_dontQuery->addMask(*i); | |
2722 | if(i!=ips.begin()) | |
4957a608 | 2723 | L<<Logger::Warning<<", "; |
eb5bae86 BH |
2724 | L<<Logger::Warning<<*i; |
2725 | } | |
2726 | L<<Logger::Warning<<endl; | |
2727 | } | |
2728 | ||
f7c1d4e3 | 2729 | g_quiet=::arg().mustDo("quiet"); |
3ddb9247 | 2730 | |
1bc3c142 BH |
2731 | g_weDistributeQueries = ::arg().mustDo("pdns-distributes-queries"); |
2732 | if(g_weDistributeQueries) { | |
2733 | L<<Logger::Warning<<"PowerDNS Recursor itself will distribute queries over threads"<<endl; | |
2734 | } | |
3ddb9247 | 2735 | |
756e82cf | 2736 | setupDelegationOnly(); |
b33c2462 | 2737 | g_outgoingEDNSBufsize=::arg().asNum("edns-outgoing-bufsize"); |
756e82cf | 2738 | |
77499b05 BH |
2739 | if(::arg()["trace"]=="fail") { |
2740 | SyncRes::setDefaultLogMode(SyncRes::Store); | |
2741 | } | |
2742 | else if(::arg().mustDo("trace")) { | |
2743 | SyncRes::setDefaultLogMode(SyncRes::Log); | |
f7c1d4e3 BH |
2744 | ::arg().set("quiet")="no"; |
2745 | g_quiet=false; | |
3e9c6c0a | 2746 | g_dnssecLOG=true; |
f7c1d4e3 | 2747 | } |
3ddb9247 | 2748 | |
aadceba8 | 2749 | SyncRes::s_minimumTTL = ::arg().asNum("minimum-ttl-override"); |
2750 | ||
1051f8a9 BH |
2751 | SyncRes::s_nopacketcache = ::arg().mustDo("disable-packetcache"); |
2752 | ||
f7c1d4e3 | 2753 | SyncRes::s_maxnegttl=::arg().asNum("max-negative-ttl"); |
63637fd8 | 2754 | SyncRes::s_maxcachettl=max(::arg().asNum("max-cache-ttl"), 15); |
1051f8a9 | 2755 | SyncRes::s_packetcachettl=::arg().asNum("packetcache-ttl"); |
79ec0627 PL |
2756 | // Cap the packetcache-servfail-ttl to the packetcache-ttl |
2757 | uint32_t packetCacheServFailTTL = ::arg().asNum("packetcache-servfail-ttl"); | |
2758 | SyncRes::s_packetcacheservfailttl=(packetCacheServFailTTL > SyncRes::s_packetcachettl) ? SyncRes::s_packetcachettl : packetCacheServFailTTL; | |
628e2c7b PA |
2759 | SyncRes::s_serverdownmaxfails=::arg().asNum("server-down-max-fails"); |
2760 | SyncRes::s_serverdownthrottletime=::arg().asNum("server-down-throttle-time"); | |
f7c1d4e3 | 2761 | SyncRes::s_serverID=::arg()["server-id"]; |
173d790e | 2762 | SyncRes::s_maxqperq=::arg().asNum("max-qperq"); |
9de3e034 | 2763 | SyncRes::s_maxtotusec=1000*::arg().asNum("max-total-msec"); |
7c3398aa | 2764 | SyncRes::s_maxdepth=::arg().asNum("max-recursion-depth"); |
01402d56 | 2765 | SyncRes::s_rootNXTrust = ::arg().mustDo( "root-nx-trust"); |
f7c1d4e3 BH |
2766 | if(SyncRes::s_serverID.empty()) { |
2767 | char tmp[128]; | |
2768 | gethostname(tmp, sizeof(tmp)-1); | |
2769 | SyncRes::s_serverID=tmp; | |
2770 | } | |
3ddb9247 | 2771 | |
5b0ddd18 | 2772 | g_networkTimeoutMsec = ::arg().asNum("network-timeout"); |
bb4bdbaf | 2773 | |
49a699c4 | 2774 | g_initialDomainMap = parseAuthAndForwards(); |
3ddb9247 | 2775 | |
08f3f638 | 2776 | g_latencyStatSize=::arg().asNum("latency-statistic-size"); |
3ddb9247 | 2777 | |
f7c1d4e3 | 2778 | g_logCommonErrors=::arg().mustDo("log-common-errors"); |
e661a20b PD |
2779 | |
2780 | g_anyToTcp = ::arg().mustDo("any-to-tcp"); | |
a09a8ce0 PD |
2781 | g_udpTruncationThreshold = ::arg().asNum("udp-truncation-threshold"); |
2782 | ||
b3adda56 PD |
2783 | g_lowercaseOutgoing = ::arg().mustDo("lowercase-outgoing"); |
2784 | ||
f7c1d4e3 BH |
2785 | makeUDPServerSockets(); |
2786 | makeTCPServerSockets(); | |
815099b2 | 2787 | |
376effcf | 2788 | parseEDNSSubnetWhitelist(::arg()["edns-subnet-whitelist"]); |
b40562da | 2789 | g_useIncomingECS = ::arg().mustDo("use-incoming-edns-subnet"); |
376effcf | 2790 | |
677e2a46 BH |
2791 | int forks; |
2792 | for(forks = 0; forks < ::arg().asNum("processes") - 1; ++forks) { | |
1bc3c142 BH |
2793 | if(!fork()) // we are child |
2794 | break; | |
2795 | } | |
3ddb9247 | 2796 | |
f7c1d4e3 BH |
2797 | if(::arg().mustDo("daemon")) { |
2798 | L<<Logger::Warning<<"Calling daemonize, going to background"<<endl; | |
2799 | L.toConsole(Logger::Critical); | |
f7c1d4e3 | 2800 | daemonize(); |
a4241908 | 2801 | loadRecursorLuaConfig(::arg()["lua-config-file"], false); |
f7c1d4e3 BH |
2802 | } |
2803 | signal(SIGUSR1,usr1Handler); | |
2804 | signal(SIGUSR2,usr2Handler); | |
2805 | signal(SIGPIPE,SIG_IGN); | |
a6414fdc | 2806 | g_numThreads = ::arg().asNum("threads") + ::arg().mustDo("pdns-distributes-queries"); |
c0063e60 | 2807 | g_numWorkerThreads = ::arg().asNum("threads"); |
a6414fdc AT |
2808 | g_maxMThreads = ::arg().asNum("max-mthreads"); |
2809 | checkOrFixFDS(); | |
3ddb9247 | 2810 | |
d1b28475 KM |
2811 | #ifdef HAVE_LIBSODIUM |
2812 | if (sodium_init() == -1) { | |
2813 | L<<Logger::Error<<"Unable to initialize sodium crypto library"<<endl; | |
2814 | exit(99); | |
2815 | } | |
2816 | #endif | |
2817 | ||
3afde9b2 PL |
2818 | openssl_thread_setup(); |
2819 | openssl_seed(); | |
2820 | ||
138435cb BH |
2821 | int newgid=0; |
2822 | if(!::arg()["setgid"].empty()) | |
2823 | newgid=Utility::makeGidNumeric(::arg()["setgid"]); | |
2824 | int newuid=0; | |
2825 | if(!::arg()["setuid"].empty()) | |
2826 | newuid=Utility::makeUidNumeric(::arg()["setuid"]); | |
2827 | ||
f1d6a7ce KM |
2828 | Utility::dropGroupPrivs(newuid, newgid); |
2829 | ||
138435cb | 2830 | if (!::arg()["chroot"].empty()) { |
75336810 PL |
2831 | #ifdef HAVE_SYSTEMD |
2832 | char *ns; | |
2833 | ns = getenv("NOTIFY_SOCKET"); | |
2834 | if (ns != nullptr) { | |
2835 | L<<Logger::Error<<"Unable to chroot when running from systemd. Please disable chroot= or set the 'Type' for this service to 'simple'"<<endl; | |
2836 | exit(1); | |
2837 | } | |
2838 | #endif | |
138435cb BH |
2839 | if (chroot(::arg()["chroot"].c_str())<0 || chdir("/") < 0) { |
2840 | L<<Logger::Error<<"Unable to chroot to '"+::arg()["chroot"]+"': "<<strerror (errno)<<", exiting"<<endl; | |
2841 | exit(1); | |
2842 | } | |
f0f3f0b0 PL |
2843 | else |
2844 | L<<Logger::Error<<"Chrooted to '"<<::arg()["chroot"]<<"'"<<endl; | |
138435cb BH |
2845 | } |
2846 | ||
f0f3f0b0 PL |
2847 | s_pidfname=::arg()["socket-dir"]+"/"+s_programname+".pid"; |
2848 | if(!s_pidfname.empty()) | |
2849 | unlink(s_pidfname.c_str()); // remove possible old pid file | |
2850 | writePid(); | |
2851 | ||
2852 | makeControlChannelSocket( ::arg().asNum("processes") > 1 ? forks : -1); | |
2853 | ||
f1d6a7ce | 2854 | Utility::dropUserPrivs(newuid); |
c0063e60 | 2855 | |
49a699c4 | 2856 | makeThreadPipes(); |
3ddb9247 | 2857 | |
5d4dd7fe BH |
2858 | g_tcpTimeout=::arg().asNum("client-tcp-timeout"); |
2859 | g_maxTCPPerClient=::arg().asNum("max-tcp-per-client"); | |
fde296a3 | 2860 | g_tcpMaxQueriesPerConn=::arg().asNum("max-tcp-queries-per-connection"); |
343257a4 | 2861 | |
d705aad9 RG |
2862 | if (::arg().mustDo("snmp-agent")) { |
2863 | g_snmpAgent = std::make_shared<RecursorSNMPAgent>("recursor", ::arg()["snmp-master-socket"]); | |
2864 | g_snmpAgent->run(); | |
2865 | } | |
2866 | ||
c3828c03 | 2867 | if(g_numThreads == 1) { |
76698c6e | 2868 | L<<Logger::Warning<<"Operating unthreaded"<<endl; |
6b6720de PL |
2869 | #ifdef HAVE_SYSTEMD |
2870 | sd_notify(0, "READY=1"); | |
2871 | #endif | |
76698c6e BH |
2872 | recursorThread(0); |
2873 | } | |
2874 | else { | |
2875 | pthread_t tid; | |
c3828c03 BH |
2876 | L<<Logger::Warning<<"Launching "<< g_numThreads <<" threads"<<endl; |
2877 | for(unsigned int n=0; n < g_numThreads; ++n) { | |
77499b05 | 2878 | pthread_create(&tid, 0, recursorThread, (void*)(long)n); |
76698c6e BH |
2879 | } |
2880 | void* res; | |
6b6720de PL |
2881 | #ifdef HAVE_SYSTEMD |
2882 | sd_notify(0, "READY=1"); | |
2883 | #endif | |
76698c6e | 2884 | pthread_join(tid, &res); |
bb4bdbaf | 2885 | } |
bb4bdbaf BH |
2886 | return 0; |
2887 | } | |
2888 | ||
2889 | void* recursorThread(void* ptr) | |
2890 | try | |
2891 | { | |
2e2cd8ec | 2892 | t_id=(int) (long) ptr; |
49a699c4 | 2893 | SyncRes tmp(g_now); // make sure it allocates tsstorage before we do anything, like primeHints or so.. |
ac0e821b | 2894 | t_sstorage->domainmap = g_initialDomainMap; |
49a699c4 BH |
2895 | t_allowFrom = g_initialAllowFrom; |
2896 | t_udpclientsocks = new UDPClientSocks(); | |
bd0289fc | 2897 | t_tcpClientCounts = new tcpClientCounts_t(); |
49a699c4 | 2898 | primeHints(); |
3ddb9247 | 2899 | |
49a699c4 | 2900 | t_packetCache = new RecursorPacketCache(); |
3ddb9247 | 2901 | |
aa7929a3 RG |
2902 | #ifdef HAVE_PROTOBUF |
2903 | t_uuidGenerator = new boost::uuids::random_generator(); | |
2904 | #endif | |
49a699c4 | 2905 | L<<Logger::Warning<<"Done priming cache with root hints"<<endl; |
3ddb9247 | 2906 | |
a3e7b735 | 2907 | t_pdl = new shared_ptr<RecursorLua4>(); |
3ddb9247 | 2908 | |
674cf0f6 BH |
2909 | try { |
2910 | if(!::arg()["lua-dns-script"].empty()) { | |
a3e7b735 | 2911 | *t_pdl = shared_ptr<RecursorLua4>(new RecursorLua4(::arg()["lua-dns-script"])); |
674cf0f6 BH |
2912 | L<<Logger::Warning<<"Loaded 'lua' script from '"<<::arg()["lua-dns-script"]<<"'"<<endl; |
2913 | } | |
674cf0f6 BH |
2914 | } |
2915 | catch(std::exception &e) { | |
2916 | L<<Logger::Error<<"Failed to load 'lua' script from '"<<::arg()["lua-dns-script"]<<"': "<<e.what()<<endl; | |
62f0ae62 | 2917 | _exit(99); |
674cf0f6 | 2918 | } |
3ddb9247 | 2919 | |
77499b05 | 2920 | t_traceRegex = new shared_ptr<Regex>(); |
f8f243b0 | 2921 | unsigned int ringsize=::arg().asNum("stats-ringbuffer-entries") / g_numWorkerThreads; |
92011b8f | 2922 | if(ringsize) { |
60c8afa8 | 2923 | t_remotes = new addrringbuf_t(); |
f8f243b0 | 2924 | if(g_weDistributeQueries) // if so, only 1 thread does recvfrom |
3ddb9247 | 2925 | t_remotes->set_capacity(::arg().asNum("stats-ringbuffer-entries")); |
f8f243b0 | 2926 | else |
3ddb9247 | 2927 | t_remotes->set_capacity(ringsize); |
60c8afa8 | 2928 | t_servfailremotes = new addrringbuf_t(); |
3ddb9247 | 2929 | t_servfailremotes->set_capacity(ringsize); |
60c8afa8 | 2930 | t_largeanswerremotes = new addrringbuf_t(); |
3ddb9247 | 2931 | t_largeanswerremotes->set_capacity(ringsize); |
92011b8f | 2932 | |
c5c066bf | 2933 | t_queryring = new boost::circular_buffer<pair<DNSName, uint16_t> >(); |
3ddb9247 | 2934 | t_queryring->set_capacity(ringsize); |
c5c066bf | 2935 | t_servfailqueryring = new boost::circular_buffer<pair<DNSName, uint16_t> >(); |
3ddb9247 | 2936 | t_servfailqueryring->set_capacity(ringsize); |
92011b8f | 2937 | } |
3ddb9247 | 2938 | |
bb4bdbaf | 2939 | MT=new MTasker<PacketID,string>(::arg().asNum("stack-size")); |
3ddb9247 | 2940 | |
bb4bdbaf BH |
2941 | PacketID pident; |
2942 | ||
2943 | t_fdm=getMultiplexer(); | |
f3d1d67b | 2944 | if(!t_id) { |
d07bf7ff | 2945 | if(::arg().mustDo("webserver")) { |
30a1aa92 | 2946 | L<<Logger::Warning << "Enabling web server" << endl; |
8989097d | 2947 | try { |
1ce57618 | 2948 | new RecursorWebServer(t_fdm); |
8989097d CH |
2949 | } |
2950 | catch(PDNSException &e) { | |
2951 | L<<Logger::Error<<"Exception: "<<e.reason<<endl; | |
2952 | exit(99); | |
2953 | } | |
f3d1d67b | 2954 | } |
83252304 | 2955 | L<<Logger::Error<<"Enabled '"<< t_fdm->getName() << "' multiplexer"<<endl; |
f3d1d67b | 2956 | } |
83252304 | 2957 | |
49a699c4 | 2958 | t_fdm->addReadFD(g_pipes[t_id].readToThread, handlePipeRequest); |
83252304 | 2959 | |
1bc3c142 | 2960 | if(!g_weDistributeQueries || !t_id) // if we distribute queries, only t_id = 0 listens |
3ddb9247 | 2961 | for(deferredAdd_t::const_iterator i=deferredAdd.begin(); i!=deferredAdd.end(); ++i) |
1bc3c142 | 2962 | t_fdm->addReadFD(i->first, i->second); |
3ddb9247 | 2963 | |
674cf0f6 | 2964 | if(!t_id) { |
674cf0f6 BH |
2965 | t_fdm->addReadFD(s_rcc.d_fd, handleRCC); // control channel |
2966 | } | |
1bc3c142 | 2967 | |
f7c1d4e3 | 2968 | unsigned int maxTcpClients=::arg().asNum("max-tcp-clients"); |
3ddb9247 | 2969 | |
f7c1d4e3 | 2970 | bool listenOnTCP(true); |
49a699c4 | 2971 | |
2c78bd57 | 2972 | time_t last_carbon=0; |
2973 | time_t carbonInterval=::arg().asNum("carbon-interval"); | |
ac0995bb | 2974 | counter.store(0); // used to periodically execute certain tasks |
f7c1d4e3 | 2975 | for(;;) { |
ac0e821b | 2976 | while(MT->schedule(&g_now)); // MTasker letting the mthreads do their thing |
3ddb9247 | 2977 | |
3427fa8a BH |
2978 | if(!(counter%500)) { |
2979 | MT->makeThread(houseKeeping, 0); | |
f7c1d4e3 BH |
2980 | } |
2981 | ||
d2392145 | 2982 | if(!(counter%55)) { |
d8f6d49f | 2983 | typedef vector<pair<int, FDMultiplexer::funcparam_t> > expired_t; |
bb4bdbaf | 2984 | expired_t expired=t_fdm->getTimeouts(g_now); |
3ddb9247 | 2985 | |
f7c1d4e3 | 2986 | for(expired_t::iterator i=expired.begin() ; i != expired.end(); ++i) { |
cd989c87 | 2987 | shared_ptr<TCPConnection> conn=any_cast<shared_ptr<TCPConnection> >(i->second); |
4957a608 | 2988 | if(g_logCommonErrors) |
cd989c87 | 2989 | L<<Logger::Warning<<"Timeout from remote TCP client "<< conn->d_remote.toString() <<endl; |
4957a608 | 2990 | t_fdm->removeReadFD(i->first); |
f7c1d4e3 BH |
2991 | } |
2992 | } | |
3ddb9247 | 2993 | |
f7c1d4e3 BH |
2994 | counter++; |
2995 | ||
3427fa8a | 2996 | if(!t_id && statsWanted) { |
f7c1d4e3 BH |
2997 | doStats(); |
2998 | } | |
2999 | ||
3000 | Utility::gettimeofday(&g_now, 0); | |
2c78bd57 | 3001 | |
3002 | if(!t_id && (g_now.tv_sec - last_carbon >= carbonInterval)) { | |
3003 | MT->makeThread(doCarbonDump, 0); | |
3004 | last_carbon = g_now.tv_sec; | |
3005 | } | |
3006 | ||
bb4bdbaf | 3007 | t_fdm->run(&g_now); |
3ea54bf0 | 3008 | // 'run' updates g_now for us |
f7c1d4e3 | 3009 | |
b8ef5c5c | 3010 | if(!g_weDistributeQueries || !t_id) { // if pdns distributes queries, only tid 0 should do this |
5c889cf5 | 3011 | if(listenOnTCP) { |
3012 | if(TCPConnection::getCurrentConnections() > maxTcpClients) { // shutdown, too many connections | |
3013 | for(tcpListenSockets_t::iterator i=g_tcpListenSockets.begin(); i != g_tcpListenSockets.end(); ++i) | |
3014 | t_fdm->removeReadFD(*i); | |
3015 | listenOnTCP=false; | |
3016 | } | |
f7c1d4e3 | 3017 | } |
5c889cf5 | 3018 | else { |
3019 | if(TCPConnection::getCurrentConnections() <= maxTcpClients) { // reenable | |
3020 | for(tcpListenSockets_t::iterator i=g_tcpListenSockets.begin(); i != g_tcpListenSockets.end(); ++i) | |
3021 | t_fdm->addReadFD(*i, handleNewTCPQuestion); | |
3022 | listenOnTCP=true; | |
3023 | } | |
f7c1d4e3 BH |
3024 | } |
3025 | } | |
3026 | } | |
3027 | } | |
3f81d239 | 3028 | catch(PDNSException &ae) { |
bb4bdbaf BH |
3029 | L<<Logger::Error<<"Exception: "<<ae.reason<<endl; |
3030 | return 0; | |
3031 | } | |
3032 | catch(std::exception &e) { | |
3033 | L<<Logger::Error<<"STL Exception: "<<e.what()<<endl; | |
3034 | return 0; | |
3035 | } | |
3036 | catch(...) { | |
3037 | L<<Logger::Error<<"any other exception in main: "<<endl; | |
3038 | return 0; | |
3039 | } | |
3040 | ||
51e2144e | 3041 | |
3ddb9247 | 3042 | int main(int argc, char **argv) |
288f4aa9 | 3043 | { |
dbd23fc2 BH |
3044 | g_argc = argc; |
3045 | g_argv = argv; | |
5e3de507 | 3046 | g_stats.startupTime=time(0); |
3e135495 | 3047 | versionSetProduct(ProductRecursor); |
8a63d3ce | 3048 | reportBasicTypes(); |
0007c2e5 | 3049 | reportOtherTypes(); |
ea634573 | 3050 | |
22030c37 | 3051 | int ret = EXIT_SUCCESS; |
caa6eefa | 3052 | |
288f4aa9 | 3053 | try { |
f888311c | 3054 | ::arg().set("stack-size","stack size per mthread")="200000"; |
2e3d8a19 | 3055 | ::arg().set("soa-minimum-ttl","Don't change")="0"; |
2e3d8a19 | 3056 | ::arg().set("no-shuffle","Don't change")="off"; |
2e3d8a19 | 3057 | ::arg().set("local-port","port to listen on")="53"; |
32252594 | 3058 | ::arg().set("local-address","IP addresses to listen on, separated by spaces or commas. Also accepts ports.")="127.0.0.1"; |
fec7dd5a | 3059 | ::arg().setSwitch("non-local-bind", "Enable binding to non-local addresses by using FREEBIND / BINDANY socket options")="no"; |
77499b05 | 3060 | ::arg().set("trace","if we should output heaps of logging. set to 'fail' to only log failing domains")="off"; |
a6415142 | 3061 | ::arg().set("dnssec", "DNSSEC mode: off/process-no-validate (default)/process/log-fail/validate")="process-no-validate"; |
c87e1876 | 3062 | ::arg().set("dnssec-log-bogus", "Log DNSSEC bogus validations")="no"; |
d3f809bf | 3063 | ::arg().set("daemon","Operate as a daemon")="no"; |
191f2e47 | 3064 | ::arg().setSwitch("write-pid","Write a PID file")="yes"; |
34162f8f | 3065 | ::arg().set("loglevel","Amount of logging. Higher is more. Do not set below 3")="4"; |
b6cfa948 | 3066 | ::arg().set("disable-syslog","Disable logging to syslog, useful when running inside a supervisor that logs stdout")="no"; |
22e0810c | 3067 | ::arg().set("log-common-errors","If we should log rather common errors")="no"; |
2e3d8a19 BH |
3068 | ::arg().set("chroot","switch to chroot jail")=""; |
3069 | ::arg().set("setgid","If set, change group id to this gid for more security")=""; | |
3070 | ::arg().set("setuid","If set, change user id to this uid for more security")=""; | |
c83ee49d | 3071 | ::arg().set("network-timeout", "Wait this number of milliseconds for network i/o")="1500"; |
bb4bdbaf | 3072 | ::arg().set("threads", "Launch this number of threads")="2"; |
adabfcb9 | 3073 | ::arg().set("processes", "Launch this number of processes (EXPERIMENTAL, DO NOT CHANGE)")="1"; // if we un-experimental this, need to fix openssl rand seeding for multiple PIDs! |
5124de27 | 3074 | ::arg().set("config-name","Name of this virtual configuration - will rename the binary image")=""; |
d07bf7ff | 3075 | ::arg().set("api-config-dir", "Directory where REST API stores config and zones") = ""; |
479e0976 CH |
3076 | ::arg().set("api-key", "Static pre-shared authentication key for access to the REST API") = ""; |
3077 | ::arg().set("api-logfile", "Location of the server logfile (used by the REST API)") = "/var/log/pdns.log"; | |
3078 | ::arg().set("api-readonly", "Disallow data modification through the REST API when set") = "no"; | |
3079 | ::arg().setSwitch("webserver", "Start a webserver (for REST API)") = "no"; | |
d07bf7ff PL |
3080 | ::arg().set("webserver-address", "IP Address of webserver to listen on") = "127.0.0.1"; |
3081 | ::arg().set("webserver-port", "Port of webserver to listen on") = "8082"; | |
3082 | ::arg().set("webserver-password", "Password required for accessing the webserver") = ""; | |
69e7f117 | 3083 | ::arg().set("webserver-allow-from","Webserver access is only allowed from these subnets")="0.0.0.0/0,::/0"; |
cc08b5a9 | 3084 | ::arg().set("carbon-ourname", "If set, overrides our reported hostname for carbon stats")=""; |
e12f8407 | 3085 | ::arg().set("carbon-server", "If set, send metrics in carbon (graphite) format to this server IP address")=""; |
2c78bd57 | 3086 | ::arg().set("carbon-interval", "Number of seconds between carbon (graphite) updates")="30"; |
c038218b | 3087 | ::arg().set("quiet","Suppress logging of questions and answers")=""; |
f27e6356 | 3088 | ::arg().set("logging-facility","Facility to log messages as. 0 corresponds to local0")=""; |
2e3d8a19 | 3089 | ::arg().set("config-dir","Location of configuration directory (recursor.conf)")=SYSCONFDIR; |
fdbf35ac BH |
3090 | ::arg().set("socket-owner","Owner of socket")=""; |
3091 | ::arg().set("socket-group","Group of socket")=""; | |
3092 | ::arg().set("socket-mode", "Permissions for socket")=""; | |
3ddb9247 | 3093 | |
f0f3f0b0 | 3094 | ::arg().set("socket-dir",string("Where the controlsocket will live, ")+LOCALSTATEDIR+" when unset and not chrooted" )=""; |
2e3d8a19 BH |
3095 | ::arg().set("delegation-only","Which domains we only accept delegations from")=""; |
3096 | ::arg().set("query-local-address","Source IP address for sending queries")="0.0.0.0"; | |
d4fb76e9 | 3097 | ::arg().set("query-local-address6","Source IPv6 address for sending queries. IF UNSET, IPv6 WILL NOT BE USED FOR OUTGOING QUERIES")=""; |
2e3d8a19 | 3098 | ::arg().set("client-tcp-timeout","Timeout in seconds when talking to TCP clients")="2"; |
85c32340 | 3099 | ::arg().set("max-mthreads", "Maximum number of simultaneous Mtasker threads")="2048"; |
2e3d8a19 | 3100 | ::arg().set("max-tcp-clients","Maximum number of simultaneous TCP clients")="128"; |
324dc148 | 3101 | ::arg().set("server-down-max-fails","Maximum number of consecutive timeouts (and unreachables) to mark a server as down ( 0 => disabled )")="64"; |
979edd70 | 3102 | ::arg().set("server-down-throttle-time","Number of seconds to throttle all queries to a server after being marked as down")="60"; |
2e3d8a19 | 3103 | ::arg().set("hint-file", "If set, load root hints from this file")=""; |
b45eb27c | 3104 | ::arg().set("max-cache-entries", "If set, maximum number of entries in the main cache")="1000000"; |
a9af3782 | 3105 | ::arg().set("max-negative-ttl", "maximum number of seconds to keep a negative cached entry in memory")="3600"; |
c3e753c7 | 3106 | ::arg().set("max-cache-ttl", "maximum number of seconds to keep a cached entry in memory")="86400"; |
1051f8a9 | 3107 | ::arg().set("packetcache-ttl", "maximum number of seconds to keep a cached entry in packetcache")="3600"; |
927c12b0 | 3108 | ::arg().set("max-packetcache-entries", "maximum number of entries to keep in the packetcache")="500000"; |
1051f8a9 | 3109 | ::arg().set("packetcache-servfail-ttl", "maximum number of seconds to keep a cached servfail entry in packetcache")="60"; |
7f7b8d55 | 3110 | ::arg().set("server-id", "Returned when queried for 'server.id' TXT or NSID, defaults to hostname")=""; |
92011b8f | 3111 | ::arg().set("stats-ringbuffer-entries", "maximum number of packets to store statistics for")="10000"; |
ba1a571d | 3112 | ::arg().set("version-string", "string reported on version.pdns or version.bind")=fullVersionString(); |
49a699c4 | 3113 | ::arg().set("allow-from", "If set, only allow these comma separated netmasks to recurse")=LOCAL_NETS; |
2c95fc65 | 3114 | ::arg().set("allow-from-file", "If set, load allowed netmasks from this file")=""; |
51e2144e | 3115 | ::arg().set("entropy-source", "If set, read entropy from this file")="/dev/urandom"; |
3ddb9247 | 3116 | ::arg().set("dont-query", "If set, do not query these netmasks for DNS data")=DONT_QUERY; |
4e120339 | 3117 | ::arg().set("max-tcp-per-client", "If set, maximum number of TCP sessions per client (IP address)")="0"; |
fde296a3 | 3118 | ::arg().set("max-tcp-queries-per-connection", "If set, maximum number of TCP queries in a TCP connection")="0"; |
0d5f0a9f | 3119 | ::arg().set("spoof-nearmiss-max", "If non-zero, assume spoofing after this many near misses")="20"; |
4ef015cd | 3120 | ::arg().set("single-socket", "If set, only use a single socket for outgoing queries")="off"; |
5605c067 | 3121 | ::arg().set("auth-zones", "Zones for which we have authoritative data, comma separated domain=file pairs ")=""; |
3e61e7f7 | 3122 | ::arg().set("lua-config-file", "More powerful configuration options")=""; |
644dd1da | 3123 | |
5605c067 | 3124 | ::arg().set("forward-zones", "Zones for which we forward queries, comma separated domain=ip pairs")=""; |
927c12b0 BH |
3125 | ::arg().set("forward-zones-recurse", "Zones for which we forward queries with recursion bit, comma separated domain=ip pairs")=""; |
3126 | ::arg().set("forward-zones-file", "File with (+)domain=ip pairs for forwarding")=""; | |
5605c067 | 3127 | ::arg().set("export-etc-hosts", "If we should serve up contents from /etc/hosts")="off"; |
ac0b4eb3 | 3128 | ::arg().set("export-etc-hosts-search-suffix", "Also serve up the contents of /etc/hosts with this suffix")=""; |
3ea54bf0 | 3129 | ::arg().set("etc-hosts-file", "Path to 'hosts' file")="/etc/hosts"; |
e498dac1 | 3130 | ::arg().set("serve-rfc1918", "If we should be authoritative for RFC 1918 private IP space")="yes"; |
4485aa35 | 3131 | ::arg().set("lua-dns-script", "Filename containing an optional 'lua' script that will be used to modify dns answers")=""; |
08f3f638 | 3132 | ::arg().set("latency-statistic-size","Number of latency values to calculate the qa-latency average")="10000"; |
3ddb9247 | 3133 | ::arg().setSwitch( "disable-packetcache", "Disable packetcache" )= "no"; |
35695d18 | 3134 | ::arg().set("ecs-ipv4-bits", "Number of bits of IPv4 address to pass for EDNS Client Subnet")="24"; |
3135 | ::arg().set("ecs-ipv6-bits", "Number of bits of IPv6 address to pass for EDNS Client Subnet")="56"; | |
3f975863 | 3136 | ::arg().set("edns-subnet-whitelist", "List of netmasks and domains that we should enable EDNS subnet for")=""; |
b40562da | 3137 | ::arg().setSwitch( "use-incoming-edns-subnet", "Pass along received EDNS Client Subnet information")=""; |
e498dac1 | 3138 | ::arg().setSwitch( "pdns-distributes-queries", "If PowerDNS itself should distribute queries over threads")="yes"; |
4ca2d205 | 3139 | ::arg().setSwitch( "root-nx-trust", "If set, believe that an NXDOMAIN from the root means the TLD does not exist")="yes"; |
e661a20b | 3140 | ::arg().setSwitch( "any-to-tcp","Answer ANY queries with tc=1, shunting to TCP" )="no"; |
b3adda56 | 3141 | ::arg().setSwitch( "lowercase-outgoing","Force outgoing questions to lowercase")="no"; |
a09a8ce0 | 3142 | ::arg().set("udp-truncation-threshold", "Maximum UDP response size before we truncate")="1680"; |
b33c2462 | 3143 | ::arg().set("edns-outgoing-bufsize", "Outgoing EDNS buffer size")="1680"; |
aadceba8 | 3144 | ::arg().set("minimum-ttl-override", "Set under adverse conditions, a minimum TTL")="0"; |
173d790e | 3145 | ::arg().set("max-qperq", "Maximum outgoing queries per query")="50"; |
c5950146 | 3146 | ::arg().set("max-total-msec", "Maximum total wall-clock time per query in milliseconds, 0 for unlimited")="7000"; |
7c3398aa | 3147 | ::arg().set("max-recursion-depth", "Maximum number of internal recursion calls per query, 0 for unlimited")="40"; |
a09a8ce0 | 3148 | |
68e6df3c | 3149 | ::arg().set("include-dir","Include *.conf files from this directory")=""; |
d67620e4 | 3150 | ::arg().set("security-poll-suffix","Domain name from which to query security update notifications")="secpoll.powerdns.com."; |
2332f42d | 3151 | |
3152 | ::arg().setSwitch("reuseport","Enable SO_REUSEPORT allowing multiple recursors processes to listen to 1 address")="no"; | |
2e3d8a19 | 3153 | |
d705aad9 RG |
3154 | ::arg().setSwitch("snmp-agent", "If set, register as an SNMP agent")="no"; |
3155 | ::arg().setSwitch("snmp-master-socket", "If set and snmp-agent is set, the socket to use to register to the SNMP master")=""; | |
3156 | ||
2e3d8a19 | 3157 | ::arg().setCmd("help","Provide a helpful message"); |
ba1a571d | 3158 | ::arg().setCmd("version","Print version string"); |
d5141417 | 3159 | ::arg().setCmd("config","Output blank configuration"); |
f27e6356 | 3160 | L.toConsole(Logger::Info); |
2e3d8a19 | 3161 | ::arg().laxParse(argc,argv); // do a lax parse |
c75a6a9e | 3162 | |
2d733c0f CH |
3163 | string configname=::arg()["config-dir"]+"/recursor.conf"; |
3164 | if(::arg()["config-name"]!="") { | |
3165 | configname=::arg()["config-dir"]+"/recursor-"+::arg()["config-name"]+".conf"; | |
5124de27 | 3166 | s_programname+="-"+::arg()["config-name"]; |
2d733c0f CH |
3167 | } |
3168 | cleanSlashes(configname); | |
5124de27 | 3169 | |
577cf284 BH |
3170 | if(::arg().mustDo("config")) { |
3171 | cout<<::arg().configstring()<<endl; | |
3172 | exit(0); | |
3173 | } | |
3174 | ||
3ddb9247 | 3175 | if(!::arg().file(configname.c_str())) |
c75a6a9e BH |
3176 | L<<Logger::Warning<<"Unable to parse configuration file '"<<configname<<"'"<<endl; |
3177 | ||
2e3d8a19 | 3178 | ::arg().parse(argc,argv); |
c836dc19 | 3179 | |
f0f3f0b0 PL |
3180 | if( !::arg()["chroot"].empty() && !::arg()["api-config-dir"].empty() && !::arg().mustDo("api-readonly") ) { |
3181 | L<<Logger::Error<<"Using chroot and a writable API is not possible"<<endl; | |
3182 | exit(EXIT_FAILURE); | |
3183 | } | |
3184 | ||
3185 | if (::arg()["socket-dir"].empty()) { | |
3186 | if (::arg()["chroot"].empty()) | |
3187 | ::arg().set("socket-dir") = LOCALSTATEDIR; | |
3188 | else | |
3189 | ::arg().set("socket-dir") = "/"; | |
3190 | } | |
3191 | ||
2e3d8a19 | 3192 | ::arg().set("delegation-only")=toLower(::arg()["delegation-only"]); |
562588a3 | 3193 | |
61d74169 | 3194 | if(::arg().asNum("threads")==1) |
3195 | ::arg().set("pdns-distributes-queries")="no"; | |
3196 | ||
2e3d8a19 | 3197 | if(::arg().mustDo("help")) { |
ff5ba4f9 WA |
3198 | cout<<"syntax:"<<endl<<endl; |
3199 | cout<<::arg().helpstring(::arg()["help"])<<endl; | |
3200 | exit(0); | |
b636533b | 3201 | } |
5e3de507 | 3202 | if(::arg().mustDo("version")) { |
ba1a571d | 3203 | showProductVersion(); |
3613a51c | 3204 | showBuildConfiguration(); |
67076869 | 3205 | exit(0); |
5e3de507 | 3206 | } |
b636533b | 3207 | |
34162f8f | 3208 | Logger::Urgency logUrgency = (Logger::Urgency)::arg().asNum("loglevel"); |
f48d7b65 | 3209 | |
34162f8f CH |
3210 | if (logUrgency < Logger::Error) |
3211 | logUrgency = Logger::Error; | |
f48d7b65 | 3212 | if(!g_quiet && logUrgency < Logger::Info) { // Logger::Info=6, Logger::Debug=7 |
3213 | logUrgency = Logger::Info; // if you do --quiet=no, you need Info to also see the query log | |
3214 | } | |
34162f8f CH |
3215 | L.setLoglevel(logUrgency); |
3216 | L.toConsole(logUrgency); | |
3217 | ||
f7c1d4e3 | 3218 | serviceMain(argc, argv); |
288f4aa9 | 3219 | } |
3f81d239 | 3220 | catch(PDNSException &ae) { |
c836dc19 | 3221 | L<<Logger::Error<<"Exception: "<<ae.reason<<endl; |
22030c37 | 3222 | ret=EXIT_FAILURE; |
288f4aa9 | 3223 | } |
fdbf35ac | 3224 | catch(std::exception &e) { |
c836dc19 | 3225 | L<<Logger::Error<<"STL Exception: "<<e.what()<<endl; |
22030c37 | 3226 | ret=EXIT_FAILURE; |
288f4aa9 BH |
3227 | } |
3228 | catch(...) { | |
c836dc19 | 3229 | L<<Logger::Error<<"any other exception in main: "<<endl; |
22030c37 | 3230 | ret=EXIT_FAILURE; |
288f4aa9 | 3231 | } |
3ddb9247 | 3232 | |
22030c37 | 3233 | return ret; |
288f4aa9 | 3234 | } |
7836f7b4 PL |
3235 | |
3236 | int getRootNS(void) { | |
3237 | SyncRes sr(g_now); | |
3238 | sr.setDoEDNS0(true); | |
3239 | sr.setNoCache(); | |
3240 | sr.d_doDNSSEC = (g_dnssecmode != DNSSECMode::Off); | |
3241 | ||
3242 | vector<DNSRecord> ret; | |
3243 | int res=-1; | |
3244 | try { | |
3245 | res=sr.beginResolve(g_rootdnsname, QType(QType::NS), 1, ret); | |
3246 | if (g_dnssecmode != DNSSECMode::Off && g_dnssecmode != DNSSECMode::ProcessNoValidate) { | |
4898a348 RG |
3247 | ResolveContext ctx; |
3248 | auto state = validateRecords(ctx, ret); | |
7836f7b4 PL |
3249 | if (state == Bogus) |
3250 | throw PDNSException("Got Bogus validation result for .|NS"); | |
3251 | } | |
3252 | return res; | |
3253 | } | |
3254 | catch(PDNSException& e) | |
3255 | { | |
3256 | L<<Logger::Error<<"Failed to update . records, got an exception: "<<e.reason<<endl; | |
3257 | } | |
3258 | ||
3259 | catch(std::exception& e) | |
3260 | { | |
3261 | L<<Logger::Error<<"Failed to update . records, got an exception: "<<e.what()<<endl; | |
3262 | } | |
3263 | ||
3264 | catch(...) | |
3265 | { | |
3266 | L<<Logger::Error<<"Failed to update . records, got an exception"<<endl; | |
3267 | } | |
3268 | if(!res) { | |
3269 | L<<Logger::Notice<<"Refreshed . records"<<endl; | |
3270 | } | |
3271 | else | |
3272 | L<<Logger::Error<<"Failed to update . records, RCODE="<<res<<endl; | |
3273 | return res; | |
3274 | } |