1*8ddfd431Sdjm /* $OpenBSD: getrrsetbyname_async.c,v 1.14 2024/05/07 23:40:53 djm Exp $ */
2b44da627Seric /*
3b44da627Seric * Copyright (c) 2012 Eric Faurot <eric@openbsd.org>
4b44da627Seric *
5b44da627Seric * Permission to use, copy, modify, and distribute this software for any
6b44da627Seric * purpose with or without fee is hereby granted, provided that the above
7b44da627Seric * copyright notice and this permission notice appear in all copies.
8b44da627Seric *
9b44da627Seric * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10b44da627Seric * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11b44da627Seric * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12b44da627Seric * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13b44da627Seric * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14b44da627Seric * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15b44da627Seric * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16b44da627Seric */
1780f48568Seric
18b44da627Seric #include <sys/types.h>
19d216d6b1Seric #include <sys/socket.h>
20b44da627Seric #include <sys/uio.h>
21b44da627Seric #include <netinet/in.h>
22b44da627Seric #include <arpa/nameser.h>
23d216d6b1Seric #include <netdb.h>
24b44da627Seric
25d216d6b1Seric #include <asr.h>
26b44da627Seric #include <errno.h>
27b44da627Seric #include <resolv.h>
28b44da627Seric #include <stdlib.h>
29b44da627Seric #include <string.h>
30b44da627Seric #include <unistd.h>
31b44da627Seric
32b44da627Seric #include "asr_private.h"
33b44da627Seric
345be03f8fSeric static int getrrsetbyname_async_run(struct asr_query *, struct asr_result *);
355be03f8fSeric static void get_response(struct asr_result *, const char *, int);
36b44da627Seric
375be03f8fSeric struct asr_query *
getrrsetbyname_async(const char * hostname,unsigned int rdclass,unsigned int rdtype,unsigned int flags,void * asr)38b44da627Seric getrrsetbyname_async(const char *hostname, unsigned int rdclass,
395be03f8fSeric unsigned int rdtype, unsigned int flags, void *asr)
40b44da627Seric {
41b44da627Seric struct asr_ctx *ac;
425be03f8fSeric struct asr_query *as;
43b44da627Seric
44253ef892Sderaadt ac = _asr_use_resolver(asr);
45253ef892Sderaadt if ((as = _asr_async_new(ac, ASR_GETRRSETBYNAME)) == NULL)
46b44da627Seric goto abort; /* errno set */
47b44da627Seric as->as_run = getrrsetbyname_async_run;
48b44da627Seric
49b44da627Seric as->as.rrset.flags = flags;
50b44da627Seric as->as.rrset.class = rdclass;
51b44da627Seric as->as.rrset.type = rdtype;
52b44da627Seric as->as.rrset.name = strdup(hostname);
53b44da627Seric if (as->as.rrset.name == NULL)
54b44da627Seric goto abort; /* errno set */
55b44da627Seric
56253ef892Sderaadt _asr_ctx_unref(ac);
57b44da627Seric return (as);
58b44da627Seric abort:
59b44da627Seric if (as)
60253ef892Sderaadt _asr_async_free(as);
61b44da627Seric
62253ef892Sderaadt _asr_ctx_unref(ac);
63b44da627Seric return (NULL);
64b44da627Seric }
655826fd8cSguenther DEF_WEAK(getrrsetbyname_async);
66b44da627Seric
67b44da627Seric static int
getrrsetbyname_async_run(struct asr_query * as,struct asr_result * ar)685be03f8fSeric getrrsetbyname_async_run(struct asr_query *as, struct asr_result *ar)
69b44da627Seric {
70b44da627Seric next:
71b44da627Seric switch (as->as_state) {
72b44da627Seric
73b44da627Seric case ASR_STATE_INIT:
74b44da627Seric
75b44da627Seric /* Check for invalid class and type. */
76b44da627Seric if (as->as.rrset.class > 0xffff || as->as.rrset.type > 0xffff) {
77b44da627Seric ar->ar_rrset_errno = ERRSET_INVAL;
78b44da627Seric async_set_state(as, ASR_STATE_HALT);
79b44da627Seric break;
80b44da627Seric }
81b44da627Seric
82b44da627Seric /* Do not allow queries of class or type ANY. */
83b44da627Seric if (as->as.rrset.class == 0xff || as->as.rrset.type == 0xff) {
84b44da627Seric ar->ar_rrset_errno = ERRSET_INVAL;
85b44da627Seric async_set_state(as, ASR_STATE_HALT);
86b44da627Seric break;
87b44da627Seric }
88b44da627Seric
89b44da627Seric /* Do not allow flags yet, unimplemented. */
90b44da627Seric if (as->as.rrset.flags) {
91b44da627Seric ar->ar_rrset_errno = ERRSET_INVAL;
92b44da627Seric async_set_state(as, ASR_STATE_HALT);
93b44da627Seric break;
94b44da627Seric }
95b44da627Seric
96b44da627Seric /* Create a delegate the lookup to a subquery. */
97f6f51dadSeric as->as_subq = _res_query_async_ctx(
98b44da627Seric as->as.rrset.name,
99b44da627Seric as->as.rrset.class,
100b44da627Seric as->as.rrset.type,
101c5221d45Seric as->as_ctx);
102f6f51dadSeric if (as->as_subq == NULL) {
103b44da627Seric ar->ar_rrset_errno = ERRSET_FAIL;
104b44da627Seric async_set_state(as, ASR_STATE_HALT);
105b44da627Seric break;
106b44da627Seric }
107b44da627Seric
108b44da627Seric async_set_state(as, ASR_STATE_SUBQUERY);
109b44da627Seric break;
110b44da627Seric
111b44da627Seric case ASR_STATE_SUBQUERY:
112b44da627Seric
113f6f51dadSeric if ((asr_run(as->as_subq, ar)) == ASYNC_COND)
114b44da627Seric return (ASYNC_COND);
115b44da627Seric
116f6f51dadSeric as->as_subq = NULL;
117b44da627Seric
118b44da627Seric /* No packet received.*/
119b44da627Seric if (ar->ar_datalen == -1) {
120b44da627Seric switch (ar->ar_h_errno) {
121b44da627Seric case HOST_NOT_FOUND:
122b44da627Seric ar->ar_rrset_errno = ERRSET_NONAME;
123b44da627Seric break;
124b44da627Seric case NO_DATA:
125b44da627Seric ar->ar_rrset_errno = ERRSET_NODATA;
126b44da627Seric break;
127b44da627Seric default:
128b44da627Seric ar->ar_rrset_errno = ERRSET_FAIL;
129b44da627Seric break;
130b44da627Seric }
131b44da627Seric async_set_state(as, ASR_STATE_HALT);
132b44da627Seric break;
133b44da627Seric }
134b44da627Seric
135b44da627Seric /* Got a packet but no answer. */
136b44da627Seric if (ar->ar_count == 0) {
137b44da627Seric free(ar->ar_data);
138b44da627Seric switch (ar->ar_rcode) {
139b44da627Seric case NXDOMAIN:
140b44da627Seric ar->ar_rrset_errno = ERRSET_NONAME;
141b44da627Seric break;
142b44da627Seric case NOERROR:
143b44da627Seric ar->ar_rrset_errno = ERRSET_NODATA;
144b44da627Seric break;
145b44da627Seric default:
146b44da627Seric ar->ar_rrset_errno = ERRSET_FAIL;
147b44da627Seric break;
148b44da627Seric }
149b44da627Seric async_set_state(as, ASR_STATE_HALT);
150b44da627Seric break;
151b44da627Seric }
152b44da627Seric
153b44da627Seric get_response(ar, ar->ar_data, ar->ar_datalen);
154b44da627Seric free(ar->ar_data);
155b44da627Seric async_set_state(as, ASR_STATE_HALT);
156b44da627Seric break;
157b44da627Seric
158b44da627Seric case ASR_STATE_HALT:
159b44da627Seric if (ar->ar_rrset_errno)
160b44da627Seric ar->ar_rrsetinfo = NULL;
161b44da627Seric return (ASYNC_DONE);
162b44da627Seric
163b44da627Seric default:
164987f89cfSeric ar->ar_rrset_errno = ERRSET_FAIL;
165b44da627Seric async_set_state(as, ASR_STATE_HALT);
166b44da627Seric break;
167b44da627Seric }
168b44da627Seric goto next;
169b44da627Seric }
170b44da627Seric
1712c53affbSjmc /* The rest of this file is taken from the original implementation. */
172b44da627Seric
173*8ddfd431Sdjm /* $OpenBSD: getrrsetbyname_async.c,v 1.14 2024/05/07 23:40:53 djm Exp $ */
174b44da627Seric
175b44da627Seric /*
176b44da627Seric * Copyright (c) 2001 Jakob Schlyter. All rights reserved.
177b44da627Seric *
178b44da627Seric * Redistribution and use in source and binary forms, with or without
179b44da627Seric * modification, are permitted provided that the following conditions
180b44da627Seric * are met:
181b44da627Seric *
182b44da627Seric * 1. Redistributions of source code must retain the above copyright
183b44da627Seric * notice, this list of conditions and the following disclaimer.
184b44da627Seric *
185b44da627Seric * 2. Redistributions in binary form must reproduce the above copyright
186b44da627Seric * notice, this list of conditions and the following disclaimer in the
187b44da627Seric * documentation and/or other materials provided with the distribution.
188b44da627Seric *
189b44da627Seric * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
190b44da627Seric * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
191b44da627Seric * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
192b44da627Seric * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
193b44da627Seric * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
194b44da627Seric * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
195b44da627Seric * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
196b44da627Seric * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
197b44da627Seric * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
198b44da627Seric * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
199b44da627Seric */
200b44da627Seric
201b44da627Seric /*
202b44da627Seric * Portions Copyright (c) 1999-2001 Internet Software Consortium.
203b44da627Seric *
204b44da627Seric * Permission to use, copy, modify, and distribute this software for any
205b44da627Seric * purpose with or without fee is hereby granted, provided that the above
206b44da627Seric * copyright notice and this permission notice appear in all copies.
207b44da627Seric *
208b44da627Seric * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
209b44da627Seric * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
210b44da627Seric * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
211b44da627Seric * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
212b44da627Seric * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
213b44da627Seric * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
214b44da627Seric * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
215b44da627Seric * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
216b44da627Seric */
217b44da627Seric
218b44da627Seric #define MAXPACKET 1024*64
219b44da627Seric
220b44da627Seric struct dns_query {
221b44da627Seric char *name;
222b44da627Seric u_int16_t type;
223b44da627Seric u_int16_t class;
224b44da627Seric struct dns_query *next;
225b44da627Seric };
226b44da627Seric
227b44da627Seric struct dns_rr {
228b44da627Seric char *name;
229b44da627Seric u_int16_t type;
230b44da627Seric u_int16_t class;
231b44da627Seric u_int16_t ttl;
232b44da627Seric u_int16_t size;
233b44da627Seric void *rdata;
234b44da627Seric struct dns_rr *next;
235b44da627Seric };
236b44da627Seric
237b44da627Seric struct dns_response {
238b44da627Seric HEADER header;
239b44da627Seric struct dns_query *query;
240b44da627Seric struct dns_rr *answer;
241b44da627Seric struct dns_rr *authority;
242b44da627Seric struct dns_rr *additional;
243b44da627Seric };
244b44da627Seric
245b44da627Seric static struct dns_response *parse_dns_response(const u_char *, int);
246b44da627Seric static struct dns_query *parse_dns_qsection(const u_char *, int,
247b44da627Seric const u_char **, int);
248b44da627Seric static struct dns_rr *parse_dns_rrsection(const u_char *, int, const u_char **,
249b44da627Seric int);
250b44da627Seric
251b44da627Seric static void free_dns_query(struct dns_query *);
252b44da627Seric static void free_dns_rr(struct dns_rr *);
253b44da627Seric static void free_dns_response(struct dns_response *);
254b44da627Seric
255b44da627Seric static int count_dns_rr(struct dns_rr *, u_int16_t, u_int16_t);
256b44da627Seric
257b44da627Seric static void
get_response(struct asr_result * ar,const char * pkt,int pktlen)2585be03f8fSeric get_response(struct asr_result *ar, const char *pkt, int pktlen)
259b44da627Seric {
260b44da627Seric struct rrsetinfo *rrset = NULL;
261b44da627Seric struct dns_response *response = NULL;
262b44da627Seric struct dns_rr *rr;
263b44da627Seric struct rdatainfo *rdata;
264b44da627Seric unsigned int index_ans, index_sig;
265b44da627Seric
266b44da627Seric /* parse result */
267b44da627Seric response = parse_dns_response(pkt, pktlen);
268b44da627Seric if (response == NULL) {
269b44da627Seric ar->ar_rrset_errno = ERRSET_FAIL;
270b44da627Seric goto fail;
271b44da627Seric }
272b44da627Seric
273b44da627Seric if (response->header.qdcount != 1) {
274b44da627Seric ar->ar_rrset_errno = ERRSET_FAIL;
275b44da627Seric goto fail;
276b44da627Seric }
277b44da627Seric
278b44da627Seric /* initialize rrset */
279b44da627Seric rrset = calloc(1, sizeof(struct rrsetinfo));
280b44da627Seric if (rrset == NULL) {
281b44da627Seric ar->ar_rrset_errno = ERRSET_NOMEMORY;
282b44da627Seric goto fail;
283b44da627Seric }
284b44da627Seric rrset->rri_rdclass = response->query->class;
285b44da627Seric rrset->rri_rdtype = response->query->type;
286b44da627Seric rrset->rri_ttl = response->answer->ttl;
287b44da627Seric rrset->rri_nrdatas = response->header.ancount;
288b44da627Seric
289b44da627Seric /* check for authenticated data */
290b44da627Seric if (response->header.ad == 1)
291b44da627Seric rrset->rri_flags |= RRSET_VALIDATED;
292b44da627Seric
293b44da627Seric /* copy name from answer section */
294b44da627Seric rrset->rri_name = strdup(response->answer->name);
295b44da627Seric if (rrset->rri_name == NULL) {
296b44da627Seric ar->ar_rrset_errno = ERRSET_NOMEMORY;
297b44da627Seric goto fail;
298b44da627Seric }
299b44da627Seric
300b44da627Seric /* count answers */
301b44da627Seric rrset->rri_nrdatas = count_dns_rr(response->answer, rrset->rri_rdclass,
302b44da627Seric rrset->rri_rdtype);
303b44da627Seric rrset->rri_nsigs = count_dns_rr(response->answer, rrset->rri_rdclass,
304b44da627Seric T_RRSIG);
305b44da627Seric
306b44da627Seric /* allocate memory for answers */
307b44da627Seric rrset->rri_rdatas = calloc(rrset->rri_nrdatas,
308b44da627Seric sizeof(struct rdatainfo));
309b44da627Seric if (rrset->rri_rdatas == NULL) {
310b44da627Seric ar->ar_rrset_errno = ERRSET_NOMEMORY;
311b44da627Seric goto fail;
312b44da627Seric }
313b44da627Seric
314b44da627Seric /* allocate memory for signatures */
315b44da627Seric rrset->rri_sigs = calloc(rrset->rri_nsigs, sizeof(struct rdatainfo));
316b44da627Seric if (rrset->rri_sigs == NULL) {
317b44da627Seric ar->ar_rrset_errno = ERRSET_NOMEMORY;
318b44da627Seric goto fail;
319b44da627Seric }
320b44da627Seric
321b44da627Seric /* copy answers & signatures */
322b44da627Seric for (rr = response->answer, index_ans = 0, index_sig = 0;
323b44da627Seric rr; rr = rr->next) {
324b44da627Seric
325b44da627Seric rdata = NULL;
326b44da627Seric
327b44da627Seric if (rr->class == rrset->rri_rdclass &&
328b44da627Seric rr->type == rrset->rri_rdtype)
329b44da627Seric rdata = &rrset->rri_rdatas[index_ans++];
330b44da627Seric
331b44da627Seric if (rr->class == rrset->rri_rdclass &&
332b44da627Seric rr->type == T_RRSIG)
333b44da627Seric rdata = &rrset->rri_sigs[index_sig++];
334b44da627Seric
335b44da627Seric if (rdata) {
336b44da627Seric rdata->rdi_length = rr->size;
337*8ddfd431Sdjm if (rr->size != 0) {
338b44da627Seric rdata->rdi_data = malloc(rr->size);
339b44da627Seric if (rdata->rdi_data == NULL) {
340b44da627Seric ar->ar_rrset_errno = ERRSET_NOMEMORY;
341b44da627Seric goto fail;
342b44da627Seric }
343b44da627Seric memcpy(rdata->rdi_data, rr->rdata, rr->size);
344b44da627Seric }
345b44da627Seric }
346*8ddfd431Sdjm }
347b44da627Seric free_dns_response(response);
348b44da627Seric
349b44da627Seric ar->ar_rrsetinfo = rrset;
350b44da627Seric ar->ar_rrset_errno = ERRSET_SUCCESS;
351b44da627Seric return;
352b44da627Seric
353b44da627Seric fail:
354b44da627Seric if (rrset != NULL)
355b44da627Seric freerrset(rrset);
356b44da627Seric if (response != NULL)
357b44da627Seric free_dns_response(response);
358b44da627Seric }
359b44da627Seric
360b44da627Seric /*
361b44da627Seric * DNS response parsing routines
362b44da627Seric */
363b44da627Seric static struct dns_response *
parse_dns_response(const u_char * answer,int size)364b44da627Seric parse_dns_response(const u_char *answer, int size)
365b44da627Seric {
366b44da627Seric struct dns_response *resp;
367b44da627Seric const u_char *cp;
368b44da627Seric
36943f5d167Smillert if (size <= HFIXEDSZ)
37043f5d167Smillert return (NULL);
37143f5d167Smillert
372b44da627Seric /* allocate memory for the response */
373b44da627Seric resp = calloc(1, sizeof(*resp));
374b44da627Seric if (resp == NULL)
375b44da627Seric return (NULL);
376b44da627Seric
377b44da627Seric /* initialize current pointer */
378b44da627Seric cp = answer;
379b44da627Seric
380b44da627Seric /* copy header */
381b44da627Seric memcpy(&resp->header, cp, HFIXEDSZ);
382b44da627Seric cp += HFIXEDSZ;
383b44da627Seric
384b44da627Seric /* fix header byte order */
385b44da627Seric resp->header.qdcount = ntohs(resp->header.qdcount);
386b44da627Seric resp->header.ancount = ntohs(resp->header.ancount);
387b44da627Seric resp->header.nscount = ntohs(resp->header.nscount);
388b44da627Seric resp->header.arcount = ntohs(resp->header.arcount);
389b44da627Seric
390b44da627Seric /* there must be at least one query */
391b44da627Seric if (resp->header.qdcount < 1) {
392b44da627Seric free_dns_response(resp);
393b44da627Seric return (NULL);
394b44da627Seric }
395b44da627Seric
396b44da627Seric /* parse query section */
397b44da627Seric resp->query = parse_dns_qsection(answer, size, &cp,
398b44da627Seric resp->header.qdcount);
399b44da627Seric if (resp->header.qdcount && resp->query == NULL) {
400b44da627Seric free_dns_response(resp);
401b44da627Seric return (NULL);
402b44da627Seric }
403b44da627Seric
404b44da627Seric /* parse answer section */
405b44da627Seric resp->answer = parse_dns_rrsection(answer, size, &cp,
406b44da627Seric resp->header.ancount);
407b44da627Seric if (resp->header.ancount && resp->answer == NULL) {
408b44da627Seric free_dns_response(resp);
409b44da627Seric return (NULL);
410b44da627Seric }
411b44da627Seric
412b44da627Seric /* parse authority section */
413b44da627Seric resp->authority = parse_dns_rrsection(answer, size, &cp,
414b44da627Seric resp->header.nscount);
415b44da627Seric if (resp->header.nscount && resp->authority == NULL) {
416b44da627Seric free_dns_response(resp);
417b44da627Seric return (NULL);
418b44da627Seric }
419b44da627Seric
420b44da627Seric /* parse additional section */
421b44da627Seric resp->additional = parse_dns_rrsection(answer, size, &cp,
422b44da627Seric resp->header.arcount);
423b44da627Seric if (resp->header.arcount && resp->additional == NULL) {
424b44da627Seric free_dns_response(resp);
425b44da627Seric return (NULL);
426b44da627Seric }
427b44da627Seric
428b44da627Seric return (resp);
429b44da627Seric }
430b44da627Seric
431b44da627Seric static struct dns_query *
parse_dns_qsection(const u_char * answer,int size,const u_char ** cp,int count)432b44da627Seric parse_dns_qsection(const u_char *answer, int size, const u_char **cp, int count)
433b44da627Seric {
434b44da627Seric struct dns_query *head, *curr, *prev;
435b44da627Seric int i, length;
436b44da627Seric char name[MAXDNAME];
437b44da627Seric
43843f5d167Smillert #define NEED(need) \
43943f5d167Smillert do { \
44043f5d167Smillert if (*cp + need > answer + size) \
44143f5d167Smillert goto fail; \
44243f5d167Smillert } while (0)
443b44da627Seric
44443f5d167Smillert for (i = 1, head = NULL, prev = NULL; i <= count; i++, prev = curr) {
44543f5d167Smillert if (*cp >= answer + size) {
44643f5d167Smillert fail:
447b44da627Seric free_dns_query(head);
448b44da627Seric return (NULL);
449b44da627Seric }
45043f5d167Smillert /* allocate and initialize struct */
45143f5d167Smillert curr = calloc(1, sizeof(struct dns_query));
45243f5d167Smillert if (curr == NULL)
45343f5d167Smillert goto fail;
454b44da627Seric if (head == NULL)
455b44da627Seric head = curr;
456b44da627Seric if (prev != NULL)
457b44da627Seric prev->next = curr;
458b44da627Seric
459b44da627Seric /* name */
460b44da627Seric length = dn_expand(answer, answer + size, *cp, name,
461b44da627Seric sizeof(name));
462b44da627Seric if (length < 0) {
463b44da627Seric free_dns_query(head);
464b44da627Seric return (NULL);
465b44da627Seric }
466b44da627Seric curr->name = strdup(name);
467b44da627Seric if (curr->name == NULL) {
468b44da627Seric free_dns_query(head);
469b44da627Seric return (NULL);
470b44da627Seric }
47143f5d167Smillert NEED(length);
472b44da627Seric *cp += length;
473b44da627Seric
474b44da627Seric /* type */
47543f5d167Smillert NEED(INT16SZ);
476b44da627Seric curr->type = _getshort(*cp);
477b44da627Seric *cp += INT16SZ;
478b44da627Seric
479b44da627Seric /* class */
48043f5d167Smillert NEED(INT16SZ);
481b44da627Seric curr->class = _getshort(*cp);
482b44da627Seric *cp += INT16SZ;
483b44da627Seric }
48443f5d167Smillert #undef NEED
485b44da627Seric
486b44da627Seric return (head);
487b44da627Seric }
488b44da627Seric
489b44da627Seric static struct dns_rr *
parse_dns_rrsection(const u_char * answer,int size,const u_char ** cp,int count)490b44da627Seric parse_dns_rrsection(const u_char *answer, int size, const u_char **cp,
491b44da627Seric int count)
492b44da627Seric {
493b44da627Seric struct dns_rr *head, *curr, *prev;
494b44da627Seric int i, length;
495b44da627Seric char name[MAXDNAME];
496b44da627Seric
49743f5d167Smillert #define NEED(need) \
49843f5d167Smillert do { \
49943f5d167Smillert if (*cp + need > answer + size) \
50043f5d167Smillert goto fail; \
50143f5d167Smillert } while (0)
502b44da627Seric
50343f5d167Smillert for (i = 1, head = NULL, prev = NULL; i <= count; i++, prev = curr) {
50443f5d167Smillert if (*cp >= answer + size) {
50543f5d167Smillert fail:
506b44da627Seric free_dns_rr(head);
507b44da627Seric return (NULL);
508b44da627Seric }
50943f5d167Smillert
51043f5d167Smillert /* allocate and initialize struct */
51143f5d167Smillert curr = calloc(1, sizeof(struct dns_rr));
51243f5d167Smillert if (curr == NULL)
51343f5d167Smillert goto fail;
514b44da627Seric if (head == NULL)
515b44da627Seric head = curr;
516b44da627Seric if (prev != NULL)
517b44da627Seric prev->next = curr;
518b44da627Seric
519b44da627Seric /* name */
520b44da627Seric length = dn_expand(answer, answer + size, *cp, name,
521b44da627Seric sizeof(name));
522b44da627Seric if (length < 0) {
523b44da627Seric free_dns_rr(head);
524b44da627Seric return (NULL);
525b44da627Seric }
526b44da627Seric curr->name = strdup(name);
527b44da627Seric if (curr->name == NULL) {
528b44da627Seric free_dns_rr(head);
529b44da627Seric return (NULL);
530b44da627Seric }
53143f5d167Smillert NEED(length);
532b44da627Seric *cp += length;
533b44da627Seric
534b44da627Seric /* type */
53543f5d167Smillert NEED(INT16SZ);
536b44da627Seric curr->type = _getshort(*cp);
537b44da627Seric *cp += INT16SZ;
538b44da627Seric
539b44da627Seric /* class */
54043f5d167Smillert NEED(INT16SZ);
541b44da627Seric curr->class = _getshort(*cp);
542b44da627Seric *cp += INT16SZ;
543b44da627Seric
544b44da627Seric /* ttl */
54543f5d167Smillert NEED(INT32SZ);
546b44da627Seric curr->ttl = _getlong(*cp);
547b44da627Seric *cp += INT32SZ;
548b44da627Seric
549b44da627Seric /* rdata size */
55043f5d167Smillert NEED(INT16SZ);
551b44da627Seric curr->size = _getshort(*cp);
552b44da627Seric *cp += INT16SZ;
553b44da627Seric
554b44da627Seric /* rdata itself */
55543f5d167Smillert NEED(curr->size);
556*8ddfd431Sdjm if (curr->size != 0) {
557*8ddfd431Sdjm if ((curr->rdata = malloc(curr->size)) == NULL) {
558b44da627Seric free_dns_rr(head);
559b44da627Seric return (NULL);
560b44da627Seric }
561b44da627Seric memcpy(curr->rdata, *cp, curr->size);
562*8ddfd431Sdjm }
563b44da627Seric *cp += curr->size;
564b44da627Seric }
56543f5d167Smillert #undef NEED
566b44da627Seric
567b44da627Seric return (head);
568b44da627Seric }
569b44da627Seric
570b44da627Seric static void
free_dns_query(struct dns_query * p)571b44da627Seric free_dns_query(struct dns_query *p)
572b44da627Seric {
573b44da627Seric if (p == NULL)
574b44da627Seric return;
575b44da627Seric
576b44da627Seric if (p->name)
577b44da627Seric free(p->name);
578b44da627Seric free_dns_query(p->next);
579b44da627Seric free(p);
580b44da627Seric }
581b44da627Seric
582b44da627Seric static void
free_dns_rr(struct dns_rr * p)583b44da627Seric free_dns_rr(struct dns_rr *p)
584b44da627Seric {
585b44da627Seric if (p == NULL)
586b44da627Seric return;
587b44da627Seric
588b44da627Seric if (p->name)
589b44da627Seric free(p->name);
590b44da627Seric if (p->rdata)
591b44da627Seric free(p->rdata);
592b44da627Seric free_dns_rr(p->next);
593b44da627Seric free(p);
594b44da627Seric }
595b44da627Seric
596b44da627Seric static void
free_dns_response(struct dns_response * p)597b44da627Seric free_dns_response(struct dns_response *p)
598b44da627Seric {
599b44da627Seric if (p == NULL)
600b44da627Seric return;
601b44da627Seric
602b44da627Seric free_dns_query(p->query);
603b44da627Seric free_dns_rr(p->answer);
604b44da627Seric free_dns_rr(p->authority);
605b44da627Seric free_dns_rr(p->additional);
606b44da627Seric free(p);
607b44da627Seric }
608b44da627Seric
609b44da627Seric static int
count_dns_rr(struct dns_rr * p,u_int16_t class,u_int16_t type)610b44da627Seric count_dns_rr(struct dns_rr *p, u_int16_t class, u_int16_t type)
611b44da627Seric {
612b44da627Seric int n = 0;
613b44da627Seric
614b44da627Seric while (p) {
615b44da627Seric if (p->class == class && p->type == type)
616b44da627Seric n++;
617b44da627Seric p = p->next;
618b44da627Seric }
619b44da627Seric
620b44da627Seric return (n);
621b44da627Seric }
622