1 /* $NetBSD: getrrsetbyname.c,v 1.3 2006/09/28 21:22:14 christos Exp $ */
2 /* $OpenBSD: getrrsetbyname.c,v 1.10 2005/03/30 02:58:28 tedu Exp $ */
5 * Copyright (c) 2001 Jakob Schlyter. All rights reserved.
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 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 * Portions Copyright (c) 1999-2001 Internet Software Consortium.
33 * Permission to use, copy, modify, and distribute this software for any
34 * purpose with or without fee is hereby granted, provided that the above
35 * copyright notice and this permission notice appear in all copies.
37 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
38 * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
39 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
40 * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
41 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
42 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
43 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
44 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
47 /* OPENBSD ORIGINAL: lib/libc/net/getrrsetbyname.c */
50 __RCSID("$NetBSD: getrrsetbyname.c,v 1.3 2006/09/28 21:22:14 christos Exp $");
52 #ifndef HAVE_GETRRSETBYNAME
58 #include "getrrsetbyname.h"
60 #if defined(HAVE_DECL_H_ERRNO) && !HAVE_DECL_H_ERRNO
64 /* We don't need multithread support here */
65 #ifdef _THREAD_PRIVATE
66 # undef _THREAD_PRIVATE
68 #define _THREAD_PRIVATE(a,b,c) (c)
70 struct __res_state _res
;
72 #define HAVE__GETSHORT
76 /* Necessary functions and macros */
79 * Inline versions of get/put short/long. Pointer is advanced.
81 * These macros demonstrate the property of C whereby it can be
82 * portable or it can be elegant but rarely both.
93 #define GETSHORT(s, cp) { \
94 register u_char *t_cp = (u_char *)(cp); \
95 (s) = ((u_int16_t)t_cp[0] << 8) \
96 | ((u_int16_t)t_cp[1]) \
103 #define GETLONG(l, cp) { \
104 register u_char *t_cp = (u_char *)(cp); \
105 (l) = ((u_int32_t)t_cp[0] << 24) \
106 | ((u_int32_t)t_cp[1] << 16) \
107 | ((u_int32_t)t_cp[2] << 8) \
108 | ((u_int32_t)t_cp[3]) \
115 * Routines to insert/extract short/long's.
118 #ifndef HAVE__GETSHORT
121 register const u_char
*msgp
;
123 register u_int16_t u
;
128 #elif defined(HAVE_DECL__GETSHORT) && (HAVE_DECL__GETSHORT == 0)
129 u_int16_t
_getshort(register const u_char
*);
132 #ifndef HAVE__GETLONG
135 register const u_char
*msgp
;
137 register u_int32_t u
;
142 #elif defined(HAVE_DECL__GETLONG) && (HAVE_DECL__GETLONG == 0)
143 u_int32_t
_getlong(register const u_char
*);
148 #define ANSWER_BUFFER_SIZE 1024*64
154 struct dns_query
*next
;
167 struct dns_response
{
169 struct dns_query
*query
;
170 struct dns_rr
*answer
;
171 struct dns_rr
*authority
;
172 struct dns_rr
*additional
;
175 static struct dns_response
*parse_dns_response(const u_char
*, int);
176 static struct dns_query
*parse_dns_qsection(const u_char
*, int,
177 const u_char
**, int);
178 static struct dns_rr
*parse_dns_rrsection(const u_char
*, int, const u_char
**,
181 static void free_dns_query(struct dns_query
*);
182 static void free_dns_rr(struct dns_rr
*);
183 static void free_dns_response(struct dns_response
*);
185 static int count_dns_rr(struct dns_rr
*, u_int16_t
, u_int16_t
);
188 getrrsetbyname(const char *hostname
, unsigned int rdclass
,
189 unsigned int rdtype
, unsigned int flags
,
190 struct rrsetinfo
**res
)
192 struct __res_state
*_resp
= _THREAD_PRIVATE(_res
, _res
, &_res
);
194 struct rrsetinfo
*rrset
= NULL
;
195 struct dns_response
*response
= NULL
;
197 struct rdatainfo
*rdata
;
199 unsigned int index_ans
, index_sig
;
200 u_char answer
[ANSWER_BUFFER_SIZE
];
202 /* check for invalid class and type */
203 if (rdclass
> 0xffff || rdtype
> 0xffff) {
204 result
= ERRSET_INVAL
;
208 /* don't allow queries of class or type ANY */
209 if (rdclass
== 0xff || rdtype
== 0xff) {
210 result
= ERRSET_INVAL
;
214 /* don't allow flags yet, unimplemented */
216 result
= ERRSET_INVAL
;
220 /* initialize resolver */
221 if ((_resp
->options
& RES_INIT
) == 0 && res_init() == -1) {
222 result
= ERRSET_FAIL
;
227 _resp
->options
|= RES_DEBUG
;
230 #ifdef RES_USE_DNSSEC
231 /* turn on DNSSEC if EDNS0 is configured */
232 if (_resp
->options
& RES_USE_EDNS0
)
233 _resp
->options
|= RES_USE_DNSSEC
;
234 #endif /* RES_USE_DNSEC */
237 length
= res_query(hostname
, (signed int) rdclass
, (signed int) rdtype
,
238 answer
, sizeof(answer
));
242 result
= ERRSET_NONAME
;
245 result
= ERRSET_NODATA
;
248 result
= ERRSET_FAIL
;
254 response
= parse_dns_response(answer
, length
);
255 if (response
== NULL
) {
256 result
= ERRSET_FAIL
;
260 if (response
->header
.qdcount
!= 1) {
261 result
= ERRSET_FAIL
;
265 /* initialize rrset */
266 rrset
= calloc(1, sizeof(struct rrsetinfo
));
268 result
= ERRSET_NOMEMORY
;
271 rrset
->rri_rdclass
= response
->query
->class;
272 rrset
->rri_rdtype
= response
->query
->type
;
273 rrset
->rri_ttl
= response
->answer
->ttl
;
274 rrset
->rri_nrdatas
= response
->header
.ancount
;
276 #ifdef HAVE_HEADER_AD
277 /* check for authenticated data */
278 if (response
->header
.ad
== 1)
279 rrset
->rri_flags
|= RRSET_VALIDATED
;
282 /* copy name from answer section */
283 rrset
->rri_name
= strdup(response
->answer
->name
);
284 if (rrset
->rri_name
== NULL
) {
285 result
= ERRSET_NOMEMORY
;
290 rrset
->rri_nrdatas
= count_dns_rr(response
->answer
, rrset
->rri_rdclass
,
292 rrset
->rri_nsigs
= count_dns_rr(response
->answer
, rrset
->rri_rdclass
,
295 /* allocate memory for answers */
296 rrset
->rri_rdatas
= calloc(rrset
->rri_nrdatas
,
297 sizeof(struct rdatainfo
));
298 if (rrset
->rri_rdatas
== NULL
) {
299 result
= ERRSET_NOMEMORY
;
303 /* allocate memory for signatures */
304 rrset
->rri_sigs
= calloc(rrset
->rri_nsigs
, sizeof(struct rdatainfo
));
305 if (rrset
->rri_sigs
== NULL
) {
306 result
= ERRSET_NOMEMORY
;
310 /* copy answers & signatures */
311 for (rr
= response
->answer
, index_ans
= 0, index_sig
= 0;
316 if (rr
->class == rrset
->rri_rdclass
&&
317 rr
->type
== rrset
->rri_rdtype
)
318 rdata
= &rrset
->rri_rdatas
[index_ans
++];
320 if (rr
->class == rrset
->rri_rdclass
&&
322 rdata
= &rrset
->rri_sigs
[index_sig
++];
325 rdata
->rdi_length
= rr
->size
;
326 rdata
->rdi_data
= malloc(rr
->size
);
328 if (rdata
->rdi_data
== NULL
) {
329 result
= ERRSET_NOMEMORY
;
332 memcpy(rdata
->rdi_data
, rr
->rdata
, rr
->size
);
335 free_dns_response(response
);
338 return (ERRSET_SUCCESS
);
343 if (response
!= NULL
)
344 free_dns_response(response
);
349 freerrset(struct rrsetinfo
*rrset
)
356 if (rrset
->rri_rdatas
) {
357 for (i
= 0; i
< rrset
->rri_nrdatas
; i
++) {
358 if (rrset
->rri_rdatas
[i
].rdi_data
== NULL
)
360 free(rrset
->rri_rdatas
[i
].rdi_data
);
362 free(rrset
->rri_rdatas
);
365 if (rrset
->rri_sigs
) {
366 for (i
= 0; i
< rrset
->rri_nsigs
; i
++) {
367 if (rrset
->rri_sigs
[i
].rdi_data
== NULL
)
369 free(rrset
->rri_sigs
[i
].rdi_data
);
371 free(rrset
->rri_sigs
);
375 free(rrset
->rri_name
);
380 * DNS response parsing routines
382 static struct dns_response
*
383 parse_dns_response(const u_char
*answer
, int size
)
385 struct dns_response
*resp
;
388 /* allocate memory for the response */
389 resp
= calloc(1, sizeof(*resp
));
393 /* initialize current pointer */
397 memcpy(&resp
->header
, cp
, HFIXEDSZ
);
400 /* fix header byte order */
401 resp
->header
.qdcount
= ntohs(resp
->header
.qdcount
);
402 resp
->header
.ancount
= ntohs(resp
->header
.ancount
);
403 resp
->header
.nscount
= ntohs(resp
->header
.nscount
);
404 resp
->header
.arcount
= ntohs(resp
->header
.arcount
);
406 /* there must be at least one query */
407 if (resp
->header
.qdcount
< 1) {
408 free_dns_response(resp
);
412 /* parse query section */
413 resp
->query
= parse_dns_qsection(answer
, size
, &cp
,
414 resp
->header
.qdcount
);
415 if (resp
->header
.qdcount
&& resp
->query
== NULL
) {
416 free_dns_response(resp
);
420 /* parse answer section */
421 resp
->answer
= parse_dns_rrsection(answer
, size
, &cp
,
422 resp
->header
.ancount
);
423 if (resp
->header
.ancount
&& resp
->answer
== NULL
) {
424 free_dns_response(resp
);
428 /* parse authority section */
429 resp
->authority
= parse_dns_rrsection(answer
, size
, &cp
,
430 resp
->header
.nscount
);
431 if (resp
->header
.nscount
&& resp
->authority
== NULL
) {
432 free_dns_response(resp
);
436 /* parse additional section */
437 resp
->additional
= parse_dns_rrsection(answer
, size
, &cp
,
438 resp
->header
.arcount
);
439 if (resp
->header
.arcount
&& resp
->additional
== NULL
) {
440 free_dns_response(resp
);
447 static struct dns_query
*
448 parse_dns_qsection(const u_char
*answer
, int size
, const u_char
**cp
, int count
)
450 struct dns_query
*head
, *curr
, *prev
;
454 for (i
= 1, head
= NULL
, prev
= NULL
; i
<= count
; i
++, prev
= curr
) {
456 /* allocate and initialize struct */
457 curr
= calloc(1, sizeof(struct dns_query
));
459 free_dns_query(head
);
468 length
= dn_expand(answer
, answer
+ size
, *cp
, name
,
471 free_dns_query(head
);
474 curr
->name
= strdup(name
);
475 if (curr
->name
== NULL
) {
476 free_dns_query(head
);
482 curr
->type
= _getshort(*cp
);
486 curr
->class = _getshort(*cp
);
493 static struct dns_rr
*
494 parse_dns_rrsection(const u_char
*answer
, int size
, const u_char
**cp
,
497 struct dns_rr
*head
, *curr
, *prev
;
501 for (i
= 1, head
= NULL
, prev
= NULL
; i
<= count
; i
++, prev
= curr
) {
503 /* allocate and initialize struct */
504 curr
= calloc(1, sizeof(struct dns_rr
));
515 length
= dn_expand(answer
, answer
+ size
, *cp
, name
,
521 curr
->name
= strdup(name
);
522 if (curr
->name
== NULL
) {
529 curr
->type
= _getshort(*cp
);
533 curr
->class = _getshort(*cp
);
537 curr
->ttl
= _getlong(*cp
);
541 curr
->size
= _getshort(*cp
);
545 curr
->rdata
= malloc(curr
->size
);
546 if (curr
->rdata
== NULL
) {
550 memcpy(curr
->rdata
, *cp
, curr
->size
);
558 free_dns_query(struct dns_query
*p
)
565 free_dns_query(p
->next
);
570 free_dns_rr(struct dns_rr
*p
)
579 free_dns_rr(p
->next
);
584 free_dns_response(struct dns_response
*p
)
589 free_dns_query(p
->query
);
590 free_dns_rr(p
->answer
);
591 free_dns_rr(p
->authority
);
592 free_dns_rr(p
->additional
);
597 count_dns_rr(struct dns_rr
*p
, u_int16_t
class, u_int16_t type
)
602 if (p
->class == class && p
->type
== type
)
610 #endif /* !defined(HAVE_GETRRSETBYNAME) */