]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/dns.h
AF_INET -> define to AF_FAMILY, will be either AF_INET or AF_INET6
[user/henk/code/inspircd.git] / include / dns.h
1 /*
2 dns.h - dns library declarations based on firedns Copyright (C) 2002 Ian Gulliver
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of version 2 of the GNU General Public License as
6 published by the Free Software Foundation.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16 */
17
18 #ifndef _DNS_H
19 #define _DNS_H
20
21 #include <string>
22 #include "inspircd_config.h"
23 #include "socket.h"
24 #include "base.h"
25
26 struct dns_ip4list
27 {
28         insp_inaddr ip;
29         dns_ip4list *next;
30 };
31
32 /**
33  * Error types that class Resolver can emit to its error method.
34  */
35 enum ResolverError
36 {
37         RESOLVER_NOERROR        =       0,
38         RESOLVER_NSDOWN         =       1,
39         RESOLVER_NXDOMAIN       =       2,
40         RESOLVER_NOTREADY       =       3
41 };
42
43
44 /** The DNS class allows fast nonblocking resolution of hostnames
45  * and ip addresses. It is based heavily upon firedns by Ian Gulliver.
46  * Modules SHOULD avoid using this class to resolve hostnames and IP
47  * addresses, as it is a low-level wrapper around the UDP socket routines
48  * and is probably not abstracted enough for real use. Please see the
49  * Resolver class if you wish to resolve hostnames.
50  */
51 class DNS : public Extensible
52 {
53 private:
54         insp_inaddr *binip;
55         char* result;
56         char localbuf[1024];
57         int t;
58         void dns_init();
59         int myfd;
60         void dns_init_2(const char* dnsserver);
61         insp_inaddr *dns_aton4(const char * const ipstring);
62         char *dns_ntoa4(const insp_inaddr * const ip);
63         int dns_getip4(const char * const name);
64         int dns_getip4list(const char * const name);
65         int dns_getname4(const insp_inaddr * const ip);
66         char *dns_getresult(const int fd);
67         insp_inaddr *dns_aton4_s(const char * const ipstring, insp_inaddr * const ip);
68         char *dns_ntoa4_s(const insp_inaddr * const ip, char * const result);
69         char *dns_getresult_s(const int fd, char * const result);
70         insp_inaddr *dns_aton4_r(const char * const ipstring);
71         char *dns_ntoa4_r(const insp_inaddr * const ip);
72         char *dns_getresult_r(const int fd);
73 public:
74         /** The default constructor uses dns addresses read from /etc/resolv.conf.
75          * Please note that it will re-read /etc/resolv.conf for each copy of the
76          * class you instantiate, causing disk access and slow lookups if you create
77          * a lot of them. Consider passing the constructor a server address as a parameter
78          * instead.
79          */
80         DNS();
81         /** This constructor accepts a dns server address. The address must be in dotted
82          * decimal form, e.g. 1.2.3.4.
83          */
84         DNS(const std::string &dnsserver);
85         /** The destructor frees all used structures.
86          */
87         ~DNS();
88         /** This method will start the reverse lookup of an ip given in dotted decimal
89          * format, e.g. 1.2.3.4, and returns true if the lookup was successfully
90          * initiated.
91          */
92         bool ReverseLookup(const std::string &ip, bool ins);
93         /** This method will start the forward lookup of a hostname, e.g. www.inspircd.org,
94          * and returns true if the lookup was successfully initiated.
95          */
96         bool ForwardLookup(const std::string &host, bool ins);
97         /** Used by modules to perform a dns lookup but have the socket engine poll a module, instead of the dns object directly.
98          */
99         bool ForwardLookupWithFD(const std::string &host, int &fd);
100         /** This method will return true when the lookup is completed. It uses poll internally
101          * to determine the status of the socket.
102          */
103         bool HasResult();
104         /** This method will return true if the lookup's fd matches the one provided
105          */
106         bool HasResult(int fd);
107         /** This method returns the result of your query as a string, depending upon wether you
108          * called DNS::ReverseLookup() or DNS::ForwardLookup.
109          */
110         std::string GetResult();
111         std::string GetResultIP();
112         /** This method returns the file handle used by the dns query socket or zero if the
113          * query is invalid for some reason, e.g. the dns server not responding.
114          */
115         int GetFD();
116         void SetNS(const std::string &dnsserver);
117 };
118
119 /**
120  * The Resolver class is a high-level abstraction for resolving DNS entries.
121  * It can do forward and reverse IPv4 lookups, and when IPv6 is supported, will
122  * also be able to do those, transparent of protocols. Module developers must
123  * extend this class via inheritence, and then insert a pointer to their derived
124  * class into the core using Server::AddResolver(). Once you have done this,
125  * the class will be able to receive callbacks. There are two callbacks which
126  * can occur by calling virtual methods, one is a success situation, and the other
127  * an error situation.
128  */
129 class Resolver : public Extensible
130 {
131  private:
132         /**
133          * The lowlevel DNS object used by Resolver
134          */
135         DNS Query;
136         /**
137          * The input data, either a host or an IP address
138          */
139         std::string input;
140         /**
141          * True if a forward lookup is being performed, false if otherwise
142          */
143         bool fwd;
144         /**
145          * The DNS erver being used for lookups. If this is an empty string,
146          * the value of ServerConfig::DNSServer is used instead.
147          */
148         std::string server;
149         /**
150          * The file descriptor used for the DNS lookup
151          */
152         int fd;
153         /**
154          * The output data, e.g. a hostname or an IP.
155          */
156         std::string result;
157  public:
158         /**
159          * Initiate DNS lookup. Your class should not attempt to delete or free these
160          * objects, as the core will do this for you. They must always be created upon
161          * the heap using new, as you cannot be sure at what time they will be deleted.
162          * Allocating them on the stack or attempting to delete them yourself could cause
163          * the object to go 'out of scope' and cause a segfault in the core if the result
164          * arrives at a later time.
165          * @param source The IP or hostname to resolve
166          * @param forward Set to true to perform a forward lookup (hostname to ip) or false
167          * to perform a reverse lookup (ip to hostname). Lookups on A records and PTR
168          * records are supported. CNAME and MX are not supported by this resolver.
169          * @param dnsserver This optional parameter specifies an alterate nameserver to use.
170          * If it is not specified, or is an empty string, the value of ServerConfig::DNSServer
171          * is used instead.
172          * @throw ModuleException This class may throw an instance of ModuleException, in the
173          * event there are no more file descriptors, or a similar hard error occurs such as
174          * the network being down.
175          */
176         Resolver(const std::string &source, bool forward, const std::string &dnsserver);
177         /**
178          * The default destructor does nothing.
179          */
180         virtual ~Resolver();
181         /**
182          * When your lookup completes, this method will be called.
183          * @param result The resulting DNS lookup, either an IP address or a hostname.
184          */
185         virtual void OnLookupComplete(const std::string &result);
186         /**
187          * If an error occurs (such as NXDOMAIN, no domain name found) then this method
188          * will be called.
189          * @param e A ResolverError enum containing the error type which has occured.
190          */
191         virtual void OnError(ResolverError e);
192         /**
193          * This method is called by the core when the object's file descriptor is ready
194          * for reading, and will then dispatch a call to either OnLookupComplete or
195          * OnError. You should never call this method yourself.
196          */
197         bool ProcessResult();
198         /**
199          * Returns the file descriptor of this class. This is primarily used by the core
200          * to determine where in various tables to place a pointer to your class, but it
201          * is safe to call and use this method.
202          */
203         int GetFd();
204 };
205
206 /**
207  * Clear the pointer table used for Resolver classes
208  */
209 void init_dns();
210 /**
211  * Deal with a Resolver class which has become writeable
212  */
213 void dns_deal_with_classes(int fd);
214 /**
215  * Add a resolver class to our active table
216  */
217 bool dns_add_class(Resolver* r);
218
219 void dns_close(int fd);
220
221 #ifdef THREADED_DNS
222 /** This is the handler function for multi-threaded DNS.
223  * It cannot be a class member as pthread will not let us
224  * create a thread whos handler function is a member of
225  * a class (ugh).
226  */
227 void* dns_task(void* arg);
228 #endif
229
230 #endif