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