From ed37b625d360719fd2c0d7787b089aa514b47b27 Mon Sep 17 00:00:00 2001 From: henning <> Date: Sun, 19 Jun 2005 16:42:57 +0000 Subject: [PATCH] use a little state engine to keep track of delayed dns lookups and such, eases things tested by Jason Ackley Matthias Kilian Stephen Marley sturm@ theo ok --- src/usr.sbin/ntpd/client.c | 8 +++++++- src/usr.sbin/ntpd/ntp.c | 17 +++++++++++++---- src/usr.sbin/ntpd/ntpd.c | 22 +++++++++++----------- src/usr.sbin/ntpd/ntpd.h | 5 ++++- src/usr.sbin/ntpd/parse.y | 6 +++++- 5 files changed, 40 insertions(+), 18 deletions(-) diff --git a/src/usr.sbin/ntpd/client.c b/src/usr.sbin/ntpd/client.c index e03d5a36..9e1cf68d 100644 --- a/src/usr.sbin/ntpd/client.c +++ b/src/usr.sbin/ntpd/client.c @@ -1,4 +1,4 @@ -/* $OpenBSD: client.c,v 1.60 2005/04/19 11:08:41 henning Exp $ */ +/* $OpenBSD: client.c,v 1.61 2005/06/19 16:42:57 henning Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -71,11 +71,13 @@ client_addr_init(struct ntp_peer *p) sa_in = (struct sockaddr_in *)&h->ss; if (ntohs(sa_in->sin_port) == 0) sa_in->sin_port = htons(123); + p->state = STATE_DNS_DONE; break; case AF_INET6: sa_in6 = (struct sockaddr_in6 *)&h->ss; if (ntohs(sa_in6->sin6_port) == 0) sa_in6->sin6_port = htons(123); + p->state = STATE_DNS_DONE; break; default: fatal("king bula sez: wrong AF in client_addr_init"); @@ -97,6 +99,7 @@ client_nextaddr(struct ntp_peer *p) if (p->addr_head.a == NULL) { priv_host_dns(p->addr_head.name, p->id); + p->state = STATE_DNS_INPROGRESS; return (-1); } @@ -119,6 +122,9 @@ client_query(struct ntp_peer *p) return (0); } + if (p->state < STATE_DNS_DONE || p->addr == NULL) + return (-1); + if (p->query->fd == -1) { struct sockaddr *sa = (struct sockaddr *)&p->addr->ss; diff --git a/src/usr.sbin/ntpd/ntp.c b/src/usr.sbin/ntpd/ntp.c index 2cf725a4..59f3284b 100644 --- a/src/usr.sbin/ntpd/ntp.c +++ b/src/usr.sbin/ntpd/ntp.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ntp.c,v 1.60 2005/05/26 09:13:06 dtucker Exp $ */ +/* $OpenBSD: ntp.c,v 1.61 2005/06/19 16:42:57 henning Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -194,13 +194,14 @@ ntp_main(int pipe_prnt[2], struct ntpd_conf *nconf) idx_peers = i; sent_cnt = trial_cnt = 0; TAILQ_FOREACH(p, &conf->ntp_peers, entry) { - if (p->next > 0 && p->next < nextaction) - nextaction = p->next; if (p->next > 0 && p->next <= time(NULL)) { - trial_cnt++; + if (p->state > STATE_DNS_INPROGRESS) + trial_cnt++; if (client_query(p) == 0) sent_cnt++; } + if (p->next > 0 && p->next < nextaction) + nextaction = p->next; if (p->deadline > 0 && p->deadline < nextaction) nextaction = p->deadline; @@ -326,7 +327,13 @@ ntp_dispatch_imsg(void) log_warnx("IMSG_HOST_DNS but addr != NULL!"); break; } + dlen = imsg.hdr.len - IMSG_HEADER_SIZE; + if (dlen == 0) { /* no data -> temp error */ + peer->state = STATE_DNS_TEMPFAIL; + break; + } + p = (u_char *)imsg.data; while (dlen >= sizeof(struct sockaddr_storage)) { if ((h = calloc(1, sizeof(struct ntp_addr))) == @@ -341,11 +348,13 @@ ntp_dispatch_imsg(void) npeer->addr = h; npeer->addr_head.a = h; client_peer_init(npeer); + npeer->state = STATE_DNS_DONE; peer_add(npeer); } else { h->next = peer->addr; peer->addr = h; peer->addr_head.a = peer->addr; + peer->state = STATE_DNS_DONE; } } if (dlen != 0) diff --git a/src/usr.sbin/ntpd/ntpd.c b/src/usr.sbin/ntpd/ntpd.c index 6c635953..8cbf68c1 100644 --- a/src/usr.sbin/ntpd/ntpd.c +++ b/src/usr.sbin/ntpd/ntpd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ntpd.c,v 1.36 2005/05/26 09:13:06 dtucker Exp $ */ +/* $OpenBSD: ntpd.c,v 1.37 2005/06/19 16:42:57 henning Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -289,17 +289,17 @@ dispatch_imsg(struct ntpd_conf *conf) if (name[imsg.hdr.len] != '\0' || strlen(name) != imsg.hdr.len) fatalx("invalid IMSG_HOST_DNS received"); - if ((cnt = host_dns(name, &hn)) > 0) { - buf = imsg_create(ibuf, IMSG_HOST_DNS, - imsg.hdr.peerid, 0, - cnt * sizeof(struct sockaddr_storage)); - if (buf == NULL) - break; - for (h = hn; h != NULL; h = h->next) { + cnt = host_dns(name, &hn); + buf = imsg_create(ibuf, IMSG_HOST_DNS, + imsg.hdr.peerid, 0, + cnt * sizeof(struct sockaddr_storage)); + if (buf == NULL) + break; + if (cnt > 0) + for (h = hn; h != NULL; h = h->next) imsg_add(buf, &h->ss, sizeof(h->ss)); - } - imsg_close(ibuf, buf); - } + + imsg_close(ibuf, buf); break; default: break; diff --git a/src/usr.sbin/ntpd/ntpd.h b/src/usr.sbin/ntpd/ntpd.h index 7387c375..2952415d 100644 --- a/src/usr.sbin/ntpd/ntpd.h +++ b/src/usr.sbin/ntpd/ntpd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: ntpd.h,v 1.55 2005/04/26 15:18:22 henning Exp $ */ +/* $OpenBSD: ntpd.h,v 1.56 2005/06/19 16:42:57 henning Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -59,6 +59,9 @@ enum client_state { STATE_NONE, + STATE_DNS_INPROGRESS, + STATE_DNS_TEMPFAIL, + STATE_DNS_DONE, STATE_QUERY_SENT, STATE_REPLY_RECEIVED }; diff --git a/src/usr.sbin/ntpd/parse.y b/src/usr.sbin/ntpd/parse.y index 05ed002f..981e03f5 100644 --- a/src/usr.sbin/ntpd/parse.y +++ b/src/usr.sbin/ntpd/parse.y @@ -1,4 +1,4 @@ -/* $OpenBSD: parse.y,v 1.24 2004/11/25 06:27:41 henning Exp $ */ +/* $OpenBSD: parse.y,v 1.25 2005/06/19 16:42:57 henning Exp $ */ /* * Copyright (c) 2002, 2003, 2004 Henning Brauer @@ -134,6 +134,8 @@ conf_main : LISTEN ON address { p->addr_head.name = strdup($2->name); if (p->addr_head.name == NULL) fatal(NULL); + if (p->addr != NULL) + p->state = STATE_DNS_DONE; TAILQ_INSERT_TAIL(&conf->ntp_peers, p, entry); h = next; @@ -168,6 +170,8 @@ conf_main : LISTEN ON address { p->addr_head.name = strdup($2->name); if (p->addr_head.name == NULL) fatal(NULL); + if (p->addr != NULL) + p->state = STATE_DNS_DONE; TAILQ_INSERT_TAIL(&conf->ntp_peers, p, entry); free($2->name); free($2);