1 /* $OpenBSD: getrrsetbyname.c,v 1.7 2003/03/07 07:34:14 itojun 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.
48 #if defined(DNS) && !defined(HAVE_GETRRSETBYNAME)
50 #include "getrrsetbyname.h"
52 /* #include "thread_private.h" */
54 #define ANSWER_BUFFER_SIZE 1024*64
60 struct dns_query *next;
75 struct dns_query *query;
76 struct dns_rr *answer;
77 struct dns_rr *authority;
78 struct dns_rr *additional;
81 static struct dns_response *parse_dns_response(const u_char *, int);
82 static struct dns_query *parse_dns_qsection(const u_char *, int,
83 const u_char **, int);
84 static struct dns_rr *parse_dns_rrsection(const u_char *, int, const u_char **,
87 static void free_dns_query(struct dns_query *);
88 static void free_dns_rr(struct dns_rr *);
89 static void free_dns_response(struct dns_response *);
91 static int count_dns_rr(struct dns_rr *, u_int16_t, u_int16_t);
94 * Inline versions of get/put short/long. Pointer is advanced.
96 * These macros demonstrate the property of C whereby it can be
97 * portable or it can be elegant but rarely both.
108 #define GETSHORT(s, cp) { \
109 register u_char *t_cp = (u_char *)(cp); \
110 (s) = ((u_int16_t)t_cp[0] << 8) \
111 | ((u_int16_t)t_cp[1]) \
118 #define GETLONG(l, cp) { \
119 register u_char *t_cp = (u_char *)(cp); \
120 (l) = ((u_int32_t)t_cp[0] << 24) \
121 | ((u_int32_t)t_cp[1] << 16) \
122 | ((u_int32_t)t_cp[2] << 8) \
123 | ((u_int32_t)t_cp[3]) \
130 * Routines to insert/extract short/long's.
135 register const u_char *msgp;
137 register u_int16_t u;
145 register const u_char *msgp;
147 register u_int32_t u;
154 getrrsetbyname(const char *hostname, unsigned int rdclass,
155 unsigned int rdtype, unsigned int flags,
156 struct rrsetinfo **res)
158 struct __res_state *_resp = &_res;
160 struct rrsetinfo *rrset = NULL;
161 struct dns_response *response;
163 struct rdatainfo *rdata;
165 unsigned int index_ans, index_sig;
166 u_char answer[ANSWER_BUFFER_SIZE];
168 /* check for invalid class and type */
169 if (rdclass > 0xffff || rdtype > 0xffff) {
170 result = ERRSET_INVAL;
174 /* don't allow queries of class or type ANY */
175 if (rdclass == 0xff || rdtype == 0xff) {
176 result = ERRSET_INVAL;
180 /* don't allow flags yet, unimplemented */
182 result = ERRSET_INVAL;
186 /* initialize resolver */
187 if ((_resp->options & RES_INIT) == 0 && res_init() == -1) {
188 result = ERRSET_FAIL;
193 _resp->options |= RES_DEBUG;
196 #ifdef RES_USE_DNSSEC
197 /* turn on DNSSEC if EDNS0 is configured */
198 if (_resp->options & RES_USE_EDNS0)
199 _resp->options |= RES_USE_DNSSEC;
200 #endif /* RES_USE_DNSEC */
203 length = res_query(hostname, (signed int) rdclass, (signed int) rdtype,
204 answer, sizeof(answer));
208 result = ERRSET_NONAME;
211 result = ERRSET_NODATA;
214 result = ERRSET_FAIL;
220 response = parse_dns_response(answer, length);
221 if (response == NULL) {
222 result = ERRSET_FAIL;
226 if (response->header.qdcount != 1) {
227 result = ERRSET_FAIL;
231 /* initialize rrset */
232 rrset = calloc(1, sizeof(struct rrsetinfo));
234 result = ERRSET_NOMEMORY;
237 rrset->rri_rdclass = response->query->class;
238 rrset->rri_rdtype = response->query->type;
239 rrset->rri_ttl = response->answer->ttl;
240 rrset->rri_nrdatas = response->header.ancount;
242 /* check for authenticated data */
243 if (response->header.ad == 1)
244 rrset->rri_flags |= RRSET_VALIDATED;
246 /* copy name from answer section */
247 length = strlen(response->answer->name);
248 rrset->rri_name = malloc(length + 1);
249 if (rrset->rri_name == NULL) {
250 result = ERRSET_NOMEMORY;
253 strlcpy(rrset->rri_name, response->answer->name, length + 1);
256 rrset->rri_nrdatas = count_dns_rr(response->answer, rrset->rri_rdclass,
258 rrset->rri_nsigs = count_dns_rr(response->answer, rrset->rri_rdclass,
261 /* allocate memory for answers */
262 rrset->rri_rdatas = calloc(rrset->rri_nrdatas,
263 sizeof(struct rdatainfo));
264 if (rrset->rri_rdatas == NULL) {
265 result = ERRSET_NOMEMORY;
269 /* allocate memory for signatures */
270 rrset->rri_sigs = calloc(rrset->rri_nsigs, sizeof(struct rdatainfo));
271 if (rrset->rri_sigs == NULL) {
272 result = ERRSET_NOMEMORY;
276 /* copy answers & signatures */
277 for (rr = response->answer, index_ans = 0, index_sig = 0;
282 if (rr->class == rrset->rri_rdclass &&
283 rr->type == rrset->rri_rdtype)
284 rdata = &rrset->rri_rdatas[index_ans++];
286 if (rr->class == rrset->rri_rdclass &&
288 rdata = &rrset->rri_sigs[index_sig++];
291 rdata->rdi_length = rr->size;
292 rdata->rdi_data = malloc(rr->size);
294 if (rdata->rdi_data == NULL) {
295 result = ERRSET_NOMEMORY;
298 memcpy(rdata->rdi_data, rr->rdata, rr->size);
303 return (ERRSET_SUCCESS);
312 freerrset(struct rrsetinfo *rrset)
319 if (rrset->rri_rdatas) {
320 for (i = 0; i < rrset->rri_nrdatas; i++) {
321 if (rrset->rri_rdatas[i].rdi_data == NULL)
323 free(rrset->rri_rdatas[i].rdi_data);
325 free(rrset->rri_rdatas);
328 if (rrset->rri_sigs) {
329 for (i = 0; i < rrset->rri_nsigs; i++) {
330 if (rrset->rri_sigs[i].rdi_data == NULL)
332 free(rrset->rri_sigs[i].rdi_data);
334 free(rrset->rri_sigs);
338 free(rrset->rri_name);
343 * DNS response parsing routines
345 static struct dns_response *
346 parse_dns_response(const u_char *answer, int size)
348 struct dns_response *resp;
351 /* allocate memory for the response */
352 resp = calloc(1, sizeof(*resp));
356 /* initialize current pointer */
360 memcpy(&resp->header, cp, HFIXEDSZ);
363 /* fix header byte order */
364 resp->header.qdcount = ntohs(resp->header.qdcount);
365 resp->header.ancount = ntohs(resp->header.ancount);
366 resp->header.nscount = ntohs(resp->header.nscount);
367 resp->header.arcount = ntohs(resp->header.arcount);
369 /* there must be at least one query */
370 if (resp->header.qdcount < 1) {
371 free_dns_response(resp);
375 /* parse query section */
376 resp->query = parse_dns_qsection(answer, size, &cp,
377 resp->header.qdcount);
378 if (resp->header.qdcount && resp->query == NULL) {
379 free_dns_response(resp);
383 /* parse answer section */
384 resp->answer = parse_dns_rrsection(answer, size, &cp,
385 resp->header.ancount);
386 if (resp->header.ancount && resp->answer == NULL) {
387 free_dns_response(resp);
391 /* parse authority section */
392 resp->authority = parse_dns_rrsection(answer, size, &cp,
393 resp->header.nscount);
394 if (resp->header.nscount && resp->authority == NULL) {
395 free_dns_response(resp);
399 /* parse additional section */
400 resp->additional = parse_dns_rrsection(answer, size, &cp,
401 resp->header.arcount);
402 if (resp->header.arcount && resp->additional == NULL) {
403 free_dns_response(resp);
410 static struct dns_query *
411 parse_dns_qsection(const u_char *answer, int size, const u_char **cp, int count)
413 struct dns_query *head, *curr, *prev;
417 for (i = 1, head = NULL, prev = NULL; i <= count; i++, prev = curr) {
419 /* allocate and initialize struct */
420 curr = calloc(1, sizeof(struct dns_query));
422 free_dns_query(head);
431 length = dn_expand(answer, answer + size, *cp, name,
434 free_dns_query(head);
437 curr->name = strdup(name);
438 if (curr->name == NULL) {
439 free_dns_query(head);
445 curr->type = _getshort(*cp);
449 curr->class = _getshort(*cp);
456 static struct dns_rr *
457 parse_dns_rrsection(const u_char *answer, int size, const u_char **cp, int count)
459 struct dns_rr *head, *curr, *prev;
463 for (i = 1, head = NULL, prev = NULL; i <= count; i++, prev = curr) {
465 /* allocate and initialize struct */
466 curr = calloc(1, sizeof(struct dns_rr));
477 length = dn_expand(answer, answer + size, *cp, name,
483 curr->name = strdup(name);
484 if (curr->name == NULL) {
491 curr->type = _getshort(*cp);
495 curr->class = _getshort(*cp);
499 curr->ttl = _getlong(*cp);
503 curr->size = _getshort(*cp);
507 curr->rdata = malloc(curr->size);
508 if (curr->rdata == NULL) {
512 memcpy(curr->rdata, *cp, curr->size);
520 free_dns_query(struct dns_query *p)
527 free_dns_query(p->next);
532 free_dns_rr(struct dns_rr *p)
541 free_dns_rr(p->next);
546 free_dns_response(struct dns_response *p)
551 free_dns_query(p->query);
552 free_dns_rr(p->answer);
553 free_dns_rr(p->authority);
554 free_dns_rr(p->additional);
559 count_dns_rr(struct dns_rr *p, u_int16_t class, u_int16_t type)
564 if (p->class == class && p->type == type)
572 #endif /* defined(DNS) && !defined(HAVE_GETRRSETBYNAME) */