rwrap: improve debug error messages
[obnox/cwrap/resolv_wrapper.git] / src / resolv_wrapper.c
index 0c079fe1fc444baefec2dea7e222bdc3a4bceb85..a2625610e23f54a75ea78b718fc1f5e7ddb25912 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Copyright (c) 2014      Andreas Schneider <asn@samba.org>
- * Copyright (c) 2014      Jakub Hrozek <jakub.hrozek@gmail.com>
+ * Copyright (c) 2014      Jakub Hrozek <jakub.hrozek@posteo.se>
  *
  * All rights reserved.
  *
 
 #include <errno.h>
 #include <arpa/inet.h>
+#ifdef HAVE_ARPA_NAMESER_H
+#include <arpa/nameser.h>
+#endif /* HAVE_ARPA_NAMESER_H */
 #include <netinet/in.h>
+#include <sys/socket.h>
 #include <sys/types.h>
 #include <stdarg.h>
 #include <stdlib.h>
 #define RWRAP_DEFAULT_FAKE_TTL 600
 #endif  /* RWRAP_DEFAULT_FAKE_TTL */
 
+#ifndef HAVE_NS_NAME_COMPRESS
+#define ns_name_compress dn_comp
+#endif
+
+#define ns_t_uri 256
+
 enum rwrap_dbglvl_e {
        RWRAP_LOG_ERROR = 0,
        RWRAP_LOG_WARN,
@@ -141,11 +151,15 @@ static void rwrap_log(enum rwrap_dbglvl_e dbglvl,
        }                                                       \
 } while(0);
 
+#define RWRAP_MAX_RECURSION 5
+
 /* Priority and weight can be omitted from the hosts file, but need to be part
  * of the output
  */
 #define DFL_SRV_PRIO   1
 #define DFL_SRV_WEIGHT 100
