xref: /openbsd-src/lib/libc/asr/res_query.c (revision d216d6b1290c53cb63da2778d039467b2c88efcf)
1 /*	$OpenBSD: res_query.c,v 1.8 2014/03/26 18:13:15 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 <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
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 
71 int
72 res_search(const char *name, int class, int type, u_char *ans, int anslen)
73 {
74 	struct asr_query *as;
75 	struct asr_result ar;
76 	size_t		 len;
77 
78 	res_init();
79 
80 	if (ans == NULL || anslen <= 0) {
81 		h_errno = NO_RECOVERY;
82 		errno = EINVAL;
83 		return (-1);
84 	}
85 
86 	as = res_search_async(name, class, type, NULL);
87 	if (as == NULL) {
88 		if (errno == EINVAL)
89 			h_errno = NO_RECOVERY;
90 		else
91 			h_errno = NETDB_INTERNAL;
92 		return (-1); /* errno set */
93 	}
94 
95 	asr_run_sync(as, &ar);
96 
97 	if (ar.ar_errno)
98 		errno = ar.ar_errno;
99 	h_errno = ar.ar_h_errno;
100 
101 	if (ar.ar_h_errno != NETDB_SUCCESS)
102 		return (-1);
103 
104 	len = anslen;
105 	if (ar.ar_datalen < len)
106 		len = ar.ar_datalen;
107 	memmove(ans, ar.ar_data, len);
108 	free(ar.ar_data);
109 
110 	return (ar.ar_datalen);
111 }
112