xref: /freebsd/crypto/openssh/openbsd-compat/getrrsetbyname.c (revision 4d3fc8b0570b29fb0d6ee9525f104d52176ff0d4)
1d4af9e69SDag-Erling Smørgrav /* $OpenBSD: getrrsetbyname.c,v 1.11 2007/10/11 18:36:41 jakob Exp $ */
2d95e11bfSDag-Erling Smørgrav 
3d95e11bfSDag-Erling Smørgrav /*
4d95e11bfSDag-Erling Smørgrav  * Copyright (c) 2001 Jakob Schlyter. All rights reserved.
5d95e11bfSDag-Erling Smørgrav  *
6d95e11bfSDag-Erling Smørgrav  * Redistribution and use in source and binary forms, with or without
7d95e11bfSDag-Erling Smørgrav  * modification, are permitted provided that the following conditions
8d95e11bfSDag-Erling Smørgrav  * are met:
9d95e11bfSDag-Erling Smørgrav  *
10d95e11bfSDag-Erling Smørgrav  * 1. Redistributions of source code must retain the above copyright
11d95e11bfSDag-Erling Smørgrav  *    notice, this list of conditions and the following disclaimer.
12d95e11bfSDag-Erling Smørgrav  *
13d95e11bfSDag-Erling Smørgrav  * 2. Redistributions in binary form must reproduce the above copyright
14d95e11bfSDag-Erling Smørgrav  *    notice, this list of conditions and the following disclaimer in the
15d95e11bfSDag-Erling Smørgrav  *    documentation and/or other materials provided with the distribution.
16d95e11bfSDag-Erling Smørgrav  *
17d95e11bfSDag-Erling Smørgrav  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18d95e11bfSDag-Erling Smørgrav  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19d95e11bfSDag-Erling Smørgrav  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20d95e11bfSDag-Erling Smørgrav  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21d95e11bfSDag-Erling Smørgrav  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22d95e11bfSDag-Erling Smørgrav  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23d95e11bfSDag-Erling Smørgrav  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24d95e11bfSDag-Erling Smørgrav  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25d95e11bfSDag-Erling Smørgrav  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26d95e11bfSDag-Erling Smørgrav  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27d95e11bfSDag-Erling Smørgrav  */
28d95e11bfSDag-Erling Smørgrav 
29d95e11bfSDag-Erling Smørgrav /*
30d95e11bfSDag-Erling Smørgrav  * Portions Copyright (c) 1999-2001 Internet Software Consortium.
31d95e11bfSDag-Erling Smørgrav  *
32d95e11bfSDag-Erling Smørgrav  * Permission to use, copy, modify, and distribute this software for any
33d95e11bfSDag-Erling Smørgrav  * purpose with or without fee is hereby granted, provided that the above
34d95e11bfSDag-Erling Smørgrav  * copyright notice and this permission notice appear in all copies.
35d95e11bfSDag-Erling Smørgrav  *
36d95e11bfSDag-Erling Smørgrav  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
37d95e11bfSDag-Erling Smørgrav  * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
38d95e11bfSDag-Erling Smørgrav  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
39d95e11bfSDag-Erling Smørgrav  * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
40d95e11bfSDag-Erling Smørgrav  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
41d95e11bfSDag-Erling Smørgrav  * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
42d95e11bfSDag-Erling Smørgrav  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
43d95e11bfSDag-Erling Smørgrav  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
44d95e11bfSDag-Erling Smørgrav  */
45d95e11bfSDag-Erling Smørgrav 
46021d409fSDag-Erling Smørgrav /* OPENBSD ORIGINAL: lib/libc/net/getrrsetbyname.c */
47021d409fSDag-Erling Smørgrav 
48d95e11bfSDag-Erling Smørgrav #include "includes.h"
49d95e11bfSDag-Erling Smørgrav 
50462c32cbSDag-Erling Smørgrav #if !defined (HAVE_GETRRSETBYNAME) && !defined (HAVE_LDNS)
51d95e11bfSDag-Erling Smørgrav 
52761efaa7SDag-Erling Smørgrav #include <stdlib.h>
53761efaa7SDag-Erling Smørgrav #include <string.h>
54761efaa7SDag-Erling Smørgrav 
55761efaa7SDag-Erling Smørgrav #include <netinet/in.h>
56761efaa7SDag-Erling Smørgrav #include <arpa/inet.h>
57761efaa7SDag-Erling Smørgrav 
58d95e11bfSDag-Erling Smørgrav #include "getrrsetbyname.h"
59d95e11bfSDag-Erling Smørgrav 
60d74d50a8SDag-Erling Smørgrav #if defined(HAVE_DECL_H_ERRNO) && !HAVE_DECL_H_ERRNO
61d74d50a8SDag-Erling Smørgrav extern int h_errno;
62d74d50a8SDag-Erling Smørgrav #endif
63d74d50a8SDag-Erling Smørgrav 
64021d409fSDag-Erling Smørgrav /* We don't need multithread support here */
65021d409fSDag-Erling Smørgrav #ifdef _THREAD_PRIVATE
66021d409fSDag-Erling Smørgrav # undef _THREAD_PRIVATE
67021d409fSDag-Erling Smørgrav #endif
68021d409fSDag-Erling Smørgrav #define _THREAD_PRIVATE(a,b,c) (c)
69761efaa7SDag-Erling Smørgrav 
70d4af9e69SDag-Erling Smørgrav #ifndef HAVE__RES_EXTERN
71021d409fSDag-Erling Smørgrav struct __res_state _res;
72d4af9e69SDag-Erling Smørgrav #endif
73d95e11bfSDag-Erling Smørgrav 
74021d409fSDag-Erling Smørgrav /* Necessary functions and macros */
75d95e11bfSDag-Erling Smørgrav 
76d95e11bfSDag-Erling Smørgrav /*
77d95e11bfSDag-Erling Smørgrav  * Inline versions of get/put short/long.  Pointer is advanced.
78d95e11bfSDag-Erling Smørgrav  *
79d95e11bfSDag-Erling Smørgrav  * These macros demonstrate the property of C whereby it can be
80d95e11bfSDag-Erling Smørgrav  * portable or it can be elegant but rarely both.
81d95e11bfSDag-Erling Smørgrav  */
82d95e11bfSDag-Erling Smørgrav 
83d95e11bfSDag-Erling Smørgrav #ifndef INT32SZ
84d95e11bfSDag-Erling Smørgrav # define INT32SZ	4
85d95e11bfSDag-Erling Smørgrav #endif
86d95e11bfSDag-Erling Smørgrav #ifndef INT16SZ
87d95e11bfSDag-Erling Smørgrav # define INT16SZ	2
88d95e11bfSDag-Erling Smørgrav #endif
89d95e11bfSDag-Erling Smørgrav 
90d95e11bfSDag-Erling Smørgrav #ifndef GETSHORT
91d95e11bfSDag-Erling Smørgrav #define GETSHORT(s, cp) { \
9287c1498dSEd Maste 	u_char *t_cp = (u_char *)(cp); \
93d95e11bfSDag-Erling Smørgrav 	(s) = ((u_int16_t)t_cp[0] << 8) \
94d95e11bfSDag-Erling Smørgrav 	    | ((u_int16_t)t_cp[1]) \
95d95e11bfSDag-Erling Smørgrav 	    ; \
96d95e11bfSDag-Erling Smørgrav 	(cp) += INT16SZ; \
97d95e11bfSDag-Erling Smørgrav }
98d95e11bfSDag-Erling Smørgrav #endif
99d95e11bfSDag-Erling Smørgrav 
100d95e11bfSDag-Erling Smørgrav #ifndef GETLONG
101d95e11bfSDag-Erling Smørgrav #define GETLONG(l, cp) { \
10287c1498dSEd Maste 	u_char *t_cp = (u_char *)(cp); \
103d95e11bfSDag-Erling Smørgrav 	(l) = ((u_int32_t)t_cp[0] << 24) \
104d95e11bfSDag-Erling Smørgrav 	    | ((u_int32_t)t_cp[1] << 16) \
105d95e11bfSDag-Erling Smørgrav 	    | ((u_int32_t)t_cp[2] << 8) \
106d95e11bfSDag-Erling Smørgrav 	    | ((u_int32_t)t_cp[3]) \
107d95e11bfSDag-Erling Smørgrav 	    ; \
108d95e11bfSDag-Erling Smørgrav 	(cp) += INT32SZ; \
109d95e11bfSDag-Erling Smørgrav }
110d95e11bfSDag-Erling Smørgrav #endif
111d95e11bfSDag-Erling Smørgrav 
112d95e11bfSDag-Erling Smørgrav /*
11387c1498dSEd Maste  * If the system doesn't have _getshort/_getlong or that are not exactly what
11487c1498dSEd Maste  * we need then use local replacements, avoiding name collisions.
11587c1498dSEd Maste  */
11687c1498dSEd Maste #if !defined(HAVE__GETSHORT) || !defined(HAVE__GETLONG) || \
11787c1498dSEd Maste     !defined(HAVE_DECL__GETSHORT) || HAVE_DECL__GETSHORT == 0 || \
11887c1498dSEd Maste     !defined(HAVE_DECL__GETLONG) || HAVE_DECL__GETLONG == 0
119f374ba41SEd Maste # ifdef _getshort
120f374ba41SEd Maste #  undef _getshort
121f374ba41SEd Maste # endif
122f374ba41SEd Maste # ifdef _getlong
123f374ba41SEd Maste #  undef _getlong
124f374ba41SEd Maste # endif
12587c1498dSEd Maste # define _getshort(x) (_ssh_compat_getshort(x))
12687c1498dSEd Maste # define _getlong(x) (_ssh_compat_getlong(x))
12787c1498dSEd Maste /*
128d95e11bfSDag-Erling Smørgrav  * Routines to insert/extract short/long's.
129d95e11bfSDag-Erling Smørgrav  */
130d95e11bfSDag-Erling Smørgrav static u_int16_t
_getshort(const u_char * msgp)13187c1498dSEd Maste _getshort(const u_char *msgp)
132d95e11bfSDag-Erling Smørgrav {
13387c1498dSEd Maste 	u_int16_t u;
134d95e11bfSDag-Erling Smørgrav 
135d95e11bfSDag-Erling Smørgrav 	GETSHORT(u, msgp);
136d95e11bfSDag-Erling Smørgrav 	return (u);
137d95e11bfSDag-Erling Smørgrav }
138d95e11bfSDag-Erling Smørgrav 
139d95e11bfSDag-Erling Smørgrav static u_int32_t
_getlong(const u_char * msgp)14087c1498dSEd Maste _getlong(const u_char *msgp)
141d95e11bfSDag-Erling Smørgrav {
14287c1498dSEd Maste 	u_int32_t u;
143d95e11bfSDag-Erling Smørgrav 
144d95e11bfSDag-Erling Smørgrav 	GETLONG(u, msgp);
145d95e11bfSDag-Erling Smørgrav 	return (u);
146d95e11bfSDag-Erling Smørgrav }
147f374ba41SEd Maste #endif /* missing _getshort/_getlong */
148d95e11bfSDag-Erling Smørgrav 
149021d409fSDag-Erling Smørgrav /* ************** */
150021d409fSDag-Erling Smørgrav 
1517aee6ffeSDag-Erling Smørgrav #define ANSWER_BUFFER_SIZE 0xffff
152021d409fSDag-Erling Smørgrav 
153021d409fSDag-Erling Smørgrav struct dns_query {
154021d409fSDag-Erling Smørgrav 	char			*name;
155021d409fSDag-Erling Smørgrav 	u_int16_t		type;
156021d409fSDag-Erling Smørgrav 	u_int16_t		class;
157021d409fSDag-Erling Smørgrav 	struct dns_query	*next;
158021d409fSDag-Erling Smørgrav };
159021d409fSDag-Erling Smørgrav 
160021d409fSDag-Erling Smørgrav struct dns_rr {
161021d409fSDag-Erling Smørgrav 	char			*name;
162021d409fSDag-Erling Smørgrav 	u_int16_t		type;
163021d409fSDag-Erling Smørgrav 	u_int16_t		class;
164021d409fSDag-Erling Smørgrav 	u_int16_t		ttl;
165021d409fSDag-Erling Smørgrav 	u_int16_t		size;
166021d409fSDag-Erling Smørgrav 	void			*rdata;
167021d409fSDag-Erling Smørgrav 	struct dns_rr		*next;
168021d409fSDag-Erling Smørgrav };
169021d409fSDag-Erling Smørgrav 
170021d409fSDag-Erling Smørgrav struct dns_response {
171021d409fSDag-Erling Smørgrav 	HEADER			header;
172021d409fSDag-Erling Smørgrav 	struct dns_query	*query;
173021d409fSDag-Erling Smørgrav 	struct dns_rr		*answer;
174021d409fSDag-Erling Smørgrav 	struct dns_rr		*authority;
175021d409fSDag-Erling Smørgrav 	struct dns_rr		*additional;
176021d409fSDag-Erling Smørgrav };
177021d409fSDag-Erling Smørgrav 
178021d409fSDag-Erling Smørgrav static struct dns_response *parse_dns_response(const u_char *, int);
179021d409fSDag-Erling Smørgrav static struct dns_query *parse_dns_qsection(const u_char *, int,
180021d409fSDag-Erling Smørgrav     const u_char **, int);
181021d409fSDag-Erling Smørgrav static struct dns_rr *parse_dns_rrsection(const u_char *, int, const u_char **,
182021d409fSDag-Erling Smørgrav     int);
183021d409fSDag-Erling Smørgrav 
184021d409fSDag-Erling Smørgrav static void free_dns_query(struct dns_query *);
185021d409fSDag-Erling Smørgrav static void free_dns_rr(struct dns_rr *);
186021d409fSDag-Erling Smørgrav static void free_dns_response(struct dns_response *);
187021d409fSDag-Erling Smørgrav 
188021d409fSDag-Erling Smørgrav static int count_dns_rr(struct dns_rr *, u_int16_t, u_int16_t);
189021d409fSDag-Erling Smørgrav 
190d95e11bfSDag-Erling Smørgrav int
getrrsetbyname(const char * hostname,unsigned int rdclass,unsigned int rdtype,unsigned int flags,struct rrsetinfo ** res)191d95e11bfSDag-Erling Smørgrav getrrsetbyname(const char *hostname, unsigned int rdclass,
192d95e11bfSDag-Erling Smørgrav     unsigned int rdtype, unsigned int flags,
193d95e11bfSDag-Erling Smørgrav     struct rrsetinfo **res)
194d95e11bfSDag-Erling Smørgrav {
195021d409fSDag-Erling Smørgrav 	struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res);
196d95e11bfSDag-Erling Smørgrav 	int result;
197d95e11bfSDag-Erling Smørgrav 	struct rrsetinfo *rrset = NULL;
198021d409fSDag-Erling Smørgrav 	struct dns_response *response = NULL;
199d95e11bfSDag-Erling Smørgrav 	struct dns_rr *rr;
200d95e11bfSDag-Erling Smørgrav 	struct rdatainfo *rdata;
201d95e11bfSDag-Erling Smørgrav 	int length;
202d95e11bfSDag-Erling Smørgrav 	unsigned int index_ans, index_sig;
203d95e11bfSDag-Erling Smørgrav 	u_char answer[ANSWER_BUFFER_SIZE];
204d95e11bfSDag-Erling Smørgrav 
205d95e11bfSDag-Erling Smørgrav 	/* check for invalid class and type */
206d95e11bfSDag-Erling Smørgrav 	if (rdclass > 0xffff || rdtype > 0xffff) {
207d95e11bfSDag-Erling Smørgrav 		result = ERRSET_INVAL;
208d95e11bfSDag-Erling Smørgrav 		goto fail;
209d95e11bfSDag-Erling Smørgrav 	}
210d95e11bfSDag-Erling Smørgrav 
211d95e11bfSDag-Erling Smørgrav 	/* don't allow queries of class or type ANY */
212d95e11bfSDag-Erling Smørgrav 	if (rdclass == 0xff || rdtype == 0xff) {
213d95e11bfSDag-Erling Smørgrav 		result = ERRSET_INVAL;
214d95e11bfSDag-Erling Smørgrav 		goto fail;
215d95e11bfSDag-Erling Smørgrav 	}
216d95e11bfSDag-Erling Smørgrav 
217d95e11bfSDag-Erling Smørgrav 	/* don't allow flags yet, unimplemented */
218d95e11bfSDag-Erling Smørgrav 	if (flags) {
219d95e11bfSDag-Erling Smørgrav 		result = ERRSET_INVAL;
220d95e11bfSDag-Erling Smørgrav 		goto fail;
221d95e11bfSDag-Erling Smørgrav 	}
222d95e11bfSDag-Erling Smørgrav 
223d95e11bfSDag-Erling Smørgrav 	/* initialize resolver */
224021d409fSDag-Erling Smørgrav 	if ((_resp->options & RES_INIT) == 0 && res_init() == -1) {
225d95e11bfSDag-Erling Smørgrav 		result = ERRSET_FAIL;
226d95e11bfSDag-Erling Smørgrav 		goto fail;
227d95e11bfSDag-Erling Smørgrav 	}
228d95e11bfSDag-Erling Smørgrav 
229d95e11bfSDag-Erling Smørgrav #ifdef DEBUG
230021d409fSDag-Erling Smørgrav 	_resp->options |= RES_DEBUG;
231d95e11bfSDag-Erling Smørgrav #endif /* DEBUG */
232d95e11bfSDag-Erling Smørgrav 
233d95e11bfSDag-Erling Smørgrav #ifdef RES_USE_DNSSEC
234d95e11bfSDag-Erling Smørgrav 	/* turn on DNSSEC if EDNS0 is configured */
235021d409fSDag-Erling Smørgrav 	if (_resp->options & RES_USE_EDNS0)
236021d409fSDag-Erling Smørgrav 		_resp->options |= RES_USE_DNSSEC;
237d95e11bfSDag-Erling Smørgrav #endif /* RES_USE_DNSEC */
238d95e11bfSDag-Erling Smørgrav 
239d95e11bfSDag-Erling Smørgrav 	/* make query */
240d95e11bfSDag-Erling Smørgrav 	length = res_query(hostname, (signed int) rdclass, (signed int) rdtype,
241d95e11bfSDag-Erling Smørgrav 	    answer, sizeof(answer));
242d95e11bfSDag-Erling Smørgrav 	if (length < 0) {
243d95e11bfSDag-Erling Smørgrav 		switch(h_errno) {
244d95e11bfSDag-Erling Smørgrav 		case HOST_NOT_FOUND:
245d95e11bfSDag-Erling Smørgrav 			result = ERRSET_NONAME;
246d95e11bfSDag-Erling Smørgrav 			goto fail;
247d95e11bfSDag-Erling Smørgrav 		case NO_DATA:
248d95e11bfSDag-Erling Smørgrav 			result = ERRSET_NODATA;
249d95e11bfSDag-Erling Smørgrav 			goto fail;
250d95e11bfSDag-Erling Smørgrav 		default:
251d95e11bfSDag-Erling Smørgrav 			result = ERRSET_FAIL;
252d95e11bfSDag-Erling Smørgrav 			goto fail;
253d95e11bfSDag-Erling Smørgrav 		}
254d95e11bfSDag-Erling Smørgrav 	}
255d95e11bfSDag-Erling Smørgrav 
256d95e11bfSDag-Erling Smørgrav 	/* parse result */
257d95e11bfSDag-Erling Smørgrav 	response = parse_dns_response(answer, length);
258d95e11bfSDag-Erling Smørgrav 	if (response == NULL) {
259d95e11bfSDag-Erling Smørgrav 		result = ERRSET_FAIL;
260d95e11bfSDag-Erling Smørgrav 		goto fail;
261d95e11bfSDag-Erling Smørgrav 	}
262d95e11bfSDag-Erling Smørgrav 
263d95e11bfSDag-Erling Smørgrav 	if (response->header.qdcount != 1) {
264d95e11bfSDag-Erling Smørgrav 		result = ERRSET_FAIL;
265d95e11bfSDag-Erling Smørgrav 		goto fail;
266d95e11bfSDag-Erling Smørgrav 	}
267d95e11bfSDag-Erling Smørgrav 
268d95e11bfSDag-Erling Smørgrav 	/* initialize rrset */
269d95e11bfSDag-Erling Smørgrav 	rrset = calloc(1, sizeof(struct rrsetinfo));
270d95e11bfSDag-Erling Smørgrav 	if (rrset == NULL) {
271d95e11bfSDag-Erling Smørgrav 		result = ERRSET_NOMEMORY;
272d95e11bfSDag-Erling Smørgrav 		goto fail;
273d95e11bfSDag-Erling Smørgrav 	}
274d95e11bfSDag-Erling Smørgrav 	rrset->rri_rdclass = response->query->class;
275d95e11bfSDag-Erling Smørgrav 	rrset->rri_rdtype = response->query->type;
276d95e11bfSDag-Erling Smørgrav 	rrset->rri_ttl = response->answer->ttl;
277d95e11bfSDag-Erling Smørgrav 	rrset->rri_nrdatas = response->header.ancount;
278d95e11bfSDag-Erling Smørgrav 
279d95e11bfSDag-Erling Smørgrav #ifdef HAVE_HEADER_AD
280d95e11bfSDag-Erling Smørgrav 	/* check for authenticated data */
281d95e11bfSDag-Erling Smørgrav 	if (response->header.ad == 1)
282d95e11bfSDag-Erling Smørgrav 		rrset->rri_flags |= RRSET_VALIDATED;
283d95e11bfSDag-Erling Smørgrav #endif
284d95e11bfSDag-Erling Smørgrav 
285d95e11bfSDag-Erling Smørgrav 	/* copy name from answer section */
286021d409fSDag-Erling Smørgrav 	rrset->rri_name = strdup(response->answer->name);
287d95e11bfSDag-Erling Smørgrav 	if (rrset->rri_name == NULL) {
288d95e11bfSDag-Erling Smørgrav 		result = ERRSET_NOMEMORY;
289d95e11bfSDag-Erling Smørgrav 		goto fail;
290d95e11bfSDag-Erling Smørgrav 	}
291d95e11bfSDag-Erling Smørgrav 
292d95e11bfSDag-Erling Smørgrav 	/* count answers */
293d95e11bfSDag-Erling Smørgrav 	rrset->rri_nrdatas = count_dns_rr(response->answer, rrset->rri_rdclass,
294d95e11bfSDag-Erling Smørgrav 	    rrset->rri_rdtype);
295d95e11bfSDag-Erling Smørgrav 	rrset->rri_nsigs = count_dns_rr(response->answer, rrset->rri_rdclass,
296d4af9e69SDag-Erling Smørgrav 	    T_RRSIG);
297d95e11bfSDag-Erling Smørgrav 
298d95e11bfSDag-Erling Smørgrav 	/* allocate memory for answers */
299d95e11bfSDag-Erling Smørgrav 	rrset->rri_rdatas = calloc(rrset->rri_nrdatas,
300d95e11bfSDag-Erling Smørgrav 	    sizeof(struct rdatainfo));
301d95e11bfSDag-Erling Smørgrav 	if (rrset->rri_rdatas == NULL) {
302d95e11bfSDag-Erling Smørgrav 		result = ERRSET_NOMEMORY;
303d95e11bfSDag-Erling Smørgrav 		goto fail;
304d95e11bfSDag-Erling Smørgrav 	}
305d95e11bfSDag-Erling Smørgrav 
306d95e11bfSDag-Erling Smørgrav 	/* allocate memory for signatures */
307d4af9e69SDag-Erling Smørgrav 	if (rrset->rri_nsigs > 0) {
308d95e11bfSDag-Erling Smørgrav 		rrset->rri_sigs = calloc(rrset->rri_nsigs, sizeof(struct rdatainfo));
309021d409fSDag-Erling Smørgrav 		if (rrset->rri_sigs == NULL) {
310d95e11bfSDag-Erling Smørgrav 			result = ERRSET_NOMEMORY;
311d95e11bfSDag-Erling Smørgrav 			goto fail;
312d95e11bfSDag-Erling Smørgrav 		}
313d4af9e69SDag-Erling Smørgrav 	}
314d95e11bfSDag-Erling Smørgrav 
315d95e11bfSDag-Erling Smørgrav 	/* copy answers & signatures */
316d95e11bfSDag-Erling Smørgrav 	for (rr = response->answer, index_ans = 0, index_sig = 0;
317d95e11bfSDag-Erling Smørgrav 	    rr; rr = rr->next) {
318d95e11bfSDag-Erling Smørgrav 
319d95e11bfSDag-Erling Smørgrav 		rdata = NULL;
320d95e11bfSDag-Erling Smørgrav 
321d95e11bfSDag-Erling Smørgrav 		if (rr->class == rrset->rri_rdclass &&
322d95e11bfSDag-Erling Smørgrav 		    rr->type  == rrset->rri_rdtype)
323d95e11bfSDag-Erling Smørgrav 			rdata = &rrset->rri_rdatas[index_ans++];
324d95e11bfSDag-Erling Smørgrav 
325d95e11bfSDag-Erling Smørgrav 		if (rr->class == rrset->rri_rdclass &&
326d4af9e69SDag-Erling Smørgrav 		    rr->type  == T_RRSIG)
327d95e11bfSDag-Erling Smørgrav 			rdata = &rrset->rri_sigs[index_sig++];
328d95e11bfSDag-Erling Smørgrav 
329d95e11bfSDag-Erling Smørgrav 		if (rdata) {
330d95e11bfSDag-Erling Smørgrav 			rdata->rdi_length = rr->size;
331d95e11bfSDag-Erling Smørgrav 			rdata->rdi_data   = malloc(rr->size);
332d95e11bfSDag-Erling Smørgrav 
333d95e11bfSDag-Erling Smørgrav 			if (rdata->rdi_data == NULL) {
334d95e11bfSDag-Erling Smørgrav 				result = ERRSET_NOMEMORY;
335d95e11bfSDag-Erling Smørgrav 				goto fail;
336d95e11bfSDag-Erling Smørgrav 			}
337d95e11bfSDag-Erling Smørgrav 			memcpy(rdata->rdi_data, rr->rdata, rr->size);
338d95e11bfSDag-Erling Smørgrav 		}
339d95e11bfSDag-Erling Smørgrav 	}
340021d409fSDag-Erling Smørgrav 	free_dns_response(response);
341d95e11bfSDag-Erling Smørgrav 
342d95e11bfSDag-Erling Smørgrav 	*res = rrset;
343d95e11bfSDag-Erling Smørgrav 	return (ERRSET_SUCCESS);
344d95e11bfSDag-Erling Smørgrav 
345d95e11bfSDag-Erling Smørgrav fail:
346d95e11bfSDag-Erling Smørgrav 	if (rrset != NULL)
347d95e11bfSDag-Erling Smørgrav 		freerrset(rrset);
348021d409fSDag-Erling Smørgrav 	if (response != NULL)
349021d409fSDag-Erling Smørgrav 		free_dns_response(response);
350d95e11bfSDag-Erling Smørgrav 	return (result);
351d95e11bfSDag-Erling Smørgrav }
352d95e11bfSDag-Erling Smørgrav 
353d95e11bfSDag-Erling Smørgrav void
freerrset(struct rrsetinfo * rrset)354d95e11bfSDag-Erling Smørgrav freerrset(struct rrsetinfo *rrset)
355d95e11bfSDag-Erling Smørgrav {
356d95e11bfSDag-Erling Smørgrav 	u_int16_t i;
357d95e11bfSDag-Erling Smørgrav 
358d95e11bfSDag-Erling Smørgrav 	if (rrset == NULL)
359d95e11bfSDag-Erling Smørgrav 		return;
360d95e11bfSDag-Erling Smørgrav 
361d95e11bfSDag-Erling Smørgrav 	if (rrset->rri_rdatas) {
362d95e11bfSDag-Erling Smørgrav 		for (i = 0; i < rrset->rri_nrdatas; i++) {
363d95e11bfSDag-Erling Smørgrav 			if (rrset->rri_rdatas[i].rdi_data == NULL)
364d95e11bfSDag-Erling Smørgrav 				break;
365d95e11bfSDag-Erling Smørgrav 			free(rrset->rri_rdatas[i].rdi_data);
366d95e11bfSDag-Erling Smørgrav 		}
367d95e11bfSDag-Erling Smørgrav 		free(rrset->rri_rdatas);
368d95e11bfSDag-Erling Smørgrav 	}
369d95e11bfSDag-Erling Smørgrav 
370d95e11bfSDag-Erling Smørgrav 	if (rrset->rri_sigs) {
371d95e11bfSDag-Erling Smørgrav 		for (i = 0; i < rrset->rri_nsigs; i++) {
372d95e11bfSDag-Erling Smørgrav 			if (rrset->rri_sigs[i].rdi_data == NULL)
373d95e11bfSDag-Erling Smørgrav 				break;
374d95e11bfSDag-Erling Smørgrav 			free(rrset->rri_sigs[i].rdi_data);
375d95e11bfSDag-Erling Smørgrav 		}
376d95e11bfSDag-Erling Smørgrav 		free(rrset->rri_sigs);
377d95e11bfSDag-Erling Smørgrav 	}
378d95e11bfSDag-Erling Smørgrav 
379d95e11bfSDag-Erling Smørgrav 	if (rrset->rri_name)
380d95e11bfSDag-Erling Smørgrav 		free(rrset->rri_name);
381d95e11bfSDag-Erling Smørgrav 	free(rrset);
382d95e11bfSDag-Erling Smørgrav }
383d95e11bfSDag-Erling Smørgrav 
384d95e11bfSDag-Erling Smørgrav /*
385d95e11bfSDag-Erling Smørgrav  * DNS response parsing routines
386d95e11bfSDag-Erling Smørgrav  */
387d95e11bfSDag-Erling Smørgrav static struct dns_response *
parse_dns_response(const u_char * answer,int size)388d95e11bfSDag-Erling Smørgrav parse_dns_response(const u_char *answer, int size)
389d95e11bfSDag-Erling Smørgrav {
390d95e11bfSDag-Erling Smørgrav 	struct dns_response *resp;
391d95e11bfSDag-Erling Smørgrav 	const u_char *cp;
392d95e11bfSDag-Erling Smørgrav 
393*4d3fc8b0SEd Maste 	if (size < HFIXEDSZ)
394*4d3fc8b0SEd Maste 		return (NULL);
395*4d3fc8b0SEd Maste 
396d95e11bfSDag-Erling Smørgrav 	/* allocate memory for the response */
397d95e11bfSDag-Erling Smørgrav 	resp = calloc(1, sizeof(*resp));
398d95e11bfSDag-Erling Smørgrav 	if (resp == NULL)
399d95e11bfSDag-Erling Smørgrav 		return (NULL);
400d95e11bfSDag-Erling Smørgrav 
401d95e11bfSDag-Erling Smørgrav 	/* initialize current pointer */
402d95e11bfSDag-Erling Smørgrav 	cp = answer;
403d95e11bfSDag-Erling Smørgrav 
404d95e11bfSDag-Erling Smørgrav 	/* copy header */
405d95e11bfSDag-Erling Smørgrav 	memcpy(&resp->header, cp, HFIXEDSZ);
406d95e11bfSDag-Erling Smørgrav 	cp += HFIXEDSZ;
407d95e11bfSDag-Erling Smørgrav 
408d95e11bfSDag-Erling Smørgrav 	/* fix header byte order */
409d95e11bfSDag-Erling Smørgrav 	resp->header.qdcount = ntohs(resp->header.qdcount);
410d95e11bfSDag-Erling Smørgrav 	resp->header.ancount = ntohs(resp->header.ancount);
411d95e11bfSDag-Erling Smørgrav 	resp->header.nscount = ntohs(resp->header.nscount);
412d95e11bfSDag-Erling Smørgrav 	resp->header.arcount = ntohs(resp->header.arcount);
413d95e11bfSDag-Erling Smørgrav 
414d95e11bfSDag-Erling Smørgrav 	/* there must be at least one query */
415d95e11bfSDag-Erling Smørgrav 	if (resp->header.qdcount < 1) {
416d95e11bfSDag-Erling Smørgrav 		free_dns_response(resp);
417d95e11bfSDag-Erling Smørgrav 		return (NULL);
418d95e11bfSDag-Erling Smørgrav 	}
419d95e11bfSDag-Erling Smørgrav 
420d95e11bfSDag-Erling Smørgrav 	/* parse query section */
421d95e11bfSDag-Erling Smørgrav 	resp->query = parse_dns_qsection(answer, size, &cp,
422d95e11bfSDag-Erling Smørgrav 	    resp->header.qdcount);
423d95e11bfSDag-Erling Smørgrav 	if (resp->header.qdcount && resp->query == NULL) {
424d95e11bfSDag-Erling Smørgrav 		free_dns_response(resp);
425d95e11bfSDag-Erling Smørgrav 		return (NULL);
426d95e11bfSDag-Erling Smørgrav 	}
427d95e11bfSDag-Erling Smørgrav 
428d95e11bfSDag-Erling Smørgrav 	/* parse answer section */
429d95e11bfSDag-Erling Smørgrav 	resp->answer = parse_dns_rrsection(answer, size, &cp,
430d95e11bfSDag-Erling Smørgrav 	    resp->header.ancount);
431d95e11bfSDag-Erling Smørgrav 	if (resp->header.ancount && resp->answer == NULL) {
432d95e11bfSDag-Erling Smørgrav 		free_dns_response(resp);
433d95e11bfSDag-Erling Smørgrav 		return (NULL);
434d95e11bfSDag-Erling Smørgrav 	}
435d95e11bfSDag-Erling Smørgrav 
436d95e11bfSDag-Erling Smørgrav 	/* parse authority section */
437d95e11bfSDag-Erling Smørgrav 	resp->authority = parse_dns_rrsection(answer, size, &cp,
438d95e11bfSDag-Erling Smørgrav 	    resp->header.nscount);
439d95e11bfSDag-Erling Smørgrav 	if (resp->header.nscount && resp->authority == NULL) {
440d95e11bfSDag-Erling Smørgrav 		free_dns_response(resp);
441d95e11bfSDag-Erling Smørgrav 		return (NULL);
442d95e11bfSDag-Erling Smørgrav 	}
443d95e11bfSDag-Erling Smørgrav 
444d95e11bfSDag-Erling Smørgrav 	/* parse additional section */
445d95e11bfSDag-Erling Smørgrav 	resp->additional = parse_dns_rrsection(answer, size, &cp,
446d95e11bfSDag-Erling Smørgrav 	    resp->header.arcount);
447d95e11bfSDag-Erling Smørgrav 	if (resp->header.arcount && resp->additional == NULL) {
448d95e11bfSDag-Erling Smørgrav 		free_dns_response(resp);
449d95e11bfSDag-Erling Smørgrav 		return (NULL);
450d95e11bfSDag-Erling Smørgrav 	}
451d95e11bfSDag-Erling Smørgrav 
452d95e11bfSDag-Erling Smørgrav 	return (resp);
453d95e11bfSDag-Erling Smørgrav }
454d95e11bfSDag-Erling Smørgrav 
455d95e11bfSDag-Erling Smørgrav static struct dns_query *
parse_dns_qsection(const u_char * answer,int size,const u_char ** cp,int count)456d95e11bfSDag-Erling Smørgrav parse_dns_qsection(const u_char *answer, int size, const u_char **cp, int count)
457d95e11bfSDag-Erling Smørgrav {
458d95e11bfSDag-Erling Smørgrav 	struct dns_query *head, *curr, *prev;
459d95e11bfSDag-Erling Smørgrav 	int i, length;
460d95e11bfSDag-Erling Smørgrav 	char name[MAXDNAME];
461d95e11bfSDag-Erling Smørgrav 
462*4d3fc8b0SEd Maste #define NEED(need) \
463*4d3fc8b0SEd Maste 	do { \
464*4d3fc8b0SEd Maste 		if (*cp + need > answer + size) \
465*4d3fc8b0SEd Maste 			goto fail; \
466*4d3fc8b0SEd Maste 	} while (0)
467d95e11bfSDag-Erling Smørgrav 
468*4d3fc8b0SEd Maste 	for (i = 1, head = NULL, prev = NULL; i <= count; i++, prev = curr) {
469*4d3fc8b0SEd Maste 		if (*cp >= answer + size) {
470*4d3fc8b0SEd Maste  fail:
471d95e11bfSDag-Erling Smørgrav 			free_dns_query(head);
472d95e11bfSDag-Erling Smørgrav 			return (NULL);
473d95e11bfSDag-Erling Smørgrav 		}
474*4d3fc8b0SEd Maste 		/* allocate and initialize struct */
475*4d3fc8b0SEd Maste 		curr = calloc(1, sizeof(struct dns_query));
476*4d3fc8b0SEd Maste 		if (curr == NULL)
477*4d3fc8b0SEd Maste 			goto fail;
478d95e11bfSDag-Erling Smørgrav 		if (head == NULL)
479d95e11bfSDag-Erling Smørgrav 			head = curr;
480d95e11bfSDag-Erling Smørgrav 		if (prev != NULL)
481d95e11bfSDag-Erling Smørgrav 			prev->next = curr;
482d95e11bfSDag-Erling Smørgrav 
483d95e11bfSDag-Erling Smørgrav 		/* name */
484d95e11bfSDag-Erling Smørgrav 		length = dn_expand(answer, answer + size, *cp, name,
485d95e11bfSDag-Erling Smørgrav 		    sizeof(name));
486d95e11bfSDag-Erling Smørgrav 		if (length < 0) {
487d95e11bfSDag-Erling Smørgrav 			free_dns_query(head);
488d95e11bfSDag-Erling Smørgrav 			return (NULL);
489d95e11bfSDag-Erling Smørgrav 		}
490d95e11bfSDag-Erling Smørgrav 		curr->name = strdup(name);
491d95e11bfSDag-Erling Smørgrav 		if (curr->name == NULL) {
492d95e11bfSDag-Erling Smørgrav 			free_dns_query(head);
493d95e11bfSDag-Erling Smørgrav 			return (NULL);
494d95e11bfSDag-Erling Smørgrav 		}
495*4d3fc8b0SEd Maste 		NEED(length);
496d95e11bfSDag-Erling Smørgrav 		*cp += length;
497d95e11bfSDag-Erling Smørgrav 
498d95e11bfSDag-Erling Smørgrav 		/* type */
499*4d3fc8b0SEd Maste 		NEED(INT16SZ);
500d95e11bfSDag-Erling Smørgrav 		curr->type = _getshort(*cp);
501d95e11bfSDag-Erling Smørgrav 		*cp += INT16SZ;
502d95e11bfSDag-Erling Smørgrav 
503d95e11bfSDag-Erling Smørgrav 		/* class */
504*4d3fc8b0SEd Maste 		NEED(INT16SZ);
505d95e11bfSDag-Erling Smørgrav 		curr->class = _getshort(*cp);
506d95e11bfSDag-Erling Smørgrav 		*cp += INT16SZ;
507d95e11bfSDag-Erling Smørgrav 	}
508*4d3fc8b0SEd Maste #undef NEED
509d95e11bfSDag-Erling Smørgrav 
510d95e11bfSDag-Erling Smørgrav 	return (head);
511d95e11bfSDag-Erling Smørgrav }
512d95e11bfSDag-Erling Smørgrav 
513d95e11bfSDag-Erling Smørgrav static struct dns_rr *
parse_dns_rrsection(const u_char * answer,int size,const u_char ** cp,int count)514021d409fSDag-Erling Smørgrav parse_dns_rrsection(const u_char *answer, int size, const u_char **cp,
515021d409fSDag-Erling Smørgrav     int count)
516d95e11bfSDag-Erling Smørgrav {
517d95e11bfSDag-Erling Smørgrav 	struct dns_rr *head, *curr, *prev;
518d95e11bfSDag-Erling Smørgrav 	int i, length;
519d95e11bfSDag-Erling Smørgrav 	char name[MAXDNAME];
520d95e11bfSDag-Erling Smørgrav 
521*4d3fc8b0SEd Maste #define NEED(need) \
522*4d3fc8b0SEd Maste 	do { \
523*4d3fc8b0SEd Maste 		if (*cp + need > answer + size) \
524*4d3fc8b0SEd Maste 			goto fail; \
525*4d3fc8b0SEd Maste 	} while (0)
526d95e11bfSDag-Erling Smørgrav 
527*4d3fc8b0SEd Maste 	for (i = 1, head = NULL, prev = NULL; i <= count; i++, prev = curr) {
528*4d3fc8b0SEd Maste 		if (*cp >= answer + size) {
529*4d3fc8b0SEd Maste  fail:
530d95e11bfSDag-Erling Smørgrav 			free_dns_rr(head);
531d95e11bfSDag-Erling Smørgrav 			return (NULL);
532d95e11bfSDag-Erling Smørgrav 		}
533*4d3fc8b0SEd Maste 
534*4d3fc8b0SEd Maste 		/* allocate and initialize struct */
535*4d3fc8b0SEd Maste 		curr = calloc(1, sizeof(struct dns_rr));
536*4d3fc8b0SEd Maste 		if (curr == NULL)
537*4d3fc8b0SEd Maste 			goto fail;
538d95e11bfSDag-Erling Smørgrav 		if (head == NULL)
539d95e11bfSDag-Erling Smørgrav 			head = curr;
540d95e11bfSDag-Erling Smørgrav 		if (prev != NULL)
541d95e11bfSDag-Erling Smørgrav 			prev->next = curr;
542d95e11bfSDag-Erling Smørgrav 
543d95e11bfSDag-Erling Smørgrav 		/* name */
544d95e11bfSDag-Erling Smørgrav 		length = dn_expand(answer, answer + size, *cp, name,
545d95e11bfSDag-Erling Smørgrav 		    sizeof(name));
546d95e11bfSDag-Erling Smørgrav 		if (length < 0) {
547d95e11bfSDag-Erling Smørgrav 			free_dns_rr(head);
548d95e11bfSDag-Erling Smørgrav 			return (NULL);
549d95e11bfSDag-Erling Smørgrav 		}
550d95e11bfSDag-Erling Smørgrav 		curr->name = strdup(name);
551d95e11bfSDag-Erling Smørgrav 		if (curr->name == NULL) {
552d95e11bfSDag-Erling Smørgrav 			free_dns_rr(head);
553d95e11bfSDag-Erling Smørgrav 			return (NULL);
554d95e11bfSDag-Erling Smørgrav 		}
555*4d3fc8b0SEd Maste 		NEED(length);
556d95e11bfSDag-Erling Smørgrav 		*cp += length;
557d95e11bfSDag-Erling Smørgrav 
558d95e11bfSDag-Erling Smørgrav 		/* type */
559*4d3fc8b0SEd Maste 		NEED(INT16SZ);
560d95e11bfSDag-Erling Smørgrav 		curr->type = _getshort(*cp);
561d95e11bfSDag-Erling Smørgrav 		*cp += INT16SZ;
562d95e11bfSDag-Erling Smørgrav 
563d95e11bfSDag-Erling Smørgrav 		/* class */
564*4d3fc8b0SEd Maste 		NEED(INT16SZ);
565d95e11bfSDag-Erling Smørgrav 		curr->class = _getshort(*cp);
566d95e11bfSDag-Erling Smørgrav 		*cp += INT16SZ;
567d95e11bfSDag-Erling Smørgrav 
568d95e11bfSDag-Erling Smørgrav 		/* ttl */
569*4d3fc8b0SEd Maste 		NEED(INT32SZ);
570d95e11bfSDag-Erling Smørgrav 		curr->ttl = _getlong(*cp);
571d95e11bfSDag-Erling Smørgrav 		*cp += INT32SZ;
572d95e11bfSDag-Erling Smørgrav 
573d95e11bfSDag-Erling Smørgrav 		/* rdata size */
574*4d3fc8b0SEd Maste 		NEED(INT16SZ);
575d95e11bfSDag-Erling Smørgrav 		curr->size = _getshort(*cp);
576d95e11bfSDag-Erling Smørgrav 		*cp += INT16SZ;
577d95e11bfSDag-Erling Smørgrav 
578d95e11bfSDag-Erling Smørgrav 		/* rdata itself */
579*4d3fc8b0SEd Maste 		NEED(curr->size);
580d95e11bfSDag-Erling Smørgrav 		curr->rdata = malloc(curr->size);
581d95e11bfSDag-Erling Smørgrav 		if (curr->rdata == NULL) {
582d95e11bfSDag-Erling Smørgrav 			free_dns_rr(head);
583d95e11bfSDag-Erling Smørgrav 			return (NULL);
584d95e11bfSDag-Erling Smørgrav 		}
585d95e11bfSDag-Erling Smørgrav 		memcpy(curr->rdata, *cp, curr->size);
586d95e11bfSDag-Erling Smørgrav 		*cp += curr->size;
587d95e11bfSDag-Erling Smørgrav 	}
588*4d3fc8b0SEd Maste #undef NEED
589d95e11bfSDag-Erling Smørgrav 
590d95e11bfSDag-Erling Smørgrav 	return (head);
591d95e11bfSDag-Erling Smørgrav }
592d95e11bfSDag-Erling Smørgrav 
593d95e11bfSDag-Erling Smørgrav static void
free_dns_query(struct dns_query * p)594d95e11bfSDag-Erling Smørgrav free_dns_query(struct dns_query *p)
595d95e11bfSDag-Erling Smørgrav {
596d95e11bfSDag-Erling Smørgrav 	if (p == NULL)
597d95e11bfSDag-Erling Smørgrav 		return;
598d95e11bfSDag-Erling Smørgrav 
599d95e11bfSDag-Erling Smørgrav 	if (p->name)
600d95e11bfSDag-Erling Smørgrav 		free(p->name);
601d95e11bfSDag-Erling Smørgrav 	free_dns_query(p->next);
602d95e11bfSDag-Erling Smørgrav 	free(p);
603d95e11bfSDag-Erling Smørgrav }
604d95e11bfSDag-Erling Smørgrav 
605d95e11bfSDag-Erling Smørgrav static void
free_dns_rr(struct dns_rr * p)606d95e11bfSDag-Erling Smørgrav free_dns_rr(struct dns_rr *p)
607d95e11bfSDag-Erling Smørgrav {
608d95e11bfSDag-Erling Smørgrav 	if (p == NULL)
609d95e11bfSDag-Erling Smørgrav 		return;
610d95e11bfSDag-Erling Smørgrav 
611d95e11bfSDag-Erling Smørgrav 	if (p->name)
612d95e11bfSDag-Erling Smørgrav 		free(p->name);
613d95e11bfSDag-Erling Smørgrav 	if (p->rdata)
614d95e11bfSDag-Erling Smørgrav 		free(p->rdata);
615d95e11bfSDag-Erling Smørgrav 	free_dns_rr(p->next);
616d95e11bfSDag-Erling Smørgrav 	free(p);
617d95e11bfSDag-Erling Smørgrav }
618d95e11bfSDag-Erling Smørgrav 
619d95e11bfSDag-Erling Smørgrav static void
free_dns_response(struct dns_response * p)620d95e11bfSDag-Erling Smørgrav free_dns_response(struct dns_response *p)
621d95e11bfSDag-Erling Smørgrav {
622d95e11bfSDag-Erling Smørgrav 	if (p == NULL)
623d95e11bfSDag-Erling Smørgrav 		return;
624d95e11bfSDag-Erling Smørgrav 
625d95e11bfSDag-Erling Smørgrav 	free_dns_query(p->query);
626d95e11bfSDag-Erling Smørgrav 	free_dns_rr(p->answer);
627d95e11bfSDag-Erling Smørgrav 	free_dns_rr(p->authority);
628d95e11bfSDag-Erling Smørgrav 	free_dns_rr(p->additional);
629d95e11bfSDag-Erling Smørgrav 	free(p);
630d95e11bfSDag-Erling Smørgrav }
631d95e11bfSDag-Erling Smørgrav 
632d95e11bfSDag-Erling Smørgrav static int
count_dns_rr(struct dns_rr * p,u_int16_t class,u_int16_t type)633d95e11bfSDag-Erling Smørgrav count_dns_rr(struct dns_rr *p, u_int16_t class, u_int16_t type)
634d95e11bfSDag-Erling Smørgrav {
635d95e11bfSDag-Erling Smørgrav 	int n = 0;
636d95e11bfSDag-Erling Smørgrav 
637d95e11bfSDag-Erling Smørgrav 	while(p) {
638d95e11bfSDag-Erling Smørgrav 		if (p->class == class && p->type == type)
639d95e11bfSDag-Erling Smørgrav 			n++;
640d95e11bfSDag-Erling Smørgrav 		p = p->next;
641d95e11bfSDag-Erling Smørgrav 	}
642d95e11bfSDag-Erling Smørgrav 
643d95e11bfSDag-Erling Smørgrav 	return (n);
644d95e11bfSDag-Erling Smørgrav }
645d95e11bfSDag-Erling Smørgrav 
646462c32cbSDag-Erling Smørgrav #endif /*  !defined (HAVE_GETRRSETBYNAME) && !defined (HAVE_LDNS) */
647