2 * Copyright (c) 2014-2018 Andreas Schneider <asn@samba.org>
3 * Copyright (c) 2014-2016 Jakub Hrozek <jakub.hrozek@posteo.se>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the author nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #include <arpa/inet.h>
39 #ifdef HAVE_ARPA_NAMESER_H
40 #include <arpa/nameser.h>
41 #endif /* HAVE_ARPA_NAMESER_H */
42 #include <netinet/in.h>
43 #include <sys/socket.h>
44 #include <sys/types.h>
55 #ifdef HAVE_RES_STATE_U_EXT_NSADDRS
56 #define HAVE_RESOLV_IPV6_NSADDRS 1
59 /* GCC has printf type attribute check. */
60 #ifdef HAVE_ATTRIBUTE_PRINTF_FORMAT
61 #define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
63 #define PRINTF_ATTRIBUTE(a,b)
64 #endif /* HAVE_ATTRIBUTE_PRINTF_FORMAT */
66 #ifdef HAVE_DESTRUCTOR_ATTRIBUTE
67 #define DESTRUCTOR_ATTRIBUTE __attribute__ ((destructor))
69 #define DESTRUCTOR_ATTRIBUTE
70 #endif /* HAVE_DESTRUCTOR_ATTRIBUTE */
72 #ifndef RWRAP_DEFAULT_FAKE_TTL
73 #define RWRAP_DEFAULT_FAKE_TTL 600
74 #endif /* RWRAP_DEFAULT_FAKE_TTL */
76 #ifndef HAVE_NS_NAME_COMPRESS
77 #define ns_name_compress dn_comp
90 #ifndef HAVE_GETPROGNAME
91 static const char *getprogname(void)
93 #if defined(HAVE_PROGRAM_INVOCATION_SHORT_NAME)
94 return program_invocation_short_name;
95 #elif defined(HAVE_GETEXECNAME)
99 #endif /* HAVE_PROGRAM_INVOCATION_SHORT_NAME */
101 #endif /* HAVE_GETPROGNAME */
103 static void rwrap_log(enum rwrap_dbglvl_e dbglvl, const char *func, const char *format, ...) PRINTF_ATTRIBUTE(3, 4);
104 # define RWRAP_LOG(dbglvl, ...) rwrap_log((dbglvl), __func__, __VA_ARGS__)
106 static void rwrap_log(enum rwrap_dbglvl_e dbglvl,
108 const char *format, ...)
113 unsigned int lvl = 0;
114 const char *prefix = NULL;
115 const char *progname = NULL;
117 d = getenv("RESOLV_WRAPPER_DEBUGLEVEL");
126 va_start(va, format);
127 vsnprintf(buffer, sizeof(buffer), format, va);
131 case RWRAP_LOG_ERROR:
132 prefix = "RWRAP_ERROR";
135 prefix = "RWRAP_WARN";
137 case RWRAP_LOG_NOTICE:
138 prefix = "RWRAP_NOTICE";
140 case RWRAP_LOG_DEBUG:
141 prefix = "RWRAP_DEBUG";
143 case RWRAP_LOG_TRACE:
144 prefix = "RWRAP_TRACE";
148 progname = getprogname();
149 if (progname == NULL) {
150 progname = "<unknown>";
154 "%s[%s (%u)] - %s: %s\n",
157 (unsigned int)getpid(),
163 #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); (x)=NULL;} } while(0)
166 #define NEXT_KEY(buf, key) do { \
167 (key) = (buf) ? strpbrk((buf), " \t") : NULL; \
168 if ((key) != NULL) { \
172 while ((key) != NULL \
173 && (isblank((int)(key)[0]))) { \
178 #define RWRAP_MAX_RECURSION 64
180 /* Priority and weight can be omitted from the hosts file, but need to be part
183 #define DFL_SRV_PRIO 1
184 #define DFL_SRV_WEIGHT 100
185 #define DFL_URI_PRIO 1
186 #define DFL_URI_WEIGHT 100
188 struct rwrap_srv_rrdata {
192 char hostname[MAXDNAME];
195 struct rwrap_uri_rrdata {
201 struct rwrap_soa_rrdata {
207 char nameserver[MAXDNAME];
208 char mailbox[MAXDNAME];
211 struct rwrap_fake_rr {
213 struct in_addr a_rec;
214 struct in6_addr aaaa_rec;
215 struct rwrap_srv_rrdata srv_rec;
216 struct rwrap_uri_rrdata uri_rec;
217 struct rwrap_soa_rrdata soa_rec;
218 char cname_rec[MAXDNAME];
219 char ptr_rec[MAXDNAME];
220 char txt_rec[MAXDNAME];
224 int type; /* ns_t_* */
227 static void rwrap_fake_rr_init(struct rwrap_fake_rr *rr, size_t len)
231 for (i = 0; i < len; i++) {
232 rr[i].type = ns_t_invalid;
236 static int rwrap_create_fake_a_rr(const char *key,
238 struct rwrap_fake_rr *rr)
242 ok = inet_pton(AF_INET, value, &rr->rrdata.a_rec);
244 RWRAP_LOG(RWRAP_LOG_ERROR,
245 "Failed to convert [%s] to binary\n", value);
249 memcpy(rr->key, key, strlen(key) + 1);
254 static int rwrap_create_fake_aaaa_rr(const char *key,
256 struct rwrap_fake_rr *rr)
260 ok = inet_pton(AF_INET6, value, &rr->rrdata.aaaa_rec);
262 RWRAP_LOG(RWRAP_LOG_ERROR,
263 "Failed to convert [%s] to binary\n", value);
267 memcpy(rr->key, key, strlen(key) + 1);
268 rr->type = ns_t_aaaa;
271 static int rwrap_create_fake_ns_rr(const char *key,
273 struct rwrap_fake_rr *rr)
275 memcpy(rr->rrdata.srv_rec.hostname, value, strlen(value) + 1);
276 memcpy(rr->key, key, strlen(key) + 1);
281 static int rwrap_create_fake_srv_rr(const char *key,
283 struct rwrap_fake_rr *rr)
288 const char *hostname;
290 /* parse the value into priority, weight, port and hostname
291 * and check the validity */
293 NEXT_KEY(hostname, str_port);
294 NEXT_KEY(str_port, str_prio);
295 NEXT_KEY(str_prio, str_weight);
296 if (str_port == NULL || hostname == NULL) {
297 RWRAP_LOG(RWRAP_LOG_ERROR,
298 "Malformed SRV entry [%s]\n", value);
303 rr->rrdata.srv_rec.prio = atoi(str_prio);
305 rr->rrdata.srv_rec.prio = DFL_SRV_PRIO;
308 rr->rrdata.srv_rec.weight = atoi(str_weight);
310 rr->rrdata.srv_rec.weight = DFL_SRV_WEIGHT;
312 rr->rrdata.srv_rec.port = atoi(str_port);
313 memcpy(rr->rrdata.srv_rec.hostname , hostname, strlen(hostname) + 1);
315 memcpy(rr->key, key, strlen(key) + 1);
320 static int rwrap_create_fake_uri_rr(const char *key,
322 struct rwrap_fake_rr *rr)
328 /* parse the value into priority, weight, and uri
329 * and check the validity */
331 NEXT_KEY(uri, str_prio);
332 NEXT_KEY(str_prio, str_weight);
334 RWRAP_LOG(RWRAP_LOG_ERROR,
335 "Malformed URI entry [<null>]\n");
340 rr->rrdata.uri_rec.prio = atoi(str_prio);
342 rr->rrdata.uri_rec.prio = DFL_URI_PRIO;
345 rr->rrdata.uri_rec.weight = atoi(str_weight);
347 rr->rrdata.uri_rec.weight = DFL_URI_WEIGHT;
349 memcpy(rr->rrdata.uri_rec.uri, uri, strlen(uri) + 1);
351 memcpy(rr->key, key, strlen(key) + 1);
356 static int rwrap_create_fake_txt_rr(const char *key,
358 struct rwrap_fake_rr *rr)
360 memcpy(rr->rrdata.txt_rec, value, strlen(value) + 1);
362 memcpy(rr->key, key, strlen(key) + 1);
367 static int rwrap_create_fake_soa_rr(const char *key,
369 struct rwrap_fake_rr *rr)
371 const char *nameserver;
379 /* parse the value into nameserver, mailbox, serial, refresh,
380 * retry, expire, minimum and check the validity
383 NEXT_KEY(nameserver, mailbox);
384 NEXT_KEY(mailbox, str_serial);
385 NEXT_KEY(str_serial, str_refresh);
386 NEXT_KEY(str_refresh, str_retry);
387 NEXT_KEY(str_retry, str_expire);
388 NEXT_KEY(str_expire, str_minimum);
389 if (nameserver == NULL || mailbox == NULL || str_serial == NULL ||
390 str_refresh == NULL || str_retry == NULL || str_expire == NULL ||
391 str_minimum == NULL) {
392 RWRAP_LOG(RWRAP_LOG_ERROR,
393 "Malformed SOA entry [%s]\n", value);
397 memcpy(rr->rrdata.soa_rec.nameserver, nameserver, strlen(nameserver)+1);
398 memcpy(rr->rrdata.soa_rec.mailbox, mailbox, strlen(mailbox)+1);
400 rr->rrdata.soa_rec.serial = atoi(str_serial);
401 rr->rrdata.soa_rec.refresh = atoi(str_refresh);
402 rr->rrdata.soa_rec.retry = atoi(str_retry);
403 rr->rrdata.soa_rec.expire = atoi(str_expire);
404 rr->rrdata.soa_rec.minimum = atoi(str_minimum);
406 memcpy(rr->key, key, strlen(key) + 1);
411 static int rwrap_create_fake_cname_rr(const char *key,
413 struct rwrap_fake_rr *rr)
415 memcpy(rr->rrdata.cname_rec , value, strlen(value) + 1);
416 memcpy(rr->key, key, strlen(key) + 1);
417 rr->type = ns_t_cname;
421 static int rwrap_create_fake_ptr_rr(const char *key,
423 struct rwrap_fake_rr *rr)
425 memcpy(rr->rrdata.ptr_rec , value, strlen(value) + 1);
426 memcpy(rr->key, key, strlen(key) + 1);
431 /* Prepares a fake header with a single response. Advances header_blob */
432 static ssize_t rwrap_fake_header(uint8_t **header_blob, size_t remaining,
433 size_t ancount, size_t arcount)
440 if (remaining < NS_HFIXEDSZ) {
441 RWRAP_LOG(RWRAP_LOG_ERROR, "Buffer too small!\n");
445 h.blob = *header_blob;
446 memset(h.blob, 0, NS_HFIXEDSZ);
448 h.header->id = res_randomid(); /* random query ID */
449 h.header->qr = 1; /* response flag */
450 h.header->rd = 1; /* recursion desired */
451 h.header->ra = 1; /* recursion available */
453 h.header->qdcount = htons(1); /* no. of questions */
454 h.header->ancount = htons(ancount); /* no. of answers */
455 h.header->arcount = htons(arcount); /* no. of add'tl records */
457 /* move past the header */
458 *header_blob = h.blob += NS_HFIXEDSZ;
463 static ssize_t rwrap_fake_question(const char *question,
465 uint8_t **question_ptr,
468 uint8_t *qb = *question_ptr;
471 n = ns_name_compress(question, qb, remaining, NULL, NULL);
473 RWRAP_LOG(RWRAP_LOG_ERROR,
474 "Failed to compress [%s]\n", question);
481 if (remaining < 2 * sizeof(uint16_t)) {
482 RWRAP_LOG(RWRAP_LOG_ERROR, "Buffer too small!\n");
487 NS_PUT16(ns_c_in, qb);
490 return n + 2 * sizeof(uint16_t);
493 static ssize_t rwrap_fake_rdata_common(uint16_t type,
499 uint8_t *rd = *rdata_ptr;
502 written = ns_name_compress(key, rd, remaining, NULL, NULL);
504 RWRAP_LOG(RWRAP_LOG_ERROR,
505 "Failed to compress [%s]\n", key);
509 remaining -= written;
511 if (remaining < 3 * sizeof(uint16_t) + sizeof(uint32_t)) {
512 RWRAP_LOG(RWRAP_LOG_ERROR, "Buffer too small\n");
517 NS_PUT16(ns_c_in, rd);
518 NS_PUT32(RWRAP_DEFAULT_FAKE_TTL, rd);
519 NS_PUT16(rdata_size, rd);
521 if (remaining < rdata_size) {
522 RWRAP_LOG(RWRAP_LOG_ERROR, "Buffer too small\n");
527 return written + 3 * sizeof(uint16_t) + sizeof(uint32_t) + rdata_size;
530 static ssize_t rwrap_fake_a(struct rwrap_fake_rr *rr,
534 uint8_t *a = answer_ptr;
537 if (rr->type != ns_t_a) {
538 RWRAP_LOG(RWRAP_LOG_ERROR, "Wrong type!\n");
541 RWRAP_LOG(RWRAP_LOG_TRACE, "Adding A RR");
543 resp_size = rwrap_fake_rdata_common(ns_t_a, sizeof(struct in_addr), rr->key,
549 memcpy(a, &rr->rrdata.a_rec, sizeof(struct in_addr));
554 static ssize_t rwrap_fake_aaaa(struct rwrap_fake_rr *rr,
561 if (rr->type != ns_t_aaaa) {
562 RWRAP_LOG(RWRAP_LOG_ERROR, "Wrong type!\n");
565 RWRAP_LOG(RWRAP_LOG_TRACE, "Adding AAAA RR");
567 resp_size = rwrap_fake_rdata_common(ns_t_aaaa, sizeof(struct in6_addr),
568 rr->key, anslen, &a);
573 memcpy(a, &rr->rrdata.aaaa_rec, sizeof(struct in6_addr));
578 static ssize_t rwrap_fake_ns(struct rwrap_fake_rr *rr,
583 ssize_t resp_size = 0;
585 unsigned char hostname_compressed[MAXDNAME];
586 ssize_t compressed_len;
588 if (rr->type != ns_t_ns) {
589 RWRAP_LOG(RWRAP_LOG_ERROR, "Wrong type!\n");
592 RWRAP_LOG(RWRAP_LOG_TRACE, "Adding NS RR");
594 /* Prepare the data to write */
595 compressed_len = ns_name_compress(rr->rrdata.srv_rec.hostname,
600 if (compressed_len < 0) {
604 /* Is this enough? */
605 rdata_size = compressed_len;
607 resp_size = rwrap_fake_rdata_common(ns_t_ns, rdata_size,
608 rr->key, anslen, &a);
613 memcpy(a, hostname_compressed, compressed_len);
618 static ssize_t rwrap_fake_srv(struct rwrap_fake_rr *rr,
625 unsigned char hostname_compressed[MAXDNAME];
626 ssize_t compressed_len;
628 if (rr->type != ns_t_srv) {
629 RWRAP_LOG(RWRAP_LOG_ERROR, "Wrong type!\n");
632 RWRAP_LOG(RWRAP_LOG_TRACE, "Adding SRV RR");
633 rdata_size = 3 * sizeof(uint16_t);
635 /* Prepare the data to write */
636 compressed_len = ns_name_compress(rr->rrdata.srv_rec.hostname,
637 hostname_compressed, MAXDNAME,
639 if (compressed_len < 0) {
642 rdata_size += compressed_len;
644 resp_size = rwrap_fake_rdata_common(ns_t_srv, rdata_size,
645 rr->key, anslen, &a);
650 NS_PUT16(rr->rrdata.srv_rec.prio, a);
651 NS_PUT16(rr->rrdata.srv_rec.weight, a);
652 NS_PUT16(rr->rrdata.srv_rec.port, a);
653 memcpy(a, hostname_compressed, compressed_len);
658 static ssize_t rwrap_fake_uri(struct rwrap_fake_rr *rr,
667 if (rr->type != ns_t_uri) {
668 RWRAP_LOG(RWRAP_LOG_ERROR, "Wrong type!\n");
671 RWRAP_LOG(RWRAP_LOG_TRACE, "Adding URI RR");
672 rdata_size = 3 * sizeof(uint16_t);
673 uri_len = strlen(rr->rrdata.uri_rec.uri) + 1;
674 rdata_size += uri_len;
676 resp_size = rwrap_fake_rdata_common(ns_t_uri, rdata_size,
677 rr->key, anslen, &a);
682 NS_PUT16(rr->rrdata.uri_rec.prio, a);
683 NS_PUT16(rr->rrdata.uri_rec.weight, a);
684 memcpy(a, rr->rrdata.uri_rec.uri, uri_len);
689 static ssize_t rwrap_fake_txt(struct rwrap_fake_rr *rr,
698 if (rr->type != ns_t_txt) {
699 RWRAP_LOG(RWRAP_LOG_ERROR, "Wrong type!\n");
702 RWRAP_LOG(RWRAP_LOG_TRACE, "Adding TXT RR");
703 txt_len = strlen(rr->rrdata.txt_rec) + 1;
704 rdata_size = txt_len;
706 resp_size = rwrap_fake_rdata_common(ns_t_txt, rdata_size,
707 rr->key, anslen, &a);
712 memcpy(a, rr->rrdata.txt_rec, txt_len);
717 static ssize_t rwrap_fake_soa(struct rwrap_fake_rr *rr,
724 unsigned char nameser_compressed[MAXDNAME];
725 ssize_t compressed_ns_len;
726 unsigned char mailbox_compressed[MAXDNAME];
727 ssize_t compressed_mb_len;
729 if (rr->type != ns_t_soa) {
730 RWRAP_LOG(RWRAP_LOG_ERROR, "Wrong type!\n");
733 RWRAP_LOG(RWRAP_LOG_TRACE, "Adding SOA RR");
734 rdata_size = 5 * sizeof(uint16_t);
736 compressed_ns_len = ns_name_compress(rr->rrdata.soa_rec.nameserver,
738 MAXDNAME, NULL, NULL);
739 if (compressed_ns_len < 0) {
742 rdata_size += compressed_ns_len;
744 compressed_mb_len = ns_name_compress(rr->rrdata.soa_rec.mailbox,
746 MAXDNAME, NULL, NULL);
747 if (compressed_mb_len < 0) {
750 rdata_size += compressed_mb_len;
752 resp_size = rwrap_fake_rdata_common(ns_t_soa, rdata_size,
753 rr->key, anslen, &a);
758 memcpy(a, nameser_compressed, compressed_ns_len);
759 a += compressed_ns_len;
760 memcpy(a, mailbox_compressed, compressed_mb_len);
761 a += compressed_mb_len;
762 NS_PUT32(rr->rrdata.soa_rec.serial, a);
763 NS_PUT32(rr->rrdata.soa_rec.refresh, a);
764 NS_PUT32(rr->rrdata.soa_rec.retry, a);
765 NS_PUT32(rr->rrdata.soa_rec.expire, a);
766 NS_PUT32(rr->rrdata.soa_rec.minimum, a);
771 static ssize_t rwrap_fake_cname(struct rwrap_fake_rr *rr,
777 unsigned char hostname_compressed[MAXDNAME];
780 if (rr->type != ns_t_cname) {
781 RWRAP_LOG(RWRAP_LOG_ERROR, "Wrong type!\n");
784 RWRAP_LOG(RWRAP_LOG_TRACE, "Adding CNAME RR");
786 /* Prepare the data to write */
787 rdata_size = ns_name_compress(rr->rrdata.cname_rec,
788 hostname_compressed, MAXDNAME,
790 if (rdata_size < 0) {
794 resp_size = rwrap_fake_rdata_common(ns_t_cname, rdata_size,
795 rr->key, anslen, &a);
800 memcpy(a, hostname_compressed, rdata_size);
805 static ssize_t rwrap_fake_ptr(struct rwrap_fake_rr *rr,
812 unsigned char hostname_compressed[MAXDNAME];
814 if (rr->type != ns_t_ptr) {
815 RWRAP_LOG(RWRAP_LOG_ERROR, "Wrong type!\n");
818 RWRAP_LOG(RWRAP_LOG_TRACE, "Adding PTR RR");
820 /* Prepare the data to write */
821 rdata_size = ns_name_compress(rr->rrdata.ptr_rec,
822 hostname_compressed, MAXDNAME,
824 if (rdata_size < 0) {
828 resp_size = rwrap_fake_rdata_common(ns_t_ptr, rdata_size,
829 rr->key, anslen, &a);
834 memcpy(a, hostname_compressed, rdata_size);
839 #define RESOLV_MATCH(line, name) \
840 (strncmp(line, name, sizeof(name) - 1) == 0 && \
841 (line[sizeof(name) - 1] == ' ' || \
842 line[sizeof(name) - 1] == '\t'))
844 #define TYPE_MATCH(type, ns_type, rec_type, str_type, key, query) \
845 ((type) == (ns_type) && \
846 (strncmp((rec_type), (str_type), sizeof(str_type)) == 0) && \
847 (strcasecmp(key, query)) == 0)
850 static int rwrap_get_record(const char *hostfile, unsigned recursion,
851 const char *query, int type,
852 struct rwrap_fake_rr *rr);
854 static int rwrap_uri_recurse(const char *hostfile, unsigned recursion,
855 const char *query, struct rwrap_fake_rr *rr)
859 rc = rwrap_get_record(hostfile, recursion, query, ns_t_uri, rr);
867 static int rwrap_srv_recurse(const char *hostfile, unsigned recursion,
868 const char *query, struct rwrap_fake_rr *rr)
872 rc = rwrap_get_record(hostfile, recursion, query, ns_t_a, rr);
873 if (rc == 0) return 0;
875 rc = rwrap_get_record(hostfile, recursion, query, ns_t_aaaa, rr);
876 if (rc == ENOENT) rc = 0;
881 static int rwrap_cname_recurse(const char *hostfile, unsigned recursion,
882 const char *query, struct rwrap_fake_rr *rr)
886 rc = rwrap_get_record(hostfile, recursion, query, ns_t_a, rr);
887 if (rc == 0) return 0;
889 rc = rwrap_get_record(hostfile, recursion, query, ns_t_aaaa, rr);
890 if (rc == 0) return 0;
892 rc = rwrap_get_record(hostfile, recursion, query, ns_t_cname, rr);
893 if (rc == ENOENT) rc = 0;
898 static int rwrap_get_record(const char *hostfile, unsigned recursion,
899 const char *query, int type,
900 struct rwrap_fake_rr *rr)
907 unsigned num_uris = 0;
909 if (recursion >= RWRAP_MAX_RECURSION) {
910 RWRAP_LOG(RWRAP_LOG_ERROR, "Recursed too deep!\n");
914 RWRAP_LOG(RWRAP_LOG_TRACE,
915 "Searching in fake hosts file %s for %s:%d\n", hostfile,
918 fp = fopen(hostfile, "r");
920 RWRAP_LOG(RWRAP_LOG_WARN,
921 "Opening %s failed: %s",
922 hostfile, strerror(errno));
926 while (fgets(buf, sizeof(buf), fp) != NULL) {
933 NEXT_KEY(rec_type, key);
934 NEXT_KEY(key, value);
936 if (key == NULL || value == NULL) {
937 RWRAP_LOG(RWRAP_LOG_WARN,
938 "Malformed line: not enough parts, use \"rec_type key data\n"
939 "For example \"A cwrap.org 10.10.10.10\"");
944 while(q[0] != '\n' && q[0] != '\0') {
949 if (type == ns_t_uri && recursion > 0) {
950 /* Skip non-URI records. */
951 if (!TYPE_MATCH(type, ns_t_uri, rec_type, "URI", key, query)) {
954 /* Skip previous records based on the recurse depth. */
956 if (num_uris <= recursion) {
961 if (TYPE_MATCH(type, ns_t_a, rec_type, "A", key, query)) {
962 rc = rwrap_create_fake_a_rr(key, value, rr);
964 } else if (TYPE_MATCH(type, ns_t_aaaa,
965 rec_type, "AAAA", key, query)) {
966 rc = rwrap_create_fake_aaaa_rr(key, value, rr);
968 } else if (TYPE_MATCH(type, ns_t_ns,
969 rec_type, "NS", key, query)) {
970 rc = rwrap_create_fake_ns_rr(key, value, rr);
972 } else if (TYPE_MATCH(type, ns_t_srv,
973 rec_type, "SRV", key, query)) {
974 rc = rwrap_create_fake_srv_rr(key, value, rr);
976 rc = rwrap_srv_recurse(hostfile, recursion+1,
977 rr->rrdata.srv_rec.hostname,
981 } else if (TYPE_MATCH(type, ns_t_uri,
982 rec_type, "URI", key, query)) {
983 rc = rwrap_create_fake_uri_rr(key, value, rr);
985 /* Recurse to collect multiple URI answers under a single key. */
986 rc = rwrap_uri_recurse(hostfile, recursion + 1, key, rr + 1);
989 } else if (TYPE_MATCH(type, ns_t_soa,
990 rec_type, "SOA", key, query)) {
991 rc = rwrap_create_fake_soa_rr(key, value, rr);
993 } else if (TYPE_MATCH(type, ns_t_cname,
994 rec_type, "CNAME", key, query)) {
995 rc = rwrap_create_fake_cname_rr(key, value, rr);
997 rc = rwrap_cname_recurse(hostfile, recursion+1,
1001 } else if (TYPE_MATCH(type, ns_t_a, rec_type, "CNAME", key, query)) {
1002 rc = rwrap_create_fake_cname_rr(key, value, rr);
1004 rc = rwrap_cname_recurse(hostfile, recursion+1,
1008 } else if (TYPE_MATCH(type, ns_t_ptr,
1009 rec_type, "PTR", key, query)) {
1010 rc = rwrap_create_fake_ptr_rr(key, value, rr);
1013 else if (TYPE_MATCH(type, ns_t_txt,
1014 rec_type, "TXT", key, query)) {
1015 rc = rwrap_create_fake_txt_rr(key, value, rr);
1020 if (rc == ENOENT && recursion == 0 && key != NULL) {
1021 RWRAP_LOG(RWRAP_LOG_TRACE, "Record for [%s] not found\n", query);
1022 memcpy(rr->key, key, strlen(key) + 1);
1029 static ssize_t rwrap_fake_empty(int type,
1030 const char *question,
1035 size_t remaining = anslen;
1037 resp_data = rwrap_fake_header(&answer, remaining, 0, 0);
1038 if (resp_data < 0) {
1041 remaining -= resp_data;
1043 resp_data += rwrap_fake_question(question, type, &answer, remaining);
1044 if (resp_data < 0) {
1047 remaining -= resp_data;
1049 resp_data += rwrap_fake_rdata_common(type, 0, question,
1050 remaining, &answer);
1051 if (resp_data < 0) {
1058 static inline bool rwrap_known_type(int type)
1076 static int rwrap_ancount(struct rwrap_fake_rr *rrs, int qtype)
1081 /* For URI return the number of URIs. */
1082 if (qtype == ns_t_uri) {
1083 for (i = 0; i < RWRAP_MAX_RECURSION; i++) {
1084 if (rwrap_known_type(rrs[i].type) &&
1085 rrs[i].type == qtype) {
1092 /* Include all RRs in the stack until the sought type
1093 * in the answer section. This is the case i.e. when looking
1094 * up an A record but the name points to a CNAME
1096 for (i = 0; i < RWRAP_MAX_RECURSION; i++) {
1099 if (rwrap_known_type(rrs[i].type) &&
1100 rrs[i].type == qtype) {
1105 /* Return 0 records if the sought type wasn't in the stack */
1106 return i < RWRAP_MAX_RECURSION ? ancount : 0;
1109 static int rwrap_arcount(struct rwrap_fake_rr *rrs, int ancount)
1114 /* start from index ancount */
1115 for (i = ancount; i < RWRAP_MAX_RECURSION; i++) {
1116 if (rwrap_known_type(rrs[i].type)) {
1124 static ssize_t rwrap_add_rr(struct rwrap_fake_rr *rr,
1131 RWRAP_LOG(RWRAP_LOG_ERROR, "Internal error!\n");
1137 resp_data = rwrap_fake_a(rr, answer, anslen);
1140 resp_data = rwrap_fake_aaaa(rr, answer, anslen);
1143 resp_data = rwrap_fake_ns(rr, answer, anslen);
1146 resp_data = rwrap_fake_srv(rr, answer, anslen);
1149 resp_data = rwrap_fake_uri(rr, answer, anslen);
1152 resp_data = rwrap_fake_soa(rr, answer, anslen);
1155 resp_data = rwrap_fake_cname(rr, answer, anslen);
1158 resp_data = rwrap_fake_ptr(rr, answer, anslen);
1161 resp_data = rwrap_fake_txt(rr, answer, anslen);
1170 static ssize_t rwrap_fake_answer(struct rwrap_fake_rr *rrs,
1178 size_t remaining = anslen;
1183 ancount = rwrap_ancount(rrs, type);
1184 arcount = rwrap_arcount(rrs, ancount);
1185 RWRAP_LOG(RWRAP_LOG_TRACE,
1186 "Got %d answers and %d additional records\n", ancount, arcount);
1188 resp_data = rwrap_fake_header(&answer, remaining, ancount, arcount);
1189 if (resp_data < 0) {
1192 remaining -= resp_data;
1194 resp_data += rwrap_fake_question(rrs->key, rrs->type, &answer, remaining);
1195 if (resp_data < 0) {
1198 remaining -= resp_data;
1201 for (i = 0; i < ancount; i++) {
1202 rrlen = rwrap_add_rr(&rrs[i], answer, remaining);
1211 /* add authoritative NS here? */
1213 /* additional records */
1214 for (i = ancount; i < ancount + arcount; i++) {
1215 rrlen = rwrap_add_rr(&rrs[i], answer, remaining);
1227 /* Reads in a file in the following format:
1230 * Malformed entries are silently skipped.
1231 * Allocates answer buffer of size anslen that has to be freed after use.
1233 static int rwrap_res_fake_hosts(const char *hostfile,
1236 unsigned char *answer,
1240 char *query_name = NULL;
1241 size_t qlen = strlen(query);
1242 struct rwrap_fake_rr rrs[RWRAP_MAX_RECURSION];
1245 RWRAP_LOG(RWRAP_LOG_TRACE,
1246 "Searching in fake hosts file %s\n", hostfile);
1248 if (qlen > 0 && query[qlen-1] == '.') {
1252 query_name = strndup(query, qlen);
1253 if (query_name == NULL) {
1257 rwrap_fake_rr_init(rrs, RWRAP_MAX_RECURSION);
1259 rc = rwrap_get_record(hostfile, 0, query_name, type, rrs);
1262 RWRAP_LOG(RWRAP_LOG_TRACE,
1263 "Found record for [%s]\n", query_name);
1264 resp_size = rwrap_fake_answer(rrs, type, answer, anslen);
1267 RWRAP_LOG(RWRAP_LOG_TRACE,
1268 "No record for [%s]\n", query_name);
1269 resp_size = rwrap_fake_empty(type, rrs->key, answer, anslen);
1272 RWRAP_LOG(RWRAP_LOG_NOTICE,
1273 "Searching for [%s] did not return any results\n",
1279 switch (resp_size) {
1281 RWRAP_LOG(RWRAP_LOG_ERROR,
1282 "Error faking answer for [%s]\n", query_name);
1285 RWRAP_LOG(RWRAP_LOG_TRACE,
1286 "Successfully faked answer for [%s]\n",
1295 /*********************************************************
1296 * RWRAP LOADING LIBC FUNCTIONS
1297 *********************************************************/
1301 typedef int (*__libc_res_ninit)(struct __res_state *state);
1302 typedef int (*__libc___res_ninit)(struct __res_state *state);
1303 typedef void (*__libc_res_nclose)(struct __res_state *state);
1304 typedef void (*__libc___res_nclose)(struct __res_state *state);
1305 typedef int (*__libc_res_nquery)(struct __res_state *state,
1309 unsigned char *answer,
1311 typedef int (*__libc___res_nquery)(struct __res_state *state,
1315 unsigned char *answer,
1317 typedef int (*__libc_res_nsearch)(struct __res_state *state,
1321 unsigned char *answer,
1323 typedef int (*__libc___res_nsearch)(struct __res_state *state,
1327 unsigned char *answer,
1330 #define RWRAP_SYMBOL_ENTRY(i) \
1336 struct rwrap_libc_symbols {
1337 RWRAP_SYMBOL_ENTRY(res_ninit);
1338 RWRAP_SYMBOL_ENTRY(__res_ninit);
1339 RWRAP_SYMBOL_ENTRY(res_nclose);
1340 RWRAP_SYMBOL_ENTRY(__res_nclose);
1341 RWRAP_SYMBOL_ENTRY(res_nquery);
1342 RWRAP_SYMBOL_ENTRY(__res_nquery);
1343 RWRAP_SYMBOL_ENTRY(res_nsearch);
1344 RWRAP_SYMBOL_ENTRY(__res_nsearch);
1346 #undef RWRAP_SYMBOL_ENTRY
1351 struct rwrap_libc_symbols symbols;
1356 struct rwrap_libc_symbols symbols;
1365 static struct rwrap rwrap;
1372 static const char *rwrap_str_lib(enum rwrap_lib lib)
1377 case RWRAP_LIBRESOLV:
1381 /* Compiler would warn us about unhandled enum value if we get here */
1385 static void *rwrap_load_lib_handle(enum rwrap_lib lib)
1387 int flags = RTLD_LAZY;
1388 void *handle = NULL;
1391 #ifdef RTLD_DEEPBIND
1392 const char *env_preload = getenv("LD_PRELOAD");
1393 const char *env_deepbind = getenv("RESOLV_WRAPPER_DISABLE_DEEPBIND");
1394 bool enable_deepbind = true;
1396 /* Don't do a deepbind if we run with libasan */
1397 if (env_preload != NULL && strlen(env_preload) < 1024) {
1398 const char *p = strstr(env_preload, "libasan.so");
1400 enable_deepbind = false;
1404 if (env_deepbind != NULL && strlen(env_deepbind) >= 1) {
1405 enable_deepbind = false;
1408 if (enable_deepbind) {
1409 flags |= RTLD_DEEPBIND;
1414 case RWRAP_LIBRESOLV:
1415 #ifdef HAVE_LIBRESOLV
1416 handle = rwrap.libresolv.handle;
1417 if (handle == NULL) {
1418 for (i = 10; i >= 0; i--) {
1419 char soname[256] = {0};
1421 snprintf(soname, sizeof(soname), "libresolv.so.%d", i);
1422 handle = dlopen(soname, flags);
1423 if (handle != NULL) {
1428 rwrap.libresolv.handle = handle;
1434 handle = rwrap.libc.handle;
1436 if (handle == NULL) {
1437 handle = dlopen(LIBC_SO, flags);
1439 rwrap.libc.handle = handle;
1442 if (handle == NULL) {
1443 for (i = 10; i >= 0; i--) {
1444 char soname[256] = {0};
1446 snprintf(soname, sizeof(soname), "libc.so.%d", i);
1447 handle = dlopen(soname, flags);
1448 if (handle != NULL) {
1453 rwrap.libc.handle = handle;
1458 if (handle == NULL) {
1460 handle = rwrap.libc.handle = rwrap.libresolv.handle = RTLD_NEXT;
1462 RWRAP_LOG(RWRAP_LOG_ERROR,
1463 "Failed to dlopen library: %s\n",
1472 static void *_rwrap_bind_symbol(enum rwrap_lib lib, const char *fn_name)
1477 handle = rwrap_load_lib_handle(lib);
1479 func = dlsym(handle, fn_name);
1481 RWRAP_LOG(RWRAP_LOG_ERROR,
1482 "Failed to find %s: %s\n",
1483 fn_name, dlerror());
1487 RWRAP_LOG(RWRAP_LOG_TRACE,
1488 "Loaded %s from %s",
1489 fn_name, rwrap_str_lib(lib));
1493 #define rwrap_bind_symbol_libc(sym_name) \
1494 if (rwrap.libc.symbols._libc_##sym_name.obj == NULL) { \
1495 rwrap.libc.symbols._libc_##sym_name.obj = \
1496 _rwrap_bind_symbol(RWRAP_LIBC, #sym_name); \
1499 #define rwrap_bind_symbol_libresolv(sym_name) \
1500 if (rwrap.libresolv.symbols._libc_##sym_name.obj == NULL) { \
1501 rwrap.libresolv.symbols._libc_##sym_name.obj = \
1502 _rwrap_bind_symbol(RWRAP_LIBRESOLV, #sym_name); \
1508 * Functions especially from libc need to be loaded individually, you can't load
1509 * all at once or gdb will segfault at startup. The same applies to valgrind and
1510 * has probably something todo with with the linker.
1511 * So we need load each function at the point it is called the first time.
1514 static int libc_res_ninit(struct __res_state *state)
1516 #if !defined(res_ninit) && defined(HAVE_RES_NINIT)
1517 rwrap_bind_symbol_libresolv(res_ninit);
1519 return rwrap.libresolv.symbols._libc_res_ninit.f(state);
1520 #elif defined(HAVE___RES_NINIT)
1521 rwrap_bind_symbol_libresolv(__res_ninit);
1523 return rwrap.libresolv.symbols._libc___res_ninit.f(state);
1525 #error "No res_ninit function"
1529 static void libc_res_nclose(struct __res_state *state)
1531 #if !defined(res_close) && defined(HAVE_RES_NCLOSE)
1532 rwrap_bind_symbol_libresolv(res_nclose);
1534 rwrap.libresolv.symbols._libc_res_nclose.f(state);
1536 #elif defined(HAVE___RES_NCLOSE)
1537 rwrap_bind_symbol_libresolv(__res_nclose);
1539 rwrap.libresolv.symbols._libc___res_nclose.f(state);
1541 #error "No res_nclose function"
1545 static int libc_res_nquery(struct __res_state *state,
1549 unsigned char *answer,
1552 #if !defined(res_nquery) && defined(HAVE_RES_NQUERY)
1553 rwrap_bind_symbol_libresolv(res_nquery);
1555 return rwrap.libresolv.symbols._libc_res_nquery.f(state,
1561 #elif defined(HAVE___RES_NQUERY)
1562 rwrap_bind_symbol_libresolv(__res_nquery);
1564 return rwrap.libresolv.symbols._libc___res_nquery.f(state,
1571 #error "No res_nquery function"
1575 static int libc_res_nsearch(struct __res_state *state,
1579 unsigned char *answer,
1582 #if !defined(res_nsearch) && defined(HAVE_RES_NSEARCH)
1583 rwrap_bind_symbol_libresolv(res_nsearch);
1585 return rwrap.libresolv.symbols._libc_res_nsearch.f(state,
1591 #elif defined(HAVE___RES_NSEARCH)
1592 rwrap_bind_symbol_libresolv(__res_nsearch);
1594 return rwrap.libresolv.symbols._libc___res_nsearch.f(state,
1601 #error "No res_nsearch function"
1605 /****************************************************************************
1607 ***************************************************************************/
1609 static void rwrap_reset_nameservers(struct __res_state *state)
1611 #ifdef HAVE_RES_STATE_U_EXT_NSADDRS
1614 for (i = 0; i < (size_t)state->nscount; i++) {
1615 if (state->_u._ext.nssocks[i] != -1) {
1616 close(state->_u._ext.nssocks[i]);
1617 state->_u._ext.nssocks[i] = -1;
1619 SAFE_FREE(state->_u._ext.nsaddrs[i]);
1621 memset(&state->_u._ext, 0, sizeof(state->_u._ext));
1622 for (i = 0; i < MAXNS; i++) {
1623 state->_u._ext.nssocks[i] = -1;
1624 state->_u._ext.nsmap[i] = MAXNS + 1;
1626 state->ipv6_unavail = false;
1628 memset(state->nsaddr_list, 0, sizeof(state->nsaddr_list));
1632 static int rwrap_parse_resolv_conf(struct __res_state *state,
1633 const char *resolv_conf)
1639 rwrap_reset_nameservers(state);
1641 fp = fopen(resolv_conf, "r");
1643 RWRAP_LOG(RWRAP_LOG_ERROR,
1644 "Opening %s failed: %s",
1645 resolv_conf, strerror(errno));
1649 while(fgets(buf, sizeof(buf), fp) != NULL) {
1652 /* Ignore comments */
1653 if (buf[0] == '#' || buf[0] == ';') {
1657 if (RESOLV_MATCH(buf, "nameserver") && nserv < MAXNS) {
1662 p = buf + strlen("nameserver");
1664 /* Skip spaces and tabs */
1665 while(isblank((int)p[0])) {
1670 while(q[0] != '\n' && q[0] != '\0') {
1675 ok = inet_pton(AF_INET, p, &a);
1677 state->nsaddr_list[nserv] = (struct sockaddr_in) {
1678 .sin_family = AF_INET,
1680 .sin_port = htons(53),
1686 #ifdef HAVE_RESOLV_IPV6_NSADDRS
1689 ok = inet_pton(AF_INET6, p, &a6);
1691 struct sockaddr_in6 *sa6;
1693 sa6 = malloc(sizeof(*sa6));
1699 sa6->sin6_family = AF_INET6;
1700 sa6->sin6_port = htons(53);
1701 sa6->sin6_flowinfo = 0;
1702 sa6->sin6_addr = a6;
1704 state->_u._ext.nsaddrs[nserv] = sa6;
1705 state->_u._ext.nssocks[nserv] = -1;
1706 state->_u._ext.nsmap[nserv] = MAXNS + 1;
1708 state->_u._ext.nscount6++;
1711 RWRAP_LOG(RWRAP_LOG_ERROR,
1712 "Malformed DNS server");
1715 #else /* !HAVE_RESOLV_IPV6_NSADDRS */
1717 * BSD uses an opaque structure to store the
1718 * IPv6 addresses. So we can not simply store
1719 * these addresses the same way as above.
1721 RWRAP_LOG(RWRAP_LOG_WARN,
1722 "resolve_wrapper does not support "
1723 "IPv6 on this platform");
1728 } /* TODO: match other keywords */
1732 * note that state->_u._ext.nscount is left as 0,
1733 * this matches glibc and allows resolv wrapper
1734 * to work with most (maybe all) glibc versions.
1736 state->nscount = nserv;
1739 RWRAP_LOG(RWRAP_LOG_ERROR,
1740 "Reading from %s failed",
1750 /****************************************************************************
1752 ***************************************************************************/
1754 static int rwrap_res_ninit(struct __res_state *state)
1758 rc = libc_res_ninit(state);
1760 const char *resolv_conf = getenv("RESOLV_WRAPPER_CONF");
1762 if (resolv_conf != NULL) {
1763 rc = rwrap_parse_resolv_conf(state, resolv_conf);
1770 #if !defined(res_ninit) && defined(HAVE_RES_NINIT)
1771 int res_ninit(struct __res_state *state)
1772 #elif defined(HAVE___RES_NINIT)
1773 int __res_ninit(struct __res_state *state)
1776 return rwrap_res_ninit(state);
1779 /****************************************************************************
1781 ***************************************************************************/
1783 static struct __res_state rwrap_res_state;
1785 static int rwrap_res_init(void)
1789 rc = rwrap_res_ninit(&rwrap_res_state);
1794 #if !defined(res_ninit) && defined(HAVE_RES_INIT)
1796 #elif defined(HAVE___RES_INIT)
1797 int __res_init(void)
1800 return rwrap_res_init();
1803 /****************************************************************************
1805 ***************************************************************************/
1807 static void rwrap_res_nclose(struct __res_state *state)
1809 rwrap_reset_nameservers(state);
1810 libc_res_nclose(state);
1813 #if !defined(res_nclose) && defined(HAVE_RES_NCLOSE)
1814 void res_nclose(struct __res_state *state)
1815 #elif defined(HAVE___RES_NCLOSE)
1816 void __res_nclose(struct __res_state *state)
1819 rwrap_res_nclose(state);
1822 /****************************************************************************
1824 ***************************************************************************/
1826 static void rwrap_res_close(void)
1828 rwrap_res_nclose(&rwrap_res_state);
1831 #if defined(HAVE_RES_CLOSE)
1832 void res_close(void)
1833 #elif defined(HAVE___RES_CLOSE)
1834 void __res_close(void)
1840 /****************************************************************************
1842 ***************************************************************************/
1844 static int rwrap_res_nquery(struct __res_state *state,
1848 unsigned char *answer,
1852 const char *fake_hosts;
1857 RWRAP_LOG(RWRAP_LOG_TRACE,
1858 "Resolve the domain name [%s] - class=%d, type=%d",
1859 dname, class, type);
1861 for (i = 0; i < state->nscount; i++) {
1862 char ip[INET6_ADDRSTRLEN];
1864 inet_ntop(AF_INET, &state->nsaddr_list[i].sin_addr, ip, sizeof(ip));
1865 RWRAP_LOG(RWRAP_LOG_TRACE,
1871 fake_hosts = getenv("RESOLV_WRAPPER_HOSTS");
1872 if (fake_hosts != NULL) {
1873 rc = rwrap_res_fake_hosts(fake_hosts, dname, type, answer, anslen);
1875 rc = libc_res_nquery(state, dname, class, type, answer, anslen);
1879 RWRAP_LOG(RWRAP_LOG_TRACE,
1880 "The returned response length is: %d",
1886 #if !defined(res_nquery) && defined(HAVE_RES_NQUERY)
1887 int res_nquery(struct __res_state *state,
1891 unsigned char *answer,
1893 #elif defined(HAVE___RES_NQUERY)
1894 int __res_nquery(struct __res_state *state,
1898 unsigned char *answer,
1902 return rwrap_res_nquery(state, dname, class, type, answer, anslen);
1905 /****************************************************************************
1907 ***************************************************************************/
1909 static int rwrap_res_query(const char *dname,
1912 unsigned char *answer,
1917 rc = rwrap_res_ninit(&rwrap_res_state);
1922 rc = rwrap_res_nquery(&rwrap_res_state,
1932 #if !defined(res_query) && defined(HAVE_RES_QUERY)
1933 int res_query(const char *dname,
1936 unsigned char *answer,
1938 #elif defined(HAVE___RES_QUERY)
1939 int __res_query(const char *dname,
1942 unsigned char *answer,
1946 return rwrap_res_query(dname, class, type, answer, anslen);
1949 /****************************************************************************
1951 ***************************************************************************/
1953 static int rwrap_res_nsearch(struct __res_state *state,
1957 unsigned char *answer,
1961 const char *fake_hosts;
1966 RWRAP_LOG(RWRAP_LOG_TRACE,
1967 "Resolve the domain name [%s] - class=%d, type=%d",
1968 dname, class, type);
1970 for (i = 0; i < state->nscount; i++) {
1971 char ip[INET6_ADDRSTRLEN];
1973 inet_ntop(AF_INET, &state->nsaddr_list[i].sin_addr, ip, sizeof(ip));
1974 RWRAP_LOG(RWRAP_LOG_TRACE,
1980 fake_hosts = getenv("RESOLV_WRAPPER_HOSTS");
1981 if (fake_hosts != NULL) {
1982 rc = rwrap_res_fake_hosts(fake_hosts, dname, type, answer, anslen);
1984 rc = libc_res_nsearch(state, dname, class, type, answer, anslen);
1987 RWRAP_LOG(RWRAP_LOG_TRACE,
1988 "The returned response length is: %d",
1994 #if !defined(res_nsearch) && defined(HAVE_RES_NSEARCH)
1995 int res_nsearch(struct __res_state *state,
1999 unsigned char *answer,
2001 #elif defined(HAVE___RES_NSEARCH)
2002 int __res_nsearch(struct __res_state *state,
2006 unsigned char *answer,
2010 return rwrap_res_nsearch(state, dname, class, type, answer, anslen);
2013 /****************************************************************************
2015 ***************************************************************************/
2017 static int rwrap_res_search(const char *dname,
2020 unsigned char *answer,
2025 rc = rwrap_res_ninit(&rwrap_res_state);
2030 rc = rwrap_res_nsearch(&rwrap_res_state,
2040 #if !defined(res_search) && defined(HAVE_RES_SEARCH)
2041 int res_search(const char *dname,
2044 unsigned char *answer,
2046 #elif defined(HAVE___RES_SEARCH)
2047 int __res_search(const char *dname,
2050 unsigned char *answer,
2054 return rwrap_res_search(dname, class, type, answer, anslen);