1*d216d6b1Seric /* $OpenBSD: res_send.c,v 1.8 2014/03/26 18:13:15 eric 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>
19*d216d6b1Seric #include <sys/socket.h>
208082e013Seric #include <netinet/in.h>
21*d216d6b1Seric #include <netdb.h>
228082e013Seric
23*d216d6b1Seric #include <asr.h>
248082e013Seric #include <errno.h>
258082e013Seric #include <resolv.h>
268082e013Seric #include <string.h>
2735d50e37Sderaadt #include <stdlib.h>
288082e013Seric
298082e013Seric int
res_send(const u_char * buf,int buflen,u_char * ans,int anslen)308082e013Seric res_send(const u_char *buf, int buflen, 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 errno = EINVAL;
408082e013Seric return (-1);
418082e013Seric }
428082e013Seric
43c5221d45Seric as = res_send_async(buf, buflen, NULL);
448082e013Seric if (as == NULL)
458082e013Seric return (-1); /* errno set */
468082e013Seric
475be03f8fSeric asr_run_sync(as, &ar);
488082e013Seric
498082e013Seric if (ar.ar_errno) {
508082e013Seric errno = ar.ar_errno;
518082e013Seric return (-1);
528082e013Seric }
538082e013Seric
54894123d3Seric len = anslen;
55894123d3Seric if (ar.ar_datalen < len)
56894123d3Seric len = ar.ar_datalen;
57894123d3Seric memmove(ans, ar.ar_data, len);
58894123d3Seric free(ar.ar_data);
59894123d3Seric
608082e013Seric return (ar.ar_datalen);
618082e013Seric }
62