summaryrefslogtreecommitdiff
path: root/usr.sbin/ntpd
diff options
context:
space:
mode:
authorHenning Brauer <henning@cvs.openbsd.org>2004-06-02 10:09:00 +0000
committerHenning Brauer <henning@cvs.openbsd.org>2004-06-02 10:09:00 +0000
commit9bb777f068fbb34930ccc6fbf86342bd7b1ad4ed (patch)
tree75d3505c3517441194728b4c78cf51ff74fac756 /usr.sbin/ntpd
parent12c37e87c9b63c007a069648dd3827e7f9e21d13 (diff)
prepare for client functionality
Diffstat (limited to 'usr.sbin/ntpd')
-rw-r--r--usr.sbin/ntpd/Makefile5
-rw-r--r--usr.sbin/ntpd/config.c5
-rw-r--r--usr.sbin/ntpd/ntp.c222
-rw-r--r--usr.sbin/ntpd/ntp.h13
-rw-r--r--usr.sbin/ntpd/ntp_msg.c152
-rw-r--r--usr.sbin/ntpd/ntpd.h4
-rw-r--r--usr.sbin/ntpd/server.c113
7 files changed, 285 insertions, 229 deletions
diff --git a/usr.sbin/ntpd/Makefile b/usr.sbin/ntpd/Makefile
index 56866b6e1b5..96664576ced 100644
--- a/usr.sbin/ntpd/Makefile
+++ b/usr.sbin/ntpd/Makefile
@@ -1,9 +1,10 @@
-# $OpenBSD: Makefile,v 1.3 2004/06/01 21:58:08 henning Exp $
+# $OpenBSD: Makefile,v 1.4 2004/06/02 10:08:59 henning Exp $
.PATH: ${.CURDIR}/..
PROG= ntpd
-SRCS= ntpd.c buffer.c log.c imsg.c ntp.c parse.y config.c
+SRCS= ntpd.c buffer.c log.c imsg.c ntp.c ntp_msg.c parse.y config.c \
+ server.c
CFLAGS+= -Wall -I${.CURDIR}
CFLAGS+= -Wstrict-prototypes -Wmissing-prototypes
CFLAGS+= -Wmissing-declarations
diff --git a/usr.sbin/ntpd/config.c b/usr.sbin/ntpd/config.c
index 670f0e0b445..fa68f6cafa3 100644
--- a/usr.sbin/ntpd/config.c
+++ b/usr.sbin/ntpd/config.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: config.c,v 1.1 2004/06/01 21:58:08 henning Exp $ */
+/* $OpenBSD: config.c,v 1.2 2004/06/02 10:08:59 henning Exp $ */
/*
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
@@ -19,11 +19,8 @@
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
-#include <sys/mman.h>
#include <errno.h>
-#include <ifaddrs.h>
-#include <netdb.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
diff --git a/usr.sbin/ntpd/ntp.c b/usr.sbin/ntpd/ntp.c
index 78a7d2b92da..55538d58efa 100644
--- a/usr.sbin/ntpd/ntp.c
+++ b/usr.sbin/ntpd/ntp.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: ntp.c,v 1.5 2004/06/01 21:58:08 henning Exp $ */
+/* $OpenBSD: ntp.c,v 1.6 2004/06/02 10:08:59 henning Exp $ */
/*
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
@@ -19,7 +19,6 @@
#include <sys/param.h>
#include <errno.h>
-#include <netdb.h>
#include <poll.h>
#include <pwd.h>
#include <stdlib.h>
@@ -36,13 +35,8 @@ struct imsgbuf ibuf_main;
struct l_fixedpt ref_ts;
void ntp_sighdlr(int);
-int setup_listeners(struct servent *, struct ntpd_conf *);
int ntp_dispatch_imsg(void);
int ntp_dispatch(int fd);
-int ntp_getmsg(char *, ssize_t, struct ntp_msg *);
-int ntp_reply(int, struct sockaddr *, struct ntp_msg *, int);
-int ntp_sendmsg(int, struct sockaddr *, struct ntp_msg *, ssize_t, int);
-void get_ts(struct l_fixedpt *);
void
ntp_sighdlr(int sig)
@@ -107,7 +101,6 @@ ntp_main(int pipe_prnt[2], struct ntpd_conf *conf)
log_info("ntp engine ready");
while (ntp_quit == 0) {
- get_ts(&ref_ts); /* XXX */
bzero(&pfd, sizeof(pfd));
pfd[PFD_PIPE_MAIN].fd = ibuf_main.fd;
pfd[PFD_PIPE_MAIN].events = POLLIN;
@@ -155,57 +148,6 @@ ntp_main(int pipe_prnt[2], struct ntpd_conf *conf)
}
int
-setup_listeners(struct servent *se, struct ntpd_conf *conf)
-{
- struct listen_addr *la;
-
- if (TAILQ_EMPTY(&conf->listen_addrs)) {
- if ((la = calloc(1, sizeof(struct listen_addr))) == NULL)
- fatal("setup_listeners calloc");
- la->sa.ss_len = sizeof(struct sockaddr_in);
- ((struct sockaddr_in *)&la->sa)->sin_family = AF_INET;
- ((struct sockaddr_in *)&la->sa)->sin_addr.s_addr =
- htonl(INADDR_ANY);
- ((struct sockaddr_in *)&la->sa)->sin_port = se->s_port;
- TAILQ_INSERT_TAIL(&conf->listen_addrs, la, entry);
-
- if ((la = calloc(1, sizeof(struct listen_addr))) == NULL)
- fatal("setup_listeners calloc");
- la->sa.ss_len = sizeof(struct sockaddr_in6);
- ((struct sockaddr_in6 *)&la->sa)->sin6_family = AF_INET6;
- ((struct sockaddr_in6 *)&la->sa)->sin6_port = se->s_port;
- TAILQ_INSERT_TAIL(&conf->listen_addrs, la, entry);
- }
-
- TAILQ_FOREACH(la, &conf->listen_addrs, entry) {
- switch (la->sa.ss_family) {
- case AF_INET:
- if (((struct sockaddr_in *)&la->sa)->sin_port == 0)
- ((struct sockaddr_in *)&la->sa)->sin_port =
- se->s_port;
- break;
- case AF_INET6:
- if (((struct sockaddr_in6 *)&la->sa)->sin6_port == 0)
- ((struct sockaddr_in6 *)&la->sa)->sin6_port =
- se->s_port;
- break;
- default:
- fatalx("king bula sez: af borked");
-
- }
-
- if ((la->fd = socket(la->sa.ss_family, SOCK_DGRAM, 0)) == -1)
- fatal("socket");
-
- if (bind(la->fd, (struct sockaddr *)&la->sa, la->sa.ss_len) ==
- -1)
- fatal("bind");
- }
-
- return (0);
-}
-
-int
ntp_dispatch_imsg(void)
{
struct imsg imsg;
@@ -254,165 +196,3 @@ ntp_dispatch(int fd)
return (0);
}
-
-int
-ntp_getmsg(char *p, ssize_t len, struct ntp_msg *msg)
-{
- int auth, i;
-
- if (len == NTP_MSGSIZE)
- auth = 1;
- else if (len == NTP_MSGSIZE_NOAUTH)
- auth = 0;
- else {
- log_warnx("malformed packet received");
- return (-1);
- }
-
- memcpy(&msg->status, p, sizeof(msg->status));
- p += sizeof(msg->status);
- memcpy(&msg->stratum, p, sizeof(msg->stratum));
- p += sizeof(msg->stratum);
- memcpy(&msg->ppoll, p, sizeof(msg->ppoll));
- p += sizeof(msg->ppoll);
- memcpy(&msg->precision, p, sizeof(msg->precision));
- p += sizeof(msg->precision);
- memcpy(&msg->distance.int_part, p, sizeof(msg->distance.int_part));
- p += sizeof(msg->distance.int_part);
- memcpy(&msg->distance.fraction, p, sizeof(msg->distance.fraction));
- p += sizeof(msg->distance.fraction);
- memcpy(&msg->dispersion.int_part, p, sizeof(msg->dispersion.int_part));
- p += sizeof(msg->dispersion.int_part);
- memcpy(&msg->dispersion.fraction, p, sizeof(msg->dispersion.fraction));
- p += sizeof(msg->dispersion.fraction);
- memcpy(&msg->refid, p, sizeof(msg->refid));
- p += sizeof(msg->refid);
- memcpy(&msg->reftime.int_part, p, sizeof(msg->reftime.int_part));
- p += sizeof(msg->reftime.int_part);
- memcpy(&msg->reftime.fraction, p, sizeof(msg->reftime.fraction));
- p += sizeof(msg->reftime.fraction);
- memcpy(&msg->orgtime.int_part, p, sizeof(msg->orgtime.int_part));
- p += sizeof(msg->orgtime.int_part);
- memcpy(&msg->orgtime.fraction, p, sizeof(msg->orgtime.fraction));
- p += sizeof(msg->orgtime.fraction);
- memcpy(&msg->rectime.int_part, p, sizeof(msg->rectime.int_part));
- p += sizeof(msg->rectime.int_part);
- memcpy(&msg->rectime.fraction, p, sizeof(msg->rectime.fraction));
- p += sizeof(msg->rectime.fraction);
- memcpy(&msg->xmttime.int_part, p, sizeof(msg->xmttime.int_part));
- p += sizeof(msg->xmttime.int_part);
- memcpy(&msg->xmttime.fraction, p, sizeof(msg->xmttime.fraction));
- p += sizeof(msg->xmttime.fraction);
-
- if (auth) {
- memcpy(&msg->keyid, p, sizeof(msg->keyid));
- p += sizeof(msg->refid);
- for (i = 0; i < NTP_DIGESTSIZE; i++) {
- memcpy(&msg->digest[i], p, sizeof(msg->digest[i]));
- p += sizeof(msg->digest[i]);
- }
-
- /* XXX check auth */
- }
-
- return (0);
-}
-
-int
-ntp_reply(int fd, struct sockaddr *sa, struct ntp_msg *query, int auth)
-{
- ssize_t len;
- struct l_fixedpt t;
- struct ntp_msg reply;
-
- if (auth)
- len = NTP_MSGSIZE;
- else
- len = NTP_MSGSIZE_NOAUTH;
-
- bzero(&reply, sizeof(reply));
- 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.precision = 0; /* XXX */
- reply.refid = htonl(t.fraction); /* XXX */
- reply.reftime.int_part = htonl(ref_ts.int_part);
- reply.reftime.fraction = htonl(ref_ts.fraction);
- get_ts(&t);
- 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));
-}
-
-int
-ntp_sendmsg(int fd, struct sockaddr *sa, struct ntp_msg *msg, ssize_t len,
- int auth)
-{
- char buf[NTP_MSGSIZE];
- char *p;
-
- p = buf;
- memcpy(p, &msg->status, sizeof(msg->status));
- p += sizeof(msg->status);
- memcpy(p, &msg->stratum, sizeof(msg->stratum));
- p += sizeof(msg->stratum);
- memcpy(p, &msg->ppoll, sizeof(msg->ppoll));
- p += sizeof(msg->ppoll);
- memcpy(p, &msg->precision, sizeof(msg->precision));
- p += sizeof(msg->precision);
- memcpy(p, &msg->distance.int_part, sizeof(msg->distance.int_part));
- p += sizeof(msg->distance.int_part);
- memcpy(p, &msg->distance.fraction, sizeof(msg->distance.fraction));
- p += sizeof(msg->distance.fraction);
- memcpy(p, &msg->dispersion.int_part, sizeof(msg->dispersion.int_part));
- p += sizeof(msg->dispersion.int_part);
- memcpy(p, &msg->dispersion.fraction, sizeof(msg->dispersion.fraction));
- p += sizeof(msg->dispersion.fraction);
- memcpy(p, &msg->refid, sizeof(msg->refid));
- p += sizeof(msg->refid);
- memcpy(p, &msg->reftime.int_part, sizeof(msg->reftime.int_part));
- p += sizeof(msg->reftime.int_part);
- memcpy(p, &msg->reftime.fraction, sizeof(msg->reftime.fraction));
- p += sizeof(msg->reftime.fraction);
- memcpy(p, &msg->orgtime.int_part, sizeof(msg->orgtime.int_part));
- p += sizeof(msg->orgtime.int_part);
- memcpy(p, &msg->orgtime.fraction, sizeof(msg->orgtime.fraction));
- p += sizeof(msg->orgtime.fraction);
- memcpy(p, &msg->rectime.int_part, sizeof(msg->rectime.int_part));
- p += sizeof(msg->rectime.int_part);
- memcpy(p, &msg->rectime.fraction, sizeof(msg->rectime.fraction));
- p += sizeof(msg->rectime.fraction);
- memcpy(p, &msg->xmttime.int_part, sizeof(msg->xmttime.int_part));
- p += sizeof(msg->xmttime.int_part);
- memcpy(p, &msg->xmttime.fraction, sizeof(msg->xmttime.fraction));
- p += sizeof(msg->xmttime.fraction);
-
- if (auth) {
- /* XXX */
- }
-
- if (sendto(fd, &buf, len, 0, sa, sa->sa_len) != len)
- fatal("sendto");
-
- 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;
-} \ No newline at end of file
diff --git a/usr.sbin/ntpd/ntp.h b/usr.sbin/ntpd/ntp.h
index e3f6b12061b..489a8d20981 100644
--- a/usr.sbin/ntpd/ntp.h
+++ b/usr.sbin/ntpd/ntp.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: ntp.h,v 1.2 2004/06/01 22:02:32 henning Exp $ */
+/* $OpenBSD: ntp.h,v 1.3 2004/06/02 10:08:59 henning Exp $ */
/*
* Copyright (c) 2004 Henning Brauer <henning@openbsd.org>
@@ -134,3 +134,14 @@ struct ntp_msg {
#define MODE_RES2 7 /* reserved for private use */
#define JAN_1970 2208988800UL /* 1970 - 1900 in seconds */
+
+/* prototypes */
+
+/* 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 *);
+int ntp_reply(int, struct sockaddr *, struct ntp_msg *, int);
diff --git a/usr.sbin/ntpd/ntp_msg.c b/usr.sbin/ntpd/ntp_msg.c
new file mode 100644
index 00000000000..178ae7cc1c9
--- /dev/null
+++ b/usr.sbin/ntpd/ntp_msg.c
@@ -0,0 +1,152 @@
+/* $OpenBSD: ntp_msg.c,v 1.1 2004/06/02 10:08:59 henning Exp $ */
+
+/*
+ * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
+ * 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/param.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "ntpd.h"
+#include "ntp.h"
+
+int
+ntp_getmsg(char *p, ssize_t len, struct ntp_msg *msg)
+{
+ int auth, i;
+
+ if (len == NTP_MSGSIZE)
+ auth = 1;
+ else if (len == NTP_MSGSIZE_NOAUTH)
+ auth = 0;
+ else {
+ log_warnx("malformed packet received");
+ return (-1);
+ }
+
+ memcpy(&msg->status, p, sizeof(msg->status));
+ p += sizeof(msg->status);
+ memcpy(&msg->stratum, p, sizeof(msg->stratum));
+ p += sizeof(msg->stratum);
+ memcpy(&msg->ppoll, p, sizeof(msg->ppoll));
+ p += sizeof(msg->ppoll);
+ memcpy(&msg->precision, p, sizeof(msg->precision));
+ p += sizeof(msg->precision);
+ memcpy(&msg->distance.int_part, p, sizeof(msg->distance.int_part));
+ p += sizeof(msg->distance.int_part);
+ memcpy(&msg->distance.fraction, p, sizeof(msg->distance.fraction));
+ p += sizeof(msg->distance.fraction);
+ memcpy(&msg->dispersion.int_part, p, sizeof(msg->dispersion.int_part));
+ p += sizeof(msg->dispersion.int_part);
+ memcpy(&msg->dispersion.fraction, p, sizeof(msg->dispersion.fraction));
+ p += sizeof(msg->dispersion.fraction);
+ memcpy(&msg->refid, p, sizeof(msg->refid));
+ p += sizeof(msg->refid);
+ memcpy(&msg->reftime.int_part, p, sizeof(msg->reftime.int_part));
+ p += sizeof(msg->reftime.int_part);
+ memcpy(&msg->reftime.fraction, p, sizeof(msg->reftime.fraction));
+ p += sizeof(msg->reftime.fraction);
+ memcpy(&msg->orgtime.int_part, p, sizeof(msg->orgtime.int_part));
+ p += sizeof(msg->orgtime.int_part);
+ memcpy(&msg->orgtime.fraction, p, sizeof(msg->orgtime.fraction));
+ p += sizeof(msg->orgtime.fraction);
+ memcpy(&msg->rectime.int_part, p, sizeof(msg->rectime.int_part));
+ p += sizeof(msg->rectime.int_part);
+ memcpy(&msg->rectime.fraction, p, sizeof(msg->rectime.fraction));
+ p += sizeof(msg->rectime.fraction);
+ memcpy(&msg->xmttime.int_part, p, sizeof(msg->xmttime.int_part));
+ p += sizeof(msg->xmttime.int_part);
+ memcpy(&msg->xmttime.fraction, p, sizeof(msg->xmttime.fraction));
+ p += sizeof(msg->xmttime.fraction);
+
+ if (auth) {
+ memcpy(&msg->keyid, p, sizeof(msg->keyid));
+ p += sizeof(msg->refid);
+ for (i = 0; i < NTP_DIGESTSIZE; i++) {
+ memcpy(&msg->digest[i], p, sizeof(msg->digest[i]));
+ p += sizeof(msg->digest[i]);
+ }
+
+ /* XXX check auth */
+ }
+
+ return (0);
+}
+
+int
+ntp_sendmsg(int fd, struct sockaddr *sa, struct ntp_msg *msg, ssize_t len,
+ int auth)
+{
+ char buf[NTP_MSGSIZE];
+ char *p;
+
+ p = buf;
+ memcpy(p, &msg->status, sizeof(msg->status));
+ p += sizeof(msg->status);
+ memcpy(p, &msg->stratum, sizeof(msg->stratum));
+ p += sizeof(msg->stratum);
+ memcpy(p, &msg->ppoll, sizeof(msg->ppoll));
+ p += sizeof(msg->ppoll);
+ memcpy(p, &msg->precision, sizeof(msg->precision));
+ p += sizeof(msg->precision);
+ memcpy(p, &msg->distance.int_part, sizeof(msg->distance.int_part));
+ p += sizeof(msg->distance.int_part);
+ memcpy(p, &msg->distance.fraction, sizeof(msg->distance.fraction));
+ p += sizeof(msg->distance.fraction);
+ memcpy(p, &msg->dispersion.int_part, sizeof(msg->dispersion.int_part));
+ p += sizeof(msg->dispersion.int_part);
+ memcpy(p, &msg->dispersion.fraction, sizeof(msg->dispersion.fraction));
+ p += sizeof(msg->dispersion.fraction);
+ memcpy(p, &msg->refid, sizeof(msg->refid));
+ p += sizeof(msg->refid);
+ memcpy(p, &msg->reftime.int_part, sizeof(msg->reftime.int_part));
+ p += sizeof(msg->reftime.int_part);
+ memcpy(p, &msg->reftime.fraction, sizeof(msg->reftime.fraction));
+ p += sizeof(msg->reftime.fraction);
+ memcpy(p, &msg->orgtime.int_part, sizeof(msg->orgtime.int_part));
+ p += sizeof(msg->orgtime.int_part);
+ memcpy(p, &msg->orgtime.fraction, sizeof(msg->orgtime.fraction));
+ p += sizeof(msg->orgtime.fraction);
+ memcpy(p, &msg->rectime.int_part, sizeof(msg->rectime.int_part));
+ p += sizeof(msg->rectime.int_part);
+ memcpy(p, &msg->rectime.fraction, sizeof(msg->rectime.fraction));
+ p += sizeof(msg->rectime.fraction);
+ memcpy(p, &msg->xmttime.int_part, sizeof(msg->xmttime.int_part));
+ p += sizeof(msg->xmttime.int_part);
+ memcpy(p, &msg->xmttime.fraction, sizeof(msg->xmttime.fraction));
+ p += sizeof(msg->xmttime.fraction);
+
+ if (auth) {
+ /* XXX */
+ }
+
+ if (sendto(fd, &buf, len, 0, sa, sa->sa_len) != len)
+ fatal("sendto");
+
+ 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/usr.sbin/ntpd/ntpd.h b/usr.sbin/ntpd/ntpd.h
index bd5cd05a648..078facd7fb2 100644
--- a/usr.sbin/ntpd/ntpd.h
+++ b/usr.sbin/ntpd/ntpd.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: ntpd.h,v 1.3 2004/06/01 21:58:09 henning Exp $ */
+/* $OpenBSD: ntpd.h,v 1.4 2004/06/02 10:08:59 henning Exp $ */
/*
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
@@ -21,6 +21,7 @@
#include <sys/queue.h>
#include <netinet/in.h>
#include <arpa/inet.h>
+#include <netdb.h>
#include <stdarg.h>
#define NTPD_USER "_ntp"
@@ -132,3 +133,4 @@ int cmdline_symset(char *);
/* config.c */
int check_file_secrecy(int, const char *);
int host(const char *, struct sockaddr *, u_int8_t *);
+
diff --git a/usr.sbin/ntpd/server.c b/usr.sbin/ntpd/server.c
new file mode 100644
index 00000000000..8b76c6598e4
--- /dev/null
+++ b/usr.sbin/ntpd/server.c
@@ -0,0 +1,113 @@
+/* $OpenBSD: server.c,v 1.1 2004/06/02 10:08:59 henning Exp $ */
+
+/*
+ * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.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/param.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "ntpd.h"
+#include "ntp.h"
+
+int
+setup_listeners(struct servent *se, struct ntpd_conf *conf)
+{
+ struct listen_addr *la;
+
+ if (TAILQ_EMPTY(&conf->listen_addrs)) {
+ if ((la = calloc(1, sizeof(struct listen_addr))) == NULL)
+ fatal("setup_listeners calloc");
+ la->sa.ss_len = sizeof(struct sockaddr_in);
+ ((struct sockaddr_in *)&la->sa)->sin_family = AF_INET;
+ ((struct sockaddr_in *)&la->sa)->sin_addr.s_addr =
+ htonl(INADDR_ANY);
+ ((struct sockaddr_in *)&la->sa)->sin_port = se->s_port;
+ TAILQ_INSERT_TAIL(&conf->listen_addrs, la, entry);
+
+ if ((la = calloc(1, sizeof(struct listen_addr))) == NULL)
+ fatal("setup_listeners calloc");
+ la->sa.ss_len = sizeof(struct sockaddr_in6);
+ ((struct sockaddr_in6 *)&la->sa)->sin6_family = AF_INET6;
+ ((struct sockaddr_in6 *)&la->sa)->sin6_port = se->s_port;
+ TAILQ_INSERT_TAIL(&conf->listen_addrs, la, entry);
+ }
+
+ TAILQ_FOREACH(la, &conf->listen_addrs, entry) {
+ switch (la->sa.ss_family) {
+ case AF_INET:
+ if (((struct sockaddr_in *)&la->sa)->sin_port == 0)
+ ((struct sockaddr_in *)&la->sa)->sin_port =
+ se->s_port;
+ break;
+ case AF_INET6:
+ if (((struct sockaddr_in6 *)&la->sa)->sin6_port == 0)
+ ((struct sockaddr_in6 *)&la->sa)->sin6_port =
+ se->s_port;
+ break;
+ default:
+ fatalx("king bula sez: af borked");
+
+ }
+
+ if ((la->fd = socket(la->sa.ss_family, SOCK_DGRAM, 0)) == -1)
+ fatal("socket");
+
+ if (bind(la->fd, (struct sockaddr *)&la->sa, la->sa.ss_len) ==
+ -1)
+ fatal("bind");
+ }
+
+ return (0);
+}
+
+int
+ntp_reply(int fd, struct sockaddr *sa, struct ntp_msg *query, int auth)
+{
+ ssize_t len;
+ struct l_fixedpt t;
+ struct ntp_msg reply;
+
+ if (auth)
+ len = NTP_MSGSIZE;
+ else
+ len = NTP_MSGSIZE_NOAUTH;
+
+ bzero(&reply, sizeof(reply));
+ 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.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));
+}