summaryrefslogtreecommitdiff
path: root/lib/libc/asr
diff options
context:
space:
mode:
Diffstat (limited to 'lib/libc/asr')
-rw-r--r--lib/libc/asr/asr.c4
-rw-r--r--lib/libc/asr/asr.h11
-rw-r--r--lib/libc/asr/asr_private.h12
-rw-r--r--lib/libc/asr/async_resolver.318
-rw-r--r--lib/libc/asr/getaddrinfo_async.c6
-rw-r--r--lib/libc/asr/gethostnamadr_async.c6
-rw-r--r--lib/libc/asr/getnetnamadr_async.c6
-rw-r--r--lib/libc/asr/getrrsetbyname_async.c6
-rw-r--r--lib/libc/asr/res_query.c6
-rw-r--r--lib/libc/asr/res_search_async.c37
-rw-r--r--lib/libc/asr/res_send.c4
-rw-r--r--lib/libc/asr/res_send_async.c61
12 files changed, 55 insertions, 122 deletions
diff --git a/lib/libc/asr/asr.c b/lib/libc/asr/asr.c
index 5bde5097142..1aea2bd4e7c 100644
--- a/lib/libc/asr/asr.c
+++ b/lib/libc/asr/asr.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: asr.c,v 1.24 2013/04/17 19:13:23 otto Exp $ */
+/* $OpenBSD: asr.c,v 1.25 2013/04/30 12:02:39 eric Exp $ */
/*
* Copyright (c) 2010-2012 Eric Faurot <eric@openbsd.org>
*
@@ -277,7 +277,7 @@ async_free(struct async *as)
close(as->as_fd);
if (as->as.dns.obuf && !(as->as.dns.flags & ASYNC_EXTOBUF))
free(as->as.dns.obuf);
- if (as->as.dns.ibuf && !(as->as.dns.flags & ASYNC_EXTIBUF))
+ if (as->as.dns.ibuf)
free(as->as.dns.ibuf);
if (as->as.dns.dname)
free(as->as.dns.dname);
diff --git a/lib/libc/asr/asr.h b/lib/libc/asr/asr.h
index 8f9f11729c6..b0513b8f167 100644
--- a/lib/libc/asr/asr.h
+++ b/lib/libc/asr/asr.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: asr.h,v 1.5 2013/04/01 15:49:54 deraadt Exp $ */
+/* $OpenBSD: asr.h,v 1.6 2013/04/30 12:02:39 eric Exp $ */
/*
* Copyright (c) 2012 Eric Faurot <eric@openbsd.org>
*
@@ -80,12 +80,9 @@ void async_resolver_done(struct asr *);
/* Async version of the resolver API */
-struct async *res_send_async(const unsigned char *, int, unsigned char *, int,
- struct asr *);
-struct async *res_query_async(const char *, int, int, unsigned char *, int,
- struct asr *);
-struct async *res_search_async(const char *, int, int, unsigned char *, int,
- struct asr *);
+struct async *res_send_async(const unsigned char *, int, struct asr *);
+struct async *res_query_async(const char *, int, int, struct asr *);
+struct async *res_search_async(const char *, int, int, struct asr *);
struct async *getrrsetbyname_async(const char *, unsigned int, unsigned int,
unsigned int, struct asr *);
diff --git a/lib/libc/asr/asr_private.h b/lib/libc/asr/asr_private.h
index f5f524c77f1..275d18eb576 100644
--- a/lib/libc/asr/asr_private.h
+++ b/lib/libc/asr/asr_private.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: asr_private.h,v 1.16 2013/04/02 16:38:37 eric Exp $ */
+/* $OpenBSD: asr_private.h,v 1.17 2013/04/30 12:02:39 eric Exp $ */
/*
* Copyright (c) 2012 Eric Faurot <eric@openbsd.org>
*
@@ -164,7 +164,6 @@ struct asr {
#define ASYNC_NODATA 0x00000100
#define ASYNC_AGAIN 0x00000200
-#define ASYNC_EXTIBUF 0x00001000
#define ASYNC_EXTOBUF 0x00002000
@@ -217,9 +216,6 @@ struct async {
char *name;
struct async *subq;
int saved_h_errno;
- unsigned char *ibuf;
- size_t ibuflen;
- size_t ibufsize;
} search;
struct {
@@ -330,10 +326,8 @@ int asr_iter_domain(struct async *, const char *, char *, size_t);
int asr_parse_namedb_line(FILE *, char **, int);
/* <*>_async.h */
-struct async *res_query_async_ctx(const char *, int, int, unsigned char *, int,
- struct asr_ctx *);
-struct async *res_search_async_ctx(const char *, int, int, unsigned char *, int,
- struct asr_ctx *);
+struct async *res_query_async_ctx(const char *, int, int, struct asr_ctx *);
+struct async *res_search_async_ctx(const char *, int, int, struct asr_ctx *);
struct async *gethostbyaddr_async_ctx(const void *, socklen_t, int,
struct asr_ctx *);
diff --git a/lib/libc/asr/async_resolver.3 b/lib/libc/asr/async_resolver.3
index 38f71f4ca81..10216920faf 100644
--- a/lib/libc/asr/async_resolver.3
+++ b/lib/libc/asr/async_resolver.3
@@ -1,4 +1,4 @@
-.\" $OpenBSD: async_resolver.3,v 1.10 2013/04/01 15:49:54 deraadt Exp $
+.\" $OpenBSD: async_resolver.3,v 1.11 2013/04/30 12:02:39 eric Exp $
.\"
.\" Copyright (c) 2012, Eric Faurot <eric@openbsd.org>
.\"
@@ -14,7 +14,7 @@
.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
.\"
-.Dd $Mdocdate: April 1 2013 $
+.Dd $Mdocdate: April 30 2013 $
.Dt ASYN_RESOLVER 3
.Os
.Sh NAME
@@ -49,9 +49,11 @@
.Ft void
.Fn async_abort "struct async *as"
.Ft struct async *
-.Fn res_send_async "const unsigned char *pkt" "int pktlen" "unsigned char *ans" "int anslen" "struct asr *asr"
+.Fn res_send_async "const unsigned char *pkt" "int pktlen" "struct asr *asr"
.Ft struct async *
-.Fn res_query_async "const char *name" "int class" "int type" "unsigned char *ans" "int anslen" "struct asr *asr"
+.Fn res_query_async "const char *name" "int class" "int type" "struct asr *asr"
+.Ft struct async *
+.Fn res_search_async "const char *name" "int class" "int type" "struct asr *asr"
.Ft struct async *
.Fn getrrsetbyname_async "const char *hostname" "unsigned int rdclass" "unsigned int rdtype" "unsigned int flags" "struct asr *asr"
.Ft struct async *
@@ -255,8 +257,8 @@ The
and
.Fn res_search_async
functions are asynchronous versions of the standard libc resolver routines.
-Their interface is very similar, except that they take a resolver context as
-last argument, and the return value is found upon completion in the
+Their interface is very similar, except that the response buffer is always
+allocated internally. The return value is found upon completion in the
.Fa ar_datalen
member of the response structure.
In addition, the
@@ -266,8 +268,8 @@ union contains the address of the DNS server that sent the response,
contains the code returned by the server in the DNS response packet, and
.Fa ar_count
contains the number of answers in the packet.
-If no answer buffer is provided, a new one is allocated to fit the response
-and returned as the
+If a response is received it placed in a newly allocated buffer
+and returned as
.Fa ar_data
member.
This buffer must be freed by the caller.
diff --git a/lib/libc/asr/getaddrinfo_async.c b/lib/libc/asr/getaddrinfo_async.c
index 68824865714..30b2a6b1e41 100644
--- a/lib/libc/asr/getaddrinfo_async.c
+++ b/lib/libc/asr/getaddrinfo_async.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: getaddrinfo_async.c,v 1.16 2013/04/30 06:53:07 eric Exp $ */
+/* $OpenBSD: getaddrinfo_async.c,v 1.17 2013/04/30 12:02:39 eric Exp $ */
/*
* Copyright (c) 2012 Eric Faurot <eric@openbsd.org>
*
@@ -296,13 +296,13 @@ getaddrinfo_async_run(struct async *as, struct async_res *ar)
if (as->as.ai.fqdn) {
as->as.ai.subq = res_query_async_ctx(
as->as.ai.fqdn, C_IN,
- (family == AF_INET6) ? T_AAAA : T_A, NULL, 0,
+ (family == AF_INET6) ? T_AAAA : T_A,
as->as_ctx);
}
else {
as->as.ai.subq = res_search_async_ctx(
as->as.ai.hostname, C_IN,
- (family == AF_INET6) ? T_AAAA : T_A, NULL, 0,
+ (family == AF_INET6) ? T_AAAA : T_A,
as->as_ctx);
}
if (as->as.ai.subq == NULL) {
diff --git a/lib/libc/asr/gethostnamadr_async.c b/lib/libc/asr/gethostnamadr_async.c
index 1cbd1289e6e..2dd251382bf 100644
--- a/lib/libc/asr/gethostnamadr_async.c
+++ b/lib/libc/asr/gethostnamadr_async.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: gethostnamadr_async.c,v 1.18 2013/04/14 22:23:08 deraadt Exp $ */
+/* $OpenBSD: gethostnamadr_async.c,v 1.19 2013/04/30 12:02:39 eric Exp $ */
/*
* Copyright (c) 2012 Eric Faurot <eric@openbsd.org>
*
@@ -216,13 +216,13 @@ gethostnamadr_async_run(struct async *as, struct async_res *ar)
T_A : T_AAAA;
as->as.hostnamadr.subq = res_search_async_ctx(
as->as.hostnamadr.name,
- C_IN, type, NULL, 0, as->as_ctx);
+ C_IN, type, as->as_ctx);
} else {
addr_as_fqdn(as->as.hostnamadr.addr,
as->as.hostnamadr.family,
dname, sizeof(dname));
as->as.hostnamadr.subq = res_query_async_ctx(
- dname, C_IN, T_PTR, NULL, 0, as->as_ctx);
+ dname, C_IN, T_PTR, as->as_ctx);
}
if (as->as.hostnamadr.subq == NULL) {
diff --git a/lib/libc/asr/getnetnamadr_async.c b/lib/libc/asr/getnetnamadr_async.c
index 644ba66ca0e..c7da0cf4b82 100644
--- a/lib/libc/asr/getnetnamadr_async.c
+++ b/lib/libc/asr/getnetnamadr_async.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: getnetnamadr_async.c,v 1.8 2013/04/01 15:49:54 deraadt Exp $ */
+/* $OpenBSD: getnetnamadr_async.c,v 1.9 2013/04/30 12:02:39 eric Exp $ */
/*
* Copyright (c) 2012 Eric Faurot <eric@openbsd.org>
*
@@ -148,7 +148,7 @@ getnetnamadr_async_run(struct async *as, struct async_res *ar)
type = T_PTR;
name = as->as.netnamadr.name;
as->as.netnamadr.subq = res_search_async_ctx(
- name, C_IN, type, NULL, 0, as->as_ctx);
+ name, C_IN, type, as->as_ctx);
} else {
type = T_PTR;
name = dname;
@@ -158,7 +158,7 @@ getnetnamadr_async_run(struct async *as, struct async_res *ar)
as->as.netnamadr.family,
dname, sizeof(dname));
as->as.netnamadr.subq = res_query_async_ctx(
- name, C_IN, type, NULL, 0, as->as_ctx);
+ name, C_IN, type, as->as_ctx);
}
if (as->as.netnamadr.subq == NULL) {
diff --git a/lib/libc/asr/getrrsetbyname_async.c b/lib/libc/asr/getrrsetbyname_async.c
index 540d13bd4fe..1e0e77617dc 100644
--- a/lib/libc/asr/getrrsetbyname_async.c
+++ b/lib/libc/asr/getrrsetbyname_async.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: getrrsetbyname_async.c,v 1.3 2012/11/24 15:12:48 eric Exp $ */
+/* $OpenBSD: getrrsetbyname_async.c,v 1.4 2013/04/30 12:02:39 eric Exp $ */
/*
* Copyright (c) 2012 Eric Faurot <eric@openbsd.org>
*
@@ -97,7 +97,7 @@ getrrsetbyname_async_run(struct async *as, struct async_res *ar)
as->as.rrset.name,
as->as.rrset.class,
as->as.rrset.type,
- NULL, 0, as->as_ctx);
+ as->as_ctx);
if (as->as.rrset.subq == NULL) {
ar->ar_rrset_errno = ERRSET_FAIL;
async_set_state(as, ASR_STATE_HALT);
@@ -169,7 +169,7 @@ getrrsetbyname_async_run(struct async *as, struct async_res *ar)
/* The rest of this file is taken from the orignal implementation. */
-/* $OpenBSD: getrrsetbyname_async.c,v 1.3 2012/11/24 15:12:48 eric Exp $ */
+/* $OpenBSD: getrrsetbyname_async.c,v 1.4 2013/04/30 12:02:39 eric Exp $ */
/*
* Copyright (c) 2001 Jakob Schlyter. All rights reserved.
diff --git a/lib/libc/asr/res_query.c b/lib/libc/asr/res_query.c
index 7351c8750a5..7a6a1db0ca4 100644
--- a/lib/libc/asr/res_query.c
+++ b/lib/libc/asr/res_query.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: res_query.c,v 1.2 2013/04/08 20:03:15 eric Exp $ */
+/* $OpenBSD: res_query.c,v 1.3 2013/04/30 12:02:39 eric Exp $ */
/*
* Copyright (c) 2012 Eric Faurot <eric@openbsd.org>
*
@@ -37,7 +37,7 @@ res_query(const char *name, int class, int type, u_char *ans, int anslen)
return (-1);
}
- as = res_query_async(name, class, type, NULL, 0, NULL);
+ as = res_query_async(name, class, type, NULL);
if (as == NULL) {
if (errno == EINVAL)
h_errno = NO_RECOVERY;
@@ -77,7 +77,7 @@ res_search(const char *name, int class, int type, u_char *ans, int anslen)
return (-1);
}
- as = res_search_async(name, class, type, NULL, 0, NULL);
+ as = res_search_async(name, class, type, NULL);
if (as == NULL) {
if (errno == EINVAL)
h_errno = NO_RECOVERY;
diff --git a/lib/libc/asr/res_search_async.c b/lib/libc/asr/res_search_async.c
index fae802f5e93..060a545eafa 100644
--- a/lib/libc/asr/res_search_async.c
+++ b/lib/libc/asr/res_search_async.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: res_search_async.c,v 1.6 2013/04/01 20:22:27 eric Exp $ */
+/* $OpenBSD: res_search_async.c,v 1.7 2013/04/30 12:02:39 eric Exp $ */
/*
* Copyright (c) 2012 Eric Faurot <eric@openbsd.org>
*
@@ -25,13 +25,6 @@
#include <string.h>
#include <unistd.h>
-/*
- * TODO:
- *
- * - make it possible to reuse ibuf if it was NULL when first called,
- * to avoid reallocating buffers everytime.
- */
-
#include "asr.h"
#include "asr_private.h"
@@ -42,8 +35,7 @@ static int res_search_async_run(struct async *, struct async_res *);
* h_errno is NETDB_SUCCESS.
*/
struct async *
-res_search_async(const char *name, int class, int type, unsigned char *ans,
- int anslen, struct asr *asr)
+res_search_async(const char *name, int class, int type, struct asr *asr)
{
struct asr_ctx *ac;
struct async *as;
@@ -51,15 +43,14 @@ res_search_async(const char *name, int class, int type, unsigned char *ans,
DPRINT("asr: res_search_async(\"%s\", %i, %i)\n", name, class, type);
ac = asr_use_resolver(asr);
- as = res_search_async_ctx(name, class, type, ans, anslen, ac);
+ as = res_search_async_ctx(name, class, type, ac);
asr_ctx_unref(ac);
return (as);
}
struct async *
-res_search_async_ctx(const char *name, int class, int type, unsigned char *ans,
- int anslen, struct asr_ctx *ac)
+res_search_async_ctx(const char *name, int class, int type, struct asr_ctx *ac)
{
struct async *as;
@@ -72,16 +63,6 @@ res_search_async_ctx(const char *name, int class, int type, unsigned char *ans,
if ((as->as.search.name = strdup(name)) == NULL)
goto err; /* errno set */
- if (ans) {
- as->as.search.flags |= ASYNC_EXTIBUF;
- as->as.search.ibuf = ans;
- as->as.search.ibufsize = anslen;
- } else {
- as->as.search.ibuf = NULL;
- as->as.search.ibufsize = 0;
- }
- as->as.search.ibuflen = 0;
-
as->as.search.class = class;
as->as.search.type = type;
@@ -130,8 +111,7 @@ res_search_async_run(struct async *as, struct async_res *ar)
break;
}
as->as.search.subq = res_query_async_ctx(fqdn,
- as->as.search.class, as->as.search.type,
- as->as.search.ibuf, as->as.search.ibufsize, as->as_ctx);
+ as->as.search.class, as->as.search.type, as->as_ctx);
if (as->as.search.subq == NULL) {
ar->ar_errno = errno;
if (errno == EINVAL)
@@ -169,12 +149,7 @@ res_search_async_run(struct async *as, struct async_res *ar)
break;
}
- /*
- * If we don't use an external buffer, the packet was allocated
- * by the subquery and it must be freed now.
- */
- if ((as->as.search.flags & ASYNC_EXTIBUF) == 0)
- free(ar->ar_data);
+ free(ar->ar_data);
/*
* The original resolver does something like this.
diff --git a/lib/libc/asr/res_send.c b/lib/libc/asr/res_send.c
index d1379c6a67b..5135a8d15da 100644
--- a/lib/libc/asr/res_send.c
+++ b/lib/libc/asr/res_send.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: res_send.c,v 1.2 2013/04/08 20:03:15 eric Exp $ */
+/* $OpenBSD: res_send.c,v 1.3 2013/04/30 12:02:39 eric Exp $ */
/*
* Copyright (c) 2012 Eric Faurot <eric@openbsd.org>
*
@@ -36,7 +36,7 @@ res_send(const u_char *buf, int buflen, u_char *ans, int anslen)
return (-1);
}
- as = res_send_async(buf, buflen, NULL, 0, NULL);
+ as = res_send_async(buf, buflen, NULL);
if (as == NULL)
return (-1); /* errno set */
diff --git a/lib/libc/asr/res_send_async.c b/lib/libc/asr/res_send_async.c
index 7a91d190b83..2a661a12e95 100644
--- a/lib/libc/asr/res_send_async.c
+++ b/lib/libc/asr/res_send_async.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: res_send_async.c,v 1.16 2013/04/14 22:23:08 deraadt Exp $ */
+/* $OpenBSD: res_send_async.c,v 1.17 2013/04/30 12:02:39 eric Exp $ */
/*
* Copyright (c) 2012 Eric Faurot <eric@openbsd.org>
*
@@ -49,8 +49,7 @@ static int ensure_ibuf(struct async *, size_t);
struct async *
-res_send_async(const unsigned char *buf, int buflen, unsigned char *ans,
- int anslen, struct asr *asr)
+res_send_async(const unsigned char *buf, int buflen, struct asr *asr)
{
struct asr_ctx *ac;
struct async *as;
@@ -67,17 +66,6 @@ res_send_async(const unsigned char *buf, int buflen, unsigned char *ans,
}
as->as_run = res_send_async_run;
- if (ans) {
- as->as.dns.flags |= ASYNC_EXTIBUF;
- as->as.dns.ibuf = ans;
- as->as.dns.ibufsize = anslen;
- as->as.dns.ibuflen = 0;
- } else {
- as->as.dns.ibuf = NULL;
- as->as.dns.ibufsize = 0;
- as->as.dns.ibuflen = 0;
- }
-
as->as.dns.flags |= ASYNC_EXTOBUF;
as->as.dns.obuf = (unsigned char *)buf;
as->as.dns.obuflen = buflen;
@@ -113,8 +101,7 @@ res_send_async(const unsigned char *buf, int buflen, unsigned char *ans,
* (ans == NULL).
*/
struct async *
-res_query_async(const char *name, int class, int type, unsigned char *ans,
- int anslen, struct asr *asr)
+res_query_async(const char *name, int class, int type, struct asr *asr)
{
struct asr_ctx *ac;
struct async *as;
@@ -122,15 +109,14 @@ res_query_async(const char *name, int class, int type, unsigned char *ans,
DPRINT("asr: res_query_async(\"%s\", %i, %i)\n", name, class, type);
ac = asr_use_resolver(asr);
- as = res_query_async_ctx(name, class, type, ans, anslen, ac);
+ as = res_query_async_ctx(name, class, type, ac);
asr_ctx_unref(ac);
return (as);
}
struct async *
-res_query_async_ctx(const char *name, int class, int type, unsigned char *ans,
- int anslen, struct asr_ctx *a_ctx)
+res_query_async_ctx(const char *name, int class, int type, struct asr_ctx *a_ctx)
{
struct async *as;
@@ -140,16 +126,6 @@ res_query_async_ctx(const char *name, int class, int type, unsigned char *ans,
return (NULL); /* errno set */
as->as_run = res_send_async_run;
- if (ans) {
- as->as.dns.flags |= ASYNC_EXTIBUF;
- as->as.dns.ibuf = ans;
- as->as.dns.ibufsize = anslen;
- } else {
- as->as.dns.ibuf = NULL;
- as->as.dns.ibufsize = 0;
- }
- as->as.dns.ibuflen = 0;
-
/* This adds a "." to name if it doesn't already has one.
* That's how res_query() behaves (through res_mkquery").
*/
@@ -478,15 +454,12 @@ udp_recv(struct async *as)
ssize_t n;
int save_errno;
- /* Allocate input buf if needed */
- if (as->as.dns.ibuf == NULL) {
- if (ensure_ibuf(as, PACKETSZ) == -1) {
- save_errno = errno;
- close(as->as_fd);
- errno = save_errno;
- as->as_fd = -1;
- return (-1);
- }
+ if (ensure_ibuf(as, PACKETSZ) == -1) {
+ save_errno = errno;
+ close(as->as_fd);
+ errno = save_errno;
+ as->as_fd = -1;
+ return (-1);
}
n = recv(as->as_fd, as->as.dns.ibuf, as->as.dns.ibufsize, 0);
@@ -664,22 +637,14 @@ close:
}
/*
- * Make sure the input buffer is at least "n" bytes long.
- * If not (or not allocated) allocated enough space, unless the
- * buffer is external (owned by the caller), in which case it fails.
+ * Make sure the input buffer is at least "n" bytes long, and allocate or
+ * extend it if necessary. Return 0 on success, or set errno and return -1.
*/
static int
ensure_ibuf(struct async *as, size_t n)
{
char *t;
- if (as->as.dns.flags & ASYNC_EXTIBUF) {
- if (n <= as->as.dns.ibufsize)
- return (0);
- errno = EINVAL;
- return (-1);
- }
-
if (as->as.dns.ibuf == NULL) {
as->as.dns.ibuf = malloc(n);
if (as->as.dns.ibuf == NULL)