]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/ueberbackend.hh
Initial revision
[thirdparty/pdns.git] / pdns / ueberbackend.hh
1 /*
2 PowerDNS Versatile Database Driven Nameserver
3 Copyright (C) 2002 PowerDNS.COM BV
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 #ifndef UEBERBACKEND_HH
20 #define UEBERBACKEND_HH
21
22 #include <vector>
23 #include <map>
24 #include <string>
25 #include <algorithm>
26 #include <pthread.h>
27 #include <semaphore.h>
28
29 #ifndef WIN32
30 #include <sys/un.h>
31 #include <dlfcn.h>
32 #include <unistd.h>
33 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <sys/stat.h>
36 #include <fcntl.h>
37 #include <unistd.h>
38 #endif // WIN32
39
40 #include "dnspacket.hh"
41 #include "dnsbackend.hh"
42
43 using namespace std;
44
45 class BackendReporter;
46
47 /** This is a very magic backend that allows us to load modules dynamically,
48 and query them in order. This is persistent over all UeberBackend instantiations
49 across multiple threads. Very magic */
50
51 class UeberBackend : public DNSBackend
52 {
53 public:
54 UeberBackend();
55 UeberBackend(const string &);
56 ~UeberBackend();
57 typedef DNSBackend *BackendMaker(); //!< typedef for functions returning pointers to new backends
58
59 bool superMasterBackend(const string &ip, const string &domain, const vector<DNSResourceRecord>&nsset, string *account, DNSBackend **db);
60
61 /** contains BackendReporter objects, which contain maker functions and information about
62 weather a module has already been reported to existing instances of the UeberBackend
63 */
64 static vector<BackendReporter>backendmakers;
65
66 /** Tracks all created UeberBackend instances for us. We use this vector to notify
67 existing threads of new modules
68 */
69 static vector<UeberBackend *>instances;
70 static pthread_mutex_t instances_lock;
71
72 static bool loadmodule(const string &name);
73
74 /** Thread function that listens on our unix domain socket for commands, for example
75 instructions to load new modules */
76 static void *DynListener(void *);
77 static void go(void);
78
79
80 /** This contains all registered backends. The DynListener modifies this list for us when
81 new modules are loaded */
82 vector<DNSBackend*>backends;
83
84 void die();
85 void cleanup();
86
87 //! the very magic handle for UeberBackend questions
88 class handle
89 {
90 public:
91 bool get(DNSResourceRecord &r);
92 handle();
93 ~handle();
94
95 //! The UeberBackend class where this handle belongs to
96 UeberBackend *parent;
97 //! The current real backend, which is answering questions
98 DNSBackend *d_hinterBackend;
99
100 //! Index of the current backend within the backends vector
101 unsigned int i;
102
103 //! DNSPacket who asked this question
104 DNSPacket *pkt_p;
105 string qname;
106 QType qtype;
107 private:
108
109 static int instances;
110 };
111
112 void lookup(const QType &, const string &qdomain, DNSPacket *pkt_p=0, int zoneId=-1);
113
114 bool getSOA(const string &domain, SOAData &sd);
115 bool list(int domain_id);
116 bool get(DNSResourceRecord &r);
117
118 static DNSBackend *maker(const map<string,string> &);
119 static void closeDynListener();
120 static void UeberBackend::setStatus(const string &st);
121 void getUnfreshSlaveInfos(vector<DomainInfo>* domains);
122 void getUpdatedMasters(vector<DomainInfo>* domains);
123 bool getDomainInfo(const string &domain, DomainInfo &di);
124 void rediscover();
125 void reload();
126 private:
127 DNSResourceRecord lastrr;
128 pthread_t tid;
129 handle d_handle;
130 bool d_negcached;
131 bool d_cached;
132 struct Question
133 {
134 QType qtype;
135 string qname;
136 int zoneId;
137 }d_question;
138 DNSResourceRecord d_answer;
139
140 int cacheHas(const Question &q, DNSResourceRecord &rr);
141 void addNegCache(const Question &q);
142 void addOneCache(const Question &q, const DNSResourceRecord &rr);
143
144 static pthread_mutex_t d_mut;
145 static pthread_cond_t d_cond;
146 static sem_t d_dynserialize;
147 static bool d_go;
148 static int s_s;
149 static string s_status;
150 int d_ancount;
151 static string programname;
152 bool stale;
153 };
154
155
156 /** Class used to report new backends. It stores a maker function, and a flag that indicates that
157 this module has been reported */
158 class BackendReporter
159 {
160 public:
161 BackendReporter(UeberBackend::BackendMaker *p)
162 {
163 maker=p;
164 reported=false;
165 };
166 map<string,string>d_parameters;
167 UeberBackend::BackendMaker *maker; //!< function to make this backend
168 bool reported; //!< if this backend has been reported to running UeberBackend threads
169 private:
170 };
171
172 #endif