]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/dns.h
Fix broken inet_pton call
[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         int dns_getip4(const char * const name);
63         int dns_getip4list(const char * const name);
64         int dns_getname4(const insp_inaddr * const ip);
65         char *dns_getresult(const int fd);
66         insp_inaddr *dns_aton4_s(const char * const ipstring, insp_inaddr * const ip);
67         char *dns_getresult_s(const int fd, char * const result);
68         insp_inaddr *dns_aton4_r(const char * const ipstring);
69         char *dns_getresult_r(const int fd);
70 public:
71         /** The default constructor uses dns addresses read from /etc/resolv.conf.
72          * Please note that it will re-read /etc/resolv.conf for each copy of the
73          * class you instantiate, causing disk access and slow lookups if you create
74          * a lot of them. Consider passing the constructor a server address as a parameter
75          * instead.
76          */
77         DNS();
78         /** This constructor accepts a dns server address. The address must be in dotted
79          * decimal form, e.g. 1.2.3.4.
80          */
81         DNS(const std::string &dnsserver);
82         /** The destructor frees all used structures.
83          */
84         ~DNS();
85         /** This method will start the reverse lookup of an ip given in dotted decimal
86          * format, e.g. 1.2.3.4, and returns true if the lookup was successfully
87          * initiated.
88          */
89         bool ReverseLookup(const std::string &ip, bool ins);
90         /** This method will start the forward lookup of a hostname, e.g. www.inspircd.org,
91          * and returns true if the lookup was successfully initiated.
92          */
93         bool ForwardLookup(const std::string &host, bool ins);
94         /** Used by modules to perform a dns lookup but have the socket engine poll a module, instead of the dns object directly.
95          */
96         bool ForwardLookupWithFD(const std::string &host, int &fd);
97         /** This method will return true when the lookup is completed. It uses poll internally
98          * to determine the status of the socket.
99          */
100         bool HasResult();
101         /** This method will return true if the lookup's fd matches the one provided
102          */
103         bool HasResult(int fd);
104         /** This method returns the result of your query as a string, depending upon wether you
105          * called DNS::ReverseLookup() or DNS::ForwardLookup.
106          */
107         std::string GetResult();
108         std::string GetResultIP();
109         /** This method returns the file handle used by the dns query socket or zero if the
110          * query is invalid for some reason, e.g. the dns server not responding.
111          */
112         int GetFD();
113         void SetNS(const std::string &dnsserver);
114 };
115
116 /**
117  * The Resolver class is a high-level abstraction for resolving DNS entries.
118  * It can do forward and reverse IPv4 lookups, and when IPv6 is supported, will
119  * also be able to do those, transparent of protocols. Module developers must
120  * extend this class via inheritence, and then insert a pointer to their derived
121  * class into the core using Server::AddResolver(). Once you have done this,
122  * the class will be able to receive callbacks. There are two callbacks which
123  * can occur by calling virtual methods, one is a success situation, and the other
124  * an error situation.
125  */
126 class Resolver : public Extensible
127 {
128  private:
129         /**
130          * The lowlevel DNS object used by Resolver
131          */
132         DNS Query;
133         /**
134          * The input data, either a host or an IP address
135          */
136         std::string input;
137         /**
138          * True if a forward lookup is being performed, false if otherwise
139          */
140         bool fwd;
141         /**
142          * The DNS erver being used for lookups. If this is an empty string,
143          * the value of ServerConfig::DNSServer is used instead.
144          */
145         std::string server;
146         /**
147          * The file descriptor used for the DNS lookup
148          */
149         int fd;
150         /**
151          * The output data, e.g. a hostname or an IP.
152          */
153         std::string result;
154  public:
155         /**
156          * Initiate DNS lookup. Your class should not attempt to delete or free these
157          * objects, as the core will do this for you. They must always be created upon
158          * the heap using new, as you cannot be sure at what time they will be deleted.
159          * Allocating them on the stack or attempting to delete them yourself could cause
160          * the object to go 'out of scope' and cause a segfault in the core if the result
161          * arrives at a later time.
162          * @param source The IP or hostname to resolve
163          * @param forward Set to true to perform a forward lookup (hostname to ip) or false
164          * to perform a reverse lookup (ip to hostname). Lookups on A records and PTR
165          * records are supported. CNAME and MX are not supported by this resolver.
166          * @param dnsserver This optional parameter specifies an alterate nameserver to use.
167          * If it is not specified, or is an empty string, the value of ServerConfig::DNSServer
168          * is used instead.
169          * @throw ModuleException This class may throw an instance of ModuleException, in the
170          * event there are no more file descriptors, or a similar hard error occurs such as
171          * the network being down.
172          */
173         Resolver(const std::string &source, bool forward, const std::string &dnsserver);
174         /**
175          * The default destructor does nothing.
176          */
177         virtual ~Resolver();
178         /**
179          * When your lookup completes, this method will be called.
180          * @param result The resulting DNS lookup, either an IP address or a hostname.
181          */
182         virtual void OnLookupComplete(const std::string &result);
183         /**
184          * If an error occurs (such as NXDOMAIN, no domain name found) then this method
185          * will be called.
186          * @param e A ResolverError enum containing the error type which has occured.
187          */
188         virtual void OnError(ResolverError e);
189         /**
190          * This method is called by the core when the object's file descriptor is ready
191          * for reading, and will then dispatch a call to either OnLookupComplete or
192          * OnError. You should never call this method yourself.
193          */
194         bool ProcessResult();
195         /**
196          * Returns the file descriptor of this class. This is primarily used by the core
197          * to determine where in various tables to place a pointer to your class, but it
198          * is safe to call and use this method.
199          */
200         int GetFd();
201 };
202
203 /**
204  * Clear the pointer table used for Resolver classes
205  */
206 void init_dns();
207 /**
208  * Deal with a Resolver class which has become writeable
209  */
210 void dns_deal_with_classes(int fd);
211 /**
212  * Add a resolver class to our active table
213  */
214 bool dns_add_class(Resolver* r);
215
216 void dns_close(int fd);
217
218 #ifdef THREADED_DNS
219 /** This is the handler function for multi-threaded DNS.
220  * It cannot be a class member as pthread will not let us
221  * create a thread whos handler function is a member of
222  * a class (ugh).
223  */
224 void* dns_task(void* arg);
225 #endif
226
227 #endif