1 /* $OpenBSD: getrrsetbyname.c,v 1.10 2005/03/30 02:58:28 tedu 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 #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 #ifdef HAVE_HEADER_AD
266 /* check for authenticated data */
267 if (response->header.ad == 1)
268 rrset->rri_flags |= RRSET_VALIDATED;
271 /* copy name from answer section */
272 rrset->rri_name = strdup(response->answer->name);
273 if (rrset->rri_name == NULL) {
274 result = ERRSET_NOMEMORY;
279 rrset->rri_nrdatas = count_dns_rr(response->answer, rrset->rri_rdclass,
281 rrset->rri_nsigs = count_dns_rr(response->answer, rrset->rri_rdclass,
284 /* allocate memory for answers */
285 rrset->rri_rdatas = calloc(rrset->rri_nrdatas,
286 sizeof(struct rdatainfo));
287 if (rrset->rri_rdatas == NULL) {
288 result = ERRSET_NOMEMORY;
292 /* allocate memory for signatures */
293 rrset->rri_sigs = calloc(rrset->rri_nsigs, sizeof(struct rdatainfo));
294 if (rrset->rri_sigs == NULL) {
295 result = ERRSET_NOMEMORY;
299 /* copy answers & signatures */
300 for (rr = response->answer, index_ans = 0, index_sig = 0;
305 if (rr->class == rrset->rri_rdclass &&
306 rr->type == rrset->rri_rdtype)
307 rdata = &rrset->rri_rdatas[index_ans++];
309 if (rr->class == rrset->rri_rdclass &&
311 rdata = &rrset->rri_sigs[index_sig++];
314 rdata->rdi_length = rr->size;
315 rdata->rdi_data = malloc(rr->size);
317 if (rdata->rdi_data == NULL) {
318 result = ERRSET_NOMEMORY;
321 memcpy(rdata->rdi_data, rr->rdata, rr->size);
324 free_dns_response(response);
327 return (ERRSET_SUCCESS);
332 if (response != NULL)
333 free_dns_response(response);
338 freerrset(struct rrsetinfo *rrset)
345 if (rrset->rri_rdatas) {
346 for (i = 0; i < rrset->rri_nrdatas; i++) {
347 if (rrset->rri_rdatas[i].rdi_data == NULL)
349 free(rrset->rri_rdatas[i].rdi_data);
351 free(rrset->rri_rdatas);
354 if (rrset->rri_sigs) {
355 for (i = 0; i < rrset->rri_nsigs; i++) {
356 if (rrset->rri_sigs[i].rdi_data == NULL)
358 free(rrset->rri_sigs[i].rdi_data);
360 free(rrset->rri_sigs);
364 free(rrset->rri_name);
369 * DNS response parsing routines
371 static struct dns_response *
372 parse_dns_response(const u_char *answer, int size)
374 struct dns_response *resp;
377 /* allocate memory for the response */
378 resp = calloc(1, sizeof(*resp));
382 /* initialize current pointer */
386 memcpy(&resp->header, cp, HFIXEDSZ);
389 /* fix header byte order */
390 resp->header.qdcount = ntohs(resp->header.qdcount);
391 resp->header.ancount = ntohs(resp->header.ancount);
392 resp->header.nscount = ntohs(resp->header.nscount);
393 resp->header.arcount = ntohs(resp->header.arcount);
395 /* there must be at least one query */
396 if (resp->header.qdcount < 1) {
397 free_dns_response(resp);
401 /* parse query section */
402 resp->query = parse_dns_qsection(answer, size, &cp,
403 resp->header.qdcount);
404 if (resp->header.qdcount && resp->query == NULL) {
405 free_dns_response(resp);
409 /* parse answer section */
410 resp->answer = parse_dns_rrsection(answer, size, &cp,
411 resp->header.ancount);
412 if (resp->header.ancount && resp->answer == NULL) {
413 free_dns_response(resp);
417 /* parse authority section */
418 resp->authority = parse_dns_rrsection(answer, size, &cp,
419 resp->header.nscount);
420 if (resp->header.nscount && resp->authority == NULL) {
421 free_dns_response(resp);
425 /* parse additional section */
426 resp->additional = parse_dns_rrsection(answer, size, &cp,
427 resp->header.arcount);
428 if (resp->header.arcount && resp->additional == NULL) {
429 free_dns_response(resp);
436 static struct dns_query *
437 parse_dns_qsection(const u_char *answer, int size, const u_char **cp, int count)
439 struct dns_query *head, *curr, *prev;
443 for (i = 1, head = NULL, prev = NULL; i <= count; i++, prev = curr) {
445 /* allocate and initialize struct */
446 curr = calloc(1, sizeof(struct dns_query));
448 free_dns_query(head);
457 length = dn_expand(answer, answer + size, *cp, name,
460 free_dns_query(head);
463 curr->name = strdup(name);
464 if (curr->name == NULL) {
465 free_dns_query(head);
471 curr->type = _getshort(*cp);
475 curr->class = _getshort(*cp);
482 static struct dns_rr *
483 parse_dns_rrsection(const u_char *answer, int size, const u_char **cp,
486 struct dns_rr *head, *curr, *prev;
490 for (i = 1, head = NULL, prev = NULL; i <= count; i++, prev = curr) {
492 /* allocate and initialize struct */
493 curr = calloc(1, sizeof(struct dns_rr));
504 length = dn_expand(answer, answer + size, *cp, name,
510 curr->name = strdup(name);
511 if (curr->name == NULL) {
518 curr->type = _getshort(*cp);
522 curr->class = _getshort(*cp);
526 curr->ttl = _getlong(*cp);
530 curr->size = _getshort(*cp);
534 curr->rdata = malloc(curr->size);
535 if (curr->rdata == NULL) {
539 memcpy(curr->rdata, *cp, curr->size);
547 free_dns_query(struct dns_query *p)
554 free_dns_query(p->next);
559 free_dns_rr(struct dns_rr *p)
568 free_dns_rr(p->next);
573 free_dns_response(struct dns_response *p)
578 free_dns_query(p->query);
579 free_dns_rr(p->answer);
580 free_dns_rr(p->authority);
581 free_dns_rr(p->additional);
586 count_dns_rr(struct dns_rr *p, u_int16_t class, u_int16_t type)
591 if (p->class == class && p->type == type)
599 #endif /* !defined(HAVE_GETRRSETBYNAME) */