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