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.
133 #ifndef HAVE__GETSHORT
136 register const u_char *msgp;
138 register u_int16_t u;
145 #ifndef HAVE__GETLONG
148 register const u_char *msgp;
150 register u_int32_t u;
158 getrrsetbyname(const char *hostname, unsigned int rdclass,
159 unsigned int rdtype, unsigned int flags,
160 struct rrsetinfo **res)
162 struct __res_state *_resp = &_res;
164 struct rrsetinfo *rrset = NULL;
165 struct dns_response *response;
167 struct rdatainfo *rdata;
169 unsigned int index_ans, index_sig;
170 u_char answer[ANSWER_BUFFER_SIZE];
172 /* check for invalid class and type */
173 if (rdclass > 0xffff || rdtype > 0xffff) {
174 result = ERRSET_INVAL;
178 /* don't allow queries of class or type ANY */
179 if (rdclass == 0xff || rdtype == 0xff) {
180 result = ERRSET_INVAL;
184 /* don't allow flags yet, unimplemented */
186 result = ERRSET_INVAL;
190 /* initialize resolver */
191 if ((_resp->options & RES_INIT) == 0 && res_init() == -1) {
192 result = ERRSET_FAIL;
197 _resp->options |= RES_DEBUG;
200 #ifdef RES_USE_DNSSEC
201 /* turn on DNSSEC if EDNS0 is configured */
202 if (_resp->options & RES_USE_EDNS0)
203 _resp->options |= RES_USE_DNSSEC;
204 #endif /* RES_USE_DNSEC */
207 length = res_query(hostname, (signed int) rdclass, (signed int) rdtype,
208 answer, sizeof(answer));
212 result = ERRSET_NONAME;
215 result = ERRSET_NODATA;
218 result = ERRSET_FAIL;
224 response = parse_dns_response(answer, length);
225 if (response == NULL) {
226 result = ERRSET_FAIL;
230 if (response->header.qdcount != 1) {
231 result = ERRSET_FAIL;
235 /* initialize rrset */
236 rrset = calloc(1, sizeof(struct rrsetinfo));
238 result = ERRSET_NOMEMORY;
241 rrset->rri_rdclass = response->query->class;
242 rrset->rri_rdtype = response->query->type;
243 rrset->rri_ttl = response->answer->ttl;
244 rrset->rri_nrdatas = response->header.ancount;
246 /* check for authenticated data */
247 if (response->header.ad == 1)
248 rrset->rri_flags |= RRSET_VALIDATED;
250 /* copy name from answer section */
251 length = strlen(response->answer->name);
252 rrset->rri_name = malloc(length + 1);
253 if (rrset->rri_name == NULL) {
254 result = ERRSET_NOMEMORY;
257 strlcpy(rrset->rri_name, response->answer->name, length + 1);
260 rrset->rri_nrdatas = count_dns_rr(response->answer, rrset->rri_rdclass,
262 rrset->rri_nsigs = count_dns_rr(response->answer, rrset->rri_rdclass,
265 /* allocate memory for answers */
266 rrset->rri_rdatas = calloc(rrset->rri_nrdatas,
267 sizeof(struct rdatainfo));
268 if (rrset->rri_rdatas == NULL) {
269 result = ERRSET_NOMEMORY;
273 /* allocate memory for signatures */
274 rrset->rri_sigs = calloc(rrset->rri_nsigs, sizeof(struct rdatainfo));
275 if (rrset->rri_sigs == NULL) {
276 result = ERRSET_NOMEMORY;
280 /* copy answers & signatures */
281 for (rr = response->answer, index_ans = 0, index_sig = 0;
286 if (rr->class == rrset->rri_rdclass &&
287 rr->type == rrset->rri_rdtype)
288 rdata = &rrset->rri_rdatas[index_ans++];
290 if (rr->class == rrset->rri_rdclass &&
292 rdata = &rrset->rri_sigs[index_sig++];
295 rdata->rdi_length = rr->size;
296 rdata->rdi_data = malloc(rr->size);
298 if (rdata->rdi_data == NULL) {
299 result = ERRSET_NOMEMORY;
302 memcpy(rdata->rdi_data, rr->rdata, rr->size);
307 return (ERRSET_SUCCESS);
316 freerrset(struct rrsetinfo *rrset)
323 if (rrset->rri_rdatas) {
324 for (i = 0; i < rrset->rri_nrdatas; i++) {
325 if (rrset->rri_rdatas[i].rdi_data == NULL)
327 free(rrset->rri_rdatas[i].rdi_data);
329 free(rrset->rri_rdatas);
332 if (rrset->rri_sigs) {
333 for (i = 0; i < rrset->rri_nsigs; i++) {
334 if (rrset->rri_sigs[i].rdi_data == NULL)
336 free(rrset->rri_sigs[i].rdi_data);
338 free(rrset->rri_sigs);
342 free(rrset->rri_name);
347 * DNS response parsing routines
349 static struct dns_response *
350 parse_dns_response(const u_char *answer, int size)
352 struct dns_response *resp;
355 /* allocate memory for the response */
356 resp = calloc(1, sizeof(*resp));
360 /* initialize current pointer */
364 memcpy(&resp->header, cp, HFIXEDSZ);
367 /* fix header byte order */
368 resp->header.qdcount = ntohs(resp->header.qdcount);
369 resp->header.ancount = ntohs(resp->header.ancount);
370 resp->header.nscount = ntohs(resp->header.nscount);
371 resp->header.arcount = ntohs(resp->header.arcount);
373 /* there must be at least one query */
374 if (resp->header.qdcount < 1) {
375 free_dns_response(resp);
379 /* parse query section */
380 resp->query = parse_dns_qsection(answer, size, &cp,
381 resp->header.qdcount);
382 if (resp->header.qdcount && resp->query == NULL) {
383 free_dns_response(resp);
387 /* parse answer section */
388 resp->answer = parse_dns_rrsection(answer, size, &cp,
389 resp->header.ancount);
390 if (resp->header.ancount && resp->answer == NULL) {
391 free_dns_response(resp);
395 /* parse authority section */
396 resp->authority = parse_dns_rrsection(answer, size, &cp,
397 resp->header.nscount);
398 if (resp->header.nscount && resp->authority == NULL) {
399 free_dns_response(resp);
403 /* parse additional section */
404 resp->additional = parse_dns_rrsection(answer, size, &cp,
405 resp->header.arcount);
406 if (resp->header.arcount && resp->additional == NULL) {
407 free_dns_response(resp);
414 static struct dns_query *
415 parse_dns_qsection(const u_char *answer, int size, const u_char **cp, int count)
417 struct dns_query *head, *curr, *prev;
421 for (i = 1, head = NULL, prev = NULL; i <= count; i++, prev = curr) {
423 /* allocate and initialize struct */
424 curr = calloc(1, sizeof(struct dns_query));
426 free_dns_query(head);
435 length = dn_expand(answer, answer + size, *cp, name,
438 free_dns_query(head);
441 curr->name = strdup(name);
442 if (curr->name == NULL) {
443 free_dns_query(head);
449 curr->type = _getshort(*cp);
453 curr->class = _getshort(*cp);
460 static struct dns_rr *
461 parse_dns_rrsection(const u_char *answer, int size, const u_char **cp, int count)
463 struct dns_rr *head, *curr, *prev;
467 for (i = 1, head = NULL, prev = NULL; i <= count; i++, prev = curr) {
469 /* allocate and initialize struct */
470 curr = calloc(1, sizeof(struct dns_rr));
481 length = dn_expand(answer, answer + size, *cp, name,
487 curr->name = strdup(name);
488 if (curr->name == NULL) {
495 curr->type = _getshort(*cp);
499 curr->class = _getshort(*cp);
503 curr->ttl = _getlong(*cp);
507 curr->size = _getshort(*cp);
511 curr->rdata = malloc(curr->size);
512 if (curr->rdata == NULL) {
516 memcpy(curr->rdata, *cp, curr->size);
524 free_dns_query(struct dns_query *p)
531 free_dns_query(p->next);
536 free_dns_rr(struct dns_rr *p)
545 free_dns_rr(p->next);
550 free_dns_response(struct dns_response *p)
555 free_dns_query(p->query);
556 free_dns_rr(p->answer);
557 free_dns_rr(p->authority);
558 free_dns_rr(p->additional);
563 count_dns_rr(struct dns_rr *p, u_int16_t class, u_int16_t type)
568 if (p->class == class && p->type == type)
576 #endif /* defined(DNS) && !defined(HAVE_GETRRSETBYNAME) */