]> git.ipfire.org Git - thirdparty/pdns.git/blame - pdns/pdns_recursor.cc
remove some unaligned memory accesses in recursor packet cache
[thirdparty/pdns.git] / pdns / pdns_recursor.cc
CommitLineData
288f4aa9
BH
1/*
2 PowerDNS Versatile Database Driven Nameserver
677e2a46 3 Copyright (C) 2003 - 2011 PowerDNS.COM BV
288f4aa9
BH
4
5 This program is free software; you can redistribute it and/or modify
f28307ad
BH
6 it under the terms of the GNU General Public License version 2
7 as published by the Free Software Foundation
288f4aa9
BH
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
06bd9ccf 16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
288f4aa9 17*/
caa6eefa 18
705f31ae 19#ifndef WIN32
f7c1d4e3 20# include <netdb.h>
f838ad8d 21# include <sys/stat.h>
f7c1d4e3
BH
22# include <unistd.h>
23#else
24 #include "ntservice.hh"
25 #include "recursorservice.hh"
705f31ae
BH
26#endif // WIN32
27
5a38281c 28#include <boost/foreach.hpp>
38c9ceaa 29
49a699c4 30#include <pthread.h>
3ea54bf0 31#include "recpacketcache.hh"
caa6eefa 32#include "utility.hh"
51e2144e 33#include "dns_random.hh"
288f4aa9
BH
34#include <iostream>
35#include <errno.h>
36#include <map>
37#include <set>
97bb160b 38#include "recursor_cache.hh"
38c9ceaa 39#include "cachecleaner.hh"
288f4aa9 40#include <stdio.h>
c75a6a9e 41#include <signal.h>
288f4aa9 42#include <stdlib.h>
bb4bdbaf 43#include "misc.hh"
288f4aa9
BH
44#include "mtasker.hh"
45#include <utility>
288f4aa9
BH
46#include "arguments.hh"
47#include "syncres.hh"
88def049
BH
48#include <fcntl.h>
49#include <fstream>
5c633640
BH
50#include "sstuff.hh"
51#include <boost/tuple/tuple.hpp>
52#include <boost/tuple/tuple_comparison.hpp>
72df400f 53#include <boost/shared_array.hpp>
ea634573 54#include <boost/lexical_cast.hpp>
7f1fa77d 55#include <boost/function.hpp>
5605c067 56#include <boost/algorithm/string.hpp>
40a3dd64 57#include <netinet/tcp.h>
ea634573
BH
58#include "dnsparser.hh"
59#include "dnswriter.hh"
60#include "dnsrecords.hh"
f814d7c8 61#include "zoneparser-tng.hh"
1d5b3ce6 62#include "rec_channel.hh"
aaacf7f2 63#include "logger.hh"
c8ddb7c2 64#include "iputils.hh"
09e6702a 65#include "mplexer.hh"
c038218b 66#include "config.h"
4485aa35 67#include "lua-pdns-recursor.hh"
1d5b3ce6 68
a2bfc3ff
BH
69#ifndef RECURSOR
70#include "statbag.hh"
71StatBag S;
72#endif
73
bb4bdbaf 74__thread FDMultiplexer* t_fdm;
674cf0f6 75__thread unsigned int t_id;
09e6702a 76unsigned int g_maxTCPPerClient;
5b0ddd18 77unsigned int g_networkTimeoutMsec;
09e6702a 78bool g_logCommonErrors;
674cf0f6 79__thread shared_ptr<PowerDNSLua>* t_pdl;
b3b5459d 80__thread RemoteKeeper* t_remotes;
674cf0f6 81
d7dae798
BH
82RecursorControlChannel s_rcc; // only active in thread 0
83
84// for communicating with our threads
49a699c4
BH
85struct ThreadPipeSet
86{
87 int writeToThread;
88 int readToThread;
89 int writeFromThread;
90 int readFromThread;
91};
3ea54bf0 92
d7dae798 93vector<ThreadPipeSet> g_pipes; // effectively readonly after startup
5c633640 94
d7dae798 95SyncRes::domainmap_t* g_initialDomainMap; // new threads needs this to be setup
49a699c4
BH
96
97#include "namespaces.hh"
3ea54bf0 98
49a699c4 99__thread MemRecursorCache* t_RC;
16beeaa4 100__thread RecursorPacketCache* t_packetCache;
1d5b3ce6
BH
101RecursorStats g_stats;
102bool g_quiet;
49a699c4 103
1bc3c142
BH
104bool g_weDistributeQueries; // if true, only 1 thread listens on the incoming query sockets
105
49a699c4
BH
106static __thread NetmaskGroup* t_allowFrom;
107static NetmaskGroup* g_initialAllowFrom; // new thread needs to be setup with this
108
eb5bae86 109NetmaskGroup* g_dontQuery;
88def049 110string s_programname="pdns_recursor";
49a699c4 111
40a3dd64
BH
112typedef vector<int> tcpListenSockets_t;
113tcpListenSockets_t g_tcpListenSockets; // shared across threads, but this is fine, never written to from a thread. All threads listen on all sockets
3159c9ef 114int g_tcpTimeout;
85c32340 115unsigned int g_maxMThreads;
d7dae798 116struct timeval g_now; // timestamp, updated (too) frequently
40a3dd64 117map<int, ComboAddress> g_listenSocketsAddresses; // is shared across all threads right now
18af64a8 118
d7dae798
BH
119__thread MT_t* MT; // the big MTasker
120
c3828c03
BH
121unsigned int g_numThreads;
122
49a699c4
BH
123#define LOCAL_NETS "127.0.0.0/8, 10.0.0.0/8, 192.168.0.0/16, 172.16.0.0/12, ::1/128, fe80::/10"
124
d7dae798 125//! used to send information to a newborn mthread
ea634573 126struct DNSComboWriter {
3ea54bf0 127 DNSComboWriter(const char* data, uint16_t len, const struct timeval& now) : d_mdp(data, len), d_now(now),
4957a608 128 d_tcp(false), d_socket(-1)
ea634573
BH
129 {}
130 MOADNSParser d_mdp;
00c9b8c1 131 void setRemote(const ComboAddress* sa)
ea634573 132 {
37d3f960 133 d_remote=*sa;
ea634573
BH
134 }
135
136 void setSocket(int sock)
137 {
138 d_socket=sock;
139 }
a1754c6a
BH
140
141 string getRemote() const
142 {
37d3f960 143 return d_remote.toString();
a1754c6a
BH
144 }
145
c9e9e5e0 146 struct timeval d_now;
37d3f960 147 ComboAddress d_remote;
ea634573
BH
148 bool d_tcp;
149 int d_socket;
cd989c87 150 shared_ptr<TCPConnection> d_tcpConnection;
ea634573
BH
151};
152
153
288f4aa9
BH
154ArgvMap &arg()
155{
156 static ArgvMap theArg;
157 return theArg;
158}
4ef015cd 159
09e6702a 160
d8f6d49f 161void handleTCPClientWritable(int fd, FDMultiplexer::funcparam_t& var);
09e6702a 162
50c81227 163// -1 is error, 0 is timeout, 1 is success
5c633640
BH
164int asendtcp(const string& data, Socket* sock)
165{
166 PacketID pident;
167 pident.sock=sock;
168 pident.outMSG=data;
23db0a09 169
bb4bdbaf 170 t_fdm->addWriteFD(sock->getHandle(), handleTCPClientWritable, pident);
50c81227 171 string packet;
5c633640 172
5b0ddd18 173 int ret=MT->waitEvent(pident, &packet, g_networkTimeoutMsec);
23db0a09 174
9170fbaf 175 if(!ret || ret==-1) { // timeout
bb4bdbaf 176 t_fdm->removeWriteFD(sock->getHandle());
5c633640 177 }
50c81227
BH
178 else if(packet.size() !=data.size()) { // main loop tells us what it sent out, or empty in case of an error
179 return -1;
180 }
9170fbaf 181 return ret;
5c633640
BH
182}
183
d8f6d49f 184void handleTCPClientReadable(int fd, FDMultiplexer::funcparam_t& var);
09e6702a 185
9170fbaf 186// -1 is error, 0 is timeout, 1 is success
5c633640 187int arecvtcp(string& data, int len, Socket* sock)
288f4aa9 188{
50c81227 189 data.clear();
5c633640
BH
190 PacketID pident;
191 pident.sock=sock;
192 pident.inNeeded=len;
bb4bdbaf 193 t_fdm->addReadFD(sock->getHandle(), handleTCPClientReadable, pident);
5c633640 194
bb4bdbaf 195 int ret=MT->waitEvent(pident,&data, g_networkTimeoutMsec);
9170fbaf 196 if(!ret || ret==-1) { // timeout
bb4bdbaf 197 t_fdm->removeReadFD(sock->getHandle());
288f4aa9 198 }
50c81227
BH
199 else if(data.empty()) {// error, EOF or other
200 return -1;
201 }
202
9170fbaf 203 return ret;
288f4aa9
BH
204}
205
5a38281c 206vector<ComboAddress> g_localQueryAddresses4, g_localQueryAddresses6;
046c5a5d 207const ComboAddress g_local4("0.0.0.0"), g_local6("::");
1652a63e 208
d7dae798 209//! pick a random query local address
1652a63e 210ComboAddress getQueryLocalAddress(int family, uint16_t port)
5a38281c 211{
1652a63e 212 ComboAddress ret;
5a38281c 213 if(family==AF_INET) {
1652a63e
BH
214 if(g_localQueryAddresses4.empty())
215 ret = g_local4;
216 else
217 ret = g_localQueryAddresses4[dns_random(g_localQueryAddresses4.size())];
218 ret.sin4.sin_port = htons(port);
5a38281c
BH
219 }
220 else {
221 if(g_localQueryAddresses6.empty())
1652a63e
BH
222 ret = g_local6;
223 else
224 ret = g_localQueryAddresses6[dns_random(g_localQueryAddresses6.size())];
225
226 ret.sin6.sin6_port = htons(port);
5a38281c 227 }
1652a63e 228 return ret;
5a38281c 229}
4ef015cd 230
d8f6d49f 231void handleUDPServerResponse(int fd, FDMultiplexer::funcparam_t&);
09e6702a 232
d7dae798
BH
233void setSocketBuffer(int fd, int optname, uint32_t size)
234{
235 uint32_t psize=0;
236 socklen_t len=sizeof(psize);
237
238 if(!getsockopt(fd, SOL_SOCKET, optname, (char*)&psize, &len) && psize > size) {
239 L<<Logger::Error<<"Not decreasing socket buffer size from "<<psize<<" to "<<size<<endl;
240 return;
241 }
242
243 if (setsockopt(fd, SOL_SOCKET, optname, (char*)&size, sizeof(size)) < 0 )
244 L<<Logger::Error<<"Warning: unable to raise socket buffer size to "<<size<<": "<<strerror(errno)<<endl;
245}
246
247
248static void setSocketReceiveBuffer(int fd, uint32_t size)
249{
250 setSocketBuffer(fd, SO_RCVBUF, size);
251}
252
253static void setSocketSendBuffer(int fd, uint32_t size)
254{
255 setSocketBuffer(fd, SO_SNDBUF, size);
256}
257
258
4ef015cd
BH
259// you can ask this class for a UDP socket to send a query from
260// this socket is not yours, don't even think about deleting it
261// but after you call 'returnSocket' on it, don't assume anything anymore
262class UDPClientSocks
263{
4ef015cd
BH
264 unsigned int d_numsocks;
265 unsigned int d_maxsocks;
266public:
998a4334 267 UDPClientSocks() : d_numsocks(0), d_maxsocks(5000)
4ef015cd
BH
268 {
269 }
270
996c89cc 271 typedef set<int> socks_t;
4ef015cd
BH
272 socks_t d_socks;
273
d8f6d49f
BH
274 // returning -1 means: temporary OS error (ie, out of files), -2 means OS error
275 int getSocket(const ComboAddress& toaddr, int* fd)
4ef015cd 276 {
d8f6d49f
BH
277 *fd=makeClientSocket(toaddr.sin4.sin_family);
278 if(*fd < 0) // temporary error - receive exception otherwise
279 return -1;
280
281 if(connect(*fd, (struct sockaddr*)(&toaddr), toaddr.getSocklen()) < 0) {
282 int err = errno;
41ff43f8
BH
283 // returnSocket(*fd);
284 Utility::closesocket(*fd);
d8f6d49f 285 if(err==ENETUNREACH) // Seth "My Interfaces Are Like A Yo Yo" Arnold special
4957a608 286 return -2;
998a4334 287 return -1;
d8f6d49f 288 }
998a4334 289
d8f6d49f 290 d_socks.insert(*fd);
998a4334 291 d_numsocks++;
d8f6d49f 292 return 0;
4ef015cd
BH
293 }
294
095c3045
BH
295 void returnSocket(int fd)
296 {
297 socks_t::iterator i=d_socks.find(fd);
34801ab1
BH
298 if(i==d_socks.end()) {
299 throw AhuException("Trying to return a socket (fd="+lexical_cast<string>(fd)+") not in the pool");
300 }
bb4bdbaf 301 returnSocketLocked(i);
095c3045
BH
302 }
303
4ef015cd 304 // return a socket to the pool, or simply erase it
bb4bdbaf 305 void returnSocketLocked(socks_t::iterator& i)
4ef015cd 306 {
600fc20b
BH
307 if(i==d_socks.end()) {
308 throw AhuException("Trying to return a socket not in the pool");
309 }
80baf329 310 try {
bb4bdbaf 311 t_fdm->removeReadFD(*i);
80baf329
BH
312 }
313 catch(FDMultiplexerException& e) {
bb4bdbaf 314 // we sometimes return a socket that has not yet been assigned to t_fdm
80baf329 315 }
c038218b 316 Utility::closesocket(*i);
998a4334
BH
317
318 d_socks.erase(i++);
319 --d_numsocks;
4ef015cd 320 }
d8f6d49f
BH
321
322 // returns -1 for errors which might go away, throws for ones that won't
bb4bdbaf 323 static int makeClientSocket(int family)
d8f6d49f
BH
324 {
325 int ret=(int)socket(family, SOCK_DGRAM, 0);
326 if(ret < 0 && errno==EMFILE) // this is not a catastrophic error
327 return ret;
328
329 if(ret<0)
330 throw AhuException("Making a socket for resolver: "+stringerror());
5a38281c 331
d8f6d49f
BH
332
333 int tries=10;
334 while(--tries) {
1652a63e
BH
335 uint16_t port;
336
d8f6d49f 337 if(tries==1) // fall back to kernel 'random'
4957a608 338 port = 0;
1652a63e
BH
339 else
340 port = 1025 + dns_random(64510);
341
342 ComboAddress sin=getQueryLocalAddress(family, port); // does htons for us
5a38281c 343
5a38281c 344 if (::bind(ret, (struct sockaddr *)&sin, sin.getSocklen()) >= 0)
4957a608 345 break;
d8f6d49f
BH
346 }
347 if(!tries)
348 throw AhuException("Resolver binding to local query client socket: "+stringerror());
349
350 Utility::setNonBlocking(ret);
351 return ret;
352 }
49a699c4
BH
353};
354
355static __thread UDPClientSocks* t_udpclientsocks;
4ef015cd 356
288f4aa9 357/* these two functions are used by LWRes */
34801ab1 358// -2 is OS error, -1 is error that depends on the remote, > 0 is success
787e5eab 359int asendto(const char *data, int len, int flags,
4957a608 360 const ComboAddress& toaddr, uint16_t id, const string& domain, uint16_t qtype, int* fd)
288f4aa9 361{
34801ab1
BH
362
363 PacketID pident;
787e5eab
BH
364 pident.domain = domain;
365 pident.remote = toaddr;
366 pident.type = qtype;
34801ab1
BH
367
368 // see if there is an existing outstanding request we can chain on to, using partial equivalence function
369 pair<MT_t::waiters_t::iterator, MT_t::waiters_t::iterator> chain=MT->d_waiters.equal_range(pident, PacketIDBirthdayCompare());
370
371 for(; chain.first != chain.second; chain.first++) {
372 if(chain.first->key.fd > -1) { // don't chain onto existing chained waiter!
e27e91a8 373 /*
4665c31e
BH
374 cerr<<"Orig: "<<pident.domain<<", "<<pident.remote.toString()<<", id="<<id<<endl;
375 cerr<<"Had hit: "<< chain.first->key.domain<<", "<<chain.first->key.remote.toString()<<", id="<<chain.first->key.id
4957a608 376 <<", count="<<chain.first->key.chain.size()<<", origfd: "<<chain.first->key.fd<<endl;
e27e91a8 377 */
34801ab1
BH
378 chain.first->key.chain.insert(id); // we can chain
379 *fd=-1; // gets used in waitEvent / sendEvent later on
380 return 1;
381 }
382 }
383
49a699c4 384 int ret=t_udpclientsocks->getSocket(toaddr, fd);
d8f6d49f
BH
385 if(ret < 0)
386 return ret;
34801ab1 387
998a4334
BH
388 pident.fd=*fd;
389 pident.id=id;
998a4334 390
bb4bdbaf
BH
391 t_fdm->addReadFD(*fd, handleUDPServerResponse, pident);
392 ret = send(*fd, data, len, 0);
393
5b0ddd18 394 int tmp = errno;
bb4bdbaf 395
7302ed0a 396 if(ret < 0)
49a699c4 397 t_udpclientsocks->returnSocket(*fd);
bb4bdbaf 398
5b0ddd18 399 errno = tmp; // this is for logging purposes only
7302ed0a 400 return ret;
288f4aa9
BH
401}
402
9170fbaf 403// -1 is error, 0 is timeout, 1 is success
787e5eab 404int arecvfrom(char *data, int len, int flags, const ComboAddress& fromaddr, int *d_len,
4957a608 405 uint16_t id, const string& domain, uint16_t qtype, int fd, struct timeval* now)
288f4aa9 406{
0d5f0a9f
BH
407 static optional<unsigned int> nearMissLimit;
408 if(!nearMissLimit)
409 nearMissLimit=::arg().asNum("spoof-nearmiss-max");
410
288f4aa9 411 PacketID pident;
4ef015cd 412 pident.fd=fd;
288f4aa9 413 pident.id=id;
0d5f0a9f 414 pident.domain=domain;
787e5eab 415 pident.type = qtype;
996c89cc 416 pident.remote=fromaddr;
b636533b 417
288f4aa9 418 string packet;
5b0ddd18 419 int ret=MT->waitEvent(pident, &packet, g_networkTimeoutMsec, now);
34801ab1 420
9170fbaf 421 if(ret > 0) {
996c89cc 422 if(packet.empty()) // means "error"
998a4334 423 return -1;
998a4334 424
705f31ae 425 *d_len=(int)packet.size();
9170fbaf 426 memcpy(data,packet.c_str(),min(len,*d_len));
0d5f0a9f 427 if(*nearMissLimit && pident.nearMisses > *nearMissLimit) {
996c89cc 428 L<<Logger::Error<<"Too many ("<<pident.nearMisses<<" > "<<*nearMissLimit<<") bogus answers for '"<<domain<<"' from "<<fromaddr.toString()<<", assuming spoof attempt."<<endl;
0d5f0a9f 429 g_stats.spoofCount++;
35ce8576
BH
430 return -1;
431 }
288f4aa9 432 }
09e6702a 433 else {
34801ab1 434 if(fd >= 0)
49a699c4 435 t_udpclientsocks->returnSocket(fd);
09e6702a 436 }
9170fbaf 437 return ret;
288f4aa9
BH
438}
439
aa4e4cbf 440
87a5ea63 441string s_pidfname;
88def049
BH
442static void writePid(void)
443{
18e7758c 444 ofstream of(s_pidfname.c_str(), std::ios_base::app);
88def049 445 if(of)
705f31ae 446 of<< Utility::getpid() <<endl;
88def049 447 else
87a5ea63 448 L<<Logger::Error<<"Requested to write pid for "<<Utility::getpid()<<" to "<<s_pidfname<<" failed: "<<strerror(errno)<<endl;
88def049
BH
449}
450
bd0289fc
BH
451typedef map<ComboAddress, uint32_t, ComboAddress::addressOnlyLessThan> tcpClientCounts_t;
452tcpClientCounts_t __thread* t_tcpClientCounts;
0e9d9ce2 453
0e408828 454
cd989c87
BH
455TCPConnection::TCPConnection(int fd, const ComboAddress& addr) : d_remote(addr), d_fd(fd)
456{
1bc9e6bd 457 ++s_currentConnections;
cd989c87 458 (*t_tcpClientCounts)[d_remote]++;
0e408828 459}
cd989c87
BH
460
461TCPConnection::~TCPConnection()
0e408828 462{
cd989c87
BH
463 if(Utility::closesocket(d_fd) < 0)
464 unixDie("closing socket for TCPConnection");
465 if(t_tcpClientCounts->count(d_remote) && !(*t_tcpClientCounts)[d_remote]--)
466 t_tcpClientCounts->erase(d_remote);
1bc9e6bd 467 --s_currentConnections;
0e408828 468}
0e9d9ce2 469
1bc9e6bd 470AtomicCounter TCPConnection::s_currentConnections;
d8f6d49f 471void handleRunningTCPQuestion(int fd, FDMultiplexer::funcparam_t& var);
6dcd28c3 472
2cc13433
BH
473void updateRcodeStats(int res)
474{
475 switch(res) {
476 case RCode::ServFail:
477 g_stats.servFails++;
478 break;
479 case RCode::NXDomain:
480 g_stats.nxDomains++;
481 break;
482 case RCode::NoError:
483 g_stats.noErrors++;
484 break;
485 }
486}
487
288f4aa9
BH
488void startDoResolve(void *p)
489{
7b1469bb 490 DNSComboWriter* dc=(DNSComboWriter *)p;
b23b8614 491
288f4aa9 492 try {
10321a98 493 uint16_t maxudpsize=512;
7f7b8d55
BH
494 EDNSOpts edo;
495 if(getEDNSOpts(dc->d_mdp, &edo)) {
54cf6f3e 496 maxudpsize=max(edo.d_packetsize, (uint16_t)1280);
10321a98 497 }
09e6702a 498
ea634573 499 vector<DNSResourceRecord> ret;
ea634573 500 vector<uint8_t> packet;
b23b8614
BH
501
502 DNSPacketWriter pw(packet, dc->d_mdp.d_qname, dc->d_mdp.d_qtype, dc->d_mdp.d_qclass);
ea634573
BH
503
504 pw.getHeader()->aa=0;
505 pw.getHeader()->ra=1;
c154c8a4 506 pw.getHeader()->qr=1;
bb4bdbaf 507 pw.getHeader()->tc=0;
ea634573 508 pw.getHeader()->id=dc->d_mdp.d_header.id;
10321a98 509 pw.getHeader()->rd=dc->d_mdp.d_header.rd;
ea634573 510
c9e9e5e0 511 SyncRes sr(dc->d_now);
1d5b3ce6 512 if(!g_quiet)
bb4bdbaf 513 L<<Logger::Error<<t_id<<" ["<<MT->getTid()<<"] " << (dc->d_tcp ? "TCP " : "") << "question for '"<<dc->d_mdp.d_qname<<"|"
8a63d3ce 514 <<DNSRecordContent::NumberToType(dc->d_mdp.d_qtype)<<"' from "<<dc->getRemote()<<endl;
c75a6a9e 515
fededf47 516 sr.setId(MT->getTid());
67828389 517 if(!dc->d_mdp.d_header.rd)
c836dc19
BH
518 sr.setCacheOnly();
519
4485aa35
BH
520 int res;
521
99c69ed3 522 bool variableAnswer = false;
50a5ef72 523 // if there is a PowerDNSLua active, and it 'took' the query in preResolve, we don't launch beginResolve
99c69ed3 524 if(!t_pdl->get() || !(*t_pdl)->preresolve(dc->d_remote, g_listenSocketsAddresses[dc->d_socket], dc->d_mdp.d_qname, QType(dc->d_mdp.d_qtype), ret, res, &variableAnswer)) {
4485aa35
BH
525 res = sr.beginResolve(dc->d_mdp.d_qname, QType(dc->d_mdp.d_qtype), dc->d_mdp.d_qclass, ret);
526
674cf0f6
BH
527 if(t_pdl->get()) {
528 if(res == RCode::NXDomain)
99c69ed3 529 (*t_pdl)->nxdomain(dc->d_remote, g_listenSocketsAddresses[dc->d_socket], dc->d_mdp.d_qname, QType(dc->d_mdp.d_qtype), ret, res, &variableAnswer);
674cf0f6 530 }
4485aa35 531 }
99c69ed3 532
18e7758c 533 uint32_t minTTL=std::numeric_limits<uint32_t>::max();
1d5b3ce6 534 if(res<0) {
ea634573 535 pw.getHeader()->rcode=RCode::ServFail;
bec87d21 536 // no commit here, because no record
1d5b3ce6
BH
537 g_stats.servFails++;
538 }
288f4aa9 539 else {
ea634573 540 pw.getHeader()->rcode=res;
2cc13433
BH
541 updateRcodeStats(res);
542
c154c8a4 543 if(ret.size()) {
4957a608
BH
544 shuffle(ret);
545
4957a608
BH
546 for(vector<DNSResourceRecord>::const_iterator i=ret.begin(); i!=ret.end(); ++i) {
547 pw.startRecord(i->qname, i->qtype.getCode(), i->ttl, i->qclass, (DNSPacketWriter::Place)i->d_place);
548 minTTL = min(minTTL, i->ttl);
549 if(i->qtype.getCode() == QType::A) { // blast out A record w/o doing whole dnswriter thing
550 uint32_t ip=0;
551 IpToU32(i->content, &ip);
552 pw.xfr32BitInt(htonl(ip));
553 } else {
554 shared_ptr<DNSRecordContent> drc(DNSRecordContent::mastermake(i->qtype.getCode(), i->qclass, i->content));
555 drc->toPacket(pw);
556 }
557 if(!dc->d_tcp && pw.size() > maxudpsize) {
558 pw.rollback();
559 if(i->d_place==DNSResourceRecord::ANSWER) // only truncate if we actually omitted parts of the answer
560 pw.getHeader()->tc=1;
561 goto sendit; // need to jump over pw.commit
562 }
563 }
b23b8614 564
18af64a8 565 pw.commit();
ea634573 566 }
288f4aa9 567 }
10321a98 568 sendit:;
ea634573 569 if(!dc->d_tcp) {
c038218b 570 sendto(dc->d_socket, (const char*)&*packet.begin(), packet.size(), 0, (struct sockaddr *)(&dc->d_remote), dc->d_remote.getSocklen());
99c69ed3 571 if(!SyncRes::s_nopacketcache && !variableAnswer ) {
49a699c4 572 t_packetCache->insertResponsePacket(string((const char*)&*packet.begin(), packet.size()), g_now.tv_sec,
4957a608 573 min(minTTL,
99c69ed3
BH
574 (pw.getHeader()->rcode == RCode::ServFail) ? SyncRes::s_packetcacheservfailttl : SyncRes::s_packetcachettl
575 )
4957a608 576 );
1051f8a9 577 }
feccc9fc 578 }
9c495589
BH
579 else {
580 char buf[2];
ea634573
BH
581 buf[0]=packet.size()/256;
582 buf[1]=packet.size()%256;
feccc9fc 583
c038218b 584 Utility::iovec iov[2];
feccc9fc 585
ea634573
BH
586 iov[0].iov_base=(void*)buf; iov[0].iov_len=2;
587 iov[1].iov_base=(void*)&*packet.begin(); iov[1].iov_len = packet.size();
feccc9fc 588
c038218b 589 int ret=Utility::writev(dc->d_socket, iov, 2);
0e9d9ce2 590 bool hadError=true;
feccc9fc 591
0e9d9ce2 592 if(ret == 0)
18af64a8 593 L<<Logger::Error<<"EOF writing TCP answer to "<<dc->getRemote()<<endl;
0e9d9ce2 594 else if(ret < 0 )
18af64a8 595 L<<Logger::Error<<"Error writing TCP answer to "<<dc->getRemote()<<": "<< strerror(errno) <<endl;
ea634573 596 else if((unsigned int)ret != 2 + packet.size())
18af64a8 597 L<<Logger::Error<<"Oops, partial answer sent to "<<dc->getRemote()<<" for "<<dc->d_mdp.d_qname<<" (size="<< (2 + packet.size()) <<", sent "<<ret<<")"<<endl;
0e9d9ce2 598 else
18af64a8 599 hadError=false;
09e6702a
BH
600
601 // update tcp connection status, either by closing or moving to 'BYTE0'
18af64a8 602
09e6702a 603 if(hadError) {
18af64a8 604 // no need to remove us from FDM, we weren't there
c36bc97a 605 dc->d_socket = -1;
09e6702a 606 }
a6ae6414 607 else {
cd989c87 608 dc->d_tcpConnection->state=TCPConnection::BYTE0;
18af64a8 609 Utility::gettimeofday(&g_now, 0); // needs to be updated
cd989c87
BH
610 t_fdm->addReadFD(dc->d_socket, handleRunningTCPQuestion, dc->d_tcpConnection);
611 t_fdm->setReadTTD(dc->d_socket, g_now, g_tcpTimeout);
0e9d9ce2 612 }
9c495589 613 }
b23b8614 614
1d5b3ce6 615 if(!g_quiet) {
bb4bdbaf 616 L<<Logger::Error<<t_id<<" ["<<MT->getTid()<<"] answer to "<<(dc->d_mdp.d_header.rd?"":"non-rd ")<<"question '"<<dc->d_mdp.d_qname<<"|"<<DNSRecordContent::NumberToType(dc->d_mdp.d_qtype);
ea634573 617 L<<"': "<<ntohs(pw.getHeader()->ancount)<<" answers, "<<ntohs(pw.getHeader()->arcount)<<" additional, took "<<sr.d_outqueries<<" packets, "<<
18af64a8 618 sr.d_throttledqueries<<" throttled, "<<sr.d_timeouts<<" timeouts, "<<sr.d_tcpoutqueries<<" tcp connections, rcode="<<res<<endl;
c75a6a9e 619 }
b23b8614 620
49a699c4 621 sr.d_outqueries ? t_RC->cacheMisses++ : t_RC->cacheHits++;
fe213470
BH
622 float spent=makeFloat(sr.d_now-dc->d_now);
623 if(spent < 0.001)
624 g_stats.answers0_1++;
625 else if(spent < 0.010)
626 g_stats.answers1_10++;
627 else if(spent < 0.1)
628 g_stats.answers10_100++;
629 else if(spent < 1.0)
630 g_stats.answers100_1000++;
631 else
632 g_stats.answersSlow++;
633
574af7ea 634 uint64_t newLat=(uint64_t)(spent*1000000);
87b8e43a
BH
635 if(newLat < 1000000) // outliers of several minutes exist..
636 g_stats.avgLatencyUsec=(uint64_t)((1-0.0001)*g_stats.avgLatencyUsec + 0.0001*newLat);
b23b8614 637
ea634573 638 delete dc;
c36bc97a 639 dc=0;
288f4aa9
BH
640 }
641 catch(AhuException &ae) {
c836dc19 642 L<<Logger::Error<<"startDoResolve problem: "<<ae.reason<<endl;
c36bc97a 643 delete dc;
288f4aa9 644 }
7b1469bb
BH
645 catch(MOADNSException& e) {
646 L<<Logger::Error<<"DNS parser error: "<<dc->d_mdp.d_qname<<", "<<e.what()<<endl;
c36bc97a 647 delete dc;
7b1469bb 648 }
fdbf35ac 649 catch(std::exception& e) {
c154c8a4 650 L<<Logger::Error<<"STL error: "<<e.what()<<endl;
c36bc97a 651 delete dc;
c154c8a4 652 }
288f4aa9 653 catch(...) {
c836dc19 654 L<<Logger::Error<<"Any other exception in a resolver context"<<endl;
288f4aa9 655 }
ec6eacbc
BH
656
657 g_stats.maxMThreadStackUsage = max(MT->getMaxStackUsage(), g_stats.maxMThreadStackUsage);
288f4aa9
BH
658}
659
677e2a46 660void makeControlChannelSocket(int processNum=-1)
1d5b3ce6 661{
677e2a46
BH
662 string sockname=::arg()["socket-dir"]+"/pdns_recursor";
663 if(processNum >= 0)
664 sockname += "."+lexical_cast<string>(processNum);
665 sockname+=".controlsocket";
41f7a068 666 s_rcc.listen(sockname);
387de317
BH
667
668#ifndef WIN32
669 int sockowner = -1;
670 int sockgroup = -1;
671
672 if (!::arg().isEmpty("socket-group"))
673 sockgroup=::arg().asGid("socket-group");
674 if (!::arg().isEmpty("socket-owner"))
675 sockowner=::arg().asUid("socket-owner");
676
f838ad8d
BH
677 if (sockgroup > -1 || sockowner > -1) {
678 if(chown(sockname.c_str(), sockowner, sockgroup) < 0) {
679 unixDie("Failed to chown control socket");
680 }
681 }
387de317
BH
682
683 // do mode change if socket-mode is given
684 if(!::arg().isEmpty("socket-mode")) {
685 mode_t sockmode=::arg().asMode("socket-mode");
686 chmod(sockname.c_str(), sockmode);
687 }
688#endif
1d5b3ce6
BH
689}
690
d8f6d49f 691void handleRunningTCPQuestion(int fd, FDMultiplexer::funcparam_t& var)
09e6702a 692{
cd989c87 693 shared_ptr<TCPConnection> conn=any_cast<shared_ptr<TCPConnection> >(var);
c038218b 694
879b3f70 695 if(conn->state==TCPConnection::BYTE0) {
cd989c87 696 int bytes=recv(conn->getFD(), conn->data, 2, 0);
09e6702a 697 if(bytes==1)
667f7e60 698 conn->state=TCPConnection::BYTE1;
09e6702a 699 if(bytes==2) {
a0aa4f64 700 conn->qlen=(((unsigned char)conn->data[0]) << 8)+ (unsigned char)conn->data[1];
667f7e60
BH
701 conn->bytesread=0;
702 conn->state=TCPConnection::GETQUESTION;
09e6702a
BH
703 }
704 if(!bytes || bytes < 0) {
bb4bdbaf 705 t_fdm->removeReadFD(fd);
09e6702a
BH
706 return;
707 }
708 }
667f7e60 709 else if(conn->state==TCPConnection::BYTE1) {
cd989c87 710 int bytes=recv(conn->getFD(), conn->data+1, 1, 0);
09e6702a 711 if(bytes==1) {
667f7e60 712 conn->state=TCPConnection::GETQUESTION;
a0aa4f64 713 conn->qlen=(((unsigned char)conn->data[0]) << 8)+ (unsigned char)conn->data[1];
667f7e60 714 conn->bytesread=0;
09e6702a
BH
715 }
716 if(!bytes || bytes < 0) {
717 if(g_logCommonErrors)
cd989c87 718 L<<Logger::Error<<"TCP client "<< conn->d_remote.toString() <<" disconnected after first byte"<<endl;
bb4bdbaf 719 t_fdm->removeReadFD(fd);
09e6702a
BH
720 return;
721 }
722 }
667f7e60 723 else if(conn->state==TCPConnection::GETQUESTION) {
cd989c87 724 int bytes=recv(conn->getFD(), conn->data + conn->bytesread, conn->qlen - conn->bytesread, 0);
09e6702a 725 if(!bytes || bytes < 0) {
cd989c87 726 L<<Logger::Error<<"TCP client "<< conn->d_remote.toString() <<" disconnected while reading question body"<<endl;
bb4bdbaf 727 t_fdm->removeReadFD(fd);
09e6702a
BH
728 return;
729 }
667f7e60
BH
730 conn->bytesread+=bytes;
731 if(conn->bytesread==conn->qlen) {
bb4bdbaf 732 t_fdm->removeReadFD(fd); // should no longer awake ourselves when there is data to read
879b3f70 733
09e6702a
BH
734 DNSComboWriter* dc=0;
735 try {
cd989c87 736 dc=new DNSComboWriter(conn->data, conn->qlen, g_now);
09e6702a
BH
737 }
738 catch(MOADNSException &mde) {
4957a608
BH
739 g_stats.clientParseError++;
740 if(g_logCommonErrors)
cd989c87 741 L<<Logger::Error<<"Unable to parse packet from TCP client "<< conn->d_remote.toString() <<endl;
4957a608 742 return;
09e6702a 743 }
cd989c87
BH
744 dc->d_tcpConnection = conn; // carry the torch
745 dc->setSocket(conn->getFD()); // this is the only time a copy is made of the actual fd
09e6702a 746 dc->d_tcp=true;
cd989c87 747 dc->setRemote(&conn->d_remote);
879b3f70 748 if(dc->d_mdp.d_header.qr) {
4957a608
BH
749 delete dc;
750 L<<Logger::Error<<"Ignoring answer on server socket!"<<endl;
4957a608 751 return;
879b3f70 752 }
09e6702a 753 else {
4957a608
BH
754 ++g_stats.qcounter;
755 ++g_stats.tcpqcounter;
50a5ef72 756 MT->makeThread(startDoResolve, dc); // deletes dc, will set state to BYTE0 again
4957a608 757 return;
09e6702a
BH
758 }
759 }
760 }
761}
762
6dcd28c3 763//! Handle new incoming TCP connection
d8f6d49f 764void handleNewTCPQuestion(int fd, FDMultiplexer::funcparam_t& )
09e6702a 765{
37d3f960 766 ComboAddress addr;
09e6702a 767 socklen_t addrlen=sizeof(addr);
705f31ae 768 int newsock=(int)accept(fd, (struct sockaddr*)&addr, &addrlen);
09e6702a 769 if(newsock>0) {
85c32340
BH
770 if(MT->numProcesses() > g_maxMThreads) {
771 g_stats.overCapacityDrops++;
772 Utility::closesocket(newsock);
773 return;
774 }
775
b3b5459d 776 t_remotes->addRemote(addr);
49a699c4 777 if(t_allowFrom && !t_allowFrom->match(&addr)) {
2914b022 778 if(!g_quiet)
4957a608 779 L<<Logger::Error<<"["<<MT->getTid()<<"] dropping TCP query from "<<addr.toString()<<", address not matched by allow-from"<<endl;
2914b022 780
09e6702a 781 g_stats.unauthorizedTCP++;
705f31ae 782 Utility::closesocket(newsock);
09e6702a
BH
783 return;
784 }
bd0289fc 785 if(g_maxTCPPerClient && t_tcpClientCounts->count(addr) && (*t_tcpClientCounts)[addr] >= g_maxTCPPerClient) {
09e6702a 786 g_stats.tcpClientOverflow++;
705f31ae 787 Utility::closesocket(newsock); // don't call TCPConnection::closeAndCleanup here - did not enter it in the counts yet!
09e6702a
BH
788 return;
789 }
cd989c87 790
09e6702a 791 Utility::setNonBlocking(newsock);
cd989c87
BH
792 shared_ptr<TCPConnection> tc(new TCPConnection(newsock, addr));
793 tc->state=TCPConnection::BYTE0;
794
795 t_fdm->addReadFD(tc->getFD(), handleRunningTCPQuestion, tc);
c038218b 796
0bff046b 797 struct timeval now;
c038218b 798 Utility::gettimeofday(&now, 0);
cd989c87 799 t_fdm->setReadTTD(tc->getFD(), now, g_tcpTimeout);
09e6702a
BH
800 }
801}
2914b022 802
1bc3c142
BH
803string* doProcessUDPQuestion(const std::string& question, const ComboAddress& fromaddr, int fd)
804{
805 ++g_stats.qcounter;
806
807 string response;
808 try {
809 uint32_t age;
810 if(!SyncRes::s_nopacketcache && t_packetCache->getResponsePacket(question, g_now.tv_sec, &response, &age)) {
811 if(!g_quiet)
44b67752 812 L<<Logger::Error<<t_id<< " question answered from packet cache from "<<fromaddr.toString()<<endl;
1bc3c142
BH
813
814 g_stats.packetCacheHits++;
815 SyncRes::s_queries++;
816 ageDNSPacket(response, age);
817 sendto(fd, response.c_str(), response.length(), 0, (struct sockaddr*) &fromaddr, fromaddr.getSocklen());
818 if(response.length() >= sizeof(struct dnsheader))
44b67752 819 updateRcodeStats(((struct dnsheader*)response.c_str())->rcode);
1bc3c142
BH
820 g_stats.avgLatencyUsec=(uint64_t)((1-0.0001)*g_stats.avgLatencyUsec + 0); // we assume 0 usec
821 return 0;
822 }
823 }
824 catch(std::exception& e) {
825 L<<Logger::Error<<"Error processing or aging answer packet: "<<e.what()<<endl;
826 return 0;
827 }
828
829
830 if(MT->numProcesses() > g_maxMThreads) {
831 g_stats.overCapacityDrops++;
832 return 0;
833 }
834
835 DNSComboWriter* dc = new DNSComboWriter(question.c_str(), question.size(), g_now);
836 dc->setSocket(fd);
837 dc->setRemote(&fromaddr);
838
839 dc->d_tcp=false;
840 MT->makeThread(startDoResolve, (void*) dc); // deletes dc
841 return 0;
842}
843
d8f6d49f 844void handleNewUDPQuestion(int fd, FDMultiplexer::funcparam_t& var)
5db529f8 845{
a9af3782 846 int len;
5db529f8
BH
847 char data[1500];
848 ComboAddress fromaddr;
849 socklen_t addrlen=sizeof(fromaddr);
85c32340 850
a9af3782 851 if((len=recvfrom(fd, data, sizeof(data), 0, (sockaddr *)&fromaddr, &addrlen)) >= 0) {
b3b5459d 852 t_remotes->addRemote(fromaddr);
b23b8614 853
49a699c4 854 if(t_allowFrom && !t_allowFrom->match(&fromaddr)) {
2914b022 855 if(!g_quiet)
4957a608 856 L<<Logger::Error<<"["<<MT->getTid()<<"] dropping UDP query from "<<fromaddr.toString()<<", address not matched by allow-from"<<endl;
2914b022 857
5db529f8 858 g_stats.unauthorizedUDP++;
a9af3782 859 return;
5db529f8 860 }
5db529f8 861 try {
b23b8614 862 dnsheader* dh=(dnsheader*)data;
5db529f8 863
b23b8614 864 if(dh->qr) {
4957a608
BH
865 if(g_logCommonErrors)
866 L<<Logger::Error<<"Ignoring answer from "<<fromaddr.toString()<<" on server socket!"<<endl;
5db529f8
BH
867 }
868 else {
1bc3c142
BH
869 string question(data, len);
870 if(g_weDistributeQueries)
871 distributeAsyncFunction(boost::bind(doProcessUDPQuestion, question, fromaddr, fd));
872 else
873 doProcessUDPQuestion(question, fromaddr, fd);
5db529f8
BH
874 }
875 }
876 catch(MOADNSException& mde) {
877 g_stats.clientParseError++;
84e66a59 878 if(g_logCommonErrors)
4957a608 879 L<<Logger::Error<<"Unable to parse packet from remote UDP client "<<fromaddr.toString() <<": "<<mde.what()<<endl;
5db529f8
BH
880 }
881 }
ac0e821b
BH
882 else {
883 // cerr<<t_id<<" had error: "<<stringerror()<<endl;
9326cae1
BH
884 if(errno == EAGAIN)
885 g_stats.noPacketError++;
ac0e821b 886 }
5db529f8
BH
887}
888
1bc3c142 889
5db529f8
BH
890typedef vector<pair<int, function< void(int, any&) > > > deferredAdd_t;
891deferredAdd_t deferredAdd;
892
f28307ad 893void makeTCPServerSockets()
9c495589 894{
37d3f960 895 int fd;
f28307ad 896 vector<string>locals;
2e3d8a19 897 stringtok(locals,::arg()["local-address"]," ,");
9c495589 898
f28307ad
BH
899 if(locals.empty())
900 throw AhuException("No local address specified");
901
f28307ad 902 for(vector<string>::const_iterator i=locals.begin();i!=locals.end();++i) {
32252594
BH
903 ServiceTuple st;
904 st.port=::arg().asNum("local-port");
905 parseService(*i, st);
906
907 ComboAddress sin;
908
f28307ad 909 memset((char *)&sin,0, sizeof(sin));
37d3f960 910 sin.sin4.sin_family = AF_INET;
32252594 911 if(!IpToU32(st.host, (uint32_t*)&sin.sin4.sin_addr.s_addr)) {
37d3f960 912 sin.sin6.sin6_family = AF_INET6;
f71bc087 913 if(makeIPv6sockaddr(st.host, &sin.sin6) < 0)
4957a608 914 throw AhuException("Unable to resolve local address for TCP server on '"+ st.host +"'");
37d3f960
BH
915 }
916
917 fd=socket(sin.sin6.sin6_family, SOCK_STREAM, 0);
918 if(fd<0)
919 throw AhuException("Making a TCP server socket for resolver: "+stringerror());
f28307ad
BH
920
921 int tmp=1;
37d3f960 922 if(setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,(char*)&tmp,sizeof tmp)<0) {
f28307ad 923 L<<Logger::Error<<"Setsockopt failed for TCP listening socket"<<endl;
c8ddb7c2 924 exit(1);
f28307ad
BH
925 }
926
c8ddb7c2 927#ifdef TCP_DEFER_ACCEPT
37d3f960
BH
928 if(setsockopt(fd, SOL_TCP,TCP_DEFER_ACCEPT,(char*)&tmp,sizeof tmp) >= 0) {
929 if(i==locals.begin())
4957a608 930 L<<Logger::Error<<"Enabled TCP data-ready filter for (slight) DoS protection"<<endl;
c8ddb7c2
BH
931 }
932#endif
933
32252594 934 sin.sin4.sin_port = htons(st.port);
37d3f960
BH
935 int socklen=sin.sin4.sin_family==AF_INET ? sizeof(sin.sin4) : sizeof(sin.sin6);
936 if (::bind(fd, (struct sockaddr *)&sin, socklen )<0)
32252594 937 throw AhuException("Binding TCP server socket for "+ st.host +": "+stringerror());
f28307ad 938
37d3f960 939 Utility::setNonBlocking(fd);
49a699c4 940 setSocketSendBuffer(fd, 65000);
37d3f960 941 listen(fd, 128);
5db529f8 942 deferredAdd.push_back(make_pair(fd, handleNewTCPQuestion));
c2136bf0
BH
943 g_tcpListenSockets.push_back(fd);
944
aa136564 945 if(sin.sin4.sin_family == AF_INET)
32252594 946 L<<Logger::Error<<"Listening for TCP queries on "<< sin.toString() <<":"<<st.port<<endl;
aa136564 947 else
32252594 948 L<<Logger::Error<<"Listening for TCP queries on ["<< sin.toString() <<"]:"<<st.port<<endl;
f28307ad 949 }
9c495589
BH
950}
951
bb4bdbaf
BH
952
953
f28307ad 954void makeUDPServerSockets()
288f4aa9 955{
f28307ad 956 vector<string>locals;
2e3d8a19 957 stringtok(locals,::arg()["local-address"]," ,");
288f4aa9 958
f28307ad
BH
959 if(locals.empty())
960 throw AhuException("No local address specified");
961
2e3d8a19 962 if(::arg()["local-address"]=="0.0.0.0") {
c836dc19 963 L<<Logger::Warning<<"It is advised to bind to explicit addresses with the --local-address option"<<endl;
288f4aa9 964 }
525b8a7c 965
f28307ad 966 for(vector<string>::const_iterator i=locals.begin();i!=locals.end();++i) {
32252594
BH
967 ServiceTuple st;
968 st.port=::arg().asNum("local-port");
969 parseService(*i, st);
970
37d3f960 971 ComboAddress sin;
996c89cc 972
37d3f960
BH
973 memset(&sin, 0, sizeof(sin));
974 sin.sin4.sin_family = AF_INET;
32252594 975 if(!IpToU32(st.host.c_str() , (uint32_t*)&sin.sin4.sin_addr.s_addr)) {
37d3f960 976 sin.sin6.sin6_family = AF_INET6;
f71bc087 977 if(makeIPv6sockaddr(st.host, &sin.sin6) < 0)
4957a608 978 throw AhuException("Unable to resolve local address for UDP server on '"+ st.host +"'");
37d3f960
BH
979 }
980
bb4bdbaf
BH
981 int fd=socket(sin.sin4.sin_family, SOCK_DGRAM, 0);
982
d3b4137e
BH
983 if(fd < 0) {
984 throw AhuException("Making a UDP server socket for resolver: "+netstringerror());
985 }
37d3f960 986
49a699c4 987 setSocketReceiveBuffer(fd, 200000);
32252594 988 sin.sin4.sin_port = htons(st.port);
37d3f960
BH
989
990 int socklen=sin.sin4.sin_family==AF_INET ? sizeof(sin.sin4) : sizeof(sin.sin6);
991 if (::bind(fd, (struct sockaddr *)&sin, socklen)<0)
32252594 992 throw AhuException("Resolver binding to server socket on port "+ lexical_cast<string>(st.port) +" for "+ st.host+": "+stringerror());
f28307ad
BH
993
994 Utility::setNonBlocking(fd);
c2136bf0 995
0aaecd50 996 deferredAdd.push_back(make_pair(fd, handleNewUDPQuestion));
40a3dd64 997 g_listenSocketsAddresses[fd]=sin; // this is written to only from the startup thread, not from the workers
aa136564 998 if(sin.sin4.sin_family == AF_INET)
32252594 999 L<<Logger::Error<<"Listening for UDP queries on "<< sin.toString() <<":"<<st.port<<endl;
aa136564 1000 else
32252594 1001 L<<Logger::Error<<"Listening for UDP queries on ["<< sin.toString() <<"]:"<<st.port<<endl;
f28307ad 1002 }
c836dc19 1003}
caa6eefa 1004
9c495589 1005
caa6eefa 1006#ifndef WIN32
c836dc19
BH
1007void daemonize(void)
1008{
1009 if(fork())
1010 exit(0); // bye bye
1011
1012 setsid();
1013
27a5ead5
BH
1014 int i=open("/dev/null",O_RDWR); /* open stdin */
1015 if(i < 0)
1016 L<<Logger::Critical<<"Unable to open /dev/null: "<<stringerror()<<endl;
1017 else {
1018 dup2(i,0); /* stdin */
1019 dup2(i,1); /* stderr */
1020 dup2(i,2); /* stderr */
1021 close(i);
1022 }
288f4aa9 1023}
caa6eefa
BH
1024#endif
1025
aaacf7f2 1026uint64_t counter;
c75a6a9e
BH
1027bool statsWanted;
1028
1029void usr1Handler(int)
1030{
1031 statsWanted=true;
1032}
ae1b2e98 1033
9170fbaf
BH
1034void usr2Handler(int)
1035{
1036 SyncRes::setLog(true);
1d5b3ce6
BH
1037 g_quiet=false;
1038 ::arg().set("quiet")="no";
c9e9e5e0 1039
9170fbaf
BH
1040}
1041
c75a6a9e
BH
1042void doStats(void)
1043{
16beeaa4
BH
1044 static time_t lastOutputTime;
1045 static uint64_t lastQueryCount;
1046
3427fa8a
BH
1047 if(g_stats.qcounter && (t_RC->cacheHits + t_RC->cacheMisses) && SyncRes::s_queries && SyncRes::s_outqueries) { // this only runs once thread 0 has had hits
1048 uint64_t cacheHits = broadcastAccFunction<uint64_t>(pleaseGetCacheHits);
1049 uint64_t cacheMisses = broadcastAccFunction<uint64_t>(pleaseGetCacheMisses);
1050
1051 L<<Logger::Warning<<"stats: "<<g_stats.qcounter<<" questions, "<<
1052 broadcastAccFunction<uint64_t>(pleaseGetCacheSize)<< " cache entries, "<<
1053 broadcastAccFunction<uint64_t>(pleaseGetNegCacheSize)<<" negative entries, "<<
1054 (int)((cacheHits*100.0)/(cacheHits+cacheMisses))<<"% cache hits"<<endl;
1055
1056 L<<Logger::Warning<<"stats: throttle map: "
1057 << broadcastAccFunction<uint64_t>(pleaseGetThrottleSize) <<", ns speeds: "
1058 << broadcastAccFunction<uint64_t>(pleaseGetNsSpeedsSize)<<endl;
70c2c8b1
BH
1059 L<<Logger::Warning<<"stats: outpacket/query ratio "<<(int)(SyncRes::s_outqueries*100.0/SyncRes::s_queries)<<"%";
1060 L<<Logger::Warning<<", "<<(int)(SyncRes::s_throttledqueries*100.0/(SyncRes::s_outqueries+SyncRes::s_throttledqueries))<<"% throttled, "
525b8a7c 1061 <<SyncRes::s_nodelegated<<" no-delegation drops"<<endl;
3427fa8a
BH
1062 L<<Logger::Warning<<"stats: "<<SyncRes::s_tcpoutqueries<<" outgoing tcp connections, "<<
1063 broadcastAccFunction<uint64_t>(pleaseGetConcurrentQueries)<<" queries running, "<<SyncRes::s_outgoingtimeouts<<" outgoing timeouts"<<endl;
81883dcc 1064
16beeaa4
BH
1065 //L<<Logger::Warning<<"stats: "<<g_stats.ednsPingMatches<<" ping matches, "<<g_stats.ednsPingMismatches<<" mismatches, "<<
1066 //g_stats.noPingOutQueries<<" outqueries w/o ping, "<< g_stats.noEdnsOutQueries<<" w/o EDNS"<<endl;
1067
1068 L<<Logger::Warning<<"stats: " << broadcastAccFunction<uint64_t>(pleaseGetPacketCacheSize) <<
1069 " packet cache entries, "<<(int)(100.0*broadcastAccFunction<uint64_t>(pleaseGetPacketCacheHits)/SyncRes::s_queries) << "% packet cache hits"<<endl;
1070
1071 time_t now = time(0);
1072 if(lastOutputTime && lastQueryCount && now != lastOutputTime) {
1073 L<<Logger::Warning<<"stats: "<< (SyncRes::s_queries - lastQueryCount) / (now - lastOutputTime) <<" qps (average over "<< (now - lastOutputTime) << " seconds)"<<endl;
1074 }
1075 lastOutputTime = now;
1076 lastQueryCount = SyncRes::s_queries;
c75a6a9e 1077 }
7becf07f 1078 else if(statsWanted)
70c2c8b1 1079 L<<Logger::Warning<<"stats: no stats yet!"<<endl;
7becf07f 1080
c75a6a9e
BH
1081 statsWanted=false;
1082}
c836dc19 1083
29f0b1ce 1084static void houseKeeping(void *)
779828c4 1085try
c836dc19 1086{
7393d6c0 1087 static __thread time_t last_stat, last_rootupdate, last_prune;
8baca3fa 1088 static __thread int cleanCounter=0;
c9e9e5e0 1089 struct timeval now;
c038218b 1090 Utility::gettimeofday(&now, 0);
c9e9e5e0 1091
1a16adf0 1092 // clog<<"* "<<t_id<<" "<<(void*)&last_stat<<"\t"<<(unsigned int)last_stat<<endl;
ac0e821b 1093
c3828c03 1094 if(now.tv_sec - last_prune > (time_t)(5 + t_id)) {
5e4a2466
BH
1095 DTime dt;
1096 dt.setTimeval(now);
49a699c4 1097 t_RC->doPrune(); // this function is local to a thread, so fine anyhow
c3828c03 1098 t_packetCache->doPruneTo(::arg().asNum("max-packetcache-entries") / g_numThreads);
33988bfb 1099
1a16adf0 1100 pruneCollection(t_sstorage->negcache, ::arg().asNum("max-cache-entries") / (g_numThreads * 10), 200);
8baca3fa
BH
1101
1102 if(!((cleanCounter++)%40)) { // this is a full scan!
1103 time_t limit=now.tv_sec-300;
ac0e821b 1104 for(SyncRes::nsspeeds_t::iterator i = t_sstorage->nsSpeeds.begin() ; i!= t_sstorage->nsSpeeds.end(); )
8baca3fa 1105 if(i->second.stale(limit))
ac0e821b 1106 t_sstorage->nsSpeeds.erase(i++);
8baca3fa
BH
1107 else
1108 ++i;
1109 }
7393d6c0 1110// L<<Logger::Warning<<"Spent "<<dt.udiff()/1000<<" msec cleaning"<<endl;
ae1b2e98
BH
1111 last_prune=time(0);
1112 }
ac0e821b 1113
3427fa8a 1114 if(!t_id) {
ac0e821b 1115 if(now.tv_sec - last_stat > 1800) {
3427fa8a
BH
1116 doStats();
1117 last_stat=time(0);
1118 }
c836dc19 1119 }
ac0e821b 1120
c038218b 1121 if(now.tv_sec - last_rootupdate > 7200) {
c9e9e5e0 1122 SyncRes sr(now);
2188dcc3 1123 sr.setDoEDNS0(true);
ea634573 1124 vector<DNSResourceRecord> ret;
c836dc19
BH
1125
1126 sr.setNoCache();
a9af3782 1127 int res=sr.beginResolve(".", QType(QType::NS), 1, ret);
c836dc19 1128 if(!res) {
70c2c8b1 1129 L<<Logger::Warning<<"Refreshed . records"<<endl;
c9e9e5e0 1130 last_rootupdate=now.tv_sec;
c836dc19
BH
1131 }
1132 else
1133 L<<Logger::Error<<"Failed to update . records, RCODE="<<res<<endl;
1134 }
1135}
779828c4
BH
1136catch(AhuException& ae)
1137{
1138 L<<Logger::Error<<"Fatal error: "<<ae.reason<<endl;
1139 throw;
1140}
705f31ae 1141;
d6d5dea7 1142
49a699c4
BH
1143void makeThreadPipes()
1144{
c3828c03 1145 for(unsigned int n=0; n < g_numThreads; ++n) {
49a699c4
BH
1146 struct ThreadPipeSet tps;
1147 int fd[2];
1148 if(pipe(fd) < 0)
1149 unixDie("Creating pipe for inter-thread communications");
1150
1151 tps.readToThread = fd[0];
1152 tps.writeToThread = fd[1];
1153
1154 if(pipe(fd) < 0)
1155 unixDie("Creating pipe for inter-thread communications");
1156 tps.readFromThread = fd[0];
1157 tps.writeFromThread = fd[1];
1158
1159 g_pipes.push_back(tps);
1160 }
1161}
1162
00c9b8c1
BH
1163struct ThreadMSG
1164{
1165 pipefunc_t func;
1166 bool wantAnswer;
1167};
1168
49a699c4
BH
1169void broadcastFunction(const pipefunc_t& func, bool skipSelf)
1170{
49a699c4
BH
1171 unsigned int n = 0;
1172 BOOST_FOREACH(ThreadPipeSet& tps, g_pipes)
1173 {
1174 if(n++ == t_id) {
1175 if(!skipSelf)
1176 func(); // don't write to ourselves!
1177 continue;
1178 }
00c9b8c1
BH
1179
1180 ThreadMSG* tmsg = new ThreadMSG();
1181 tmsg->func = func;
1182 tmsg->wantAnswer = true;
1183 if(write(tps.writeToThread, &tmsg, sizeof(tmsg)) != sizeof(tmsg))
49a699c4
BH
1184 unixDie("write to thread pipe returned wrong size or error");
1185
1186 string* resp;
1187 if(read(tps.readFromThread, &resp, sizeof(resp)) != sizeof(resp))
1188 unixDie("read from thread pipe returned wrong size or error");
1189
1190 if(resp) {
1191// cerr <<"got response: " << *resp << endl;
1192 delete resp;
1193 }
1194 }
1195}
00c9b8c1
BH
1196void distributeAsyncFunction(const pipefunc_t& func)
1197{
1198 static unsigned int counter;
1bc3c142 1199 unsigned int target = 1 + (++counter % (g_pipes.size()-1));
00c9b8c1
BH
1200 // cerr<<"Sending to: "<<target<<endl;
1201 if(target == t_id) {
1202 func();
1203 return;
1204 }
1205 ThreadPipeSet& tps = g_pipes[target];
1206 ThreadMSG* tmsg = new ThreadMSG();
1207 tmsg->func = func;
1208 tmsg->wantAnswer = false;
1209
1210 if(write(tps.writeToThread, &tmsg, sizeof(tmsg)) != sizeof(tmsg))
1211 unixDie("write to thread pipe returned wrong size or error");
1212
1213}
3427fa8a 1214
49a699c4
BH
1215void handlePipeRequest(int fd, FDMultiplexer::funcparam_t& var)
1216{
00c9b8c1
BH
1217 ThreadMSG* tmsg;
1218
1219 if(read(fd, &tmsg, sizeof(tmsg)) != sizeof(tmsg)) { // fd == readToThread
49a699c4
BH
1220 unixDie("read from thread pipe returned wrong size or error");
1221 }
3427fa8a 1222
00c9b8c1
BH
1223 void *resp = tmsg->func();
1224 if(tmsg->wantAnswer)
1225 if(write(g_pipes[t_id].writeFromThread, &resp, sizeof(resp)) != sizeof(resp))
1226 unixDie("write to thread pipe returned wrong size or error");
3427fa8a 1227
00c9b8c1 1228 delete tmsg;
49a699c4 1229}
09e6702a 1230
13034931
BH
1231template<class T> void *voider(const boost::function<T*()>& func)
1232{
1233 return func();
1234}
1235
b3b5459d
BH
1236vector<ComboAddress>& operator+=(vector<ComboAddress>&a, const vector<ComboAddress>& b)
1237{
1238 a.insert(a.end(), b.begin(), b.end());
1239 return a;
1240}
1241
13034931 1242template<class T> T broadcastAccFunction(const boost::function<T*()>& func, bool skipSelf)
3427fa8a
BH
1243{
1244 unsigned int n = 0;
1245 T ret=T();
1246 BOOST_FOREACH(ThreadPipeSet& tps, g_pipes)
1247 {
1248 if(n++ == t_id) {
1249 if(!skipSelf) {
1250 T* resp = (T*)func(); // don't write to ourselves!
1251 if(resp) {
1252 //~ cerr <<"got direct: " << *resp << endl;
1253 ret += *resp;
1254 delete resp;
1255 }
1256 }
1257 continue;
1258 }
1259
00c9b8c1
BH
1260 ThreadMSG* tmsg = new ThreadMSG();
1261 tmsg->func = boost::bind(voider<T>, func);
1262 tmsg->wantAnswer = true;
1263
1264 if(write(tps.writeToThread, &tmsg, sizeof(tmsg)) != sizeof(tmsg))
3427fa8a 1265 unixDie("write to thread pipe returned wrong size or error");
00c9b8c1 1266
3427fa8a
BH
1267
1268 T* resp;
1269 if(read(tps.readFromThread, &resp, sizeof(resp)) != sizeof(resp))
1270 unixDie("read from thread pipe returned wrong size or error");
1271
1272 if(resp) {
1273 //~ cerr <<"got response: " << *resp << endl;
1274 ret += *resp;
1275 delete resp;
1276 }
1277 }
1278 return ret;
1279}
1280
13034931
BH
1281template string broadcastAccFunction(const boost::function<string*()>& fun, bool skipSelf); // explicit instantiation
1282template uint64_t broadcastAccFunction(const boost::function<uint64_t*()>& fun, bool skipSelf); // explicit instantiation
b3b5459d 1283template vector<ComboAddress> broadcastAccFunction(const boost::function<vector<ComboAddress> *()>& fun, bool skipSelf); // explicit instantiation
3427fa8a 1284
d8f6d49f 1285void handleRCC(int fd, FDMultiplexer::funcparam_t& var)
09e6702a
BH
1286{
1287 string remote;
1288 string msg=s_rcc.recv(&remote);
1289 RecursorControlParser rcp;
1290 RecursorControlParser::func_t* command;
1291 string answer=rcp.getAnswer(msg, &command);
ab5c053d
BH
1292 try {
1293 s_rcc.send(answer, &remote);
1294 command();
1295 }
fdbf35ac 1296 catch(std::exception& e) {
ab5c053d
BH
1297 L<<Logger::Error<<"Error dealing with control socket request: "<<e.what()<<endl;
1298 }
1299 catch(AhuException& ae) {
1300 L<<Logger::Error<<"Error dealing with control socket request: "<<ae.reason<<endl;
1301 }
09e6702a
BH
1302}
1303
d8f6d49f 1304void handleTCPClientReadable(int fd, FDMultiplexer::funcparam_t& var)
09e6702a 1305{
0b18b22e 1306 PacketID* pident=any_cast<PacketID>(&var);
667f7e60 1307 // cerr<<"handleTCPClientReadable called for fd "<<fd<<", pident->inNeeded: "<<pident->inNeeded<<", "<<pident->sock->getHandle()<<endl;
09e6702a 1308
667f7e60 1309 shared_array<char> buffer(new char[pident->inNeeded]);
09e6702a 1310
705f31ae 1311 int ret=recv(fd, buffer.get(), pident->inNeeded,0);
09e6702a 1312 if(ret > 0) {
667f7e60
BH
1313 pident->inMSG.append(&buffer[0], &buffer[ret]);
1314 pident->inNeeded-=ret;
1315 if(!pident->inNeeded) {
1316 // cerr<<"Got entire load of "<<pident->inMSG.size()<<" bytes"<<endl;
1317 PacketID pid=*pident;
1318 string msg=pident->inMSG;
09e6702a 1319
bb4bdbaf 1320 t_fdm->removeReadFD(fd);
09e6702a
BH
1321 MT->sendEvent(pid, &msg);
1322 }
1323 else {
667f7e60 1324 // cerr<<"Still have "<<pident->inNeeded<<" left to go"<<endl;
09e6702a
BH
1325 }
1326 }
1327 else {
667f7e60 1328 PacketID tmp=*pident;
bb4bdbaf 1329 t_fdm->removeReadFD(fd); // pident might now be invalid (it isn't, but still)
09e6702a
BH
1330 string empty;
1331 MT->sendEvent(tmp, &empty); // this conveys error status
1332 }
1333}
1334
d8f6d49f 1335void handleTCPClientWritable(int fd, FDMultiplexer::funcparam_t& var)
09e6702a 1336{
0b18b22e 1337 PacketID* pid=any_cast<PacketID>(&var);
4ca15bca 1338 int ret=send(fd, pid->outMSG.c_str() + pid->outPos, pid->outMSG.size() - pid->outPos,0);
09e6702a 1339 if(ret > 0) {
667f7e60
BH
1340 pid->outPos+=ret;
1341 if(pid->outPos==pid->outMSG.size()) {
1342 PacketID tmp=*pid;
bb4bdbaf 1343 t_fdm->removeWriteFD(fd);
09e6702a
BH
1344 MT->sendEvent(tmp, &tmp.outMSG); // send back what we sent to convey everything is ok
1345 }
1346 }
1347 else { // error or EOF
667f7e60 1348 PacketID tmp(*pid);
bb4bdbaf 1349 t_fdm->removeWriteFD(fd);
09e6702a 1350 string sent;
998a4334 1351 MT->sendEvent(tmp, &sent); // we convey error status by sending empty string
09e6702a
BH
1352 }
1353}
1354
34801ab1
BH
1355// resend event to everybody chained onto it
1356void doResends(MT_t::waiters_t::iterator& iter, PacketID resend, const string& content)
1357{
1358 if(iter->key.chain.empty())
1359 return;
e27e91a8 1360 // cerr<<"doResends called!\n";
34801ab1
BH
1361 for(PacketID::chain_t::iterator i=iter->key.chain.begin(); i != iter->key.chain.end() ; ++i) {
1362 resend.fd=-1;
1363 resend.id=*i;
e27e91a8 1364 // cerr<<"\tResending "<<content.size()<<" bytes for fd="<<resend.fd<<" and id="<<resend.id<<endl;
4665c31e 1365
34801ab1
BH
1366 MT->sendEvent(resend, &content);
1367 g_stats.chainResends++;
34801ab1
BH
1368 }
1369}
1370
d8f6d49f 1371void handleUDPServerResponse(int fd, FDMultiplexer::funcparam_t& var)
09e6702a 1372{
600fc20b 1373 PacketID pid=any_cast<PacketID>(var);
998a4334 1374 int len;
09e6702a 1375 char data[1500];
996c89cc 1376 ComboAddress fromaddr;
09e6702a
BH
1377 socklen_t addrlen=sizeof(fromaddr);
1378
998a4334 1379 len=recvfrom(fd, data, sizeof(data), 0, (sockaddr *)&fromaddr, &addrlen);
c1da7976 1380
998a4334
BH
1381 if(len < (int)sizeof(dnsheader)) {
1382 if(len < 0)
996c89cc 1383 ; // cerr<<"Error on fd "<<fd<<": "<<stringerror()<<"\n";
09e6702a
BH
1384 else {
1385 g_stats.serverParseError++;
1386 if(g_logCommonErrors)
85db02c5 1387 L<<Logger::Error<<"Unable to parse packet from remote UDP server "<< fromaddr.toString() <<
4957a608 1388 ": packet smalller than DNS header"<<endl;
998a4334 1389 }
34801ab1 1390
49a699c4 1391 t_udpclientsocks->returnSocket(fd);
34801ab1
BH
1392 string empty;
1393
1394 MT_t::waiters_t::iterator iter=MT->d_waiters.find(pid);
1395 if(iter != MT->d_waiters.end())
1396 doResends(iter, pid, empty);
1397
1398 MT->sendEvent(pid, &empty); // this denotes error (does lookup again.. at least L1 will be hot)
998a4334
BH
1399 return;
1400 }
1401
1402 dnsheader dh;
1403 memcpy(&dh, data, sizeof(dh));
1404
998a4334
BH
1405 if(dh.qr) {
1406 PacketID pident;
1407 pident.remote=fromaddr;
1408 pident.id=dh.id;
1409 pident.fd=fd;
c1da7976
BH
1410 if(!dh.qdcount) { // UPC, Nominum, very old BIND on FormErr, NSD
1411 pident.domain.clear();
1412 pident.type = 0;
1413 }
1414 else {
edb1c9ee 1415 try {
4957a608 1416 pident.domain=questionExpand(data, len, pident.type); // don't copy this from above - we need to do the actual read
edb1c9ee
BH
1417 }
1418 catch(std::exception& e) {
284aa5c2 1419 g_stats.serverParseError++; // won't be fed to lwres.cc, so we have to increment
85db02c5 1420 L<<Logger::Warning<<"Error in packet from "<< fromaddr.toStringWithPort() << ": "<<e.what() << endl;
4957a608 1421 return;
edb1c9ee 1422 }
c1da7976 1423 }
998a4334
BH
1424 string packet;
1425 packet.assign(data, len);
34801ab1
BH
1426
1427 MT_t::waiters_t::iterator iter=MT->d_waiters.find(pident);
1428 if(iter != MT->d_waiters.end()) {
1429 doResends(iter, pident, packet);
1430 }
1431
c1da7976
BH
1432 retryWithName:
1433
998a4334 1434 if(!MT->sendEvent(pident, &packet)) {
284aa5c2 1435 // 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
998a4334 1436 for(MT_t::waiters_t::iterator mthread=MT->d_waiters.begin(); mthread!=MT->d_waiters.end(); ++mthread) {
4957a608
BH
1437 if(pident.fd==mthread->key.fd && mthread->key.remote==pident.remote && mthread->key.type == pident.type &&
1438 pdns_iequals(pident.domain, mthread->key.domain)) {
1439 mthread->key.nearMisses++;
1440 }
1441
1442 // be a bit paranoid here since we're weakening our matching
1443 if(pident.domain.empty() && !mthread->key.domain.empty() && !pident.type && mthread->key.type &&
1444 pident.id == mthread->key.id && mthread->key.remote == pident.remote) {
bd0289fc 1445 // cerr<<"Empty response, rest matches though, sending to a waiter"<<endl;
4957a608
BH
1446 pident.domain = mthread->key.domain;
1447 pident.type = mthread->key.type;
bd0289fc 1448 goto retryWithName; // note that this only passes on an error, lwres will still reject the packet
4957a608 1449 }
998a4334 1450 }
284aa5c2
BH
1451 g_stats.unexpectedCount++; // if we made it here, it really is an unexpected answer
1452 if(g_logCommonErrors)
99c69ed3 1453 L<<Logger::Warning<<"Discarding unexpected packet from "<<fromaddr.toStringWithPort()<<": "<<pident.domain<<", "<<pident.type<<endl;
09e6702a 1454 }
d8f6d49f 1455 else if(fd >= 0) {
49a699c4 1456 t_udpclientsocks->returnSocket(fd);
d8f6d49f 1457 }
09e6702a 1458 }
998a4334 1459 else
85db02c5 1460 L<<Logger::Warning<<"Ignoring question on outgoing socket from "<< fromaddr.toStringWithPort() <<endl;
09e6702a
BH
1461}
1462
1f4abb20
BH
1463FDMultiplexer* getMultiplexer()
1464{
1465 FDMultiplexer* ret;
1466 for(FDMultiplexer::FDMultiplexermap_t::const_iterator i = FDMultiplexer::getMultiplexerMap().begin();
1467 i != FDMultiplexer::getMultiplexerMap().end(); ++i) {
1468 try {
1469 ret=i->second();
1f4abb20
BH
1470 return ret;
1471 }
98d0ee4a 1472 catch(FDMultiplexerException &fe) {
0a7f24cb 1473 L<<Logger::Error<<"Non-fatal error initializing possible multiplexer ("<<fe.what()<<"), falling back"<<endl;
98d0ee4a
BH
1474 }
1475 catch(...) {
1476 L<<Logger::Error<<"Non-fatal error initializing possible multiplexer"<<endl;
1477 }
1f4abb20
BH
1478 }
1479 L<<Logger::Error<<"No working multiplexer found!"<<endl;
1480 exit(1);
1481}
1482
5605c067 1483
3427fa8a 1484void* doReloadLuaScript()
4485aa35 1485{
674cf0f6 1486 string fname= ::arg()["lua-dns-script"];
4485aa35 1487 try {
674cf0f6
BH
1488 if(fname.empty()) {
1489 t_pdl->reset();
1490 L<<Logger::Error<<t_id<<" Unloaded current lua script"<<endl;
4485aa35
BH
1491 }
1492 else {
674cf0f6 1493 *t_pdl = shared_ptr<PowerDNSLua>(new PowerDNSLua(fname));
4485aa35
BH
1494 }
1495 }
fdbf35ac 1496 catch(std::exception& e) {
674cf0f6 1497 L<<Logger::Error<<t_id<<" Retaining current script, error from '"<<fname<<"': "<< e.what() <<endl;
4485aa35 1498 }
674cf0f6
BH
1499
1500 L<<Logger::Warning<<t_id<<" (Re)loaded lua script from '"<<fname<<"'"<<endl;
3427fa8a 1501 return 0;
4485aa35
BH
1502}
1503
49a699c4
BH
1504string doQueueReloadLuaScript(vector<string>::const_iterator begin, vector<string>::const_iterator end)
1505{
1506 if(begin != end)
1507 ::arg().set("lua-dns-script") = *begin;
1508
1509 broadcastFunction(doReloadLuaScript);
1510
1511 return "ok, reload/unload queued\n";
1512}
1513
bb4bdbaf 1514void* recursorThread(void*);
51e2144e 1515
3427fa8a 1516void* pleaseSupplantACLs(NetmaskGroup *ng)
49a699c4
BH
1517{
1518 t_allowFrom = ng;
3427fa8a 1519 return 0;
49a699c4
BH
1520}
1521
18af64a8 1522void parseACLs()
f7c1d4e3 1523{
18af64a8 1524 static bool l_initialized;
49a699c4
BH
1525
1526 if(l_initialized) { // only reload configuration file on second call
18af64a8
BH
1527 string configname=::arg()["config-dir"]+"/recursor.conf";
1528 cleanSlashes(configname);
1529
1530 if(!::arg().preParseFile(configname.c_str(), "allow-from-file"))
1531 L<<Logger::Warning<<"Unable to re-parse configuration file '"<<configname<<"'"<<endl;
1532
49a699c4 1533 ::arg().preParseFile(configname.c_str(), "allow-from", LOCAL_NETS);
f27e6356 1534 }
49a699c4
BH
1535
1536 NetmaskGroup* oldAllowFrom = t_allowFrom, *allowFrom=new NetmaskGroup;
1537
2c95fc65
BH
1538 if(!::arg()["allow-from-file"].empty()) {
1539 string line;
2c95fc65
BH
1540 ifstream ifs(::arg()["allow-from-file"].c_str());
1541 if(!ifs) {
49a699c4 1542 delete allowFrom;
9c61b9d0 1543 throw runtime_error("Could not open '"+::arg()["allow-from-file"]+"': "+stringerror());
2c95fc65
BH
1544 }
1545
1546 string::size_type pos;
1547 while(getline(ifs,line)) {
1548 pos=line.find('#');
1549 if(pos!=string::npos)
1550 line.resize(pos);
1551 trim(line);
1552 if(line.empty())
1553 continue;
1554
18af64a8 1555 allowFrom->addMask(line);
2c95fc65 1556 }
49a699c4 1557 L<<Logger::Warning<<"Done parsing " << allowFrom->size() <<" allow-from ranges from file '"<<::arg()["allow-from-file"]<<"' - overriding 'allow-from' setting"<<endl;
2c95fc65
BH
1558 }
1559 else if(!::arg()["allow-from"].empty()) {
f7c1d4e3
BH
1560 vector<string> ips;
1561 stringtok(ips, ::arg()["allow-from"], ", ");
c36bc97a 1562
f7c1d4e3
BH
1563 L<<Logger::Warning<<"Only allowing queries from: ";
1564 for(vector<string>::const_iterator i = ips.begin(); i!= ips.end(); ++i) {
18af64a8 1565 allowFrom->addMask(*i);
f7c1d4e3 1566 if(i!=ips.begin())
674cf0f6 1567 L<<Logger::Warning<<", ";
f7c1d4e3
BH
1568 L<<Logger::Warning<<*i;
1569 }
1570 L<<Logger::Warning<<endl;
1571 }
49a699c4
BH
1572 else {
1573 if(::arg()["local-address"]!="127.0.0.1" && ::arg().asNum("local-port")==53)
1574 L<<Logger::Error<<"WARNING: Allowing queries from all IP addresses - this can be a security risk!"<<endl;
1575 delete allowFrom;
1576 allowFrom = 0;
1577 }
1578
1579 g_initialAllowFrom = allowFrom;
d7dae798 1580 broadcastFunction(boost::bind(pleaseSupplantACLs, allowFrom));
49a699c4
BH
1581 delete oldAllowFrom;
1582
1583 l_initialized = true;
18af64a8
BH
1584}
1585
1586int serviceMain(int argc, char*argv[])
1587{
1588 L.setName("pdns_recursor");
1589
1590 L.setLoglevel((Logger::Urgency)(6)); // info and up
1591
1592 if(!::arg()["logging-facility"].empty()) {
1593 boost::optional<int> val=logFacilityToLOG(::arg().asNum("logging-facility") );
1594 if(val)
1595 theL().setFacility(*val);
1596 else
1597 L<<Logger::Error<<"Unknown logging facility "<<::arg().asNum("logging-facility") <<endl;
1598 }
1599
3427fa8a 1600 L<<Logger::Warning<<"PowerDNS recursor "<<VERSION<<" (C) 2001-2010 PowerDNS.COM BV ("<<__DATE__", "__TIME__;
18af64a8
BH
1601#ifdef __GNUC__
1602 L<<", gcc "__VERSION__;
1603#endif // add other compilers here
1604#ifdef _MSC_VER
1605 L<<", MSVC "<<_MSC_VER;
1606#endif
1607 L<<") starting up"<<endl;
1608
1609 L<<Logger::Warning<<"PowerDNS comes with ABSOLUTELY NO WARRANTY. "
1610 "This is free software, and you are welcome to redistribute it "
1611 "according to the terms of the GPL version 2."<<endl;
f7c1d4e3 1612
18af64a8
BH
1613 L<<Logger::Warning<<"Operating in "<<(sizeof(unsigned long)*8) <<" bits mode"<<endl;
1614
85c32340
BH
1615 #if 0
1616 unsigned int maxFDs, curFDs;
1617 getFDLimits(curFDs, maxFDs);
1618 if(curFDs < 2048)
1619 L<<Logger::Warning<<"Only "<<curFDs<<" file descriptors available (out of: "<<maxFDs<<"), may not be suitable for high performance"<<endl;
1620 #endif
1621
18af64a8 1622 seedRandom(::arg()["entropy-source"]);
2c95fc65 1623
18af64a8
BH
1624 parseACLs();
1625
eb5bae86
BH
1626 if(!::arg()["dont-query"].empty()) {
1627 g_dontQuery=new NetmaskGroup;
1628 vector<string> ips;
1629 stringtok(ips, ::arg()["dont-query"], ", ");
66e0b6ea
BH
1630 ips.push_back("0.0.0.0");
1631 ips.push_back("::");
c36bc97a 1632
eb5bae86
BH
1633 L<<Logger::Warning<<"Will not send queries to: ";
1634 for(vector<string>::const_iterator i = ips.begin(); i!= ips.end(); ++i) {
1635 g_dontQuery->addMask(*i);
1636 if(i!=ips.begin())
4957a608 1637 L<<Logger::Warning<<", ";
eb5bae86
BH
1638 L<<Logger::Warning<<*i;
1639 }
1640 L<<Logger::Warning<<endl;
1641 }
1642
f7c1d4e3 1643 g_quiet=::arg().mustDo("quiet");
1bc3c142
BH
1644 g_weDistributeQueries = ::arg().mustDo("pdns-distributes-queries");
1645 if(g_weDistributeQueries) {
1646 L<<Logger::Warning<<"PowerDNS Recursor itself will distribute queries over threads"<<endl;
1647 }
1648
f7c1d4e3
BH
1649 if(::arg().mustDo("trace")) {
1650 SyncRes::setLog(true);
1651 ::arg().set("quiet")="no";
1652 g_quiet=false;
1653 }
f7c1d4e3 1654
5a38281c
BH
1655 try {
1656 vector<string> addrs;
1657 if(!::arg()["query-local-address6"].empty()) {
1658 SyncRes::s_doIPv6=true;
1659 L<<Logger::Error<<"Enabling IPv6 transport for outgoing queries"<<endl;
1660
1661 stringtok(addrs, ::arg()["query-local-address6"], ", ;");
1662 BOOST_FOREACH(const string& addr, addrs) {
4957a608 1663 g_localQueryAddresses6.push_back(ComboAddress(addr));
5a38281c
BH
1664 }
1665 }
1666 addrs.clear();
1667 stringtok(addrs, ::arg()["query-local-address"], ", ;");
1668 BOOST_FOREACH(const string& addr, addrs) {
1669 g_localQueryAddresses4.push_back(ComboAddress(addr));
1670 }
1671 }
1672 catch(std::exception& e) {
1673 L<<Logger::Error<<"Assigning local query addresses: "<<e.what();
1674 exit(99);
f7c1d4e3 1675 }
bb4bdbaf 1676
840c10ec 1677 SyncRes::s_noEDNSPing = ::arg().mustDo("disable-edns-ping");
4bfae16d 1678 SyncRes::s_noEDNS = ::arg().mustDo("disable-edns");
bb4bdbaf 1679
1051f8a9
BH
1680 SyncRes::s_nopacketcache = ::arg().mustDo("disable-packetcache");
1681
f7c1d4e3 1682 SyncRes::s_maxnegttl=::arg().asNum("max-negative-ttl");
c3e753c7 1683 SyncRes::s_maxcachettl=::arg().asNum("max-cache-ttl");
1051f8a9
BH
1684 SyncRes::s_packetcachettl=::arg().asNum("packetcache-ttl");
1685 SyncRes::s_packetcacheservfailttl=::arg().asNum("packetcache-servfail-ttl");
f7c1d4e3
BH
1686 SyncRes::s_serverID=::arg()["server-id"];
1687 if(SyncRes::s_serverID.empty()) {
1688 char tmp[128];
1689 gethostname(tmp, sizeof(tmp)-1);
1690 SyncRes::s_serverID=tmp;
1691 }
1692
5b0ddd18 1693 g_networkTimeoutMsec = ::arg().asNum("network-timeout");
bb4bdbaf 1694
49a699c4 1695 g_initialDomainMap = parseAuthAndForwards();
674cf0f6 1696
b3b5459d 1697
f7c1d4e3
BH
1698 g_logCommonErrors=::arg().mustDo("log-common-errors");
1699
1700 makeUDPServerSockets();
1701 makeTCPServerSockets();
815099b2 1702
677e2a46
BH
1703 int forks;
1704 for(forks = 0; forks < ::arg().asNum("processes") - 1; ++forks) {
1bc3c142
BH
1705 if(!fork()) // we are child
1706 break;
1707 }
1708
815099b2
BH
1709 s_pidfname=::arg()["socket-dir"]+"/"+s_programname+".pid";
1710 if(!s_pidfname.empty())
1711 unlink(s_pidfname.c_str()); // remove possible old pid file
f7c1d4e3 1712
f7c1d4e3
BH
1713#ifndef WIN32
1714 if(::arg().mustDo("daemon")) {
1715 L<<Logger::Warning<<"Calling daemonize, going to background"<<endl;
1716 L.toConsole(Logger::Critical);
f7c1d4e3
BH
1717 daemonize();
1718 }
1719 signal(SIGUSR1,usr1Handler);
1720 signal(SIGUSR2,usr2Handler);
1721 signal(SIGPIPE,SIG_IGN);
1722 writePid();
1723#endif
677e2a46 1724 makeControlChannelSocket( ::arg().asNum("processes") > 1 ? forks : -1);
138435cb
BH
1725
1726 int newgid=0;
1727 if(!::arg()["setgid"].empty())
1728 newgid=Utility::makeGidNumeric(::arg()["setgid"]);
1729 int newuid=0;
1730 if(!::arg()["setuid"].empty())
1731 newuid=Utility::makeUidNumeric(::arg()["setuid"]);
1732
138435cb
BH
1733 if (!::arg()["chroot"].empty()) {
1734 if (chroot(::arg()["chroot"].c_str())<0 || chdir("/") < 0) {
1735 L<<Logger::Error<<"Unable to chroot to '"+::arg()["chroot"]+"': "<<strerror (errno)<<", exiting"<<endl;
1736 exit(1);
1737 }
1738 }
1739
1740 Utility::dropPrivs(newuid, newgid);
1bc3c142 1741 g_numThreads = ::arg().asNum("threads") + ::arg().mustDo("pdns-distributes-queries");
343257a4 1742
49a699c4 1743 makeThreadPipes();
5d4dd7fe
BH
1744
1745 g_tcpTimeout=::arg().asNum("client-tcp-timeout");
1746 g_maxTCPPerClient=::arg().asNum("max-tcp-per-client");
85c32340 1747 g_maxMThreads=::arg().asNum("max-mthreads");
343257a4 1748
c3828c03 1749 if(g_numThreads == 1) {
76698c6e 1750 L<<Logger::Warning<<"Operating unthreaded"<<endl;
76698c6e
BH
1751 recursorThread(0);
1752 }
1753 else {
1754 pthread_t tid;
c3828c03
BH
1755 L<<Logger::Warning<<"Launching "<< g_numThreads <<" threads"<<endl;
1756 for(unsigned int n=0; n < g_numThreads; ++n) {
76698c6e
BH
1757 pthread_create(&tid, 0, recursorThread, (void*)n);
1758 }
1759 void* res;
49a699c4
BH
1760
1761
76698c6e 1762 pthread_join(tid, &res);
bb4bdbaf 1763 }
bb4bdbaf
BH
1764 return 0;
1765}
1766
1767void* recursorThread(void* ptr)
1768try
1769{
2e2cd8ec 1770 t_id=(int) (long) ptr;
49a699c4 1771 SyncRes tmp(g_now); // make sure it allocates tsstorage before we do anything, like primeHints or so..
ac0e821b 1772 t_sstorage->domainmap = g_initialDomainMap;
49a699c4
BH
1773 t_allowFrom = g_initialAllowFrom;
1774 t_udpclientsocks = new UDPClientSocks();
bd0289fc 1775 t_tcpClientCounts = new tcpClientCounts_t();
49a699c4 1776 primeHints();
674cf0f6 1777
49a699c4
BH
1778 t_packetCache = new RecursorPacketCache();
1779
1780 L<<Logger::Warning<<"Done priming cache with root hints"<<endl;
1781
1782 t_RC->d_followRFC2181=::arg().mustDo("auth-can-lower-ttl");
674cf0f6 1783 t_pdl = new shared_ptr<PowerDNSLua>();
49a699c4 1784
674cf0f6
BH
1785 try {
1786 if(!::arg()["lua-dns-script"].empty()) {
1787 *t_pdl = shared_ptr<PowerDNSLua>(new PowerDNSLua(::arg()["lua-dns-script"]));
1788 L<<Logger::Warning<<"Loaded 'lua' script from '"<<::arg()["lua-dns-script"]<<"'"<<endl;
1789 }
1790
1791 }
1792 catch(std::exception &e) {
1793 L<<Logger::Error<<"Failed to load 'lua' script from '"<<::arg()["lua-dns-script"]<<"': "<<e.what()<<endl;
1794 exit(99);
1795 }
1796
b3b5459d
BH
1797 t_remotes = new RemoteKeeper();
1798 t_remotes->remotes.resize(::arg().asNum("remotes-ringbuffer-entries") / g_numThreads);
1799
1800 if(!t_remotes->remotes.empty())
1801 memset(&t_remotes->remotes[0], 0, t_remotes->remotes.size() * sizeof(RemoteKeeper::remotes_t::value_type));
1802
1803
bb4bdbaf
BH
1804 MT=new MTasker<PacketID,string>(::arg().asNum("stack-size"));
1805
bb4bdbaf
BH
1806 PacketID pident;
1807
1808 t_fdm=getMultiplexer();
83252304
BH
1809 if(!t_id)
1810 L<<Logger::Error<<"Enabled '"<< t_fdm->getName() << "' multiplexer"<<endl;
1811
49a699c4 1812 t_fdm->addReadFD(g_pipes[t_id].readToThread, handlePipeRequest);
83252304 1813
1bc3c142
BH
1814 if(!g_weDistributeQueries || !t_id) // if we distribute queries, only t_id = 0 listens
1815 for(deferredAdd_t::const_iterator i=deferredAdd.begin(); i!=deferredAdd.end(); ++i)
1816 t_fdm->addReadFD(i->first, i->second);
f7c1d4e3 1817
674cf0f6 1818 if(!t_id) {
674cf0f6
BH
1819 t_fdm->addReadFD(s_rcc.d_fd, handleRCC); // control channel
1820 }
1bc3c142 1821
f7c1d4e3 1822 unsigned int maxTcpClients=::arg().asNum("max-tcp-clients");
f7c1d4e3 1823
f7c1d4e3 1824 bool listenOnTCP(true);
49a699c4 1825
3427fa8a 1826 counter=0; // used to periodically execute certain tasks
f7c1d4e3 1827 for(;;) {
ac0e821b 1828 while(MT->schedule(&g_now)); // MTasker letting the mthreads do their thing
f7c1d4e3 1829
3427fa8a
BH
1830 if(!(counter%500)) {
1831 MT->makeThread(houseKeeping, 0);
f7c1d4e3
BH
1832 }
1833
d2392145 1834 if(!(counter%55)) {
d8f6d49f 1835 typedef vector<pair<int, FDMultiplexer::funcparam_t> > expired_t;
bb4bdbaf 1836 expired_t expired=t_fdm->getTimeouts(g_now);
4957a608 1837
f7c1d4e3 1838 for(expired_t::iterator i=expired.begin() ; i != expired.end(); ++i) {
cd989c87 1839 shared_ptr<TCPConnection> conn=any_cast<shared_ptr<TCPConnection> >(i->second);
4957a608 1840 if(g_logCommonErrors)
cd989c87 1841 L<<Logger::Warning<<"Timeout from remote TCP client "<< conn->d_remote.toString() <<endl;
4957a608 1842 t_fdm->removeReadFD(i->first);
f7c1d4e3
BH
1843 }
1844 }
1845
1846 counter++;
1847
3427fa8a 1848 if(!t_id && statsWanted) {
f7c1d4e3
BH
1849 doStats();
1850 }
1851
1852 Utility::gettimeofday(&g_now, 0);
bb4bdbaf 1853 t_fdm->run(&g_now);
3ea54bf0 1854 // 'run' updates g_now for us
f7c1d4e3
BH
1855
1856 if(listenOnTCP) {
50a5ef72 1857 if(TCPConnection::getCurrentConnections() > maxTcpClients) { // shutdown, too many connections
4957a608
BH
1858 for(tcpListenSockets_t::iterator i=g_tcpListenSockets.begin(); i != g_tcpListenSockets.end(); ++i)
1859 t_fdm->removeReadFD(*i);
1860 listenOnTCP=false;
f7c1d4e3
BH
1861 }
1862 }
1863 else {
50a5ef72 1864 if(TCPConnection::getCurrentConnections() <= maxTcpClients) { // reenable
4957a608
BH
1865 for(tcpListenSockets_t::iterator i=g_tcpListenSockets.begin(); i != g_tcpListenSockets.end(); ++i)
1866 t_fdm->addReadFD(*i, handleNewTCPQuestion);
1867 listenOnTCP=true;
f7c1d4e3
BH
1868 }
1869 }
1870 }
1871}
bb4bdbaf
BH
1872catch(AhuException &ae) {
1873 L<<Logger::Error<<"Exception: "<<ae.reason<<endl;
1874 return 0;
1875}
1876catch(std::exception &e) {
1877 L<<Logger::Error<<"STL Exception: "<<e.what()<<endl;
1878 return 0;
1879}
1880catch(...) {
1881 L<<Logger::Error<<"any other exception in main: "<<endl;
1882 return 0;
1883}
1884
f7c1d4e3
BH
1885#ifdef WIN32
1886void doWindowsServiceArguments(RecursorService& recursor)
1887{
1888 if(::arg().mustDo( "register-service" )) {
1889 if ( !recursor.registerService( "The PowerDNS Recursor.", true )) {
1890 cerr << "Could not register service." << endl;
1891 exit( 99 );
1892 }
1893
1894 exit( 0 );
1895 }
1896
1897 if ( ::arg().mustDo( "unregister-service" )) {
1898 recursor.unregisterService();
1899 exit( 0 );
1900 }
1901}
1902#endif
1903
51e2144e 1904
288f4aa9
BH
1905int main(int argc, char **argv)
1906{
5e3de507 1907 g_stats.startupTime=time(0);
8a63d3ce 1908 reportBasicTypes();
ea634573 1909
22030c37 1910 int ret = EXIT_SUCCESS;
caa6eefa 1911#ifdef WIN32
f7c1d4e3
BH
1912 RecursorService service;
1913 WSADATA wsaData;
1914 if(WSAStartup( MAKEWORD( 2, 2 ), &wsaData )) {
1915 cerr<<"Unable to initialize winsock\n";
1916 exit(1);
1917 }
caa6eefa
BH
1918#endif // WIN32
1919
288f4aa9 1920 try {
f888311c 1921 ::arg().set("stack-size","stack size per mthread")="200000";
2e3d8a19
BH
1922 ::arg().set("soa-minimum-ttl","Don't change")="0";
1923 ::arg().set("soa-serial-offset","Don't change")="0";
1924 ::arg().set("no-shuffle","Don't change")="off";
1925 ::arg().set("aaaa-additional-processing","turn on to do AAAA additional processing (slow)")="off";
1926 ::arg().set("local-port","port to listen on")="53";
32252594 1927 ::arg().set("local-address","IP addresses to listen on, separated by spaces or commas. Also accepts ports.")="127.0.0.1";
2e3d8a19
BH
1928 ::arg().set("trace","if we should output heaps of logging")="off";
1929 ::arg().set("daemon","Operate as a daemon")="yes";
0e9d9ce2 1930 ::arg().set("log-common-errors","If we should log rather common errors")="yes";
2e3d8a19
BH
1931 ::arg().set("chroot","switch to chroot jail")="";
1932 ::arg().set("setgid","If set, change group id to this gid for more security")="";
1933 ::arg().set("setuid","If set, change user id to this uid for more security")="";
5b0ddd18 1934 ::arg().set("network-timeout", "Wait this nummer of milliseconds for network i/o")="1500";
bb4bdbaf 1935 ::arg().set("threads", "Launch this number of threads")="2";
1bc3c142 1936 ::arg().set("processes", "Launch this number of processes (EXPERIMENTAL, DO NOT CHANGE)")="1";
c038218b
BH
1937#ifdef WIN32
1938 ::arg().set("quiet","Suppress logging of questions and answers")="off";
f7c1d4e3
BH
1939 ::arg().setSwitch( "register-service", "Register the service" )= "no";
1940 ::arg().setSwitch( "unregister-service", "Unregister the service" )= "no";
1941 ::arg().setSwitch( "ntservice", "Run as service" )= "no";
1942 ::arg().setSwitch( "use-ntlog", "Use the NT logging facilities" )= "yes";
1943 ::arg().setSwitch( "use-logfile", "Use a log file" )= "no";
1944 ::arg().setSwitch( "logfile", "Filename of the log file" )= "recursor.log";
c038218b
BH
1945#else
1946 ::arg().set("quiet","Suppress logging of questions and answers")="";
f27e6356 1947 ::arg().set("logging-facility","Facility to log messages as. 0 corresponds to local0")="";
c038218b 1948#endif
2e3d8a19 1949 ::arg().set("config-dir","Location of configuration directory (recursor.conf)")=SYSCONFDIR;
fdbf35ac
BH
1950#ifndef WIN32
1951 ::arg().set("socket-owner","Owner of socket")="";
1952 ::arg().set("socket-group","Group of socket")="";
1953 ::arg().set("socket-mode", "Permissions for socket")="";
1954#endif
1955
2e3d8a19
BH
1956 ::arg().set("socket-dir","Where the controlsocket will live")=LOCALSTATEDIR;
1957 ::arg().set("delegation-only","Which domains we only accept delegations from")="";
1958 ::arg().set("query-local-address","Source IP address for sending queries")="0.0.0.0";
996c89cc 1959 ::arg().set("query-local-address6","Source IPv6 address for sending queries")="";
2e3d8a19 1960 ::arg().set("client-tcp-timeout","Timeout in seconds when talking to TCP clients")="2";
85c32340 1961 ::arg().set("max-mthreads", "Maximum number of simultaneous Mtasker threads")="2048";
2e3d8a19
BH
1962 ::arg().set("max-tcp-clients","Maximum number of simultaneous TCP clients")="128";
1963 ::arg().set("hint-file", "If set, load root hints from this file")="";
b45eb27c 1964 ::arg().set("max-cache-entries", "If set, maximum number of entries in the main cache")="1000000";
a9af3782 1965 ::arg().set("max-negative-ttl", "maximum number of seconds to keep a negative cached entry in memory")="3600";
c3e753c7 1966 ::arg().set("max-cache-ttl", "maximum number of seconds to keep a cached entry in memory")="86400";
1051f8a9 1967 ::arg().set("packetcache-ttl", "maximum number of seconds to keep a cached entry in packetcache")="3600";
927c12b0 1968 ::arg().set("max-packetcache-entries", "maximum number of entries to keep in the packetcache")="500000";
1051f8a9 1969 ::arg().set("packetcache-servfail-ttl", "maximum number of seconds to keep a cached servfail entry in packetcache")="60";
7f7b8d55 1970 ::arg().set("server-id", "Returned when queried for 'server.id' TXT or NSID, defaults to hostname")="";
a9af3782 1971 ::arg().set("remotes-ringbuffer-entries", "maximum number of packets to store statistics for")="0";
d5141417 1972 ::arg().set("version-string", "string reported on version.pdns or version.bind")="PowerDNS Recursor "VERSION" $Id$";
49a699c4 1973 ::arg().set("allow-from", "If set, only allow these comma separated netmasks to recurse")=LOCAL_NETS;
2c95fc65 1974 ::arg().set("allow-from-file", "If set, load allowed netmasks from this file")="";
51e2144e 1975 ::arg().set("entropy-source", "If set, read entropy from this file")="/dev/urandom";
49a699c4 1976 ::arg().set("dont-query", "If set, do not query these netmasks for DNS data")=LOCAL_NETS;
4e120339 1977 ::arg().set("max-tcp-per-client", "If set, maximum number of TCP sessions per client (IP address)")="0";
0d5f0a9f 1978 ::arg().set("spoof-nearmiss-max", "If non-zero, assume spoofing after this many near misses")="20";
4ef015cd 1979 ::arg().set("single-socket", "If set, only use a single socket for outgoing queries")="off";
5605c067
BH
1980 ::arg().set("auth-zones", "Zones for which we have authoritative data, comma separated domain=file pairs ")="";
1981 ::arg().set("forward-zones", "Zones for which we forward queries, comma separated domain=ip pairs")="";
927c12b0
BH
1982 ::arg().set("forward-zones-recurse", "Zones for which we forward queries with recursion bit, comma separated domain=ip pairs")="";
1983 ::arg().set("forward-zones-file", "File with (+)domain=ip pairs for forwarding")="";
5605c067 1984 ::arg().set("export-etc-hosts", "If we should serve up contents from /etc/hosts")="off";
3ea54bf0 1985 ::arg().set("etc-hosts-file", "Path to 'hosts' file")="/etc/hosts";
9bc8c14c 1986 ::arg().set("serve-rfc1918", "If we should be authoritative for RFC 1918 private IP space")="";
36cbe5c8 1987 ::arg().set("auth-can-lower-ttl", "If we follow RFC 2181 to the letter, an authoritative server can lower the TTL of NS records")="off";
4485aa35 1988 ::arg().set("lua-dns-script", "Filename containing an optional 'lua' script that will be used to modify dns answers")="";
c34d9fe9 1989 ::arg().setSwitch( "ignore-rd-bit", "Assume each packet requires recursion, for compatability" )= "off";
4bfae16d
BH
1990 ::arg().setSwitch( "disable-edns-ping", "Disable EDNSPing" )= "no";
1991 ::arg().setSwitch( "disable-edns", "Disable EDNS" )= "";
1bc3c142
BH
1992 ::arg().setSwitch( "disable-packetcache", "Disable packetcache" )= "no";
1993 ::arg().setSwitch( "pdns-distributes-queries", "If PowerDNS itself should distribute queries over threads (EXPERIMENTAL)")="no";
1994
2e3d8a19
BH
1995
1996 ::arg().setCmd("help","Provide a helpful message");
5e3de507 1997 ::arg().setCmd("version","Print version string ("VERSION")");
d5141417 1998 ::arg().setCmd("config","Output blank configuration");
f27e6356 1999 L.toConsole(Logger::Info);
2e3d8a19 2000 ::arg().laxParse(argc,argv); // do a lax parse
c75a6a9e 2001
577cf284
BH
2002 if(::arg().mustDo("config")) {
2003 cout<<::arg().configstring()<<endl;
2004 exit(0);
2005 }
2006
2007
2e3d8a19 2008 string configname=::arg()["config-dir"]+"/recursor.conf";
c75a6a9e
BH
2009 cleanSlashes(configname);
2010
2e3d8a19 2011 if(!::arg().file(configname.c_str()))
c75a6a9e
BH
2012 L<<Logger::Warning<<"Unable to parse configuration file '"<<configname<<"'"<<endl;
2013
2e3d8a19 2014 ::arg().parse(argc,argv);
c836dc19 2015
2e3d8a19 2016 ::arg().set("delegation-only")=toLower(::arg()["delegation-only"]);
562588a3 2017
2e3d8a19 2018 if(::arg().mustDo("help")) {
b636533b 2019 cerr<<"syntax:"<<endl<<endl;
2e3d8a19 2020 cerr<<::arg().helpstring(::arg()["help"])<<endl;
b636533b
BH
2021 exit(99);
2022 }
5e3de507
BH
2023 if(::arg().mustDo("version")) {
2024 cerr<<"version: "VERSION<<endl;
2025 exit(99);
2026 }
b636533b 2027
caa6eefa 2028#ifndef WIN32
f7c1d4e3
BH
2029 serviceMain(argc, argv);
2030#else
6a0bb0cf 2031 doWindowsServiceArguments(service);
4957a608 2032 L.toNTLog();
f7c1d4e3 2033 RecursorService::instance()->start( argc, argv, ::arg().mustDo( "ntservice" ));
caa6eefa 2034#endif
998a4334 2035
288f4aa9
BH
2036 }
2037 catch(AhuException &ae) {
c836dc19 2038 L<<Logger::Error<<"Exception: "<<ae.reason<<endl;
22030c37 2039 ret=EXIT_FAILURE;
288f4aa9 2040 }
fdbf35ac 2041 catch(std::exception &e) {
c836dc19 2042 L<<Logger::Error<<"STL Exception: "<<e.what()<<endl;
22030c37 2043 ret=EXIT_FAILURE;
288f4aa9
BH
2044 }
2045 catch(...) {
c836dc19 2046 L<<Logger::Error<<"any other exception in main: "<<endl;
22030c37 2047 ret=EXIT_FAILURE;
288f4aa9 2048 }
caa6eefa
BH
2049
2050#ifdef WIN32
2051 WSACleanup();
2052#endif // WIN32
2053
22030c37 2054 return ret;
288f4aa9 2055}