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