]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/dns.h
Added class Resolver (needs testing)
[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         in_addr ip;
29         dns_ip4list *next;
30 };
31
32 enum ResolverError
33 {
34         RESOLVER_NOERROR        =       0,
35         RESOLVER_NSDOWN         =       1,
36         RESOLVER_NXDOMAIN       =       2,
37         RESOLVER_NOTREADY       =       3
38 };
39
40
41 /** The DNS class allows fast nonblocking resolution of hostnames
42  * and ip addresses. It is based heavily upon firedns by Ian Gulliver.
43  */
44 class DNS : public Extensible
45 {
46 private:
47         in_addr *binip;
48         char* result;
49         char localbuf[1024];
50         int t;
51         void dns_init();
52         int myfd;
53         void dns_init_2(const char* dnsserver);
54         in_addr *dns_aton4(const char * const ipstring);
55         char *dns_ntoa4(const in_addr * const ip);
56         int dns_getip4(const char * const name);
57         int dns_getip4list(const char * const name);
58         int dns_getname4(const in_addr * const ip);
59         char *dns_getresult(const int fd);
60         in_addr *dns_aton4_s(const char * const ipstring, in_addr * const ip);
61         char *dns_ntoa4_s(const in_addr * const ip, char * const result);
62         char *dns_getresult_s(const int fd, char * const result);
63         in_addr *dns_aton4_r(const char * const ipstring);
64         char *dns_ntoa4_r(const in_addr * const ip);
65         char *dns_getresult_r(const int fd);
66 public:
67         /** The default constructor uses dns addresses read from /etc/resolv.conf.
68          * Please note that it will re-read /etc/resolv.conf for each copy of the
69          * class you instantiate, causing disk access and slow lookups if you create
70          * a lot of them. Consider passing the constructor a server address as a parameter
71          * instead.
72          */
73         DNS();
74         /** This constructor accepts a dns server address. The address must be in dotted
75          * decimal form, e.g. 1.2.3.4.
76          */
77         DNS(const std::string &dnsserver);
78         /** The destructor frees all used structures.
79          */
80         ~DNS();
81         /** This method will start the reverse lookup of an ip given in dotted decimal
82          * format, e.g. 1.2.3.4, and returns true if the lookup was successfully
83          * initiated.
84          */
85         bool ReverseLookup(const std::string &ip);
86         /** This method will start the forward lookup of a hostname, e.g. www.inspircd.org,
87          * and returns true if the lookup was successfully initiated.
88          */
89         bool ForwardLookup(const std::string &host);
90         /** Used by modules to perform a dns lookup but have the socket engine poll a module, instead of the dns object directly.
91          */
92         bool ForwardLookupWithFD(const std::string &host, int &fd);
93         /** This method will return true when the lookup is completed. It uses poll internally
94          * to determine the status of the socket.
95          */
96         bool HasResult();
97         /** This method will return true if the lookup's fd matches the one provided
98          */
99         bool HasResult(int fd);
100         /** This method returns the result of your query as a string, depending upon wether you
101          * called DNS::ReverseLookup() or DNS::ForwardLookup.
102          */
103         std::string GetResult();
104         std::string GetResultIP();
105         /** This method returns the file handle used by the dns query socket or zero if the
106          * query is invalid for some reason, e.g. the dns server not responding.
107          */
108         int GetFD();
109         void SetNS(const std::string &dnsserver);
110 };
111
112 class Resolver : public Extensible
113 {
114  private:
115         DNS Query;
116         std::string input;
117         bool fwd;
118         std::string server;
119         int fd;
120         std::string result;
121  public:
122         Resolver(const std::string &source, bool forward, const std::string &dnsserver);
123         virtual ~Resolver();
124         virtual void OnLookupComplete(const std::string &result);
125         virtual void OnError(ResolverError e);
126
127         bool ProcessResult();
128         int GetFd();
129 };
130
131 void init_dns();
132 void dns_deal_with_classes(int fd);
133 bool dns_add_class(Resolver* r);
134
135 /** This is the handler function for multi-threaded DNS.
136  * It cannot be a class member as pthread will not let us
137  * create a thread whos handler function is a member of
138  * a class (ugh).
139  */
140 void* dns_task(void* arg);
141
142 #endif