From 89f43414dba7b8224102032a938645228a507045 Mon Sep 17 00:00:00 2001 From: otto <> Date: Wed, 7 Jun 2006 06:29:03 +0000 Subject: [PATCH] Compensate old offsets with the amount of adjustment done, avoiding overcompensating. From DragonFly, uses recent adjtime(2) changes, so you'll need a recent kernel. ok henning@ --- src/usr.sbin/ntpd/client.c | 6 +++--- src/usr.sbin/ntpd/ntp.c | 8 ++++++-- src/usr.sbin/ntpd/ntpd.c | 3 ++- src/usr.sbin/ntpd/ntpd.h | 4 +++- src/usr.sbin/ntpd/server.c | 6 +++--- src/usr.sbin/ntpd/util.c | 17 ++++++++++++++++- 6 files changed, 33 insertions(+), 11 deletions(-) diff --git a/src/usr.sbin/ntpd/client.c b/src/usr.sbin/ntpd/client.c index 65fad713..60ffcd17 100644 --- a/src/usr.sbin/ntpd/client.c +++ b/src/usr.sbin/ntpd/client.c @@ -1,4 +1,4 @@ -/* $OpenBSD: client.c,v 1.69 2006/06/04 18:58:13 otto Exp $ */ +/* $OpenBSD: client.c,v 1.70 2006/06/07 06:29:03 otto Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -161,7 +161,7 @@ client_query(struct ntp_peer *p) p->query->msg.xmttime.int_partl = arc4random(); p->query->msg.xmttime.fractionl = arc4random(); - p->query->xmttime = gettime(); + p->query->xmttime = gettime_corrected(); if (ntp_sendmsg(p->query->fd, NULL, &p->query->msg, NTP_MSGSIZE_NOAUTH, 0) == -1) { @@ -196,7 +196,7 @@ client_dispatch(struct ntp_peer *p, u_int8_t settime) fatal("recvfrom"); } - T4 = gettime(); + T4 = gettime_corrected(); ntp_getmsg((struct sockaddr *)&p->addr->ss, buf, size, &msg); diff --git a/src/usr.sbin/ntpd/ntp.c b/src/usr.sbin/ntpd/ntp.c index 4657f6f0..8ee2a981 100644 --- a/src/usr.sbin/ntpd/ntp.c +++ b/src/usr.sbin/ntpd/ntp.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ntp.c,v 1.83 2006/06/04 18:58:13 otto Exp $ */ +/* $OpenBSD: ntp.c,v 1.84 2006/06/07 06:29:03 otto Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -500,8 +500,11 @@ priv_adjtime(void) free(offsets); - TAILQ_FOREACH(p, &conf->ntp_peers, entry) + TAILQ_FOREACH(p, &conf->ntp_peers, entry) { + for (i = 0; i < OFFSET_ARRAY_SIZE; i++) + p->reply[i].offset -= offset_median; p->update.good = 0; + } return (0); } @@ -551,6 +554,7 @@ priv_host_dns(char *name, u_int32_t peerid) void update_scale(double offset) { + offset += getoffset(); if (offset < 0) offset = -offset; diff --git a/src/usr.sbin/ntpd/ntpd.c b/src/usr.sbin/ntpd/ntpd.c index 6d31ed2e..17b08b61 100644 --- a/src/usr.sbin/ntpd/ntpd.c +++ b/src/usr.sbin/ntpd/ntpd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ntpd.c,v 1.41 2006/02/21 23:47:00 stevesk Exp $ */ +/* $OpenBSD: ntpd.c,v 1.42 2006/06/07 06:29:03 otto Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -320,6 +320,7 @@ ntpd_adjtime(double d) int synced = 0; static int firstadj = 1; + d += getoffset(); if (d >= (double)LOG_NEGLIGEE / 1000 || d <= -1 * (double)LOG_NEGLIGEE / 1000) log_info("adjusting local clock by %fs", d); diff --git a/src/usr.sbin/ntpd/ntpd.h b/src/usr.sbin/ntpd/ntpd.h index ec7c0a7d..76aa6d09 100644 --- a/src/usr.sbin/ntpd/ntpd.h +++ b/src/usr.sbin/ntpd/ntpd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: ntpd.h,v 1.70 2006/06/04 18:58:13 otto Exp $ */ +/* $OpenBSD: ntpd.h,v 1.71 2006/06/07 06:29:03 otto Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -276,6 +276,8 @@ time_t error_interval(void); void set_next(struct ntp_peer *, time_t); /* util.c */ +double gettime_corrected(void); +double getoffset(void); double gettime(void); void d_to_tv(double, struct timeval *); double lfp_to_d(struct l_fixedpt); diff --git a/src/usr.sbin/ntpd/server.c b/src/usr.sbin/ntpd/server.c index e2529e5d..8b6352b4 100644 --- a/src/usr.sbin/ntpd/server.c +++ b/src/usr.sbin/ntpd/server.c @@ -1,4 +1,4 @@ -/* $OpenBSD: server.c,v 1.28 2006/01/19 11:20:23 dtucker Exp $ */ +/* $OpenBSD: server.c,v 1.29 2006/06/07 06:29:03 otto Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -137,7 +137,7 @@ server_dispatch(int fd, struct ntpd_conf *conf) fatal("recvfrom"); } - rectime = gettime(); + rectime = gettime_corrected(); if (ntp_getmsg((struct sockaddr *)&fsa, buf, size, &query) == -1) return (0); @@ -160,7 +160,7 @@ server_dispatch(int fd, struct ntpd_conf *conf) reply.precision = conf->status.precision; reply.rectime = d_to_lfp(rectime); reply.reftime = d_to_lfp(conf->status.reftime); - reply.xmttime = d_to_lfp(gettime()); + reply.xmttime = d_to_lfp(gettime_corrected()); reply.orgtime = query.xmttime; reply.rootdelay = d_to_sfp(conf->status.rootdelay); diff --git a/src/usr.sbin/ntpd/util.c b/src/usr.sbin/ntpd/util.c index 76cf52a0..41d42eb9 100644 --- a/src/usr.sbin/ntpd/util.c +++ b/src/usr.sbin/ntpd/util.c @@ -1,4 +1,4 @@ -/* $OpenBSD: util.c,v 1.10 2004/12/08 15:47:38 mickey Exp $ */ +/* $OpenBSD: util.c,v 1.11 2006/06/07 06:29:03 otto Exp $ */ /* * Copyright (c) 2004 Alexander Guy @@ -21,6 +21,21 @@ #include "ntpd.h" +double +gettime_corrected(void) +{ + return (gettime() + getoffset()); +} + +double +getoffset(void) +{ + struct timeval tv; + if (adjtime(NULL, &tv) == -1) + return (0.0); + return (tv.tv_sec + 1.0e-6 * tv.tv_usec); +} + double gettime(void) {