Don't retry resolving in DNS_Name2IPAddress

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.
This commit is contained in:
Miroslav Lichvar 2010-04-26 16:05:56 +02:00
parent 2458325c09
commit 3d260d41b3
5 changed files with 36 additions and 33 deletions

View file

@ -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 {

View 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;
}
}

4
conf.c
View file

@ -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;

View 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();
}
/* ================================================== */

View file

@ -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 */