1 /* OPENBSD ORIGINAL: lib/libc/net/getrrsetbyname.c */
3 /* $OpenBSD: getrrsetbyname.c,v 1.10 2005/03/30 02:58:28 tedu Exp $ */
6 * Copyright (c) 2001 Jakob Schlyter. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * Portions Copyright (c) 1999-2001 Internet Software Consortium.
34 * Permission to use, copy, modify, and distribute this software for any
35 * purpose with or without fee is hereby granted, provided that the above
36 * copyright notice and this permission notice appear in all copies.
38 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
39 * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
40 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
41 * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
42 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
43 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
44 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
45 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
50 #ifndef HAVE_GETRRSETBYNAME
52 #include "getrrsetbyname.h"
54 #if defined(HAVE_DECL_H_ERRNO) && !HAVE_DECL_H_ERRNO
58 /* We don't need multithread support here */
59 #ifdef _THREAD_PRIVATE
60 # undef _THREAD_PRIVATE
62 #define _THREAD_PRIVATE(a,b,c) (c)
63 struct __res_state _res;
65 /* Necessary functions and macros */
68 * Inline versions of get/put short/long. Pointer is advanced.
70 * These macros demonstrate the property of C whereby it can be
71 * portable or it can be elegant but rarely both.
82 #define GETSHORT(s, cp) { \
83 register u_char *t_cp = (u_char *)(cp); \
84 (s) = ((u_int16_t)t_cp[0] << 8) \
85 | ((u_int16_t)t_cp[1]) \
92 #define GETLONG(l, cp) { \
93 register u_char *t_cp = (u_char *)(cp); \
94 (l) = ((u_int32_t)t_cp[0] << 24) \
95 | ((u_int32_t)t_cp[1] << 16) \
96 | ((u_int32_t)t_cp[2] << 8) \
97 | ((u_int32_t)t_cp[3]) \
104 * Routines to insert/extract short/long's.
107 #ifndef HAVE__GETSHORT
110 register const u_char *msgp;
112 register u_int16_t u;
117 #elif defined(HAVE_DECL__GETSHORT) && (HAVE_DECL__GETSHORT == 0)
118 u_int16_t _getshort(register const u_char *);
121 #ifndef HAVE__GETLONG
124 register const u_char *msgp;
126 register u_int32_t u;
131 #elif defined(HAVE_DECL__GETLONG) && (HAVE_DECL__GETLONG == 0)
132 u_int32_t _getlong(register const u_char *);
137 #define ANSWER_BUFFER_SIZE 1024*64
143 struct dns_query *next;
156 struct dns_response {
158 struct dns_query *query;
159 struct dns_rr *answer;
160 struct dns_rr *authority;
161 struct dns_rr *additional;
164 static struct dns_response *parse_dns_response(const u_char *, int);
165 static struct dns_query *parse_dns_qsection(const u_char *, int,
166 const u_char **, int);
167 static struct dns_rr *parse_dns_rrsection(const u_char *, int, const u_char **,
170 static void free_dns_query(struct dns_query *);
171 static void free_dns_rr(struct dns_rr *);
172 static void free_dns_response(struct dns_response *);
174 static int count_dns_rr(struct dns_rr *, u_int16_t, u_int16_t);
177 getrrsetbyname(const char *hostname, unsigned int rdclass,
178 unsigned int rdtype, unsigned int flags,
179 struct rrsetinfo **res)
181 struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res);
183 struct rrsetinfo *rrset = NULL;
184 struct dns_response *response = NULL;
186 struct rdatainfo *rdata;
188 unsigned int index_ans, index_sig;
189 u_char answer[ANSWER_BUFFER_SIZE];
191 /* check for invalid class and type */
192 if (rdclass > 0xffff || rdtype > 0xffff) {
193 result = ERRSET_INVAL;
197 /* don't allow queries of class or type ANY */
198 if (rdclass == 0xff || rdtype == 0xff) {
199 result = ERRSET_INVAL;
203 /* don't allow flags yet, unimplemented */
205 result = ERRSET_INVAL;
209 /* initialize resolver */
210 if ((_resp->options & RES_INIT) == 0 && res_init() == -1) {
211 result = ERRSET_FAIL;
216 _resp->options |= RES_DEBUG;
219 #ifdef RES_USE_DNSSEC
220 /* turn on DNSSEC if EDNS0 is configured */
221 if (_resp->options & RES_USE_EDNS0)
222 _resp->options |= RES_USE_DNSSEC;
223 #endif /* RES_USE_DNSEC */
226 length = res_query(hostname, (signed int) rdclass, (signed int) rdtype,
227 answer, sizeof(answer));
231 result = ERRSET_NONAME;
234 result = ERRSET_NODATA;
237 result = ERRSET_FAIL;
243 response = parse_dns_response(answer, length);
244 if (response == NULL) {
245 result = ERRSET_FAIL;
249 if (response->header.qdcount != 1) {
250 result = ERRSET_FAIL;
254 /* initialize rrset */
255 rrset = calloc(1, sizeof(struct rrsetinfo));
257 result = ERRSET_NOMEMORY;
260 rrset->rri_rdclass = response->query->class;
261 rrset->rri_rdtype = response->query->type;
262 rrset->rri_ttl = response->answer->ttl;
263 rrset->rri_nrdatas = response->header.ancount;
265 /* check for authenticated data */
266 if (response->header.ad == 1)
267 rrset->rri_flags |= RRSET_VALIDATED;
269 /* copy name from answer section */
270 rrset->rri_name = strdup(response->answer->name);
271 if (rrset->rri_name == NULL) {
272 result = ERRSET_NOMEMORY;
277 rrset->rri_nrdatas = count_dns_rr(response->answer, rrset->rri_rdclass,
279 rrset->rri_nsigs = count_dns_rr(response->answer, rrset->rri_rdclass,
282 /* allocate memory for answers */
283 rrset->rri_rdatas = calloc(rrset->rri_nrdatas,
284 sizeof(struct rdatainfo));
285 if (rrset->rri_rdatas == NULL) {
286 result = ERRSET_NOMEMORY;
290 /* allocate memory for signatures */
291 rrset->rri_sigs = calloc(rrset->rri_nsigs, sizeof(struct rdatainfo));
292 if (rrset->rri_sigs == NULL) {
293 result = ERRSET_NOMEMORY;
297 /* copy answers & signatures */
298 for (rr = response->answer, index_ans = 0, index_sig = 0;
303 if (rr->class == rrset->rri_rdclass &&
304 rr->type == rrset->rri_rdtype)
305 rdata = &rrset->rri_rdatas[index_ans++];
307 if (rr->class == rrset->rri_rdclass &&
309 rdata = &rrset->rri_sigs[index_sig++];
312 rdata->rdi_length = rr->size;
313 rdata->rdi_data = malloc(rr->size);
315 if (rdata->rdi_data == NULL) {
316 result = ERRSET_NOMEMORY;
319 memcpy(rdata->rdi_data, rr->rdata, rr->size);
322 free_dns_response(response);
325 return (ERRSET_SUCCESS);
330 if (response != NULL)
331 free_dns_response(response);
336 freerrset(struct rrsetinfo *rrset)
343 if (rrset->rri_rdatas) {
344 for (i = 0; i < rrset->rri_nrdatas; i++) {
345 if (rrset->rri_rdatas[i].rdi_data == NULL)
347 free(rrset->rri_rdatas[i].rdi_data);
349 free(rrset->rri_rdatas);
352 if (rrset->rri_sigs) {
353 for (i = 0; i < rrset->rri_nsigs; i++) {
354 if (rrset->rri_sigs[i].rdi_data == NULL)
356 free(rrset->rri_sigs[i].rdi_data);
358 free(rrset->rri_sigs);
362 free(rrset->rri_name);
367 * DNS response parsing routines
369 static struct dns_response *
370 parse_dns_response(const u_char *answer, int size)
372 struct dns_response *resp;
375 /* allocate memory for the response */
376 resp = calloc(1, sizeof(*resp));
380 /* initialize current pointer */
384 memcpy(&resp->header, cp, HFIXEDSZ);
387 /* fix header byte order */
388 resp->header.qdcount = ntohs(resp->header.qdcount);
389 resp->header.ancount = ntohs(resp->header.ancount);
390 resp->header.nscount = ntohs(resp->header.nscount);
391 resp->header.arcount = ntohs(resp->header.arcount);
393 /* there must be at least one query */
394 if (resp->header.qdcount < 1) {
395 free_dns_response(resp);
399 /* parse query section */
400 resp->query = parse_dns_qsection(answer, size, &cp,
401 resp->header.qdcount);
402 if (resp->header.qdcount && resp->query == NULL) {
403 free_dns_response(resp);
407 /* parse answer section */
408 resp->answer = parse_dns_rrsection(answer, size, &cp,
409 resp->header.ancount);
410 if (resp->header.ancount && resp->answer == NULL) {
411 free_dns_response(resp);
415 /* parse authority section */
416 resp->authority = parse_dns_rrsection(answer, size, &cp,
417 resp->header.nscount);
418 if (resp->header.nscount && resp->authority == NULL) {
419 free_dns_response(resp);
423 /* parse additional section */
424 resp->additional = parse_dns_rrsection(answer, size, &cp,
425 resp->header.arcount);
426 if (resp->header.arcount && resp->additional == NULL) {
427 free_dns_response(resp);
434 static struct dns_query *
435 parse_dns_qsection(const u_char *answer, int size, const u_char **cp, int count)
437 struct dns_query *head, *curr, *prev;
441 for (i = 1, head = NULL, prev = NULL; i <= count; i++, prev = curr) {
443 /* allocate and initialize struct */
444 curr = calloc(1, sizeof(struct dns_query));
446 free_dns_query(head);
455 length = dn_expand(answer, answer + size, *cp, name,
458 free_dns_query(head);
461 curr->name = strdup(name);
462 if (curr->name == NULL) {
463 free_dns_query(head);
469 curr->type = _getshort(*cp);
473 curr->class = _getshort(*cp);
480 static struct dns_rr *
481 parse_dns_rrsection(const u_char *answer, int size, const u_char **cp,
484 struct dns_rr *head, *curr, *prev;
488 for (i = 1, head = NULL, prev = NULL; i <= count; i++, prev = curr) {
490 /* allocate and initialize struct */
491 curr = calloc(1, sizeof(struct dns_rr));
502 length = dn_expand(answer, answer + size, *cp, name,
508 curr->name = strdup(name);
509 if (curr->name == NULL) {
516 curr->type = _getshort(*cp);
520 curr->class = _getshort(*cp);
524 curr->ttl = _getlong(*cp);
528 curr->size = _getshort(*cp);
532 curr->rdata = malloc(curr->size);
533 if (curr->rdata == NULL) {
537 memcpy(curr->rdata, *cp, curr->size);
545 free_dns_query(struct dns_query *p)
552 free_dns_query(p->next);
557 free_dns_rr(struct dns_rr *p)
566 free_dns_rr(p->next);
571 free_dns_response(struct dns_response *p)
576 free_dns_query(p->query);
577 free_dns_rr(p->answer);
578 free_dns_rr(p->authority);
579 free_dns_rr(p->additional);
584 count_dns_rr(struct dns_rr *p, u_int16_t class, u_int16_t type)
589 if (p->class == class && p->type == type)
597 #endif /* !defined(HAVE_GETRRSETBYNAME) */