-extern SocketEngine* SE;
-
-extern int statsAccept,statsRefused,statsUnknown,statsCollisions,statsDns,statsDnsGood,statsDnsBad,statsConnects,statsSent,statsRecv;
-
-#define max(a,b) (a > b ? a : b)
-#define DNS_MAX 8 /* max number of nameservers used */
-#define DNS_CONFIG_FBCK "/etc/resolv.conf" /* fallback config file */
-#define DNS_PORT 53 /* DNS well known port */
-#define DNS_QRY_A 1 /* name to IP address */
-#define DNS_QRY_AAAA 28 /* name to IP6 address */
-#define DNS_QRY_PTR 12 /* IP address to name */
-#define DNS_QRY_MX 15 /* name to MX */
-#define DNS_QRY_TXT 16 /* name to TXT */
-#define DNS_QRY_CNAME 5
-
-#define DNS_ALIGN (sizeof(void *) > sizeof(long) ? sizeof(void *) : sizeof(long))
-#define DNS_TRIES 3
-#define RESULTSIZE 1024
-#define min(a,b) (a < b ? a : b)
-
-static struct in_addr servers4[DNS_MAX]; /* up to DNS_MAX nameservers; populated by dns_init() */
-static int i4; /* actual count of nameservers; set by dns_init() */
-
-static int initdone = 0; /* to ensure dns_init() only runs once (on the first call) */
-static int wantclose = 0;
-static int lastcreate = -1;
-
-struct s_connection { /* open DNS query */
- struct s_connection *next; /* next in list */
- unsigned char id[2];
- unsigned int _class;
- unsigned int type;
- int want_list;
- int fd; /* file descriptor returned from sockets */
+extern InspIRCd* ServerInstance;
+extern ServerConfig* Config;
+extern time_t TIME;
+
+enum QueryType { DNS_QRY_A = 1, DNS_QRY_PTR = 12 };
+enum QueryFlags1 { FLAGS1_MASK_RD = 0x01, FLAGS1_MASK_TC = 0x02, FLAGS1_MASK_AA = 0x04, FLAGS1_MASK_OPCODE = 0x78, FLAGS1_MASK_QR = 0x80 };
+enum QueryFlags2 { FLAGS2_MASK_RCODE = 0x0F, FLAGS2_MASK_Z = 0x70, FLAGS2_MASK_RA = 0x80 };
+
+class s_connection;
+
+typedef std::map<int,s_connection*> connlist;
+typedef connlist::iterator connlist_iter;
+connlist connections;
+
+struct in_addr servers4[8];
+int i4;
+int initdone = 0;
+int wantclose = 0;
+int lastcreate = -1;
+
+class s_connection
+{
+ public:
+ unsigned char id[2];
+ unsigned int _class;
+ QueryType type;
+ int want_list;
+ int fd;