+class CoreExport DNS : public EventHandler
+{
+ private:
+
+ /**
+ * The maximum value of a dns request id,
+ * 16 bits wide, 0xFFFF.
+ */
+ static const int MAX_REQUEST_ID = 0xFFFF;
+
+ /** Maximum number of entries in cache
+ */
+ static const unsigned int MAX_CACHE_SIZE = 1000;
+
+ /**
+ * Currently cached items
+ */
+ dnscache* cache;
+
+ /** A timer which ticks every hour to remove expired
+ * items from the DNS cache.
+ */
+ class CacheTimer* PruneTimer;
+
+ /**
+ * Build a dns packet payload
+ */
+ int MakePayload(const char* name, const QueryType rr, const unsigned short rr_class, unsigned char* payload);
+
+ public:
+
+ irc::sockets::sockaddrs myserver;
+
+ /**
+ * 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.
+ */
+ static const int QUERY_PORT = 53;
+
+ /**
+ * Fill an rr (resource record) with data from input
+ */
+ static void FillResourceRecord(ResourceRecord* rr, const unsigned char* input);
+
+ /**
+ * Fill a header with data from input limited by a length
+ */
+ static void FillHeader(DNSHeader *header, const unsigned char *input, const int length);
+
+ /**
+ * Empty out a header into a data stream ready for transmission "on the wire"
+ */
+ static void EmptyHeader(unsigned char *output, const DNSHeader *header, const int length);
+
+ /**
+ * Start the lookup of an ipv4 from a hostname
+ */
+ int GetIP(const char* name);
+
+ /**
+ * Start lookup of a hostname from an ip, but
+ * force a specific protocol to be used for the lookup
+ * for example to perform an ipv6 reverse lookup.
+ */
+ int GetNameForce(const char *ip, ForceProtocol fp);
+
+ /**
+ * Start lookup of an ipv6 from a hostname
+ */
+ int GetIP6(const char *name);
+
+ /**
+ * Start lookup of a CNAME from another hostname
+ */
+ int GetCName(const char* alias);
+
+ /**
+ * Fetch the result string (an ip or host)
+ * and/or an error message to go with it.
+ */
+ DNSResult GetResult();
+
+ /**
+ * Handle a SocketEngine read event
+ * Inherited from EventHandler
+ */
+ void HandleEvent(EventType et, int errornum = 0);
+
+ /**
+ * Add a Resolver* to the list of active classes
+ */
+ bool AddResolverClass(Resolver* r);
+
+ /**
+ * Add a query to the list to be sent
+ */
+ DNSRequest* AddQuery(DNSHeader *header, int &id, const char* original);
+
+ /**
+ * The constructor initialises the dns socket,
+ * and clears the request lists.
+ */
+ DNS();
+
+ /**
+ * Re-initialize the DNS subsystem.
+ */
+ void Rehash();
+
+ /**
+ * Destructor
+ */
+ ~DNS();
+
+ /**
+ * 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);
+
+ /** Return the cached value of an IP or hostname
+ * @param source An IP or hostname to find in the cache.
+ * @return A pointer to a CachedQuery if the item exists,
+ * otherwise NULL.
+ */
+ CachedQuery* GetCache(const std::string &source);
+
+ /** Delete a cached item from the DNS cache.
+ * @param source An IP or hostname to remove
+ */
+ void DelCache(const std::string &source);
+
+ /** Clear all items from the DNS cache immediately.
+ */
+ int ClearCache();
+
+ /** Prune the DNS cache, e.g. remove all expired
+ * items and rehash the cache buckets, but leave
+ * items in the hash which are still valid.
+ */
+ int PruneCache();
+};