diff --git a/client.c b/client.c index 55d17e7..c2af79c 100644 --- 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 { diff --git a/cmdparse.c b/cmdparse.c index ae301dd..668e08d 100644 --- a/cmdparse.c +++ b/cmdparse.c @@ -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 1c71674..0f049b5 100644 --- 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; diff --git a/nameserv.c b/nameserv.c index 9cd434c..843fc1f 100644 --- a/nameserv.c +++ b/nameserv.c @@ -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(); +} + +/* ================================================== */ + diff --git a/nameserv.h b/nameserv.h index f1cfb1e..87f2f2d 100644 --- a/nameserv.h +++ b/nameserv.h @@ -34,12 +34,20 @@ #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 */