]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
Don't retry resolving in DNS_Name2IPAddress
authorMiroslav Lichvar <mlichvar@redhat.com>
Mon, 26 Apr 2010 14:05:56 +0000 (16:05 +0200)
committerMiroslav Lichvar <mlichvar@redhat.com>
Tue, 27 Apr 2010 12:35:28 +0000 (14:35 +0200)
Instead of retrying to resolve it in the function and blocking for a
long time, return a TryAgain status and let the caller retry it later if
necessary.

client.c
cmdparse.c
conf.c
nameserv.c
nameserv.h

index 55d17e7f237788da23b724c1e9b7e92fa07b5a85..c2af79c0f69f2383bf1131a54467ac26923f4abe 100644 (file)
--- a/client.c
+++ b/client.c
@@ -150,7 +150,7 @@ open_io(const char *hostname, int port)
   IPAddr ip;
 
   /* Note, this call could block for a while */
-  if (!DNS_Name2IPAddress(hostname, &ip, 0)) {
+  if (DNS_Name2IPAddress(hostname, &ip) != DNS_Success) {
     fprintf(stderr, "Could not get IP address for %s\n", hostname);
     exit(1);
   }
@@ -330,7 +330,7 @@ read_address_integer(char *line, IPAddr *address, int *value)
     fprintf(stderr, "Invalid syntax for address value\n");
     ok = 0;
   } else {
-    if (!DNS_Name2IPAddress(hostname, address, 0)) {
+    if (DNS_Name2IPAddress(hostname, address) != DNS_Success) {
       fprintf(stderr, "Could not get address for hostname\n");
       ok = 0;
     } else {
@@ -355,7 +355,7 @@ read_address_double(char *line, IPAddr *address, double *value)
     fprintf(stderr, "Invalid syntax for address value\n");
     ok = 0;
   } else {
-    if (!DNS_Name2IPAddress(hostname, address, 0)) {
+    if (DNS_Name2IPAddress(hostname, address) != DNS_Success) {
       fprintf(stderr, "Could not get address for hostname\n");
       ok = 0;
     } else {
@@ -621,7 +621,7 @@ parse_allow_deny(CMD_Request *msg, char *line)
         if (*q == '\n') *q = 0;
         q++;
       }
-      if (!DNS_Name2IPAddress(p, &ip, 0)) {
+      if (DNS_Name2IPAddress(p, &ip) != DNS_Success) {
         fprintf(stderr, "Could not read address\n");
         return 0;
       } else {
@@ -795,7 +795,7 @@ accheck_getaddr(char *line, IPAddr *addr)
         if (*q == '\n') *q = 0;
         q++;
       }
-      if (!DNS_Name2IPAddress(p, &ip, 0)) {
+      if (DNS_Name2IPAddress(p, &ip) != DNS_Success) {
         return 0;
       } else {
         *addr = ip;
@@ -982,7 +982,7 @@ process_cmd_delete(CMD_Request *msg, char *line)
     fprintf(stderr, "Invalid syntax for address\n");
     ok = 0;
   } else {
-    if (!DNS_Name2IPAddress(hostname, &address, 0)) {
+    if (DNS_Name2IPAddress(hostname, &address) != DNS_Success) {
       fprintf(stderr, "Could not get address for hostname\n");
       ok = 0;
     } else {
index ae301dd1ee074722b20655a219642bcdd677a702..668e08d709052643826a2edef4ecafb1d5ff1cdd 100644 (file)
@@ -46,6 +46,7 @@ CPS_ParseNTPSourceAdd(const char *line, CPS_NTP_Source *src)
   int ok, n, done;
   char cmd[MAXLEN+1], hostname[MAXLEN+1];
   CPS_Status result;
+  DNS_Status s;
   
   src->port = 123;
   src->params.minpoll = 6;
@@ -62,7 +63,8 @@ CPS_ParseNTPSourceAdd(const char *line, CPS_NTP_Source *src)
   
   ok = 0;
   if (sscanf(line, "%" SMAXLEN "s%n", hostname, &n) == 1) {
-    if (DNS_Name2IPAddress(hostname, &src->ip_addr, 1)) {
+    s = DNS_Name2IPAddress(hostname, &src->ip_addr);
+    if (s == DNS_Success) {
       ok = 1;
     }
   }
diff --git a/conf.c b/conf.c
index 1c7167456b214d59f05bbe11b1f125557cfe752d..0f049b549bb2c60eab8c7a7618e8b54165f3c888 100644 (file)
--- a/conf.c
+++ b/conf.c
@@ -754,7 +754,7 @@ parse_initstepslew(const char *line)
   }
   while (*p) {
     if (sscanf(p, "%" SHOSTNAME_LEN "s%n", hostname, &n) == 1) {
-      if (DNS_Name2IPAddress(hostname, &ip_addr, 1)) {
+      if (DNS_Name2IPAddress(hostname, &ip_addr) == DNS_Success) {
         init_srcs_ip[n_init_srcs] = ip_addr;
         ++n_init_srcs;
       }
@@ -962,7 +962,7 @@ parse_allow_deny(const char *line, AllowDeny *list, int allow)
       }
 
     } else {
-      if (DNS_Name2IPAddress(p, &ip_addr, 1)) {
+      if (DNS_Name2IPAddress(p, &ip_addr) == DNS_Success) {
         new_node = MallocNew(AllowDeny);
         new_node->allow = allow;
         new_node->all = all;
index 9cd434c4eda067469a65c33962adf2cb677ff6e6..843fc1ff573daa9de3018efe1e80f6990d0ce092 100644 (file)
@@ -38,9 +38,6 @@
 
 /* ================================================== */
 
-#define MAXRETRIES 10
-static unsigned int retries = 0;
-
 static int address_family = IPADDR_UNSPEC;
 
 void
@@ -49,8 +46,8 @@ DNS_SetAddressFamily(int family)
   address_family = family;
 }
 
-int 
-DNS_Name2IPAddress(const char *name, IPAddr *addr, int retry)
+DNS_Status 
+DNS_Name2IPAddress(const char *name, IPAddr *addr)
 {
 #ifdef HAVE_IPV6
   struct addrinfo hints, *res, *ai;
@@ -63,17 +60,10 @@ DNS_Name2IPAddress(const char *name, IPAddr *addr, int retry)
   hints.ai_flags = AI_ADDRCONFIG;
 #endif
 
-try_again:
   result = getaddrinfo(name, NULL, &hints, &res);
 
   if (result) {
-    if (retry && result == EAI_AGAIN && retries < MAXRETRIES) {
-      sleep(2 << retries);
-      retries++;
-      res_init();
-      goto try_again;
-    }
-    return 0;
+    return result == EAI_AGAIN ? DNS_TryAgain : DNS_Failure;
   }
 
   for (ai = res; !result && ai != NULL; ai = ai->ai_next) {
@@ -96,21 +86,16 @@ try_again:
   }
 
   freeaddrinfo(res);
-  return result;
+  return result ? DNS_Success : DNS_Failure;
 #else
   struct hostent *host;
   char *address0;
   
-try_again:
   host = gethostbyname(name);
 
   if (host == NULL) {
-    if (retry && h_errno == TRY_AGAIN && retries < MAXRETRIES) {
-      sleep(2 << retries);
-      retries++;
-      res_init();
-      goto try_again;
-    }
+    if (h_errno == TRY_AGAIN)
+      return DNS_TryAgain;
   } else {
     addr->family = IPADDR_INET4;
     address0 = host->h_addr_list[0];
@@ -118,10 +103,10 @@ try_again:
                      (((unsigned long)address0[1])<<16) |
                      (((unsigned long)address0[2])<<8) |
                      (((unsigned long)address0[3])));
-    return 1;
+    return DNS_Success;
   }
 
-  return 0;
+  return DNS_Failure;
 #endif
 }
 
@@ -190,3 +175,11 @@ DNS_IPAddress2Name(IPAddr *ip_addr, char *name, int len)
 
 /* ================================================== */
 
+void
+DNS_Reload(void)
+{
+  res_init();
+}
+
+/* ================================================== */
+
index f1cfb1e210f4fe150f727f92b8644c3fcd823982..87f2f2d19e04110f30e6b4e9df84638ad284cf3e 100644 (file)
 
 #include "addressing.h"
 
+typedef enum {
+  DNS_Success,
+  DNS_TryAgain,
+  DNS_Failure
+} DNS_Status;
+
 /* Resolve names only to selected address family */
 extern void DNS_SetAddressFamily(int family);
 
-extern int DNS_Name2IPAddress(const char *name, IPAddr *addr, int retry);
+extern DNS_Status DNS_Name2IPAddress(const char *name, IPAddr *addr);
 
 extern int DNS_IPAddress2Name(IPAddr *ip_addr, char *name, int len);
 
+extern void DNS_Reload(void);
+
 #endif /* GOT_NAMESERV_H */