]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/nameserver.hh
Merge pull request #9396 from omoerbeek/rec-rpz-ip-multi
[thirdparty/pdns.git] / pdns / nameserver.hh
1 /*
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
4 *
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.
8 *
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.
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 Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22 #pragma once
23 #include <poll.h>
24 #include <sys/types.h>
25 #include <sys/socket.h>
26 #include <netinet/in.h>
27 #include <sys/time.h>
28 #include <unistd.h>
29 #include <arpa/inet.h>
30 #include <netdb.h>
31 #include <vector>
32
33 #include "statbag.hh"
34 #include "namespaces.hh"
35 #include "dnspacket.hh"
36 #include "responsestats.hh"
37
38 /** This is the main class. It opens a socket on udp port 53 and waits for packets. Those packets can
39 be retrieved with the receive() member function, which returns a DNSPacket.
40
41 Some sample code in main():
42 \code
43 typedef Distributor<DNSPacket,DNSPacket,PacketHandler> DNSDistributor;
44 DNSDistributor D(6); // the big dispatcher!
45
46 pthread_t qtid, atid;
47 N=new UDPNameserver;
48
49 pthread_create(&qtid,0,qthread,static_cast<void *>(&D)); // receives packets
50 pthread_create(&atid,0,athread,static_cast<void *>(&D)); // sends packets
51 \endcode
52
53 Code for qthread:
54 \code
55 void *qthread(void *p)
56 {
57 DNSDistributor *D=static_cast<DNSDistributor *>(p);
58
59 DNSPacket *P;
60
61 while((P=N->receive())) // receive a packet
62 {
63 D->question(P); // and give to the distributor, they will delete it
64 }
65 return 0;
66 }
67
68 \endcode
69
70 */
71
72 #ifdef __linux__
73 #ifndef SO_REUSEPORT
74 #define SO_REUSEPORT 15
75 #endif
76 #endif
77
78 class UDPNameserver
79 {
80 public:
81 UDPNameserver( bool additional_socket = false ); //!< Opens the socket
82 bool receive(DNSPacket& packet, std::string& buffer); //!< call this in a while or for(;;) loop to get packets
83 void send(DNSPacket&); //!< send a DNSPacket. Will call DNSPacket::truncate() if over 512 bytes
84 inline bool canReusePort() {
85 return d_can_reuseport;
86 };
87
88 private:
89 bool d_additional_socket;
90 bool d_can_reuseport{false};
91 vector<int> d_sockets;
92 void bindAddresses();
93 vector<pollfd> d_rfds;
94 };
95
96 bool AddressIsUs(const ComboAddress& remote);
97
98 extern ResponseStats g_rs;