xref: /openbsd-src/lib/libc/asr/getrrsetbyname.c (revision 8082e013b3e34cd58819c630b3b36d768ac74f45)
1 /*	$OpenBSD: getrrsetbyname.c,v 1.1 2012/09/08 11:08:21 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 	as = getrrsetbyname_async(name, class, type, flags, NULL);
36 	if (as == NULL) {
37 		r = (errno == ENOMEM) ? ERRSET_NOMEMORY : ERRSET_FAIL;
38 		errno = saved_errno;
39 		return (r);
40 	}
41 
42 	async_run_sync(as, &ar);
43 
44 	*res = ar.ar_rrsetinfo;
45 
46 	return (ar.ar_rrset_errno);
47 }
48 
49 /* from net/getrrsetbyname.c */
50 void
51 freerrset(struct rrsetinfo *rrset)
52 {
53 	u_int16_t i;
54 
55 	if (rrset == NULL)
56 		return;
57 
58 	if (rrset->rri_rdatas) {
59 		for (i = 0; i < rrset->rri_nrdatas; i++) {
60 			if (rrset->rri_rdatas[i].rdi_data == NULL)
61 				break;
62 			free(rrset->rri_rdatas[i].rdi_data);
63 		}
64 		free(rrset->rri_rdatas);
65 	}
66 
67 	if (rrset->rri_sigs) {
68 		for (i = 0; i < rrset->rri_nsigs; i++) {
69 			if (rrset->rri_sigs[i].rdi_data == NULL)
70 				break;
71 			free(rrset->rri_sigs[i].rdi_data);
72 		}
73 		free(rrset->rri_sigs);
74 	}
75 
76 	if (rrset->rri_name)
77 		free(rrset->rri_name);
78 	free(rrset);
79 }
80