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 /* #include "thread_private.h" */
56 #define ANSWER_BUFFER_SIZE 1024*64
62 struct dns_query *next;
77 struct dns_query *query;
78 struct dns_rr *answer;
79 struct dns_rr *authority;
80 struct dns_rr *additional;
83 static struct dns_response *parse_dns_response(const u_char *, int);
84 static struct dns_query *parse_dns_qsection(const u_char *, int,
85 const u_char **, int);
86 static struct dns_rr *parse_dns_rrsection(const u_char *, int, const u_char **,
89 static void free_dns_query(struct dns_query *);
90 static void free_dns_rr(struct dns_rr *);
91 static void free_dns_response(struct dns_response *);
93 static int count_dns_rr(struct dns_rr *, u_int16_t, u_int16_t);
96 * Inline versions of get/put short/long. Pointer is advanced.
98 * These macros demonstrate the property of C whereby it can be
99 * portable or it can be elegant but rarely both.
110 #define GETSHORT(s, cp) { \
111 register u_char *t_cp = (u_char *)(cp); \
112 (s) = ((u_int16_t)t_cp[0] << 8) \
113 | ((u_int16_t)t_cp[1]) \
120 #define GETLONG(l, cp) { \
121 register u_char *t_cp = (u_char *)(cp); \
122 (l) = ((u_int32_t)t_cp[0] << 24) \
123 | ((u_int32_t)t_cp[1] << 16) \
124 | ((u_int32_t)t_cp[2] << 8) \
125 | ((u_int32_t)t_cp[3]) \
132 * Routines to insert/extract short/long's.
135 #ifndef HAVE__GETSHORT
138 register const u_char *msgp;
140 register u_int16_t u;
147 #ifndef HAVE__GETLONG
150 register const u_char *msgp;
152 register u_int32_t u;
160 getrrsetbyname(const char *hostname, unsigned int rdclass,
161 unsigned int rdtype, unsigned int flags,
162 struct rrsetinfo **res)
164 struct __res_state *_resp = &_res;
166 struct rrsetinfo *rrset = NULL;
167 struct dns_response *response;
169 struct rdatainfo *rdata;
171 unsigned int index_ans, index_sig;
172 u_char answer[ANSWER_BUFFER_SIZE];
174 /* check for invalid class and type */
175 if (rdclass > 0xffff || rdtype > 0xffff) {
176 result = ERRSET_INVAL;
180 /* don't allow queries of class or type ANY */
181 if (rdclass == 0xff || rdtype == 0xff) {
182 result = ERRSET_INVAL;
186 /* don't allow flags yet, unimplemented */
188 result = ERRSET_INVAL;
192 /* initialize resolver */
193 if ((_resp->options & RES_INIT) == 0 && res_init() == -1) {
194 result = ERRSET_FAIL;
199 _resp->options |= RES_DEBUG;
202 #ifdef RES_USE_DNSSEC
203 /* turn on DNSSEC if EDNS0 is configured */
204 if (_resp->options & RES_USE_EDNS0)
205 _resp->options |= RES_USE_DNSSEC;
206 #endif /* RES_USE_DNSEC */
209 length = res_query(hostname, (signed int) rdclass, (signed int) rdtype,
210 answer, sizeof(answer));
214 result = ERRSET_NONAME;
217 result = ERRSET_NODATA;
220 result = ERRSET_FAIL;
226 response = parse_dns_response(answer, length);
227 if (response == NULL) {
228 result = ERRSET_FAIL;
232 if (response->header.qdcount != 1) {
233 result = ERRSET_FAIL;
237 /* initialize rrset */
238 rrset = calloc(1, sizeof(struct rrsetinfo));
240 result = ERRSET_NOMEMORY;
243 rrset->rri_rdclass = response->query->class;
244 rrset->rri_rdtype = response->query->type;
245 rrset->rri_ttl = response->answer->ttl;
246 rrset->rri_nrdatas = response->header.ancount;
248 #ifdef HAVE_HEADER_AD
249 /* check for authenticated data */
250 if (response->header.ad == 1)
251 rrset->rri_flags |= RRSET_VALIDATED;
254 /* copy name from answer section */
255 length = strlen(response->answer->name);
256 rrset->rri_name = malloc(length + 1);
257 if (rrset->rri_name == NULL) {
258 result = ERRSET_NOMEMORY;
261 strlcpy(rrset->rri_name, response->answer->name, length + 1);
264 rrset->rri_nrdatas = count_dns_rr(response->answer, rrset->rri_rdclass,
266 rrset->rri_nsigs = count_dns_rr(response->answer, rrset->rri_rdclass,
269 /* allocate memory for answers */
270 rrset->rri_rdatas = calloc(rrset->rri_nrdatas,
271 sizeof(struct rdatainfo));
272 if (rrset->rri_rdatas == NULL) {
273 result = ERRSET_NOMEMORY;
277 /* allocate memory for signatures */
278 rrset->rri_sigs = calloc(rrset->rri_nsigs, sizeof(struct rdatainfo));
279 if (rrset->rri_sigs == NULL) {
280 result = ERRSET_NOMEMORY;
284 /* copy answers & signatures */
285 for (rr = response->answer, index_ans = 0, index_sig = 0;
290 if (rr->class == rrset->rri_rdclass &&
291 rr->type == rrset->rri_rdtype)
292 rdata = &rrset->rri_rdatas[index_ans++];
294 if (rr->class == rrset->rri_rdclass &&
296 rdata = &rrset->rri_sigs[index_sig++];
299 rdata->rdi_length = rr->size;
300 rdata->rdi_data = malloc(rr->size);
302 if (rdata->rdi_data == NULL) {
303 result = ERRSET_NOMEMORY;
306 memcpy(rdata->rdi_data, rr->rdata, rr->size);
311 return (ERRSET_SUCCESS);
320 freerrset(struct rrsetinfo *rrset)
327 if (rrset->rri_rdatas) {
328 for (i = 0; i < rrset->rri_nrdatas; i++) {
329 if (rrset->rri_rdatas[i].rdi_data == NULL)
331 free(rrset->rri_rdatas[i].rdi_data);
333 free(rrset->rri_rdatas);
336 if (rrset->rri_sigs) {
337 for (i = 0; i < rrset->rri_nsigs; i++) {
338 if (rrset->rri_sigs[i].rdi_data == NULL)
340 free(rrset->rri_sigs[i].rdi_data);
342 free(rrset->rri_sigs);
346 free(rrset->rri_name);
351 * DNS response parsing routines
353 static struct dns_response *
354 parse_dns_response(const u_char *answer, int size)
356 struct dns_response *resp;
359 /* allocate memory for the response */
360 resp = calloc(1, sizeof(*resp));
364 /* initialize current pointer */
368 memcpy(&resp->header, cp, HFIXEDSZ);
371 /* fix header byte order */
372 resp->header.qdcount = ntohs(resp->header.qdcount);
373 resp->header.ancount = ntohs(resp->header.ancount);
374 resp->header.nscount = ntohs(resp->header.nscount);
375 resp->header.arcount = ntohs(resp->header.arcount);
377 /* there must be at least one query */
378 if (resp->header.qdcount < 1) {
379 free_dns_response(resp);
383 /* parse query section */
384 resp->query = parse_dns_qsection(answer, size, &cp,
385 resp->header.qdcount);
386 if (resp->header.qdcount && resp->query == NULL) {
387 free_dns_response(resp);
391 /* parse answer section */
392 resp->answer = parse_dns_rrsection(answer, size, &cp,
393 resp->header.ancount);
394 if (resp->header.ancount && resp->answer == NULL) {
395 free_dns_response(resp);
399 /* parse authority section */
400 resp->authority = parse_dns_rrsection(answer, size, &cp,
401 resp->header.nscount);
402 if (resp->header.nscount && resp->authority == NULL) {
403 free_dns_response(resp);
407 /* parse additional section */
408 resp->additional = parse_dns_rrsection(answer, size, &cp,
409 resp->header.arcount);
410 if (resp->header.arcount && resp->additional == NULL) {
411 free_dns_response(resp);
418 static struct dns_query *
419 parse_dns_qsection(const u_char *answer, int size, const u_char **cp, int count)
421 struct dns_query *head, *curr, *prev;
425 for (i = 1, head = NULL, prev = NULL; i <= count; i++, prev = curr) {
427 /* allocate and initialize struct */
428 curr = calloc(1, sizeof(struct dns_query));
430 free_dns_query(head);
439 length = dn_expand(answer, answer + size, *cp, name,
442 free_dns_query(head);
445 curr->name = strdup(name);
446 if (curr->name == NULL) {
447 free_dns_query(head);
453 curr->type = _getshort(*cp);
457 curr->class = _getshort(*cp);
464 static struct dns_rr *
465 parse_dns_rrsection(const u_char *answer, int size, const u_char **cp, int count)
467 struct dns_rr *head, *curr, *prev;
471 for (i = 1, head = NULL, prev = NULL; i <= count; i++, prev = curr) {
473 /* allocate and initialize struct */
474 curr = calloc(1, sizeof(struct dns_rr));
485 length = dn_expand(answer, answer + size, *cp, name,
491 curr->name = strdup(name);
492 if (curr->name == NULL) {
499 curr->type = _getshort(*cp);
503 curr->class = _getshort(*cp);
507 curr->ttl = _getlong(*cp);
511 curr->size = _getshort(*cp);
515 curr->rdata = malloc(curr->size);
516 if (curr->rdata == NULL) {
520 memcpy(curr->rdata, *cp, curr->size);
528 free_dns_query(struct dns_query *p)
535 free_dns_query(p->next);
540 free_dns_rr(struct dns_rr *p)
549 free_dns_rr(p->next);
554 free_dns_response(struct dns_response *p)
559 free_dns_query(p->query);
560 free_dns_rr(p->answer);
561 free_dns_rr(p->authority);
562 free_dns_rr(p->additional);
567 count_dns_rr(struct dns_rr *p, u_int16_t class, u_int16_t type)
572 if (p->class == class && p->type == type)
580 #endif /* !defined(HAVE_GETRRSETBYNAME) */