Home | History | Annotate | Line # | Download | only in date
netdate.c revision 1.23
      1 /* $NetBSD: netdate.c,v 1.23 2003/07/12 13:23:55 itojun 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 #include <sys/cdefs.h>
     37 #ifndef lint
     38 #if 0
     39 static char sccsid[] = "@(#)netdate.c	8.2 (Berkeley) 4/28/95";
     40 #else
     41 __RCSID("$NetBSD: netdate.c,v 1.23 2003/07/12 13:23:55 itojun Exp $");
     42 #endif
     43 #endif /* not lint */
     44 
     45 #include <sys/param.h>
     46 #include <sys/time.h>
     47 #include <sys/socket.h>
     48 
     49 #include <netinet/in.h>
     50 #include <netdb.h>
     51 #define TSPTYPES
     52 #include <protocols/timed.h>
     53 
     54 #include <err.h>
     55 #include <errno.h>
     56 #include <poll.h>
     57 #include <stdio.h>
     58 #include <string.h>
     59 #include <unistd.h>
     60 
     61 #include "extern.h"
     62 
     63 #define	WAITACK		2	/* seconds */
     64 #define	WAITDATEACK	5	/* seconds */
     65 
     66 extern int retval;
     67 
     68 /*
     69  * Set the date in the machines controlled by timedaemons by communicating the
     70  * new date to the local timedaemon.  If the timedaemon is in the master state,
     71  * it performs the correction on all slaves.  If it is in the slave state, it
     72  * notifies the master that a correction is needed.
     73  * Returns 0 on success.  Returns > 0 on failure, setting retval to 2;
     74  */
     75 int
     76 netsettime(time_t tval)
     77 {
     78 	struct sockaddr_in dest, from, nsin;
     79 	struct tsp msg;
     80 	char hostname[MAXHOSTNAMELEN];
     81 	struct servent *sp;
     82 	struct pollfd ready[1];
     83 	long waittime;
     84 	int error, found, s, timed_ack;
     85 	socklen_t length;
     86 #ifdef IP_PORTRANGE
     87 	int on;
     88 #endif
     89 
     90 	if ((sp = getservbyname("timed", "udp")) == NULL) {
     91 		warnx("udp/timed: unknown service");
     92 		return (retval = 2);
     93 	}
     94 
     95 	(void)memset(&dest, 0, sizeof(dest));
     96 #ifdef BSD4_4
     97 	dest.sin_len = sizeof(struct sockaddr_in);
     98 #endif
     99 	dest.sin_family = AF_INET;
    100 	dest.sin_port = sp->s_port;
    101 	dest.sin_addr.s_addr = htonl(INADDR_ANY);
    102 	s = socket(AF_INET, SOCK_DGRAM, 0);
    103 	if (s < 0) {
    104 		if (errno != EPROTONOSUPPORT)
    105 			warn("timed");
    106 		return (retval = 2);
    107 	}
    108 
    109 #ifdef IP_PORTRANGE
    110 	on = IP_PORTRANGE_LOW;
    111 	if (setsockopt(s, IPPROTO_IP, IP_PORTRANGE, &on, sizeof(on)) < 0) {
    112 		warn("setsockopt");
    113 		goto bad;
    114 	}
    115 #endif
    116 
    117 	(void)memset(&nsin, 0, sizeof(nsin));
    118 #ifdef BSD4_4
    119 	nsin.sin_len = sizeof(struct sockaddr_in);
    120 #endif
    121 	nsin.sin_family = AF_INET;
    122 	if (bind(s, (struct sockaddr *)&nsin, sizeof(nsin)) < 0) {
    123 		warn("bind");
    124 		goto bad;
    125 	}
    126 
    127 	msg.tsp_type = TSP_SETDATE;
    128 	msg.tsp_vers = TSPVERSION;
    129 	if (gethostname(hostname, sizeof(hostname))) {
    130 		warn("gethostname");
    131 		goto bad;
    132 	}
    133 	hostname[sizeof(hostname) - 1] = '\0';
    134 	(void)strlcpy(msg.tsp_name, hostname, sizeof(msg.tsp_name));
    135 	msg.tsp_seq = htons((u_short)0);
    136 	msg.tsp_time.tv_sec = htonl((u_long)tval);
    137 	msg.tsp_time.tv_usec = htonl((u_long)0);
    138 	length = sizeof(struct sockaddr_in);
    139 	if (connect(s, (struct sockaddr *)&dest, length) < 0) {
    140 		warn("connect");
    141 		goto bad;
    142 	}
    143 	if (send(s, (char *)&msg, sizeof(struct tsp), 0) < 0) {
    144 		if (errno != ECONNREFUSED)
    145 			warn("send");
    146 		goto bad;
    147 	}
    148 
    149 	timed_ack = -1;
    150 	waittime = WAITACK;
    151 	ready[0].fd = s;
    152 	ready[0].events = POLLIN;
    153 loop:
    154 	found = poll(ready, 1, waittime * 1000);
    155 
    156 	length = sizeof(error);
    157 	if (!getsockopt(s,
    158 	    SOL_SOCKET, SO_ERROR, (char *)&error, &length) && error) {
    159 		if (error != ECONNREFUSED)
    160 			warn("send (delayed error)");
    161 		goto bad;
    162 	}
    163 
    164 	if (found > 0 && ready[0].revents & POLLIN) {
    165 		length = sizeof(struct sockaddr_in);
    166 		if (recvfrom(s, &msg, sizeof(struct tsp), 0,
    167 		    (struct sockaddr *)&from, &length) < 0) {
    168 			if (errno != ECONNREFUSED)
    169 				warn("recvfrom");
    170 			goto bad;
    171 		}
    172 		msg.tsp_seq = ntohs(msg.tsp_seq);
    173 		msg.tsp_time.tv_sec = ntohl(msg.tsp_time.tv_sec);
    174 		msg.tsp_time.tv_usec = ntohl(msg.tsp_time.tv_usec);
    175 		switch (msg.tsp_type) {
    176 		case TSP_ACK:
    177 			timed_ack = TSP_ACK;
    178 			waittime = WAITDATEACK;
    179 			goto loop;
    180 		case TSP_DATEACK:
    181 			(void)close(s);
    182 			return (0);
    183 		default:
    184 			warnx("wrong ack received from timed: %s",
    185 			    tsptype[msg.tsp_type]);
    186 			timed_ack = -1;
    187 			break;
    188 		}
    189 	}
    190 	if (timed_ack == -1)
    191 		warnx("can't reach time daemon, time set locally");
    192 
    193 bad:
    194 	(void)close(s);
    195 	return (retval = 2);
    196 }
    197