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