xref: /openbsd-src/lib/libc/asr/getrrsetbyname.c (revision 06201dd4d8b19e0383322797ffab28e23bd09e0e)
1*06201dd4Sguenther /*	$OpenBSD: getrrsetbyname.c,v 1.6 2015/09/14 07:38:37 guenther Exp $	*/
28082e013Seric /*
38082e013Seric  * Copyright (c) 2012 Eric Faurot <eric@openbsd.org>
48082e013Seric  *
58082e013Seric  * Permission to use, copy, modify, and distribute this software for any
68082e013Seric  * purpose with or without fee is hereby granted, provided that the above
78082e013Seric  * copyright notice and this permission notice appear in all copies.
88082e013Seric  *
98082e013Seric  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
108082e013Seric  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
118082e013Seric  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
128082e013Seric  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
138082e013Seric  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
148082e013Seric  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
158082e013Seric  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
168082e013Seric  */
178082e013Seric 
188082e013Seric #include <sys/types.h>
19d216d6b1Seric #include <sys/socket.h>
208082e013Seric #include <netinet/in.h>
21d216d6b1Seric #include <netdb.h>
228082e013Seric 
23d216d6b1Seric #include <asr.h>
248082e013Seric #include <errno.h>
258082e013Seric #include <resolv.h>
268082e013Seric #include <stdlib.h>
278082e013Seric 
288082e013Seric int
getrrsetbyname(const char * name,unsigned int class,unsigned int type,unsigned int flags,struct rrsetinfo ** res)298082e013Seric getrrsetbyname(const char *name, unsigned int class, unsigned int type,
308082e013Seric     unsigned int flags, struct rrsetinfo **res)
318082e013Seric {
325be03f8fSeric 	struct asr_query *as;
335be03f8fSeric 	struct asr_result ar;
348082e013Seric 	int r, saved_errno = errno;
358082e013Seric 
361ed934d0Seric 	res_init();
371ed934d0Seric 
388082e013Seric 	as = getrrsetbyname_async(name, class, type, flags, NULL);
398082e013Seric 	if (as == NULL) {
408082e013Seric 		r = (errno == ENOMEM) ? ERRSET_NOMEMORY : ERRSET_FAIL;
418082e013Seric 		errno = saved_errno;
428082e013Seric 		return (r);
438082e013Seric 	}
448082e013Seric 
455be03f8fSeric 	asr_run_sync(as, &ar);
468082e013Seric 
478082e013Seric 	*res = ar.ar_rrsetinfo;
488082e013Seric 
498082e013Seric 	return (ar.ar_rrset_errno);
508082e013Seric }
518082e013Seric 
528082e013Seric /* from net/getrrsetbyname.c */
538082e013Seric void
freerrset(struct rrsetinfo * rrset)548082e013Seric freerrset(struct rrsetinfo *rrset)
558082e013Seric {
568082e013Seric 	u_int16_t i;
578082e013Seric 
588082e013Seric 	if (rrset == NULL)
598082e013Seric 		return;
608082e013Seric 
618082e013Seric 	if (rrset->rri_rdatas) {
628082e013Seric 		for (i = 0; i < rrset->rri_nrdatas; i++) {
638082e013Seric 			if (rrset->rri_rdatas[i].rdi_data == NULL)
648082e013Seric 				break;
658082e013Seric 			free(rrset->rri_rdatas[i].rdi_data);
668082e013Seric 		}
678082e013Seric 		free(rrset->rri_rdatas);
688082e013Seric 	}
698082e013Seric 
708082e013Seric 	if (rrset->rri_sigs) {
718082e013Seric 		for (i = 0; i < rrset->rri_nsigs; i++) {
728082e013Seric 			if (rrset->rri_sigs[i].rdi_data == NULL)
738082e013Seric 				break;
748082e013Seric 			free(rrset->rri_sigs[i].rdi_data);
758082e013Seric 		}
768082e013Seric 		free(rrset->rri_sigs);
778082e013Seric 	}
788082e013Seric 
798082e013Seric 	if (rrset->rri_name)
808082e013Seric 		free(rrset->rri_name);
818082e013Seric 	free(rrset);
828082e013Seric }
83*06201dd4Sguenther DEF_WEAK(freerrset);
84