From 56d2eb7b65a2a9001f8caab20b2c4094c8665b16 Mon Sep 17 00:00:00 2001 From: alexander <> Date: Tue, 13 Jul 2004 19:41:26 +0000 Subject: [PATCH] Respond to client queries with better server statistics. We now output a close-to-reality stratum, a real reference time, and a leap indicator that will indicate if the local clock isn't synchronized. This also means that until the server feels it's synchronized, it will tell the clients it isn't. This is normal, and correct. ok henning@ --- src/usr.sbin/ntpd/ntp.c | 39 ++++++++++++++++++++++++++------------ src/usr.sbin/ntpd/ntpd.h | 5 +++-- src/usr.sbin/ntpd/server.c | 22 ++++++++++++++------- 3 files changed, 45 insertions(+), 21 deletions(-) diff --git a/src/usr.sbin/ntpd/ntp.c b/src/usr.sbin/ntpd/ntp.c index 0a382d72..ac537634 100644 --- a/src/usr.sbin/ntpd/ntp.c +++ b/src/usr.sbin/ntpd/ntp.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ntp.c,v 1.22 2004/07/10 18:42:51 henning Exp $ */ +/* $OpenBSD: ntp.c,v 1.23 2004/07/13 19:41:26 alexander Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -109,6 +109,9 @@ ntp_main(int pipe_prnt[2], struct ntpd_conf *conf) TAILQ_FOREACH(p, &conf->ntp_peers, entry) client_peer_init(p); + bzero(&conf->status, sizeof(conf->status)); + conf->status.leap = LI_ALARM; + log_info("ntp engine ready"); peer_cnt = 0; @@ -223,7 +226,7 @@ ntp_main(int pipe_prnt[2], struct ntpd_conf *conf) for (j = 1; nfds > 0 && j < idx_peers; j++) if (pfd[j].revents & POLLIN) { nfds--; - if (server_dispatch(pfd[j].fd) == -1) + if (server_dispatch(pfd[j].fd, conf) == -1) ntp_quit = 1; } @@ -278,22 +281,34 @@ ntp_adjtime(struct ntpd_conf *conf) { struct ntp_peer *p; double offset_median = 0; - int offset_cnt = 0; + int offset_cnt = 0, stratum = 254; - TAILQ_FOREACH(p, &conf->ntp_peers, entry) - if (p->update.good) { - if (p->update.rcvd + REPLY_MAXAGE < time(NULL)) - p->update.good = 0; - else - if (p->trustlevel >= TRUSTLEVEL_BADPEER) { - offset_median += p->update.offset; - offset_cnt++; - } + TAILQ_FOREACH(p, &conf->ntp_peers, entry) { + if (!p->update.good) + continue; + + if (p->update.rcvd + REPLY_MAXAGE < time(NULL)) { + p->update.good = 0; + continue; } + if (p->trustlevel < TRUSTLEVEL_BADPEER) + continue; + + offset_median += p->update.offset; + offset_cnt++; + + if (p->update.status.stratum < stratum) + stratum = p->update.status.stratum; /* XXX */ + } + if (offset_cnt > 0) { offset_median /= offset_cnt; imsg_compose(&ibuf_main, IMSG_ADJTIME, 0, &offset_median, sizeof(offset_median)); + + conf->status.reftime = gettime(); + conf->status.stratum = stratum + 1; + conf->status.leap = LI_NOWARNING; /* XXX */ } } diff --git a/src/usr.sbin/ntpd/ntpd.h b/src/usr.sbin/ntpd/ntpd.h index 452dace9..6379d1cd 100644 --- a/src/usr.sbin/ntpd/ntpd.h +++ b/src/usr.sbin/ntpd/ntpd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: ntpd.h,v 1.24 2004/07/11 00:15:10 alexander Exp $ */ +/* $OpenBSD: ntpd.h,v 1.25 2004/07/13 19:41:26 alexander Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -109,6 +109,7 @@ struct ntpd_conf { TAILQ_HEAD(ntp_peers, ntp_peer) ntp_peers; u_int8_t opts; u_int8_t listen_all; + struct ntp_status status; }; struct buf { @@ -211,7 +212,7 @@ int ntp_sendmsg(int, struct sockaddr *, struct ntp_msg *, ssize_t, int); /* server.c */ int setup_listeners(struct servent *, struct ntpd_conf *, u_int *); int ntp_reply(int, struct sockaddr *, struct ntp_msg *, int); -int server_dispatch(int); +int server_dispatch(int, struct ntpd_conf *); /* client.c */ int client_peer_init(struct ntp_peer *); diff --git a/src/usr.sbin/ntpd/server.c b/src/usr.sbin/ntpd/server.c index 31a5ad24..cea72591 100644 --- a/src/usr.sbin/ntpd/server.c +++ b/src/usr.sbin/ntpd/server.c @@ -1,7 +1,8 @@ -/* $OpenBSD: server.c,v 1.9 2004/07/13 17:27:57 alexander Exp $ */ +/* $OpenBSD: server.c,v 1.10 2004/07/13 19:41:26 alexander Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer + * Copyright (c) 2004 Alexander Guy * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -92,9 +93,10 @@ setup_listeners(struct servent *se, struct ntpd_conf *conf, u_int *cnt) } int -server_dispatch(int fd) +server_dispatch(int fd, struct ntpd_conf *conf) { ssize_t size; + u_int8_t version; double rectime; struct sockaddr_storage fsa; socklen_t fsa_len; @@ -111,21 +113,27 @@ server_dispatch(int fd) if (ntp_getmsg(buf, size, &query) == -1) return (0); + version = (query.status & VERSIONMASK) >> 3; + bzero(&reply, sizeof(reply)); - reply.status = 0 | (query.status & VERSIONMASK); + reply.status = conf->status.leap | (query.status & VERSIONMASK); if ((query.status & MODEMASK) == MODE_CLIENT) reply.status |= MODE_SERVER; else reply.status |= MODE_SYM_PAS; - reply.stratum = 2; /* XXX */ + reply.stratum = conf->status.stratum; reply.ppoll = query.ppoll; - reply.precision = 0; /* XXX */ + reply.precision = conf->status.precision; reply.rectime = d_to_lfp(rectime); - reply.reftime = reply.rectime; /* XXX */ + reply.reftime = d_to_lfp(conf->status.reftime); reply.xmttime = d_to_lfp(gettime()); reply.orgtime = query.xmttime; - reply.refid = reply.xmttime.fraction; /* XXX */ + + if (version > 3) + reply.refid = reply.xmttime.fraction; + else + reply.refid = 0; /* XXX */ return (ntp_sendmsg(fd, (struct sockaddr *)&fsa, &reply, size, 0)); }