xref: /openbsd-src/lib/libc/asr/res_query.c (revision c8503c07ccb750253efac312844687992be1a829)
1*c8503c07Sguenther /*	$OpenBSD: res_query.c,v 1.9 2015/10/05 02:57:16 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 <string.h>
2735d50e37Sderaadt #include <stdlib.h>
288082e013Seric 
298082e013Seric int
res_query(const char * name,int class,int type,u_char * ans,int anslen)308082e013Seric res_query(const char *name, int class, int type, u_char *ans, int anslen)
318082e013Seric {
325be03f8fSeric 	struct asr_query *as;
335be03f8fSeric 	struct asr_result ar;
34894123d3Seric 	size_t		 len;
358082e013Seric 
361ed934d0Seric 	res_init();
371ed934d0Seric 
388082e013Seric 	if (ans == NULL || anslen <= 0) {
398082e013Seric 		h_errno = NO_RECOVERY;
408082e013Seric 		errno = EINVAL;
418082e013Seric 		return (-1);
428082e013Seric 	}
438082e013Seric 
44c5221d45Seric 	as = res_query_async(name, class, type, NULL);
458082e013Seric 	if (as == NULL) {
468082e013Seric 		if (errno == EINVAL)
478082e013Seric 			h_errno = NO_RECOVERY;
488082e013Seric 		else
498082e013Seric 			h_errno = NETDB_INTERNAL;
508082e013Seric 		return (-1); /* errno set */
518082e013Seric 	}
528082e013Seric 
535be03f8fSeric 	asr_run_sync(as, &ar);
548082e013Seric 
558082e013Seric 	if (ar.ar_errno)
568082e013Seric 		errno = ar.ar_errno;
578082e013Seric 	h_errno = ar.ar_h_errno;
588082e013Seric 
598082e013Seric 	if (ar.ar_h_errno != NETDB_SUCCESS)
608082e013Seric 		return (-1);
618082e013Seric 
62894123d3Seric 	len = anslen;
63894123d3Seric 	if (ar.ar_datalen < len)
64894123d3Seric 		len = ar.ar_datalen;
65894123d3Seric 	memmove(ans, ar.ar_data, len);
66894123d3Seric 	free(ar.ar_data);
67894123d3Seric 
688082e013Seric 	return (ar.ar_datalen);
698082e013Seric }
70*c8503c07Sguenther DEF_WEAK(res_query);
718082e013Seric 
728082e013Seric int
res_search(const char * name,int class,int type,u_char * ans,int anslen)738082e013Seric res_search(const char *name, int class, int type, u_char *ans, int anslen)
748082e013Seric {
755be03f8fSeric 	struct asr_query *as;
765be03f8fSeric 	struct asr_result ar;
77894123d3Seric 	size_t		 len;
788082e013Seric 
791ed934d0Seric 	res_init();
801ed934d0Seric 
818082e013Seric 	if (ans == NULL || anslen <= 0) {
828082e013Seric 		h_errno = NO_RECOVERY;
838082e013Seric 		errno = EINVAL;
848082e013Seric 		return (-1);
858082e013Seric 	}
868082e013Seric 
87c5221d45Seric 	as = res_search_async(name, class, type, NULL);
888082e013Seric 	if (as == NULL) {
898082e013Seric 		if (errno == EINVAL)
908082e013Seric 			h_errno = NO_RECOVERY;
918082e013Seric 		else
928082e013Seric 			h_errno = NETDB_INTERNAL;
938082e013Seric 		return (-1); /* errno set */
948082e013Seric 	}
958082e013Seric 
965be03f8fSeric 	asr_run_sync(as, &ar);
978082e013Seric 
988082e013Seric 	if (ar.ar_errno)
998082e013Seric 		errno = ar.ar_errno;
1008082e013Seric 	h_errno = ar.ar_h_errno;
1018082e013Seric 
1028082e013Seric 	if (ar.ar_h_errno != NETDB_SUCCESS)
1038082e013Seric 		return (-1);
1048082e013Seric 
105894123d3Seric 	len = anslen;
106894123d3Seric 	if (ar.ar_datalen < len)
107894123d3Seric 		len = ar.ar_datalen;
108894123d3Seric 	memmove(ans, ar.ar_data, len);
109894123d3Seric 	free(ar.ar_data);
110894123d3Seric 
1118082e013Seric 	return (ar.ar_datalen);
1128082e013Seric }
113