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