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