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
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)
163 struct rrsetinfo *rrset = NULL;
164 struct dns_response *response;
166 struct rdatainfo *rdata;
168 unsigned int index_ans, index_sig;
169 u_char answer[ANSWER_BUFFER_SIZE];
171 /* check for invalid class and type */
172 if (rdclass > 0xffff || rdtype > 0xffff) {
173 result = ERRSET_INVAL;
177 /* don't allow queries of class or type ANY */
178 if (rdclass == 0xff || rdtype == 0xff) {
179 result = ERRSET_INVAL;
183 /* don't allow flags yet, unimplemented */
185 result = ERRSET_INVAL;
189 /* initialize resolver */
190 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
191 result = ERRSET_FAIL;
196 _res.options |= RES_DEBUG;
199 #ifdef RES_USE_DNSSEC
200 /* turn on DNSSEC if EDNS0 is configured */
201 if (_res.options & RES_USE_EDNS0)
202 _res.options |= RES_USE_DNSSEC;
203 #endif /* RES_USE_DNSEC */
206 length = res_query(hostname, (signed int) rdclass, (signed int) rdtype,
207 answer, sizeof(answer));
211 result = ERRSET_NONAME;
214 result = ERRSET_NODATA;
217 result = ERRSET_FAIL;
223 response = parse_dns_response(answer, length);
224 if (response == NULL) {
225 result = ERRSET_FAIL;
229 if (response->header.qdcount != 1) {
230 result = ERRSET_FAIL;
234 /* initialize rrset */
235 rrset = calloc(1, sizeof(struct rrsetinfo));
237 result = ERRSET_NOMEMORY;
240 rrset->rri_rdclass = response->query->class;
241 rrset->rri_rdtype = response->query->type;
242 rrset->rri_ttl = response->answer->ttl;
243 rrset->rri_nrdatas = response->header.ancount;
245 #ifdef HAVE_HEADER_AD
246 /* check for authenticated data */
247 if (response->header.ad == 1)
248 rrset->rri_flags |= RRSET_VALIDATED;
251 /* copy name from answer section */
252 length = strlen(response->answer->name);
253 rrset->rri_name = malloc(length + 1);
254 if (rrset->rri_name == NULL) {
255 result = ERRSET_NOMEMORY;
258 strlcpy(rrset->rri_name, response->answer->name, length + 1);
261 rrset->rri_nrdatas = count_dns_rr(response->answer, rrset->rri_rdclass,
263 rrset->rri_nsigs = count_dns_rr(response->answer, rrset->rri_rdclass,
266 /* allocate memory for answers */
267 rrset->rri_rdatas = calloc(rrset->rri_nrdatas,
268 sizeof(struct rdatainfo));
269 if (rrset->rri_rdatas == NULL) {
270 result = ERRSET_NOMEMORY;
274 /* allocate memory for signatures */
275 rrset->rri_sigs = calloc(rrset->rri_nsigs, sizeof(struct rdatainfo));
276 if (rrset->rri_sigs == NULL) {
277 result = ERRSET_NOMEMORY;
281 /* copy answers & signatures */
282 for (rr = response->answer, index_ans = 0, index_sig = 0;
287 if (rr->class == rrset->rri_rdclass &&
288 rr->type == rrset->rri_rdtype)
289 rdata = &rrset->rri_rdatas[index_ans++];
291 if (rr->class == rrset->rri_rdclass &&
293 rdata = &rrset->rri_sigs[index_sig++];
296 rdata->rdi_length = rr->size;
297 rdata->rdi_data = malloc(rr->size);
299 if (rdata->rdi_data == NULL) {
300 result = ERRSET_NOMEMORY;
303 memcpy(rdata->rdi_data, rr->rdata, rr->size);
308 return (ERRSET_SUCCESS);
317 freerrset(struct rrsetinfo *rrset)
324 if (rrset->rri_rdatas) {
325 for (i = 0; i < rrset->rri_nrdatas; i++) {
326 if (rrset->rri_rdatas[i].rdi_data == NULL)
328 free(rrset->rri_rdatas[i].rdi_data);
330 free(rrset->rri_rdatas);
333 if (rrset->rri_sigs) {
334 for (i = 0; i < rrset->rri_nsigs; i++) {
335 if (rrset->rri_sigs[i].rdi_data == NULL)
337 free(rrset->rri_sigs[i].rdi_data);
339 free(rrset->rri_sigs);
343 free(rrset->rri_name);
348 * DNS response parsing routines
350 static struct dns_response *
351 parse_dns_response(const u_char *answer, int size)
353 struct dns_response *resp;
356 /* allocate memory for the response */
357 resp = calloc(1, sizeof(*resp));
361 /* initialize current pointer */
365 memcpy(&resp->header, cp, HFIXEDSZ);
368 /* fix header byte order */
369 resp->header.qdcount = ntohs(resp->header.qdcount);
370 resp->header.ancount = ntohs(resp->header.ancount);
371 resp->header.nscount = ntohs(resp->header.nscount);
372 resp->header.arcount = ntohs(resp->header.arcount);
374 /* there must be at least one query */
375 if (resp->header.qdcount < 1) {
376 free_dns_response(resp);
380 /* parse query section */
381 resp->query = parse_dns_qsection(answer, size, &cp,
382 resp->header.qdcount);
383 if (resp->header.qdcount && resp->query == NULL) {
384 free_dns_response(resp);
388 /* parse answer section */
389 resp->answer = parse_dns_rrsection(answer, size, &cp,
390 resp->header.ancount);
391 if (resp->header.ancount && resp->answer == NULL) {
392 free_dns_response(resp);
396 /* parse authority section */
397 resp->authority = parse_dns_rrsection(answer, size, &cp,
398 resp->header.nscount);
399 if (resp->header.nscount && resp->authority == NULL) {
400 free_dns_response(resp);
404 /* parse additional section */
405 resp->additional = parse_dns_rrsection(answer, size, &cp,
406 resp->header.arcount);
407 if (resp->header.arcount && resp->additional == NULL) {
408 free_dns_response(resp);
415 static struct dns_query *
416 parse_dns_qsection(const u_char *answer, int size, const u_char **cp, int count)
418 struct dns_query *head, *curr, *prev;
422 for (i = 1, head = NULL, prev = NULL; i <= count; i++, prev = curr) {
424 /* allocate and initialize struct */
425 curr = calloc(1, sizeof(struct dns_query));
427 free_dns_query(head);
436 length = dn_expand(answer, answer + size, *cp, name,
439 free_dns_query(head);
442 curr->name = strdup(name);
443 if (curr->name == NULL) {
444 free_dns_query(head);
450 curr->type = _getshort(*cp);
454 curr->class = _getshort(*cp);
461 static struct dns_rr *
462 parse_dns_rrsection(const u_char *answer, int size, const u_char **cp, int count)
464 struct dns_rr *head, *curr, *prev;
468 for (i = 1, head = NULL, prev = NULL; i <= count; i++, prev = curr) {
470 /* allocate and initialize struct */
471 curr = calloc(1, sizeof(struct dns_rr));
482 length = dn_expand(answer, answer + size, *cp, name,
488 curr->name = strdup(name);
489 if (curr->name == NULL) {
496 curr->type = _getshort(*cp);
500 curr->class = _getshort(*cp);
504 curr->ttl = _getlong(*cp);
508 curr->size = _getshort(*cp);
512 curr->rdata = malloc(curr->size);
513 if (curr->rdata == NULL) {
517 memcpy(curr->rdata, *cp, curr->size);
525 free_dns_query(struct dns_query *p)
532 free_dns_query(p->next);
537 free_dns_rr(struct dns_rr *p)
546 free_dns_rr(p->next);
551 free_dns_response(struct dns_response *p)
556 free_dns_query(p->query);
557 free_dns_rr(p->answer);
558 free_dns_rr(p->authority);
559 free_dns_rr(p->additional);
564 count_dns_rr(struct dns_rr *p, u_int16_t class, u_int16_t type)
569 if (p->class == class && p->type == type)
577 #endif /* !defined(HAVE_GETRRSETBYNAME) */