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 = htons(1); /* response flag */
167 h->rd = htons(1); /* recursion desired */
168 h->ra = htons(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);
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;
258 written = rwrap_fake_header(&a, remaining, rdata_size);
262 remaining -= written;
264 written = rwrap_fake_question(question, type, &a, remaining);
268 remaining -= written;
270 /* rdata_size = 0 denotes an empty answer */
271 if (rdata_size > 0) {
272 written = rwrap_fake_rdata_common(type, rdata_size, question,
283 static int rwrap_fake_a(const char *key,
288 uint8_t *a = answer_ptr;
289 struct in_addr a_rec;
294 RWRAP_LOG(RWRAP_LOG_ERROR, "Malformed record, no value!\n");
298 rc = rwrap_fake_common(ns_t_a, key, sizeof(a_rec), &a, anslen);
303 ok = inet_pton(AF_INET, value, &a_rec);
305 RWRAP_LOG(RWRAP_LOG_ERROR,
306 "Failed to convert [%s] to binary\n", value);
309 memcpy(a, &a_rec, sizeof(struct in_addr));
314 static int rwrap_fake_aaaa(const char *key,
320 struct in6_addr aaaa_rec;
325 RWRAP_LOG(RWRAP_LOG_ERROR, "Malformed record, no value!\n");
329 rc = rwrap_fake_common(ns_t_aaaa, key, sizeof(aaaa_rec), &a, anslen);
334 ok = inet_pton(AF_INET6, value, &aaaa_rec);
336 RWRAP_LOG(RWRAP_LOG_ERROR,
337 "Failed to convert [%s] to binary\n", value);
340 memcpy(a, &aaaa_rec, sizeof(struct in6_addr));
346 * Priority and weight can be omitted from the hosts file, but need to be part
349 #define DFL_SRV_PRIO 1
350 #define DFL_SRV_WEIGHT 100
352 static int rwrap_fake_srv(const char *key,
363 const char *hostname;
364 unsigned char hostname_compressed[MAXDNAME];
365 ssize_t compressed_len;
368 * Parse the value into priority, weight, port and hostname
369 * and check the validity.
372 NEXT_KEY(hostname, str_port);
373 NEXT_KEY(str_port, str_prio);
374 NEXT_KEY(str_prio, str_weight);
375 if (str_port == NULL || hostname == NULL) {
376 RWRAP_LOG(RWRAP_LOG_ERROR,
377 "Malformed SRV entry [%s]\n", value);
380 rdata_size = 3 * sizeof(uint16_t);
382 /* Prepare the data to write */
383 compressed_len = ns_name_compress(hostname,
384 hostname_compressed, MAXDNAME,
386 if (compressed_len < 0) {
389 rdata_size += compressed_len;
391 rv = rwrap_fake_common(ns_t_srv, key, rdata_size, &a, anslen);
397 NS_PUT16(atoi(str_prio), a);
399 NS_PUT16(DFL_SRV_PRIO, a);
402 NS_PUT16(atoi(str_weight), a);
404 NS_PUT16(DFL_SRV_WEIGHT, a);
406 NS_PUT16(atoi(str_port), a);
407 memcpy(a, hostname_compressed, compressed_len);
412 static int rwrap_fake_soa(const char *key,
419 const char *nameserver;
427 unsigned char nameser_compressed[MAXDNAME];
428 ssize_t compressed_ns_len;
429 unsigned char mailbox_compressed[MAXDNAME];
430 ssize_t compressed_mb_len;
433 * parse the value into nameserver, mailbox, serial, refresh,
434 * retry, expire, minimum and check the validity
437 NEXT_KEY(nameserver, mailbox);
438 NEXT_KEY(mailbox, str_serial);
439 NEXT_KEY(str_serial, str_refresh);
440 NEXT_KEY(str_refresh, str_retry);
441 NEXT_KEY(str_retry, str_expire);
442 NEXT_KEY(str_expire, str_minimum);
443 if (nameserver == NULL || mailbox == NULL || str_serial == NULL ||
444 str_refresh == NULL || str_retry == NULL || str_expire == NULL ||
447 RWRAP_LOG(RWRAP_LOG_ERROR,
448 "Malformed SOA entry [%s]\n", value);
451 rdata_size = 5 * sizeof(uint16_t);
453 compressed_ns_len = ns_name_compress(nameserver, nameser_compressed,
454 MAXDNAME, NULL, NULL);
455 if (compressed_ns_len < 0) {
458 rdata_size += compressed_ns_len;
460 compressed_mb_len = ns_name_compress(mailbox, mailbox_compressed,
461 MAXDNAME, NULL, NULL);
462 if (compressed_mb_len < 0) {
465 rdata_size += compressed_mb_len;
467 rv = rwrap_fake_common(ns_t_soa, key, rdata_size, &a, anslen);
472 memcpy(a, nameser_compressed, compressed_ns_len);
473 a += compressed_ns_len;
474 memcpy(a, mailbox_compressed, compressed_mb_len);
475 a += compressed_mb_len;
476 NS_PUT32(atoi(str_serial), a);
477 NS_PUT32(atoi(str_refresh), a);
478 NS_PUT32(atoi(str_retry), a);
479 NS_PUT32(atoi(str_expire), a);
480 NS_PUT32(atoi(str_minimum), a);
485 static int rwrap_fake_cname(const char *key,
492 unsigned char hostname_compressed[MAXDNAME];
496 RWRAP_LOG(RWRAP_LOG_ERROR, "Malformed record, no value!\n");
500 /* Prepare the data to write */
501 rdata_size = ns_name_compress(value,
502 hostname_compressed, MAXDNAME,
504 if (rdata_size < 0) {
508 rv = rwrap_fake_common(ns_t_cname, key, rdata_size, &a, anslen);
513 memcpy(a, hostname_compressed, rdata_size);
518 static int rwrap_fake_empty_query(const char *key,
525 rc = rwrap_fake_common(type, key, 0, &answer, anslen);
533 #define RESOLV_MATCH(line, name) \
534 (strncmp(line, name, sizeof(name) - 1) == 0 && \
535 (line[sizeof(name) - 1] == ' ' || \
536 line[sizeof(name) - 1] == '\t'))
538 #define TYPE_MATCH(type, ns_type, rec_type, str_type, key, query) \
539 ((type) == (ns_type) && \
540 (strncmp((rec_type), (str_type), sizeof(str_type)) == 0) && \
541 (strcmp(key, query)) == 0)
544 /* Reads in a file in the following format:
547 * Malformed entried are silently skipped.
548 * Allocates answer buffer of size anslen that has to be freed after use.
550 static int rwrap_res_fake_hosts(const char *hostfile,
553 unsigned char *answer,
562 RWRAP_LOG(RWRAP_LOG_TRACE,
563 "Searching in fake hosts file %s\n", hostfile);
565 fp = fopen(hostfile, "r");
567 RWRAP_LOG(RWRAP_LOG_ERROR,
568 "Opening %s failed: %s",
569 hostfile, strerror(errno));
573 while (fgets(buf, sizeof(buf), fp) != NULL) {
580 NEXT_KEY(rec_type, key);
581 NEXT_KEY(key, value);
584 while(q[0] != '\n' && q[0] != '\0') {
589 if (key == NULL || value == NULL) {
590 RWRAP_LOG(RWRAP_LOG_WARN,
591 "Malformed line: not enough parts, use \"rec_type key data\n"
592 "For example \"A cwrap.org 10.10.10.10\"");
596 if (TYPE_MATCH(type, ns_t_a, rec_type, "A", key, query)) {
597 rc = rwrap_fake_a(key, value, answer, anslen);
599 } else if (TYPE_MATCH(type, ns_t_aaaa,
600 rec_type, "AAAA", key, query)) {
601 rc = rwrap_fake_aaaa(key, value, answer, anslen);
603 } else if (TYPE_MATCH(type, ns_t_srv,
604 rec_type, "SRV", key, query)) {
605 rc = rwrap_fake_srv(key, value, answer, anslen);
607 } else if (TYPE_MATCH(type, ns_t_soa,
608 rec_type, "SOA", key, query)) {
609 rc = rwrap_fake_soa(key, value, answer, anslen);
611 } else if (TYPE_MATCH(type, ns_t_cname,
612 rec_type, "CNAME", key, query)) {
613 rc = rwrap_fake_cname(key, value, answer, anslen);
620 RWRAP_LOG(RWRAP_LOG_TRACE,
621 "Successfully faked answer for [%s]\n", query);
624 RWRAP_LOG(RWRAP_LOG_ERROR,
625 "Error faking answer for [%s]\n", query);
628 RWRAP_LOG(RWRAP_LOG_TRACE,
629 "Record for [%s] not found\n", query);
630 rc = rwrap_fake_empty_query(key, type, answer, anslen);
638 /*********************************************************
639 * RWRAP LOADING LIBC FUNCTIONS
640 *********************************************************/
644 struct rwrap_libc_fns {
645 int (*libc_res_init)(void);
646 int (*libc___res_init)(void);
647 int (*libc_res_ninit)(struct __res_state *state);
648 int (*libc___res_ninit)(struct __res_state *state);
649 void (*libc_res_nclose)(struct __res_state *state);
650 void (*libc___res_nclose)(struct __res_state *state);
651 void (*libc_res_close)(void);
652 void (*libc___res_close)(void);
653 int (*libc_res_nquery)(struct __res_state *state,
657 unsigned char *answer,
659 int (*libc___res_nquery)(struct __res_state *state,
663 unsigned char *answer,
665 int (*libc_res_nsearch)(struct __res_state *state,
669 unsigned char *answer,
671 int (*libc___res_nsearch)(struct __res_state *state,
675 unsigned char *answer,
681 void *libresolv_handle;
688 struct rwrap_libc_fns fns;
691 static struct rwrap rwrap;
699 static const char *rwrap_str_lib(enum rwrap_lib lib)
704 case RWRAP_LIBRESOLV:
708 /* Compiler would warn us about unhandled enum value if we get here */
713 static void *rwrap_load_lib_handle(enum rwrap_lib lib)
715 int flags = RTLD_LAZY;
720 flags |= RTLD_DEEPBIND;
724 case RWRAP_LIBRESOLV:
725 #ifdef HAVE_LIBRESOLV
726 handle = rwrap.libresolv_handle;
727 if (handle == NULL) {
728 for (i = 10; i >= 0; i--) {
729 char soname[256] = {0};
731 snprintf(soname, sizeof(soname), "libresolv.so.%d", i);
732 handle = dlopen(soname, flags);
733 if (handle != NULL) {
738 rwrap.libresolv_handle = handle;
744 handle = rwrap.libc_handle;
746 if (handle == NULL) {
747 handle = dlopen(LIBC_SO, flags);
749 rwrap.libc_handle = handle;
752 if (handle == NULL) {
753 for (i = 10; i >= 0; i--) {
754 char soname[256] = {0};
756 snprintf(soname, sizeof(soname), "libc.so.%d", i);
757 handle = dlopen(soname, flags);
758 if (handle != NULL) {
763 rwrap.libc_handle = handle;
768 if (handle == NULL) {
770 handle = rwrap.libc_handle = rwrap.libresolv_handle = RTLD_NEXT;
772 RWRAP_LOG(RWRAP_LOG_ERROR,
773 "Failed to dlopen library: %s\n",
782 static void *_rwrap_load_lib_function(enum rwrap_lib lib, const char *fn_name)
787 handle = rwrap_load_lib_handle(lib);
789 func = dlsym(handle, fn_name);
791 RWRAP_LOG(RWRAP_LOG_ERROR,
792 "Failed to find %s: %s\n",
797 RWRAP_LOG(RWRAP_LOG_TRACE,
799 fn_name, rwrap_str_lib(lib));
803 #define rwrap_load_lib_function(lib, fn_name) \
804 if (rwrap.fns.libc_##fn_name == NULL) { \
805 *(void **) (&rwrap.fns.libc_##fn_name) = \
806 _rwrap_load_lib_function(lib, #fn_name); \
812 * Functions especially from libc need to be loaded individually, you can't load
813 * all at once or gdb will segfault at startup. The same applies to valgrind and
814 * has probably something todo with with the linker.
815 * So we need load each function at the point it is called the first time.
818 static int libc_res_init(void)
820 #if defined(HAVE_RES_INIT)
821 rwrap_load_lib_function(RWRAP_LIBRESOLV, res_init);
823 return rwrap.fns.libc_res_init();
824 #elif defined(HAVE___RES_INIT)
825 rwrap_load_lib_function(RWRAP_LIBRESOLV, __res_init);
827 return rwrap.fns.libc___res_init();
832 static int libc_res_ninit(struct __res_state *state)
834 #if defined(HAVE_RES_NINIT)
835 rwrap_load_lib_function(RWRAP_LIBC, res_ninit);
837 return rwrap.fns.libc_res_ninit(state);
838 #elif defined(HAVE___RES_NINIT)
839 rwrap_load_lib_function(RWRAP_LIBC, __res_ninit);
841 return rwrap.fns.libc___res_ninit(state);
843 #error "No res_ninit function"
847 static void libc_res_nclose(struct __res_state *state)
849 #if defined(HAVE_RES_NCLOSE)
850 rwrap_load_lib_function(RWRAP_LIBC, res_nclose);
852 rwrap.fns.libc_res_nclose(state);
853 #elif defined(HAVE___RES_NCLOSE)
854 rwrap_load_lib_function(RWRAP_LIBC, __res_nclose);
856 rwrap.fns.libc___res_nclose(state);
858 #error "No res_nclose function"
862 static int libc_res_nquery(struct __res_state *state,
866 unsigned char *answer,
869 #if defined(HAVE_RES_NQUERY)
870 rwrap_load_lib_function(RWRAP_LIBRESOLV, res_nquery);
872 return rwrap.fns.libc_res_nquery(state,
878 #elif defined(HAVE___RES_NQUERY)
879 rwrap_load_lib_function(RWRAP_LIBRESOLV, __res_nquery);
881 return rwrap.fns.libc___res_nquery(state,
888 #error "No res_nquery function"
892 static int libc_res_nsearch(struct __res_state *state,
896 unsigned char *answer,
899 #if defined(HAVE_RES_NSEARCH)
900 rwrap_load_lib_function(RWRAP_LIBRESOLV, res_nsearch);
902 return rwrap.fns.libc_res_nsearch(state,
908 #elif defined(HAVE___RES_NSEARCH)
909 rwrap_load_lib_function(RWRAP_LIBRESOLV, __res_nsearch);
911 return rwrap.fns.libc___res_nsearch(state,
918 #error "No res_nsearch function"
922 /****************************************************************************
924 ***************************************************************************/
926 static int rwrap_parse_resolv_conf(struct __res_state *state,
927 const char *resolv_conf)
933 fp = fopen(resolv_conf, "r");
935 RWRAP_LOG(RWRAP_LOG_ERROR,
936 "Opening %s failed: %s",
937 resolv_conf, strerror(errno));
941 while(fgets(buf, sizeof(buf), fp) != NULL) {
944 /* Ignore comments */
945 if (buf[0] == '#' || buf[0] == ';') {
949 if (RESOLV_MATCH(buf, "nameserver") && nserv < MAXNS) {
954 p = buf + strlen("nameserver");
956 /* Skip spaces and tabs */
957 while(isblank((int)p[0])) {
962 while(q[0] != '\n' && q[0] != '\0') {
967 ok = inet_pton(AF_INET, p, &a);
969 state->nsaddr_list[state->nscount] = (struct sockaddr_in) {
970 .sin_family = AF_INET,
972 .sin_port = htons(53),
979 #ifdef HAVE_RESOLV_IPV6_NSADDRS
982 ok = inet_pton(AF_INET6, p, &a6);
984 struct sockaddr_in6 *sa6;
986 sa6 = malloc(sizeof(*sa6));
992 sa6->sin6_family = AF_INET6;
993 sa6->sin6_port = htons(53);
994 sa6->sin6_flowinfo = 0;
997 state->_u._ext.nsaddrs[state->_u._ext.nscount] = sa6;
998 state->_u._ext.nssocks[state->_u._ext.nscount] = -1;
999 state->_u._ext.nsmap[state->_u._ext.nscount] = MAXNS + 1;
1001 state->_u._ext.nscount++;
1004 RWRAP_LOG(RWRAP_LOG_ERROR,
1005 "Malformed DNS server");
1008 #else /* !HAVE_RESOLV_IPV6_NSADDRS */
1010 * BSD uses an opaque structure to store the
1011 * IPv6 addresses. So we can not simply store
1012 * these addresses the same way as above.
1014 RWRAP_LOG(RWRAP_LOG_WARN,
1015 "resolve_wrapper does not support "
1016 "IPv6 on this platform");
1021 } /* TODO: match other keywords */
1025 RWRAP_LOG(RWRAP_LOG_ERROR,
1026 "Reading from %s failed",
1036 /****************************************************************************
1038 ***************************************************************************/
1040 static int rwrap_res_ninit(struct __res_state *state)
1044 rc = libc_res_ninit(state);
1046 const char *resolv_conf = getenv("RESOLV_WRAPPER_CONF");
1048 if (resolv_conf != NULL) {
1051 (void)i; /* maybe unused */
1053 /* Delete name servers */
1055 memset(state->nsaddr_list, 0, sizeof(state->nsaddr_list));
1057 state->_u._ext.nscount = 0;
1058 #ifdef HAVE_RESOLV_IPV6_NSADDRS
1059 for (i = 0; i < state->_u._ext.nscount; i++) {
1060 free(state->_u._ext.nsaddrs[i]);
1061 state->_u._ext.nssocks[i] = 0;
1065 rc = rwrap_parse_resolv_conf(state, resolv_conf);
1072 #if defined(HAVE_RES_NINIT)
1073 int res_ninit(struct __res_state *state)
1074 #elif defined(HAVE___RES_NINIT)
1075 int __res_ninit(struct __res_state *state)
1078 return rwrap_res_ninit(state);
1081 /****************************************************************************
1083 ***************************************************************************/
1085 static struct __res_state rwrap_res_state;
1087 static int rwrap_res_init(void)
1091 rc = rwrap_res_ninit(&rwrap_res_state);
1096 #if defined(HAVE_RES_INIT)
1098 #elif defined(HAVE___RES_INIT)
1099 int __res_init(void)
1102 return rwrap_res_init();
1105 /****************************************************************************
1107 ***************************************************************************/
1109 static void rwrap_res_nclose(struct __res_state *state)
1111 #ifdef HAVE_RESOLV_IPV6_NSADDRS
1114 if (state != NULL) {
1115 for (i = 0; i < state->_u._ext.nscount; i++) {
1116 SAFE_FREE(state->_u._ext.nsaddrs[i]);
1117 state->_u._ext.nssocks[i] = 0;
1121 libc_res_nclose(state);
1124 #if defined(HAVE_RES_NCLOSE)
1125 void res_nclose(struct __res_state *state)
1126 #elif defined(HAVE___RES_NCLOSE)
1127 void __res_nclose(struct __res_state *state)
1130 libc_res_nclose(state);
1133 /****************************************************************************
1135 ***************************************************************************/
1137 static void rwrap_res_close(void)
1139 rwrap_res_nclose(&rwrap_res_state);
1142 #if defined(HAVE_RES_CLOSE)
1143 void res_close(void)
1144 #elif defined(HAVE___RES_CLOSE)
1145 void __res_close(void)
1151 /****************************************************************************
1153 ***************************************************************************/
1155 static int rwrap_res_nquery(struct __res_state *state,
1159 unsigned char *answer,
1163 const char *fake_hosts;
1168 RWRAP_LOG(RWRAP_LOG_TRACE,
1169 "Resolve the domain name [%s] - class=%d, type=%d",
1170 dname, class, type);
1172 for (i = 0; i < state->nscount; i++) {
1173 char ip[INET6_ADDRSTRLEN];
1175 inet_ntop(AF_INET, &state->nsaddr_list[i].sin_addr, ip, sizeof(ip));
1176 RWRAP_LOG(RWRAP_LOG_TRACE,
1182 fake_hosts = getenv("RESOLV_WRAPPER_HOSTS");
1183 if (fake_hosts != NULL) {
1184 rc = rwrap_res_fake_hosts(fake_hosts, dname, type, answer, anslen);
1186 rc = libc_res_nquery(state, dname, class, type, answer, anslen);
1190 RWRAP_LOG(RWRAP_LOG_TRACE,
1191 "The returned response length is: %d",
1197 #if defined(HAVE_RES_NQUERY)
1198 int res_nquery(struct __res_state *state,
1202 unsigned char *answer,
1204 #elif defined(HAVE___RES_NQUERY)
1205 int __res_nquery(struct __res_state *state,
1209 unsigned char *answer,
1213 return rwrap_res_nquery(state, dname, class, type, answer, anslen);
1216 /****************************************************************************
1218 ***************************************************************************/
1220 static int rwrap_res_query(const char *dname,
1223 unsigned char *answer,
1228 rc = rwrap_res_ninit(&rwrap_res_state);
1233 rc = rwrap_res_nquery(&rwrap_res_state,
1243 #if defined(HAVE_RES_QUERY)
1244 int res_query(const char *dname,
1247 unsigned char *answer,
1249 #elif defined(HAVE___RES_QUERY)
1250 int __res_query(const char *dname,
1253 unsigned char *answer,
1257 return rwrap_res_query(dname, class, type, answer, anslen);
1260 /****************************************************************************
1262 ***************************************************************************/
1264 static int rwrap_res_nsearch(struct __res_state *state,
1268 unsigned char *answer,
1272 const char *fake_hosts;
1277 RWRAP_LOG(RWRAP_LOG_TRACE,
1278 "Resolve the domain name [%s] - class=%d, type=%d",
1279 dname, class, type);
1281 for (i = 0; i < state->nscount; i++) {
1282 char ip[INET6_ADDRSTRLEN];
1284 inet_ntop(AF_INET, &state->nsaddr_list[i].sin_addr, ip, sizeof(ip));
1285 RWRAP_LOG(RWRAP_LOG_TRACE,
1291 fake_hosts = getenv("RESOLV_WRAPPER_HOSTS");
1292 if (fake_hosts != NULL) {
1293 rc = rwrap_res_fake_hosts(fake_hosts, dname, type, answer, anslen);
1295 rc = libc_res_nsearch(state, dname, class, type, answer, anslen);
1298 RWRAP_LOG(RWRAP_LOG_TRACE,
1299 "The returned response length is: %d",
1305 #if defined(HAVE_RES_NSEARCH)
1306 int res_nsearch(struct __res_state *state,
1310 unsigned char *answer,
1312 #elif defined(HAVE___RES_NSEARCH)
1313 int __res_nsearch(struct __res_state *state,
1317 unsigned char *answer,
1321 return rwrap_res_nsearch(state, dname, class, type, answer, anslen);
1324 /****************************************************************************
1326 ***************************************************************************/
1328 static int rwrap_res_search(const char *dname,
1331 unsigned char *answer,
1336 rc = rwrap_res_ninit(&rwrap_res_state);
1341 rc = rwrap_res_nsearch(&rwrap_res_state,
1351 #if defined(HAVE_RES_SEARCH)
1352 int res_search(const char *dname,
1355 unsigned char *answer,
1357 #elif defined(HAVE___RES_SEARCH)
1358 int __res_search(const char *dname,
1361 unsigned char *answer,
1365 return rwrap_res_search(dname, class, type, answer, anslen);