root/trunk/pdns/pdns/pdns_recursor.cc @ 1329

Revision 1329, 63.5 KB (checked in by ahu, 4 years ago)

deal with remote authoritative servers that send back responses w/o a question section, and pass on any errors to appropriate mthreads

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