]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
stubs: don't call DNS_Name2IPAddress handler directly
authorMiroslav Lichvar <mlichvar@redhat.com>
Tue, 29 Sep 2015 15:39:27 +0000 (17:39 +0200)
committerMiroslav Lichvar <mlichvar@redhat.com>
Tue, 29 Sep 2015 16:06:33 +0000 (18:06 +0200)
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
stubs.c

index cea2d82ccabbae37e36ea6401d448d8560e6d191..b8479e1df72616e9be07e46915c2a0b6b44cda8c 100644 (file)
@@ -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 536176d864a012030664752267afbdc8fe63357a..ac667f151db3719ce31fb3cd79513a9218bb3251 100644 (file)
--- a/stubs.c
+++ b/stubs.c
 #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
 
 
 /* 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 */