Always use delayed name resolving for server and peer directives

This significantly reduces initialization time.
This commit is contained in:
Miroslav Lichvar 2011-08-26 14:22:10 +02:00
parent 919b5b5a7d
commit 7fb50d9a3e
6 changed files with 27 additions and 31 deletions

View file

@ -960,17 +960,18 @@ process_cmd_add_server_or_peer(CMD_Request *msg, char *line)
{ {
CPS_NTP_Source data; CPS_NTP_Source data;
CPS_Status status; CPS_Status status;
IPAddr ip_addr;
int result = 0; int result = 0;
status = CPS_ParseNTPSourceAdd(line, &data); status = CPS_ParseNTPSourceAdd(line, &data);
switch (status) { switch (status) {
case CPS_Success: case CPS_Success:
/* Don't retry name resolving */ if (DNS_Name2IPAddress(data.name, &ip_addr) != DNS_Success) {
if (data.ip_addr.family == IPADDR_UNSPEC) {
Free(data.name); Free(data.name);
fprintf(stderr, "Invalid host/IP address\n"); fprintf(stderr, "Invalid host/IP address\n");
break; break;
} }
Free(data.name);
if (data.params.min_stratum != SRC_DEFAULT_MINSTRATUM) { if (data.params.min_stratum != SRC_DEFAULT_MINSTRATUM) {
fprintf(stderr, "Option minstratum not supported\n"); fprintf(stderr, "Option minstratum not supported\n");
@ -988,7 +989,7 @@ process_cmd_add_server_or_peer(CMD_Request *msg, char *line)
} }
msg->data.ntp_source.port = htonl((unsigned long) data.port); msg->data.ntp_source.port = htonl((unsigned long) data.port);
UTI_IPHostToNetwork(&data.ip_addr, &msg->data.ntp_source.ip_addr); UTI_IPHostToNetwork(&ip_addr, &msg->data.ntp_source.ip_addr);
msg->data.ntp_source.minpoll = htonl(data.params.minpoll); msg->data.ntp_source.minpoll = htonl(data.params.minpoll);
msg->data.ntp_source.maxpoll = htonl(data.params.maxpoll); msg->data.ntp_source.maxpoll = htonl(data.params.maxpoll);
msg->data.ntp_source.presend_minpoll = htonl(data.params.presend_minpoll); msg->data.ntp_source.presend_minpoll = htonl(data.params.presend_minpoll);

View file

@ -45,7 +45,6 @@ CPS_ParseNTPSourceAdd(const char *line, CPS_NTP_Source *src)
int ok, n, done; int ok, n, done;
char cmd[MAXLEN+1], hostname[MAXLEN+1]; char cmd[MAXLEN+1], hostname[MAXLEN+1];
CPS_Status result; CPS_Status result;
DNS_Status s;
src->port = SRC_DEFAULT_PORT; src->port = SRC_DEFAULT_PORT;
src->params.minpoll = SRC_DEFAULT_MINPOLL; src->params.minpoll = SRC_DEFAULT_MINPOLL;
@ -66,14 +65,7 @@ CPS_ParseNTPSourceAdd(const char *line, CPS_NTP_Source *src)
ok = 0; ok = 0;
if (sscanf(line, "%" SMAXLEN "s%n", hostname, &n) == 1) { if (sscanf(line, "%" SMAXLEN "s%n", hostname, &n) == 1) {
s = DNS_Name2IPAddress(hostname, &src->ip_addr); ok = 1;
if (s == DNS_Success) {
ok = 1;
src->name = NULL;
} else if (s == DNS_TryAgain) {
ok = 1;
src->ip_addr.family = IPADDR_UNSPEC;
}
} }
if (!ok) { if (!ok) {
@ -199,7 +191,7 @@ CPS_ParseNTPSourceAdd(const char *line, CPS_NTP_Source *src)
} while (!done); } while (!done);
} }
if (ok && src->ip_addr.family == IPADDR_UNSPEC) { if (ok) {
n = strlen(hostname); n = strlen(hostname);
src->name = MallocArray(char, n + 1); src->name = MallocArray(char, n + 1);
strncpy(src->name, hostname, n); strncpy(src->name, hostname, n);

View file

@ -47,7 +47,6 @@ typedef enum {
} CPS_Status; } CPS_Status;
typedef struct { typedef struct {
IPAddr ip_addr;
char *name; char *name;
unsigned short port; unsigned short port;
SourceParameters params; SourceParameters params;

15
conf.c
View file

@ -1286,22 +1286,15 @@ CNF_ProcessInitStepSlew(void (*after_hook)(void *), void *anything)
void void
CNF_AddSources(void) { CNF_AddSources(void) {
NTP_Remote_Address server;
int i; int i;
for (i=0; i<n_ntp_sources; i++) { for (i=0; i<n_ntp_sources; i++) {
if (ntp_sources[i].params.ip_addr.family != IPADDR_UNSPEC) { NSR_AddUnresolvedSource(ntp_sources[i].params.name, ntp_sources[i].params.port,
server.ip_addr = ntp_sources[i].params.ip_addr; ntp_sources[i].type, &ntp_sources[i].params.params);
memset(&server.local_ip_addr, 0, sizeof (server.local_ip_addr));
server.port = ntp_sources[i].params.port;
NSR_AddSource(&server, ntp_sources[i].type, &ntp_sources[i].params.params);
} else {
NSR_AddUnresolvedSource(ntp_sources[i].params.name, ntp_sources[i].params.port,
ntp_sources[i].type, &ntp_sources[i].params.params);
}
} }
NSR_ResolveSources();
return; return;
} }

View file

@ -287,6 +287,19 @@ NSR_AddUnresolvedSource(char *name, int port, NTP_Source_Type type, SourceParame
/* ================================================== */ /* ================================================== */
void
NSR_ResolveSources(void)
{
/* Try to resolve unresolved sources now */
if (resolving_interval) {
SCH_RemoveTimeout(resolving_id);
resolving_interval--;
resolve_sources(NULL);
}
}
/* ================================================== */
/* Procedure to remove a source. We don't bother whether the port /* Procedure to remove a source. We don't bother whether the port
address is matched - we're only interested in removing a record for address is matched - we're only interested in removing a record for
the right IP address. Thus the caller can specify the port number the right IP address. Thus the caller can specify the port number
@ -406,12 +419,7 @@ NSR_TakeSourcesOnline(IPAddr *mask, IPAddr *address)
int i; int i;
int any; int any;
/* Try to resolve unresolved sources now */ NSR_ResolveSources();
if (resolving_interval) {
SCH_RemoveTimeout(resolving_id);
resolving_interval--;
resolve_sources(NULL);
}
any = 0; any = 0;
for (i=0; i<N_RECORDS; i++) { for (i=0; i<N_RECORDS; i++) {

View file

@ -54,6 +54,9 @@ extern NSR_Status NSR_AddSource(NTP_Remote_Address *remote_addr, NTP_Source_Type
until it succeeds or fails with a non-temporary error. */ until it succeeds or fails with a non-temporary error. */
extern void NSR_AddUnresolvedSource(char *name, int port, NTP_Source_Type type, SourceParameters *params); extern void NSR_AddUnresolvedSource(char *name, int port, NTP_Source_Type type, SourceParameters *params);
/* Procedure to try resolve unresolved sources immediately. */
extern void NSR_ResolveSources(void);
/* Procedure to remove a source */ /* Procedure to remove a source */
extern NSR_Status NSR_RemoveSource(NTP_Remote_Address *remote_addr); extern NSR_Status NSR_RemoveSource(NTP_Remote_Address *remote_addr);