+#define DFL_URI_PRIO   1
+#define DFL_URI_WEIGHT 100
 
 struct rwrap_srv_rrdata {
        uint16_t port;
@@ -154,6 +168,12 @@ struct rwrap_srv_rrdata {
        char hostname[MAXDNAME];
 };
 
+struct rwrap_uri_rrdata {
+       uint16_t prio;
+       uint16_t weight;
+       char uri[MAXDNAME];
+};
+
 struct rwrap_soa_rrdata {
        uint32_t serial;
        uint32_t refresh;
@@ -169,6 +189,7 @@ struct rwrap_fake_rr {
                struct in_addr a_rec;
                struct in6_addr aaaa_rec;
                struct rwrap_srv_rrdata srv_rec;
+               struct rwrap_uri_rrdata uri_rec;
                struct rwrap_soa_rrdata soa_rec;
                char cname_rec[MAXDNAME];
        } rrdata;
@@ -221,6 +242,15 @@ static int rwrap_create_fake_aaaa_rr(const char *key,
        rr->type = ns_t_aaaa;
        return 0;
 }
+static int rwrap_create_fake_ns_rr(const char *key,
+                                  const char *value,
+                                  struct rwrap_fake_rr *rr)
+{
+       memcpy(rr->rrdata.srv_rec.hostname, value, strlen(value) + 1);
+       memcpy(rr->key, key, strlen(key) + 1);
+       rr->type = ns_t_ns;
+       return 0;
+}
 
 static int rwrap_create_fake_srv_rr(const char *key,
                                    const char *value,
@@ -261,6 +291,42 @@ static int rwrap_create_fake_srv_rr(const char *key,
        return 0;
 }
 
+static int rwrap_create_fake_uri_rr(const char *key,
+                                   const char *value,
+                                   struct rwrap_fake_rr *rr)
+{
+       char *str_prio;
+       char *str_weight;
+       const char *uri;
+
+       /* parse the value into priority, weight, and uri
+        * and check the validity */
+       uri = value;
+       NEXT_KEY(uri, str_prio);
+       NEXT_KEY(str_prio, str_weight);
+       if (uri == NULL) {
+               RWRAP_LOG(RWRAP_LOG_ERROR,
+                         "Malformed URI entry [%s]\n", value);
+               return -1;
+       }
+
+       if (str_prio) {
+               rr->rrdata.uri_rec.prio = atoi(str_prio);
+       } else {
+               rr->rrdata.uri_rec.prio = DFL_URI_PRIO;
+       }
+       if (str_weight) {
+               rr->rrdata.uri_rec.weight = atoi(str_weight);
+       } else {
+               rr->rrdata.uri_rec.weight = DFL_URI_WEIGHT;
+       }
+       memcpy(rr->rrdata.uri_rec.uri, uri, strlen(uri) + 1);
+
+       memcpy(rr->key, key, strlen(key) + 1);
+       rr->type = ns_t_uri;
+       return 0;
+}
+
 static int rwrap_create_fake_soa_rr(const char *key,
                                    const char *value,
                                    struct rwrap_fake_rr *rr)
@@ -317,14 +383,10 @@ static int rwrap_create_fake_cname_rr(const char *key,
 
 /* Prepares a fake header with a single response. Advances header_blob */
 static ssize_t rwrap_fake_header(uint8_t **header_blob, size_t remaining,
-                                size_t rdata_size)
+                                size_t ancount, size_t arcount)
 {
        uint8_t *hb;
        HEADER *h;
-       int answers;
-
-       /* If rdata_size is zero, the answer is empty */
-       answers = rdata_size > 0 ? 1 : 0;
 
        if (remaining < NS_HFIXEDSZ) {
                RWRAP_LOG(RWRAP_LOG_ERROR, "Buffer too small!\n");
@@ -338,10 +400,11 @@ static ssize_t rwrap_fake_header(uint8_t **header_blob, size_t remaining,
        h->id = res_randomid();         /* random query ID */
        h->qr = 1;                      /* response flag */
        h->rd = 1;                      /* recursion desired */
-       h->ra = 1;                      /* resursion available */
+       h->ra = 1;                      /* recursion available */
 
        h->qdcount = htons(1);          /* no. of questions */
-       h->ancount = htons(answers);    /* no. of answers */
+       h->ancount = htons(ancount);    /* no. of answers */
+       h->arcount = htons(arcount);    /* no. of add'tl records */
 
        hb += NS_HFIXEDSZ;              /* move past the header */
        *header_blob = hb;
@@ -416,91 +479,90 @@ static ssize_t rwrap_fake_rdata_common(uint16_t type,
        return written + 3 * sizeof(uint16_t) + sizeof(uint32_t) + rdata_size;
 }
 
-static ssize_t rwrap_fake_common(uint16_t type,
-                                const char *question,
-                                size_t rdata_size,
-                                uint8_t **answer_ptr,
-                                size_t anslen)
+static ssize_t rwrap_fake_a(struct rwrap_fake_rr *rr,
+                           uint8_t *answer_ptr,
+                           size_t anslen)
 {
-       uint8_t *a = *answer_ptr;
-       ssize_t written;
-       ssize_t total = 0;
-       size_t remaining;
-
-       remaining = anslen;
+       uint8_t *a = answer_ptr;
+       ssize_t resp_size;
 
-       written = rwrap_fake_header(&a, remaining, rdata_size);
-       if (written < 0) {
+       if (rr->type != ns_t_a) {
+               RWRAP_LOG(RWRAP_LOG_ERROR, "Wrong type!\n");
                return -1;
        }
-       total += written;
-       remaining -= written;
+       RWRAP_LOG(RWRAP_LOG_TRACE, "Adding A RR");
 
-       written = rwrap_fake_question(question, type, &a, remaining);
-       if (written < 0) {
+       resp_size = rwrap_fake_rdata_common(ns_t_a, sizeof(struct in_addr), rr->key,
+                                           anslen, &a);
+       if (resp_size < 0) {
                return -1;
        }
-       remaining -= written;
-       total += written;
 
-       /* rdata_size = 0 denotes an empty answer */
-       if (rdata_size > 0) {
-               written = rwrap_fake_rdata_common(type, rdata_size, question,
-                                               remaining, &a);
-               if (written < 0) {
-                       return -1;
-               }
-               total += written;
-       }
+       memcpy(a, &rr->rrdata.a_rec, sizeof(struct in_addr));
 
-       *answer_ptr = a;
-       return total;
+       return resp_size;
 }
 
-static ssize_t rwrap_fake_a(struct rwrap_fake_rr *rr,
-                           uint8_t *answer_ptr,
-                           size_t anslen)
+static ssize_t rwrap_fake_aaaa(struct rwrap_fake_rr *rr,
+                              uint8_t *answer,
+                              size_t anslen)
 {
-       uint8_t *a = answer_ptr;
+       uint8_t *a = answer;
        ssize_t resp_size;
 
-       if (rr == NULL || rr->type != ns_t_a) {
-               RWRAP_LOG(RWRAP_LOG_ERROR,
-                         "Malformed record, no or wrong value!\n");
+       if (rr->type != ns_t_aaaa) {
+               RWRAP_LOG(RWRAP_LOG_ERROR, "Wrong type!\n");
                return -1;
        }
+       RWRAP_LOG(RWRAP_LOG_TRACE, "Adding AAAA RR");
 
-       resp_size = rwrap_fake_common(ns_t_a, rr->key, sizeof(struct in_addr),
-                                     &a, anslen);
+       resp_size = rwrap_fake_rdata_common(ns_t_aaaa, sizeof(struct in6_addr),
+                                           rr->key, anslen, &a);
        if (resp_size < 0) {
                return -1;
        }
 
-       memcpy(a, &rr->rrdata.a_rec, sizeof(struct in_addr));
+       memcpy(a, &rr->rrdata.aaaa_rec, sizeof(struct in6_addr));
 
        return resp_size;
 }
 
-static ssize_t rwrap_fake_aaaa(struct rwrap_fake_rr *rr,
-                              uint8_t *answer,
-                              size_t anslen)
+static ssize_t rwrap_fake_ns(struct rwrap_fake_rr *rr,
+                            uint8_t *answer,
+                           size_t anslen)
 {
        uint8_t *a = answer;
-       ssize_t resp_size;
+       ssize_t resp_size = 0;
+       size_t rdata_size;
+       unsigned char hostname_compressed[MAXDNAME];
+       ssize_t compressed_len;
 
-       if (rr == NULL || rr->type != ns_t_aaaa) {
-               RWRAP_LOG(RWRAP_LOG_ERROR,
-                         "Malformed record, no or wrong value!\n");
+       if (rr->type != ns_t_ns) {
+               RWRAP_LOG(RWRAP_LOG_ERROR, "Wrong type!\n");
+               return -1;
+       }
+       RWRAP_LOG(RWRAP_LOG_TRACE, "Adding NS RR");
+
+       /* Prepare the data to write */
+       compressed_len = ns_name_compress(rr->rrdata.srv_rec.hostname,
+                                         hostname_compressed,
+                                         MAXDNAME,
+                                         NULL,
+                                         NULL);
+       if (compressed_len < 0) {
                return -1;
        }
 
-       resp_size = rwrap_fake_common(ns_t_aaaa, rr->key,
-                                     sizeof(struct in6_addr), &a, anslen);
+       /* Is this enough? */
+       rdata_size = compressed_len;
+
+       resp_size = rwrap_fake_rdata_common(ns_t_ns, rdata_size,
+                                           rr->key, anslen, &a);
        if (resp_size < 0) {
                return -1;
        }
 
-       memcpy(a, &rr->rrdata.aaaa_rec, sizeof(struct in6_addr));
+       memcpy(a, hostname_compressed, compressed_len);
 
        return resp_size;
 }
@@ -515,11 +577,11 @@ static ssize_t rwrap_fake_srv(struct rwrap_fake_rr *rr,
        unsigned char hostname_compressed[MAXDNAME];
        ssize_t compressed_len;
 
-       if (rr == NULL || rr->type != ns_t_srv) {
-               RWRAP_LOG(RWRAP_LOG_ERROR,
-                         "Malformed record, no or wrong value!\n");
+       if (rr->type != ns_t_srv) {
+               RWRAP_LOG(RWRAP_LOG_ERROR, "Wrong type!\n");
                return -1;
        }
+       RWRAP_LOG(RWRAP_LOG_TRACE, "Adding SRV RR");
        rdata_size = 3 * sizeof(uint16_t);
 
        /* Prepare the data to write */
@@ -531,7 +593,8 @@ static ssize_t rwrap_fake_srv(struct rwrap_fake_rr *rr,
        }
        rdata_size += compressed_len;
 
-       resp_size = rwrap_fake_common(ns_t_srv, rr->key, rdata_size, &a, anslen);
+       resp_size = rwrap_fake_rdata_common(ns_t_srv, rdata_size,
+                                           rr->key, anslen, &a);
        if (resp_size < 0) {
                return -1;
        }
@@ -544,6 +607,45 @@ static ssize_t rwrap_fake_srv(struct rwrap_fake_rr *rr,
        return resp_size;
 }
 
+static ssize_t rwrap_fake_uri(struct rwrap_fake_rr *rr,
+                             uint8_t *answer,
+                             size_t anslen)
+{
+       uint8_t *a = answer;
+       ssize_t resp_size;
+       size_t rdata_size;
+       unsigned char uri_compressed[MAXDNAME];
+       ssize_t compressed_len;
+
+       if (rr->type != ns_t_uri) {
+               RWRAP_LOG(RWRAP_LOG_ERROR, "Wrong type!\n");
+               return -1;
+       }
+       RWRAP_LOG(RWRAP_LOG_TRACE, "Adding URI RR");
+       rdata_size = 3 * sizeof(uint16_t);
+
+       /* Prepare the data to write */
+       compressed_len = ns_name_compress(rr->rrdata.uri_rec.uri,
+                                         uri_compressed, MAXDNAME,
+                                         NULL, NULL);
+       if (compressed_len < 0) {
+               return -1;
+       }
+       rdata_size += compressed_len;
+
+       resp_size = rwrap_fake_rdata_common(ns_t_uri, rdata_size,
+                                           rr->key, anslen, &a);
+       if (resp_size < 0) {
+               return -1;
+       }
+
+       NS_PUT16(rr->rrdata.uri_rec.prio, a);
+       NS_PUT16(rr->rrdata.uri_rec.weight, a);
+       memcpy(a, uri_compressed, compressed_len);
+
+       return resp_size;
+}
+
 static ssize_t rwrap_fake_soa(struct rwrap_fake_rr *rr,
                              uint8_t *answer,
                              size_t anslen)
@@ -556,11 +658,11 @@ static ssize_t rwrap_fake_soa(struct rwrap_fake_rr *rr,
        unsigned char mailbox_compressed[MAXDNAME];
        ssize_t compressed_mb_len;
 
-       if (rr == NULL || rr->type != ns_t_soa) {
-               RWRAP_LOG(RWRAP_LOG_ERROR,
-                         "Malformed record, no or wrong value!\n");
+       if (rr->type != ns_t_soa) {
+               RWRAP_LOG(RWRAP_LOG_ERROR, "Wrong type!\n");
                return -1;
        }
+       RWRAP_LOG(RWRAP_LOG_TRACE, "Adding SOA RR");
        rdata_size = 5 * sizeof(uint16_t);
 
        compressed_ns_len = ns_name_compress(rr->rrdata.soa_rec.nameserver,
@@ -579,7 +681,8 @@ static ssize_t rwrap_fake_soa(struct rwrap_fake_rr *rr,
        }
        rdata_size += compressed_mb_len;
 
-       resp_size = rwrap_fake_common(ns_t_soa, rr->key, rdata_size, &a, anslen);
+       resp_size = rwrap_fake_rdata_common(ns_t_soa, rdata_size,
+                                           rr->key, anslen, &a);
        if (resp_size < 0) {
                return -1;
        }
@@ -606,11 +709,11 @@ static ssize_t rwrap_fake_cname(struct rwrap_fake_rr *rr,
        unsigned char hostname_compressed[MAXDNAME];
        ssize_t rdata_size;
 
-       if (rr == NULL || rr->type != ns_t_cname) {
-               RWRAP_LOG(RWRAP_LOG_ERROR,
-                         "Malformed record, no or wrong value!\n");
+       if (rr->type != ns_t_cname) {
+               RWRAP_LOG(RWRAP_LOG_ERROR, "Wrong type!\n");
                return -1;
        }
+       RWRAP_LOG(RWRAP_LOG_TRACE, "Adding CNAME RR");
 
        /* Prepare the data to write */
        rdata_size = ns_name_compress(rr->rrdata.cname_rec,
@@ -620,8 +723,8 @@ static ssize_t rwrap_fake_cname(struct rwrap_fake_rr *rr,
                return -1;
        }
 
-       resp_size = rwrap_fake_common(ns_t_cname, rr->key, rdata_size,
-                                     &a, anslen);
+       resp_size = rwrap_fake_rdata_common(ns_t_cname, rdata_size,
+                                           rr->key, anslen, &a);
        if (resp_size < 0) {
                return -1;
        }
@@ -631,21 +734,6 @@ static ssize_t rwrap_fake_cname(struct rwrap_fake_rr *rr,
        return resp_size;
 }
 
-static ssize_t rwrap_fake_empty_query(const char *key,
-                                     uint16_t type,
-                                     uint8_t *answer,
-                                     size_t anslen)
-{
-       ssize_t resp_size;
-
-       resp_size = rwrap_fake_common(type, key, 0, &answer, anslen);
-       if (resp_size < 0) {
-               return -1;
-       }
-
-       return resp_size;
-}
-
 #define RESOLV_MATCH(line, name) \
        (strncmp(line, name, sizeof(name) - 1) == 0 && \
        (line[sizeof(name) - 1] == ' ' || \
@@ -657,7 +745,42 @@ static ssize_t rwrap_fake_empty_query(const char *key,
         (strcasecmp(key, query)) == 0)
 
 
-static int rwrap_get_record(const char *hostfile,
+static int rwrap_get_record(const char *hostfile, unsigned recursion,
+                           const char *query, int type,
+                           struct rwrap_fake_rr *rr);
+
+static int rwrap_srv_recurse(const char *hostfile, unsigned recursion,
+                            const char *query, struct rwrap_fake_rr *rr)
+{
+       int rc;
+
+       rc = rwrap_get_record(hostfile, recursion, query, ns_t_a, rr);
+       if (rc == 0) return 0;
+
+       rc = rwrap_get_record(hostfile, recursion, query, ns_t_aaaa, rr);
+       if (rc == ENOENT) rc = 0;
+
+       return rc;
+}
+
+static int rwrap_cname_recurse(const char *hostfile, unsigned recursion,
+                              const char *query, struct rwrap_fake_rr *rr)
+{
+       int rc;
+
+       rc = rwrap_get_record(hostfile, recursion, query, ns_t_a, rr);
+       if (rc == 0) return 0;
+
+       rc = rwrap_get_record(hostfile, recursion, query, ns_t_aaaa, rr);
+       if (rc == 0) return 0;
+
+       rc = rwrap_get_record(hostfile, recursion, query, ns_t_cname, rr);
+       if (rc == ENOENT) rc = 0;
+
+       return rc;
+}
+
+static int rwrap_get_record(const char *hostfile, unsigned recursion,
                            const char *query, int type,
                            struct rwrap_fake_rr *rr)
 {
@@ -665,10 +788,16 @@ static int rwrap_get_record(const char *hostfile,
        char buf[BUFSIZ];
        char *key = NULL;
        char *value = NULL;
-       int rc = 0;
+       int rc = ENOENT;
+
+       if (recursion >= RWRAP_MAX_RECURSION) {
+               RWRAP_LOG(RWRAP_LOG_ERROR, "Recursed too deep!\n");
+               return -1;
+       }
 
        RWRAP_LOG(RWRAP_LOG_TRACE,
-                 "Searching in fake hosts file %s\n", hostfile);
+                 "Searching in fake hosts file %s for %s:%d\n", hostfile,
+                 query, type);
 
        fp = fopen(hostfile, "r");
        if (fp == NULL) {
@@ -688,12 +817,6 @@ static int rwrap_get_record(const char *hostfile,
                NEXT_KEY(rec_type, key);
                NEXT_KEY(key, value);
 
-               q = value;
-               while(q[0] != '\n' && q[0] != '\0') {
-                       q++;
-               }
-               q[0] = '\0';
-
                if (key == NULL || value == NULL) {
                        RWRAP_LOG(RWRAP_LOG_WARN,
                                "Malformed line: not enough parts, use \"rec_type key data\n"
@@ -701,6 +824,12 @@ static int rwrap_get_record(const char *hostfile,
                        continue;
                }
 
+               q = value;
+               while(q[0] != '\n' && q[0] != '\0') {
+                       q++;
+               }
+               q[0] = '\0';
+
                if (TYPE_MATCH(type, ns_t_a, rec_type, "A", key, query)) {
                        rc = rwrap_create_fake_a_rr(key, value, rr);
                        break;
@@ -708,9 +837,22 @@ static int rwrap_get_record(const char *hostfile,
                                      rec_type, "AAAA", key, query)) {
                        rc = rwrap_create_fake_aaaa_rr(key, value, rr);
                        break;
+               } else if (TYPE_MATCH(type, ns_t_ns,
+                                     rec_type, "NS", key, query)) {
+                       rc = rwrap_create_fake_ns_rr(key, value, rr);
+                       break;
                } else if (TYPE_MATCH(type, ns_t_srv,
                                      rec_type, "SRV", key, query)) {
                        rc = rwrap_create_fake_srv_rr(key, value, rr);
+                       if (rc == 0) {
+                               rc = rwrap_srv_recurse(hostfile, recursion+1,
+                                               rr->rrdata.srv_rec.hostname,
+                                               rr + 1);
+                       }
+                       break;
+               } else if (TYPE_MATCH(type, ns_t_uri,
+                                     rec_type, "URI", key, query)) {
+                       rc = rwrap_create_fake_uri_rr(key, value, rr);
                        break;
                } else if (TYPE_MATCH(type, ns_t_soa,
                                      rec_type, "SOA", key, query)) {
@@ -719,11 +861,22 @@ static int rwrap_get_record(const char *hostfile,
                } else if (TYPE_MATCH(type, ns_t_cname,
                                      rec_type, "CNAME", key, query)) {
                        rc = rwrap_create_fake_cname_rr(key, value, rr);
+                       if (rc == 0) {
+                               rc = rwrap_cname_recurse(hostfile, recursion+1,
+                                                        value, rr + 1);
+                       }
+                       break;
+               } else if (TYPE_MATCH(type, ns_t_a, rec_type, "CNAME", key, query)) {
+                       rc = rwrap_create_fake_cname_rr(key, value, rr);
+                       if (rc == 0) {
+                               rc = rwrap_cname_recurse(hostfile, recursion+1,
+                                                        value, rr + 1);
+                       }
                        break;
                }
        }
 
-       if (rc == 0 && rr->type == ns_t_invalid) {
+       if (rc == ENOENT && recursion == 0 && key != NULL) {
                RWRAP_LOG(RWRAP_LOG_TRACE, "Record for [%s] not found\n", query);
                memcpy(rr->key, key, strlen(key) + 1);
        }
@@ -732,33 +885,120 @@ static int rwrap_get_record(const char *hostfile,
        return rc;
 }
 
-static ssize_t rwrap_fake_answer(struct rwrap_fake_rr *rrs,
-                                int type,
-                                uint8_t *answer,
-                                size_t anslen)
+static ssize_t rwrap_fake_empty(int type,
+                               const char *question,
+                               uint8_t *answer,
+                               size_t anslen)
+{
+       ssize_t resp_data;
+       size_t remaining = anslen;
+
+       resp_data = rwrap_fake_header(&answer, remaining, 0, 0);
+       if (resp_data < 0) {
+               return -1;
+       }
+       remaining -= resp_data;
+
+       resp_data += rwrap_fake_question(question, type, &answer, remaining);
+       if (resp_data < 0) {
+               return -1;
+       }
+       remaining -= resp_data;
+
+       resp_data += rwrap_fake_rdata_common(type, 0, question,
+                                           remaining, &answer);
+       if (resp_data < 0) {
+               return -1;
+       }
+
+       return resp_data;
+}
+
+static inline bool rwrap_known_type(int type)
+{
+       switch (type) {
+       case ns_t_a:
+       case ns_t_aaaa:
+       case ns_t_ns:
+       case ns_t_srv:
+       case ns_t_uri:
+       case ns_t_soa:
+       case ns_t_cname:
+               return true;
+       }
+
+       return false;
+}
+
+static int rwrap_ancount(struct rwrap_fake_rr *rrs, int qtype)
+{
+       int i;
+       int ancount = 0;
+
+       /* Include all RRs in the stack until the sought type
+        * in the answer section. This is the case i.e. when looking
+        * up an A record but the name points to a CNAME
+        */
+       for (i = 0; i < RWRAP_MAX_RECURSION; i++) {
+               ancount++;
+
+               if (rwrap_known_type(rrs[i].type) &&
+                   rrs[i].type == qtype) {
+                       break;
+               }
+       }
+
+       /* Return 0 records if the sought type wasn't in the stack */
+       return i < RWRAP_MAX_RECURSION ? ancount : 0;
+}
+
+static int rwrap_arcount(struct rwrap_fake_rr *rrs, int ancount)
+{
+       int i;
+       int arcount = 0;
+
+       /* start from index ancount */
+       for (i = ancount; i < RWRAP_MAX_RECURSION; i++) {
+               if (rwrap_known_type(rrs[i].type)) {
+                       arcount++;
+               }
+       }
 
+       return arcount;
+}
+
+static ssize_t rwrap_add_rr(struct rwrap_fake_rr *rr,
+                           uint8_t *answer,
+                           size_t anslen)
 {
        ssize_t resp_data;
 
-       switch (rrs->type) {
+       if (rr == NULL) {
+               RWRAP_LOG(RWRAP_LOG_ERROR, "Internal error!\n");
+               return -1;
+       }
+
+       switch (rr->type) {
        case ns_t_a:
-               resp_data = rwrap_fake_a(rrs, answer, anslen);
+               resp_data = rwrap_fake_a(rr, answer, anslen);
                break;
        case ns_t_aaaa:
-               resp_data = rwrap_fake_aaaa(rrs, answer, anslen);
+               resp_data = rwrap_fake_aaaa(rr, answer, anslen);
+               break;
+       case ns_t_ns:
+               resp_data = rwrap_fake_ns(rr, answer, anslen);
                break;
        case ns_t_srv:
-               resp_data = rwrap_fake_srv(rrs, answer, anslen);
+               resp_data = rwrap_fake_srv(rr, answer, anslen);
+               break;
+       case ns_t_uri:
+               resp_data = rwrap_fake_uri(rr, answer, anslen);
                break;
        case ns_t_soa:
-               resp_data = rwrap_fake_soa(rrs, answer, anslen);
+               resp_data = rwrap_fake_soa(rr, answer, anslen);
                break;
        case ns_t_cname:
-               resp_data = rwrap_fake_cname(rrs, answer, anslen);
-               break;
-       case ns_t_invalid:
-               resp_data = rwrap_fake_empty_query(rrs->key, type,
-                                                  answer, anslen);
+               resp_data = rwrap_fake_cname(rr, answer, anslen);
                break;
        default:
                return -1;
@@ -767,10 +1007,67 @@ static ssize_t rwrap_fake_answer(struct rwrap_fake_rr *rrs,
        return resp_data;
 }
 
+static ssize_t rwrap_fake_answer(struct rwrap_fake_rr *rrs,
+                                int type,
+                                uint8_t *answer,
+                                size_t anslen)
+
+{
+       ssize_t resp_data;
+       ssize_t rrlen;
+       size_t remaining = anslen;
+       int ancount;
+       int arcount;
+       int i;
+
+       ancount = rwrap_ancount(rrs, type);
+       arcount = rwrap_arcount(rrs, ancount);
+       RWRAP_LOG(RWRAP_LOG_TRACE,
+                 "Got %d answers and %d additional records\n", ancount, arcount);
+
+       resp_data = rwrap_fake_header(&answer, remaining, ancount, arcount);
+       if (resp_data < 0) {
+               return -1;
+       }
+       remaining -= resp_data;
+
+       resp_data += rwrap_fake_question(rrs->key, rrs->type, &answer, remaining);
+       if (resp_data < 0) {
+               return -1;
+       }
+       remaining -= resp_data;
+
+       /* answer */
+       for (i = 0; i < ancount; i++) {
+               rrlen = rwrap_add_rr(&rrs[i], answer, remaining);
+               if (rrlen < 0) {
+                       return -1;
+               }
+               remaining -= rrlen;
+               answer += rrlen;
+               resp_data += rrlen;
+       }
+
+       /* add authoritative NS here? */
+
+       /* additional records */
+       for (i = ancount; i < ancount + arcount; i++) {
+               rrlen = rwrap_add_rr(&rrs[i], answer, remaining);
+               if (rrlen < 0) {
+                       return -1;
+               }
+               remaining -= rrlen;
+               answer += rrlen;
+               resp_data += rrlen;
+       }
+
+       return resp_data;
+}
+
 /* Reads in a file in the following format:
  * TYPE RDATA
  *
- * Malformed entried are silently skipped.
+ * Malformed entries are silently skipped.
  * Allocates answer buffer of size anslen that has to be freed after use.
  */
 static int rwrap_res_fake_hosts(const char *hostfile,
@@ -782,7 +1079,7 @@ static int rwrap_res_fake_hosts(const char *hostfile,
        int rc = ENOENT;
        char *query_name = NULL;
        size_t qlen = strlen(query);
-       struct rwrap_fake_rr rr;
+       struct rwrap_fake_rr rrs[RWRAP_MAX_RECURSION];
        ssize_t resp_size;
 
        RWRAP_LOG(RWRAP_LOG_TRACE,
@@ -797,17 +1094,27 @@ static int rwrap_res_fake_hosts(const char *hostfile,
                return -1;
        }
 
-       rwrap_fake_rr_init(&rr, 1);
+       rwrap_fake_rr_init(rrs, RWRAP_MAX_RECURSION);
 
-       rc = rwrap_get_record(hostfile, query_name, type, &rr);
-       if (rc != 0) {
+       rc = rwrap_get_record(hostfile, 0, query_name, type, rrs);
+       switch (rc) {
+       case 0:
+               RWRAP_LOG(RWRAP_LOG_TRACE,
+                               "Found record for [%s]\n", query_name);
+               resp_size = rwrap_fake_answer(rrs, type, answer, anslen);
+               break;
+       case ENOENT:
+               RWRAP_LOG(RWRAP_LOG_TRACE,
+                               "No record for [%s]\n", query_name);
+               resp_size = rwrap_fake_empty(type, rrs->key, answer, anslen);
+               break;
+       default:
                RWRAP_LOG(RWRAP_LOG_ERROR,
                                "Error searching for [%s]\n", query_name);
                free(query_name);
                return -1;
        }
 
-       resp_size = rwrap_fake_answer(&rr, type, answer, anslen);
        switch (resp_size) {
        case -1:
                RWRAP_LOG(RWRAP_LOG_ERROR,
@@ -830,51 +1137,68 @@ static int rwrap_res_fake_hosts(const char *hostfile,
 
 #include <dlfcn.h>
 
-struct rwrap_libc_fns {
-       int (*libc_res_init)(void);
-       int (*libc___res_init)(void);
-       int (*libc_res_ninit)(struct __res_state *state);
-       int (*libc___res_ninit)(struct __res_state *state);
-       void (*libc_res_nclose)(struct __res_state *state);
-       void (*libc___res_nclose)(struct __res_state *state);
-       void (*libc_res_close)(void);
-       void (*libc___res_close)(void);
-       int (*libc_res_nquery)(struct __res_state *state,
-                              const char *dname,
-                              int class,
-                              int type,
-                              unsigned char *answer,
-                              int anslen);
-       int (*libc___res_nquery)(struct __res_state *state,
+typedef int (*__libc_res_ninit)(struct __res_state *state);
+typedef int (*__libc___res_ninit)(struct __res_state *state);
+typedef void (*__libc_res_nclose)(struct __res_state *state);
+typedef void (*__libc___res_nclose)(struct __res_state *state);
+typedef int (*__libc_res_nquery)(struct __res_state *state,
                                 const char *dname,
                                 int class,
                                 int type,
                                 unsigned char *answer,
                                 int anslen);
-       int (*libc_res_nsearch)(struct __res_state *state,
-                               const char *dname,
-                               int class,
-                               int type,
-                               unsigned char *answer,
-                               int anslen);
-       int (*libc___res_nsearch)(struct __res_state *state,
+typedef int (*__libc___res_nquery)(struct __res_state *state,
+                                  const char *dname,
+                                  int class,
+                                  int type,
+                                  unsigned char *answer,
+                                  int anslen);
+typedef int (*__libc_res_nsearch)(struct __res_state *state,
                                  const char *dname,
                                  int class,
                                  int type,
                                  unsigned char *answer,
                                  int anslen);
+typedef int (*__libc___res_nsearch)(struct __res_state *state,
+                                   const char *dname,
+                                   int class,
+                                   int type,
+                                   unsigned char *answer,
+                                   int anslen);
+
+#define RWRAP_SYMBOL_ENTRY(i) \
+       union { \
+               __libc_##i f; \
+               void *obj; \
+       } _libc_##i
+
+struct rwrap_libc_symbols {
+       RWRAP_SYMBOL_ENTRY(res_ninit);
+       RWRAP_SYMBOL_ENTRY(__res_ninit);
+       RWRAP_SYMBOL_ENTRY(res_nclose);
+       RWRAP_SYMBOL_ENTRY(__res_nclose);
+       RWRAP_SYMBOL_ENTRY(res_nquery);
+       RWRAP_SYMBOL_ENTRY(__res_nquery);
+       RWRAP_SYMBOL_ENTRY(res_nsearch);
+       RWRAP_SYMBOL_ENTRY(__res_nsearch);
 };
+#undef RWRAP_SYMBOL_ENTRY
 
 struct rwrap {
-       void *libc_handle;
-       void *libresolv_handle;
+       struct {
+               void *handle;
+               struct rwrap_libc_symbols symbols;
+       } libc;
+
+       struct {
+               void *handle;
+               struct rwrap_libc_symbols symbols;
+       } libresolv;
 
        bool initialised;
        bool enabled;
 
        char *socket_dir;
-
-       struct rwrap_libc_fns fns;
 };
 
 static struct rwrap rwrap;
@@ -912,7 +1236,7 @@ static void *rwrap_load_lib_handle(enum rwrap_lib lib)
        switch (lib) {
        case RWRAP_LIBRESOLV:
 #ifdef HAVE_LIBRESOLV
-               handle = rwrap.libresolv_handle;
+               handle = rwrap.libresolv.handle;
                if (handle == NULL) {
                        for (i = 10; i >= 0; i--) {
                                char soname[256] = {0};
@@ -924,18 +1248,18 @@ static void *rwrap_load_lib_handle(enum rwrap_lib lib)
                                }
                        }
 
-                       rwrap.libresolv_handle = handle;
+                       rwrap.libresolv.handle = handle;
                }
                break;
 #endif
                /* FALL TROUGH */
        case RWRAP_LIBC:
-               handle = rwrap.libc_handle;
+               handle = rwrap.libc.handle;
 #ifdef LIBC_SO
                if (handle == NULL) {
                        handle = dlopen(LIBC_SO, flags);
 
-                       rwrap.libc_handle = handle;
+                       rwrap.libc.handle = handle;
                }
 #endif
                if (handle == NULL) {
@@ -949,14 +1273,14 @@ static void *rwrap_load_lib_handle(enum rwrap_lib lib)
                                }
                        }
 
-                       rwrap.libc_handle = handle;
+                       rwrap.libc.handle = handle;
                }
                break;
        }
 
        if (handle == NULL) {
 #ifdef RTLD_NEXT
-               handle = rwrap.libc_handle = rwrap.libresolv_handle = RTLD_NEXT;
+               handle = rwrap.libc.handle = rwrap.libresolv.handle = RTLD_NEXT;
 #else
                RWRAP_LOG(RWRAP_LOG_ERROR,
                          "Failed to dlopen library: %s\n",
@@ -968,7 +1292,7 @@ static void *rwrap_load_lib_handle(enum rwrap_lib lib)
        return handle;
 }
 
-static void *_rwrap_load_lib_function(enum rwrap_lib lib, const char *fn_name)
+static void *_rwrap_bind_symbol(enum rwrap_lib lib, const char *fn_name)
 {
        void *handle;
        void *func;
@@ -989,10 +1313,16 @@ static void *_rwrap_load_lib_function(enum rwrap_lib lib, const char *fn_name)
        return func;
 }
 
-#define rwrap_load_lib_function(lib, fn_name) \
-       if (rwrap.fns.libc_##fn_name == NULL) { \
-               *(void **) (&rwrap.fns.libc_##fn_name) = \
-                       _rwrap_load_lib_function(lib, #fn_name); \
+#define rwrap_bind_symbol_libc(sym_name) \
+       if (rwrap.libc.symbols._libc_##sym_name.obj == NULL) { \
+               rwrap.libc.symbols._libc_##sym_name.obj = \
+                       _rwrap_bind_symbol(RWRAP_LIBC, #sym_name); \
+       }
+
+#define rwrap_bind_symbol_libresolv(sym_name) \
+       if (rwrap.libresolv.symbols._libc_##sym_name.obj == NULL) { \
+               rwrap.libresolv.symbols._libc_##sym_name.obj = \
+                       _rwrap_bind_symbol(RWRAP_LIBRESOLV, #sym_name); \
        }
 
 /*
@@ -1003,36 +1333,25 @@ static void *_rwrap_load_lib_function(enum rwrap_lib lib, const char *fn_name)
  * has probably something todo with with the linker.
  * So we need load each function at the point it is called the first time.
  */
-#if 0
-static int libc_res_init(void)
-{
-#if defined(HAVE_RES_INIT)
-       rwrap_load_lib_function(RWRAP_LIBRESOLV, res_init);
-
-       return rwrap.fns.libc_res_init();
-#elif defined(HAVE___RES_INIT)
-       rwrap_load_lib_function(RWRAP_LIBRESOLV, __res_init);
-
-       return rwrap.fns.libc___res_init();
-#endif
-}
-#endif
 
 static int libc_res_ninit(struct __res_state *state)
 {
-#if defined(HAVE_RES_NINIT)
+#if !defined(res_ninit) && defined(HAVE_RES_NINIT)
 
 #if defined(HAVE_RES_NINIT_IN_LIBRESOLV)
-       rwrap_load_lib_function(RWRAP_LIBRESOLV, res_ninit);
+       rwrap_bind_symbol_libresolv(res_ninit);
+
+       return rwrap.libresolv.symbols._libc_res_ninit.f(state);
 #else /* HAVE_RES_NINIT_IN_LIBRESOLV */
-       rwrap_load_lib_function(RWRAP_LIBC, res_ninit);
+       rwrap_bind_symbol_libc(res_ninit);
+
+       return rwrap.libc.symbols._libc_res_ninit.f(state);
 #endif /* HAVE_RES_NINIT_IN_LIBRESOLV */
 
-       return rwrap.fns.libc_res_ninit(state);
 #elif defined(HAVE___RES_NINIT)
-       rwrap_load_lib_function(RWRAP_LIBC, __res_ninit);
+       rwrap_bind_symbol_libc(__res_ninit);
 
-       return rwrap.fns.libc___res_ninit(state);
+       return rwrap.libc.symbols._libc___res_ninit.f(state);
 #else
 #error "No res_ninit function"
 #endif
@@ -1040,19 +1359,24 @@ static int libc_res_ninit(struct __res_state *state)
 
 static void libc_res_nclose(struct __res_state *state)
 {
-#if defined(HAVE_RES_NCLOSE)
+#if !defined(res_close) && defined(HAVE_RES_NCLOSE)
 
 #if defined(HAVE_RES_NCLOSE_IN_LIBRESOLV)
-       rwrap_load_lib_function(RWRAP_LIBRESOLV, res_nclose);
+       rwrap_bind_symbol_libresolv(res_nclose);
+
+       rwrap.libresolv.symbols._libc_res_nclose.f(state);
+       return;
 #else /* HAVE_RES_NCLOSE_IN_LIBRESOLV */
-       rwrap_load_lib_function(RWRAP_LIBC, res_nclose);
+       rwrap_bind_symbol_libc(res_nclose);
+
+       rwrap.libc.symbols._libc_res_nclose.f(state);
+       return;
 #endif /* HAVE_RES_NCLOSE_IN_LIBRESOLV */
 
-       rwrap.fns.libc_res_nclose(state);
 #elif defined(HAVE___RES_NCLOSE)
-       rwrap_load_lib_function(RWRAP_LIBC, __res_nclose);
+       rwrap_bind_symbol_libc(__res_nclose);
 
-       rwrap.fns.libc___res_nclose(state);
+       rwrap.libc.symbols._libc___res_nclose.f(state);
 #else
 #error "No res_nclose function"
 #endif
@@ -1065,24 +1389,24 @@ static int libc_res_nquery(struct __res_state *state,
                           unsigned char *answer,
                           int anslen)
 {
-#if defined(HAVE_RES_NQUERY)
-       rwrap_load_lib_function(RWRAP_LIBRESOLV, res_nquery);
-
-       return rwrap.fns.libc_res_nquery(state,
-                                        dname,
-                                        class,
-                                        type,
-                                        answer,
-                                        anslen);
+#if !defined(res_nquery) && defined(HAVE_RES_NQUERY)
+       rwrap_bind_symbol_libresolv(res_nquery);
+
+       return rwrap.libresolv.symbols._libc_res_nquery.f(state,
+                                                         dname,
+                                                         class,
+                                                         type,
+                                                         answer,
+                                                         anslen);
 #elif defined(HAVE___RES_NQUERY)
-       rwrap_load_lib_function(RWRAP_LIBRESOLV, __res_nquery);
-
-       return rwrap.fns.libc___res_nquery(state,
-                                          dname,
-                                          class,
-                                          type,
-                                          answer,
-                                          anslen);
+       rwrap_bind_symbol_libresolv(__res_nquery);
+
+       return rwrap.libresolv.symbols._libc___res_nquery.f(state,
+                                                           dname,
+                                                           class,
+                                                           type,
+                                                           answer,
+                                                           anslen);
 #else
 #error "No res_nquery function"
 #endif
@@ -1095,24 +1419,24 @@ static int libc_res_nsearch(struct __res_state *state,
                            unsigned char *answer,
                            int anslen)
 {
-#if defined(HAVE_RES_NSEARCH)
-       rwrap_load_lib_function(RWRAP_LIBRESOLV, res_nsearch);
-
-       return rwrap.fns.libc_res_nsearch(state,
-                                         dname,
-                                         class,
-                                         type,
-                                         answer,
-                                         anslen);
+#if !defined(res_nsearch) && defined(HAVE_RES_NSEARCH)
+       rwrap_bind_symbol_libresolv(res_nsearch);
+
+       return rwrap.libresolv.symbols._libc_res_nsearch.f(state,
+                                                          dname,
+                                                          class,
+                                                          type,
+                                                          answer,
+                                                          anslen);
 #elif defined(HAVE___RES_NSEARCH)
-       rwrap_load_lib_function(RWRAP_LIBRESOLV, __res_nsearch);
-
-       return rwrap.fns.libc___res_nsearch(state,
-                                           dname,
-                                           class,
-                                           type,
-                                           answer,
-                                           anslen);
+       rwrap_bind_symbol_libresolv(__res_nsearch);
+
+       return rwrap.libresolv.symbols._libc___res_nsearch.f(state,
+                                                            dname,
+                                                            class,
+                                                            type,
+                                                            answer,
+                                                            anslen);
 #else
 #error "No res_nsearch function"
 #endif
@@ -1253,8 +1577,8 @@ static int rwrap_res_ninit(struct __res_state *state)
                        state->nscount = 0;
                        memset(state->nsaddr_list, 0, sizeof(state->nsaddr_list));
 
-                       state->_u._ext.nscount = 0;
 #ifdef HAVE_RESOLV_IPV6_NSADDRS
+                       state->_u._ext.nscount = 0;
                        for (i = 0; i < state->_u._ext.nscount; i++) {
                                SAFE_FREE(state->_u._ext.nsaddrs[i]);
                        }
@@ -1267,7 +1591,7 @@ static int rwrap_res_ninit(struct __res_state *state)
        return rc;
 }
 
-#if defined(HAVE_RES_NINIT)
+#if !defined(res_ninit) && defined(HAVE_RES_NINIT)
 int res_ninit(struct __res_state *state)
 #elif defined(HAVE___RES_NINIT)
 int __res_ninit(struct __res_state *state)
@@ -1291,7 +1615,7 @@ static int rwrap_res_init(void)
        return rc;
 }
 
-#if defined(HAVE_RES_INIT)
+#if !defined(res_ninit) && defined(HAVE_RES_INIT)
 int res_init(void)
 #elif defined(HAVE___RES_INIT)
 int __res_init(void)
@@ -1321,7 +1645,7 @@ static void rwrap_res_nclose(struct __res_state *state)
 #endif
 }
 
-#if defined(HAVE_RES_NCLOSE)
+#if !defined(res_nclose) && defined(HAVE_RES_NCLOSE)
 void res_nclose(struct __res_state *state)
 #elif defined(HAVE___RES_NCLOSE)
 void __res_nclose(struct __res_state *state)
@@ -1394,7 +1718,7 @@ static int rwrap_res_nquery(struct __res_state *state,
        return rc;
 }
 
-#if defined(HAVE_RES_NQUERY)
+#if !defined(res_nquery) && defined(HAVE_RES_NQUERY)
 int res_nquery(struct __res_state *state,
               const char *dname,
               int class,
@@ -1440,7 +1764,7 @@ static int rwrap_res_query(const char *dname,
        return rc;
 }
 
-#if defined(HAVE_RES_QUERY)
+#if !defined(res_query) && defined(HAVE_RES_QUERY)
 int res_query(const char *dname,
              int class,
              int type,
@@ -1502,7 +1826,7 @@ static int rwrap_res_nsearch(struct __res_state *state,
        return rc;
 }
 
-#if defined(HAVE_RES_NSEARCH)
+#if !defined(res_nsearch) && defined(HAVE_RES_NSEARCH)
 int res_nsearch(struct __res_state *state,
                const char *dname,
                int class,
@@ -1522,7 +1846,7 @@ int __res_nsearch(struct __res_state *state,
 }
 
 /****************************************************************************
- *   RES_QUERY
+ *   RES_SEARCH
  ***************************************************************************/
 
 static int rwrap_res_search(const char *dname,
@@ -1548,7 +1872,7 @@ static int rwrap_res_search(const char *dname,
        return rc;
 }
 
-#if defined(HAVE_RES_SEARCH)
+#if !defined(res_search) && defined(HAVE_RES_SEARCH)
 int res_search(const char *dname,
               int class,
               int type,