Browse Source

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
OPENBSD_3_6
henning 20 years ago
parent
commit
5512eb59e0
4 changed files with 44 additions and 19 deletions
  1. +17
    -2
      src/usr.sbin/ntpd/client.c
  2. +22
    -13
      src/usr.sbin/ntpd/config.c
  3. +3
    -2
      src/usr.sbin/ntpd/ntpd.h
  4. +2
    -2
      src/usr.sbin/ntpd/parse.y

+ 17
- 2
src/usr.sbin/ntpd/client.c View File

@ -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 <henning@openbsd.org>
@ -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


+ 22
- 13
src/usr.sbin/ntpd/config.c View File

@ -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 <henning@openbsd.org>
@ -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);
}

+ 3
- 2
src/usr.sbin/ntpd/ntpd.h View File

@ -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 <henning@openbsd.org>
@ -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 *);


+ 2
- 2
src/usr.sbin/ntpd/parse.y View File

@ -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 <henning@openbsd.org>
@ -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);


Loading…
Cancel
Save