2 PowerDNS Versatile Database Driven Nameserver
3 Copyright (C) 2002 PowerDNS.COM BV
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.
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.
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
19 #include "common_startup.hh"
21 typedef Distributor
<DNSPacket
,DNSPacket
,PacketHandler
> DNSDistributor
;
25 StatBag S
; //!< Statistics are gathered accross PDNS via the StatBag class S
26 PacketCache PC
; //!< This is the main PacketCache, shared accross all threads
29 CommunicatorClass Communicator
;
40 void declareArguments()
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("max-queue-length","Maximum queuelength before considering situation lost")="5000";
50 arg().set("soa-serial-offset","Make sure that no SOA serial is less than this number")="0";
51 arg().set("only-soa","Make sure that no SOA serial is less than this number")="org";
52 arg().setCmd("help","Provide a helpful message");
53 arg().setCmd("config","Provide a helpful message");
54 arg().setCmd("list-modules","Lists all modules available");
55 arg().setCmd("no-config","Don't parse configuration file");
57 arg().set("control-console","Debugging switch - don't use")="no"; // but I know you will!
58 arg().set("fancy-records","Process URL and MBOXFW records")="no";
59 arg().set("wildcard-url","Process URL and MBOXFW records")="no";
60 arg().set("wildcards","Honor wildcards in the database")="";
61 arg().set("loglevel","Amount of logging. Higher is more. Do not set below 3")="4";
62 arg().set("default-soa-name","name to insert in the SOA record if none set in the backend")="a.misconfigured.powerdns.server";
63 arg().set("distributor-threads","Default number of Distributor (backend) threads to start")="3";
64 arg().set("queue-limit","Maximum number of milliseconds to queue a query")="1500";
65 arg().set("recursor","If recursion is desired, IP address of a recursing nameserver")="no";
66 arg().set("lazy-recursion","Only recurse if question cannot be answered locally")="yes";
67 arg().set("allow-recursion","List of netmasks that are allowed to recurse")="";
69 arg().set("disable-tcp","Do not listen to TCP queries")="no";
70 arg().set("disable-axfr","Do not allow zone transfers")="no";
72 arg().set("config-name","Name of this virtual configuration - will rename the binary image")="";
74 arg().set("load-modules","Load this module - supply absolute or relative path")="";
75 arg().set("launch","Which backends to launch and order to query them in")="";
76 arg().setSwitch("disable-axfr","Disable zonetransfers but do allow TCP queries")="no";
77 arg().set("allow-axfr-ips","If disabled, DO allow zonetransfers from these IP addresses")="";
78 arg().set("slave-cycle-interval","Reschedule failed SOA serial checks once every .. seconds")="60";
80 arg().setSwitch("slave","Act as a slave")="no";
81 arg().setSwitch("master","Act as a master")="no";
82 arg().setSwitch("guardian","Run within a guardian process")="no";
83 arg().setSwitch("skip-cname","Do not perform CNAME indirection for each query")="no";
84 arg().setSwitch("strict-rfc-axfrs","Perform strictly rfc compliant axfrs (very slow)")="no";
86 arg().setSwitch("webserver","Start a webserver for monitoring")="no";
87 arg().setSwitch("webserver-print-arguments","If the webserver should print arguments")="no";
88 arg().set("webserver-address","IP Address of webserver to listen on")="127.0.0.1";
89 arg().set("webserver-port","Port of webserver to listen on")="8081";
90 arg().set("webserver-password","Password required for accessing the webserver")="";
92 arg().set("receiver-threads","Number of receiver threads to launch")="1";
94 arg().setSwitch("out-of-zone-additional-processing","Do out of zone additional processing")="no";
95 arg().setSwitch("query-logging","Hint backends that queries should be logged")="no";
97 arg().set("cache-ttl","Seconds to store packets in the PacketCache")="20";
98 arg().set("recursive-cache-ttl","Seconds to store packets in the PacketCache")="10";
99 arg().set("negquery-cache-ttl","Seconds to store packets in the PacketCache")="60";
100 arg().set("query-cache-ttl","Seconds to store packets in the PacketCache")="20";
101 arg().set("soa-minimum-ttl","Default SOA mininum ttl")="3600";
102 arg().set("max-tcp-connections","Maximum number of TCP connections")="10";
104 arg().setSwitch( "use-logfile", "Use a log file" )= "no";
105 arg().set( "logfile", "Logfile to use" )= "pdns.log";
106 arg().set("setuid","If set, change user id to this uid for more security")="";
107 arg().set("setgid","If set, change group id to this gid for more security")="";
112 void declareStats(void)
114 S
.declare("udp-queries","Number of UDP queries received");
115 S
.declare("udp-answers","Number of answers sent out over UDP");
116 S
.declare("recursing-answers","Number of recursive answers sent out");
117 S
.declare("recursing-questions","Number of questions sent to recursor");
118 S
.declare("corrupt-packets","Number of corrupt packets received");
120 S
.declare("tcp-queries","Number of TCP queries received");
121 S
.declare("tcp-answers","Number of answers sent out over TCP");
123 S
.declare("qsize-q","Number of questions waiting for database attention");
125 S
.declare("deferred-cache-inserts","Amount of cache inserts that were deferred because of maintenance");
126 S
.declare("deferred-cache-lookup","Amount of cache lookups that were deferred because of maintenance");
128 S
.declare("query-cache-hit","Number of hits on the query cache");
129 S
.declare("query-cache-miss","Number of misses on the query cache");
132 S
.declare("servfail-packets","Number of times a server-failed packet was sent out");
133 S
.declare("latency","Average number of microseconds needed to answer a question");
134 S
.declare("timedout-packets","Number of packets which weren't answered within timeout set");
136 S
.declareRing("queries","UDP Queries Received");
137 S
.declareRing("nxdomain-queries","Queries for non-existent records within existent domains");
138 S
.declareRing("noerror-queries","Queries for existing records, but for type we don't have");
139 S
.declareRing("servfail-queries","Queries that could not be answered due to backend errors");
140 S
.declareRing("unauth-queries","Queries for domains that we are not authoritative for");
141 S
.declareRing("logmessages","Log Messages");
142 S
.declareRing("remotes","Remote server IP addresses");
143 S
.declareRing("remotes-unauth","Remote hosts querying domains for which we are not auth");
144 S
.declareRing("remotes-corrupt","Remote hosts sending corrupt packets");
149 int isGuarded(char **argv
)
151 char *p
=strstr(argv
[0],"-instance");
157 void sendout(const DNSDistributor::AnswerData
&AD
)
159 static int &numanswered
=*S
.getPointer("udp-answers");
165 int diff
=AD
.A
->d_dt
.udiff();
166 avg_latency
=(int)(0.999*avg_latency
+0.001*diff
);
174 //! The qthread receives questions over the internet via the Nameserver class, and hands them to the Distributor for futher processing
175 void *qthread(void *p
)
177 DNSDistributor
*D
=static_cast<DNSDistributor
*>(p
);
184 int &numreceived
=*S
.getPointer("udp-queries");
185 int &numanswered
=*S
.getPointer("udp-answers");
190 if(!((numreceived
++)%50)) { // maintenance tasks
191 S
.set("latency",(int)avg_latency
);
193 D
->getQueueSizes(qcount
, acount
);
194 S
.set("qsize-q",qcount
);
197 if(!(P
=N
->receive(&question
))) { // receive a packet inline
198 continue; // packet was broken, try again
202 S
.ringAccount("queries", P
->qdomain
+"/"+P
->qtype
.getName());
203 S
.ringAccount("remotes",P
->getRemote());
205 if(PC
.get(P
,&cached
)) { // short circuit - does the PacketCache recognize this question?
206 cached
.setRemote((struct sockaddr
*)(P
->remote
),P
->d_socklen
); // inlined
207 cached
.setSocket(P
->getSocket()); // inlined
208 cached
.spoofID(P
->d
.id
); // inlined
209 cached
.d
.rd
=P
->d
.rd
; // copy in recursion desired bit
210 cached
.commitD(); // commit d to the packet inlined
212 N
->send(&cached
); // answer it then inlined
213 diff
=P
->d_dt
.udiff();
214 avg_latency
=(int)(0.999*avg_latency
+0.001*diff
); // 'EWMA'
220 D
->question(P
, &sendout
); // otherwise, give to the distributor
228 Utility::srandom(time(0));
231 if(!arg()["setgid"].empty())
232 newgid
=Utility::makeGidNumeric(arg()["setgid"]);
234 if(!arg()["setuid"].empty())
235 newuid
=Utility::makeUidNumeric(arg()["setuid"]);
237 if(!arg()["chroot"].empty()) {
238 if(chroot(arg()["chroot"].c_str())<0) {
239 L
<<Logger::Error
<<"Unable to chroot: "<<strerror(errno
)<<", exiting"<<endl
;
243 L
<<Logger::Error
<<"Chrooted to '"<<arg()["chroot"]<<"'"<<endl
;
247 Utility::dropPrivs(newuid
, newgid
);
249 if(arg().mustDo("recursor")){
250 DP
=new DNSProxy(arg()["recursor"]);
251 DP
->onlyFrom(arg()["allow-recursion"]);
254 // NOW SAFE TO CREATE THREADS!
263 if(arg()["webserver"]!="no")
266 if(arg().mustDo("slave") || arg().mustDo("master"))
270 TN
->go(); // tcp nameserver launch
272 // fork(); (this worked :-))
273 for(int n
=0;n
<arg().asNum("receiver-threads");++n
) {
274 DNSDistributor
*D
= new DNSDistributor(arg().asNum("distributor-threads")); // the big dispatcher!
275 pthread_create(&qtid
,0,qthread
,static_cast<void *>(D
)); // receives packets
279 pthread_join(qtid
, &p
);
281 L
<<Logger::Error
<<"Mainthread exiting - should never happen"<<endl
;