xref: /openbsd/lib/libc/asr/getrrsetbyname.c (revision 91f110e0)
1 /*	$OpenBSD: getrrsetbyname.c,v 1.3 2013/07/12 14:36:22 eric Exp $	*/
2 /*
3  * Copyright (c) 2012 Eric Faurot <eric@openbsd.org>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include <sys/types.h>
19 #include <netinet/in.h>
20 
21 #include <errno.h>
22 #include <resolv.h>
23 #include <stdlib.h>
24 
25 #include "asr.h"
26 
27 int
28 getrrsetbyname(const char *name, unsigned int class, unsigned int type,
29     unsigned int flags, struct rrsetinfo **res)
30 {
31 	struct async	*as;
32 	struct async_res ar;
33 	int		 r, saved_errno = errno;
34 
35 	res_init();
36 
37 	as = getrrsetbyname_async(name, class, type, flags, NULL);
38 	if (as == NULL) {
39 		r = (errno == ENOMEM) ? ERRSET_NOMEMORY : ERRSET_FAIL;
40 		errno = saved_errno;
41 		return (r);
42 	}
43 
44 	asr_async_run_sync(as, &ar);
45 
46 	*res = ar.ar_rrsetinfo;
47 
48 	return (ar.ar_rrset_errno);
49 }
50 
51 /* from net/getrrsetbyname.c */
52 void
53 freerrset(struct rrsetinfo *rrset)
54 {
55 	u_int16_t i;
56 
57 	if (rrset == NULL)
58 		return;
59 
60 	if (rrset->rri_rdatas) {
61 		for (i = 0; i < rrset->rri_nrdatas; i++) {
62 			if (rrset->rri_rdatas[i].rdi_data == NULL)
63 				break;
64 			free(rrset->rri_rdatas[i].rdi_data);
65 		}
66 		free(rrset->rri_rdatas);
67 	}
68 
69 	if (rrset->rri_sigs) {
70 		for (i = 0; i < rrset->rri_nsigs; i++) {
71 			if (rrset->rri_sigs[i].rdi_data == NULL)
72 				break;
73 			free(rrset->rri_sigs[i].rdi_data);
74 		}
75 		free(rrset->rri_sigs);
76 	}
77 
78 	if (rrset->rri_name)
79 		free(rrset->rri_name);
80 	free(rrset);
81 }
82