* | Inspire Internet Relay Chat Daemon |
* +------------------------------------+
*
- * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
- * E-mail:
- * <brain@chatspike.net>
- * <Craig@chatspike.net>
+ * InspIRCd: (C) 2002-2007 InspIRCd Development Team
+ * See: http://www.inspircd.org/wiki/index.php/Credits
*
- * Written by Craig Edwards, Craig McLure, and others.
* This program is free but copyrighted software; see
- * the file COPYING for details.
+ * the file COPYING for details.
*
* ---------------------------------------------------
*/
#include "base.h"
#include "socketengine.h"
#include "socket.h"
+#include "hash_map.h"
+#include "hashcomp.h"
using namespace std;
using irc::sockets::insp_aton;
using irc::sockets::insp_inaddr;
class InspIRCd;
+class Module;
/**
* Result status, used internally
*/
-typedef std::pair<int,std::string> DNSResult;
+class DNSResult : public classbase
+{
+ public:
+ int id;
+ std::string result;
+ unsigned long ttl;
+ std::string original;
+
+ DNSResult(int i, const std::string &res, unsigned long timetolive, const std::string &orig) : id(i), result(res), ttl(timetolive), original(orig) { }
+};
/**
* Information on a completed lookup, used internally
*/
typedef std::pair<unsigned char*, std::string> DNSInfo;
+/** Cached item
+ */
+class CachedQuery
+{
+ public:
+ std::string data;
+ time_t expires;
+
+ CachedQuery(const std::string &res, unsigned int ttl) : data(res)
+ {
+ expires = time(NULL) + ttl;
+ }
+
+ int CalcTTLRemaining()
+ {
+ int n = expires - time(NULL);
+ return (n < 0 ? 0 : n);
+ }
+};
+
+/** DNS cache information
+ */
+typedef nspace::hash_map<irc::string, CachedQuery, nspace::hash<irc::string> > dnscache;
+
/**
* Error types that class Resolver can emit to its error method.
*/
RESOLVER_NXDOMAIN = 2,
RESOLVER_NOTREADY = 3,
RESOLVER_BADIP = 4,
- RESOLVER_TIMEOUT = 5
+ RESOLVER_TIMEOUT = 5,
+ RESLOVER_FORCEUNLOAD = 6
};
/**
*/
class ResourceRecord;
-/**
- * A set of requests keyed by request id
- */
-typedef std::map<int,DNSRequest*> requestlist;
-
-/**
- * An iterator into a set of requests
- */
-typedef requestlist::iterator requestlist_iter;
-
/**
* Query and resource record types
*/
enum QueryType
{
+ DNS_QUERY_NONE = 0, /* Uninitialized Query */
DNS_QUERY_A = 1, /* 'A' record: an ipv4 address */
DNS_QUERY_CNAME = 5, /* 'CNAME' record: An alias */
DNS_QUERY_PTR = 12, /* 'PTR' record: a hostname */
* Pointer to creator
*/
InspIRCd* ServerInstance;
+ /**
+ * Pointer to creator module (if any, or NULL)
+ */
+ Module* Creator;
/**
* The input data, either a host or an IP address
*/
* The core uses this to route results to the correct objects.
*/
int myid;
+
+ /**
+ * Cached result, if there is one
+ */
+ CachedQuery *CQ;
+
+ /**
+ * Time left before cache expiry
+ */
+ int time_left;
public:
/**
* Initiate DNS lookup. Your class should not attempt to delete or free these
* To get around this automatic behaviour, you must use one of the values
* DNS_QUERY_PTR4 or DNS_QUERY_PTR6 to force ipv4 or ipv6 behaviour on the lookup,
* irrespective of what protocol InspIRCd has been built for.
+ * @param cached The constructor will set this boolean to true or false depending
+ * on whether the DNS lookup you are attempting is cached (and not expired) or not.
+ * If the value is cached, upon return this will be set to true, otherwise it will
+ * be set to false. You should pass this value to InspIRCd::AddResolver(), which
+ * will then influence the behaviour of the method and determine whether a cached
+ * or non-cached result is obtained. The value in this variable is always correct
+ * for the given request when the constructor exits.
+ * @param creator See the note below.
* @throw ModuleException This class may throw an instance of ModuleException, in the
* event a lookup could not be allocated, or a similar hard error occurs such as
* the network being down. This will also be thrown if an invalid IP address is
* passed when resolving a 'PTR' record.
+ *
+ * NOTE: If you are instantiating your DNS lookup from a module, you should set the
+ * value of creator to point at your Module class. This way if your module is unloaded
+ * whilst lookups are in progress, they can be safely removed and your module will not
+ * crash the server.
*/
- Resolver(InspIRCd* Instance, const std::string &source, QueryType qt);
+ Resolver(InspIRCd* Instance, const std::string &source, QueryType qt, bool &cached, Module* creator = NULL);
+
/**
* The default destructor does nothing.
*/
/**
* When your lookup completes, this method will be called.
* @param result The resulting DNS lookup, either an IP address or a hostname.
+ * @param ttl The time-to-live value of the result, in the instance of a cached
+ * result, this is the number of seconds remaining before refresh/expiry.
+ * @param cached True if the result is a cached result, false if it was requested
+ * from the DNS server.
*/
- virtual void OnLookupComplete(const std::string &result) = 0;
+ virtual void OnLookupComplete(const std::string &result, unsigned int ttl, bool cached) = 0;
/**
* If an error occurs (such as NXDOMAIN, no domain name found) then this method
* will be called.
* this method will return -1.
*/
int GetId();
+
+ /**
+ * Returns the creator module, or NULL
+ */
+ Module* GetCreator();
+
+ void TriggerCachedResult();
};
/** DNS is a singleton class used by the core to dispatch dns
*/
static const int MAX_REQUEST_ID = 0xFFFF;
- /**
- * Requests that are currently 'in flight'
- */
- requestlist requests;
-
/**
* Server address being used currently
*/
*/
bool ip6munge;
+ /**
+ * Currently cached items
+ */
+ dnscache* cache;
+
+ class CacheTimer* PruneTimer;
+
/**
* Build a dns packet payload
*/
* Currently active Resolver classes
*/
Resolver* Classes[MAX_REQUEST_ID];
+ /**
+ * Requests that are currently 'in flight'
+ */
+ DNSRequest* requests[MAX_REQUEST_ID];
/**
* The port number DNS requests are made on,
* and replies have as a source-port number.
* Handle a SocketEngine read event
* Inherited from EventHandler
*/
- void HandleEvent(EventType et);
+ void HandleEvent(EventType et, int errornum = 0);
/**
* Add a Resolver* to the list of active classes
/**
* Add a query to the list to be sent
*/
- DNSRequest* AddQuery(DNSHeader *header, int &id);
+ DNSRequest* AddQuery(DNSHeader *header, int &id, const char* original);
/**
* The constructor initialises the dns socket,
*/
DNS(InspIRCd* Instance);
+ /**
+ * Re-initialize the DNS subsystem.
+ */
+ void Rehash();
+
/**
* Destructor
*/
* Turn an in6_addr into a .ip6.arpa domain
*/
static void MakeIP6Int(char* query, const in6_addr *ip);
+
+ /**
+ * Clean out all dns resolvers owned by a particular
+ * module, to make unloading a module safe if there
+ * are dns requests currently in progress.
+ */
+ void CleanResolvers(Module* module);
+
+ CachedQuery* GetCache(const std::string &source);
+
+ void DelCache(const std::string &source);
+
+ int ClearCache();
+ int PruneCache();
};
#endif