2 dns.h - dns library declarations based on firedns Copyright (C) 2002 Ian Gulliver
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.
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.
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
22 #include "inspircd_config.h"
33 * Error types that class Resolver can emit to its error method.
39 RESOLVER_NXDOMAIN = 2,
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.
51 class DNS : public Extensible
60 void dns_init_2(const char* dnsserver);
61 in_addr *dns_aton4(const char * const ipstring);
62 char *dns_ntoa4(const in_addr * const ip);
63 int dns_getip4(const char * const name);
64 int dns_getip4list(const char * const name);
65 int dns_getname4(const in_addr * const ip);
66 char *dns_getresult(const int fd);
67 in_addr *dns_aton4_s(const char * const ipstring, in_addr * const ip);
68 char *dns_ntoa4_s(const in_addr * const ip, char * const result);
69 char *dns_getresult_s(const int fd, char * const result);
70 in_addr *dns_aton4_r(const char * const ipstring);
71 char *dns_ntoa4_r(const in_addr * const ip);
72 char *dns_getresult_r(const int fd);
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
81 /** This constructor accepts a dns server address. The address must be in dotted
82 * decimal form, e.g. 1.2.3.4.
84 DNS(const std::string &dnsserver);
85 /** The destructor frees all used structures.
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
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.
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.
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.
104 /** This method will return true if the lookup's fd matches the one provided
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.
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.
116 void SetNS(const std::string &dnsserver);
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.
129 class Resolver : public Extensible
133 * The lowlevel DNS object used by Resolver
137 * The input data, either a host or an IP address
141 * True if a forward lookup is being performed, false if otherwise
145 * The DNS erver being used for lookups. If this is an empty string,
146 * the value of ServerConfig::DNSServer is used instead.
150 * The file descriptor used for the DNS lookup
154 * The output data, e.g. a hostname or an IP.
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
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.
176 Resolver(const std::string &source, bool forward, const std::string &dnsserver);
178 * The default destructor does nothing.
182 * When your lookup completes, this method will be called.
183 * @param result The resulting DNS lookup, either an IP address or a hostname.
185 virtual void OnLookupComplete(const std::string &result);
187 * If an error occurs (such as NXDOMAIN, no domain name found) then this method
189 * @param e A ResolverError enum containing the error type which has occured.
191 virtual void OnError(ResolverError e);
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.
197 bool ProcessResult();
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.
207 * Clear the pointer table used for Resolver classes
211 * Deal with a Resolver class which has become writeable
213 void dns_deal_with_classes(int fd);
215 * Add a resolver class to our active table
217 bool dns_add_class(Resolver* r);
220 /** This is the handler function for multi-threaded DNS.
221 * It cannot be a class member as pthread will not let us
222 * create a thread whos handler function is a member of
225 void* dns_task(void* arg);