1 /* $NetBSD: netdate.c,v 1.10 1995/09/07 06:21:06 jtc Exp $ */ 2 3 /*- 4 * Copyright (c) 1990, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #ifndef lint 37 #if 0 38 static char sccsid[] = "@(#)netdate.c 8.2 (Berkeley) 4/28/95"; 39 #else 40 static char rcsid[] = "$NetBSD: netdate.c,v 1.10 1995/09/07 06:21:06 jtc Exp $"; 41 #endif 42 #endif /* not lint */ 43 44 #include <sys/param.h> 45 #include <sys/time.h> 46 #include <sys/socket.h> 47 48 #include <netinet/in.h> 49 #include <netdb.h> 50 #define TSPTYPES 51 #include <protocols/timed.h> 52 53 #include <err.h> 54 #include <errno.h> 55 #include <stdio.h> 56 #include <string.h> 57 #include <unistd.h> 58 59 #include "extern.h" 60 61 #define WAITACK 2 /* seconds */ 62 #define WAITDATEACK 5 /* seconds */ 63 64 extern int retval; 65 66 /* 67 * Set the date in the machines controlled by timedaemons by communicating the 68 * new date to the local timedaemon. If the timedaemon is in the master state, 69 * it performs the correction on all slaves. If it is in the slave state, it 70 * notifies the master that a correction is needed. 71 * Returns 0 on success. Returns > 0 on failure, setting retval to 2; 72 */ 73 int 74 netsettime(tval) 75 time_t tval; 76 { 77 struct timeval tout; 78 struct servent *sp; 79 struct tsp msg; 80 struct sockaddr_in sin, dest, from; 81 fd_set ready; 82 long waittime; 83 int s, length, port, timed_ack, found, err; 84 char hostname[MAXHOSTNAMELEN]; 85 86 if ((sp = getservbyname("timed", "udp")) == NULL) { 87 warnx("udp/timed: unknown service"); 88 return (retval = 2); 89 } 90 91 memset(&dest, 0, sizeof(dest)); 92 dest.sin_len = sizeof(struct sockaddr_in); 93 dest.sin_family = AF_INET; 94 dest.sin_port = sp->s_port; 95 dest.sin_addr.s_addr = htonl(INADDR_ANY); 96 s = socket(AF_INET, SOCK_DGRAM, 0); 97 if (s < 0) { 98 if (errno != EPROTONOSUPPORT) 99 warn("timed"); 100 return (retval = 2); 101 } 102 103 memset(&sin, 0, sizeof(sin)); 104 sin.sin_len = sizeof(struct sockaddr_in); 105 sin.sin_family = AF_INET; 106 for (port = IPPORT_RESERVED - 1; port > IPPORT_RESERVED / 2; port--) { 107 sin.sin_port = htons((u_short)port); 108 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0) 109 break; 110 if (errno == EADDRINUSE) 111 continue; 112 if (errno != EADDRNOTAVAIL) 113 warn("bind"); 114 goto bad; 115 } 116 if (port == IPPORT_RESERVED / 2) { 117 warnx("all ports in use"); 118 goto bad; 119 } 120 msg.tsp_type = TSP_SETDATE; 121 msg.tsp_vers = TSPVERSION; 122 if (gethostname(hostname, sizeof(hostname))) { 123 warn("gethostname"); 124 goto bad; 125 } 126 (void)strncpy(msg.tsp_name, hostname, sizeof(hostname)); 127 msg.tsp_seq = htons((u_short)0); 128 msg.tsp_time.tv_sec = htonl((u_long)tval); 129 msg.tsp_time.tv_usec = htonl((u_long)0); 130 length = sizeof(struct sockaddr_in); 131 if (connect(s, (struct sockaddr *)&dest, length) < 0) { 132 warn("connect"); 133 goto bad; 134 } 135 if (send(s, (char *)&msg, sizeof(struct tsp), 0) < 0) { 136 if (errno != ECONNREFUSED) 137 warn("send"); 138 goto bad; 139 } 140 141 timed_ack = -1; 142 waittime = WAITACK; 143 loop: 144 tout.tv_sec = waittime; 145 tout.tv_usec = 0; 146 147 FD_ZERO(&ready); 148 FD_SET(s, &ready); 149 found = select(FD_SETSIZE, &ready, (fd_set *)0, (fd_set *)0, &tout); 150 151 length = sizeof(err); 152 if (!getsockopt(s, 153 SOL_SOCKET, SO_ERROR, (char *)&err, &length) && err) { 154 if (err != ECONNREFUSED) 155 warn("send (delayed error)"); 156 goto bad; 157 } 158 159 if (found > 0 && FD_ISSET(s, &ready)) { 160 length = sizeof(struct sockaddr_in); 161 if (recvfrom(s, &msg, sizeof(struct tsp), 0, 162 (struct sockaddr *)&from, &length) < 0) { 163 if (errno != ECONNREFUSED) 164 warn("recvfrom"); 165 goto bad; 166 } 167 msg.tsp_seq = ntohs(msg.tsp_seq); 168 msg.tsp_time.tv_sec = ntohl(msg.tsp_time.tv_sec); 169 msg.tsp_time.tv_usec = ntohl(msg.tsp_time.tv_usec); 170 switch (msg.tsp_type) { 171 case TSP_ACK: 172 timed_ack = TSP_ACK; 173 waittime = WAITDATEACK; 174 goto loop; 175 case TSP_DATEACK: 176 (void)close(s); 177 return (0); 178 default: 179 warnx("wrong ack received from timed: %s", 180 tsptype[msg.tsp_type]); 181 timed_ack = -1; 182 break; 183 } 184 } 185 if (timed_ack == -1) 186 warnx("can't reach time daemon, time set locally"); 187 188 bad: 189 (void)close(s); 190 return (retval = 2); 191 } 192