]>
git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/nameserver.cc
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * In addition, for the avoidance of any doubt, permission is granted to
10 * link this program with OpenSSL and to (re)distribute the binaries
11 * produced as the result of such linking.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
32 #include <sys/types.h>
33 #include "responsestats.hh"
36 #include "dnsbackend.hh"
37 #include "dnspacket.hh"
38 #include "nameserver.hh"
39 #include "distributor.hh"
41 #include "arguments.hh"
44 #include "namespaces.hh"
49 PowerDNS is a very versatile nameserver that can answer questions from different backends. To implement your
50 own backend, see the documentation for the DNSBackend class.
52 \section copyright Copyright and License
53 PowerDNS is (C) 2001-2008 PowerDNS.COM BV. It is distributed according to the terms of the General Public License version 2.
55 \section overview High level overview
57 The Distributor contains a configurable number of PacketHandler instances, each in its own thread, for connection pooling.
58 PacketHandler instances are recycled of they let escape an PDNSException.
60 The PacketHandler implements the RFC1034 algorithm and converts question packets into DNSBackend queries.
62 A DNSBackend is an entity that returns DNSResourceRecord objects in return to explicit questions for domains with a specified QType
64 PowerDNS uses the UeberBackend, which hosts DNSBackends. By default it has no DNSBackends within itself, those are loaded
65 by setting --load=<list of backends>. This way DNSBackend implementations can be kept completely separate, but most aren't.
67 If one or more DNSBackends are loaded, the UeberBackend fields the queries to all of them until one answers.
69 \section TCP TCP Operations
71 The TCP operation runs within a single thread called tcpreceiver(), that also queries the PacketHandler.
73 \section Cache Caching
75 On its own, this setup is not suitable for high performance operations. A single DNS query can turn into many DNSBackend questions,
76 each taking many milliseconds to complete. This is why the qthread() first checks the PacketCache to see if an answer is known to a packet
77 asking this question. If so, the entire Distributor is shunted, and the answer is sent back *directly*, within a few microseconds.
79 \section misc Miscellaneous
80 Configuration details are available via the ArgvMap instance arg. Statistics are created by making calls to the StatBag object called S.
81 These statistics are made available via the UeberBackend on the same socket that is used for dynamic module commands.
84 The main() of PowerDNS can be found in receiver.cc - start reading there for further insights into the operation of the nameserver
87 vector
< ComboAddress
> g_localaddresses
; // not static, our unit tests need to poke this
89 void UDPNameserver :: bindAddresses ()
92 stringtok ( locals
,:: arg ()[ "local-ipv6" ], " ," );
93 if (! locals
. empty ()) {
94 g_log
<< Logger :: Error
<< "NOTE: Deprecated local-ipv6 setting used. Please move those addresses to the local-address setting." << endl
;
96 stringtok ( locals
,:: arg ()[ "local-address" ], " ," );
101 throw PDNSException ( "No local address specified" );
104 // for(vector<string>::const_iterator i=locals.begin();i!=locals.end();++i) {
105 for ( const auto & local
: locals
) {
106 ComboAddress
locala ( local
, :: arg (). asNum ( "local-port" ));
108 s
= socket ( locala
. sin4
. sin_family
, SOCK_DGRAM
, 0 );
111 if ( errno
== EAFNOSUPPORT
) {
112 g_log
<< Logger :: Error
<< "Binding " << locala
. toStringWithPort ()<< ": Address Family is not supported - skipping bind" << endl
;
115 throw PDNSException ( "Unable to acquire a UDP socket: " + stringerror ());
119 if (! setNonBlocking ( s
))
120 throw PDNSException ( "Unable to set UDP socket " + locala
. toStringWithPort () + " to non-blocking: " + stringerror ());
122 if ( IsAnyAddress ( locala
)) {
123 setsockopt ( s
, IPPROTO_IP
, GEN_IP_PKTINFO
, & one
, sizeof ( one
));
124 if ( locala
. isIPv6 ()) {
125 setsockopt ( s
, IPPROTO_IPV6
, IPV6_V6ONLY
, & one
, sizeof ( one
)); // if this fails, we report an error in tcpreceiver too
126 #ifdef IPV6_RECVPKTINFO
127 setsockopt ( s
, IPPROTO_IPV6
, IPV6_RECVPKTINFO
, & one
, sizeof ( one
));
132 if (! setSocketTimestamps ( s
))
133 g_log
<< Logger :: Warning
<< "Unable to enable timestamp reporting for socket " << locala
. toStringWithPort ()<< endl
;
135 if ( locala
. isIPv4 ()) {
137 setSocketIgnorePMTU ( s
);
139 catch ( const std :: exception
& e
) {
140 g_log
<< Logger :: Warning
<< "Failed to set IP_MTU_DISCOVER on UDP server socket: " << e
. what ()<< endl
;
145 if ( d_can_reuseport
)
146 if ( setsockopt ( s
, SOL_SOCKET
, SO_REUSEPORT
, & one
, sizeof ( one
)) )
147 d_can_reuseport
= false ;
150 if ( :: arg (). mustDo ( "non-local-bind" ) )
151 Utility :: setBindAny ( locala
. sin4
. sin_family
, s
);
153 if ( ! d_additional_socket
)
154 g_localaddresses
. push_back ( locala
);
156 if (:: bind ( s
, ( sockaddr
*)& locala
, locala
. getSocklen ()) < 0 ) {
157 string binderror
= stringerror ();
159 if ( errno
== EADDRNOTAVAIL
&& ! :: arg (). mustDo ( "local-address-nonexist-fail" ) ) {
160 g_log
<< Logger :: Error
<< "Address " << locala
<< " does not exist on this server - skipping UDP bind" << endl
;
163 g_log
<< Logger :: Error
<< "Unable to bind UDP socket to '" + locala
. toStringWithPort ()+ "': " << binderror
<< endl
;
164 throw PDNSException ( "Unable to bind to UDP socket" );
167 d_sockets
. push_back ( s
);
172 d_rfds
. push_back ( pfd
);
173 g_log
<< Logger :: Error
<< "UDP server bound to " << locala
. toStringWithPort ()<< endl
;
177 bool AddressIsUs ( const ComboAddress
& remote
)
179 for ( const ComboAddress
& us
: g_localaddresses
) {
182 if ( IsAnyAddress ( us
)) {
183 int s
= socket ( remote
. sin4
. sin_family
, SOCK_DGRAM
, 0 );
187 if ( connect ( s
, ( struct sockaddr
*)& remote
, remote
. getSocklen ()) < 0 ) {
192 ComboAddress actualLocal
;
193 actualLocal
. sin4
. sin_family
= remote
. sin4
. sin_family
;
194 socklen_t socklen
= actualLocal
. getSocklen ();
196 if ( getsockname ( s
, ( struct sockaddr
*) & actualLocal
, & socklen
) < 0 ) {
201 actualLocal
. sin4
. sin_port
= us
. sin4
. sin_port
;
202 if ( actualLocal
== remote
)
209 UDPNameserver :: UDPNameserver ( bool additional_socket
)
212 d_can_reuseport
= :: arg (). mustDo ( "reuseport" );
214 // Are we the main socket (false) or a rebinding using SO_REUSEPORT ?
215 d_additional_socket
= additional_socket
;
217 if (:: arg ()[ "local-address" ]. empty ())
218 g_log
<< Logger :: Critical
<< "PDNS is deaf and mute! Not listening on any interfaces" << endl
;
223 void UDPNameserver :: send ( DNSPacket
& p
)
225 const string
& buffer
= p
. getString ();
226 g_rs
. submitResponse ( p
, true );
230 cmsgbuf_aligned cbuf
;
232 fillMSGHdr (& msgh
, & iov
, & cbuf
, 0 , ( char *) buffer
. c_str (), buffer
. length (), & p
. d_remote
);
234 msgh
. msg_control
= NULL
;
236 addCMsgSrcAddr (& msgh
, & cbuf
, p
. d_anyLocal
. get_ptr (), 0 );
238 DLOG ( g_log
<< Logger :: Notice
<< "Sending a packet to " << p
. getRemote () << " (" << buffer
. length ()<< " octets)" << endl
);
239 if ( buffer
. length () > p
. getMaxReplyLen ()) {
240 g_log
<< Logger :: Error
<< "Weird, trying to send a message that needs truncation, " << buffer
. length ()<< " > " << p
. getMaxReplyLen ()<< ". Question was for " << p
. qdomain
<< "|" << p
. qtype
. getName ()<< endl
;
242 if ( sendmsg ( p
. getSocket (), & msgh
, 0 ) < 0 )
243 g_log
<< Logger :: Error
<< "Error sending reply with sendmsg (socket=" << p
. getSocket ()<< ", dest=" << p
. d_remote
. toStringWithPort ()<< "): " << stringerror ()<< endl
;
246 bool UDPNameserver :: receive ( DNSPacket
& packet
, std :: string
& buffer
)
251 Utility :: sock_t sock
=- 1 ;
255 cmsgbuf_aligned cbuf
;
257 remote
. sin6
. sin6_family
= AF_INET6
; // make sure it is big enough
258 fillMSGHdr (& msgh
, & iov
, & cbuf
, sizeof ( cbuf
), & buffer
. at ( 0 ), buffer
. size (), & remote
);
261 vector
< struct pollfd
> rfds
= d_rfds
;
263 for ( auto & pfd
: rfds
) {
270 err
= poll (& rfds
[ 0 ], rfds
. size (), - 1 );
274 unixDie ( "Unable to poll for new UDP events" );
277 for ( auto & pfd
: rfds
) {
278 if ( pfd
. revents
& POLLIN
) {
280 if (( len
= recvmsg ( sock
, & msgh
, 0 )) < 0 ) {
282 g_log
<< Logger :: Error
<< "recvfrom gave error, ignoring: " << stringerror ()<< endl
;
289 throw PDNSException ( "poll betrayed us! (should not happen)" );
291 DLOG ( g_log
<< "Received a packet " << len
<< " bytes long from " << remote
. toString ()<< endl
);
293 BOOST_STATIC_ASSERT ( offsetof ( sockaddr_in
, sin_port
) == offsetof ( sockaddr_in6
, sin6_port
));
295 if ( remote
. sin4
. sin_port
== 0 ) // would generate error on responding. sin4 also works for ipv6
298 packet
. setSocket ( sock
);
299 packet
. setRemote (& remote
);
302 if ( HarvestDestinationAddress (& msgh
, & dest
)) {
303 // cerr<<"Setting d_anyLocal to '"<<dest.toString()<<"'"<<endl;
304 packet
. d_anyLocal
= dest
;
307 struct timeval recvtv
;
308 if ( HarvestTimestamp (& msgh
, & recvtv
)) {
309 packet
. d_dt
. setTimeval ( recvtv
);
312 packet
. d_dt
. set (); // timing
314 if ( packet
. parse (& buffer
. at ( 0 ), ( size_t ) len
)< 0 ) {
315 S
. inc ( "corrupt-packets" );
316 S
. ringAccount ( "remotes-corrupt" , packet
. d_remote
);
318 return false ; // unable to parse