root/trunk/pdns/pdns/common_startup.cc @ 155

Revision 155, 11.1 KB (checked in by ahu, 10 years ago)

work on zone2sql dot stripping
local-query-address

  • 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) 2002  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 as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18*/
19#include "common_startup.hh"
20
21typedef Distributor<DNSPacket,DNSPacket,PacketHandler> DNSDistributor;
22
23
24ArgvMap theArg;
25StatBag S;  //!< Statistics are gathered accross PDNS via the StatBag class S
26PacketCache PC; //!< This is the main PacketCache, shared accross all threads
27DNSProxy *DP;
28DynListener *dl;
29CommunicatorClass Communicator;
30UDPNameserver *N;
31int avg_latency;
32TCPNameserver *TN;
33
34ArgvMap &arg()
35{
36  return theArg;
37}
38
39
40void declareArguments()
41{
42  arg().set("local-port","The port on which we listen")="53";
43  arg().setSwitch("log-failed-updates","If PDNS should log failed update requests")="";
44  arg().setSwitch("log-dns-details","If PDNS should log DNS non-erroneous details")="";
45  arg().set("urlredirector","Where we send hosts to that need to be url redirected")="127.0.0.1";
46  arg().set("smtpredirector","Our smtpredir MX host")="a.misconfigured.powerdns.smtp.server";
47  arg().set("local-address","Local IP address to which we bind")="0.0.0.0";
48  arg().set("local-ipv6","Local IP address to which we bind")="";
49  arg().set("query-local-address","Source IP address for sending queries")="";
50  arg().set("max-queue-length","Maximum queuelength before considering situation lost")="5000";
51  arg().set("soa-serial-offset","Make sure that no SOA serial is less than this number")="0";
52  arg().set("only-soa","Make sure that no SOA serial is less than this number")="org";
53  arg().setCmd("help","Provide a helpful message");
54  arg().setCmd("config","Provide a helpful message");
55  arg().setCmd("list-modules","Lists all modules available");
56  arg().setCmd("no-config","Don't parse configuration file");
57 
58  arg().set("control-console","Debugging switch - don't use")="no"; // but I know you will!
59  arg().set("fancy-records","Process URL and MBOXFW records")="no";
60  arg().set("wildcard-url","Process URL and MBOXFW records")="no";
61  arg().set("wildcards","Honor wildcards in the database")="";
62  arg().set("loglevel","Amount of logging. Higher is more. Do not set below 3")="4";
63  arg().set("default-soa-name","name to insert in the SOA record if none set in the backend")="a.misconfigured.powerdns.server";
64  arg().set("distributor-threads","Default number of Distributor (backend) threads to start")="3";
65  arg().set("queue-limit","Maximum number of milliseconds to queue a query")="1500"; 
66  arg().set("recursor","If recursion is desired, IP address of a recursing nameserver")="no"; 
67  arg().set("lazy-recursion","Only recurse if question cannot be answered locally")="yes";
68  arg().set("allow-recursion","List of netmasks that are allowed to recurse")=""; 
69 
70  arg().set("disable-tcp","Do not listen to TCP queries")="no";
71  arg().set("disable-axfr","Do not allow zone transfers")="no";
72 
73  arg().set("config-name","Name of this virtual configuration - will rename the binary image")="";
74
75  arg().set("load-modules","Load this module - supply absolute or relative path")="";
76  arg().set("launch","Which backends to launch and order to query them in")="";
77  arg().setSwitch("disable-axfr","Disable zonetransfers but do allow TCP queries")="no";
78  arg().set("allow-axfr-ips","If disabled, DO allow zonetransfers from these IP addresses")="";
79  arg().set("slave-cycle-interval","Reschedule failed SOA serial checks once every .. seconds")="60";
80 
81  arg().setSwitch("slave","Act as a slave")="no";
82  arg().setSwitch("master","Act as a master")="no";
83  arg().setSwitch("guardian","Run within a guardian process")="no";
84  arg().setSwitch("skip-cname","Do not perform CNAME indirection for each query")="no";
85  arg().setSwitch("strict-rfc-axfrs","Perform strictly rfc compliant axfrs (very slow)")="no";
86 
87  arg().setSwitch("webserver","Start a webserver for monitoring")="no"; 
88  arg().setSwitch("webserver-print-arguments","If the webserver should print arguments")="no"; 
89  arg().set("webserver-address","IP Address of webserver to listen on")="127.0.0.1";
90  arg().set("webserver-port","Port of webserver to listen on")="8081";
91  arg().set("webserver-password","Password required for accessing the webserver")="";
92
93  arg().set("receiver-threads","Number of receiver threads to launch")="1";
94 
95  arg().setSwitch("out-of-zone-additional-processing","Do out of zone additional processing")="no";
96  arg().setSwitch("query-logging","Hint backends that queries should be logged")="no";
97 
98  arg().set("cache-ttl","Seconds to store packets in the PacketCache")="20";
99  arg().set("recursive-cache-ttl","Seconds to store packets in the PacketCache")="10";
100  arg().set("negquery-cache-ttl","Seconds to store packets in the PacketCache")="60";
101  arg().set("query-cache-ttl","Seconds to store packets in the PacketCache")="20";
102  arg().set("soa-minimum-ttl","Default SOA mininum ttl")="3600";
103  arg().set("max-tcp-connections","Maximum number of TCP connections")="10";
104
105  arg().setSwitch( "use-logfile", "Use a log file" )= "no";
106  arg().set( "logfile", "Logfile to use" )= "pdns.log";
107  arg().set("setuid","If set, change user id to this uid for more security")="";
108  arg().set("setgid","If set, change group id to this gid for more security")="";
109}
110
111
112
113void declareStats(void)
114{
115  S.declare("udp-queries","Number of UDP queries received");
116  S.declare("udp-answers","Number of answers sent out over UDP");
117  S.declare("recursing-answers","Number of recursive answers sent out");
118  S.declare("recursing-questions","Number of questions sent to recursor");
119  S.declare("corrupt-packets","Number of corrupt packets received");
120
121  S.declare("tcp-queries","Number of TCP queries received");
122  S.declare("tcp-answers","Number of answers sent out over TCP");
123
124  S.declare("qsize-q","Number of questions waiting for database attention");
125
126  S.declare("deferred-cache-inserts","Amount of cache inserts that were deferred because of maintenance");
127  S.declare("deferred-cache-lookup","Amount of cache lookups that were deferred because of maintenance");
128
129  S.declare("query-cache-hit","Number of hits on the query cache");
130  S.declare("query-cache-miss","Number of misses on the query cache");
131
132
133  S.declare("servfail-packets","Number of times a server-failed packet was sent out");
134  S.declare("latency","Average number of microseconds needed to answer a question");
135  S.declare("timedout-packets","Number of packets which weren't answered within timeout set");
136
137  S.declareRing("queries","UDP Queries Received");
138  S.declareRing("nxdomain-queries","Queries for non-existent records within existent domains");
139  S.declareRing("noerror-queries","Queries for existing records, but for type we don't have");
140  S.declareRing("servfail-queries","Queries that could not be answered due to backend errors");
141  S.declareRing("unauth-queries","Queries for domains that we are not authoritative for");
142  S.declareRing("logmessages","Log Messages");
143  S.declareRing("remotes","Remote server IP addresses");
144  S.declareRing("remotes-unauth","Remote hosts querying domains for which we are not auth");
145  S.declareRing("remotes-corrupt","Remote hosts sending corrupt packets");
146
147}
148
149
150int isGuarded(char **argv)
151{
152  char *p=strstr(argv[0],"-instance");
153
154  return !!p;
155}
156
157
158void sendout(const DNSDistributor::AnswerData &AD)
159{
160  static int &numanswered=*S.getPointer("udp-answers");
161  if(!AD.A)
162    return;
163 
164  N->send(AD.A);
165  numanswered++;
166  int diff=AD.A->d_dt.udiff();
167  avg_latency=(int)(0.999*avg_latency+0.001*diff);
168
169  delete AD.A; 
170
171
172}
173
174
175//! The qthread receives questions over the internet via the Nameserver class, and hands them to the Distributor for futher processing
176void *qthread(void *p)
177{
178  DNSDistributor *D=static_cast<DNSDistributor *>(p);
179
180  DNSPacket *P;
181
182  DNSPacket question;
183  DNSPacket cached;
184
185  int &numreceived=*S.getPointer("udp-queries");
186  int &numanswered=*S.getPointer("udp-answers");
187  numreceived=-1;
188  int diff;
189
190  for(;;) {
191    if(!((numreceived++)%50)) { // maintenance tasks
192      S.set("latency",(int)avg_latency);
193      int qcount, acount;
194      D->getQueueSizes(qcount, acount);
195      S.set("qsize-q",qcount);
196    }
197   
198    if(!(P=N->receive(&question))) { // receive a packet         inline
199      continue;                    // packet was broken, try again
200    }
201
202
203    S.ringAccount("queries", P->qdomain+"/"+P->qtype.getName());
204    S.ringAccount("remotes",P->getRemote());
205
206    if(PC.get(P,&cached)) { // short circuit - does the PacketCache recognize this question?
207      cached.setRemote((struct sockaddr *)(P->remote),P->d_socklen);  // inlined
208      cached.setSocket(P->getSocket());                               // inlined
209      cached.spoofID(P->d.id);                                        // inlined
210      cached.d.rd=P->d.rd; // copy in recursion desired bit
211      cached.commitD(); // commit d to the packet                        inlined
212
213      N->send(&cached);   // answer it then                              inlined
214      diff=P->d_dt.udiff();                                                   
215      avg_latency=(int)(0.999*avg_latency+0.001*diff); // 'EWMA'
216     
217      numanswered++;
218      continue;
219    }
220
221    D->question(P, &sendout); // otherwise, give to the distributor
222  }
223  return 0;
224}
225
226
227void mainthread()
228{
229  Utility::srandom(time(0));
230
231   int newgid=0;     
232   if(!arg()["setgid"].empty()) 
233     newgid=Utility::makeGidNumeric(arg()["setgid"]);     
234   int newuid=0;     
235   if(!arg()["setuid"].empty())       
236     newuid=Utility::makeUidNumeric(arg()["setuid"]); 
237#ifndef WIN32
238   if(!arg()["chroot"].empty()) { 
239     if(chroot(arg()["chroot"].c_str())<0) {
240       L<<Logger::Error<<"Unable to chroot to '"+arg()["chroot"]+"': "<<strerror(errno)<<", exiting"<<endl; 
241       exit(1);
242     }   
243     else
244       L<<Logger::Error<<"Chrooted to '"<<arg()["chroot"]<<"'"<<endl;     
245   } 
246#endif
247
248   Utility::dropPrivs(newuid, newgid);
249
250  if(arg().mustDo("recursor")){
251    DP=new DNSProxy(arg()["recursor"]);
252    DP->onlyFrom(arg()["allow-recursion"]);
253    DP->go();
254  }
255  // NOW SAFE TO CREATE THREADS!
256  dl->go();
257
258  pthread_t qtid;
259  StatWebServer sws;
260
261  if(arg()["webserver"]!="no") 
262    sws.go();
263 
264  if(arg().mustDo("slave") || arg().mustDo("master"))
265    Communicator.go(); 
266
267  if(TN)
268    TN->go(); // tcp nameserver launch
269   
270  //  fork(); (this worked :-))
271  for(int n=0;n<arg().asNum("receiver-threads");++n) {
272    DNSDistributor *D= new DNSDistributor(arg().asNum("distributor-threads")); // the big dispatcher!
273    pthread_create(&qtid,0,qthread,static_cast<void *>(D)); // receives packets
274  }
275
276  void *p;
277  pthread_join(qtid, &p);
278 
279  L<<Logger::Error<<"Mainthread exiting - should never happen"<<endl;
280}
281
282
283
284
Note: See TracBrowser for help on using the browser.