From 967e358dbc93aa7a99c3c0ee2a634f1fedecba74 Mon Sep 17 00:00:00 2001 From: Miroslav Lichvar Date: Tue, 29 Sep 2015 17:39:27 +0200 Subject: [PATCH] stubs: don't call DNS_Name2IPAddress handler directly Instead of calling the handler directly schedule a timeout with zero delay for resolving to make the function behave similarly to the real asynchronous resolver. This should prevent problems with code that inadvertently depends on this behavior and which would break only when compiled without support for asynchronous resolving. --- nameserv_async.h | 3 +-- stubs.c | 35 ++++++++++++++++++++++++++++++----- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/nameserv_async.h b/nameserv_async.h index cea2d82..b8479e1 100644 --- a/nameserv_async.h +++ b/nameserv_async.h @@ -34,8 +34,7 @@ typedef void (*DNS_NameResolveHandler)(DNS_Status status, int n_addrs, IPAddr *ip_addrs, void *anything); /* Request resolving of a name to IP address. The handler will be - called when the result is available, but it may be also called - directly from this function call. */ + called when the result is available. */ extern void DNS_Name2IPAddressAsync(const char *name, DNS_NameResolveHandler handler, void *anything); #endif diff --git a/stubs.c b/stubs.c index 536176d..ac667f1 100644 --- a/stubs.c +++ b/stubs.c @@ -32,12 +32,14 @@ #include "keys.h" #include "logging.h" #include "manual.h" +#include "memory.h" #include "nameserv.h" #include "nameserv_async.h" #include "ntp_core.h" #include "ntp_io.h" #include "ntp_sources.h" #include "refclock.h" +#include "sched.h" #ifndef FEAT_ASYNCDNS @@ -45,20 +47,43 @@ /* This is a blocking implementation used when asynchronous resolving is not available */ -void -DNS_Name2IPAddressAsync(const char *name, DNS_NameResolveHandler handler, void *anything) +struct DNS_Async_Instance { + const char *name; + DNS_NameResolveHandler handler; + void *arg; +}; + +static void +resolve_name(void *anything) { + struct DNS_Async_Instance *inst; IPAddr addrs[MAX_ADDRESSES]; DNS_Status status; int i; - status = DNS_Name2IPAddress(name, addrs, MAX_ADDRESSES); + inst = (struct DNS_Async_Instance *)anything; + status = DNS_Name2IPAddress(inst->name, addrs, MAX_ADDRESSES); for (i = 0; status == DNS_Success && i < MAX_ADDRESSES && - addrs[i].family != IPADDR_UNSPEC; i++) + addrs[i].family != IPADDR_UNSPEC; i++) ; - (handler)(status, i, addrs, anything); + (inst->handler)(status, i, addrs, inst->arg); + + Free(inst); +} + +void +DNS_Name2IPAddressAsync(const char *name, DNS_NameResolveHandler handler, void *anything) +{ + struct DNS_Async_Instance *inst; + + inst = MallocNew(struct DNS_Async_Instance); + inst->name = name; + inst->handler = handler; + inst->arg = anything; + + SCH_AddTimeoutByDelay(0.0, resolve_name, inst); } #endif /* !FEAT_ASYNCDNS */