1 /* $OpenBSD: getrrsetbyname.c,v 1.11 2007/10/11 18:36:41 jakob Exp $ */
4 * Copyright (c) 2001 Jakob Schlyter. All rights reserved.
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 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * Portions Copyright (c) 1999-2001 Internet Software Consortium.
32 * Permission to use, copy, modify, and distribute this software for any
33 * purpose with or without fee is hereby granted, provided that the above
34 * copyright notice and this permission notice appear in all copies.
36 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
37 * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
39 * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
40 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
41 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
42 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
43 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
46 /* OPENBSD ORIGINAL: lib/libc/net/getrrsetbyname.c */
50 #if !defined (HAVE_GETRRSETBYNAME) && !defined (HAVE_LDNS)
55 #include <netinet/in.h>
56 #include <arpa/inet.h>
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 #ifndef HAVE__RES_EXTERN
71 struct __res_state _res;
74 /* Necessary functions and macros */
77 * Inline versions of get/put short/long. Pointer is advanced.
79 * These macros demonstrate the property of C whereby it can be
80 * portable or it can be elegant but rarely both.
91 #define GETSHORT(s, cp) { \
92 u_char *t_cp = (u_char *)(cp); \
93 (s) = ((u_int16_t)t_cp[0] << 8) \
94 | ((u_int16_t)t_cp[1]) \
101 #define GETLONG(l, cp) { \
102 u_char *t_cp = (u_char *)(cp); \
103 (l) = ((u_int32_t)t_cp[0] << 24) \
104 | ((u_int32_t)t_cp[1] << 16) \
105 | ((u_int32_t)t_cp[2] << 8) \
106 | ((u_int32_t)t_cp[3]) \
113 * If the system doesn't have _getshort/_getlong or that are not exactly what
114 * we need then use local replacements, avoiding name collisions.
116 #if !defined(HAVE__GETSHORT) || !defined(HAVE__GETLONG) || \
117 !defined(HAVE_DECL__GETSHORT) || HAVE_DECL__GETSHORT == 0 || \
118 !defined(HAVE_DECL__GETLONG) || HAVE_DECL__GETLONG == 0
125 # define _getshort(x) (_ssh_compat_getshort(x))
126 # define _getlong(x) (_ssh_compat_getlong(x))
128 * Routines to insert/extract short/long's.
131 _getshort(const u_char *msgp)
140 _getlong(const u_char *msgp)
147 #endif /* missing _getshort/_getlong */
151 #define ANSWER_BUFFER_SIZE 0xffff
157 struct dns_query *next;
170 struct dns_response {
172 struct dns_query *query;
173 struct dns_rr *answer;
174 struct dns_rr *authority;
175 struct dns_rr *additional;
178 static struct dns_response *parse_dns_response(const u_char *, int);
179 static struct dns_query *parse_dns_qsection(const u_char *, int,
180 const u_char **, int);
181 static struct dns_rr *parse_dns_rrsection(const u_char *, int, const u_char **,
184 static void free_dns_query(struct dns_query *);
185 static void free_dns_rr(struct dns_rr *);
186 static void free_dns_response(struct dns_response *);
188 static int count_dns_rr(struct dns_rr *, u_int16_t, u_int16_t);
191 getrrsetbyname(const char *hostname, unsigned int rdclass,
192 unsigned int rdtype, unsigned int flags,
193 struct rrsetinfo **res)
195 struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res);
197 struct rrsetinfo *rrset = NULL;
198 struct dns_response *response = NULL;
200 struct rdatainfo *rdata;
202 unsigned int index_ans, index_sig;
203 u_char answer[ANSWER_BUFFER_SIZE];
205 /* check for invalid class and type */
206 if (rdclass > 0xffff || rdtype > 0xffff) {
207 result = ERRSET_INVAL;
211 /* don't allow queries of class or type ANY */
212 if (rdclass == 0xff || rdtype == 0xff) {
213 result = ERRSET_INVAL;
217 /* don't allow flags yet, unimplemented */
219 result = ERRSET_INVAL;
223 /* initialize resolver */
224 if ((_resp->options & RES_INIT) == 0 && res_init() == -1) {
225 result = ERRSET_FAIL;
230 _resp->options |= RES_DEBUG;
233 #ifdef RES_USE_DNSSEC
234 /* turn on DNSSEC if EDNS0 is configured */
235 if (_resp->options & RES_USE_EDNS0)
236 _resp->options |= RES_USE_DNSSEC;
237 #endif /* RES_USE_DNSEC */
240 length = res_query(hostname, (signed int) rdclass, (signed int) rdtype,
241 answer, sizeof(answer));
245 result = ERRSET_NONAME;
248 result = ERRSET_NODATA;
251 result = ERRSET_FAIL;
257 response = parse_dns_response(answer, length);
258 if (response == NULL) {
259 result = ERRSET_FAIL;
263 if (response->header.qdcount != 1) {
264 result = ERRSET_FAIL;
268 /* initialize rrset */
269 rrset = calloc(1, sizeof(struct rrsetinfo));
271 result = ERRSET_NOMEMORY;
274 rrset->rri_rdclass = response->query->class;
275 rrset->rri_rdtype = response->query->type;
276 rrset->rri_ttl = response->answer->ttl;
277 rrset->rri_nrdatas = response->header.ancount;
279 #ifdef HAVE_HEADER_AD
280 /* check for authenticated data */
281 if (response->header.ad == 1)
282 rrset->rri_flags |= RRSET_VALIDATED;
285 /* copy name from answer section */
286 rrset->rri_name = strdup(response->answer->name);
287 if (rrset->rri_name == NULL) {
288 result = ERRSET_NOMEMORY;
293 rrset->rri_nrdatas = count_dns_rr(response->answer, rrset->rri_rdclass,
295 rrset->rri_nsigs = count_dns_rr(response->answer, rrset->rri_rdclass,
298 /* allocate memory for answers */
299 rrset->rri_rdatas = calloc(rrset->rri_nrdatas,
300 sizeof(struct rdatainfo));
301 if (rrset->rri_rdatas == NULL) {
302 result = ERRSET_NOMEMORY;
306 /* allocate memory for signatures */
307 if (rrset->rri_nsigs > 0) {
308 rrset->rri_sigs = calloc(rrset->rri_nsigs, sizeof(struct rdatainfo));
309 if (rrset->rri_sigs == NULL) {
310 result = ERRSET_NOMEMORY;
315 /* copy answers & signatures */
316 for (rr = response->answer, index_ans = 0, index_sig = 0;
321 if (rr->class == rrset->rri_rdclass &&
322 rr->type == rrset->rri_rdtype)
323 rdata = &rrset->rri_rdatas[index_ans++];
325 if (rr->class == rrset->rri_rdclass &&
327 rdata = &rrset->rri_sigs[index_sig++];
330 rdata->rdi_length = rr->size;
331 rdata->rdi_data = malloc(rr->size);
333 if (rdata->rdi_data == NULL) {
334 result = ERRSET_NOMEMORY;
337 memcpy(rdata->rdi_data, rr->rdata, rr->size);
340 free_dns_response(response);
343 return (ERRSET_SUCCESS);
348 if (response != NULL)
349 free_dns_response(response);
354 freerrset(struct rrsetinfo *rrset)
361 if (rrset->rri_rdatas) {
362 for (i = 0; i < rrset->rri_nrdatas; i++) {
363 if (rrset->rri_rdatas[i].rdi_data == NULL)
365 free(rrset->rri_rdatas[i].rdi_data);
367 free(rrset->rri_rdatas);
370 if (rrset->rri_sigs) {
371 for (i = 0; i < rrset->rri_nsigs; i++) {
372 if (rrset->rri_sigs[i].rdi_data == NULL)
374 free(rrset->rri_sigs[i].rdi_data);
376 free(rrset->rri_sigs);
380 free(rrset->rri_name);
385 * DNS response parsing routines
387 static struct dns_response *
388 parse_dns_response(const u_char *answer, int size)
390 struct dns_response *resp;
396 /* allocate memory for the response */
397 resp = calloc(1, sizeof(*resp));
401 /* initialize current pointer */
405 memcpy(&resp->header, cp, HFIXEDSZ);
408 /* fix header byte order */
409 resp->header.qdcount = ntohs(resp->header.qdcount);
410 resp->header.ancount = ntohs(resp->header.ancount);
411 resp->header.nscount = ntohs(resp->header.nscount);
412 resp->header.arcount = ntohs(resp->header.arcount);
414 /* there must be at least one query */
415 if (resp->header.qdcount < 1) {
416 free_dns_response(resp);
420 /* parse query section */
421 resp->query = parse_dns_qsection(answer, size, &cp,
422 resp->header.qdcount);
423 if (resp->header.qdcount && resp->query == NULL) {
424 free_dns_response(resp);
428 /* parse answer section */
429 resp->answer = parse_dns_rrsection(answer, size, &cp,
430 resp->header.ancount);
431 if (resp->header.ancount && resp->answer == NULL) {
432 free_dns_response(resp);
436 /* parse authority section */
437 resp->authority = parse_dns_rrsection(answer, size, &cp,
438 resp->header.nscount);
439 if (resp->header.nscount && resp->authority == NULL) {
440 free_dns_response(resp);
444 /* parse additional section */
445 resp->additional = parse_dns_rrsection(answer, size, &cp,
446 resp->header.arcount);
447 if (resp->header.arcount && resp->additional == NULL) {
448 free_dns_response(resp);
455 static struct dns_query *
456 parse_dns_qsection(const u_char *answer, int size, const u_char **cp, int count)
458 struct dns_query *head, *curr, *prev;
464 if (*cp + need > answer + size) \
468 for (i = 1, head = NULL, prev = NULL; i <= count; i++, prev = curr) {
469 if (*cp >= answer + size) {
471 free_dns_query(head);
474 /* allocate and initialize struct */
475 curr = calloc(1, sizeof(struct dns_query));
484 length = dn_expand(answer, answer + size, *cp, name,
487 free_dns_query(head);
490 curr->name = strdup(name);
491 if (curr->name == NULL) {
492 free_dns_query(head);
500 curr->type = _getshort(*cp);
505 curr->class = _getshort(*cp);
513 static struct dns_rr *
514 parse_dns_rrsection(const u_char *answer, int size, const u_char **cp,
517 struct dns_rr *head, *curr, *prev;
523 if (*cp + need > answer + size) \
527 for (i = 1, head = NULL, prev = NULL; i <= count; i++, prev = curr) {
528 if (*cp >= answer + size) {
534 /* allocate and initialize struct */
535 curr = calloc(1, sizeof(struct dns_rr));
544 length = dn_expand(answer, answer + size, *cp, name,
550 curr->name = strdup(name);
551 if (curr->name == NULL) {
560 curr->type = _getshort(*cp);
565 curr->class = _getshort(*cp);
570 curr->ttl = _getlong(*cp);
575 curr->size = _getshort(*cp);
580 curr->rdata = malloc(curr->size);
581 if (curr->rdata == NULL) {
585 memcpy(curr->rdata, *cp, curr->size);
594 free_dns_query(struct dns_query *p)
601 free_dns_query(p->next);
606 free_dns_rr(struct dns_rr *p)
615 free_dns_rr(p->next);
620 free_dns_response(struct dns_response *p)
625 free_dns_query(p->query);
626 free_dns_rr(p->answer);
627 free_dns_rr(p->authority);
628 free_dns_rr(p->additional);
633 count_dns_rr(struct dns_rr *p, u_int16_t class, u_int16_t type)
638 if (p->class == class && p->type == type)
646 #endif /* !defined (HAVE_GETRRSETBYNAME) && !defined (HAVE_LDNS) */