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 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);
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
78 /** This constructor accepts a dns server address. The address must be in dotted
79 * decimal form, e.g. 1.2.3.4.
81 DNS(const std::string &dnsserver);
82 /** The destructor frees all used structures.
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
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.
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.
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.
101 /** This method will return true if the lookup's fd matches the one provided
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.
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.
113 void SetNS(const std::string &dnsserver);
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.
126 class Resolver : public Extensible
130 * The lowlevel DNS object used by Resolver
134 * The input data, either a host or an IP address
138 * True if a forward lookup is being performed, false if otherwise
142 * The DNS erver being used for lookups. If this is an empty string,
143 * the value of ServerConfig::DNSServer is used instead.
147 * The file descriptor used for the DNS lookup
151 * The output data, e.g. a hostname or an IP.
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
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.
173 Resolver(const std::string &source, bool forward, const std::string &dnsserver);
175 * The default destructor does nothing.
179 * When your lookup completes, this method will be called.
180 * @param result The resulting DNS lookup, either an IP address or a hostname.
182 virtual void OnLookupComplete(const std::string &result);
184 * If an error occurs (such as NXDOMAIN, no domain name found) then this method
186 * @param e A ResolverError enum containing the error type which has occured.
188 virtual void OnError(ResolverError e);
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.
194 bool ProcessResult();
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.
204 * Clear the pointer table used for Resolver classes
208 * Deal with a Resolver class which has become writeable
210 void dns_deal_with_classes(int fd);
212 * Add a resolver class to our active table
214 bool dns_add_class(Resolver* r);
216 void dns_close(int fd);
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
224 void* dns_task(void* arg);