xref: /openbsd/lib/libc/asr/res_query.c (revision c8503c07)
1 /*	$OpenBSD: res_query.c,v 1.9 2015/10/05 02:57:16 guenther 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 <sys/socket.h>
20 #include <netinet/in.h>
21 #include <netdb.h>
22 
23 #include <asr.h>
24 #include <errno.h>
25 #include <resolv.h>
26 #include <string.h>
27 #include <stdlib.h>
28 
29 int
res_query(const char * name,int class,int type,u_char * ans,int anslen)30 res_query(const char *name, int class, int type, u_char *ans, int anslen)
31 {
32 	struct asr_query *as;
33 	struct asr_result ar;
34 	size_t		 len;
35 
36 	res_init();
37 
38 	if (ans == NULL || anslen <= 0) {
39 		h_errno = NO_RECOVERY;
40 		errno = EINVAL;
41 		return (-1);
42 	}
43 
44 	as = res_query_async(name, class, type, NULL);
45 	if (as == NULL) {
46 		if (errno == EINVAL)
47 			h_errno = NO_RECOVERY;
48 		else
49 			h_errno = NETDB_INTERNAL;
50 		return (-1); /* errno set */
51 	}
52 
53 	asr_run_sync(as, &ar);
54 
55 	if (ar.ar_errno)
56 		errno = ar.ar_errno;
57 	h_errno = ar.ar_h_errno;
58 
59 	if (ar.ar_h_errno != NETDB_SUCCESS)
60 		return (-1);
61 
62 	len = anslen;
63 	if (ar.ar_datalen < len)
64 		len = ar.ar_datalen;
65 	memmove(ans, ar.ar_data, len);
66 	free(ar.ar_data);
67 
68 	return (ar.ar_datalen);
69 }
70 DEF_WEAK(res_query);
71 
72 int
res_search(const char * name,int class,int type,u_char * ans,int anslen)73 res_search(const char *name, int class, int type, u_char *ans, int anslen)
74 {
75 	struct asr_query *as;
76 	struct asr_result ar;
77 	size_t		 len;
78 
79 	res_init();
80 
81 	if (ans == NULL || anslen <= 0) {
82 		h_errno = NO_RECOVERY;
83 		errno = EINVAL;
84 		return (-1);
85 	}
86 
87 	as = res_search_async(name, class, type, NULL);
88 	if (as == NULL) {
89 		if (errno == EINVAL)
90 			h_errno = NO_RECOVERY;
91 		else
92 			h_errno = NETDB_INTERNAL;
93 		return (-1); /* errno set */
94 	}
95 
96 	asr_run_sync(as, &ar);
97 
98 	if (ar.ar_errno)
99 		errno = ar.ar_errno;
100 	h_errno = ar.ar_h_errno;
101 
102 	if (ar.ar_h_errno != NETDB_SUCCESS)
103 		return (-1);
104 
105 	len = anslen;
106 	if (ar.ar_datalen < len)
107 		len = ar.ar_datalen;
108 	memmove(ans, ar.ar_data, len);
109 	free(ar.ar_data);
110 
111 	return (ar.ar_datalen);
112 }
113