xref: /openbsd-src/lib/libc/asr/res_query.c (revision 8082e013b3e34cd58819c630b3b36d768ac74f45)
1 /*	$OpenBSD: res_query.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 <string.h>
24 
25 #include "asr.h"
26 
27 int
28 res_query(const char *name, int class, int type, u_char *ans, int anslen)
29 {
30 	struct async	*as;
31 	struct async_res ar;
32 
33 	if (ans == NULL || anslen <= 0) {
34 		h_errno = NO_RECOVERY;
35 		errno = EINVAL;
36 		return (-1);
37 	}
38 
39 	as = res_query_async(name, class, type, ans, anslen, NULL);
40 	if (as == NULL) {
41 		if (errno == EINVAL)
42 			h_errno = NO_RECOVERY;
43 		else
44 			h_errno = NETDB_INTERNAL;
45 		return (-1); /* errno set */
46 	}
47 
48 	async_run_sync(as, &ar);
49 
50 	if (ar.ar_errno)
51 		errno = ar.ar_errno;
52 	h_errno = ar.ar_h_errno;
53 
54 	if (ar.ar_h_errno != NETDB_SUCCESS)
55 		return (-1);
56 
57 	return (ar.ar_datalen);
58 }
59 
60 int
61 res_search(const char *name, int class, int type, u_char *ans, int anslen)
62 {
63 	struct async	*as;
64 	struct async_res ar;
65 
66 	if (ans == NULL || anslen <= 0) {
67 		h_errno = NO_RECOVERY;
68 		errno = EINVAL;
69 		return (-1);
70 	}
71 
72 	as = res_search_async(name, class, type, ans, anslen, NULL);
73 	if (as == NULL) {
74 		if (errno == EINVAL)
75 			h_errno = NO_RECOVERY;
76 		else
77 			h_errno = NETDB_INTERNAL;
78 		return (-1); /* errno set */
79 	}
80 
81 	async_run_sync(as, &ar);
82 
83 	if (ar.ar_errno)
84 		errno = ar.ar_errno;
85 	h_errno = ar.ar_h_errno;
86 
87 	if (ar.ar_h_errno != NETDB_SUCCESS)
88 		return (-1);
89 
90 	return (ar.ar_datalen);
91 }
92