diff --git a/src/usr.sbin/ntpd/client.c b/src/usr.sbin/ntpd/client.c index a1882953..254bae52 100644 --- a/src/usr.sbin/ntpd/client.c +++ b/src/usr.sbin/ntpd/client.c @@ -1,4 +1,4 @@ -/* $OpenBSD: client.c,v 1.29 2004/07/28 16:38:43 henning Exp $ */ +/* $OpenBSD: client.c,v 1.30 2004/08/12 16:33:59 henning Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -30,14 +30,25 @@ int client_update(struct ntp_peer *); int client_peer_init(struct ntp_peer *p) +{ + if ((p->query = calloc(1, sizeof(struct ntp_query))) == NULL) + fatal("client_query calloc"); + p->query->fd = -1; + p->query->msg.status = MODE_CLIENT | (NTP_VERSION << 3); + p->state = STATE_NONE; + p->shift = 0; + p->trustlevel = TRUSTLEVEL_PATHETIC; + + return (client_addr_init(p)); +} + +int +client_addr_init(struct ntp_peer *p) { struct sockaddr_in *sa_in; struct sockaddr_in6 *sa_in6; struct ntp_addr *h; - if ((p->query = calloc(1, sizeof(struct ntp_query))) == NULL) - fatal("client_query calloc"); - for (h = p->addr; h != NULL; h = h->next) { switch (h->ss.ss_family) { case AF_INET: @@ -51,7 +62,7 @@ client_peer_init(struct ntp_peer *p) sa_in6->sin6_port = htons(123); break; default: - fatal("king bula sez: wrong AF in client_peer_init"); + fatal("king bula sez: wrong AF in client_addr_init"); /* not reached */ } } @@ -60,11 +71,7 @@ client_peer_init(struct ntp_peer *p) (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); - p->state = STATE_NONE; p->next = time(NULL); - p->shift = 0; - p->trustlevel = TRUSTLEVEL_PATHETIC; return (0); } @@ -75,12 +82,8 @@ 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); + ntp_host_dns(p->addr_head.name, p->id); + return (-1); } if ((p->addr = p->addr->next) == NULL) diff --git a/src/usr.sbin/ntpd/config.c b/src/usr.sbin/ntpd/config.c index 140052de..2b01da17 100644 --- a/src/usr.sbin/ntpd/config.c +++ b/src/usr.sbin/ntpd/config.c @@ -1,4 +1,4 @@ -/* $OpenBSD: config.c,v 1.10 2004/08/10 12:41:15 henning Exp $ */ +/* $OpenBSD: config.c,v 1.11 2004/08/12 16:33:59 henning Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -124,6 +124,7 @@ host_dns(const char *s, struct ntp_addr **hn) bzero(&hints, sizeof(hints)); hints.ai_family = PF_UNSPEC; hints.ai_socktype = SOCK_DGRAM; /* DUMMY */ + res_init(); /* XXX */ error = getaddrinfo(s, NULL, &hints, &res0); if (error) { log_warnx("could not parse \"%s\": %s", s, diff --git a/src/usr.sbin/ntpd/ntp.c b/src/usr.sbin/ntpd/ntp.c index 5326c28f..cadbc32f 100644 --- a/src/usr.sbin/ntpd/ntp.c +++ b/src/usr.sbin/ntpd/ntp.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ntp.c,v 1.25 2004/07/18 12:59:41 henning Exp $ */ +/* $OpenBSD: ntp.c,v 1.26 2004/08/12 16:33:59 henning Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -245,6 +245,10 @@ ntp_dispatch_imsg(void) { struct imsg imsg; int n; + struct ntp_peer *peer, *npeer; + u_int16_t dlen; + u_char *p; + struct ntp_addr *h; if ((n = imsg_read(&ibuf_main)) == -1) return (-1); @@ -262,6 +266,47 @@ ntp_dispatch_imsg(void) break; switch (imsg.hdr.type) { + case IMSG_HOST_DNS: + TAILQ_FOREACH(peer, &conf->ntp_peers, entry) + if (peer->id == imsg.hdr.peerid) + break; + if (peer == NULL) + fatal("IMSG_HOST_DNS with invalid peerID"); + if (peer->addr != NULL) { + log_warnx("IMSG_HOST_DNS but addr != NULL!"); + break; + } + dlen = imsg.hdr.len - IMSG_HEADER_SIZE; + p = (u_char *)imsg.data; + while (dlen >= sizeof(struct sockaddr_storage)) { + if ((h = calloc(1, sizeof(struct ntp_addr))) == + NULL) + fatal(NULL); + memcpy(&h->ss, p, sizeof(h->ss)); + p += sizeof(h->ss); + dlen -= sizeof(h->ss); + if (peer->addr_head.pool) { + npeer = new_peer(); + h->next = NULL; + npeer->addr = h; + npeer->addr_head.a = h; + client_peer_init(npeer); + TAILQ_INSERT_TAIL(&conf->ntp_peers, + npeer, entry); + } else { + h->next = peer->addr; + peer->addr = h; + peer->addr_head.a = peer->addr; + } + } + if (dlen != 0) + fatal("IMSG_HOST_DNS: dlen != 0"); + if (peer->addr_head.pool) { + TAILQ_REMOVE(&conf->ntp_peers, peer, entry); + free(peer); + } else + client_addr_init(peer); + break; default: break; } @@ -300,3 +345,12 @@ ntp_adjtime(void) TAILQ_FOREACH(p, &conf->ntp_peers, entry) p->update.good = 0; } + +void +ntp_host_dns(char *name, u_int32_t peerid) +{ + u_int16_t dlen; + + dlen = strlen(name) + 1; + imsg_compose(&ibuf_main, IMSG_HOST_DNS, peerid, name, dlen); +} diff --git a/src/usr.sbin/ntpd/ntpd.c b/src/usr.sbin/ntpd/ntpd.c index a3dfbd75..b7179456 100644 --- a/src/usr.sbin/ntpd/ntpd.c +++ b/src/usr.sbin/ntpd/ntpd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ntpd.c,v 1.13 2004/07/13 11:16:22 henning Exp $ */ +/* $OpenBSD: ntpd.c,v 1.14 2004/08/12 16:33:59 henning Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -213,8 +213,11 @@ int dispatch_imsg(void) { struct imsg imsg; - int n; + int n, cnt; double d; + char *name; + struct ntp_addr *h, *hn; + struct buf *buf; if ((n = imsg_read(&ibuf)) == -1) return (-1); @@ -237,6 +240,23 @@ dispatch_imsg(void) fatal("invalid IMSG_ADJTIME received"); memcpy(&d, imsg.data, sizeof(d)); ntpd_adjtime(d); + break; + case IMSG_HOST_DNS: + name = imsg.data; + if (imsg.hdr.len != strlen(name) + 1 + IMSG_HEADER_SIZE) + fatal("invalid IMSG_HOST_DNS received"); + if ((cnt = host_dns(name, &hn)) > 0) { + buf = imsg_create(&ibuf, IMSG_HOST_DNS, + imsg.hdr.peerid, + cnt * sizeof(struct sockaddr_storage)); + if (buf == NULL) + break; + for (h = hn; h != NULL; h = h->next) { + imsg_add(buf, &h->ss, sizeof(h->ss)); + } + imsg_close(&ibuf, buf); + } + break; default: break; } diff --git a/src/usr.sbin/ntpd/ntpd.h b/src/usr.sbin/ntpd/ntpd.h index db3efbc8..a1d3537f 100644 --- a/src/usr.sbin/ntpd/ntpd.h +++ b/src/usr.sbin/ntpd/ntpd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: ntpd.h,v 1.32 2004/08/10 12:41:15 henning Exp $ */ +/* $OpenBSD: ntpd.h,v 1.33 2004/08/12 16:33:59 henning Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -150,7 +150,8 @@ struct imsgbuf { enum imsg_type { IMSG_NONE, - IMSG_ADJTIME + IMSG_ADJTIME, + IMSG_HOST_DNS }; struct imsg_hdr { @@ -201,6 +202,7 @@ void imsg_free(struct imsg *); /* ntp.c */ pid_t ntp_main(int[2], struct ntpd_conf *); void ntp_adjtime(void); +void ntp_host_dns(char *, u_int32_t); /* parse.y */ int parse_config(char *, struct ntpd_conf *); @@ -222,6 +224,7 @@ int server_dispatch(int, struct ntpd_conf *); /* client.c */ int client_peer_init(struct ntp_peer *); +int client_addr_init(struct ntp_peer *); int client_nextaddr(struct ntp_peer *); int client_query(struct ntp_peer *); int client_dispatch(struct ntp_peer *);