Browse Source

* 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@
OPENBSD_3_6
alexander 20 years ago
parent
commit
353bc5222d
5 changed files with 34 additions and 63 deletions
  1. +2
    -23
      src/usr.sbin/ntpd/ntp.c
  2. +1
    -11
      src/usr.sbin/ntpd/ntp_msg.c
  3. +2
    -2
      src/usr.sbin/ntpd/ntpd.h
  4. +27
    -25
      src/usr.sbin/ntpd/server.c
  5. +2
    -2
      src/usr.sbin/ntpd/util.c

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

@ -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 <henning@openbsd.org>
@ -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)
{


+ 1
- 11
src/usr.sbin/ntpd/ntp_msg.c View File

@ -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 <henning@openbsd.org>
@ -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;
}

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

@ -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 <henning@openbsd.org>
@ -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 *);


+ 27
- 25
src/usr.sbin/ntpd/server.c View File

@ -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 <henning@openbsd.org>
@ -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));
}

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

@ -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 <alexander.guy@andern.org>
@ -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);
}

Loading…
Cancel
Save