2 * Copyright (c) 1995 - 2003 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>
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
) {
171 status
= dn_expand(data
, end_data
, p
+ 2, host
, sizeof(host
));
176 if (status
+ 2 > size
) {
181 hostlen
= strlen(host
);
182 (*rr
)->u
.mx
= (struct mx_record
*)malloc(sizeof(struct mx_record
) +
184 if((*rr
)->u
.mx
== NULL
) {
188 (*rr
)->u
.mx
->preference
= (p
[0] << 8) | p
[1];
189 strlcpy((*rr
)->u
.mx
->domain
, host
, hostlen
+ 1);
194 status
= dn_expand(data
, end_data
, p
+ 6, host
, sizeof(host
));
199 if (status
+ 6 > size
) {
204 hostlen
= strlen(host
);
206 (struct srv_record
*)malloc(sizeof(struct srv_record
) +
208 if((*rr
)->u
.srv
== NULL
) {
212 (*rr
)->u
.srv
->priority
= (p
[0] << 8) | p
[1];
213 (*rr
)->u
.srv
->weight
= (p
[2] << 8) | p
[3];
214 (*rr
)->u
.srv
->port
= (p
[4] << 8) | p
[5];
215 strlcpy((*rr
)->u
.srv
->target
, host
, hostlen
+ 1);
219 if(size
== 0 || size
< *p
+ 1) {
223 (*rr
)->u
.txt
= (char*)malloc(*p
+ 1);
224 if((*rr
)->u
.txt
== NULL
) {
228 strncpy((*rr
)->u
.txt
, (char*)p
+ 1, *p
);
229 (*rr
)->u
.txt
[*p
] = '\0';
241 (*rr
)->u
.key
= malloc (sizeof(*(*rr
)->u
.key
) + key_len
- 1);
242 if ((*rr
)->u
.key
== NULL
) {
247 (*rr
)->u
.key
->flags
= (p
[0] << 8) | p
[1];
248 (*rr
)->u
.key
->protocol
= p
[2];
249 (*rr
)->u
.key
->algorithm
= p
[3];
250 (*rr
)->u
.key
->key_len
= key_len
;
251 memcpy ((*rr
)->u
.key
->key_data
, p
+ 4, key_len
);
255 size_t sig_len
, hostlen
;
261 status
= dn_expand (data
, end_data
, p
+ 18, host
, sizeof(host
));
266 if (status
+ 18 > size
) {
271 /* the signer name is placed after the sig_data, to make it
272 easy to free this struture; the size calculation below
273 includes the zero-termination if the structure itself.
274 don't you just love C?
276 sig_len
= size
- 18 - status
;
277 hostlen
= strlen(host
);
278 (*rr
)->u
.sig
= malloc(sizeof(*(*rr
)->u
.sig
)
279 + hostlen
+ sig_len
);
280 if ((*rr
)->u
.sig
== NULL
) {
284 (*rr
)->u
.sig
->type
= (p
[0] << 8) | p
[1];
285 (*rr
)->u
.sig
->algorithm
= p
[2];
286 (*rr
)->u
.sig
->labels
= p
[3];
287 (*rr
)->u
.sig
->orig_ttl
= (p
[4] << 24) | (p
[5] << 16)
288 | (p
[6] << 8) | p
[7];
289 (*rr
)->u
.sig
->sig_expiration
= (p
[8] << 24) | (p
[9] << 16)
290 | (p
[10] << 8) | p
[11];
291 (*rr
)->u
.sig
->sig_inception
= (p
[12] << 24) | (p
[13] << 16)
292 | (p
[14] << 8) | p
[15];
293 (*rr
)->u
.sig
->key_tag
= (p
[16] << 8) | p
[17];
294 (*rr
)->u
.sig
->sig_len
= sig_len
;
295 memcpy ((*rr
)->u
.sig
->sig_data
, p
+ 18 + status
, sig_len
);
296 (*rr
)->u
.sig
->signer
= &(*rr
)->u
.sig
->sig_data
[sig_len
];
297 strlcpy((*rr
)->u
.sig
->signer
, host
, hostlen
+ 1);
310 (*rr
)->u
.cert
= malloc (sizeof(*(*rr
)->u
.cert
) + cert_len
- 1);
311 if ((*rr
)->u
.cert
== NULL
) {
316 (*rr
)->u
.cert
->type
= (p
[0] << 8) | p
[1];
317 (*rr
)->u
.cert
->tag
= (p
[2] << 8) | p
[3];
318 (*rr
)->u
.cert
->algorithm
= p
[4];
319 (*rr
)->u
.cert
->cert_len
= cert_len
;
320 memcpy ((*rr
)->u
.cert
->cert_data
, p
+ 5, cert_len
);
324 (*rr
)->u
.data
= (unsigned char*)malloc(size
);
325 if(size
!= 0 && (*rr
)->u
.data
== NULL
) {
329 memcpy((*rr
)->u
.data
, p
, size
);
339 parse_reply(const unsigned char *data
, size_t len
)
341 const unsigned char *p
;
345 const unsigned char *end_data
= data
+ len
;
347 struct resource_record
**rr
;
349 r
= calloc(1, sizeof(*r
));
355 /* doesn't work on Crays */
356 memcpy(&r
->h
, p
, sizeof(HEADER
));
359 memcpy(&r
->h
, p
, 12); /* XXX this will probably be mostly garbage */
362 if(ntohs(r
->h
.qdcount
) != 1) {
366 status
= dn_expand(data
, end_data
, p
, host
, sizeof(host
));
371 r
->q
.domain
= strdup(host
);
372 if(r
->q
.domain
== NULL
) {
376 if (p
+ status
+ 4 > end_data
) {
381 r
->q
.type
= (p
[0] << 8 | p
[1]);
383 r
->q
.class = (p
[0] << 8 | p
[1]);
387 for(i
= 0; i
< ntohs(r
->h
.ancount
); i
++) {
388 if(parse_record(data
, end_data
, &p
, rr
) != 0) {
394 for(i
= 0; i
< ntohs(r
->h
.nscount
); i
++) {
395 if(parse_record(data
, end_data
, &p
, rr
) != 0) {
401 for(i
= 0; i
< ntohs(r
->h
.arcount
); i
++) {
402 if(parse_record(data
, end_data
, &p
, rr
) != 0) {
412 static struct dns_reply
*
413 dns_lookup_int(const char *domain
, int rr_class
, int rr_type
)
415 unsigned char reply
[1024];
417 #ifdef HAVE_RES_NSEARCH
418 struct __res_state stat
;
419 memset(&stat
, 0, sizeof(stat
));
421 return NULL
; /* is this the best we can do? */
422 #elif defined(HAVE__RES)
423 u_long old_options
= 0;
426 if (_resolve_debug
) {
427 #ifdef HAVE_RES_NSEARCH
428 stat
.options
|= RES_DEBUG
;
429 #elif defined(HAVE__RES)
430 old_options
= _res
.options
;
431 _res
.options
|= RES_DEBUG
;
433 fprintf(stderr
, "dns_lookup(%s, %d, %s)\n", domain
,
434 rr_class
, dns_type_to_string(rr_type
));
436 #ifdef HAVE_RES_NSEARCH
437 len
= res_nsearch(&stat
, domain
, rr_class
, rr_type
, reply
, sizeof(reply
));
439 len
= res_search(domain
, rr_class
, rr_type
, reply
, sizeof(reply
));
441 if (_resolve_debug
) {
442 #if defined(HAVE__RES) && !defined(HAVE_RES_NSEARCH)
443 _res
.options
= old_options
;
445 fprintf(stderr
, "dns_lookup(%s, %d, %s) --> %d\n",
446 domain
, rr_class
, dns_type_to_string(rr_type
), len
);
448 #ifdef HAVE_RES_NSEARCH
454 len
= min(len
, sizeof(reply
));
455 return parse_reply(reply
, len
);
460 dns_lookup(const char *domain
, const char *type_name
)
464 type
= dns_string_to_type(type_name
);
467 fprintf(stderr
, "dns_lookup: unknown resource type: `%s'\n",
471 return dns_lookup_int(domain
, C_IN
, type
);
475 compare_srv(const void *a
, const void *b
)
477 const struct resource_record
*const* aa
= a
, *const* bb
= b
;
479 if((*aa
)->u
.srv
->priority
== (*bb
)->u
.srv
->priority
)
480 return ((*aa
)->u
.srv
->weight
- (*bb
)->u
.srv
->weight
);
481 return ((*aa
)->u
.srv
->priority
- (*bb
)->u
.srv
->priority
);
485 #define random() rand()
488 /* try to rearrange the srv-records by the algorithm in RFC2782 */
490 dns_srv_order(struct dns_reply
*r
)
492 struct resource_record
**srvs
, **ss
, **headp
;
493 struct resource_record
*rr
;
496 #if defined(HAVE_INITSTATE) && defined(HAVE_SETSTATE)
497 int state
[256 / sizeof(int)];
501 for(rr
= r
->head
; rr
; rr
= rr
->next
)
502 if(rr
->type
== T_SRV
)
508 srvs
= malloc(num_srv
* sizeof(*srvs
));
510 return; /* XXX not much to do here */
512 /* unlink all srv-records from the linked list and put them in
514 for(ss
= srvs
, headp
= &r
->head
; *headp
; )
515 if((*headp
)->type
== T_SRV
) {
517 *headp
= (*headp
)->next
;
521 headp
= &(*headp
)->next
;
523 /* sort them by priority and weight */
524 qsort(srvs
, num_srv
, sizeof(*srvs
), compare_srv
);
526 #if defined(HAVE_INITSTATE) && defined(HAVE_SETSTATE)
527 oldstate
= initstate(time(NULL
), (char*)state
, sizeof(state
));
532 for(ss
= srvs
; ss
< srvs
+ num_srv
; ) {
534 struct resource_record
**ee
, **tt
;
535 /* find the last record with the same priority and count the
536 sum of all weights */
537 for(sum
= 0, tt
= ss
; tt
< srvs
+ num_srv
; tt
++) {
540 if((*tt
)->u
.srv
->priority
!= (*ss
)->u
.srv
->priority
)
542 sum
+= (*tt
)->u
.srv
->weight
;
545 /* ss is now the first record of this priority and ee is the
548 rnd
= random() % (sum
+ 1);
549 for(count
= 0, tt
= ss
; ; tt
++) {
552 count
+= (*tt
)->u
.srv
->weight
;
559 /* insert the selected record at the tail (of the head) of
561 (*tt
)->next
= *headp
;
563 headp
= &(*tt
)->next
;
564 sum
-= (*tt
)->u
.srv
->weight
;
566 while(ss
< ee
&& *ss
== NULL
)
571 #if defined(HAVE_INITSTATE) && defined(HAVE_SETSTATE)
578 #else /* NOT defined(HAVE_RES_SEARCH) && defined(HAVE_DN_EXPAND) */
581 dns_lookup(const char *domain
, const char *type_name
)
587 dns_free_data(struct dns_reply
*r
)
592 dns_srv_order(struct dns_reply
*r
)
600 main(int argc
, char **argv
)
603 struct resource_record
*rr
;
604 r
= dns_lookup(argv
[1], argv
[2]);
606 printf("No reply.\n");
609 if(r
->q
.type
== T_SRV
)
612 for(rr
= r
->head
; rr
;rr
=rr
->next
){
613 printf("%-30s %-5s %-6d ", rr
->domain
, dns_type_to_string(rr
->type
), rr
->ttl
);
618 printf("%s\n", (char*)rr
->u
.data
);
621 printf("%s\n", inet_ntoa(*rr
->u
.a
));
625 printf("%d %s\n", rr
->u
.mx
->preference
, rr
->u
.mx
->domain
);
629 struct srv_record
*srv
= rr
->u
.srv
;
630 printf("%d %d %d %s\n", srv
->priority
, srv
->weight
,
631 srv
->port
, srv
->target
);
635 printf("%s\n", rr
->u
.txt
);
639 struct sig_record
*sig
= rr
->u
.sig
;
640 const char *type_string
= dns_type_to_string (sig
->type
);
642 printf ("type %u (%s), algorithm %u, labels %u, orig_ttl %u, sig_expiration %u, sig_inception %u, key_tag %u, signer %s\n",
643 sig
->type
, type_string
? type_string
: "",
644 sig
->algorithm
, sig
->labels
, sig
->orig_ttl
,
645 sig
->sig_expiration
, sig
->sig_inception
, sig
->key_tag
,
650 struct key_record
*key
= rr
->u
.key
;
652 printf ("flags %u, protocol %u, algorithm %u\n",
653 key
->flags
, key
->protocol
, key
->algorithm
);