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.
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <netinet/in.h>
31 #include <arpa/inet.h>
36 #include "namespaces.hh"
37 #include "dnspacket.hh"
38 #include "responsestats.hh"
40 /** This is the main class. It opens a socket on udp port 53 and waits for packets. Those packets can
41 be retrieved with the receive() member function, which returns a DNSPacket.
43 Some sample code in main():
45 typedef Distributor<DNSPacket,DNSPacket,PacketHandler> DNSDistributor;
46 DNSDistributor D(6); // the big dispatcher!
51 pthread_create(&qtid,0,qthread,static_cast<void *>(&D)); // receives packets
52 pthread_create(&atid,0,athread,static_cast<void *>(&D)); // sends packets
57 void *qthread(void *p)
59 DNSDistributor *D=static_cast<DNSDistributor *>(p);
63 while((P=N->receive())) // receive a packet
65 D->question(P); // and give to the distributor, they will delete it
76 #define SO_REUSEPORT 15
83 UDPNameserver( bool additional_socket = false ); //!< Opens the socket
84 DNSPacket *receive(DNSPacket *prefilled=0); //!< call this in a while or for(;;) loop to get packets
85 void send(DNSPacket *); //!< send a DNSPacket. Will call DNSPacket::truncate() if over 512 bytes
86 inline bool canReusePort() {
88 return d_can_reuseport;
95 bool d_additional_socket;
99 vector<int> d_sockets;
102 vector<pollfd> d_rfds;
105 bool AddressIsUs(const ComboAddress& remote);
107 extern ResponseStats g_rs;