Browse Source

Compute the local clock offset from the server's response.

ok henning@
OPENBSD_3_6
alexander 20 years ago
parent
commit
9b6618babd
5 changed files with 101 additions and 13 deletions
  1. +2
    -2
      src/usr.sbin/ntpd/Makefile
  2. +30
    -8
      src/usr.sbin/ntpd/client.c
  3. +2
    -2
      src/usr.sbin/ntpd/ntp.h
  4. +8
    -1
      src/usr.sbin/ntpd/ntpd.h
  5. +59
    -0
      src/usr.sbin/ntpd/util.c

+ 2
- 2
src/usr.sbin/ntpd/Makefile View File

@ -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


+ 30
- 8
src/usr.sbin/ntpd/client.c View File

@ -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);
}

+ 2
- 2
src/usr.sbin/ntpd/ntp.h View File

@ -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;
};
/*


+ 8
- 1
src/usr.sbin/ntpd/ntpd.h View File

@ -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);

+ 59
- 0
src/usr.sbin/ntpd/util.c View File

@ -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);
}

Loading…
Cancel
Save