vBulletin Search Engine Optimization
| |||||||
| Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| the way we see it, ntpd is a daemon to run in background and "just work" without much attention. at least one user had teh problem that he doesn't have a dns by the time ntpd is started by rc and thus parsing the config file fails, preventing ntpd from starting up. the right thing to do is actually to delay the dns lookups if necessary and try again later. this is what this patch implements. it has two known quirks, of which one is trivial to fix which doesn't matter now and one that I am a bit concerned about - getaddrinfo()s return values do not really allow to distinguish between a nonexistant hostname and a timeout querying the resolvers. right now this means if you specify an unresolveable hostname ntpd will try to resolve it every 60 seconds never realizing that it cannot succeed - but well, this is logged at least and not THAT terrible. please test, sanity check etc and report back... Index: client.c ================================================== ================= RCS file: /cvs/src/usr.sbin/ntpd/client.c,v retrieving revision 1.28 diff -u -r1.28 client.c --- client.c 20 Jul 2004 16:47:55 -0000 1.28 +++ client.c 26 Jul 2004 17:04:06 -0000 @@ -56,7 +56,8 @@ } } - if ((p->query->fd = socket(p->addr->ss.ss_family, SOCK_DGRAM, 0)) == -1) + if (p->addr != NULL && + (p->query->fd = socket(p->addr->ss.ss_family, SOCK_DGRAM, 0)) == -1) fatal("client_query socket"); p->query->msg.status = MODE_CLIENT | (NTP_VERSION << 3); @@ -73,6 +74,15 @@ { close(p->query->fd); + if (p->addr_head.a == NULL) { + if (host_dns(p->addr_head.name, &p->addr_head.a) > 0) { + p->addr = p->addr_head.a; + p->shift = 0; + p->trustlevel = TRUSTLEVEL_PATHETIC; + } else + return (-1); + } + if ((p->addr = p->addr->next) == NULL) p->addr = p->addr_head.a; @@ -88,6 +98,11 @@ int client_query(struct ntp_peer *p) { + if (p->addr == NULL && client_nextaddr(p) == -1) { + p->next = time(NULL) + INTERVAL_QUERY_PATHETIC; + return (-1); + } + /* * Send out a random 64-bit number as our transmit time. The NTP * server will copy said number into the originate field on the Index: config.c ================================================== ================= RCS file: /cvs/src/usr.sbin/ntpd/config.c,v retrieving revision 1.8 diff -u -r1.8 config.c --- config.c 25 Jul 2004 18:27:58 -0000 1.8 +++ config.c 26 Jul 2004 17:04:06 -0000 @@ -29,12 +29,12 @@ struct ntp_addr *host_v4(const char *); struct ntp_addr *host_v6(const char *); -struct ntp_addr *host_dns(const char *); -struct ntp_addr * -host(const char *s) +int +host(const char *s, struct ntp_addr **hn) { - struct ntp_addr *h = NULL; + struct ntp_addr *h = NULL; + int cnt = 1; if (!strcmp(s, "*")) if ((h = calloc(1, sizeof(struct ntp_addr))) == NULL) @@ -50,9 +50,10 @@ /* Hostname? */ if (h == NULL) - h = host_dns(s); + cnt = host_dns(s, &h); - return (h); + *hn = h; + return (cnt); } struct ntp_addr * @@ -105,11 +106,11 @@ return (h); } -struct ntp_addr * -host_dns(const char *s) +int +host_dns(const char *s, struct ntp_addr **hn) { struct addrinfo hints, *res0, *res; - int error; + int error, cnt = 0; struct sockaddr_in *sa_in; struct sockaddr_in6 *sa_in6; struct ntp_addr *h, *hh = NULL; @@ -118,8 +119,14 @@ hints.ai_family = PF_UNSPEC; hints.ai_socktype = SOCK_STREAM; /* DUMMY */ error = getaddrinfo(s, NULL, &hints, &res0); - if (error) - return (NULL); + if (error) { + log_warnx("could not parse \"%s\": %s", s, + gai_strerror(error)); + if (error == EAI_AGAIN || error == EAI_NODATA) + return (0); + else + return (-1); + } for (res = res0; res; res = res->ai_next) { if (res->ai_family != AF_INET && @@ -142,8 +149,10 @@ h->next = hh; hh = h; + cnt++; } freeaddrinfo(res0); - return (hh); + *hn = hh; + return (cnt); } Index: ntpd.h ================================================== ================= RCS file: /cvs/src/usr.sbin/ntpd/ntpd.h,v retrieving revision 1.29 diff -u -r1.29 ntpd.h --- ntpd.h 25 Jul 2004 18:27:58 -0000 1.29 +++ ntpd.h 26 Jul 2004 17:04:07 -0000 @@ -206,7 +206,8 @@ int cmdline_symset(char *); /* config.c */ -struct ntp_addr *host(const char *); +int host(const char *, struct ntp_addr **); +int host_dns(const char *, struct ntp_addr **); /* ntp_msg.c */ int ntp_getmsg(char *, ssize_t, struct ntp_msg *); Index: parse.y ================================================== ================= RCS file: /cvs/src/usr.sbin/ntpd/parse.y,v retrieving revision 1.14 diff -u -r1.14 parse.y --- parse.y 21 Jul 2004 09:40:55 -0000 1.14 +++ parse.y 26 Jul 2004 17:04:07 -0000 @@ -211,7 +211,7 @@ if (($$ = calloc(1, sizeof(struct ntp_addr_wrap))) == NULL) fatal(NULL); - if (($$->a = host($1)) == NULL) { + if (host($1, &$$->a) == -1) { yyerror("could not parse address spec \"%s\"", $1); free($1); |