]>
Commit | Line | Data |
---|---|---|
12c86877 | 1 | /* |
12471842 PL |
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 | */ | |
12c86877 BH |
22 | #ifndef NAMESERVER_HH |
23 | #define NAMESERVER_HH | |
6e059b95 | 24 | |
76473b92 KM |
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> | |
12c86877 | 33 | #include <vector> |
fa8fd4d2 | 34 | |
12c86877 | 35 | #include "statbag.hh" |
10f4eea8 | 36 | #include "namespaces.hh" |
38720c1b | 37 | #include "dnspacket.hh" |
ba7244a5 | 38 | #include "responsestats.hh" |
12c86877 BH |
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 | ||
4ee30c66 MZ |
74 | #ifdef __linux__ |
75 | #ifndef SO_REUSEPORT | |
76 | #define SO_REUSEPORT 15 | |
77 | #endif | |
78 | #endif | |
79 | ||
12c86877 BH |
80 | class UDPNameserver |
81 | { | |
82 | public: | |
3a56adcc | 83 | UDPNameserver( bool additional_socket = false ); //!< Opens the socket |
2b6f1436 | 84 | DNSPacket *receive(DNSPacket *prefilled=0); //!< call this in a while or for(;;) loop to get packets |
4ee30c66 MZ |
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 | }; | |
12c86877 BH |
93 | |
94 | private: | |
3a56adcc | 95 | bool d_additional_socket; |
4ee30c66 MZ |
96 | #ifdef SO_REUSEPORT |
97 | bool d_can_reuseport; | |
98 | #endif | |
12c86877 BH |
99 | vector<int> d_sockets; |
100 | void bindIPv4(); | |
101 | void bindIPv6(); | |
6e242246 | 102 | vector<pollfd> d_rfds; |
12c86877 BH |
103 | }; |
104 | ||
8db49a64 | 105 | bool AddressIsUs(const ComboAddress& remote); |
12c86877 | 106 | |
ba7244a5 PD |
107 | extern ResponseStats g_rs; |
108 | ||
12c86877 | 109 | #endif |