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 */
128 #define NEXT_KEY(buf, key) do { \
129 (key) = (buf) ? strpbrk((buf), " \t") : NULL; \
130 if ((key) != NULL) { \
134 while ((key) != NULL \
135 && (isblank((int)(key)[0]))) { \
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_cname(const char *key,
488 unsigned char hostname_compressed[MAXDNAME];
492 RWRAP_LOG(RWRAP_LOG_ERROR, "Malformed record, no value!\n");
496 /* Prepare the data to write */
497 rdata_size = ns_name_compress(value,
498 hostname_compressed, MAXDNAME,
500 if (rdata_size < 0) {
504 rv = rwrap_fake_common(ns_t_cname, key, rdata_size, &a, anslen);
509 memcpy(a, hostname_compressed, rdata_size);
514 static int rwrap_fake_empty_query(const char *key,
521 rc = rwrap_fake_common(type, key, 0, &answer, anslen);
529 #define RESOLV_MATCH(line, name) \
530 (strncmp(line, name, sizeof(name) - 1) == 0 && \
531 (line[sizeof(name) - 1] == ' ' || \
532 line[sizeof(name) - 1] == '\t'))
534 #define TYPE_MATCH(type, ns_type, rec_type, str_type, key, query) \
535 ((type) == (ns_type) && \
536 (strncmp((rec_type), (str_type), sizeof(str_type)) == 0) && \
537 (strcmp(key, query)) == 0)
540 /* Reads in a file in the following format:
543 * Malformed entried are silently skipped.
544 * Allocates answer buffer of size anslen that has to be freed after use.
546 static int rwrap_res_fake_hosts(const char *hostfile,
549 unsigned char *answer,
558 RWRAP_LOG(RWRAP_LOG_TRACE,
559 "Searching in fake hosts file %s\n", hostfile);
561 fp = fopen(hostfile, "r");
563 RWRAP_LOG(RWRAP_LOG_ERROR,
564 "Opening %s failed: %s",
565 hostfile, strerror(errno));
569 while (fgets(buf, sizeof(buf), fp) != NULL) {
576 NEXT_KEY(rec_type, key);
577 NEXT_KEY(key, value);
580 while(q[0] != '\n' && q[0] != '\0') {
585 if (key == NULL || value == NULL) {
586 RWRAP_LOG(RWRAP_LOG_WARN,
587 "Malformed line: not enough parts, use \"rec_type key data\n"
588 "For example \"A cwrap.org 10.10.10.10\"");
592 if (TYPE_MATCH(type, ns_t_a, rec_type, "A", key, query)) {
593 rc = rwrap_fake_a(key, value, answer, anslen);
595 } else if (TYPE_MATCH(type, ns_t_aaaa,
596 rec_type, "AAAA", key, query)) {
597 rc = rwrap_fake_aaaa(key, value, answer, anslen);
599 } else if (TYPE_MATCH(type, ns_t_srv,
600 rec_type, "SRV", key, query)) {
601 rc = rwrap_fake_srv(key, value, answer, anslen);
603 } else if (TYPE_MATCH(type, ns_t_soa,
604 rec_type, "SOA", key, query)) {
605 rc = rwrap_fake_soa(key, value, answer, anslen);
607 } else if (TYPE_MATCH(type, ns_t_cname,
608 rec_type, "CNAME", key, query)) {
609 rc = rwrap_fake_cname(key, value, answer, anslen);
616 RWRAP_LOG(RWRAP_LOG_TRACE,
617 "Successfully faked answer for [%s]\n", query);
620 RWRAP_LOG(RWRAP_LOG_ERROR,
621 "Error faking answer for [%s]\n", query);
624 RWRAP_LOG(RWRAP_LOG_TRACE,
625 "Record for [%s] not found\n", query);
626 rc = rwrap_fake_empty_query(key, type, answer, anslen);
634 /*********************************************************
635 * RWRAP LOADING LIBC FUNCTIONS
636 *********************************************************/
640 struct rwrap_libc_fns {
641 int (*libc_res_init)(void);
642 int (*libc___res_init)(void);
643 int (*libc_res_ninit)(struct __res_state *state);
644 int (*libc___res_ninit)(struct __res_state *state);
645 void (*libc_res_nclose)(struct __res_state *state);
646 void (*libc___res_nclose)(struct __res_state *state);
647 void (*libc_res_close)(void);
648 void (*libc___res_close)(void);
649 int (*libc_res_nquery)(struct __res_state *state,
653 unsigned char *answer,
655 int (*libc___res_nquery)(struct __res_state *state,
659 unsigned char *answer,
661 int (*libc_res_nsearch)(struct __res_state *state,
665 unsigned char *answer,
667 int (*libc___res_nsearch)(struct __res_state *state,
671 unsigned char *answer,
677 void *libresolv_handle;
684 struct rwrap_libc_fns fns;
687 static struct rwrap rwrap;
695 static const char *rwrap_str_lib(enum rwrap_lib lib)
700 case RWRAP_LIBRESOLV:
704 /* Compiler would warn us about unhandled enum value if we get here */
709 static void *rwrap_load_lib_handle(enum rwrap_lib lib)
711 int flags = RTLD_LAZY;
716 flags |= RTLD_DEEPBIND;
720 case RWRAP_LIBRESOLV:
721 #ifdef HAVE_LIBRESOLV
722 handle = rwrap.libresolv_handle;
723 if (handle == NULL) {
724 for (i = 10; i >= 0; i--) {
725 char soname[256] = {0};
727 snprintf(soname, sizeof(soname), "libresolv.so.%d", i);
728 handle = dlopen(soname, flags);
729 if (handle != NULL) {
734 rwrap.libresolv_handle = handle;
740 handle = rwrap.libc_handle;
742 if (handle == NULL) {
743 handle = dlopen(LIBC_SO, flags);
745 rwrap.libc_handle = handle;
748 if (handle == NULL) {
749 for (i = 10; i >= 0; i--) {
750 char soname[256] = {0};
752 snprintf(soname, sizeof(soname), "libc.so.%d", i);
753 handle = dlopen(soname, flags);
754 if (handle != NULL) {
759 rwrap.libc_handle = handle;
764 if (handle == NULL) {
766 handle = rwrap.libc_handle = rwrap.libresolv_handle = RTLD_NEXT;
768 RWRAP_LOG(RWRAP_LOG_ERROR,
769 "Failed to dlopen library: %s\n",
778 static void *_rwrap_load_lib_function(enum rwrap_lib lib, const char *fn_name)
783 handle = rwrap_load_lib_handle(lib);
785 func = dlsym(handle, fn_name);
787 RWRAP_LOG(RWRAP_LOG_ERROR,
788 "Failed to find %s: %s\n",
793 RWRAP_LOG(RWRAP_LOG_TRACE,
795 fn_name, rwrap_str_lib(lib));
799 #define rwrap_load_lib_function(lib, fn_name) \
800 if (rwrap.fns.libc_##fn_name == NULL) { \
801 *(void **) (&rwrap.fns.libc_##fn_name) = \
802 _rwrap_load_lib_function(lib, #fn_name); \
808 * Functions especially from libc need to be loaded individually, you can't load
809 * all at once or gdb will segfault at startup. The same applies to valgrind and
810 * has probably something todo with with the linker.
811 * So we need load each function at the point it is called the first time.
814 static int libc_res_init(void)
816 #if defined(HAVE_RES_INIT)
817 rwrap_load_lib_function(RWRAP_LIBRESOLV, res_init);
819 return rwrap.fns.libc_res_init();
820 #elif defined(HAVE___RES_INIT)
821 rwrap_load_lib_function(RWRAP_LIBRESOLV, __res_init);
823 return rwrap.fns.libc___res_init();
828 static int libc_res_ninit(struct __res_state *state)
830 #if defined(HAVE_RES_NINIT)
831 rwrap_load_lib_function(RWRAP_LIBC, res_ninit);
833 return rwrap.fns.libc_res_ninit(state);
834 #elif defined(HAVE___RES_NINIT)
835 rwrap_load_lib_function(RWRAP_LIBC, __res_ninit);
837 return rwrap.fns.libc___res_ninit(state);
839 #error "No res_ninit function"
843 static void libc_res_nclose(struct __res_state *state)
845 #if defined(HAVE_RES_NCLOSE)
846 rwrap_load_lib_function(RWRAP_LIBC, res_nclose);
848 rwrap.fns.libc_res_nclose(state);
849 #elif defined(HAVE___RES_NCLOSE)
850 rwrap_load_lib_function(RWRAP_LIBC, __res_nclose);
852 rwrap.fns.libc___res_nclose(state);
854 #error "No res_nclose function"
858 static int libc_res_nquery(struct __res_state *state,
862 unsigned char *answer,
865 #if defined(HAVE_RES_NQUERY)
866 rwrap_load_lib_function(RWRAP_LIBRESOLV, res_nquery);
868 return rwrap.fns.libc_res_nquery(state,
874 #elif defined(HAVE___RES_NQUERY)
875 rwrap_load_lib_function(RWRAP_LIBRESOLV, __res_nquery);
877 return rwrap.fns.libc___res_nquery(state,
884 #error "No res_nquery function"
888 static int libc_res_nsearch(struct __res_state *state,
892 unsigned char *answer,
895 #if defined(HAVE_RES_NSEARCH)
896 rwrap_load_lib_function(RWRAP_LIBRESOLV, res_nsearch);
898 return rwrap.fns.libc_res_nsearch(state,
904 #elif defined(HAVE___RES_NSEARCH)
905 rwrap_load_lib_function(RWRAP_LIBRESOLV, __res_nsearch);
907 return rwrap.fns.libc___res_nsearch(state,
914 #error "No res_nsearch function"
918 /****************************************************************************
920 ***************************************************************************/
922 static int rwrap_parse_resolv_conf(struct __res_state *state,
923 const char *resolv_conf)
929 fp = fopen(resolv_conf, "r");
931 RWRAP_LOG(RWRAP_LOG_ERROR,
932 "Opening %s failed: %s",
933 resolv_conf, strerror(errno));
937 while(fgets(buf, sizeof(buf), fp) != NULL) {
940 /* Ignore comments */
941 if (buf[0] == '#' || buf[0] == ';') {
945 if (RESOLV_MATCH(buf, "nameserver") && nserv < MAXNS) {
950 p = buf + strlen("nameserver");
952 /* Skip spaces and tabs */
953 while(isblank((int)p[0])) {
958 while(q[0] != '\n' && q[0] != '\0') {
963 ok = inet_pton(AF_INET, p, &a);
965 state->nsaddr_list[state->nscount] = (struct sockaddr_in) {
966 .sin_family = AF_INET,
968 .sin_port = htons(53),
974 #ifdef HAVE_RESOLV_IPV6_NSADDRS
977 ok = inet_pton(AF_INET6, p, &a6);
979 struct sockaddr_in6 *sa6;
981 sa6 = malloc(sizeof(*sa6));
986 sa6->sin6_family = AF_INET6;
987 sa6->sin6_port = htons(53);
988 sa6->sin6_flowinfo = 0;
991 state->_u._ext.nsaddrs[state->_u._ext.nscount] = sa6;
992 state->_u._ext.nssocks[state->_u._ext.nscount] = -1;
993 state->_u._ext.nsmap[state->_u._ext.nscount] = MAXNS + 1;
995 state->_u._ext.nscount++;
998 RWRAP_LOG(RWRAP_LOG_ERROR,
999 "Malformed DNS server");
1002 #else /* !HAVE_RESOLV_IPV6_NSADDRS */
1004 * BSD uses an opaque structure to store the
1005 * IPv6 addresses. So we can not simply store
1006 * these addresses the same way as above.
1008 RWRAP_LOG(RWRAP_LOG_WARN,
1009 "resolve_wrapper does not support "
1010 "IPv6 on this platform");
1015 } /* TODO: match other keywords */
1019 RWRAP_LOG(RWRAP_LOG_ERROR,
1020 "Reading from %s failed",
1028 /****************************************************************************
1030 ***************************************************************************/
1032 static int rwrap_res_ninit(struct __res_state *state)
1036 rc = libc_res_ninit(state);
1038 const char *resolv_conf = getenv("RESOLV_WRAPPER_CONF");
1040 if (resolv_conf != NULL) {
1043 (void)i; /* maybe unused */
1045 /* Delete name servers */
1047 memset(state->nsaddr_list, 0, sizeof(state->nsaddr_list));
1049 state->_u._ext.nscount = 0;
1050 #ifdef HAVE_RESOLV_IPV6_NSADDRS
1051 for (i = 0; i < state->_u._ext.nscount; i++) {
1052 free(state->_u._ext.nsaddrs[i]);
1053 state->_u._ext.nssocks[i] = 0;
1057 rc = rwrap_parse_resolv_conf(state, resolv_conf);
1064 #if defined(HAVE_RES_NINIT)
1065 int res_ninit(struct __res_state *state)
1066 #elif defined(HAVE___RES_NINIT)
1067 int __res_ninit(struct __res_state *state)
1070 return rwrap_res_ninit(state);
1073 /****************************************************************************
1075 ***************************************************************************/
1077 static struct __res_state rwrap_res_state;
1079 static int rwrap_res_init(void)
1083 rc = rwrap_res_ninit(&rwrap_res_state);
1088 #if defined(HAVE_RES_INIT)
1090 #elif defined(HAVE___RES_INIT)
1091 int __res_init(void)
1094 return rwrap_res_init();
1097 /****************************************************************************
1099 ***************************************************************************/
1101 static void rwrap_res_nclose(struct __res_state *state)
1103 libc_res_nclose(state);
1106 #if defined(HAVE_RES_NCLOSE)
1107 void res_nclose(struct __res_state *state)
1108 #elif defined(HAVE___RES_NCLOSE)
1109 void __res_nclose(struct __res_state *state)
1112 libc_res_nclose(state);
1115 /****************************************************************************
1117 ***************************************************************************/
1119 static void rwrap_res_close(void)
1121 rwrap_res_nclose(&rwrap_res_state);
1124 #if defined(HAVE_RES_CLOSE)
1125 void res_close(void)
1126 #elif defined(HAVE___RES_CLOSE)
1127 void __res_close(void)
1133 /****************************************************************************
1135 ***************************************************************************/
1137 static int rwrap_res_nquery(struct __res_state *state,
1141 unsigned char *answer,
1145 const char *fake_hosts;
1150 RWRAP_LOG(RWRAP_LOG_TRACE,
1151 "Resolve the domain name [%s] - class=%d, type=%d",
1152 dname, class, type);
1154 for (i = 0; i < state->nscount; i++) {
1155 char ip[INET6_ADDRSTRLEN];
1157 inet_ntop(AF_INET, &state->nsaddr_list[i].sin_addr, ip, sizeof(ip));
1158 RWRAP_LOG(RWRAP_LOG_TRACE,
1164 fake_hosts = getenv("RESOLV_WRAPPER_HOSTS");
1165 if (fake_hosts != NULL) {
1166 rc = rwrap_res_fake_hosts(fake_hosts, dname, type, answer, anslen);
1168 rc = libc_res_nquery(state, dname, class, type, answer, anslen);
1172 RWRAP_LOG(RWRAP_LOG_TRACE,
1173 "The returned response length is: %d",
1179 #if defined(HAVE_RES_NQUERY)
1180 int res_nquery(struct __res_state *state,
1184 unsigned char *answer,
1186 #elif defined(HAVE___RES_NQUERY)
1187 int __res_nquery(struct __res_state *state,
1191 unsigned char *answer,
1195 return rwrap_res_nquery(state, dname, class, type, answer, anslen);
1198 /****************************************************************************
1200 ***************************************************************************/
1202 static int rwrap_res_query(const char *dname,
1205 unsigned char *answer,
1210 rc = rwrap_res_ninit(&rwrap_res_state);
1215 rc = rwrap_res_nquery(&rwrap_res_state,
1225 #if defined(HAVE_RES_QUERY)
1226 int res_query(const char *dname,
1229 unsigned char *answer,
1231 #elif defined(HAVE___RES_QUERY)
1232 int __res_query(const char *dname,
1235 unsigned char *answer,
1239 return rwrap_res_query(dname, class, type, answer, anslen);
1242 /****************************************************************************
1244 ***************************************************************************/
1246 static int rwrap_res_nsearch(struct __res_state *state,
1250 unsigned char *answer,
1254 const char *fake_hosts;
1259 RWRAP_LOG(RWRAP_LOG_TRACE,
1260 "Resolve the domain name [%s] - class=%d, type=%d",
1261 dname, class, type);
1263 for (i = 0; i < state->nscount; i++) {
1264 char ip[INET6_ADDRSTRLEN];
1266 inet_ntop(AF_INET, &state->nsaddr_list[i].sin_addr, ip, sizeof(ip));
1267 RWRAP_LOG(RWRAP_LOG_TRACE,
1273 fake_hosts = getenv("RESOLV_WRAPPER_HOSTS");
1274 if (fake_hosts != NULL) {
1275 rc = rwrap_res_fake_hosts(fake_hosts, dname, type, answer, anslen);
1277 rc = libc_res_nsearch(state, dname, class, type, answer, anslen);
1280 RWRAP_LOG(RWRAP_LOG_TRACE,
1281 "The returned response length is: %d",
1287 #if defined(HAVE_RES_NSEARCH)
1288 int res_nsearch(struct __res_state *state,
1292 unsigned char *answer,
1294 #elif defined(HAVE___RES_NSEARCH)
1295 int __res_nsearch(struct __res_state *state,
1299 unsigned char *answer,
1303 return rwrap_res_nsearch(state, dname, class, type, answer, anslen);
1306 /****************************************************************************
1308 ***************************************************************************/
1310 static int rwrap_res_search(const char *dname,
1313 unsigned char *answer,
1318 rc = rwrap_res_ninit(&rwrap_res_state);
1323 rc = rwrap_res_nsearch(&rwrap_res_state,
1333 #if defined(HAVE_RES_SEARCH)
1334 int res_search(const char *dname,
1337 unsigned char *answer,
1339 #elif defined(HAVE___RES_SEARCH)
1340 int __res_search(const char *dname,
1343 unsigned char *answer,
1347 return rwrap_res_search(dname, class, type, answer, anslen);