rwrap: Make the rwrap_fake_* functions only fake RRs.
[Samba.git] / lib / resolv_wrapper / resolv_wrapper.c
blobea13aa645e0f37746c27ea186c282cd6cee69a3b
1 /*
2 * Copyright (c) 2014 Andreas Schneider <asn@samba.org>
3 * Copyright (c) 2014 Jakub Hrozek <jakub.hrozek@gmail.com>
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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
32 * SUCH DAMAGE.
35 #include "config.h"
37 #include <errno.h>
38 #include <arpa/inet.h>
39 #include <netinet/in.h>
40 #include <sys/types.h>
41 #include <stdarg.h>
42 #include <stdlib.h>
43 #include <stdio.h>
44 #include <stdbool.h>
45 #include <string.h>
46 #include <unistd.h>
47 #include <ctype.h>
49 #include <resolv.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)))
54 #else
55 #define PRINTF_ATTRIBUTE(a,b)
56 #endif /* HAVE_ATTRIBUTE_PRINTF_FORMAT */
58 #ifdef HAVE_DESTRUCTOR_ATTRIBUTE
59 #define DESTRUCTOR_ATTRIBUTE __attribute__ ((destructor))
60 #else
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 */
68 enum rwrap_dbglvl_e {
69 RWRAP_LOG_ERROR = 0,
70 RWRAP_LOG_WARN,
71 RWRAP_LOG_DEBUG,
72 RWRAP_LOG_TRACE
75 #ifdef NDEBUG
76 # define RWRAP_LOG(...)
77 #else /* NDEBUG */
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,
83 const char *func,
84 const char *format, ...)
86 char buffer[1024];
87 va_list va;
88 const char *d;
89 unsigned int lvl = 0;
90 int pid = getpid();
92 d = getenv("RESOLV_WRAPPER_DEBUGLEVEL");
93 if (d != NULL) {
94 lvl = atoi(d);
97 va_start(va, format);
98 vsnprintf(buffer, sizeof(buffer), format, va);
99 va_end(va);
101 if (lvl >= dbglvl) {
102 switch (dbglvl) {
103 case RWRAP_LOG_ERROR:
104 fprintf(stderr,
105 "RWRAP_ERROR(%d) - %s: %s\n",
106 pid, func, buffer);
107 break;
108 case RWRAP_LOG_WARN:
109 fprintf(stderr,
110 "RWRAP_WARN(%d) - %s: %s\n",
111 pid, func, buffer);
112 break;
113 case RWRAP_LOG_DEBUG:
114 fprintf(stderr,
115 "RWRAP_DEBUG(%d) - %s: %s\n",
116 pid, func, buffer);
117 break;
118 case RWRAP_LOG_TRACE:
119 fprintf(stderr,
120 "RWRAP_TRACE(%d) - %s: %s\n",
121 pid, func, buffer);
122 break;
126 #endif /* NDEBUG RWRAP_LOG */
128 #ifndef SAFE_FREE
129 #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); (x)=NULL;} } while(0)
130 #endif
132 #define NEXT_KEY(buf, key) do { \
133 (key) = (buf) ? strpbrk((buf), " \t") : NULL; \
134 if ((key) != NULL) { \
135 (key)[0] = '\0'; \
136 (key)++; \
138 while ((key) != NULL \
139 && (isblank((int)(key)[0]))) { \
140 (key)++; \
142 } while(0);
144 #define RWRAP_MAX_RECURSION 5
146 /* Priority and weight can be omitted from the hosts file, but need to be part
147 * of the output
149 #define DFL_SRV_PRIO 1
150 #define DFL_SRV_WEIGHT 100
152 struct rwrap_srv_rrdata {
153 uint16_t port;
154 uint16_t prio;
155 uint16_t weight;
156 char hostname[MAXDNAME];
159 struct rwrap_soa_rrdata {
160 uint32_t serial;
161 uint32_t refresh;
162 uint32_t retry;
163 uint32_t expire;
164 uint32_t minimum;
165 char nameserver[MAXDNAME];
166 char mailbox[MAXDNAME];
169 struct rwrap_fake_rr {
170 union fake_rrdata {
171 struct in_addr a_rec;
172 struct in6_addr aaaa_rec;
173 struct rwrap_srv_rrdata srv_rec;
174 struct rwrap_soa_rrdata soa_rec;
175 char cname_rec[MAXDNAME];
176 } rrdata;
178 char key[MAXDNAME];
179 int type; /* ns_t_* */
182 static void rwrap_fake_rr_init(struct rwrap_fake_rr *rr, size_t len)
184 size_t i;
186 for (i = 0; i < len; i++) {
187 rr[i].type = ns_t_invalid;
191 static int rwrap_create_fake_a_rr(const char *key,
192 const char *value,
193 struct rwrap_fake_rr *rr)
195 int ok;
197 ok = inet_pton(AF_INET, value, &rr->rrdata.a_rec);
198 if (!ok) {
199 RWRAP_LOG(RWRAP_LOG_ERROR,
200 "Failed to convert [%s] to binary\n", value);
201 return -1;
204 memcpy(rr->key, key, strlen(key) + 1);
205 rr->type = ns_t_a;
206 return 0;
209 static int rwrap_create_fake_aaaa_rr(const char *key,
210 const char *value,
211 struct rwrap_fake_rr *rr)
213 int ok;
215 ok = inet_pton(AF_INET6, value, &rr->rrdata.aaaa_rec);
216 if (!ok) {
217 RWRAP_LOG(RWRAP_LOG_ERROR,
218 "Failed to convert [%s] to binary\n", value);
219 return -1;
222 memcpy(rr->key, key, strlen(key) + 1);
223 rr->type = ns_t_aaaa;
224 return 0;
227 static int rwrap_create_fake_srv_rr(const char *key,
228 const char *value,
229 struct rwrap_fake_rr *rr)
231 char *str_prio;
232 char *str_weight;
233 char *str_port;
234 const char *hostname;
236 /* parse the value into priority, weight, port and hostname
237 * and check the validity */
238 hostname = value;
239 NEXT_KEY(hostname, str_port);
240 NEXT_KEY(str_port, str_prio);
241 NEXT_KEY(str_prio, str_weight);
242 if (str_port == NULL || hostname == NULL) {
243 RWRAP_LOG(RWRAP_LOG_ERROR,
244 "Malformed SRV entry [%s]\n", value);
245 return -1;
248 if (str_prio) {
249 rr->rrdata.srv_rec.prio = atoi(str_prio);
250 } else {
251 rr->rrdata.srv_rec.prio = DFL_SRV_PRIO;
253 if (str_weight) {
254 rr->rrdata.srv_rec.weight = atoi(str_weight);
255 } else {
256 rr->rrdata.srv_rec.weight = DFL_SRV_WEIGHT;
258 rr->rrdata.srv_rec.port = atoi(str_port);
259 memcpy(rr->rrdata.srv_rec.hostname , hostname, strlen(hostname) + 1);
261 memcpy(rr->key, key, strlen(key) + 1);
262 rr->type = ns_t_srv;
263 return 0;
266 static int rwrap_create_fake_soa_rr(const char *key,
267 const char *value,
268 struct rwrap_fake_rr *rr)
270 const char *nameserver;
271 char *mailbox;
272 char *str_serial;
273 char *str_refresh;
274 char *str_retry;
275 char *str_expire;
276 char *str_minimum;
278 /* parse the value into nameserver, mailbox, serial, refresh,
279 * retry, expire, minimum and check the validity
281 nameserver = value;
282 NEXT_KEY(nameserver, mailbox);
283 NEXT_KEY(mailbox, str_serial);
284 NEXT_KEY(str_serial, str_refresh);
285 NEXT_KEY(str_refresh, str_retry);
286 NEXT_KEY(str_retry, str_expire);
287 NEXT_KEY(str_expire, str_minimum);
288 if (nameserver == NULL || mailbox == NULL || str_serial == NULL ||
289 str_refresh == NULL || str_retry == NULL || str_expire == NULL ||
290 str_minimum == NULL) {
291 RWRAP_LOG(RWRAP_LOG_ERROR,
292 "Malformed SOA entry [%s]\n", value);
293 return -1;
296 memcpy(rr->rrdata.soa_rec.nameserver, nameserver, strlen(nameserver)+1);
297 memcpy(rr->rrdata.soa_rec.mailbox, mailbox, strlen(mailbox)+1);
299 rr->rrdata.soa_rec.serial = atoi(str_serial);
300 rr->rrdata.soa_rec.refresh = atoi(str_refresh);
301 rr->rrdata.soa_rec.retry = atoi(str_retry);
302 rr->rrdata.soa_rec.expire = atoi(str_expire);
303 rr->rrdata.soa_rec.minimum = atoi(str_minimum);
305 memcpy(rr->key, key, strlen(key) + 1);
306 rr->type = ns_t_soa;
307 return 0;
310 static int rwrap_create_fake_cname_rr(const char *key,
311 const char *value,
312 struct rwrap_fake_rr *rr)
314 memcpy(rr->rrdata.cname_rec , value, strlen(value) + 1);
315 memcpy(rr->key, key, strlen(key) + 1);
316 rr->type = ns_t_cname;
317 return 0;
320 /* Prepares a fake header with a single response. Advances header_blob */
321 static ssize_t rwrap_fake_header(uint8_t **header_blob, size_t remaining,
322 size_t ancount, size_t arcount)
324 uint8_t *hb;
325 HEADER *h;
327 if (remaining < NS_HFIXEDSZ) {
328 RWRAP_LOG(RWRAP_LOG_ERROR, "Buffer too small!\n");
329 return -1;
332 hb = *header_blob;
333 memset(hb, 0, NS_HFIXEDSZ);
335 h = (HEADER *) hb;
336 h->id = res_randomid(); /* random query ID */
337 h->qr = 1; /* response flag */
338 h->rd = 1; /* recursion desired */
339 h->ra = 1; /* resursion available */
341 h->qdcount = htons(1); /* no. of questions */
342 h->ancount = htons(ancount); /* no. of answers */
343 h->arcount = htons(arcount); /* no. of add'tl records */
345 hb += NS_HFIXEDSZ; /* move past the header */
346 *header_blob = hb;
348 return NS_HFIXEDSZ;
351 static ssize_t rwrap_fake_question(const char *question,
352 uint16_t type,
353 uint8_t **question_ptr,
354 size_t remaining)
356 uint8_t *qb = *question_ptr;
357 int n;
359 n = ns_name_compress(question, qb, remaining, NULL, NULL);
360 if (n < 0) {
361 RWRAP_LOG(RWRAP_LOG_ERROR,
362 "Failed to compress [%s]\n", question);
363 return -1;
366 qb += n;
367 remaining -= n;
369 if (remaining < 2 * sizeof(uint16_t)) {
370 RWRAP_LOG(RWRAP_LOG_ERROR, "Buffer too small!\n");
371 return -1;
374 NS_PUT16(type, qb);
375 NS_PUT16(ns_c_in, qb);
377 *question_ptr = qb;
378 return n + 2 * sizeof(uint16_t);
381 static ssize_t rwrap_fake_rdata_common(uint16_t type,
382 size_t rdata_size,
383 const char *key,
384 size_t remaining,
385 uint8_t **rdata_ptr)
387 uint8_t *rd = *rdata_ptr;
388 ssize_t written = 0;
390 written = ns_name_compress(key, rd, remaining, NULL, NULL);
391 if (written < 0) {
392 RWRAP_LOG(RWRAP_LOG_ERROR,
393 "Failed to compress [%s]\n", key);
394 return -1;
396 rd += written;
397 remaining -= written;
399 if (remaining < 3 * sizeof(uint16_t) + sizeof(uint32_t)) {
400 RWRAP_LOG(RWRAP_LOG_ERROR, "Buffer too small\n");
401 return -1;
404 NS_PUT16(type, rd);
405 NS_PUT16(ns_c_in, rd);
406 NS_PUT32(RWRAP_DEFAULT_FAKE_TTL, rd);
407 NS_PUT16(rdata_size, rd);
409 if (remaining < rdata_size) {
410 RWRAP_LOG(RWRAP_LOG_ERROR, "Buffer too small\n");
411 return -1;
414 *rdata_ptr = rd;
415 return written + 3 * sizeof(uint16_t) + sizeof(uint32_t) + rdata_size;
418 static ssize_t rwrap_fake_a(struct rwrap_fake_rr *rr,
419 uint8_t *answer_ptr,
420 size_t anslen)
422 uint8_t *a = answer_ptr;
423 ssize_t resp_size;
425 if (rr == NULL || rr->type != ns_t_a) {
426 RWRAP_LOG(RWRAP_LOG_ERROR,
427 "Malformed record, no or wrong value!\n");
428 return -1;
431 resp_size = rwrap_fake_rdata_common(ns_t_a, sizeof(struct in_addr), rr->key,
432 anslen, &a);
433 if (resp_size < 0) {
434 return -1;
437 memcpy(a, &rr->rrdata.a_rec, sizeof(struct in_addr));
439 return resp_size;
442 static ssize_t rwrap_fake_aaaa(struct rwrap_fake_rr *rr,
443 uint8_t *answer,
444 size_t anslen)
446 uint8_t *a = answer;
447 ssize_t resp_size;
449 if (rr == NULL || rr->type != ns_t_aaaa) {
450 RWRAP_LOG(RWRAP_LOG_ERROR,
451 "Malformed record, no or wrong value!\n");
452 return -1;
455 resp_size = rwrap_fake_rdata_common(ns_t_aaaa, sizeof(struct in6_addr),
456 rr->key, anslen, &a);
457 if (resp_size < 0) {
458 return -1;
461 memcpy(a, &rr->rrdata.aaaa_rec, sizeof(struct in6_addr));
463 return resp_size;
466 static ssize_t rwrap_fake_srv(struct rwrap_fake_rr *rr,
467 uint8_t *answer,
468 size_t anslen)
470 uint8_t *a = answer;
471 ssize_t resp_size;
472 size_t rdata_size;
473 unsigned char hostname_compressed[MAXDNAME];
474 ssize_t compressed_len;
476 if (rr == NULL || rr->type != ns_t_srv) {
477 RWRAP_LOG(RWRAP_LOG_ERROR,
478 "Malformed record, no or wrong value!\n");
479 return -1;
481 rdata_size = 3 * sizeof(uint16_t);
483 /* Prepare the data to write */
484 compressed_len = ns_name_compress(rr->rrdata.srv_rec.hostname,
485 hostname_compressed, MAXDNAME,
486 NULL, NULL);
487 if (compressed_len < 0) {
488 return -1;
490 rdata_size += compressed_len;
492 resp_size = rwrap_fake_rdata_common(ns_t_srv, rdata_size,
493 rr->key, anslen, &a);
494 if (resp_size < 0) {
495 return -1;
498 NS_PUT16(rr->rrdata.srv_rec.prio, a);
499 NS_PUT16(rr->rrdata.srv_rec.weight, a);
500 NS_PUT16(rr->rrdata.srv_rec.port, a);
501 memcpy(a, hostname_compressed, compressed_len);
503 return resp_size;
506 static ssize_t rwrap_fake_soa(struct rwrap_fake_rr *rr,
507 uint8_t *answer,
508 size_t anslen)
510 uint8_t *a = answer;
511 ssize_t resp_size;
512 size_t rdata_size;
513 unsigned char nameser_compressed[MAXDNAME];
514 ssize_t compressed_ns_len;
515 unsigned char mailbox_compressed[MAXDNAME];
516 ssize_t compressed_mb_len;
518 if (rr == NULL || rr->type != ns_t_soa) {
519 RWRAP_LOG(RWRAP_LOG_ERROR,
520 "Malformed record, no or wrong value!\n");
521 return -1;
523 rdata_size = 5 * sizeof(uint16_t);
525 compressed_ns_len = ns_name_compress(rr->rrdata.soa_rec.nameserver,
526 nameser_compressed,
527 MAXDNAME, NULL, NULL);
528 if (compressed_ns_len < 0) {
529 return -1;
531 rdata_size += compressed_ns_len;
533 compressed_mb_len = ns_name_compress(rr->rrdata.soa_rec.mailbox,
534 mailbox_compressed,
535 MAXDNAME, NULL, NULL);
536 if (compressed_mb_len < 0) {
537 return -1;
539 rdata_size += compressed_mb_len;
541 resp_size = rwrap_fake_rdata_common(ns_t_soa, rdata_size,
542 rr->key, anslen, &a);
543 if (resp_size < 0) {
544 return -1;
547 memcpy(a, nameser_compressed, compressed_ns_len);
548 a += compressed_ns_len;
549 memcpy(a, mailbox_compressed, compressed_mb_len);
550 a += compressed_mb_len;
551 NS_PUT32(rr->rrdata.soa_rec.serial, a);
552 NS_PUT32(rr->rrdata.soa_rec.refresh, a);
553 NS_PUT32(rr->rrdata.soa_rec.retry, a);
554 NS_PUT32(rr->rrdata.soa_rec.expire, a);
555 NS_PUT32(rr->rrdata.soa_rec.minimum, a);
557 return resp_size;
560 static ssize_t rwrap_fake_cname(struct rwrap_fake_rr *rr,
561 uint8_t *answer,
562 size_t anslen)
564 uint8_t *a = answer;
565 ssize_t resp_size;
566 unsigned char hostname_compressed[MAXDNAME];
567 ssize_t rdata_size;
569 if (rr == NULL || rr->type != ns_t_cname) {
570 RWRAP_LOG(RWRAP_LOG_ERROR,
571 "Malformed record, no or wrong value!\n");
572 return -1;
575 /* Prepare the data to write */
576 rdata_size = ns_name_compress(rr->rrdata.cname_rec,
577 hostname_compressed, MAXDNAME,
578 NULL, NULL);
579 if (rdata_size < 0) {
580 return -1;
583 resp_size = rwrap_fake_rdata_common(ns_t_cname, rdata_size,
584 rr->key, anslen, &a);
585 if (resp_size < 0) {
586 return -1;
589 memcpy(a, hostname_compressed, rdata_size);
591 return resp_size;
594 #define RESOLV_MATCH(line, name) \
595 (strncmp(line, name, sizeof(name) - 1) == 0 && \
596 (line[sizeof(name) - 1] == ' ' || \
597 line[sizeof(name) - 1] == '\t'))
599 #define TYPE_MATCH(type, ns_type, rec_type, str_type, key, query) \
600 ((type) == (ns_type) && \
601 (strncmp((rec_type), (str_type), sizeof(str_type)) == 0) && \
602 (strcasecmp(key, query)) == 0)
605 static int rwrap_get_record(const char *hostfile, unsigned recursion,
606 const char *query, int type,
607 struct rwrap_fake_rr *rr);
609 static int rwrap_srv_recurse(const char *hostfile, unsigned recursion,
610 const char *query, struct rwrap_fake_rr *rr)
612 int rc;
614 rc = rwrap_get_record(hostfile, recursion, query, ns_t_a, rr);
615 if (rc == 0) return 0;
617 rc = rwrap_get_record(hostfile, recursion, query, ns_t_aaaa, rr);
618 if (rc == ENOENT) rc = 0;
620 return rc;
623 static int rwrap_cname_recurse(const char *hostfile, unsigned recursion,
624 const char *query, struct rwrap_fake_rr *rr)
626 int rc;
628 rc = rwrap_get_record(hostfile, recursion, query, ns_t_a, rr);
629 if (rc == 0) return 0;
631 rc = rwrap_get_record(hostfile, recursion, query, ns_t_aaaa, rr);
632 if (rc == 0) return 0;
634 rc = rwrap_get_record(hostfile, recursion, query, ns_t_cname, rr);
635 if (rc == ENOENT) rc = 0;
637 return rc;
640 static int rwrap_get_record(const char *hostfile, unsigned recursion,
641 const char *query, int type,
642 struct rwrap_fake_rr *rr)
644 FILE *fp = NULL;
645 char buf[BUFSIZ];
646 char *key = NULL;
647 char *value = NULL;
648 int rc = ENOENT;
650 if (recursion >= RWRAP_MAX_RECURSION) {
651 RWRAP_LOG(RWRAP_LOG_ERROR, "Recursed too deep!\n");
652 return -1;
655 RWRAP_LOG(RWRAP_LOG_TRACE,
656 "Searching in fake hosts file %s\n", hostfile);
658 fp = fopen(hostfile, "r");
659 if (fp == NULL) {
660 RWRAP_LOG(RWRAP_LOG_ERROR,
661 "Opening %s failed: %s",
662 hostfile, strerror(errno));
663 return -1;
666 while (fgets(buf, sizeof(buf), fp) != NULL) {
667 char *rec_type;
668 char *q;
670 rec_type = buf;
671 key = value = NULL;
673 NEXT_KEY(rec_type, key);
674 NEXT_KEY(key, value);
676 q = value;
677 while(q[0] != '\n' && q[0] != '\0') {
678 q++;
680 q[0] = '\0';
682 if (key == NULL || value == NULL) {
683 RWRAP_LOG(RWRAP_LOG_WARN,
684 "Malformed line: not enough parts, use \"rec_type key data\n"
685 "For example \"A cwrap.org 10.10.10.10\"");
686 continue;
689 if (TYPE_MATCH(type, ns_t_a, rec_type, "A", key, query)) {
690 rc = rwrap_create_fake_a_rr(key, value, rr);
691 break;
692 } else if (TYPE_MATCH(type, ns_t_aaaa,
693 rec_type, "AAAA", key, query)) {
694 rc = rwrap_create_fake_aaaa_rr(key, value, rr);
695 break;
696 } else if (TYPE_MATCH(type, ns_t_srv,
697 rec_type, "SRV", key, query)) {
698 rc = rwrap_create_fake_srv_rr(key, value, rr);
699 if (rc == 0) {
700 rc = rwrap_srv_recurse(hostfile, recursion+1,
701 rr->rrdata.srv_rec.hostname,
702 rr + 1);
704 break;
705 } else if (TYPE_MATCH(type, ns_t_soa,
706 rec_type, "SOA", key, query)) {
707 rc = rwrap_create_fake_soa_rr(key, value, rr);
708 break;
709 } else if (TYPE_MATCH(type, ns_t_cname,
710 rec_type, "CNAME", key, query)) {
711 rc = rwrap_create_fake_cname_rr(key, value, rr);
712 if (rc == 0) {
713 rc = rwrap_cname_recurse(hostfile, recursion+1,
714 value, rr + 1);
716 break;
720 if (rc == ENOENT && recursion == 0) {
721 RWRAP_LOG(RWRAP_LOG_TRACE, "Record for [%s] not found\n", query);
722 memcpy(rr->key, key, strlen(key) + 1);
725 fclose(fp);
726 return rc;
729 static ssize_t rwrap_fake_empty(int type,
730 const char *question,
731 uint8_t *answer,
732 size_t anslen)
734 ssize_t resp_data;
735 size_t remaining = anslen;
737 resp_data = rwrap_fake_header(&answer, remaining, 0, 0);
738 if (resp_data < 0) {
739 return -1;
741 remaining -= resp_data;
743 resp_data += rwrap_fake_question(question, type, &answer, remaining);
744 if (resp_data < 0) {
745 return -1;
747 remaining -= resp_data;
749 resp_data += rwrap_fake_rdata_common(type, 0, question,
750 remaining, &answer);
751 if (resp_data < 0) {
752 return -1;
755 return resp_data;
758 static ssize_t rwrap_fake_answer(struct rwrap_fake_rr *rrs,
759 uint8_t *answer,
760 size_t anslen)
763 ssize_t resp_data;
764 size_t remaining = anslen;
766 resp_data = rwrap_fake_header(&answer, remaining, 1, 0);
767 if (resp_data < 0) {
768 return -1;
770 remaining -= resp_data;
772 resp_data += rwrap_fake_question(rrs->key, rrs->type, &answer, remaining);
773 if (resp_data < 0) {
774 return -1;
776 remaining -= resp_data;
778 switch (rrs->type) {
779 case ns_t_a:
780 resp_data += rwrap_fake_a(rrs, answer, anslen);
781 break;
782 case ns_t_aaaa:
783 resp_data += rwrap_fake_aaaa(rrs, answer, anslen);
784 break;
785 case ns_t_srv:
786 resp_data += rwrap_fake_srv(rrs, answer, anslen);
787 break;
788 case ns_t_soa:
789 resp_data += rwrap_fake_soa(rrs, answer, anslen);
790 break;
791 case ns_t_cname:
792 resp_data += rwrap_fake_cname(rrs, answer, anslen);
793 break;
794 default:
795 return -1;
798 return resp_data;
801 /* Reads in a file in the following format:
802 * TYPE RDATA
804 * Malformed entried are silently skipped.
805 * Allocates answer buffer of size anslen that has to be freed after use.
807 static int rwrap_res_fake_hosts(const char *hostfile,
808 const char *query,
809 int type,
810 unsigned char *answer,
811 size_t anslen)
813 int rc = ENOENT;
814 char *query_name = NULL;
815 size_t qlen = strlen(query);
816 struct rwrap_fake_rr rrs[RWRAP_MAX_RECURSION];
817 ssize_t resp_size;
819 RWRAP_LOG(RWRAP_LOG_TRACE,
820 "Searching in fake hosts file %s\n", hostfile);
822 if (qlen > 0 && query[qlen-1] == '.') {
823 qlen--;
826 query_name = strndup(query, qlen);
827 if (query_name == NULL) {
828 return -1;
831 rwrap_fake_rr_init(rrs, RWRAP_MAX_RECURSION);
833 rc = rwrap_get_record(hostfile, 0, query_name, type, rrs);
834 switch (rc) {
835 case 0:
836 RWRAP_LOG(RWRAP_LOG_TRACE,
837 "Found record for [%s]\n", query_name);
838 resp_size = rwrap_fake_answer(rrs, answer, anslen);
839 break;
840 case ENOENT:
841 RWRAP_LOG(RWRAP_LOG_TRACE,
842 "No record for [%s]\n", query_name);
843 resp_size = rwrap_fake_empty(type, rrs->key, answer, anslen);
844 break;
845 default:
846 RWRAP_LOG(RWRAP_LOG_ERROR,
847 "Error searching for [%s]\n", query_name);
848 free(query_name);
849 return -1;
852 switch (resp_size) {
853 case -1:
854 RWRAP_LOG(RWRAP_LOG_ERROR,
855 "Error faking answer for [%s]\n", query_name);
856 break;
857 default:
858 RWRAP_LOG(RWRAP_LOG_TRACE,
859 "Successfully faked answer for [%s]\n",
860 query_name);
861 break;
864 free(query_name);
865 return resp_size;
868 /*********************************************************
869 * RWRAP LOADING LIBC FUNCTIONS
870 *********************************************************/
872 #include <dlfcn.h>
874 struct rwrap_libc_fns {
875 int (*libc_res_init)(void);
876 int (*libc___res_init)(void);
877 int (*libc_res_ninit)(struct __res_state *state);
878 int (*libc___res_ninit)(struct __res_state *state);
879 void (*libc_res_nclose)(struct __res_state *state);
880 void (*libc___res_nclose)(struct __res_state *state);
881 void (*libc_res_close)(void);
882 void (*libc___res_close)(void);
883 int (*libc_res_nquery)(struct __res_state *state,
884 const char *dname,
885 int class,
886 int type,
887 unsigned char *answer,
888 int anslen);
889 int (*libc___res_nquery)(struct __res_state *state,
890 const char *dname,
891 int class,
892 int type,
893 unsigned char *answer,
894 int anslen);
895 int (*libc_res_nsearch)(struct __res_state *state,
896 const char *dname,
897 int class,
898 int type,
899 unsigned char *answer,
900 int anslen);
901 int (*libc___res_nsearch)(struct __res_state *state,
902 const char *dname,
903 int class,
904 int type,
905 unsigned char *answer,
906 int anslen);
909 struct rwrap {
910 void *libc_handle;
911 void *libresolv_handle;
913 bool initialised;
914 bool enabled;
916 char *socket_dir;
918 struct rwrap_libc_fns fns;
921 static struct rwrap rwrap;
923 enum rwrap_lib {
924 RWRAP_LIBC,
925 RWRAP_LIBRESOLV
928 #ifndef NDEBUG
929 static const char *rwrap_str_lib(enum rwrap_lib lib)
931 switch (lib) {
932 case RWRAP_LIBC:
933 return "libc";
934 case RWRAP_LIBRESOLV:
935 return "libresolv";
938 /* Compiler would warn us about unhandled enum value if we get here */
939 return "unknown";
941 #endif
943 static void *rwrap_load_lib_handle(enum rwrap_lib lib)
945 int flags = RTLD_LAZY;
946 void *handle = NULL;
947 int i;
949 #ifdef RTLD_DEEPBIND
950 flags |= RTLD_DEEPBIND;
951 #endif
953 switch (lib) {
954 case RWRAP_LIBRESOLV:
955 #ifdef HAVE_LIBRESOLV
956 handle = rwrap.libresolv_handle;
957 if (handle == NULL) {
958 for (i = 10; i >= 0; i--) {
959 char soname[256] = {0};
961 snprintf(soname, sizeof(soname), "libresolv.so.%d", i);
962 handle = dlopen(soname, flags);
963 if (handle != NULL) {
964 break;
968 rwrap.libresolv_handle = handle;
970 break;
971 #endif
972 /* FALL TROUGH */
973 case RWRAP_LIBC:
974 handle = rwrap.libc_handle;
975 #ifdef LIBC_SO
976 if (handle == NULL) {
977 handle = dlopen(LIBC_SO, flags);
979 rwrap.libc_handle = handle;
981 #endif
982 if (handle == NULL) {
983 for (i = 10; i >= 0; i--) {
984 char soname[256] = {0};
986 snprintf(soname, sizeof(soname), "libc.so.%d", i);
987 handle = dlopen(soname, flags);
988 if (handle != NULL) {
989 break;
993 rwrap.libc_handle = handle;
995 break;
998 if (handle == NULL) {
999 #ifdef RTLD_NEXT
1000 handle = rwrap.libc_handle = rwrap.libresolv_handle = RTLD_NEXT;
1001 #else
1002 RWRAP_LOG(RWRAP_LOG_ERROR,
1003 "Failed to dlopen library: %s\n",
1004 dlerror());
1005 exit(-1);
1006 #endif
1009 return handle;
1012 static void *_rwrap_load_lib_function(enum rwrap_lib lib, const char *fn_name)
1014 void *handle;
1015 void *func;
1017 handle = rwrap_load_lib_handle(lib);
1019 func = dlsym(handle, fn_name);
1020 if (func == NULL) {
1021 RWRAP_LOG(RWRAP_LOG_ERROR,
1022 "Failed to find %s: %s\n",
1023 fn_name, dlerror());
1024 exit(-1);
1027 RWRAP_LOG(RWRAP_LOG_TRACE,
1028 "Loaded %s from %s",
1029 fn_name, rwrap_str_lib(lib));
1030 return func;
1033 #define rwrap_load_lib_function(lib, fn_name) \
1034 if (rwrap.fns.libc_##fn_name == NULL) { \
1035 *(void **) (&rwrap.fns.libc_##fn_name) = \
1036 _rwrap_load_lib_function(lib, #fn_name); \
1040 * IMPORTANT
1042 * Functions especially from libc need to be loaded individually, you can't load
1043 * all at once or gdb will segfault at startup. The same applies to valgrind and
1044 * has probably something todo with with the linker.
1045 * So we need load each function at the point it is called the first time.
1047 #if 0
1048 static int libc_res_init(void)
1050 #if defined(HAVE_RES_INIT)
1051 rwrap_load_lib_function(RWRAP_LIBRESOLV, res_init);
1053 return rwrap.fns.libc_res_init();
1054 #elif defined(HAVE___RES_INIT)
1055 rwrap_load_lib_function(RWRAP_LIBRESOLV, __res_init);
1057 return rwrap.fns.libc___res_init();
1058 #endif
1060 #endif
1062 static int libc_res_ninit(struct __res_state *state)
1064 #if defined(HAVE_RES_NINIT)
1066 #if defined(HAVE_RES_NINIT_IN_LIBRESOLV)
1067 rwrap_load_lib_function(RWRAP_LIBRESOLV, res_ninit);
1068 #else /* HAVE_RES_NINIT_IN_LIBRESOLV */
1069 rwrap_load_lib_function(RWRAP_LIBC, res_ninit);
1070 #endif /* HAVE_RES_NINIT_IN_LIBRESOLV */
1072 return rwrap.fns.libc_res_ninit(state);
1073 #elif defined(HAVE___RES_NINIT)
1074 rwrap_load_lib_function(RWRAP_LIBC, __res_ninit);
1076 return rwrap.fns.libc___res_ninit(state);
1077 #else
1078 #error "No res_ninit function"
1079 #endif
1082 static void libc_res_nclose(struct __res_state *state)
1084 #if defined(HAVE_RES_NCLOSE)
1086 #if defined(HAVE_RES_NCLOSE_IN_LIBRESOLV)
1087 rwrap_load_lib_function(RWRAP_LIBRESOLV, res_nclose);
1088 #else /* HAVE_RES_NCLOSE_IN_LIBRESOLV */
1089 rwrap_load_lib_function(RWRAP_LIBC, res_nclose);
1090 #endif /* HAVE_RES_NCLOSE_IN_LIBRESOLV */
1092 rwrap.fns.libc_res_nclose(state);
1093 #elif defined(HAVE___RES_NCLOSE)
1094 rwrap_load_lib_function(RWRAP_LIBC, __res_nclose);
1096 rwrap.fns.libc___res_nclose(state);
1097 #else
1098 #error "No res_nclose function"
1099 #endif
1102 static int libc_res_nquery(struct __res_state *state,
1103 const char *dname,
1104 int class,
1105 int type,
1106 unsigned char *answer,
1107 int anslen)
1109 #if defined(HAVE_RES_NQUERY)
1110 rwrap_load_lib_function(RWRAP_LIBRESOLV, res_nquery);
1112 return rwrap.fns.libc_res_nquery(state,
1113 dname,
1114 class,
1115 type,
1116 answer,
1117 anslen);
1118 #elif defined(HAVE___RES_NQUERY)
1119 rwrap_load_lib_function(RWRAP_LIBRESOLV, __res_nquery);
1121 return rwrap.fns.libc___res_nquery(state,
1122 dname,
1123 class,
1124 type,
1125 answer,
1126 anslen);
1127 #else
1128 #error "No res_nquery function"
1129 #endif
1132 static int libc_res_nsearch(struct __res_state *state,
1133 const char *dname,
1134 int class,
1135 int type,
1136 unsigned char *answer,
1137 int anslen)
1139 #if defined(HAVE_RES_NSEARCH)
1140 rwrap_load_lib_function(RWRAP_LIBRESOLV, res_nsearch);
1142 return rwrap.fns.libc_res_nsearch(state,
1143 dname,
1144 class,
1145 type,
1146 answer,
1147 anslen);
1148 #elif defined(HAVE___RES_NSEARCH)
1149 rwrap_load_lib_function(RWRAP_LIBRESOLV, __res_nsearch);
1151 return rwrap.fns.libc___res_nsearch(state,
1152 dname,
1153 class,
1154 type,
1155 answer,
1156 anslen);
1157 #else
1158 #error "No res_nsearch function"
1159 #endif
1162 /****************************************************************************
1163 * RES_HELPER
1164 ***************************************************************************/
1166 static int rwrap_parse_resolv_conf(struct __res_state *state,
1167 const char *resolv_conf)
1169 FILE *fp;
1170 char buf[BUFSIZ];
1171 int nserv = 0;
1173 fp = fopen(resolv_conf, "r");
1174 if (fp == NULL) {
1175 RWRAP_LOG(RWRAP_LOG_ERROR,
1176 "Opening %s failed: %s",
1177 resolv_conf, strerror(errno));
1178 return -1;
1181 while(fgets(buf, sizeof(buf), fp) != NULL) {
1182 char *p;
1184 /* Ignore comments */
1185 if (buf[0] == '#' || buf[0] == ';') {
1186 continue;
1189 if (RESOLV_MATCH(buf, "nameserver") && nserv < MAXNS) {
1190 struct in_addr a;
1191 char *q;
1192 int ok;
1194 p = buf + strlen("nameserver");
1196 /* Skip spaces and tabs */
1197 while(isblank((int)p[0])) {
1198 p++;
1201 q = p;
1202 while(q[0] != '\n' && q[0] != '\0') {
1203 q++;
1205 q[0] = '\0';
1207 ok = inet_pton(AF_INET, p, &a);
1208 if (ok) {
1209 state->nsaddr_list[state->nscount] = (struct sockaddr_in) {
1210 .sin_family = AF_INET,
1211 .sin_addr = a,
1212 .sin_port = htons(53),
1213 .sin_zero = { 0 },
1216 state->nscount++;
1217 nserv++;
1218 } else {
1219 #ifdef HAVE_RESOLV_IPV6_NSADDRS
1220 /* IPv6 */
1221 struct in6_addr a6;
1222 ok = inet_pton(AF_INET6, p, &a6);
1223 if (ok) {
1224 struct sockaddr_in6 *sa6;
1226 sa6 = malloc(sizeof(*sa6));
1227 if (sa6 == NULL) {
1228 fclose(fp);
1229 return -1;
1232 sa6->sin6_family = AF_INET6;
1233 sa6->sin6_port = htons(53);
1234 sa6->sin6_flowinfo = 0;
1235 sa6->sin6_addr = a6;
1237 state->_u._ext.nsaddrs[state->_u._ext.nscount] = sa6;
1238 state->_u._ext.nssocks[state->_u._ext.nscount] = -1;
1239 state->_u._ext.nsmap[state->_u._ext.nscount] = MAXNS + 1;
1241 state->_u._ext.nscount++;
1242 nserv++;
1243 } else {
1244 RWRAP_LOG(RWRAP_LOG_ERROR,
1245 "Malformed DNS server");
1246 continue;
1248 #else /* !HAVE_RESOLV_IPV6_NSADDRS */
1250 * BSD uses an opaque structure to store the
1251 * IPv6 addresses. So we can not simply store
1252 * these addresses the same way as above.
1254 RWRAP_LOG(RWRAP_LOG_WARN,
1255 "resolve_wrapper does not support "
1256 "IPv6 on this platform");
1257 continue;
1258 #endif
1260 continue;
1261 } /* TODO: match other keywords */
1264 if (ferror(fp)) {
1265 RWRAP_LOG(RWRAP_LOG_ERROR,
1266 "Reading from %s failed",
1267 resolv_conf);
1268 fclose(fp);
1269 return -1;
1272 fclose(fp);
1273 return 0;
1276 /****************************************************************************
1277 * RES_NINIT
1278 ***************************************************************************/
1280 static int rwrap_res_ninit(struct __res_state *state)
1282 int rc;
1284 rc = libc_res_ninit(state);
1285 if (rc == 0) {
1286 const char *resolv_conf = getenv("RESOLV_WRAPPER_CONF");
1288 if (resolv_conf != NULL) {
1289 uint16_t i;
1291 (void)i; /* maybe unused */
1293 /* Delete name servers */
1294 state->nscount = 0;
1295 memset(state->nsaddr_list, 0, sizeof(state->nsaddr_list));
1297 state->_u._ext.nscount = 0;
1298 #ifdef HAVE_RESOLV_IPV6_NSADDRS
1299 for (i = 0; i < state->_u._ext.nscount; i++) {
1300 SAFE_FREE(state->_u._ext.nsaddrs[i]);
1302 #endif
1304 rc = rwrap_parse_resolv_conf(state, resolv_conf);
1308 return rc;
1311 #if defined(HAVE_RES_NINIT)
1312 int res_ninit(struct __res_state *state)
1313 #elif defined(HAVE___RES_NINIT)
1314 int __res_ninit(struct __res_state *state)
1315 #endif
1317 return rwrap_res_ninit(state);
1320 /****************************************************************************
1321 * RES_INIT
1322 ***************************************************************************/
1324 static struct __res_state rwrap_res_state;
1326 static int rwrap_res_init(void)
1328 int rc;
1330 rc = rwrap_res_ninit(&rwrap_res_state);
1332 return rc;
1335 #if defined(HAVE_RES_INIT)
1336 int res_init(void)
1337 #elif defined(HAVE___RES_INIT)
1338 int __res_init(void)
1339 #endif
1341 return rwrap_res_init();
1344 /****************************************************************************
1345 * RES_NCLOSE
1346 ***************************************************************************/
1348 static void rwrap_res_nclose(struct __res_state *state)
1350 #ifdef HAVE_RESOLV_IPV6_NSADDRS
1351 int i;
1352 #endif
1354 libc_res_nclose(state);
1356 #ifdef HAVE_RESOLV_IPV6_NSADDRS
1357 if (state != NULL) {
1358 for (i = 0; i < state->_u._ext.nscount; i++) {
1359 SAFE_FREE(state->_u._ext.nsaddrs[i]);
1362 #endif
1365 #if defined(HAVE_RES_NCLOSE)
1366 void res_nclose(struct __res_state *state)
1367 #elif defined(HAVE___RES_NCLOSE)
1368 void __res_nclose(struct __res_state *state)
1369 #endif
1371 rwrap_res_nclose(state);
1374 /****************************************************************************
1375 * RES_CLOSE
1376 ***************************************************************************/
1378 static void rwrap_res_close(void)
1380 rwrap_res_nclose(&rwrap_res_state);
1383 #if defined(HAVE_RES_CLOSE)
1384 void res_close(void)
1385 #elif defined(HAVE___RES_CLOSE)
1386 void __res_close(void)
1387 #endif
1389 rwrap_res_close();
1392 /****************************************************************************
1393 * RES_NQUERY
1394 ***************************************************************************/
1396 static int rwrap_res_nquery(struct __res_state *state,
1397 const char *dname,
1398 int class,
1399 int type,
1400 unsigned char *answer,
1401 int anslen)
1403 int rc;
1404 const char *fake_hosts;
1405 #ifndef NDEBUG
1406 int i;
1407 #endif
1409 RWRAP_LOG(RWRAP_LOG_TRACE,
1410 "Resolve the domain name [%s] - class=%d, type=%d",
1411 dname, class, type);
1412 #ifndef NDEBUG
1413 for (i = 0; i < state->nscount; i++) {
1414 char ip[INET6_ADDRSTRLEN];
1416 inet_ntop(AF_INET, &state->nsaddr_list[i].sin_addr, ip, sizeof(ip));
1417 RWRAP_LOG(RWRAP_LOG_TRACE,
1418 " nameserver: %s",
1419 ip);
1421 #endif
1423 fake_hosts = getenv("RESOLV_WRAPPER_HOSTS");
1424 if (fake_hosts != NULL) {
1425 rc = rwrap_res_fake_hosts(fake_hosts, dname, type, answer, anslen);
1426 } else {
1427 rc = libc_res_nquery(state, dname, class, type, answer, anslen);
1431 RWRAP_LOG(RWRAP_LOG_TRACE,
1432 "The returned response length is: %d",
1433 rc);
1435 return rc;
1438 #if defined(HAVE_RES_NQUERY)
1439 int res_nquery(struct __res_state *state,
1440 const char *dname,
1441 int class,
1442 int type,
1443 unsigned char *answer,
1444 int anslen)
1445 #elif defined(HAVE___RES_NQUERY)
1446 int __res_nquery(struct __res_state *state,
1447 const char *dname,
1448 int class,
1449 int type,
1450 unsigned char *answer,
1451 int anslen)
1452 #endif
1454 return rwrap_res_nquery(state, dname, class, type, answer, anslen);
1457 /****************************************************************************
1458 * RES_QUERY
1459 ***************************************************************************/
1461 static int rwrap_res_query(const char *dname,
1462 int class,
1463 int type,
1464 unsigned char *answer,
1465 int anslen)
1467 int rc;
1469 rc = rwrap_res_ninit(&rwrap_res_state);
1470 if (rc != 0) {
1471 return rc;
1474 rc = rwrap_res_nquery(&rwrap_res_state,
1475 dname,
1476 class,
1477 type,
1478 answer,
1479 anslen);
1481 return rc;
1484 #if defined(HAVE_RES_QUERY)
1485 int res_query(const char *dname,
1486 int class,
1487 int type,
1488 unsigned char *answer,
1489 int anslen)
1490 #elif defined(HAVE___RES_QUERY)
1491 int __res_query(const char *dname,
1492 int class,
1493 int type,
1494 unsigned char *answer,
1495 int anslen)
1496 #endif
1498 return rwrap_res_query(dname, class, type, answer, anslen);
1501 /****************************************************************************
1502 * RES_NSEARCH
1503 ***************************************************************************/
1505 static int rwrap_res_nsearch(struct __res_state *state,
1506 const char *dname,
1507 int class,
1508 int type,
1509 unsigned char *answer,
1510 int anslen)
1512 int rc;
1513 const char *fake_hosts;
1514 #ifndef NDEBUG
1515 int i;
1516 #endif
1518 RWRAP_LOG(RWRAP_LOG_TRACE,
1519 "Resolve the domain name [%s] - class=%d, type=%d",
1520 dname, class, type);
1521 #ifndef NDEBUG
1522 for (i = 0; i < state->nscount; i++) {
1523 char ip[INET6_ADDRSTRLEN];
1525 inet_ntop(AF_INET, &state->nsaddr_list[i].sin_addr, ip, sizeof(ip));
1526 RWRAP_LOG(RWRAP_LOG_TRACE,
1527 " nameserver: %s",
1528 ip);
1530 #endif
1532 fake_hosts = getenv("RESOLV_WRAPPER_HOSTS");
1533 if (fake_hosts != NULL) {
1534 rc = rwrap_res_fake_hosts(fake_hosts, dname, type, answer, anslen);
1535 } else {
1536 rc = libc_res_nsearch(state, dname, class, type, answer, anslen);
1539 RWRAP_LOG(RWRAP_LOG_TRACE,
1540 "The returned response length is: %d",
1541 rc);
1543 return rc;
1546 #if defined(HAVE_RES_NSEARCH)
1547 int res_nsearch(struct __res_state *state,
1548 const char *dname,
1549 int class,
1550 int type,
1551 unsigned char *answer,
1552 int anslen)
1553 #elif defined(HAVE___RES_NSEARCH)
1554 int __res_nsearch(struct __res_state *state,
1555 const char *dname,
1556 int class,
1557 int type,
1558 unsigned char *answer,
1559 int anslen)
1560 #endif
1562 return rwrap_res_nsearch(state, dname, class, type, answer, anslen);
1565 /****************************************************************************
1566 * RES_QUERY
1567 ***************************************************************************/
1569 static int rwrap_res_search(const char *dname,
1570 int class,
1571 int type,
1572 unsigned char *answer,
1573 int anslen)
1575 int rc;
1577 rc = rwrap_res_ninit(&rwrap_res_state);
1578 if (rc != 0) {
1579 return rc;
1582 rc = rwrap_res_nsearch(&rwrap_res_state,
1583 dname,
1584 class,
1585 type,
1586 answer,
1587 anslen);
1589 return rc;
1592 #if defined(HAVE_RES_SEARCH)
1593 int res_search(const char *dname,
1594 int class,
1595 int type,
1596 unsigned char *answer,
1597 int anslen)
1598 #elif defined(HAVE___RES_SEARCH)
1599 int __res_search(const char *dname,
1600 int class,
1601 int type,
1602 unsigned char *answer,
1603 int anslen)
1604 #endif
1606 return rwrap_res_search(dname, class, type, answer, anslen);