From 5512eb59e021859f2d09b4ec2ccffce3dc71653d Mon Sep 17 00:00:00 2001 From: henning <> Date: Wed, 28 Jul 2004 16:38:43 +0000 Subject: [PATCH] when a dns lookup fails at parse time, do not abort but try again to resolve the hostname every 60 seconds fixes ntpd invocations before e. g. a dialup link is established and such. as we want ntpd to be a "fire and forget" background daemon it should cope with such situations. tested by many --- src/usr.sbin/ntpd/client.c | 19 +++++++++++++++++-- src/usr.sbin/ntpd/config.c | 35 ++++++++++++++++++++++------------- src/usr.sbin/ntpd/ntpd.h | 5 +++-- src/usr.sbin/ntpd/parse.y | 4 ++-- 4 files changed, 44 insertions(+), 19 deletions(-) diff --git a/src/usr.sbin/ntpd/client.c b/src/usr.sbin/ntpd/client.c index de993cee..a1882953 100644 --- a/src/usr.sbin/ntpd/client.c +++ b/src/usr.sbin/ntpd/client.c @@ -1,4 +1,4 @@ -/* $OpenBSD: client.c,v 1.28 2004/07/20 16:47:55 henning Exp $ */ +/* $OpenBSD: client.c,v 1.29 2004/07/28 16:38:43 henning Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -56,7 +56,8 @@ client_peer_init(struct ntp_peer *p) } } - 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 @@ client_nextaddr(struct ntp_peer *p) { 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 @@ client_nextaddr(struct ntp_peer *p) 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 diff --git a/src/usr.sbin/ntpd/config.c b/src/usr.sbin/ntpd/config.c index c78199c5..b3a0dba1 100644 --- a/src/usr.sbin/ntpd/config.c +++ b/src/usr.sbin/ntpd/config.c @@ -1,4 +1,4 @@ -/* $OpenBSD: config.c,v 1.8 2004/07/25 18:27:58 henning Exp $ */ +/* $OpenBSD: config.c,v 1.9 2004/07/28 16:38:43 henning Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -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 @@ host(const char *s) /* 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 @@ host_v6(const char *s) 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 @@ host_dns(const char *s) 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 @@ host_dns(const char *s) h->next = hh; hh = h; + cnt++; } freeaddrinfo(res0); - return (hh); + *hn = hh; + return (cnt); } diff --git a/src/usr.sbin/ntpd/ntpd.h b/src/usr.sbin/ntpd/ntpd.h index d1506ac3..3904669a 100644 --- a/src/usr.sbin/ntpd/ntpd.h +++ b/src/usr.sbin/ntpd/ntpd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: ntpd.h,v 1.29 2004/07/25 18:27:58 henning Exp $ */ +/* $OpenBSD: ntpd.h,v 1.30 2004/07/28 16:38:43 henning Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -206,7 +206,8 @@ int parse_config(char *, struct ntpd_conf *); 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 *); diff --git a/src/usr.sbin/ntpd/parse.y b/src/usr.sbin/ntpd/parse.y index 7efb7fc8..0baa4383 100644 --- a/src/usr.sbin/ntpd/parse.y +++ b/src/usr.sbin/ntpd/parse.y @@ -1,4 +1,4 @@ -/* $OpenBSD: parse.y,v 1.14 2004/07/21 09:40:55 henning Exp $ */ +/* $OpenBSD: parse.y,v 1.15 2004/07/28 16:38:43 henning Exp $ */ /* * Copyright (c) 2002, 2003, 2004 Henning Brauer @@ -211,7 +211,7 @@ address : STRING { 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);