| 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. |
|---|
| 50 | |
|---|
| 51 | The UeberBackend is transparent for exceptions, which should fall straight through. |
|---|
| 52 | */ |
|---|
| 53 | |
|---|
| 54 | class UeberBackend : public DNSBackend |
|---|
| 55 | { |
|---|
| 56 | public: |
|---|
| 57 | UeberBackend(); |
|---|
| 58 | UeberBackend(const string &); |
|---|
| 59 | ~UeberBackend(); |
|---|
| 60 | typedef DNSBackend *BackendMaker(); //!< typedef for functions returning pointers to new backends |
|---|
| 61 | |
|---|
| 62 | bool superMasterBackend(const string &ip, const string &domain, const vector<DNSResourceRecord>&nsset, string *account, DNSBackend **db); |
|---|
| 63 | |
|---|
| 64 | /** contains BackendReporter objects, which contain maker functions and information about |
|---|
| 65 | weather a module has already been reported to existing instances of the UeberBackend |
|---|
| 66 | */ |
|---|
| 67 | static vector<BackendReporter>backendmakers; |
|---|
| 68 | |
|---|
| 69 | /** Tracks all created UeberBackend instances for us. We use this vector to notify |
|---|
| 70 | existing threads of new modules |
|---|
| 71 | */ |
|---|
| 72 | static vector<UeberBackend *>instances; |
|---|
| 73 | static pthread_mutex_t instances_lock; |
|---|
| 74 | |
|---|
| 75 | static bool loadmodule(const string &name); |
|---|
| 76 | |
|---|
| 77 | /** Thread function that listens on our unix domain socket for commands, for example |
|---|
| 78 | instructions to load new modules */ |
|---|
| 79 | static void *DynListener(void *); |
|---|
| 80 | static void go(void); |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | /** This contains all registered backends. The DynListener modifies this list for us when |
|---|
| 84 | new modules are loaded */ |
|---|
| 85 | vector<DNSBackend*>backends; |
|---|
| 86 | |
|---|
| 87 | void die(); |
|---|
| 88 | void cleanup(); |
|---|
| 89 | |
|---|
| 90 | //! the very magic handle for UeberBackend questions |
|---|
| 91 | class handle |
|---|
| 92 | { |
|---|
| 93 | public: |
|---|
| 94 | bool get(DNSResourceRecord &r); |
|---|
| 95 | handle(); |
|---|
| 96 | ~handle(); |
|---|
| 97 | |
|---|
| 98 | //! The UeberBackend class where this handle belongs to |
|---|
| 99 | UeberBackend *parent; |
|---|
| 100 | //! The current real backend, which is answering questions |
|---|
| 101 | DNSBackend *d_hinterBackend; |
|---|
| 102 | |
|---|
| 103 | //! Index of the current backend within the backends vector |
|---|
| 104 | unsigned int i; |
|---|
| 105 | |
|---|
| 106 | //! DNSPacket who asked this question |
|---|
| 107 | DNSPacket *pkt_p; |
|---|
| 108 | string qname; |
|---|
| 109 | QType qtype; |
|---|
| 110 | private: |
|---|
| 111 | |
|---|
| 112 | static int instances; |
|---|
| 113 | }; |
|---|
| 114 | |
|---|
| 115 | void lookup(const QType &, const string &qdomain, DNSPacket *pkt_p=0, int zoneId=-1); |
|---|
| 116 | |
|---|
| 117 | bool getSOA(const string &domain, SOAData &sd); |
|---|
| 118 | bool list(const string &target, int domain_id); |
|---|
| 119 | bool get(DNSResourceRecord &r); |
|---|
| 120 | |
|---|
| 121 | static DNSBackend *maker(const map<string,string> &); |
|---|
| 122 | static void closeDynListener(); |
|---|
| 123 | static void UeberBackend::setStatus(const string &st); |
|---|
| 124 | void getUnfreshSlaveInfos(vector<DomainInfo>* domains); |
|---|
| 125 | void getUpdatedMasters(vector<DomainInfo>* domains); |
|---|
| 126 | bool getDomainInfo(const string &domain, DomainInfo &di); |
|---|
| 127 | void rediscover(string* status=0); |
|---|
| 128 | void reload(); |
|---|
| 129 | private: |
|---|
| 130 | DNSResourceRecord lastrr; |
|---|
| 131 | pthread_t tid; |
|---|
| 132 | handle d_handle; |
|---|
| 133 | bool d_negcached; |
|---|
| 134 | bool d_cached; |
|---|
| 135 | struct Question |
|---|
| 136 | { |
|---|
| 137 | QType qtype; |
|---|
| 138 | string qname; |
|---|
| 139 | int zoneId; |
|---|
| 140 | }d_question; |
|---|
| 141 | DNSResourceRecord d_answer; |
|---|
| 142 | |
|---|
| 143 | int cacheHas(const Question &q, DNSResourceRecord &rr); |
|---|
| 144 | void addNegCache(const Question &q); |
|---|
| 145 | void addOneCache(const Question &q, const DNSResourceRecord &rr); |
|---|
| 146 | |
|---|
| 147 | static pthread_mutex_t d_mut; |
|---|
| 148 | static pthread_cond_t d_cond; |
|---|
| 149 | static sem_t d_dynserialize; |
|---|
| 150 | static bool d_go; |
|---|
| 151 | static int s_s; |
|---|
| 152 | static string s_status; |
|---|
| 153 | int d_ancount; |
|---|
| 154 | static string programname; |
|---|
| 155 | bool stale; |
|---|
| 156 | }; |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | /** Class used to report new backends. It stores a maker function, and a flag that indicates that |
|---|
| 160 | this module has been reported */ |
|---|
| 161 | class BackendReporter |
|---|
| 162 | { |
|---|
| 163 | public: |
|---|
| 164 | BackendReporter(UeberBackend::BackendMaker *p) |
|---|
| 165 | { |
|---|
| 166 | maker=p; |
|---|
| 167 | reported=false; |
|---|
| 168 | }; |
|---|
| 169 | map<string,string>d_parameters; |
|---|
| 170 | UeberBackend::BackendMaker *maker; //!< function to make this backend |
|---|
| 171 | bool reported; //!< if this backend has been reported to running UeberBackend threads |
|---|
| 172 | private: |
|---|
| 173 | }; |
|---|
| 174 | |
|---|
| 175 | #endif |
|---|