1 |
|
|
/* $OpenBSD: res_send.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_send(const u_char *buf, int buflen, 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 |
|
|
errno = EINVAL; |
40 |
|
|
return (-1); |
41 |
|
|
} |
42 |
|
|
|
43 |
|
|
as = res_send_async(buf, buflen, NULL); |
44 |
|
|
if (as == NULL) |
45 |
|
|
return (-1); /* errno set */ |
46 |
|
|
|
47 |
|
|
asr_run_sync(as, &ar); |
48 |
|
|
|
49 |
|
|
if (ar.ar_errno) { |
50 |
|
|
errno = ar.ar_errno; |
51 |
|
|
return (-1); |
52 |
|
|
} |
53 |
|
|
|
54 |
|
|
len = anslen; |
55 |
|
|
if (ar.ar_datalen < len) |
56 |
|
|
len = ar.ar_datalen; |
57 |
|
|
memmove(ans, ar.ar_data, len); |
58 |
|
|
free(ar.ar_data); |
59 |
|
|
|
60 |
|
|
return (ar.ar_datalen); |
61 |
|
|
} |