2 * Copyright (c) 1995 - 2006 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #ifdef HAVE_ARPA_NAMESER_H
39 #include <arpa/nameser.h>
48 RCSID("$Id: resolve.c 22873 2008-04-07 18:50:39Z lha $");
50 #ifdef _AIX /* AIX have broken res_nsearch() in 5.1 (5.0 also ?) */
51 #undef HAVE_RES_NSEARCH
54 #define DECL(X) {#X, rk_ns_t_##X}
78 int _resolve_debug = 0;
80 int ROKEN_LIB_FUNCTION
81 dns_string_to_type(const char *name)
83 struct stot *p = stot;
84 for(p = stot; p->name; p++)
85 if(strcasecmp(name, p->name) == 0)
90 const char * ROKEN_LIB_FUNCTION
91 dns_type_to_string(int type)
93 struct stot *p = stot;
94 for(p = stot; p->name; p++)
100 #if (defined(HAVE_RES_SEARCH) || defined(HAVE_RES_NSEARCH)) && defined(HAVE_DN_EXPAND)
103 dns_free_rr(struct resource_record *rr)
112 void ROKEN_LIB_FUNCTION
113 dns_free_data(struct dns_reply *r)
115 struct resource_record *rr;
118 for(rr = r->head; rr;){
119 struct resource_record *tmp = rr;
127 parse_record(const unsigned char *data, const unsigned char *end_data,
128 const unsigned char **pp, struct resource_record **ret_rr)
130 struct resource_record *rr;
131 int type, class, ttl;
135 const unsigned char *p = *pp;
139 status = dn_expand(data, end_data, p, host, sizeof(host));
142 if (p + status + 10 > end_data)
146 type = (p[0] << 8) | p[1];
148 class = (p[0] << 8) | p[1];
150 ttl = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
152 size = (p[0] << 8) | p[1];
155 if (p + size > end_data)
158 rr = calloc(1, sizeof(*rr));
161 rr->domain = strdup(host);
162 if(rr->domain == NULL) {
174 status = dn_expand(data, end_data, p, host, sizeof(host));
179 rr->u.txt = strdup(host);
180 if(rr->u.txt == NULL) {
189 status = dn_expand(data, end_data, p + 2, host, sizeof(host));
194 if (status + 2 > size) {
199 hostlen = strlen(host);
200 rr->u.mx = (struct mx_record*)malloc(sizeof(struct mx_record) +
202 if(rr->u.mx == NULL) {
206 rr->u.mx->preference = (p[0] << 8) | p[1];
207 strlcpy(rr->u.mx->domain, host, hostlen + 1);
212 status = dn_expand(data, end_data, p + 6, host, sizeof(host));
217 if (status + 6 > size) {
222 hostlen = strlen(host);
224 (struct srv_record*)malloc(sizeof(struct srv_record) +
226 if(rr->u.srv == NULL) {
230 rr->u.srv->priority = (p[0] << 8) | p[1];
231 rr->u.srv->weight = (p[2] << 8) | p[3];
232 rr->u.srv->port = (p[4] << 8) | p[5];
233 strlcpy(rr->u.srv->target, host, hostlen + 1);
237 if(size == 0 || size < *p + 1) {
241 rr->u.txt = (char*)malloc(*p + 1);
242 if(rr->u.txt == NULL) {
246 strncpy(rr->u.txt, (const char*)(p + 1), *p);
247 rr->u.txt[*p] = '\0';
259 rr->u.key = malloc (sizeof(*rr->u.key) + key_len - 1);
260 if (rr->u.key == NULL) {
265 rr->u.key->flags = (p[0] << 8) | p[1];
266 rr->u.key->protocol = p[2];
267 rr->u.key->algorithm = p[3];
268 rr->u.key->key_len = key_len;
269 memcpy (rr->u.key->key_data, p + 4, key_len);
273 size_t sig_len, hostlen;
279 status = dn_expand (data, end_data, p + 18, host, sizeof(host));
284 if (status + 18 > size) {
289 /* the signer name is placed after the sig_data, to make it
290 easy to free this structure; the size calculation below
291 includes the zero-termination if the structure itself.
292 don't you just love C?
294 sig_len = size - 18 - status;
295 hostlen = strlen(host);
296 rr->u.sig = malloc(sizeof(*rr->u.sig)
297 + hostlen + sig_len);
298 if (rr->u.sig == NULL) {
302 rr->u.sig->type = (p[0] << 8) | p[1];
303 rr->u.sig->algorithm = p[2];
304 rr->u.sig->labels = p[3];
305 rr->u.sig->orig_ttl = (p[4] << 24) | (p[5] << 16)
306 | (p[6] << 8) | p[7];
307 rr->u.sig->sig_expiration = (p[8] << 24) | (p[9] << 16)
308 | (p[10] << 8) | p[11];
309 rr->u.sig->sig_inception = (p[12] << 24) | (p[13] << 16)
310 | (p[14] << 8) | p[15];
311 rr->u.sig->key_tag = (p[16] << 8) | p[17];
312 rr->u.sig->sig_len = sig_len;
313 memcpy (rr->u.sig->sig_data, p + 18 + status, sig_len);
314 rr->u.sig->signer = &rr->u.sig->sig_data[sig_len];
315 strlcpy(rr->u.sig->signer, host, hostlen + 1);
319 case rk_ns_t_cert : {
328 rr->u.cert = malloc (sizeof(*rr->u.cert) + cert_len - 1);
329 if (rr->u.cert == NULL) {
334 rr->u.cert->type = (p[0] << 8) | p[1];
335 rr->u.cert->tag = (p[2] << 8) | p[3];
336 rr->u.cert->algorithm = p[4];
337 rr->u.cert->cert_len = cert_len;
338 memcpy (rr->u.cert->cert_data, p + 5, cert_len);
341 case rk_ns_t_sshfp : {
349 sshfp_len = size - 2;
351 rr->u.sshfp = malloc (sizeof(*rr->u.sshfp) + sshfp_len - 1);
352 if (rr->u.sshfp == NULL) {
357 rr->u.sshfp->algorithm = p[0];
358 rr->u.sshfp->type = p[1];
359 rr->u.sshfp->sshfp_len = sshfp_len;
360 memcpy (rr->u.sshfp->sshfp_data, p + 2, sshfp_len);
371 digest_len = size - 4;
373 rr->u.ds = malloc (sizeof(*rr->u.ds) + digest_len - 1);
374 if (rr->u.ds == NULL) {
379 rr->u.ds->key_tag = (p[0] << 8) | p[1];
380 rr->u.ds->algorithm = p[2];
381 rr->u.ds->digest_type = p[3];
382 rr->u.ds->digest_len = digest_len;
383 memcpy (rr->u.ds->digest_data, p + 4, digest_len);
387 rr->u.data = (unsigned char*)malloc(size);
388 if(size != 0 && rr->u.data == NULL) {
393 memcpy(rr->u.data, p, size);
405 parse_reply(const unsigned char *data, size_t len)
407 const unsigned char *p;
411 const unsigned char *end_data = data + len;
413 struct resource_record **rr;
415 r = calloc(1, sizeof(*r));
421 r->h.id = (p[0] << 8) | p[1];
424 r->h.flags |= rk_DNS_HEADER_RESPONSE_FLAG;
425 r->h.opcode = (p[2] >> 1) & 0xf;
427 r->h.flags |= rk_DNS_HEADER_AUTHORITIVE_ANSWER;
429 r->h.flags |= rk_DNS_HEADER_TRUNCATED_MESSAGE;
431 r->h.flags |= rk_DNS_HEADER_RECURSION_DESIRED;
433 r->h.flags |= rk_DNS_HEADER_RECURSION_AVAILABLE;
435 r->h.flags |= rk_DNS_HEADER_AUTHORITIVE_ANSWER;
437 r->h.flags |= rk_DNS_HEADER_CHECKING_DISABLED;
438 r->h.response_code = (p[3] >> 4) & 0xf;
439 r->h.qdcount = (p[4] << 8) | p[5];
440 r->h.ancount = (p[6] << 8) | p[7];
441 r->h.nscount = (p[8] << 8) | p[9];
442 r->h.arcount = (p[10] << 8) | p[11];
446 if(r->h.qdcount != 1) {
450 status = dn_expand(data, end_data, p, host, sizeof(host));
455 r->q.domain = strdup(host);
456 if(r->q.domain == NULL) {
460 if (p + status + 4 > end_data) {
465 r->q.type = (p[0] << 8 | p[1]);
467 r->q.class = (p[0] << 8 | p[1]);
471 for(i = 0; i < r->h.ancount; i++) {
472 if(parse_record(data, end_data, &p, rr) != 0) {
478 for(i = 0; i < r->h.nscount; i++) {
479 if(parse_record(data, end_data, &p, rr) != 0) {
485 for(i = 0; i < r->h.arcount; i++) {
486 if(parse_record(data, end_data, &p, rr) != 0) {
496 #ifdef HAVE_RES_NSEARCH
497 #ifdef HAVE_RES_NDESTROY
498 #define rk_res_free(x) res_ndestroy(x)
500 #define rk_res_free(x) res_nclose(x)
504 static struct dns_reply *
505 dns_lookup_int(const char *domain, int rr_class, int rr_type)
508 unsigned char *reply = NULL;
511 #ifdef HAVE_RES_NSEARCH
512 struct __res_state state;
513 memset(&state, 0, sizeof(state));
514 if(res_ninit(&state))
515 return NULL; /* is this the best we can do? */
516 #elif defined(HAVE__RES)
517 u_long old_options = 0;
529 if (_resolve_debug) {
530 #ifdef HAVE_RES_NSEARCH
531 state.options |= RES_DEBUG;
532 #elif defined(HAVE__RES)
533 old_options = _res.options;
534 _res.options |= RES_DEBUG;
536 fprintf(stderr, "dns_lookup(%s, %d, %s), buffer size %d\n", domain,
537 rr_class, dns_type_to_string(rr_type), size);
539 reply = malloc(size);
541 #ifdef HAVE_RES_NSEARCH
546 #ifdef HAVE_RES_NSEARCH
547 len = res_nsearch(&state, domain, rr_class, rr_type, reply, size);
549 len = res_search(domain, rr_class, rr_type, reply, size);
551 if (_resolve_debug) {
552 #if defined(HAVE__RES) && !defined(HAVE_RES_NSEARCH)
553 _res.options = old_options;
555 fprintf(stderr, "dns_lookup(%s, %d, %s) --> %d\n",
556 domain, rr_class, dns_type_to_string(rr_type), len);
559 #ifdef HAVE_RES_NSEARCH
565 } while (size < len && len < rk_DNS_MAX_PACKET_SIZE);
566 #ifdef HAVE_RES_NSEARCH
570 len = min(len, size);
571 r = parse_reply(reply, len);
576 struct dns_reply * ROKEN_LIB_FUNCTION
577 dns_lookup(const char *domain, const char *type_name)
581 type = dns_string_to_type(type_name);
584 fprintf(stderr, "dns_lookup: unknown resource type: `%s'\n",
588 return dns_lookup_int(domain, C_IN, type);
592 compare_srv(const void *a, const void *b)
594 const struct resource_record *const* aa = a, *const* bb = b;
596 if((*aa)->u.srv->priority == (*bb)->u.srv->priority)
597 return ((*aa)->u.srv->weight - (*bb)->u.srv->weight);
598 return ((*aa)->u.srv->priority - (*bb)->u.srv->priority);
602 #define random() rand()
605 /* try to rearrange the srv-records by the algorithm in RFC2782 */
606 void ROKEN_LIB_FUNCTION
607 dns_srv_order(struct dns_reply *r)
609 struct resource_record **srvs, **ss, **headp;
610 struct resource_record *rr;
613 #if defined(HAVE_INITSTATE) && defined(HAVE_SETSTATE)
614 int state[256 / sizeof(int)];
618 for(rr = r->head; rr; rr = rr->next)
619 if(rr->type == rk_ns_t_srv)
625 srvs = malloc(num_srv * sizeof(*srvs));
627 return; /* XXX not much to do here */
629 /* unlink all srv-records from the linked list and put them in
631 for(ss = srvs, headp = &r->head; *headp; )
632 if((*headp)->type == rk_ns_t_srv) {
634 *headp = (*headp)->next;
638 headp = &(*headp)->next;
640 /* sort them by priority and weight */
641 qsort(srvs, num_srv, sizeof(*srvs), compare_srv);
643 #if defined(HAVE_INITSTATE) && defined(HAVE_SETSTATE)
644 oldstate = initstate(time(NULL), (char*)state, sizeof(state));
649 for(ss = srvs; ss < srvs + num_srv; ) {
651 struct resource_record **ee, **tt;
652 /* find the last record with the same priority and count the
653 sum of all weights */
654 for(sum = 0, tt = ss; tt < srvs + num_srv; tt++) {
656 if((*tt)->u.srv->priority != (*ss)->u.srv->priority)
658 sum += (*tt)->u.srv->weight;
661 /* ss is now the first record of this priority and ee is the
664 rnd = random() % (sum + 1);
665 for(count = 0, tt = ss; ; tt++) {
668 count += (*tt)->u.srv->weight;
675 /* insert the selected record at the tail (of the head) of
677 (*tt)->next = *headp;
679 headp = &(*tt)->next;
680 sum -= (*tt)->u.srv->weight;
682 while(ss < ee && *ss == NULL)
687 #if defined(HAVE_INITSTATE) && defined(HAVE_SETSTATE)
694 #else /* NOT defined(HAVE_RES_SEARCH) && defined(HAVE_DN_EXPAND) */
696 struct dns_reply * ROKEN_LIB_FUNCTION
697 dns_lookup(const char *domain, const char *type_name)
702 void ROKEN_LIB_FUNCTION
703 dns_free_data(struct dns_reply *r)
707 void ROKEN_LIB_FUNCTION
708 dns_srv_order(struct dns_reply *r)