]> jfr.im git - solanum.git/blobdiff - ircd/dns.c
Mailmap and copyright update for Ariadne
[solanum.git] / ircd / dns.c
index c9d231524fe29efd617bdc31590fccdb95047bac..20ce14800e5d94850aaaef0a53a812aabef08518 100644 (file)
@@ -4,7 +4,7 @@
  *
  *  Copyright (C) 2005 Aaron Sethman <androsyn@ratbox.org>
  *  Copyright (C) 2005-2012 ircd-ratbox development team
- *  Copyright (C) 2016 William Pitcock <nenolod@dereferenced.org>
+ *  Copyright (C) 2016 Ariadne Conill <ariadne@dereferenced.org>
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
@@ -36,6 +36,7 @@
 #include "numeric.h"
 #include "msg.h"
 #include "hash.h"
+#include "s_assert.h"
 
 #define DNS_HOST_IPV4          ((char)'4')
 #define DNS_HOST_IPV6          ((char)'6')
@@ -58,16 +59,23 @@ struct dnsstatreq
 };
 
 /* These serve as a form of sparse array */
-static struct Dictionary *query_dict;
-static struct Dictionary *stat_dict;
+static rb_dictionary *query_dict;
+static rb_dictionary *stat_dict;
 
 rb_dlink_list nameservers;
 
 static uint32_t query_id = 0;
 static uint32_t stat_id = 0;
 
-#define ASSIGN_ID(id) (id++)
 
+static inline uint32_t
+assign_id(uint32_t *id)
+{
+       if(++(*id) == 0)
+               *id = 1;
+
+       return *id;
+}
 
 static void
 handle_dns_failure(uint32_t xid)
@@ -75,7 +83,7 @@ handle_dns_failure(uint32_t xid)
        struct dnsreq *req = rb_dictionary_retrieve(query_dict, RB_UINT_TO_POINTER(xid));
        s_assert(req);
 
-       if(req->callback == NULL)
+       if(req == NULL || req->callback == NULL)
                return;
 
        req->callback("FAILED", 0, 0, req->data);
@@ -89,7 +97,7 @@ handle_dns_stat_failure(uint32_t xid)
        struct dnsstatreq *req = rb_dictionary_retrieve(stat_dict, RB_UINT_TO_POINTER(xid));
        s_assert(req);
 
-       if(req->callback == NULL)
+       if(req == NULL || req->callback == NULL)
                return;
 
        req->callback(1, NULL, 2, req->data);
@@ -103,6 +111,10 @@ cancel_lookup(uint32_t xid)
 {
        struct dnsreq *req = rb_dictionary_retrieve(query_dict, RB_UINT_TO_POINTER(xid));
        s_assert(req);
+
+       if (req == NULL)
+               return;
+
        req->callback = NULL;
        req->data = NULL;
 }
@@ -112,6 +124,10 @@ cancel_dns_stats(uint32_t xid)
 {
        struct dnsstatreq *req = rb_dictionary_retrieve(stat_dict, RB_UINT_TO_POINTER(xid));
        s_assert(req);
+
+       if (req == NULL)
+               return;
+
        req->callback = NULL;
        req->data = NULL;
 }
@@ -122,7 +138,7 @@ lookup_hostname(const char *hostname, int aftype, DNSCB callback, void *data)
 {
        struct dnsreq *req = rb_malloc(sizeof(struct dnsreq));
        int aft;
-       uint32_t rid = ASSIGN_ID(query_id);
+       uint32_t rid = assign_id(&query_id);
 
        check_authd();
 
@@ -131,11 +147,9 @@ lookup_hostname(const char *hostname, int aftype, DNSCB callback, void *data)
        req->callback = callback;
        req->data = data;
 
-#ifdef RB_IPV6
        if(aftype == AF_INET6)
                aft = 6;
        else
-#endif
                aft = 4;
 
        submit_dns(rid, aft == 4 ? DNS_HOST_IPV4 : DNS_HOST_IPV6, hostname);
@@ -147,7 +161,7 @@ lookup_ip(const char *addr, int aftype, DNSCB callback, void *data)
 {
        struct dnsreq *req = rb_malloc(sizeof(struct dnsreq));
        int aft;
-       uint32_t rid = ASSIGN_ID(query_id);
+       uint32_t rid = assign_id(&query_id);
 
        check_authd();
 
@@ -156,22 +170,20 @@ lookup_ip(const char *addr, int aftype, DNSCB callback, void *data)
        req->callback = callback;
        req->data = data;
 
-#ifdef RB_IPV6
        if(aftype == AF_INET6)
                aft = 6;
        else
-#endif
                aft = 4;
 
        submit_dns(rid, aft == 4 ? DNS_REVERSE_IPV4 : DNS_REVERSE_IPV6, addr);
        return (rid);
 }
 
-uint32_t
+static uint32_t
 get_nameservers(DNSLISTCB callback, void *data)
 {
        struct dnsstatreq *req = rb_malloc(sizeof(struct dnsstatreq));
-       uint32_t qid = ASSIGN_ID(stat_id);
+       uint32_t qid = assign_id(&stat_id);
 
        check_authd();
 
@@ -210,11 +222,9 @@ dns_results_callback(const char *callid, const char *status, const char *type, c
                req->data = NULL;
                return;
        }
-#ifdef RB_IPV6
        if(aft == 6)
                aft = AF_INET6;
        else
-#endif
                aft = AF_INET;
 
        req->callback(results, st, aft, req->data);
@@ -238,6 +248,8 @@ dns_stats_results_callback(const char *callid, const char *status, int resc, con
        req = rb_dictionary_retrieve(stat_dict, RB_UINT_TO_POINTER(qid));
 
        s_assert(req);
+       if (req == NULL)
+               return;
 
        if(req->callback == NULL)
        {
@@ -285,7 +297,7 @@ stats_results_callback(int resc, const char *resv[], int status, void *data)
        }
        else
        {
-               const char *error = resc ? resv[resc] : "Unknown error";
+               const char *error = resc ? resv[resc - 1] : "Unknown error";
                iwarn("Error getting DNS servers: %s", error);
        }
 }
@@ -303,7 +315,7 @@ void
 reload_nameservers(void)
 {
        check_authd();
-       rb_helper_write(authd_helper, "H D");
+       rb_helper_write(authd_helper, "R D");
        (void)get_nameservers(stats_results_callback, NULL);
 }