Skip to content

Commit

Permalink
evdns: export cancel via callbacks in util (like async lib core/extra…
Browse files Browse the repository at this point in the history
… issues)
  • Loading branch information
azat committed Mar 23, 2016
1 parent 334340d commit 8cbe65d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
1 change: 1 addition & 0 deletions evdns.c
Expand Up @@ -3908,6 +3908,7 @@ evdns_base_new(struct event_base *event_base, int flags)
* functionality. We can't just call evdns_getaddrinfo directly or
* else libevent-core will depend on libevent-extras. */
evutil_set_evdns_getaddrinfo_fn_(evdns_getaddrinfo);
evutil_set_evdns_getaddrinfo_cancel_fn_(evdns_getaddrinfo_cancel);

base = mm_malloc(sizeof(struct evdns_base));
if (base == NULL)
Expand Down
22 changes: 18 additions & 4 deletions evutil.c
Expand Up @@ -1546,34 +1546,48 @@ evutil_freeaddrinfo(struct evutil_addrinfo *ai)
}

static evdns_getaddrinfo_fn evdns_getaddrinfo_impl = NULL;
static evdns_getaddrinfo_cancel_fn evdns_getaddrinfo_cancel_impl = NULL;

void
evutil_set_evdns_getaddrinfo_fn_(evdns_getaddrinfo_fn fn)
{
if (!evdns_getaddrinfo_impl)
evdns_getaddrinfo_impl = fn;
}
void
evutil_set_evdns_getaddrinfo_cancel_fn_(evdns_getaddrinfo_cancel_fn fn)
{
if (!evdns_getaddrinfo_cancel_impl)
evdns_getaddrinfo_cancel_impl = fn;
}

/* Internal helper function: act like evdns_getaddrinfo if dns_base is set;
* otherwise do a blocking resolve and pass the result to the callback in the
* way that evdns_getaddrinfo would.
*/
int
evutil_getaddrinfo_async_(struct evdns_base *dns_base,
struct evdns_getaddrinfo_request *evutil_getaddrinfo_async_(
struct evdns_base *dns_base,
const char *nodename, const char *servname,
const struct evutil_addrinfo *hints_in,
void (*cb)(int, struct evutil_addrinfo *, void *), void *arg)
{
if (dns_base && evdns_getaddrinfo_impl) {
evdns_getaddrinfo_impl(
return evdns_getaddrinfo_impl(
dns_base, nodename, servname, hints_in, cb, arg);
} else {
struct evutil_addrinfo *ai=NULL;
int err;
err = evutil_getaddrinfo(nodename, servname, hints_in, &ai);
cb(err, ai, arg);
return NULL;
}
}

void evutil_getaddrinfo_cancel_async_(struct evdns_getaddrinfo_request *data)
{
if (evdns_getaddrinfo_cancel_impl && data) {
evdns_getaddrinfo_cancel_impl(data);
}
return 0;
}

const char *
Expand Down
8 changes: 6 additions & 2 deletions util-internal.h
Expand Up @@ -357,8 +357,10 @@ typedef struct evdns_getaddrinfo_request* (*evdns_getaddrinfo_fn)(
const char *nodename, const char *servname,
const struct evutil_addrinfo *hints_in,
void (*cb)(int, struct evutil_addrinfo *, void *), void *arg);

void evutil_set_evdns_getaddrinfo_fn_(evdns_getaddrinfo_fn fn);
typedef void (*evdns_getaddrinfo_cancel_fn)(
struct evdns_getaddrinfo_request *req);
void evutil_set_evdns_getaddrinfo_cancel_fn_(evdns_getaddrinfo_cancel_fn fn);

struct evutil_addrinfo *evutil_new_addrinfo_(struct sockaddr *sa,
ev_socklen_t socklen, const struct evutil_addrinfo *hints);
Expand All @@ -368,10 +370,12 @@ void evutil_adjust_hints_for_addrconfig_(struct evutil_addrinfo *hints);
int evutil_getaddrinfo_common_(const char *nodename, const char *servname,
struct evutil_addrinfo *hints, struct evutil_addrinfo **res, int *portnum);

int evutil_getaddrinfo_async_(struct evdns_base *dns_base,
struct evdns_getaddrinfo_request *evutil_getaddrinfo_async_(
struct evdns_base *dns_base,
const char *nodename, const char *servname,
const struct evutil_addrinfo *hints_in,
void (*cb)(int, struct evutil_addrinfo *, void *), void *arg);
void evutil_getaddrinfo_cancel_async_(struct evdns_getaddrinfo_request *data);

/** Return true iff sa is a looback address. (That is, it is 127.0.0.1/8, or
* ::1). */
Expand Down

0 comments on commit 8cbe65d

Please sign in to comment.