From 944ef50b12f1a440804f699009ae7a1638c96137 Mon Sep 17 00:00:00 2001 From: henning <> Date: Fri, 24 Sep 2004 14:51:16 +0000 Subject: [PATCH] connect() the client-side sockets. idea & test & ok camield@ --- src/usr.sbin/ntpd/client.c | 31 ++++++++++++++++++------------- src/usr.sbin/ntpd/ntp_msg.c | 14 ++++++++++---- 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/src/usr.sbin/ntpd/client.c b/src/usr.sbin/ntpd/client.c index 816f72f9..fa6f84d2 100644 --- a/src/usr.sbin/ntpd/client.c +++ b/src/usr.sbin/ntpd/client.c @@ -1,4 +1,4 @@ -/* $OpenBSD: client.c,v 1.34 2004/09/18 20:01:38 henning Exp $ */ +/* $OpenBSD: client.c,v 1.35 2004/09/24 14:51:16 henning Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -83,9 +83,14 @@ client_addr_init(struct ntp_peer *p) } } - if (p->addr != NULL && - (p->query->fd = socket(p->addr->ss.ss_family, SOCK_DGRAM, 0)) == -1) - fatal("client_query socket"); + if (p->addr != NULL) { + if ((p->query->fd = socket(p->addr->ss.ss_family, + SOCK_DGRAM, 0)) == -1) + fatal("client_query socket"); + if (connect(p->query->fd, (struct sockaddr *)&p->addr->ss, + p->addr->ss.ss_len) == -1) + fatal("client_query connect"); + } set_next(p, 0); @@ -108,6 +113,9 @@ client_nextaddr(struct ntp_peer *p) if ((p->query->fd = socket(p->addr->ss.ss_family, SOCK_DGRAM, 0)) == -1) fatal("client_query socket"); + if (connect(p->query->fd, (struct sockaddr *)&p->addr->ss, + p->addr->ss.ss_len) == -1) + fatal("client_query connect"); p->shift = 0; p->trustlevel = TRUSTLEVEL_PATHETIC; @@ -141,8 +149,8 @@ client_query(struct ntp_peer *p) p->query->msg.xmttime.fraction = arc4random(); p->query->xmttime = gettime(); - if (ntp_sendmsg(p->query->fd, (struct sockaddr *)&p->addr->ss, - &p->query->msg, NTP_MSGSIZE_NOAUTH, 0) == -1) { + if (ntp_sendmsg(p->query->fd, NULL, &p->query->msg, + NTP_MSGSIZE_NOAUTH, 0) == -1) { set_next(p, INTERVAL_QUERY_PATHETIC); return (-1); } @@ -156,8 +164,6 @@ client_query(struct ntp_peer *p) int client_dispatch(struct ntp_peer *p, u_int8_t settime) { - struct sockaddr_storage fsa; - socklen_t fsa_len; char buf[NTP_MSGSIZE]; ssize_t size; struct ntp_msg msg; @@ -165,13 +171,12 @@ client_dispatch(struct ntp_peer *p, u_int8_t settime) double abs_offset; time_t interval; - fsa_len = sizeof(fsa); if ((size = recvfrom(p->query->fd, &buf, sizeof(buf), 0, - (struct sockaddr *)&fsa, &fsa_len)) == -1) { + NULL, NULL)) == -1) { if (errno == EHOSTUNREACH || errno == EHOSTDOWN || errno == ENETDOWN) { log_warn("recvfrom %s", - log_sockaddr((struct sockaddr *)&fsa)); + log_sockaddr((struct sockaddr *)&p->addr->ss)); return (0); } else fatal("recvfrom"); @@ -246,7 +251,7 @@ client_dispatch(struct ntp_peer *p, u_int8_t settime) if (p->trustlevel < TRUSTLEVEL_BADPEER && p->trustlevel + 1 >= TRUSTLEVEL_BADPEER) log_info("peer %s now valid", - log_sockaddr((struct sockaddr *)&fsa)); + log_sockaddr((struct sockaddr *)&p->addr->ss)); p->trustlevel++; } @@ -255,7 +260,7 @@ client_dispatch(struct ntp_peer *p, u_int8_t settime) ntp_settime(p->reply[p->shift].offset); log_debug("reply from %s: offset %f delay %f, " - "next query %ds", log_sockaddr((struct sockaddr *)&fsa), + "next query %ds", log_sockaddr((struct sockaddr *)&p->addr->ss), p->reply[p->shift].offset, p->reply[p->shift].delay, interval); if (++p->shift >= OFFSET_ARRAY_SIZE) diff --git a/src/usr.sbin/ntpd/ntp_msg.c b/src/usr.sbin/ntpd/ntp_msg.c index 21013651..31312838 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.6 2004/08/30 11:50:56 deraadt Exp $ */ +/* $OpenBSD: ntp_msg.c,v 1.7 2004/09/24 14:51:16 henning Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -93,8 +93,9 @@ int ntp_sendmsg(int fd, struct sockaddr *sa, struct ntp_msg *msg, ssize_t len, int auth) { - char buf[NTP_MSGSIZE]; - char *p; + char buf[NTP_MSGSIZE]; + char *p; + u_int8_t sa_len; p = buf; memcpy(p, &msg->status, sizeof(msg->status)); @@ -136,7 +137,12 @@ ntp_sendmsg(int fd, struct sockaddr *sa, struct ntp_msg *msg, ssize_t len, /* XXX */ } - if (sendto(fd, &buf, len, 0, sa, SA_LEN(sa)) != len) { + if (sa != NULL) + sa_len = SA_LEN(sa); + else + sa_len = 0; + + if (sendto(fd, &buf, len, 0, sa, sa_len) != len) { if (errno == ENOBUFS || errno == EHOSTUNREACH || errno == ENETDOWN || errno == EHOSTDOWN) { /* logging is futile */