]> jfr.im git - solanum.git/blobdiff - src/res.c
move README to markdown.
[solanum.git] / src / res.c
index 3093b0f2b8492cd88c329125cbab901101cea831..be634e547316d81e813c0908baf8053c015556ee 100644 (file)
--- a/src/res.c
+++ b/src/res.c
@@ -15,7 +15,7 @@
  *     --Bleep (Thomas Helvey <tomh@inxpress.net>)
  *
  * This was all needlessly complicated for irc. Simplified. No more hostent
- * All we really care about is the IP -> hostname mappings. Thats all. 
+ * All we really care about is the IP -> hostname mappings. Thats all.
  *
  * Apr 28, 2003 --cryogen and Dianora
  *
@@ -88,6 +88,8 @@ static int ns_failure_count[IRCD_MAXNS]; /* timeouts and invalid/failed replies
 
 static void rem_request(struct reslist *request);
 static struct reslist *make_request(struct DNSQuery *query);
+static void gethost_byname_type_fqdn(const char *name, struct DNSQuery *query,
+               int type);
 static void do_query_name(struct DNSQuery *query, const char *name, struct reslist *request, int);
 static void do_query_number(struct DNSQuery *query, const struct rb_sockaddr_storage *,
                            struct reslist *request);
@@ -166,7 +168,7 @@ static int res_ourserver(const struct rb_sockaddr_storage *inp)
 }
 
 /*
- * timeout_query_list - Remove queries from the list which have been 
+ * timeout_query_list - Remove queries from the list which have been
  * there too long without being resolved.
  */
 static time_t timeout_query_list(time_t now)
@@ -278,8 +280,8 @@ void add_local_domain(char *hname, size_t size)
 }
 
 /*
- * rem_request - remove a request from the list. 
- * This must also free any memory that has been allocated for 
+ * rem_request - remove a request from the list.
+ * This must also free any memory that has been allocated for
  * temporary storage of DNS results.
  */
 static void rem_request(struct reslist *request)
@@ -332,7 +334,7 @@ static struct reslist *make_request(struct DNSQuery *query)
 }
 
 /*
- * delete_resolver_queries - cleanup outstanding queries 
+ * delete_resolver_queries - cleanup outstanding queries
  * for which there no longer exist clients or conf lines.
  */
 void delete_resolver_queries(const struct DNSQuery *query)
@@ -375,7 +377,7 @@ static int retryfreq(int timeouts)
 /*
  * send_res_msg - sends msg to a nameserver.
  * This should reflect /etc/resolv.conf.
- * Returns number of nameserver successfully sent to 
+ * Returns number of nameserver successfully sent to
  * or -1 if no successful sends.
  */
 static int send_res_msg(const char *msg, int len, int rcount)
@@ -395,7 +397,7 @@ static int send_res_msg(const char *msg, int len, int rcount)
                if (ns_failure_count[ns] && retrycnt % retryfreq(ns_failure_count[ns]))
                        continue;
                if (sendto(rb_get_fd(res_fd), msg, len, 0,
-                    (struct sockaddr *)&(irc_nsaddr_list[ns]), 
+                    (struct sockaddr *)&(irc_nsaddr_list[ns]),
                                GET_SS_LEN(&irc_nsaddr_list[ns])) == len)
                        return ns;
        }
@@ -407,7 +409,7 @@ static int send_res_msg(const char *msg, int len, int rcount)
                if (!ns_failure_count[ns])
                        continue;
                if (sendto(rb_get_fd(res_fd), msg, len, 0,
-                    (struct sockaddr *)&(irc_nsaddr_list[ns]), 
+                    (struct sockaddr *)&(irc_nsaddr_list[ns]),
                                GET_SS_LEN(&irc_nsaddr_list[ns])) == len)
                        return ns;
        }
@@ -434,11 +436,24 @@ static struct reslist *find_id(int id)
        return (NULL);
 }
 
-/* 
- * gethost_byname_type - get host address from name
- *
+/*
+ * gethost_byname_type - get host address from name, adding domain if needed
  */
 void gethost_byname_type(const char *name, struct DNSQuery *query, int type)
+{
+       char fqdn[IRCD_RES_HOSTLEN + 1];
+       assert(name != 0);
+
+       rb_strlcpy(fqdn, name, sizeof fqdn);
+       add_local_domain(fqdn, IRCD_RES_HOSTLEN);
+       gethost_byname_type_fqdn(fqdn, query, type);
+}
+
+/*
+ * gethost_byname_type_fqdn - get host address from fqdn
+ */
+static void gethost_byname_type_fqdn(const char *name, struct DNSQuery *query,
+               int type)
 {
        assert(name != 0);
        do_query_name(query, name, NULL, type);
@@ -458,19 +473,13 @@ void gethost_byaddr(const struct rb_sockaddr_storage *addr, struct DNSQuery *que
 static void do_query_name(struct DNSQuery *query, const char *name, struct reslist *request,
                          int type)
 {
-       char host_name[IRCD_RES_HOSTLEN + 1];
-
-       rb_strlcpy(host_name, name, IRCD_RES_HOSTLEN + 1);
-       add_local_domain(host_name, IRCD_RES_HOSTLEN);
-
        if (request == NULL)
        {
                request = make_request(query);
-               request->name = (char *)rb_malloc(strlen(host_name) + 1);
-               strcpy(request->name, host_name);
+               request->name = rb_strdup(name);
        }
 
-       rb_strlcpy(request->queryname, host_name, sizeof(request->queryname));
+       rb_strlcpy(request->queryname, name, sizeof(request->queryname));
        request->type = type;
        query_name(request);
 }
@@ -674,8 +683,8 @@ static int proc_answer(struct reslist *request, HEADER * header, char *buf, char
                rd_length = irc_ns_get16(current);
                current += RDLENGTH_SIZE;
 
-               /* 
-                * Wait to set request->type until we verify this structure 
+               /*
+                * Wait to set request->type until we verify this structure
                 */
                switch (type)
                {
@@ -746,8 +755,8 @@ static int proc_answer(struct reslist *request, HEADER * header, char *buf, char
 static int res_read_single_reply(rb_fde_t *F, void *data)
 {
        char buf[sizeof(HEADER) + MAXPACKET]
-               /* Sparc and alpha need 16bit-alignment for accessing header->id 
-                * (which is uint16_t). Because of the header = (HEADER*) buf; 
+               /* Sparc and alpha need 16bit-alignment for accessing header->id
+                * (which is uint16_t). Because of the header = (HEADER*) buf;
                 * lateron, this is neeeded. --FaUl
                 */
 #if defined(__sparc__) || defined(__alpha__)
@@ -862,14 +871,14 @@ static int res_read_single_reply(rb_fde_t *F, void *data)
 
                        /*
                         * Lookup the 'authoritative' name that we were given for the
-                        * ip#. 
+                        * ip#.
                         */
 #ifdef RB_IPV6
                        if (request->addr.ss_family == AF_INET6)
-                               gethost_byname_type(request->name, request->query, T_AAAA);
+                               gethost_byname_type_fqdn(request->name, request->query, T_AAAA);
                        else
 #endif
-                               gethost_byname_type(request->name, request->query, T_A);
+                               gethost_byname_type_fqdn(request->name, request->query, T_A);
                        rem_request(request);
                }
                else