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;
149 #ifndef HAVE__GETLONG
152 register const u_char *msgp;
154 register u_int32_t u;
162 getrrsetbyname(const char *hostname, unsigned int rdclass,
163 unsigned int rdtype, unsigned int flags,
164 struct rrsetinfo **res)
167 struct rrsetinfo *rrset = NULL;
168 struct dns_response *response;
170 struct rdatainfo *rdata;
172 unsigned int index_ans, index_sig;
173 u_char answer[ANSWER_BUFFER_SIZE];
175 /* check for invalid class and type */
176 if (rdclass > 0xffff || rdtype > 0xffff) {
177 result = ERRSET_INVAL;
181 /* don't allow queries of class or type ANY */
182 if (rdclass == 0xff || rdtype == 0xff) {
183 result = ERRSET_INVAL;
187 /* don't allow flags yet, unimplemented */
189 result = ERRSET_INVAL;
193 /* initialize resolver */
194 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
195 result = ERRSET_FAIL;
200 _res.options |= RES_DEBUG;
203 #ifdef RES_USE_DNSSEC
204 /* turn on DNSSEC if EDNS0 is configured */
205 if (_res.options & RES_USE_EDNS0)
206 _res.options |= RES_USE_DNSSEC;
207 #endif /* RES_USE_DNSEC */
210 length = res_query(hostname, (signed int) rdclass, (signed int) rdtype,
211 answer, sizeof(answer));
215 result = ERRSET_NONAME;
218 result = ERRSET_NODATA;
221 result = ERRSET_FAIL;
227 response = parse_dns_response(answer, length);
228 if (response == NULL) {
229 result = ERRSET_FAIL;
233 if (response->header.qdcount != 1) {
234 result = ERRSET_FAIL;
238 /* initialize rrset */
239 rrset = calloc(1, sizeof(struct rrsetinfo));
241 result = ERRSET_NOMEMORY;
244 rrset->rri_rdclass = response->query->class;
245 rrset->rri_rdtype = response->query->type;
246 rrset->rri_ttl = response->answer->ttl;
247 rrset->rri_nrdatas = response->header.ancount;
249 #ifdef HAVE_HEADER_AD
250 /* check for authenticated data */
251 if (response->header.ad == 1)
252 rrset->rri_flags |= RRSET_VALIDATED;
255 /* copy name from answer section */
256 length = strlen(response->answer->name);
257 rrset->rri_name = malloc(length + 1);
258 if (rrset->rri_name == NULL) {
259 result = ERRSET_NOMEMORY;
262 strlcpy(rrset->rri_name, response->answer->name, length + 1);
265 rrset->rri_nrdatas = count_dns_rr(response->answer, rrset->rri_rdclass,
267 rrset->rri_nsigs = count_dns_rr(response->answer, rrset->rri_rdclass,
270 /* allocate memory for answers */
271 rrset->rri_rdatas = calloc(rrset->rri_nrdatas,
272 sizeof(struct rdatainfo));
273 if (rrset->rri_rdatas == NULL) {
274 result = ERRSET_NOMEMORY;
278 /* allocate memory for signatures */
279 rrset->rri_sigs = calloc(rrset->rri_nsigs, sizeof(struct rdatainfo));
280 if (rrset->rri_sigs == NULL) {
281 result = ERRSET_NOMEMORY;
285 /* copy answers & signatures */
286 for (rr = response->answer, index_ans = 0, index_sig = 0;
291 if (rr->class == rrset->rri_rdclass &&
292 rr->type == rrset->rri_rdtype)
293 rdata = &rrset->rri_rdatas[index_ans++];
295 if (rr->class == rrset->rri_rdclass &&
297 rdata = &rrset->rri_sigs[index_sig++];
300 rdata->rdi_length = rr->size;
301 rdata->rdi_data = malloc(rr->size);
303 if (rdata->rdi_data == NULL) {
304 result = ERRSET_NOMEMORY;
307 memcpy(rdata->rdi_data, rr->rdata, rr->size);
312 return (ERRSET_SUCCESS);
321 freerrset(struct rrsetinfo *rrset)
328 if (rrset->rri_rdatas) {
329 for (i = 0; i < rrset->rri_nrdatas; i++) {
330 if (rrset->rri_rdatas[i].rdi_data == NULL)
332 free(rrset->rri_rdatas[i].rdi_data);
334 free(rrset->rri_rdatas);
337 if (rrset->rri_sigs) {
338 for (i = 0; i < rrset->rri_nsigs; i++) {
339 if (rrset->rri_sigs[i].rdi_data == NULL)
341 free(rrset->rri_sigs[i].rdi_data);
343 free(rrset->rri_sigs);
347 free(rrset->rri_name);
352 * DNS response parsing routines
354 static struct dns_response *
355 parse_dns_response(const u_char *answer, int size)
357 struct dns_response *resp;
360 /* allocate memory for the response */
361 resp = calloc(1, sizeof(*resp));
365 /* initialize current pointer */
369 memcpy(&resp->header, cp, HFIXEDSZ);
372 /* fix header byte order */
373 resp->header.qdcount = ntohs(resp->header.qdcount);
374 resp->header.ancount = ntohs(resp->header.ancount);
375 resp->header.nscount = ntohs(resp->header.nscount);
376 resp->header.arcount = ntohs(resp->header.arcount);
378 /* there must be at least one query */
379 if (resp->header.qdcount < 1) {
380 free_dns_response(resp);
384 /* parse query section */
385 resp->query = parse_dns_qsection(answer, size, &cp,
386 resp->header.qdcount);
387 if (resp->header.qdcount && resp->query == NULL) {
388 free_dns_response(resp);
392 /* parse answer section */
393 resp->answer = parse_dns_rrsection(answer, size, &cp,
394 resp->header.ancount);
395 if (resp->header.ancount && resp->answer == NULL) {
396 free_dns_response(resp);
400 /* parse authority section */
401 resp->authority = parse_dns_rrsection(answer, size, &cp,
402 resp->header.nscount);
403 if (resp->header.nscount && resp->authority == NULL) {
404 free_dns_response(resp);
408 /* parse additional section */
409 resp->additional = parse_dns_rrsection(answer, size, &cp,
410 resp->header.arcount);
411 if (resp->header.arcount && resp->additional == NULL) {
412 free_dns_response(resp);
419 static struct dns_query *
420 parse_dns_qsection(const u_char *answer, int size, const u_char **cp, int count)
422 struct dns_query *head, *curr, *prev;
426 for (i = 1, head = NULL, prev = NULL; i <= count; i++, prev = curr) {
428 /* allocate and initialize struct */
429 curr = calloc(1, sizeof(struct dns_query));
431 free_dns_query(head);
440 length = dn_expand(answer, answer + size, *cp, name,
443 free_dns_query(head);
446 curr->name = strdup(name);
447 if (curr->name == NULL) {
448 free_dns_query(head);
454 curr->type = _getshort(*cp);
458 curr->class = _getshort(*cp);
465 static struct dns_rr *
466 parse_dns_rrsection(const u_char *answer, int size, const u_char **cp, int count)
468 struct dns_rr *head, *curr, *prev;
472 for (i = 1, head = NULL, prev = NULL; i <= count; i++, prev = curr) {
474 /* allocate and initialize struct */
475 curr = calloc(1, sizeof(struct dns_rr));
486 length = dn_expand(answer, answer + size, *cp, name,
492 curr->name = strdup(name);
493 if (curr->name == NULL) {
500 curr->type = _getshort(*cp);
504 curr->class = _getshort(*cp);
508 curr->ttl = _getlong(*cp);
512 curr->size = _getshort(*cp);
516 curr->rdata = malloc(curr->size);
517 if (curr->rdata == NULL) {
521 memcpy(curr->rdata, *cp, curr->size);
529 free_dns_query(struct dns_query *p)
536 free_dns_query(p->next);
541 free_dns_rr(struct dns_rr *p)
550 free_dns_rr(p->next);
555 free_dns_response(struct dns_response *p)
560 free_dns_query(p->query);
561 free_dns_rr(p->answer);
562 free_dns_rr(p->authority);
563 free_dns_rr(p->additional);
568 count_dns_rr(struct dns_rr *p, u_int16_t class, u_int16_t type)
573 if (p->class == class && p->type == type)
581 #endif /* !defined(HAVE_GETRRSETBYNAME) */