summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Guy <alexander@cvs.openbsd.org>2004-07-04 11:01:50 +0000
committerAlexander Guy <alexander@cvs.openbsd.org>2004-07-04 11:01:50 +0000
commitbe3980131a31107d19494946d6a9af420ae10f0d (patch)
tree300b3381f74e98440ec0d3148044164dbd8c6981
parentbbad53b0c249028523868f4424bb8926cb1bdab3 (diff)
Compute the local clock offset from the server's response.
ok henning@
-rw-r--r--usr.sbin/ntpd/Makefile4
-rw-r--r--usr.sbin/ntpd/client.c38
-rw-r--r--usr.sbin/ntpd/ntp.h4
-rw-r--r--usr.sbin/ntpd/ntpd.h9
-rw-r--r--usr.sbin/ntpd/util.c59
5 files changed, 101 insertions, 13 deletions
diff --git a/usr.sbin/ntpd/Makefile b/usr.sbin/ntpd/Makefile
index 785de81d37a..86cb8cc8ff6 100644
--- a/usr.sbin/ntpd/Makefile
+++ b/usr.sbin/ntpd/Makefile
@@ -1,10 +1,10 @@
-# $OpenBSD: Makefile,v 1.6 2004/06/18 04:48:12 henning Exp $
+# $OpenBSD: Makefile,v 1.7 2004/07/04 11:01:49 alexander Exp $
.PATH: ${.CURDIR}/..
PROG= ntpd
SRCS= ntpd.c buffer.c log.c imsg.c ntp.c ntp_msg.c parse.y config.c \
- server.c client.c
+ server.c client.c util.c
CFLAGS+= -Wall -I${.CURDIR}
CFLAGS+= -Wstrict-prototypes -Wmissing-prototypes
CFLAGS+= -Wmissing-declarations
diff --git a/usr.sbin/ntpd/client.c b/usr.sbin/ntpd/client.c
index b79ea20e317..3cab1788e54 100644
--- a/usr.sbin/ntpd/client.c
+++ b/usr.sbin/ntpd/client.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: client.c,v 1.2 2004/07/03 21:11:29 alexander Exp $ */
+/* $OpenBSD: client.c,v 1.3 2004/07/04 11:01:49 alexander Exp $ */
/*
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
@@ -80,7 +80,7 @@ client_query(struct ntp_peer *p)
p->query->msg.xmttime.int_part = arc4random();
p->query->msg.xmttime.fraction = arc4random();
- get_ts(&p->query->xmttime);
+ p->query->xmttime = gettime();
ntp_sendmsg(p->query->fd, (struct sockaddr *)&p->ss, &p->query->msg,
NTP_MSGSIZE_NOAUTH, 0);
@@ -99,13 +99,16 @@ client_dispatch(struct ntp_peer *p)
char buf[NTP_MSGSIZE];
ssize_t size;
struct ntp_msg msg;
- struct l_fixedpt rtt, t;
+ double T1, T2, T3, T4;
+ double offset, error;
fsa_len = sizeof(fsa);
if ((size = recvfrom(p->query->fd, &buf, sizeof(buf), 0,
(struct sockaddr *)&fsa, &fsa_len)) == -1)
fatal("recvfrom");
+ T4 = gettime();
+
ntp_getmsg(buf, size, &msg);
if (msg.orgtime.int_part != p->query->msg.xmttime.int_part ||
@@ -114,16 +117,35 @@ client_dispatch(struct ntp_peer *p)
return (0);
}
-log_debug("reply received");
+ /*
+ * From RFC 2030:
+ *
+ * Timestamp Name ID When Generated
+ * ------------------------------------------------------------
+ * Originate Timestamp T1 time request sent by client
+ * Receive Timestamp T2 time request received by server
+ * Transmit Timestamp T3 time reply sent by server
+ * Destination Timestamp T4 time reply received by client
+ *
+ * The roundtrip delay d and local clock offset t are defined as
+ *
+ * d = (T4 - T1) - (T2 - T3) t = ((T2 - T1) + (T3 - T4)) / 2.
+ */
+
+ T1 = p->query->xmttime;
+ T2 = lfp_to_d(msg.rectime);
+ T3 = lfp_to_d(msg.xmttime);
- /* XXX parse */
- get_ts(&t);
- rtt.int_part = (t.int_part - p->query->xmttime.int_part) -
- (msg.rectime.int_part - msg.xmttime.int_part);
+ offset = ((T2 - T1) + (T3 - T4)) / 2;
+ error = (T2 - T1) - (T3 - T4);
p->state = STATE_REPLY_RECEIVED;
p->next = time(NULL) + QUERY_INTERVAL;
p->deadline = 0;
+ p->offset = offset;
+ p->error = error;
+
+ log_debug("reply received: offset %f error %f", offset, error);
return (0);
}
diff --git a/usr.sbin/ntpd/ntp.h b/usr.sbin/ntpd/ntp.h
index 8a63c006904..8a3c49ae5d0 100644
--- a/usr.sbin/ntpd/ntp.h
+++ b/usr.sbin/ntpd/ntp.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: ntp.h,v 1.5 2004/06/17 19:17:48 henning Exp $ */
+/* $OpenBSD: ntp.h,v 1.6 2004/07/04 11:01:49 alexander Exp $ */
/*
* Copyright (c) 2004 Henning Brauer <henning@openbsd.org>
@@ -112,7 +112,7 @@ struct ntp_msg {
struct ntp_query {
int fd;
struct ntp_msg msg;
- struct l_fixedpt xmttime;
+ double xmttime;
};
/*
diff --git a/usr.sbin/ntpd/ntpd.h b/usr.sbin/ntpd/ntpd.h
index 8bc4ec0fce2..85fcb273fbb 100644
--- a/usr.sbin/ntpd/ntpd.h
+++ b/usr.sbin/ntpd/ntpd.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: ntpd.h,v 1.7 2004/06/18 04:51:31 henning Exp $ */
+/* $OpenBSD: ntpd.h,v 1.8 2004/07/04 11:01:49 alexander Exp $ */
/*
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
@@ -55,6 +55,8 @@ struct ntp_peer {
enum client_state state;
time_t next;
time_t deadline;
+ double offset;
+ double error;
};
struct ntpd_conf {
@@ -168,3 +170,8 @@ int ntp_reply(int, struct sockaddr *, struct ntp_msg *, int);
int client_peer_init(struct ntp_peer *);
int client_query(struct ntp_peer *);
int client_dispatch(struct ntp_peer *);
+
+/* util.c */
+double gettime(void);
+double lfp_to_d(struct l_fixedpt);
+struct l_fixedpt d_to_lfp(double);
diff --git a/usr.sbin/ntpd/util.c b/usr.sbin/ntpd/util.c
new file mode 100644
index 00000000000..f80d71b4e89
--- /dev/null
+++ b/usr.sbin/ntpd/util.c
@@ -0,0 +1,59 @@
+/* $OpenBSD: util.c,v 1.1 2004/07/04 11:01:49 alexander Exp $ */
+
+/*
+ * Copyright (c) 2004 Alexander Guy <alexander.guy@andern.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
+ * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
+ * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <sys/time.h>
+
+#include "ntpd.h"
+
+double
+gettime(void)
+{
+ struct timeval tv;
+ double ret;
+
+ if (gettimeofday(&tv, NULL) == -1)
+ fatal("gettimeofday");
+
+ ret = tv.tv_sec + JAN_1970 + 1.0e-6 * tv.tv_usec;
+
+ return (ret);
+}
+
+double
+lfp_to_d(struct l_fixedpt lfp)
+{
+ double ret;
+
+ lfp.int_part = ntohl(lfp.int_part);
+ lfp.fraction = ntohl(lfp.fraction);
+
+ ret = (double)(lfp.int_part) + ((double)lfp.fraction / UINT_MAX);
+
+ return (ret);
+}
+
+struct l_fixedpt
+d_to_lfp(double d)
+{
+ struct l_fixedpt lfp;
+
+ lfp.int_part = htonl(d);
+ lfp.fraction = htonl((d - lfp.int_part) * UINT_MAX);
+
+ return (lfp);
+}