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 #define NEXT_KEY(buf, key) do { \
83 (key) = (buf) ? strpbrk((buf), " \t") : NULL; \
84 if ((key) != NULL) { \
88 while ((key) != NULL \
89 && (isblank((int)(key)[0]))) { \
94 static void rwrap_log(enum rwrap_dbglvl_e dbglvl,
96 const char *format, ...)
101 unsigned int lvl = 0;
104 d = getenv("RESOLV_WRAPPER_DEBUGLEVEL");
109 va_start(va, format);
110 vsnprintf(buffer, sizeof(buffer), format, va);
115 case RWRAP_LOG_ERROR:
117 "RWRAP_ERROR(%d) - %s: %s\n",
122 "RWRAP_WARN(%d) - %s: %s\n",
125 case RWRAP_LOG_DEBUG:
127 "RWRAP_DEBUG(%d) - %s: %s\n",
130 case RWRAP_LOG_TRACE:
132 "RWRAP_TRACE(%d) - %s: %s\n",
138 #endif /* NDEBUG RWRAP_LOG */
141 /* Prepares a fake header with a single response. Advances header_blob */
142 static ssize_t rwrap_fake_header(uint8_t **header_blob, size_t remaining,
149 /* If rdata_size is zero, the answer is empty */
150 answers = rdata_size > 0 ? 1 : 0;
152 if (remaining < NS_HFIXEDSZ) {
153 RWRAP_LOG(RWRAP_LOG_ERROR, "Buffer too small!\n");
158 memset(hb, 0, NS_HFIXEDSZ);
161 h->id = res_randomid(); /* random query ID */
162 h->qr = htons(1); /* response flag */
163 h->rd = htons(1); /* recursion desired */
164 h->ra = htons(1); /* resursion available */
166 h->qdcount = htons(1); /* no. of questions */
167 h->ancount = htons(answers); /* no. of answers */
169 hb += NS_HFIXEDSZ; /* move past the header */
175 static ssize_t rwrap_fake_question(const char *question,
177 uint8_t **question_ptr,
180 uint8_t *qb = *question_ptr;
183 n = ns_name_compress(question, qb, remaining, NULL, NULL);
185 RWRAP_LOG(RWRAP_LOG_ERROR,
186 "Failed to compress [%s]\n", question);
193 if (remaining < 2 * sizeof(uint16_t)) {
194 RWRAP_LOG(RWRAP_LOG_ERROR, "Buffer too small!\n");
199 NS_PUT16(ns_c_in, qb);
202 return n + 2 * sizeof(uint16_t);
205 static ssize_t rwrap_fake_rdata_common(uint16_t type,
211 uint8_t *rd = *rdata_ptr;
214 written = ns_name_compress(key, rd, remaining, NULL, NULL);
216 RWRAP_LOG(RWRAP_LOG_ERROR,
217 "Failed to compress [%s]\n", key);
221 remaining -= written;
223 if (remaining < 3 * sizeof(uint16_t) + sizeof(uint32_t)) {
224 RWRAP_LOG(RWRAP_LOG_ERROR, "Buffer too small\n");
229 NS_PUT16(ns_c_in, rd);
230 NS_PUT32(RWRAP_DEFAULT_FAKE_TTL, rd);
231 NS_PUT16(rdata_size, rd);
233 if (remaining < rdata_size) {
234 RWRAP_LOG(RWRAP_LOG_ERROR, "Buffer too small\n");
239 return written + 3 * sizeof(uint16_t) + sizeof(uint32_t);
242 static ssize_t rwrap_fake_common(uint16_t type,
243 const char *question,
245 uint8_t **answer_ptr,
248 uint8_t *a = *answer_ptr;
254 written = rwrap_fake_header(&a, remaining, rdata_size);
258 remaining -= written;
260 written = rwrap_fake_question(question, type, &a, remaining);
264 remaining -= written;
266 /* rdata_size = 0 denotes an empty answer */
267 if (rdata_size > 0) {
268 written = rwrap_fake_rdata_common(type, rdata_size, question,
279 static int rwrap_fake_a(const char *key,
284 uint8_t *a = answer_ptr;
285 struct in_addr a_rec;
290 RWRAP_LOG(RWRAP_LOG_ERROR, "Malformed record, no value!\n");
294 rc = rwrap_fake_common(ns_t_a, key, sizeof(a_rec), &a, anslen);
299 ok = inet_pton(AF_INET, value, &a_rec);
301 RWRAP_LOG(RWRAP_LOG_ERROR,
302 "Failed to convert [%s] to binary\n", value);
305 memcpy(a, &a_rec, sizeof(struct in_addr));
310 static int rwrap_fake_aaaa(const char *key,
316 struct in6_addr aaaa_rec;
321 RWRAP_LOG(RWRAP_LOG_ERROR, "Malformed record, no value!\n");
325 rc = rwrap_fake_common(ns_t_aaaa, key, sizeof(aaaa_rec), &a, anslen);
330 ok = inet_pton(AF_INET6, value, &aaaa_rec);
332 RWRAP_LOG(RWRAP_LOG_ERROR,
333 "Failed to convert [%s] to binary\n", value);
336 memcpy(a, &aaaa_rec, sizeof(struct in6_addr));
342 * Priority and weight can be omitted from the hosts file, but need to be part
345 #define DFL_SRV_PRIO 1
346 #define DFL_SRV_WEIGHT 100
348 static int rwrap_fake_srv(const char *key,
359 const char *hostname;
360 unsigned char hostname_compressed[MAXDNAME];
361 ssize_t compressed_len;
364 * Parse the value into priority, weight, port and hostname
365 * and check the validity.
368 NEXT_KEY(hostname, str_port);
369 NEXT_KEY(str_port, str_prio);
370 NEXT_KEY(str_prio, str_weight);
371 if (str_port == NULL || hostname == NULL) {
372 RWRAP_LOG(RWRAP_LOG_ERROR,
373 "Malformed SRV entry [%s]\n", value);
376 rdata_size = 3 * sizeof(uint16_t);
378 /* Prepare the data to write */
379 compressed_len = ns_name_compress(hostname,
380 hostname_compressed, MAXDNAME,
382 if (compressed_len < 0) {
385 rdata_size += compressed_len;
387 rv = rwrap_fake_common(ns_t_srv, key, rdata_size, &a, anslen);
393 NS_PUT16(atoi(str_prio), a);
395 NS_PUT16(DFL_SRV_PRIO, a);
398 NS_PUT16(atoi(str_weight), a);
400 NS_PUT16(DFL_SRV_WEIGHT, a);
402 NS_PUT16(atoi(str_port), a);
403 memcpy(a, hostname_compressed, compressed_len);
408 static int rwrap_fake_soa(const char *key,
415 const char *nameserver;
423 unsigned char nameser_compressed[MAXDNAME];
424 ssize_t compressed_ns_len;
425 unsigned char mailbox_compressed[MAXDNAME];
426 ssize_t compressed_mb_len;
429 * parse the value into nameserver, mailbox, serial, refresh,
430 * retry, expire, minimum and check the validity
433 NEXT_KEY(nameserver, mailbox);
434 NEXT_KEY(mailbox, str_serial);
435 NEXT_KEY(str_serial, str_refresh);
436 NEXT_KEY(str_refresh, str_retry);
437 NEXT_KEY(str_retry, str_expire);
438 NEXT_KEY(str_expire, str_minimum);
439 if (nameserver == NULL || mailbox == NULL || str_serial == NULL ||
440 str_refresh == NULL || str_retry == NULL || str_expire == NULL ||
443 RWRAP_LOG(RWRAP_LOG_ERROR,
444 "Malformed SOA entry [%s]\n", value);
447 rdata_size = 5 * sizeof(uint16_t);
449 compressed_ns_len = ns_name_compress(nameserver, nameser_compressed,
450 MAXDNAME, NULL, NULL);
451 if (compressed_ns_len < 0) {
454 rdata_size += compressed_ns_len;
456 compressed_mb_len = ns_name_compress(mailbox, mailbox_compressed,
457 MAXDNAME, NULL, NULL);
458 if (compressed_mb_len < 0) {
461 rdata_size += compressed_mb_len;
463 rv = rwrap_fake_common(ns_t_soa, key, rdata_size, &a, anslen);
468 memcpy(a, nameser_compressed, compressed_ns_len);
469 a += compressed_ns_len;
470 memcpy(a, mailbox_compressed, compressed_mb_len);
471 a += compressed_mb_len;
472 NS_PUT32(atoi(str_serial), a);
473 NS_PUT32(atoi(str_refresh), a);
474 NS_PUT32(atoi(str_retry), a);
475 NS_PUT32(atoi(str_expire), a);
476 NS_PUT32(atoi(str_minimum), a);
481 static int rwrap_fake_empty_query(const char *key,
488 rc = rwrap_fake_common(type, key, 0, &answer, anslen);
496 #define RESOLV_MATCH(line, name) \
497 (strncmp(line, name, sizeof(name) - 1) == 0 && \
498 (line[sizeof(name) - 1] == ' ' || \
499 line[sizeof(name) - 1] == '\t'))
501 #define TYPE_MATCH(type, ns_type, rec_type, str_type, key, query) \
502 ((type) == (ns_type) && \
503 (strncmp((rec_type), (str_type), sizeof(str_type)) == 0) && \
504 (strcmp(key, query)) == 0)
507 /* Reads in a file in the following format:
510 * Malformed entried are silently skipped.
511 * Allocates answer buffer of size anslen that has to be freed after use.
513 static int rwrap_res_fake_hosts(const char *hostfile,
516 unsigned char *answer,
525 RWRAP_LOG(RWRAP_LOG_TRACE,
526 "Searching in fake hosts file %s\n", hostfile);
528 fp = fopen(hostfile, "r");
530 RWRAP_LOG(RWRAP_LOG_ERROR,
531 "Opening %s failed: %s",
532 hostfile, strerror(errno));
536 while (fgets(buf, sizeof(buf), fp) != NULL) {
543 NEXT_KEY(rec_type, key);
544 NEXT_KEY(key, value);
547 while(q[0] != '\n' && q[0] != '\0') {
552 if (key == NULL || value == NULL) {
553 RWRAP_LOG(RWRAP_LOG_WARN,
554 "Malformed line: not enough parts, use \"rec_type key data\n"
555 "For example \"A cwrap.org 10.10.10.10\"");
559 if (TYPE_MATCH(type, ns_t_a, rec_type, "A", key, query)) {
560 rc = rwrap_fake_a(key, value, answer, anslen);
562 } else if (TYPE_MATCH(type, ns_t_aaaa,
563 rec_type, "AAAA", key, query)) {
564 rc = rwrap_fake_aaaa(key, value, answer, anslen);
566 } else if (TYPE_MATCH(type, ns_t_srv,
567 rec_type, "SRV", key, query)) {
568 rc = rwrap_fake_srv(key, value, answer, anslen);
570 } else if (TYPE_MATCH(type, ns_t_soa,
571 rec_type, "SOA", key, query)) {
572 rc = rwrap_fake_soa(key, value, answer, anslen);
579 RWRAP_LOG(RWRAP_LOG_TRACE,
580 "Successfully faked answer for [%s]\n", query);
583 RWRAP_LOG(RWRAP_LOG_ERROR,
584 "Error faking answer for [%s]\n", query);
587 RWRAP_LOG(RWRAP_LOG_TRACE,
588 "Record for [%s] not found\n", query);
589 rc = rwrap_fake_empty_query(key, type, answer, anslen);
597 /*********************************************************
598 * RWRAP LOADING LIBC FUNCTIONS
599 *********************************************************/
603 struct rwrap_libc_fns {
604 int (*libc_res_init)(void);
605 int (*libc___res_init)(void);
606 int (*libc_res_ninit)(struct __res_state *state);
607 int (*libc___res_ninit)(struct __res_state *state);
608 void (*libc_res_nclose)(struct __res_state *state);
609 void (*libc___res_nclose)(struct __res_state *state);
610 void (*libc_res_close)(void);
611 void (*libc___res_close)(void);
612 int (*libc_res_nquery)(struct __res_state *state,
616 unsigned char *answer,
618 int (*libc___res_nquery)(struct __res_state *state,
622 unsigned char *answer,
624 int (*libc_res_nsearch)(struct __res_state *state,
628 unsigned char *answer,
630 int (*libc___res_nsearch)(struct __res_state *state,
634 unsigned char *answer,
640 void *libresolv_handle;
647 struct rwrap_libc_fns fns;
650 static struct rwrap rwrap;
658 static const char *rwrap_str_lib(enum rwrap_lib lib)
663 case RWRAP_LIBRESOLV:
667 /* Compiler would warn us about unhandled enum value if we get here */
672 static void *rwrap_load_lib_handle(enum rwrap_lib lib)
674 int flags = RTLD_LAZY;
679 flags |= RTLD_DEEPBIND;
683 case RWRAP_LIBRESOLV:
684 #ifdef HAVE_LIBRESOLV
685 handle = rwrap.libresolv_handle;
686 if (handle == NULL) {
687 for (i = 10; i >= 0; i--) {
688 char soname[256] = {0};
690 snprintf(soname, sizeof(soname), "libresolv.so.%d", i);
691 handle = dlopen(soname, flags);
692 if (handle != NULL) {
697 rwrap.libresolv_handle = handle;
703 handle = rwrap.libc_handle;
705 if (handle == NULL) {
706 handle = dlopen(LIBC_SO, flags);
708 rwrap.libc_handle = handle;
711 if (handle == NULL) {
712 for (i = 10; i >= 0; i--) {
713 char soname[256] = {0};
715 snprintf(soname, sizeof(soname), "libc.so.%d", i);
716 handle = dlopen(soname, flags);
717 if (handle != NULL) {
722 rwrap.libc_handle = handle;
727 if (handle == NULL) {
729 handle = rwrap.libc_handle = rwrap.libresolv_handle = RTLD_NEXT;
731 RWRAP_LOG(RWRAP_LOG_ERROR,
732 "Failed to dlopen library: %s\n",
741 static void *_rwrap_load_lib_function(enum rwrap_lib lib, const char *fn_name)
746 handle = rwrap_load_lib_handle(lib);
748 func = dlsym(handle, fn_name);
750 RWRAP_LOG(RWRAP_LOG_ERROR,
751 "Failed to find %s: %s\n",
756 RWRAP_LOG(RWRAP_LOG_TRACE,
758 fn_name, rwrap_str_lib(lib));
762 #define rwrap_load_lib_function(lib, fn_name) \
763 if (rwrap.fns.libc_##fn_name == NULL) { \
764 *(void **) (&rwrap.fns.libc_##fn_name) = \
765 _rwrap_load_lib_function(lib, #fn_name); \
771 * Functions especially from libc need to be loaded individually, you can't load
772 * all at once or gdb will segfault at startup. The same applies to valgrind and
773 * has probably something todo with with the linker.
774 * So we need load each function at the point it is called the first time.
777 static int libc_res_init(void)
779 #if defined(HAVE_RES_INIT)
780 rwrap_load_lib_function(RWRAP_LIBRESOLV, res_init);
782 return rwrap.fns.libc_res_init();
783 #elif defined(HAVE___RES_INIT)
784 rwrap_load_lib_function(RWRAP_LIBRESOLV, __res_init);
786 return rwrap.fns.libc___res_init();
791 static int libc_res_ninit(struct __res_state *state)
793 #if defined(HAVE_RES_NINIT)
794 rwrap_load_lib_function(RWRAP_LIBC, res_ninit);
796 return rwrap.fns.libc_res_ninit(state);
797 #elif defined(HAVE___RES_NINIT)
798 rwrap_load_lib_function(RWRAP_LIBC, __res_ninit);
800 return rwrap.fns.libc___res_ninit(state);
802 #error "No res_ninit function"
806 static void libc_res_nclose(struct __res_state *state)
808 #if defined(HAVE_RES_NCLOSE)
809 rwrap_load_lib_function(RWRAP_LIBC, res_nclose);
811 rwrap.fns.libc_res_nclose(state);
812 #elif defined(HAVE___RES_NCLOSE)
813 rwrap_load_lib_function(RWRAP_LIBC, __res_nclose);
815 rwrap.fns.libc___res_nclose(state);
817 #error "No res_nclose function"
821 static int libc_res_nquery(struct __res_state *state,
825 unsigned char *answer,
828 #if defined(HAVE_RES_NQUERY)
829 rwrap_load_lib_function(RWRAP_LIBRESOLV, res_nquery);
831 return rwrap.fns.libc_res_nquery(state,
837 #elif defined(HAVE___RES_NQUERY)
838 rwrap_load_lib_function(RWRAP_LIBRESOLV, __res_nquery);
840 return rwrap.fns.libc___res_nquery(state,
847 #error "No res_nquery function"
851 static int libc_res_nsearch(struct __res_state *state,
855 unsigned char *answer,
858 #if defined(HAVE_RES_NSEARCH)
859 rwrap_load_lib_function(RWRAP_LIBRESOLV, res_nsearch);
861 return rwrap.fns.libc_res_nsearch(state,
867 #elif defined(HAVE___RES_NSEARCH)
868 rwrap_load_lib_function(RWRAP_LIBRESOLV, __res_nsearch);
870 return rwrap.fns.libc___res_nsearch(state,
877 #error "No res_nsearch function"
881 /****************************************************************************
883 ***************************************************************************/
885 static int rwrap_parse_resolv_conf(struct __res_state *state,
886 const char *resolv_conf)
892 fp = fopen(resolv_conf, "r");
894 RWRAP_LOG(RWRAP_LOG_ERROR,
895 "Opening %s failed: %s",
896 resolv_conf, strerror(errno));
900 while(fgets(buf, sizeof(buf), fp) != NULL) {
903 /* Ignore comments */
904 if (buf[0] == '#' || buf[0] == ';') {
908 if (RESOLV_MATCH(buf, "nameserver") && nserv < MAXNS) {
913 p = buf + strlen("nameserver");
915 /* Skip spaces and tabs */
916 while(isblank((int)p[0])) {
921 while(q[0] != '\n' && q[0] != '\0') {
926 ok = inet_pton(AF_INET, p, &a);
928 state->nsaddr_list[state->nscount] = (struct sockaddr_in) {
929 .sin_family = AF_INET,
931 .sin_port = htons(53),
937 #ifdef HAVE_RESOLV_IPV6_NSADDRS
940 ok = inet_pton(AF_INET6, p, &a6);
942 struct sockaddr_in6 *sa6;
944 sa6 = malloc(sizeof(*sa6));
949 sa6->sin6_family = AF_INET6;
950 sa6->sin6_port = htons(53);
951 sa6->sin6_flowinfo = 0;
954 state->_u._ext.nsaddrs[state->_u._ext.nscount] = sa6;
955 state->_u._ext.nssocks[state->_u._ext.nscount] = -1;
956 state->_u._ext.nsmap[state->_u._ext.nscount] = MAXNS + 1;
958 state->_u._ext.nscount++;
961 RWRAP_LOG(RWRAP_LOG_ERROR,
962 "Malformed DNS server");
965 #else /* !HAVE_RESOLV_IPV6_NSADDRS */
967 * BSD uses an opaque structure to store the
968 * IPv6 addresses. So we can not simply store
969 * these addresses the same way as above.
971 RWRAP_LOG(RWRAP_LOG_WARN,
972 "resolve_wrapper does not support "
973 "IPv6 on this platform");
978 } /* TODO: match other keywords */
982 RWRAP_LOG(RWRAP_LOG_ERROR,
983 "Reading from %s failed",
991 /****************************************************************************
993 ***************************************************************************/
995 static int rwrap_res_ninit(struct __res_state *state)
999 rc = libc_res_ninit(state);
1001 const char *resolv_conf = getenv("RESOLV_WRAPPER_CONF");
1003 if (resolv_conf != NULL) {
1006 (void)i; /* maybe unused */
1008 /* Delete name servers */
1010 memset(state->nsaddr_list, 0, sizeof(state->nsaddr_list));
1012 state->_u._ext.nscount = 0;
1013 #ifdef HAVE_RESOLV_IPV6_NSADDRS
1014 for (i = 0; i < state->_u._ext.nscount; i++) {
1015 free(state->_u._ext.nsaddrs[i]);
1016 state->_u._ext.nssocks[i] = 0;
1020 rc = rwrap_parse_resolv_conf(state, resolv_conf);
1027 #if defined(HAVE_RES_NINIT)
1028 int res_ninit(struct __res_state *state)
1029 #elif defined(HAVE___RES_NINIT)
1030 int __res_ninit(struct __res_state *state)
1033 return rwrap_res_ninit(state);
1036 /****************************************************************************
1038 ***************************************************************************/
1040 static struct __res_state rwrap_res_state;
1042 static int rwrap_res_init(void)
1046 rc = rwrap_res_ninit(&rwrap_res_state);
1051 #if defined(HAVE_RES_INIT)
1053 #elif defined(HAVE___RES_INIT)
1054 int __res_init(void)
1057 return rwrap_res_init();
1060 /****************************************************************************
1062 ***************************************************************************/
1064 static void rwrap_res_nclose(struct __res_state *state)
1066 libc_res_nclose(state);
1069 #if defined(HAVE_RES_NCLOSE)
1070 void res_nclose(struct __res_state *state)
1071 #elif defined(HAVE___RES_NCLOSE)
1072 void __res_nclose(struct __res_state *state)
1075 libc_res_nclose(state);
1078 /****************************************************************************
1080 ***************************************************************************/
1082 static void rwrap_res_close(void)
1084 rwrap_res_nclose(&rwrap_res_state);
1087 #if defined(HAVE_RES_CLOSE)
1088 void res_close(void)
1089 #elif defined(HAVE___RES_CLOSE)
1090 void __res_close(void)
1096 /****************************************************************************
1098 ***************************************************************************/
1100 static int rwrap_res_nquery(struct __res_state *state,
1104 unsigned char *answer,
1108 const char *fake_hosts;
1113 RWRAP_LOG(RWRAP_LOG_TRACE,
1114 "Resolve the domain name [%s] - class=%d, type=%d",
1115 dname, class, type);
1117 for (i = 0; i < state->nscount; i++) {
1118 char ip[INET6_ADDRSTRLEN];
1120 inet_ntop(AF_INET, &state->nsaddr_list[i].sin_addr, ip, sizeof(ip));
1121 RWRAP_LOG(RWRAP_LOG_TRACE,
1127 fake_hosts = getenv("RESOLV_WRAPPER_HOSTS");
1128 if (fake_hosts != NULL) {
1129 rc = rwrap_res_fake_hosts(fake_hosts, dname, type, answer, anslen);
1131 rc = libc_res_nquery(state, dname, class, type, answer, anslen);
1135 RWRAP_LOG(RWRAP_LOG_TRACE,
1136 "The returned response length is: %d",
1142 #if defined(HAVE_RES_NQUERY)
1143 int res_nquery(struct __res_state *state,
1147 unsigned char *answer,
1149 #elif defined(HAVE___RES_NQUERY)
1150 int __res_nquery(struct __res_state *state,
1154 unsigned char *answer,
1158 return rwrap_res_nquery(state, dname, class, type, answer, anslen);
1161 /****************************************************************************
1163 ***************************************************************************/
1165 static int rwrap_res_query(const char *dname,
1168 unsigned char *answer,
1173 rc = rwrap_res_ninit(&rwrap_res_state);
1178 rc = rwrap_res_nquery(&rwrap_res_state,
1188 #if defined(HAVE_RES_QUERY)
1189 int res_query(const char *dname,
1192 unsigned char *answer,
1194 #elif defined(HAVE___RES_QUERY)
1195 int __res_query(const char *dname,
1198 unsigned char *answer,
1202 return rwrap_res_query(dname, class, type, answer, anslen);
1205 /****************************************************************************
1207 ***************************************************************************/
1209 static int rwrap_res_nsearch(struct __res_state *state,
1213 unsigned char *answer,
1217 const char *fake_hosts;
1222 RWRAP_LOG(RWRAP_LOG_TRACE,
1223 "Resolve the domain name [%s] - class=%d, type=%d",
1224 dname, class, type);
1226 for (i = 0; i < state->nscount; i++) {
1227 char ip[INET6_ADDRSTRLEN];
1229 inet_ntop(AF_INET, &state->nsaddr_list[i].sin_addr, ip, sizeof(ip));
1230 RWRAP_LOG(RWRAP_LOG_TRACE,
1236 fake_hosts = getenv("RESOLV_WRAPPER_HOSTS");
1237 if (fake_hosts != NULL) {
1238 rc = rwrap_res_fake_hosts(fake_hosts, dname, type, answer, anslen);
1240 rc = libc_res_nsearch(state, dname, class, type, answer, anslen);
1243 RWRAP_LOG(RWRAP_LOG_TRACE,
1244 "The returned response length is: %d",
1250 #if defined(HAVE_RES_NSEARCH)
1251 int res_nsearch(struct __res_state *state,
1255 unsigned char *answer,
1257 #elif defined(HAVE___RES_NSEARCH)
1258 int __res_nsearch(struct __res_state *state,
1262 unsigned char *answer,
1266 return rwrap_res_nsearch(state, dname, class, type, answer, anslen);
1269 /****************************************************************************
1271 ***************************************************************************/
1273 static int rwrap_res_search(const char *dname,
1276 unsigned char *answer,
1281 rc = rwrap_res_ninit(&rwrap_res_state);
1286 rc = rwrap_res_nsearch(&rwrap_res_state,
1296 #if defined(HAVE_RES_SEARCH)
1297 int res_search(const char *dname,
1300 unsigned char *answer,
1302 #elif defined(HAVE___RES_SEARCH)
1303 int __res_search(const char *dname,
1306 unsigned char *answer,
1310 return rwrap_res_search(dname, class, type, answer, anslen);