]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/nameserver.hh
Merge pull request #1896 from tuxis-ie/mysql-foreign-keys
[thirdparty/pdns.git] / pdns / nameserver.hh
1 /*
2 PowerDNS Versatile Database Driven Nameserver
3 Copyright (C) 2005 - 2011 PowerDNS.COM BV
4
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License version 2 as published
7 by the Free Software Foundation
8
9 Additionally, the license of this program contains a special
10 exception which allows to distribute the program in binary form when
11 it is linked against OpenSSL.
12
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.
17
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 St, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22 #ifndef NAMESERVER_HH
23 #define NAMESERVER_HH
24
25 #include <poll.h>
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <netinet/in.h>
29 #include <sys/time.h>
30 #include <unistd.h>
31 #include <arpa/inet.h>
32 #include <netdb.h>
33 #include <vector>
34
35 #include "statbag.hh"
36 #include "namespaces.hh"
37 #include "dnspacket.hh"
38 #include "responsestats.hh"
39
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.
42
43 Some sample code in main():
44 \code
45 typedef Distributor<DNSPacket,DNSPacket,PacketHandler> DNSDistributor;
46 DNSDistributor D(6); // the big dispatcher!
47
48 pthread_t qtid, atid;
49 N=new UDPNameserver;
50
51 pthread_create(&qtid,0,qthread,static_cast<void *>(&D)); // receives packets
52 pthread_create(&atid,0,athread,static_cast<void *>(&D)); // sends packets
53 \endcode
54
55 Code for qthread:
56 \code
57 void *qthread(void *p)
58 {
59 DNSDistributor *D=static_cast<DNSDistributor *>(p);
60
61 DNSPacket *P;
62
63 while((P=N->receive())) // receive a packet
64 {
65 D->question(P); // and give to the distributor, they will delete it
66 }
67 return 0;
68 }
69
70 \endcode
71
72 */
73
74 #ifdef __linux__
75 #ifndef SO_REUSEPORT
76 #define SO_REUSEPORT 15
77 #endif
78 #endif
79
80 class UDPNameserver
81 {
82 public:
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() {
87 #ifdef SO_REUSEPORT
88 return d_can_reuseport;
89 #else
90 return false;
91 #endif
92 };
93
94 private:
95 bool d_additional_socket;
96 #ifdef SO_REUSEPORT
97 bool d_can_reuseport;
98 #endif
99 vector<int> d_sockets;
100 void bindIPv4();
101 void bindIPv6();
102 vector<pollfd> d_rfds;
103 };
104
105 bool AddressIsUs(const ComboAddress& remote);
106
107 extern ResponseStats g_rs;
108
109 #endif