Home | History | Annotate | Line # | Download | only in date
netdate.c revision 1.24
      1  1.24       agc /* $NetBSD: netdate.c,v 1.24 2003/08/07 09:05:09 agc 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.24       agc __RCSID("$NetBSD: netdate.c,v 1.24 2003/08/07 09:05:09 agc 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.1       cgd #define	WAITACK		2	/* seconds */
     60   1.1       cgd #define	WAITDATEACK	5	/* seconds */
     61   1.1       cgd 
     62   1.1       cgd extern int retval;
     63   1.1       cgd 
     64   1.1       cgd /*
     65   1.1       cgd  * Set the date in the machines controlled by timedaemons by communicating the
     66   1.1       cgd  * new date to the local timedaemon.  If the timedaemon is in the master state,
     67   1.1       cgd  * it performs the correction on all slaves.  If it is in the slave state, it
     68   1.1       cgd  * notifies the master that a correction is needed.
     69   1.1       cgd  * Returns 0 on success.  Returns > 0 on failure, setting retval to 2;
     70   1.1       cgd  */
     71   1.7   mycroft int
     72  1.20       wiz netsettime(time_t tval)
     73   1.1       cgd {
     74  1.21     lukem 	struct sockaddr_in dest, from, nsin;
     75  1.20       wiz 	struct tsp msg;
     76  1.20       wiz 	char hostname[MAXHOSTNAMELEN];
     77   1.1       cgd 	struct servent *sp;
     78  1.22   mycroft 	struct pollfd ready[1];
     79   1.1       cgd 	long waittime;
     80  1.20       wiz 	int error, found, s, timed_ack;
     81  1.19  gmcgarry 	socklen_t length;
     82  1.14   mycroft #ifdef IP_PORTRANGE
     83  1.14   mycroft 	int on;
     84  1.14   mycroft #endif
     85   1.1       cgd 
     86   1.1       cgd 	if ((sp = getservbyname("timed", "udp")) == NULL) {
     87   1.7   mycroft 		warnx("udp/timed: unknown service");
     88   1.1       cgd 		return (retval = 2);
     89   1.1       cgd 	}
     90   1.1       cgd 
     91  1.12       cgd 	(void)memset(&dest, 0, sizeof(dest));
     92  1.17  christos #ifdef BSD4_4
     93   1.9   mycroft 	dest.sin_len = sizeof(struct sockaddr_in);
     94  1.17  christos #endif
     95   1.9   mycroft 	dest.sin_family = AF_INET;
     96   1.1       cgd 	dest.sin_port = sp->s_port;
     97   1.9   mycroft 	dest.sin_addr.s_addr = htonl(INADDR_ANY);
     98   1.1       cgd 	s = socket(AF_INET, SOCK_DGRAM, 0);
     99   1.1       cgd 	if (s < 0) {
    100   1.1       cgd 		if (errno != EPROTONOSUPPORT)
    101   1.7   mycroft 			warn("timed");
    102   1.7   mycroft 		return (retval = 2);
    103   1.1       cgd 	}
    104   1.1       cgd 
    105  1.14   mycroft #ifdef IP_PORTRANGE
    106  1.13     lukem 	on = IP_PORTRANGE_LOW;
    107  1.13     lukem 	if (setsockopt(s, IPPROTO_IP, IP_PORTRANGE, &on, sizeof(on)) < 0) {
    108  1.13     lukem 		warn("setsockopt");
    109  1.13     lukem 		goto bad;
    110  1.13     lukem 	}
    111  1.14   mycroft #endif
    112  1.13     lukem 
    113  1.21     lukem 	(void)memset(&nsin, 0, sizeof(nsin));
    114  1.17  christos #ifdef BSD4_4
    115  1.21     lukem 	nsin.sin_len = sizeof(struct sockaddr_in);
    116  1.17  christos #endif
    117  1.21     lukem 	nsin.sin_family = AF_INET;
    118  1.21     lukem 	if (bind(s, (struct sockaddr *)&nsin, sizeof(nsin)) < 0) {
    119  1.13     lukem 		warn("bind");
    120   1.1       cgd 		goto bad;
    121   1.1       cgd 	}
    122  1.13     lukem 
    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.15       mrg 	hostname[sizeof(hostname) - 1] = '\0';
    130  1.23    itojun 	(void)strlcpy(msg.tsp_name, hostname, sizeof(msg.tsp_name));
    131   1.1       cgd 	msg.tsp_seq = htons((u_short)0);
    132   1.1       cgd 	msg.tsp_time.tv_sec = htonl((u_long)tval);
    133   1.1       cgd 	msg.tsp_time.tv_usec = htonl((u_long)0);
    134   1.1       cgd 	length = sizeof(struct sockaddr_in);
    135   1.1       cgd 	if (connect(s, (struct sockaddr *)&dest, length) < 0) {
    136   1.7   mycroft 		warn("connect");
    137   1.1       cgd 		goto bad;
    138   1.1       cgd 	}
    139   1.1       cgd 	if (send(s, (char *)&msg, sizeof(struct tsp), 0) < 0) {
    140   1.1       cgd 		if (errno != ECONNREFUSED)
    141   1.7   mycroft 			warn("send");
    142   1.1       cgd 		goto bad;
    143   1.1       cgd 	}
    144   1.1       cgd 
    145   1.1       cgd 	timed_ack = -1;
    146   1.1       cgd 	waittime = WAITACK;
    147  1.22   mycroft 	ready[0].fd = s;
    148  1.22   mycroft 	ready[0].events = POLLIN;
    149   1.1       cgd loop:
    150  1.22   mycroft 	found = poll(ready, 1, waittime * 1000);
    151   1.1       cgd 
    152  1.16   mycroft 	length = sizeof(error);
    153   1.7   mycroft 	if (!getsockopt(s,
    154  1.16   mycroft 	    SOL_SOCKET, SO_ERROR, (char *)&error, &length) && error) {
    155  1.16   mycroft 		if (error != ECONNREFUSED)
    156   1.7   mycroft 			warn("send (delayed error)");
    157   1.1       cgd 		goto bad;
    158   1.1       cgd 	}
    159   1.1       cgd 
    160  1.22   mycroft 	if (found > 0 && ready[0].revents & POLLIN) {
    161   1.1       cgd 		length = sizeof(struct sockaddr_in);
    162   1.1       cgd 		if (recvfrom(s, &msg, sizeof(struct tsp), 0,
    163   1.1       cgd 		    (struct sockaddr *)&from, &length) < 0) {
    164   1.1       cgd 			if (errno != ECONNREFUSED)
    165   1.7   mycroft 				warn("recvfrom");
    166   1.1       cgd 			goto bad;
    167   1.1       cgd 		}
    168   1.1       cgd 		msg.tsp_seq = ntohs(msg.tsp_seq);
    169   1.1       cgd 		msg.tsp_time.tv_sec = ntohl(msg.tsp_time.tv_sec);
    170   1.1       cgd 		msg.tsp_time.tv_usec = ntohl(msg.tsp_time.tv_usec);
    171   1.1       cgd 		switch (msg.tsp_type) {
    172   1.1       cgd 		case TSP_ACK:
    173   1.1       cgd 			timed_ack = TSP_ACK;
    174   1.1       cgd 			waittime = WAITDATEACK;
    175   1.1       cgd 			goto loop;
    176   1.1       cgd 		case TSP_DATEACK:
    177   1.1       cgd 			(void)close(s);
    178   1.1       cgd 			return (0);
    179   1.1       cgd 		default:
    180   1.7   mycroft 			warnx("wrong ack received from timed: %s",
    181   1.1       cgd 			    tsptype[msg.tsp_type]);
    182   1.1       cgd 			timed_ack = -1;
    183   1.1       cgd 			break;
    184   1.1       cgd 		}
    185   1.1       cgd 	}
    186   1.1       cgd 	if (timed_ack == -1)
    187   1.7   mycroft 		warnx("can't reach time daemon, time set locally");
    188   1.1       cgd 
    189   1.1       cgd bad:
    190   1.1       cgd 	(void)close(s);
    191   1.7   mycroft 	return (retval = 2);
    192   1.1       cgd }
    193