]>
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 :: bindIPv4 ()
92 stringtok ( locals
,:: arg ()[ "local-address" ], " ," );
96 throw PDNSException ( "No local address specified" );
99 for ( vector
< string
>:: const_iterator i
= locals
. begin (); i
!= locals
. end ();++ i
) {
100 string
localname (* i
);
103 s
= socket ( AF_INET
, SOCK_DGRAM
, 0 );
106 g_log
<< Logger :: Error
<< "Unable to acquire UDP socket: " + string ( strerror ( errno
)) << endl
;
107 throw PDNSException ( "Unable to acquire a UDP socket: " + string ( strerror ( errno
)));
112 if (! setNonBlocking ( s
))
113 throw PDNSException ( "Unable to set UDP socket to non-blocking: " + stringerror ());
115 locala
= ComboAddress ( localname
, :: arg (). asNum ( "local-port" ));
117 if ( locala
. sin4
. sin_family
!= AF_INET
)
118 throw PDNSException ( "Attempting to bind IPv4 socket to IPv6 address" );
120 if ( IsAnyAddress ( locala
))
121 setsockopt ( s
, IPPROTO_IP
, GEN_IP_PKTINFO
, & one
, sizeof ( one
));
123 if (! setSocketTimestamps ( s
))
124 g_log
<< Logger :: Warning
<< "Unable to enable timestamp reporting for socket" << endl
;
127 if ( d_can_reuseport
)
128 if ( setsockopt ( s
, SOL_SOCKET
, SO_REUSEPORT
, & one
, sizeof ( one
)) )
129 d_can_reuseport
= false ;
132 if ( :: arg (). mustDo ( "non-local-bind" ) )
133 Utility :: setBindAny ( AF_INET
, s
);
136 if ( ! d_additional_socket
)
137 g_localaddresses
. push_back ( locala
);
139 if (:: bind ( s
, ( sockaddr
*)& locala
, locala
. getSocklen ()) < 0 ) {
140 string binderror
= strerror ( errno
);
142 if ( errno
== EADDRNOTAVAIL
&& ! :: arg (). mustDo ( "local-address-nonexist-fail" ) ) {
143 g_log
<< Logger :: Error
<< "IPv4 Address " << localname
<< " does not exist on this server - skipping UDP bind" << endl
;
146 g_log
<< Logger :: Error
<< "Unable to bind UDP socket to '" + locala
. toStringWithPort ()+ "': " << binderror
<< endl
;
147 throw PDNSException ( "Unable to bind to UDP socket" );
150 d_sockets
. push_back ( s
);
151 g_log
<< Logger :: Error
<< "UDP server bound to " << locala
. toStringWithPort ()<< endl
;
156 d_rfds
. push_back ( pfd
);
160 bool AddressIsUs ( const ComboAddress
& remote
)
162 for ( const ComboAddress
& us
: g_localaddresses
) {
165 if ( IsAnyAddress ( us
)) {
166 int s
= socket ( remote
. sin4
. sin_family
, SOCK_DGRAM
, 0 );
170 if ( connect ( s
, ( struct sockaddr
*)& remote
, remote
. getSocklen ()) < 0 ) {
175 ComboAddress actualLocal
;
176 actualLocal
. sin4
. sin_family
= remote
. sin4
. sin_family
;
177 socklen_t socklen
= actualLocal
. getSocklen ();
179 if ( getsockname ( s
, ( struct sockaddr
*) & actualLocal
, & socklen
) < 0 ) {
184 actualLocal
. sin4
. sin_port
= us
. sin4
. sin_port
;
185 if ( actualLocal
== remote
)
193 void UDPNameserver :: bindIPv6 ()
195 vector
< string
> locals
;
196 stringtok ( locals
,:: arg ()[ "local-ipv6" ], " ," );
203 for ( vector
< string
>:: const_iterator i
= locals
. begin (); i
!= locals
. end ();++ i
) {
204 string
localname (* i
);
206 s
= socket ( AF_INET6
, SOCK_DGRAM
, 0 );
208 if ( errno
== EAFNOSUPPORT
) {
209 g_log
<< Logger :: Error
<< "IPv6 Address Family is not supported - skipping UDPv6 bind" << endl
;
212 g_log
<< Logger :: Error
<< "Unable to acquire a UDPv6 socket: " + string ( strerror ( errno
)) << endl
;
213 throw PDNSException ( "Unable to acquire a UDPv6 socket: " + string ( strerror ( errno
)));
218 if (! setNonBlocking ( s
))
219 throw PDNSException ( "Unable to set UDPv6 socket to non-blocking: " + stringerror ());
221 ComboAddress
locala ( localname
, :: arg (). asNum ( "local-port" ));
223 if ( IsAnyAddress ( locala
)) {
224 setsockopt ( s
, IPPROTO_IP
, GEN_IP_PKTINFO
, & one
, sizeof ( one
)); // linux supports this, so why not - might fail on other systems
225 #ifdef IPV6_RECVPKTINFO
226 setsockopt ( s
, IPPROTO_IPV6
, IPV6_RECVPKTINFO
, & one
, sizeof ( one
));
228 setsockopt ( s
, IPPROTO_IPV6
, IPV6_V6ONLY
, & one
, sizeof ( one
)); // if this fails, we report an error in tcpreceiver too
231 if (! setSocketTimestamps ( s
))
232 g_log
<< Logger :: Warning
<< "Unable to enable timestamp reporting for socket" << endl
;
235 if ( d_can_reuseport
)
236 if ( setsockopt ( s
, SOL_SOCKET
, SO_REUSEPORT
, & one
, sizeof ( one
)) )
237 d_can_reuseport
= false ;
240 if ( :: arg (). mustDo ( "non-local-bind" ) )
241 Utility :: setBindAny ( AF_INET6
, s
);
243 if ( ! d_additional_socket
)
244 g_localaddresses
. push_back ( locala
);
245 if (:: bind ( s
, ( sockaddr
*)& locala
, locala
. getSocklen ())< 0 ) {
247 if ( errno
== EADDRNOTAVAIL
&& ! :: arg (). mustDo ( "local-ipv6-nonexist-fail" ) ) {
248 g_log
<< Logger :: Error
<< "IPv6 Address " << localname
<< " does not exist on this server - skipping UDP bind" << endl
;
251 g_log
<< Logger :: Error
<< "Unable to bind to UDPv6 socket " << localname
<< ": " << strerror ( errno
)<< endl
;
252 throw PDNSException ( "Unable to bind to UDPv6 socket" );
255 d_sockets
. push_back ( s
);
260 d_rfds
. push_back ( pfd
);
261 g_log
<< Logger :: Error
<< "UDPv6 server bound to " << locala
. toStringWithPort ()<< endl
;
265 UDPNameserver :: UDPNameserver ( bool additional_socket
)
268 d_can_reuseport
= :: arg (). mustDo ( "reuseport" );
270 // Are we the main socket (false) or a rebinding using SO_REUSEPORT ?
271 d_additional_socket
= additional_socket
;
273 if (!:: arg ()[ "local-address" ]. empty ())
275 if (!:: arg ()[ "local-ipv6" ]. empty ())
278 if (:: arg ()[ "local-address" ]. empty () && :: arg ()[ "local-ipv6" ]. empty ())
279 g_log
<< Logger :: Critical
<< "PDNS is deaf and mute! Not listening on any interfaces" << endl
;
282 void UDPNameserver :: send ( DNSPacket
* p
)
284 string buffer
= p
-> getString ();
285 g_rs
. submitResponse (* p
, true );
291 fillMSGHdr (& msgh
, & iov
, cbuf
, 0 , ( char *) buffer
. c_str (), buffer
. length (), & p
-> d_remote
);
293 msgh
. msg_control
= NULL
;
295 addCMsgSrcAddr (& msgh
, cbuf
, p
-> d_anyLocal
. get_ptr (), 0 );
297 DLOG ( g_log
<< Logger :: Notice
<< "Sending a packet to " << p
-> getRemote () << " (" << buffer
. length ()<< " octets)" << endl
);
298 if ( buffer
. length () > p
-> getMaxReplyLen ()) {
299 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
;
301 if ( sendmsg ( p
-> getSocket (), & msgh
, 0 ) < 0 )
302 g_log
<< Logger :: Error
<< "Error sending reply with sendmsg (socket=" << p
-> getSocket ()<< ", dest=" << p
-> d_remote
. toStringWithPort ()<< "): " << strerror ( errno
)<< endl
;
305 DNSPacket
* UDPNameserver :: receive ( DNSPacket
* prefilled
, std :: string
& buffer
)
310 Utility :: sock_t sock
=- 1 ;
316 remote
. sin6
. sin6_family
= AF_INET6
; // make sure it is big enough
317 fillMSGHdr (& msgh
, & iov
, cbuf
, sizeof ( cbuf
), & buffer
. at ( 0 ), buffer
. size (), & remote
);
320 vector
< struct pollfd
> rfds
= d_rfds
;
322 for ( auto & pfd
: rfds
) {
329 err
= poll (& rfds
[ 0 ], rfds
. size (), - 1 );
333 unixDie ( "Unable to poll for new UDP events" );
336 for ( auto & pfd
: rfds
) {
337 if ( pfd
. revents
& POLLIN
) {
339 if (( len
= recvmsg ( sock
, & msgh
, 0 )) < 0 ) {
341 g_log
<< Logger :: Error
<< "recvfrom gave error, ignoring: " << strerror ( errno
)<< endl
;
348 throw PDNSException ( "poll betrayed us! (should not happen)" );
350 DLOG ( g_log
<< "Received a packet " << len
<< " bytes long from " << remote
. toString ()<< endl
);
352 BOOST_STATIC_ASSERT ( offsetof ( sockaddr_in
, sin_port
) == offsetof ( sockaddr_in6
, sin6_port
));
354 if ( remote
. sin4
. sin_port
== 0 ) // would generate error on responding. sin4 also works for ipv6
358 if ( prefilled
) // they gave us a preallocated packet
361 packet
= new DNSPacket ( true ); // don't forget to free it!
363 packet
-> setSocket ( sock
);
364 packet
-> setRemote (& remote
);
367 if ( HarvestDestinationAddress (& msgh
, & dest
)) {
368 // cerr<<"Setting d_anyLocal to '"<<dest.toString()<<"'"<<endl;
369 packet
-> d_anyLocal
= dest
;
372 struct timeval recvtv
;
373 if ( HarvestTimestamp (& msgh
, & recvtv
)) {
374 packet
-> d_dt
. setTimeval ( recvtv
);
377 packet
-> d_dt
. set (); // timing
379 if ( packet
-> parse (& buffer
. at ( 0 ), ( size_t ) len
)< 0 ) {
380 S
. inc ( "corrupt-packets" );
381 S
. ringAccount ( "remotes-corrupt" , packet
-> d_remote
);
385 return 0 ; // unable to parse