2 * Copyright (c) 2014 Andreas Schneider <asn@samba.org>
3 * Copyright (c) 2014 Jakub Hrozek <jakub.hrozek@gmail.com>
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 #include <netinet/in.h>
40 #include <sys/types.h>
51 /* GCC has printf type attribute check. */
52 #ifdef HAVE_ATTRIBUTE_PRINTF_FORMAT
53 #define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
55 #define PRINTF_ATTRIBUTE(a,b)
56 #endif /* HAVE_ATTRIBUTE_PRINTF_FORMAT */
58 #ifdef HAVE_DESTRUCTOR_ATTRIBUTE
59 #define DESTRUCTOR_ATTRIBUTE __attribute__ ((destructor))
61 #define DESTRUCTOR_ATTRIBUTE
62 #endif /* HAVE_DESTRUCTOR_ATTRIBUTE */
64 #ifndef RWRAP_DEFAULT_FAKE_TTL
65 #define RWRAP_DEFAULT_FAKE_TTL 600
66 #endif /* RWRAP_DEFAULT_FAKE_TTL */
76 # define RWRAP_LOG(...)
79 static void rwrap_log(enum rwrap_dbglvl_e dbglvl, const char *func, const char *format, ...) PRINTF_ATTRIBUTE(3, 4);
80 # define RWRAP_LOG(dbglvl, ...) rwrap_log((dbglvl), __func__, __VA_ARGS__)
82 static void rwrap_log(enum rwrap_dbglvl_e dbglvl,
84 const char *format, ...)
92 d = getenv("RESOLV_WRAPPER_DEBUGLEVEL");
98 vsnprintf(buffer, sizeof(buffer), format, va);
103 case RWRAP_LOG_ERROR:
105 "RWRAP_ERROR(%d) - %s: %s\n",
110 "RWRAP_WARN(%d) - %s: %s\n",
113 case RWRAP_LOG_DEBUG:
115 "RWRAP_DEBUG(%d) - %s: %s\n",
118 case RWRAP_LOG_TRACE:
120 "RWRAP_TRACE(%d) - %s: %s\n",
126 #endif /* NDEBUG RWRAP_LOG */
129 #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); (x)=NULL;} } while(0)
132 #define NEXT_KEY(buf, key) do { \
133 (key) = (buf) ? strpbrk((buf), " \t") : NULL; \
134 if ((key) != NULL) { \
138 while ((key) != NULL \
139 && (isblank((int)(key)[0]))) { \
145 /* Prepares a fake header with a single response. Advances header_blob */
146 static ssize_t rwrap_fake_header(uint8_t **header_blob, size_t remaining,
153 /* If rdata_size is zero, the answer is empty */
154 answers = rdata_size > 0 ? 1 : 0;
156 if (remaining < NS_HFIXEDSZ) {
157 RWRAP_LOG(RWRAP_LOG_ERROR, "Buffer too small!\n");
162 memset(hb, 0, NS_HFIXEDSZ);
165 h->id = res_randomid(); /* random query ID */
166 h->qr = 1; /* response flag */
167 h->rd = 1; /* recursion desired */
168 h->ra = 1; /* resursion available */
170 h->qdcount = htons(1); /* no. of questions */
171 h->ancount = htons(answers); /* no. of answers */
173 hb += NS_HFIXEDSZ; /* move past the header */
179 static ssize_t rwrap_fake_question(const char *question,
181 uint8_t **question_ptr,
184 uint8_t *qb = *question_ptr;
187 n = ns_name_compress(question, qb, remaining, NULL, NULL);
189 RWRAP_LOG(RWRAP_LOG_ERROR,
190 "Failed to compress [%s]\n", question);
197 if (remaining < 2 * sizeof(uint16_t)) {
198 RWRAP_LOG(RWRAP_LOG_ERROR, "Buffer too small!\n");
203 NS_PUT16(ns_c_in, qb);
206 return n + 2 * sizeof(uint16_t);
209 static ssize_t rwrap_fake_rdata_common(uint16_t type,
215 uint8_t *rd = *rdata_ptr;
218 written = ns_name_compress(key, rd, remaining, NULL, NULL);
220 RWRAP_LOG(RWRAP_LOG_ERROR,
221 "Failed to compress [%s]\n", key);
225 remaining -= written;
227 if (remaining < 3 * sizeof(uint16_t) + sizeof(uint32_t)) {
228 RWRAP_LOG(RWRAP_LOG_ERROR, "Buffer too small\n");
233 NS_PUT16(ns_c_in, rd);
234 NS_PUT32(RWRAP_DEFAULT_FAKE_TTL, rd);
235 NS_PUT16(rdata_size, rd);
237 if (remaining < rdata_size) {
238 RWRAP_LOG(RWRAP_LOG_ERROR, "Buffer too small\n");
243 return written + 3 * sizeof(uint16_t) + sizeof(uint32_t) + rdata_size;
246 static ssize_t rwrap_fake_common(uint16_t type,
247 const char *question,
249 uint8_t **answer_ptr,
252 uint8_t *a = *answer_ptr;
259 written = rwrap_fake_header(&a, remaining, rdata_size);
264 remaining -= written;
266 written = rwrap_fake_question(question, type, &a, remaining);
270 remaining -= written;
273 /* rdata_size = 0 denotes an empty answer */
274 if (rdata_size > 0) {
275 written = rwrap_fake_rdata_common(type, rdata_size, question,
287 static ssize_t rwrap_fake_a(const char *key,
292 uint8_t *a = answer_ptr;
293 struct in_addr a_rec;
298 RWRAP_LOG(RWRAP_LOG_ERROR, "Malformed record, no value!\n");
302 resp_size = rwrap_fake_common(ns_t_a, key, sizeof(a_rec), &a, anslen);
307 ok = inet_pton(AF_INET, value, &a_rec);
309 RWRAP_LOG(RWRAP_LOG_ERROR,
310 "Failed to convert [%s] to binary\n", value);
313 memcpy(a, &a_rec, sizeof(struct in_addr));
318 static ssize_t rwrap_fake_aaaa(const char *key,
324 struct in6_addr aaaa_rec;
329 RWRAP_LOG(RWRAP_LOG_ERROR, "Malformed record, no value!\n");
333 resp_size = rwrap_fake_common(ns_t_aaaa, key, sizeof(aaaa_rec), &a, anslen);
338 ok = inet_pton(AF_INET6, value, &aaaa_rec);
340 RWRAP_LOG(RWRAP_LOG_ERROR,
341 "Failed to convert [%s] to binary\n", value);
344 memcpy(a, &aaaa_rec, sizeof(struct in6_addr));
350 * Priority and weight can be omitted from the hosts file, but need to be part
353 #define DFL_SRV_PRIO 1
354 #define DFL_SRV_WEIGHT 100
356 static ssize_t rwrap_fake_srv(const char *key,
367 const char *hostname;
368 unsigned char hostname_compressed[MAXDNAME];
369 ssize_t compressed_len;
372 * Parse the value into priority, weight, port and hostname
373 * and check the validity.
376 NEXT_KEY(hostname, str_port);
377 NEXT_KEY(str_port, str_prio);
378 NEXT_KEY(str_prio, str_weight);
379 if (str_port == NULL || hostname == NULL) {
380 RWRAP_LOG(RWRAP_LOG_ERROR,
381 "Malformed SRV entry [%s]\n", value);
384 rdata_size = 3 * sizeof(uint16_t);
386 /* Prepare the data to write */
387 compressed_len = ns_name_compress(hostname,
388 hostname_compressed, MAXDNAME,
390 if (compressed_len < 0) {
393 rdata_size += compressed_len;
395 resp_size = rwrap_fake_common(ns_t_srv, key, rdata_size, &a, anslen);
401 NS_PUT16(atoi(str_prio), a);
403 NS_PUT16(DFL_SRV_PRIO, a);
406 NS_PUT16(atoi(str_weight), a);
408 NS_PUT16(DFL_SRV_WEIGHT, a);
410 NS_PUT16(atoi(str_port), a);
411 memcpy(a, hostname_compressed, compressed_len);
416 static ssize_t rwrap_fake_soa(const char *key,
423 const char *nameserver;
431 unsigned char nameser_compressed[MAXDNAME];
432 ssize_t compressed_ns_len;
433 unsigned char mailbox_compressed[MAXDNAME];
434 ssize_t compressed_mb_len;
437 * parse the value into nameserver, mailbox, serial, refresh,
438 * retry, expire, minimum and check the validity
441 NEXT_KEY(nameserver, mailbox);
442 NEXT_KEY(mailbox, str_serial);
443 NEXT_KEY(str_serial, str_refresh);
444 NEXT_KEY(str_refresh, str_retry);
445 NEXT_KEY(str_retry, str_expire);
446 NEXT_KEY(str_expire, str_minimum);
447 if (nameserver == NULL || mailbox == NULL || str_serial == NULL ||
448 str_refresh == NULL || str_retry == NULL || str_expire == NULL ||
451 RWRAP_LOG(RWRAP_LOG_ERROR,
452 "Malformed SOA entry [%s]\n", value);
455 rdata_size = 5 * sizeof(uint16_t);
457 compressed_ns_len = ns_name_compress(nameserver, nameser_compressed,
458 MAXDNAME, NULL, NULL);
459 if (compressed_ns_len < 0) {
462 rdata_size += compressed_ns_len;
464 compressed_mb_len = ns_name_compress(mailbox, mailbox_compressed,
465 MAXDNAME, NULL, NULL);
466 if (compressed_mb_len < 0) {
469 rdata_size += compressed_mb_len;
471 resp_size = rwrap_fake_common(ns_t_soa, key, rdata_size, &a, anslen);
476 memcpy(a, nameser_compressed, compressed_ns_len);
477 a += compressed_ns_len;
478 memcpy(a, mailbox_compressed, compressed_mb_len);
479 a += compressed_mb_len;
480 NS_PUT32(atoi(str_serial), a);
481 NS_PUT32(atoi(str_refresh), a);
482 NS_PUT32(atoi(str_retry), a);
483 NS_PUT32(atoi(str_expire), a);
484 NS_PUT32(atoi(str_minimum), a);
489 static ssize_t rwrap_fake_cname(const char *key,
496 unsigned char hostname_compressed[MAXDNAME];
500 RWRAP_LOG(RWRAP_LOG_ERROR, "Malformed record, no value!\n");
504 /* Prepare the data to write */
505 rdata_size = ns_name_compress(value,
506 hostname_compressed, MAXDNAME,
508 if (rdata_size < 0) {
512 resp_size = rwrap_fake_common(ns_t_cname, key, rdata_size, &a, anslen);
517 memcpy(a, hostname_compressed, rdata_size);
522 static ssize_t rwrap_fake_empty_query(const char *key,
529 resp_size = rwrap_fake_common(type, key, 0, &answer, anslen);
537 #define RESOLV_MATCH(line, name) \
538 (strncmp(line, name, sizeof(name) - 1) == 0 && \
539 (line[sizeof(name) - 1] == ' ' || \
540 line[sizeof(name) - 1] == '\t'))
542 #define TYPE_MATCH(type, ns_type, rec_type, str_type, key, query) \
543 ((type) == (ns_type) && \
544 (strncmp((rec_type), (str_type), sizeof(str_type)) == 0) && \
545 (strcasecmp(key, query)) == 0)
548 /* Reads in a file in the following format:
551 * Malformed entried are silently skipped.
552 * Allocates answer buffer of size anslen that has to be freed after use.
554 static ssize_t rwrap_res_fake_hosts(const char *hostfile,
557 unsigned char *answer,
564 char *query_name = NULL;
565 size_t qlen = strlen(query);
566 ssize_t resp_size = 0;
568 RWRAP_LOG(RWRAP_LOG_TRACE,
569 "Searching in fake hosts file %s\n", hostfile);
571 fp = fopen(hostfile, "r");
573 RWRAP_LOG(RWRAP_LOG_ERROR,
574 "Opening %s failed: %s",
575 hostfile, strerror(errno));
579 if (qlen > 0 && query[qlen-1] == '.') {
583 query_name = strndup(query, qlen);
584 if (query_name == NULL) {
588 while (fgets(buf, sizeof(buf), fp) != NULL) {
595 NEXT_KEY(rec_type, key);
596 NEXT_KEY(key, value);
599 while(q[0] != '\n' && q[0] != '\0') {
604 if (key == NULL || value == NULL) {
605 RWRAP_LOG(RWRAP_LOG_WARN,
606 "Malformed line: not enough parts, use \"rec_type key data\n"
607 "For example \"A cwrap.org 10.10.10.10\"");
611 if (TYPE_MATCH(type, ns_t_a, rec_type, "A", key, query_name)) {
612 resp_size = rwrap_fake_a(key, value, answer, anslen);
614 } else if (TYPE_MATCH(type, ns_t_aaaa,
615 rec_type, "AAAA", key, query_name)) {
616 resp_size = rwrap_fake_aaaa(key, value, answer, anslen);
618 } else if (TYPE_MATCH(type, ns_t_srv,
619 rec_type, "SRV", key, query_name)) {
620 resp_size = rwrap_fake_srv(key, value, answer, anslen);
622 } else if (TYPE_MATCH(type, ns_t_soa,
623 rec_type, "SOA", key, query_name)) {
624 resp_size = rwrap_fake_soa(key, value, answer, anslen);
626 } else if (TYPE_MATCH(type, ns_t_cname,
627 rec_type, "CNAME", key, query_name)) {
628 resp_size = rwrap_fake_cname(key, value, answer, anslen);
635 RWRAP_LOG(RWRAP_LOG_TRACE,
636 "Record for [%s] not found\n", query_name);
637 resp_size = rwrap_fake_empty_query(key, type, answer, anslen);
640 RWRAP_LOG(RWRAP_LOG_ERROR,
641 "Error faking answer for [%s]\n", query_name);
644 RWRAP_LOG(RWRAP_LOG_TRACE,
645 "Successfully faked answer for [%s]\n", query_name);
654 /*********************************************************
655 * RWRAP LOADING LIBC FUNCTIONS
656 *********************************************************/
660 struct rwrap_libc_fns {
661 int (*libc_res_init)(void);
662 int (*libc___res_init)(void);
663 int (*libc_res_ninit)(struct __res_state *state);
664 int (*libc___res_ninit)(struct __res_state *state);
665 void (*libc_res_nclose)(struct __res_state *state);
666 void (*libc___res_nclose)(struct __res_state *state);
667 void (*libc_res_close)(void);
668 void (*libc___res_close)(void);
669 int (*libc_res_nquery)(struct __res_state *state,
673 unsigned char *answer,
675 int (*libc___res_nquery)(struct __res_state *state,
679 unsigned char *answer,
681 int (*libc_res_nsearch)(struct __res_state *state,
685 unsigned char *answer,
687 int (*libc___res_nsearch)(struct __res_state *state,
691 unsigned char *answer,
697 void *libresolv_handle;
704 struct rwrap_libc_fns fns;
707 static struct rwrap rwrap;
715 static const char *rwrap_str_lib(enum rwrap_lib lib)
720 case RWRAP_LIBRESOLV:
724 /* Compiler would warn us about unhandled enum value if we get here */
729 static void *rwrap_load_lib_handle(enum rwrap_lib lib)
731 int flags = RTLD_LAZY;
736 flags |= RTLD_DEEPBIND;
740 case RWRAP_LIBRESOLV:
741 #ifdef HAVE_LIBRESOLV
742 handle = rwrap.libresolv_handle;
743 if (handle == NULL) {
744 for (i = 10; i >= 0; i--) {
745 char soname[256] = {0};
747 snprintf(soname, sizeof(soname), "libresolv.so.%d", i);
748 handle = dlopen(soname, flags);
749 if (handle != NULL) {
754 rwrap.libresolv_handle = handle;
760 handle = rwrap.libc_handle;
762 if (handle == NULL) {
763 handle = dlopen(LIBC_SO, flags);
765 rwrap.libc_handle = handle;
768 if (handle == NULL) {
769 for (i = 10; i >= 0; i--) {
770 char soname[256] = {0};
772 snprintf(soname, sizeof(soname), "libc.so.%d", i);
773 handle = dlopen(soname, flags);
774 if (handle != NULL) {
779 rwrap.libc_handle = handle;
784 if (handle == NULL) {
786 handle = rwrap.libc_handle = rwrap.libresolv_handle = RTLD_NEXT;
788 RWRAP_LOG(RWRAP_LOG_ERROR,
789 "Failed to dlopen library: %s\n",
798 static void *_rwrap_load_lib_function(enum rwrap_lib lib, const char *fn_name)
803 handle = rwrap_load_lib_handle(lib);
805 func = dlsym(handle, fn_name);
807 RWRAP_LOG(RWRAP_LOG_ERROR,
808 "Failed to find %s: %s\n",
813 RWRAP_LOG(RWRAP_LOG_TRACE,
815 fn_name, rwrap_str_lib(lib));
819 #define rwrap_load_lib_function(lib, fn_name) \
820 if (rwrap.fns.libc_##fn_name == NULL) { \
821 *(void **) (&rwrap.fns.libc_##fn_name) = \
822 _rwrap_load_lib_function(lib, #fn_name); \
828 * Functions especially from libc need to be loaded individually, you can't load
829 * all at once or gdb will segfault at startup. The same applies to valgrind and
830 * has probably something todo with with the linker.
831 * So we need load each function at the point it is called the first time.
834 static int libc_res_init(void)
836 #if defined(HAVE_RES_INIT)
837 rwrap_load_lib_function(RWRAP_LIBRESOLV, res_init);
839 return rwrap.fns.libc_res_init();
840 #elif defined(HAVE___RES_INIT)
841 rwrap_load_lib_function(RWRAP_LIBRESOLV, __res_init);
843 return rwrap.fns.libc___res_init();
848 static int libc_res_ninit(struct __res_state *state)
850 #if defined(HAVE_RES_NINIT)
852 #if defined(HAVE_RES_NINIT_IN_LIBRESOLV)
853 rwrap_load_lib_function(RWRAP_LIBRESOLV, res_ninit);
854 #else /* HAVE_RES_NINIT_IN_LIBRESOLV */
855 rwrap_load_lib_function(RWRAP_LIBC, res_ninit);
856 #endif /* HAVE_RES_NINIT_IN_LIBRESOLV */
858 return rwrap.fns.libc_res_ninit(state);
859 #elif defined(HAVE___RES_NINIT)
860 rwrap_load_lib_function(RWRAP_LIBC, __res_ninit);
862 return rwrap.fns.libc___res_ninit(state);
864 #error "No res_ninit function"
868 static void libc_res_nclose(struct __res_state *state)
870 #if defined(HAVE_RES_NCLOSE)
872 #if defined(HAVE_RES_NCLOSE_IN_LIBRESOLV)
873 rwrap_load_lib_function(RWRAP_LIBRESOLV, res_nclose);
874 #else /* HAVE_RES_NCLOSE_IN_LIBRESOLV */
875 rwrap_load_lib_function(RWRAP_LIBC, res_nclose);
876 #endif /* HAVE_RES_NCLOSE_IN_LIBRESOLV */
878 rwrap.fns.libc_res_nclose(state);
879 #elif defined(HAVE___RES_NCLOSE)
880 rwrap_load_lib_function(RWRAP_LIBC, __res_nclose);
882 rwrap.fns.libc___res_nclose(state);
884 #error "No res_nclose function"
888 static int libc_res_nquery(struct __res_state *state,
892 unsigned char *answer,
895 #if defined(HAVE_RES_NQUERY)
896 rwrap_load_lib_function(RWRAP_LIBRESOLV, res_nquery);
898 return rwrap.fns.libc_res_nquery(state,
904 #elif defined(HAVE___RES_NQUERY)
905 rwrap_load_lib_function(RWRAP_LIBRESOLV, __res_nquery);
907 return rwrap.fns.libc___res_nquery(state,
914 #error "No res_nquery function"
918 static int libc_res_nsearch(struct __res_state *state,
922 unsigned char *answer,
925 #if defined(HAVE_RES_NSEARCH)
926 rwrap_load_lib_function(RWRAP_LIBRESOLV, res_nsearch);
928 return rwrap.fns.libc_res_nsearch(state,
934 #elif defined(HAVE___RES_NSEARCH)
935 rwrap_load_lib_function(RWRAP_LIBRESOLV, __res_nsearch);
937 return rwrap.fns.libc___res_nsearch(state,
944 #error "No res_nsearch function"
948 /****************************************************************************
950 ***************************************************************************/
952 static int rwrap_parse_resolv_conf(struct __res_state *state,
953 const char *resolv_conf)
959 fp = fopen(resolv_conf, "r");
961 RWRAP_LOG(RWRAP_LOG_ERROR,
962 "Opening %s failed: %s",
963 resolv_conf, strerror(errno));
967 while(fgets(buf, sizeof(buf), fp) != NULL) {
970 /* Ignore comments */
971 if (buf[0] == '#' || buf[0] == ';') {
975 if (RESOLV_MATCH(buf, "nameserver") && nserv < MAXNS) {
980 p = buf + strlen("nameserver");
982 /* Skip spaces and tabs */
983 while(isblank((int)p[0])) {
988 while(q[0] != '\n' && q[0] != '\0') {
993 ok = inet_pton(AF_INET, p, &a);
995 state->nsaddr_list[state->nscount] = (struct sockaddr_in) {
996 .sin_family = AF_INET,
998 .sin_port = htons(53),
1005 #ifdef HAVE_RESOLV_IPV6_NSADDRS
1008 ok = inet_pton(AF_INET6, p, &a6);
1010 struct sockaddr_in6 *sa6;
1012 sa6 = malloc(sizeof(*sa6));
1018 sa6->sin6_family = AF_INET6;
1019 sa6->sin6_port = htons(53);
1020 sa6->sin6_flowinfo = 0;
1021 sa6->sin6_addr = a6;
1023 state->_u._ext.nsaddrs[state->_u._ext.nscount] = sa6;
1024 state->_u._ext.nssocks[state->_u._ext.nscount] = -1;
1025 state->_u._ext.nsmap[state->_u._ext.nscount] = MAXNS + 1;
1027 state->_u._ext.nscount++;
1030 RWRAP_LOG(RWRAP_LOG_ERROR,
1031 "Malformed DNS server");
1034 #else /* !HAVE_RESOLV_IPV6_NSADDRS */
1036 * BSD uses an opaque structure to store the
1037 * IPv6 addresses. So we can not simply store
1038 * these addresses the same way as above.
1040 RWRAP_LOG(RWRAP_LOG_WARN,
1041 "resolve_wrapper does not support "
1042 "IPv6 on this platform");
1047 } /* TODO: match other keywords */
1051 RWRAP_LOG(RWRAP_LOG_ERROR,
1052 "Reading from %s failed",
1062 /****************************************************************************
1064 ***************************************************************************/
1066 static int rwrap_res_ninit(struct __res_state *state)
1070 rc = libc_res_ninit(state);
1072 const char *resolv_conf = getenv("RESOLV_WRAPPER_CONF");
1074 if (resolv_conf != NULL) {
1077 (void)i; /* maybe unused */
1079 /* Delete name servers */
1081 memset(state->nsaddr_list, 0, sizeof(state->nsaddr_list));
1083 state->_u._ext.nscount = 0;
1084 #ifdef HAVE_RESOLV_IPV6_NSADDRS
1085 for (i = 0; i < state->_u._ext.nscount; i++) {
1086 SAFE_FREE(state->_u._ext.nsaddrs[i]);
1090 rc = rwrap_parse_resolv_conf(state, resolv_conf);
1097 #if defined(HAVE_RES_NINIT)
1098 int res_ninit(struct __res_state *state)
1099 #elif defined(HAVE___RES_NINIT)
1100 int __res_ninit(struct __res_state *state)
1103 return rwrap_res_ninit(state);
1106 /****************************************************************************
1108 ***************************************************************************/
1110 static struct __res_state rwrap_res_state;
1112 static int rwrap_res_init(void)
1116 rc = rwrap_res_ninit(&rwrap_res_state);
1121 #if defined(HAVE_RES_INIT)
1123 #elif defined(HAVE___RES_INIT)
1124 int __res_init(void)
1127 return rwrap_res_init();
1130 /****************************************************************************
1132 ***************************************************************************/
1134 static void rwrap_res_nclose(struct __res_state *state)
1136 #ifdef HAVE_RESOLV_IPV6_NSADDRS
1140 libc_res_nclose(state);
1142 #ifdef HAVE_RESOLV_IPV6_NSADDRS
1143 if (state != NULL) {
1144 for (i = 0; i < state->_u._ext.nscount; i++) {
1145 SAFE_FREE(state->_u._ext.nsaddrs[i]);
1151 #if defined(HAVE_RES_NCLOSE)
1152 void res_nclose(struct __res_state *state)
1153 #elif defined(HAVE___RES_NCLOSE)
1154 void __res_nclose(struct __res_state *state)
1157 rwrap_res_nclose(state);
1160 /****************************************************************************
1162 ***************************************************************************/
1164 static void rwrap_res_close(void)
1166 rwrap_res_nclose(&rwrap_res_state);
1169 #if defined(HAVE_RES_CLOSE)
1170 void res_close(void)
1171 #elif defined(HAVE___RES_CLOSE)
1172 void __res_close(void)
1178 /****************************************************************************
1180 ***************************************************************************/
1182 static int rwrap_res_nquery(struct __res_state *state,
1186 unsigned char *answer,
1190 const char *fake_hosts;
1195 RWRAP_LOG(RWRAP_LOG_TRACE,
1196 "Resolve the domain name [%s] - class=%d, type=%d",
1197 dname, class, type);
1199 for (i = 0; i < state->nscount; i++) {
1200 char ip[INET6_ADDRSTRLEN];
1202 inet_ntop(AF_INET, &state->nsaddr_list[i].sin_addr, ip, sizeof(ip));
1203 RWRAP_LOG(RWRAP_LOG_TRACE,
1209 fake_hosts = getenv("RESOLV_WRAPPER_HOSTS");
1210 if (fake_hosts != NULL) {
1211 rc = rwrap_res_fake_hosts(fake_hosts, dname, type, answer, anslen);
1213 rc = libc_res_nquery(state, dname, class, type, answer, anslen);
1217 RWRAP_LOG(RWRAP_LOG_TRACE,
1218 "The returned response length is: %d",
1224 #if defined(HAVE_RES_NQUERY)
1225 int res_nquery(struct __res_state *state,
1229 unsigned char *answer,
1231 #elif defined(HAVE___RES_NQUERY)
1232 int __res_nquery(struct __res_state *state,
1236 unsigned char *answer,
1240 return rwrap_res_nquery(state, dname, class, type, answer, anslen);
1243 /****************************************************************************
1245 ***************************************************************************/
1247 static int rwrap_res_query(const char *dname,
1250 unsigned char *answer,
1255 rc = rwrap_res_ninit(&rwrap_res_state);
1260 rc = rwrap_res_nquery(&rwrap_res_state,
1270 #if defined(HAVE_RES_QUERY)
1271 int res_query(const char *dname,
1274 unsigned char *answer,
1276 #elif defined(HAVE___RES_QUERY)
1277 int __res_query(const char *dname,
1280 unsigned char *answer,
1284 return rwrap_res_query(dname, class, type, answer, anslen);
1287 /****************************************************************************
1289 ***************************************************************************/
1291 static int rwrap_res_nsearch(struct __res_state *state,
1295 unsigned char *answer,
1299 const char *fake_hosts;
1304 RWRAP_LOG(RWRAP_LOG_TRACE,
1305 "Resolve the domain name [%s] - class=%d, type=%d",
1306 dname, class, type);
1308 for (i = 0; i < state->nscount; i++) {
1309 char ip[INET6_ADDRSTRLEN];
1311 inet_ntop(AF_INET, &state->nsaddr_list[i].sin_addr, ip, sizeof(ip));
1312 RWRAP_LOG(RWRAP_LOG_TRACE,
1318 fake_hosts = getenv("RESOLV_WRAPPER_HOSTS");
1319 if (fake_hosts != NULL) {
1320 rc = rwrap_res_fake_hosts(fake_hosts, dname, type, answer, anslen);
1322 rc = libc_res_nsearch(state, dname, class, type, answer, anslen);
1325 RWRAP_LOG(RWRAP_LOG_TRACE,
1326 "The returned response length is: %d",
1332 #if defined(HAVE_RES_NSEARCH)
1333 int res_nsearch(struct __res_state *state,
1337 unsigned char *answer,
1339 #elif defined(HAVE___RES_NSEARCH)
1340 int __res_nsearch(struct __res_state *state,
1344 unsigned char *answer,
1348 return rwrap_res_nsearch(state, dname, class, type, answer, anslen);
1351 /****************************************************************************
1353 ***************************************************************************/
1355 static int rwrap_res_search(const char *dname,
1358 unsigned char *answer,
1363 rc = rwrap_res_ninit(&rwrap_res_state);
1368 rc = rwrap_res_nsearch(&rwrap_res_state,
1378 #if defined(HAVE_RES_SEARCH)
1379 int res_search(const char *dname,
1382 unsigned char *answer,
1384 #elif defined(HAVE___RES_SEARCH)
1385 int __res_search(const char *dname,
1388 unsigned char *answer,
1392 return rwrap_res_search(dname, class, type, answer, anslen);