1 /* OPENBSD ORIGINAL: lib/libc/net/getrrsetbyname.c */
3 /* $OpenBSD: getrrsetbyname.c,v 1.7 2003/03/07 07:34:14 itojun 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 #define ANSWER_BUFFER_SIZE 1024*64
56 #if defined(HAVE_DECL_H_ERRNO) && !HAVE_DECL_H_ERRNO
64 struct dns_query *next;
79 struct dns_query *query;
80 struct dns_rr *answer;
81 struct dns_rr *authority;
82 struct dns_rr *additional;
85 static struct dns_response *parse_dns_response(const u_char *, int);
86 static struct dns_query *parse_dns_qsection(const u_char *, int,
87 const u_char **, int);
88 static struct dns_rr *parse_dns_rrsection(const u_char *, int, const u_char **,
91 static void free_dns_query(struct dns_query *);
92 static void free_dns_rr(struct dns_rr *);
93 static void free_dns_response(struct dns_response *);
95 static int count_dns_rr(struct dns_rr *, u_int16_t, u_int16_t);
98 * Inline versions of get/put short/long. Pointer is advanced.
100 * These macros demonstrate the property of C whereby it can be
101 * portable or it can be elegant but rarely both.
112 #define GETSHORT(s, cp) { \
113 register u_char *t_cp = (u_char *)(cp); \
114 (s) = ((u_int16_t)t_cp[0] << 8) \
115 | ((u_int16_t)t_cp[1]) \
122 #define GETLONG(l, cp) { \
123 register u_char *t_cp = (u_char *)(cp); \
124 (l) = ((u_int32_t)t_cp[0] << 24) \
125 | ((u_int32_t)t_cp[1] << 16) \
126 | ((u_int32_t)t_cp[2] << 8) \
127 | ((u_int32_t)t_cp[3]) \
134 * Routines to insert/extract short/long's.
137 #ifndef HAVE__GETSHORT
140 register const u_char *msgp;
142 register u_int16_t u;
147 #elif defined(HAVE_DECL__GETSHORT) && (HAVE_DECL__GETSHORT == 0)
148 u_int16_t _getshort(register const u_char *);
151 #ifndef HAVE__GETLONG
154 register const u_char *msgp;
156 register u_int32_t u;
161 #elif defined(HAVE_DECL__GETLONG) && (HAVE_DECL__GETLONG == 0)
162 u_int32_t _getlong(register const u_char *);
166 getrrsetbyname(const char *hostname, unsigned int rdclass,
167 unsigned int rdtype, unsigned int flags,
168 struct rrsetinfo **res)
171 struct rrsetinfo *rrset = NULL;
172 struct dns_response *response;
174 struct rdatainfo *rdata;
176 unsigned int index_ans, index_sig;
177 u_char answer[ANSWER_BUFFER_SIZE];
179 /* check for invalid class and type */
180 if (rdclass > 0xffff || rdtype > 0xffff) {
181 result = ERRSET_INVAL;
185 /* don't allow queries of class or type ANY */
186 if (rdclass == 0xff || rdtype == 0xff) {
187 result = ERRSET_INVAL;
191 /* don't allow flags yet, unimplemented */
193 result = ERRSET_INVAL;
197 /* initialize resolver */
198 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
199 result = ERRSET_FAIL;
204 _res.options |= RES_DEBUG;
207 #ifdef RES_USE_DNSSEC
208 /* turn on DNSSEC if EDNS0 is configured */
209 if (_res.options & RES_USE_EDNS0)
210 _res.options |= RES_USE_DNSSEC;
211 #endif /* RES_USE_DNSEC */
214 length = res_query(hostname, (signed int) rdclass, (signed int) rdtype,
215 answer, sizeof(answer));
219 result = ERRSET_NONAME;
222 result = ERRSET_NODATA;
225 result = ERRSET_FAIL;
231 response = parse_dns_response(answer, length);
232 if (response == NULL) {
233 result = ERRSET_FAIL;
237 if (response->header.qdcount != 1) {
238 result = ERRSET_FAIL;
242 /* initialize rrset */
243 rrset = calloc(1, sizeof(struct rrsetinfo));
245 result = ERRSET_NOMEMORY;
248 rrset->rri_rdclass = response->query->class;
249 rrset->rri_rdtype = response->query->type;
250 rrset->rri_ttl = response->answer->ttl;
251 rrset->rri_nrdatas = response->header.ancount;
253 #ifdef HAVE_HEADER_AD
254 /* check for authenticated data */
255 if (response->header.ad == 1)
256 rrset->rri_flags |= RRSET_VALIDATED;
259 /* copy name from answer section */
260 length = strlen(response->answer->name);
261 rrset->rri_name = malloc(length + 1);
262 if (rrset->rri_name == NULL) {
263 result = ERRSET_NOMEMORY;
266 strlcpy(rrset->rri_name, response->answer->name, length + 1);
269 rrset->rri_nrdatas = count_dns_rr(response->answer, rrset->rri_rdclass,
271 rrset->rri_nsigs = count_dns_rr(response->answer, rrset->rri_rdclass,
274 /* allocate memory for answers */
275 rrset->rri_rdatas = calloc(rrset->rri_nrdatas,
276 sizeof(struct rdatainfo));
277 if (rrset->rri_rdatas == NULL) {
278 result = ERRSET_NOMEMORY;
282 /* allocate memory for signatures */
283 rrset->rri_sigs = calloc(rrset->rri_nsigs, sizeof(struct rdatainfo));
284 if (rrset->rri_nsigs > 0 && rrset->rri_sigs == NULL) {
285 result = ERRSET_NOMEMORY;
289 /* copy answers & signatures */
290 for (rr = response->answer, index_ans = 0, index_sig = 0;
295 if (rr->class == rrset->rri_rdclass &&
296 rr->type == rrset->rri_rdtype)
297 rdata = &rrset->rri_rdatas[index_ans++];
299 if (rr->class == rrset->rri_rdclass &&
301 rdata = &rrset->rri_sigs[index_sig++];
304 rdata->rdi_length = rr->size;
305 rdata->rdi_data = malloc(rr->size);
307 if (rdata->rdi_data == NULL) {
308 result = ERRSET_NOMEMORY;
311 memcpy(rdata->rdi_data, rr->rdata, rr->size);
316 return (ERRSET_SUCCESS);
325 freerrset(struct rrsetinfo *rrset)
332 if (rrset->rri_rdatas) {
333 for (i = 0; i < rrset->rri_nrdatas; i++) {
334 if (rrset->rri_rdatas[i].rdi_data == NULL)
336 free(rrset->rri_rdatas[i].rdi_data);
338 free(rrset->rri_rdatas);
341 if (rrset->rri_sigs) {
342 for (i = 0; i < rrset->rri_nsigs; i++) {
343 if (rrset->rri_sigs[i].rdi_data == NULL)
345 free(rrset->rri_sigs[i].rdi_data);
347 free(rrset->rri_sigs);
351 free(rrset->rri_name);
356 * DNS response parsing routines
358 static struct dns_response *
359 parse_dns_response(const u_char *answer, int size)
361 struct dns_response *resp;
364 /* allocate memory for the response */
365 resp = calloc(1, sizeof(*resp));
369 /* initialize current pointer */
373 memcpy(&resp->header, cp, HFIXEDSZ);
376 /* fix header byte order */
377 resp->header.qdcount = ntohs(resp->header.qdcount);
378 resp->header.ancount = ntohs(resp->header.ancount);
379 resp->header.nscount = ntohs(resp->header.nscount);
380 resp->header.arcount = ntohs(resp->header.arcount);
382 /* there must be at least one query */
383 if (resp->header.qdcount < 1) {
384 free_dns_response(resp);
388 /* parse query section */
389 resp->query = parse_dns_qsection(answer, size, &cp,
390 resp->header.qdcount);
391 if (resp->header.qdcount && resp->query == NULL) {
392 free_dns_response(resp);
396 /* parse answer section */
397 resp->answer = parse_dns_rrsection(answer, size, &cp,
398 resp->header.ancount);
399 if (resp->header.ancount && resp->answer == NULL) {
400 free_dns_response(resp);
404 /* parse authority section */
405 resp->authority = parse_dns_rrsection(answer, size, &cp,
406 resp->header.nscount);
407 if (resp->header.nscount && resp->authority == NULL) {
408 free_dns_response(resp);
412 /* parse additional section */
413 resp->additional = parse_dns_rrsection(answer, size, &cp,
414 resp->header.arcount);
415 if (resp->header.arcount && resp->additional == NULL) {
416 free_dns_response(resp);
423 static struct dns_query *
424 parse_dns_qsection(const u_char *answer, int size, const u_char **cp, int count)
426 struct dns_query *head, *curr, *prev;
430 for (i = 1, head = NULL, prev = NULL; i <= count; i++, prev = curr) {
432 /* allocate and initialize struct */
433 curr = calloc(1, sizeof(struct dns_query));
435 free_dns_query(head);
444 length = dn_expand(answer, answer + size, *cp, name,
447 free_dns_query(head);
450 curr->name = strdup(name);
451 if (curr->name == NULL) {
452 free_dns_query(head);
458 curr->type = _getshort(*cp);
462 curr->class = _getshort(*cp);
469 static struct dns_rr *
470 parse_dns_rrsection(const u_char *answer, int size, const u_char **cp, int count)
472 struct dns_rr *head, *curr, *prev;
476 for (i = 1, head = NULL, prev = NULL; i <= count; i++, prev = curr) {
478 /* allocate and initialize struct */
479 curr = calloc(1, sizeof(struct dns_rr));
490 length = dn_expand(answer, answer + size, *cp, name,
496 curr->name = strdup(name);
497 if (curr->name == NULL) {
504 curr->type = _getshort(*cp);
508 curr->class = _getshort(*cp);
512 curr->ttl = _getlong(*cp);
516 curr->size = _getshort(*cp);
520 curr->rdata = malloc(curr->size);
521 if (curr->rdata == NULL) {
525 memcpy(curr->rdata, *cp, curr->size);
533 free_dns_query(struct dns_query *p)
540 free_dns_query(p->next);
545 free_dns_rr(struct dns_rr *p)
554 free_dns_rr(p->next);
559 free_dns_response(struct dns_response *p)
564 free_dns_query(p->query);
565 free_dns_rr(p->answer);
566 free_dns_rr(p->authority);
567 free_dns_rr(p->additional);
572 count_dns_rr(struct dns_rr *p, u_int16_t class, u_int16_t type)
577 if (p->class == class && p->type == type)
585 #endif /* !defined(HAVE_GETRRSETBYNAME) */