From 353bc5222df27ad90ee3833943bb2ba4c4953e1a Mon Sep 17 00:00:00 2001 From: alexander <> Date: Wed, 7 Jul 2004 07:32:05 +0000 Subject: [PATCH] * Convert to use the new double-based time handling functions. * Respond to the query with a reasonable received time (which will help clients get better accuracy). * Consolidate the server response code in preparation for a completely 'proper' response to the client. tips and ok from henning@ --- src/usr.sbin/ntpd/ntp.c | 25 ++---------------- src/usr.sbin/ntpd/ntp_msg.c | 12 +-------- src/usr.sbin/ntpd/ntpd.h | 4 +-- src/usr.sbin/ntpd/server.c | 52 +++++++++++++++++++------------------ src/usr.sbin/ntpd/util.c | 4 +-- 5 files changed, 34 insertions(+), 63 deletions(-) diff --git a/src/usr.sbin/ntpd/ntp.c b/src/usr.sbin/ntpd/ntp.c index ec350e49..73892f54 100644 --- a/src/usr.sbin/ntpd/ntp.c +++ b/src/usr.sbin/ntpd/ntp.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ntp.c,v 1.14 2004/07/07 03:57:28 henning Exp $ */ +/* $OpenBSD: ntp.c,v 1.15 2004/07/07 07:32:05 alexander Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -37,7 +37,6 @@ struct l_fixedpt ref_ts; void ntp_sighdlr(int); int ntp_dispatch_imsg(void); -int ntp_dispatch(int fd); void ntp_adjtime(struct ntpd_conf *); int get_peer_update(struct ntp_peer *, double *); @@ -219,7 +218,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 (ntp_dispatch(pfd[j].fd) == -1) + if (server_dispatch(pfd[j].fd) == -1) ntp_quit = 1; } @@ -269,26 +268,6 @@ ntp_dispatch_imsg(void) return (0); } -int -ntp_dispatch(int fd) -{ - struct sockaddr_storage fsa; - socklen_t fsa_len; - char buf[NTP_MSGSIZE]; - ssize_t size; - struct ntp_msg msg; - - fsa_len = sizeof(fsa); - if ((size = recvfrom(fd, &buf, sizeof(buf), 0, - (struct sockaddr *)&fsa, &fsa_len)) == -1) - fatal("recvfrom"); - - ntp_getmsg(buf, size, &msg); - ntp_reply(fd, (struct sockaddr *)&fsa, &msg, 0); - - return (0); -} - void ntp_adjtime(struct ntpd_conf *conf) { diff --git a/src/usr.sbin/ntpd/ntp_msg.c b/src/usr.sbin/ntpd/ntp_msg.c index 178ae7cc..a053678a 100644 --- a/src/usr.sbin/ntpd/ntp_msg.c +++ b/src/usr.sbin/ntpd/ntp_msg.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ntp_msg.c,v 1.1 2004/06/02 10:08:59 henning Exp $ */ +/* $OpenBSD: ntp_msg.c,v 1.2 2004/07/07 07:32:05 alexander Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -140,13 +140,3 @@ ntp_sendmsg(int fd, struct sockaddr *sa, struct ntp_msg *msg, ssize_t len, return (0); } - -void -get_ts(struct l_fixedpt *t) -{ - struct timeval tv; - - gettimeofday(&tv, NULL); - t->int_part = tv.tv_sec + JAN_1970; - t->fraction = ((float)tv.tv_usec)/1000000 * UINT_MAX; -} diff --git a/src/usr.sbin/ntpd/ntpd.h b/src/usr.sbin/ntpd/ntpd.h index 2f790282..643fbd41 100644 --- a/src/usr.sbin/ntpd/ntpd.h +++ b/src/usr.sbin/ntpd/ntpd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: ntpd.h,v 1.16 2004/07/07 05:47:57 henning Exp $ */ +/* $OpenBSD: ntpd.h,v 1.17 2004/07/07 07:32:05 alexander Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -187,13 +187,13 @@ int check_file_secrecy(int, const char *); struct ntp_addr *host(const char *, u_int8_t *); /* ntp_msg.c */ -void get_ts(struct l_fixedpt *); int ntp_getmsg(char *, ssize_t, struct ntp_msg *); 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 fd); /* 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 4bee0c53..36c4cfd9 100644 --- a/src/usr.sbin/ntpd/server.c +++ b/src/usr.sbin/ntpd/server.c @@ -1,4 +1,4 @@ -/* $OpenBSD: server.c,v 1.6 2004/07/07 07:05:35 henning Exp $ */ +/* $OpenBSD: server.c,v 1.7 2004/07/07 07:32:05 alexander Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -92,37 +92,39 @@ setup_listeners(struct servent *se, struct ntpd_conf *conf, u_int *cnt) } int -ntp_reply(int fd, struct sockaddr *sa, struct ntp_msg *query, int auth) +server_dispatch(int fd) { - ssize_t len; - struct l_fixedpt t; - struct ntp_msg reply; + ssize_t size; + double rectime; + struct sockaddr_storage fsa; + socklen_t fsa_len; + struct ntp_msg query, reply; + char buf[NTP_MSGSIZE]; - if (auth) - len = NTP_MSGSIZE; - else - len = NTP_MSGSIZE_NOAUTH; + fsa_len = sizeof(fsa); + if ((size = recvfrom(fd, &buf, sizeof(buf), 0, + (struct sockaddr *)&fsa, &fsa_len)) == -1) + fatal("recvfrom"); + + rectime = gettime(); + + ntp_getmsg(buf, size, &query); bzero(&reply, sizeof(reply)); - reply.status = 0 | (query->status & VERSIONMASK); - if ((query->status & MODEMASK) == MODE_CLIENT) + reply.status = 0 | (query.status & VERSIONMASK); + if ((query.status & MODEMASK) == MODE_CLIENT) reply.status |= MODE_SERVER; else reply.status |= MODE_SYM_PAS; - reply.stratum = 2; - reply.ppoll = query->ppoll; + reply.stratum = 2; /* XXX */ + reply.ppoll = query.ppoll; reply.precision = 0; /* XXX */ - reply.refid = htonl(t.fraction); /* XXX */ - get_ts(&t); - reply.reftime.int_part = htonl(t.int_part); /* XXX */ - reply.reftime.fraction = htonl(t.fraction); /* XXX */ - reply.rectime.int_part = htonl(t.int_part); - reply.rectime.fraction = htonl(t.fraction); - reply.xmttime.int_part = htonl(t.int_part); - reply.xmttime.fraction = htonl(t.fraction); - reply.orgtime.int_part = query->xmttime.int_part; - reply.orgtime.fraction = query->xmttime.fraction; - - return (ntp_sendmsg(fd, sa, &reply, len, auth)); + reply.rectime = d_to_lfp(rectime); + reply.reftime = reply.rectime; /* XXX */ + reply.xmttime = d_to_lfp(gettime()); + reply.orgtime = query.xmttime; + reply.refid = reply.xmttime.fraction; /* XXX */ + + return (ntp_sendmsg(fd, (struct sockaddr *)&fsa, &reply, size, 0)); } diff --git a/src/usr.sbin/ntpd/util.c b/src/usr.sbin/ntpd/util.c index e218aea5..090e8075 100644 --- a/src/usr.sbin/ntpd/util.c +++ b/src/usr.sbin/ntpd/util.c @@ -1,4 +1,4 @@ -/* $OpenBSD: util.c,v 1.5 2004/07/07 07:16:16 alexander Exp $ */ +/* $OpenBSD: util.c,v 1.6 2004/07/07 07:32:05 alexander Exp $ */ /* * Copyright (c) 2004 Alexander Guy @@ -58,7 +58,7 @@ d_to_lfp(double d) struct l_fixedpt lfp; lfp.int_part = htonl(d); - lfp.fraction = htonl((d - (u_int32_t)d) * UINT_MAX); + lfp.fraction = htonl((u_int32_t)((d - (u_int32_t)d) * UINT_MAX)); return (lfp); }