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 "dns.hh"
24 #include "iputils.hh"
25 #include "dnsbackend.hh"
26 #include "packethandler.hh"
27 #include <vector>
28 #include <mutex>
29 #include <poll.h>
30 #include <sys/select.h>
31 #include <sys/socket.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
34 #include <sys/stat.h>
35 #include <unistd.h>
36 #include <netdb.h>
37 #include <sys/uio.h>
38 #include <sys/select.h>
39 
40 #include "namespaces.hh"
41 
42 class TCPNameserver
43 {
44 public:
45   TCPNameserver();
46   ~TCPNameserver();
47   void go();
48   unsigned int numTCPConnections();
49 private:
50 
51   static void sendPacket(std::unique_ptr<DNSPacket>& p, int outsock, bool last=true);
52   static int readLength(int fd, ComboAddress *remote);
53   static void getQuestion(int fd, char *mesg, int pktlen, const ComboAddress& remote, unsigned int totalTime);
54   static int doAXFR(const DNSName &target, std::unique_ptr<DNSPacket>& q, int outsock);
55   static int doIXFR(std::unique_ptr<DNSPacket>& q, int outsock);
56   static bool canDoAXFR(std::unique_ptr<DNSPacket>& q, bool isAXFR);
57   static void doConnection(int fd);
58   static void decrementClientCount(const ComboAddress& remote);
59   void thread(void);
60   static std::mutex s_plock;
61   static std::mutex s_clientsCountMutex;
62   static std::map<ComboAddress,size_t,ComboAddress::addressOnlyLessThan> s_clientsCount;
63   static std::unique_ptr<PacketHandler> s_P;
64   static std::unique_ptr<Semaphore> d_connectionroom_sem;
65   static unsigned int d_maxTCPConnections;
66   static NetmaskGroup d_ng;
67   static size_t d_maxTransactionsPerConn;
68   static size_t d_maxConnectionsPerClient;
69   static unsigned int d_idleTimeout;
70   static unsigned int d_maxConnectionDuration;
71 
72   vector<int>d_sockets;
73   vector<struct pollfd> d_prfds;
74 };
75