2 * Copyright (c) 1995 - 2002 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,v 1.36.4.1 2002/10/21 14:48:15 joda Exp $");
50 #undef HAVE_RES_NSEARCH
51 #if (defined(HAVE_RES_SEARCH) || defined(HAVE_RES_NSEARCH)) && defined(HAVE_DN_EXPAND)
53 #define DECL(X) {#X, T_##X}
74 int _resolve_debug = 0;
77 dns_string_to_type(const char *name)
79 struct stot *p = stot;
80 for(p = stot; p->name; p++)
81 if(strcasecmp(name, p->name) == 0)
87 dns_type_to_string(int type)
89 struct stot *p = stot;
90 for(p = stot; p->name; p++)
97 dns_free_data(struct dns_reply *r)
99 struct resource_record *rr;
102 for(rr = r->head; rr;){
103 struct resource_record *tmp = rr;
115 parse_record(const unsigned char *data, const unsigned char *end_data,
116 const unsigned char **pp, struct resource_record **rr)
118 int type, class, ttl, size;
121 const unsigned char *p = *pp;
122 status = dn_expand(data, end_data, p, host, sizeof(host));
125 if (p + status + 10 > end_data)
128 type = (p[0] << 8) | p[1];
130 class = (p[0] << 8) | p[1];
132 ttl = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
134 size = (p[0] << 8) | p[1];
137 if (p + size > end_data)
140 *rr = calloc(1, sizeof(**rr));
143 (*rr)->domain = strdup(host);
144 if((*rr)->domain == NULL) {
149 (*rr)->class = class;
156 status = dn_expand(data, end_data, p, host, sizeof(host));
161 (*rr)->u.txt = strdup(host);
162 if((*rr)->u.txt == NULL) {
169 status = dn_expand(data, end_data, p + 2, host, sizeof(host));
174 if (status + 2 > size) {
179 (*rr)->u.mx = (struct mx_record*)malloc(sizeof(struct mx_record) +
181 if((*rr)->u.mx == NULL) {
185 (*rr)->u.mx->preference = (p[0] << 8) | p[1];
186 strcpy((*rr)->u.mx->domain, host);
190 status = dn_expand(data, end_data, p + 6, host, sizeof(host));
195 if (status + 6 > size) {
201 (struct srv_record*)malloc(sizeof(struct srv_record) +
203 if((*rr)->u.srv == NULL) {
207 (*rr)->u.srv->priority = (p[0] << 8) | p[1];
208 (*rr)->u.srv->weight = (p[2] << 8) | p[3];
209 (*rr)->u.srv->port = (p[4] << 8) | p[5];
210 strcpy((*rr)->u.srv->target, host);
214 if(size == 0 || size < *p + 1) {
218 (*rr)->u.txt = (char*)malloc(*p + 1);
219 if((*rr)->u.txt == NULL) {
223 strncpy((*rr)->u.txt, (char*)p + 1, *p);
224 (*rr)->u.txt[*p] = '\0';
236 (*rr)->u.key = malloc (sizeof(*(*rr)->u.key) + key_len - 1);
237 if ((*rr)->u.key == NULL) {
242 (*rr)->u.key->flags = (p[0] << 8) | p[1];
243 (*rr)->u.key->protocol = p[2];
244 (*rr)->u.key->algorithm = p[3];
245 (*rr)->u.key->key_len = key_len;
246 memcpy ((*rr)->u.key->key_data, p + 4, key_len);
256 status = dn_expand (data, end_data, p + 18, host, sizeof(host));
261 if (status + 18 > size) {
266 /* the signer name is placed after the sig_data, to make it
267 easy to free this struture; the size calculation below
268 includes the zero-termination if the structure itself.
269 don't you just love C?
271 sig_len = size - 18 - status;
272 (*rr)->u.sig = malloc(sizeof(*(*rr)->u.sig)
273 + strlen(host) + sig_len);
274 if ((*rr)->u.sig == NULL) {
278 (*rr)->u.sig->type = (p[0] << 8) | p[1];
279 (*rr)->u.sig->algorithm = p[2];
280 (*rr)->u.sig->labels = p[3];
281 (*rr)->u.sig->orig_ttl = (p[4] << 24) | (p[5] << 16)
282 | (p[6] << 8) | p[7];
283 (*rr)->u.sig->sig_expiration = (p[8] << 24) | (p[9] << 16)
284 | (p[10] << 8) | p[11];
285 (*rr)->u.sig->sig_inception = (p[12] << 24) | (p[13] << 16)
286 | (p[14] << 8) | p[15];
287 (*rr)->u.sig->key_tag = (p[16] << 8) | p[17];
288 (*rr)->u.sig->sig_len = sig_len;
289 memcpy ((*rr)->u.sig->sig_data, p + 18 + status, sig_len);
290 (*rr)->u.sig->signer = &(*rr)->u.sig->sig_data[sig_len];
291 strcpy((*rr)->u.sig->signer, host);
304 (*rr)->u.cert = malloc (sizeof(*(*rr)->u.cert) + cert_len - 1);
305 if ((*rr)->u.cert == NULL) {
310 (*rr)->u.cert->type = (p[0] << 8) | p[1];
311 (*rr)->u.cert->tag = (p[2] << 8) | p[3];
312 (*rr)->u.cert->algorithm = p[4];
313 (*rr)->u.cert->cert_len = cert_len;
314 memcpy ((*rr)->u.cert->cert_data, p + 5, cert_len);
318 (*rr)->u.data = (unsigned char*)malloc(size);
319 if(size != 0 && (*rr)->u.data == NULL) {
323 memcpy((*rr)->u.data, p, size);
333 parse_reply(const unsigned char *data, size_t len)
335 const unsigned char *p;
339 const unsigned char *end_data = data + len;
341 struct resource_record **rr;
343 r = calloc(1, sizeof(*r));
349 /* doesn't work on Crays */
350 memcpy(&r->h, p, sizeof(HEADER));
353 memcpy(&r->h, p, 12); /* XXX this will probably be mostly garbage */
356 if(ntohs(r->h.qdcount) != 1) {
360 status = dn_expand(data, end_data, p, host, sizeof(host));
365 r->q.domain = strdup(host);
366 if(r->q.domain == NULL) {
370 if (p + status + 4 > end_data) {
375 r->q.type = (p[0] << 8 | p[1]);
377 r->q.class = (p[0] << 8 | p[1]);
381 for(i = 0; i < ntohs(r->h.ancount); i++) {
382 if(parse_record(data, end_data, &p, rr) != 0) {
388 for(i = 0; i < ntohs(r->h.nscount); i++) {
389 if(parse_record(data, end_data, &p, rr) != 0) {
395 for(i = 0; i < ntohs(r->h.arcount); i++) {
396 if(parse_record(data, end_data, &p, rr) != 0) {
406 static struct dns_reply *
407 dns_lookup_int(const char *domain, int rr_class, int rr_type)
409 unsigned char reply[1024];
411 #ifdef HAVE_RES_NSEARCH
412 struct __res_state stat;
413 memset(&stat, 0, sizeof(stat));
415 return NULL; /* is this the best we can do? */
416 #elif defined(HAVE__RES)
417 u_long old_options = 0;
420 if (_resolve_debug) {
421 #ifdef HAVE_RES_NSEARCH
422 stat.options |= RES_DEBUG;
423 #elif defined(HAVE__RES)
424 old_options = _res.options;
425 _res.options |= RES_DEBUG;
427 fprintf(stderr, "dns_lookup(%s, %d, %s)\n", domain,
428 rr_class, dns_type_to_string(rr_type));
430 #ifdef HAVE_RES_NSEARCH
431 len = res_nsearch(&stat, domain, rr_class, rr_type, reply, sizeof(reply));
433 len = res_search(domain, rr_class, rr_type, reply, sizeof(reply));
435 if (_resolve_debug) {
436 #if defined(HAVE__RES) && !defined(HAVE_RES_NSEARCH)
437 _res.options = old_options;
439 fprintf(stderr, "dns_lookup(%s, %d, %s) --> %d\n",
440 domain, rr_class, dns_type_to_string(rr_type), len);
442 #ifdef HAVE_RES_NSEARCH
448 len = min(len, sizeof(reply));
449 return parse_reply(reply, len);
454 dns_lookup(const char *domain, const char *type_name)
458 type = dns_string_to_type(type_name);
461 fprintf(stderr, "dns_lookup: unknown resource type: `%s'\n",
465 return dns_lookup_int(domain, C_IN, type);
469 compare_srv(const void *a, const void *b)
471 const struct resource_record *const* aa = a, *const* bb = b;
473 if((*aa)->u.srv->priority == (*bb)->u.srv->priority)
474 return ((*aa)->u.srv->weight - (*bb)->u.srv->weight);
475 return ((*aa)->u.srv->priority - (*bb)->u.srv->priority);
479 #define random() rand()
482 /* try to rearrange the srv-records by the algorithm in RFC2782 */
484 dns_srv_order(struct dns_reply *r)
486 struct resource_record **srvs, **ss, **headp;
487 struct resource_record *rr;
490 #if defined(HAVE_INITSTATE) && defined(HAVE_SETSTATE)
491 int state[256 / sizeof(int)];
495 for(rr = r->head; rr; rr = rr->next)
496 if(rr->type == T_SRV)
502 srvs = malloc(num_srv * sizeof(*srvs));
504 return; /* XXX not much to do here */
506 /* unlink all srv-records from the linked list and put them in
508 for(ss = srvs, headp = &r->head; *headp; )
509 if((*headp)->type == T_SRV) {
511 *headp = (*headp)->next;
515 headp = &(*headp)->next;
517 /* sort them by priority and weight */
518 qsort(srvs, num_srv, sizeof(*srvs), compare_srv);
520 #if defined(HAVE_INITSTATE) && defined(HAVE_SETSTATE)
521 oldstate = initstate(time(NULL), (char*)state, sizeof(state));
526 for(ss = srvs; ss < srvs + num_srv; ) {
528 struct resource_record **ee, **tt;
529 /* find the last record with the same priority and count the
530 sum of all weights */
531 for(sum = 0, tt = ss; tt < srvs + num_srv; tt++) {
534 if((*tt)->u.srv->priority != (*ss)->u.srv->priority)
536 sum += (*tt)->u.srv->weight;
539 /* ss is now the first record of this priority and ee is the
542 rnd = random() % (sum + 1);
543 for(count = 0, tt = ss; ; tt++) {
546 count += (*tt)->u.srv->weight;
553 /* insert the selected record at the tail (of the head) of
555 (*tt)->next = *headp;
557 headp = &(*tt)->next;
558 sum -= (*tt)->u.srv->weight;
560 while(ss < ee && *ss == NULL)
565 #if defined(HAVE_INITSTATE) && defined(HAVE_SETSTATE)
572 #else /* NOT defined(HAVE_RES_SEARCH) && defined(HAVE_DN_EXPAND) */
575 dns_lookup(const char *domain, const char *type_name)
581 dns_free_data(struct dns_reply *r)
586 dns_srv_order(struct dns_reply *r)
594 main(int argc, char **argv)
597 struct resource_record *rr;
598 r = dns_lookup(argv[1], argv[2]);
600 printf("No reply.\n");
603 if(r->q.type == T_SRV)
606 for(rr = r->head; rr;rr=rr->next){
607 printf("%-30s %-5s %-6d ", rr->domain, dns_type_to_string(rr->type), rr->ttl);
612 printf("%s\n", (char*)rr->u.data);
615 printf("%s\n", inet_ntoa(*rr->u.a));
619 printf("%d %s\n", rr->u.mx->preference, rr->u.mx->domain);
623 struct srv_record *srv = rr->u.srv;
624 printf("%d %d %d %s\n", srv->priority, srv->weight,
625 srv->port, srv->target);
629 printf("%s\n", rr->u.txt);
633 struct sig_record *sig = rr->u.sig;
634 const char *type_string = dns_type_to_string (sig->type);
636 printf ("type %u (%s), algorithm %u, labels %u, orig_ttl %u, sig_expiration %u, sig_inception %u, key_tag %u, signer %s\n",
637 sig->type, type_string ? type_string : "",
638 sig->algorithm, sig->labels, sig->orig_ttl,
639 sig->sig_expiration, sig->sig_inception, sig->key_tag,
644 struct key_record *key = rr->u.key;
646 printf ("flags %u, protocol %u, algorithm %u\n",
647 key->flags, key->protocol, key->algorithm);