netdate.c revision 26362
1/*-
2 * Copyright (c) 1990, 1993
3 *	The Regents of the University of California.  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 *	$Id: netdate.c,v 1.7 1997/03/10 19:49:03 guido Exp $
34 */
35
36#ifndef lint
37static char const sccsid[] = "@(#)netdate.c	8.1 (Berkeley) 5/31/93";
38#endif /* not lint */
39
40#include <sys/param.h>
41#include <sys/time.h>
42#include <sys/socket.h>
43
44#include <netinet/in.h>
45#include <netdb.h>
46#define TSPTYPES
47#include <protocols/timed.h>
48
49#include <err.h>
50#include <errno.h>
51#include <stdio.h>
52#include <string.h>
53#include <unistd.h>
54
55#include "extern.h"
56
57#define	WAITACK		2	/* seconds */
58#define	WAITDATEACK	5	/* seconds */
59
60extern int retval;
61
62/*
63 * Set the date in the machines controlled by timedaemons by communicating the
64 * new date to the local timedaemon.  If the timedaemon is in the master state,
65 * it performs the correction on all slaves.  If it is in the slave state, it
66 * notifies the master that a correction is needed.
67 * Returns 0 on success.  Returns > 0 on failure, setting retval to 2;
68 */
69int
70netsettime(tval)
71	time_t tval;
72{
73	struct timeval tout;
74	struct servent *sp;
75	struct tsp msg;
76	struct sockaddr_in sin, dest, from;
77	fd_set ready;
78	long waittime;
79	int s, length, port, timed_ack, found, err;
80	char hostname[MAXHOSTNAMELEN];
81
82	if ((sp = getservbyname("timed", "udp")) == NULL) {
83		warnx("udp/timed: unknown service");
84		return (retval = 2);
85	}
86
87	dest.sin_port = sp->s_port;
88	dest.sin_family = AF_INET;
89	dest.sin_addr.s_addr = htonl((u_long)INADDR_ANY);
90	s = socket(AF_INET, SOCK_DGRAM, 0);
91	if (s < 0) {
92		if (errno != EPROTONOSUPPORT)
93			warn("timed");
94		return (retval = 2);
95	}
96
97	memset(&sin, 0, sizeof(sin));
98	sin.sin_family = AF_INET;
99	for (port = IPPORT_RESERVED - 1; port > IPPORT_RESERVED / 2; port--) {
100		sin.sin_port = htons((u_short)port);
101		if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
102			break;
103		if (errno == EADDRINUSE)
104			continue;
105		if (errno != EADDRNOTAVAIL)
106			warn("bind");
107		goto bad;
108	}
109	if (port == IPPORT_RESERVED / 2) {
110		warnx("all ports in use");
111		goto bad;
112	}
113	msg.tsp_type = TSP_SETDATE;
114	msg.tsp_vers = TSPVERSION;
115	if (gethostname(hostname, sizeof(hostname))) {
116		warn("gethostname");
117		goto bad;
118	}
119	(void)strncpy(msg.tsp_name, hostname, sizeof(msg.tsp_name) - 1);
120	msg.tsp_name[sizeof(msg.tsp_name) - 1] = '\0';
121	msg.tsp_seq = htons((u_short)0);
122	msg.tsp_time.tv_sec = htonl((u_long)tval);
123	msg.tsp_time.tv_usec = htonl((u_long)0);
124	length = sizeof(struct sockaddr_in);
125	if (connect(s, (struct sockaddr *)&dest, length) < 0) {
126		warn("connect");
127		goto bad;
128	}
129	if (send(s, (char *)&msg, sizeof(struct tsp), 0) < 0) {
130		if (errno != ECONNREFUSED)
131			warn("send");
132		goto bad;
133	}
134
135	timed_ack = -1;
136	waittime = WAITACK;
137loop:
138	tout.tv_sec = waittime;
139	tout.tv_usec = 0;
140
141	FD_ZERO(&ready);
142	FD_SET(s, &ready);
143	found = select(FD_SETSIZE, &ready, (fd_set *)0, (fd_set *)0, &tout);
144
145	length = sizeof(err);
146	if (!getsockopt(s,
147	    SOL_SOCKET, SO_ERROR, (char *)&err, &length) && err) {
148		if (err != ECONNREFUSED)
149			warn("send (delayed error)");
150		goto bad;
151	}
152
153	if (found > 0 && FD_ISSET(s, &ready)) {
154		length = sizeof(struct sockaddr_in);
155		if (recvfrom(s, &msg, sizeof(struct tsp), 0,
156		    (struct sockaddr *)&from, &length) < 0) {
157			if (errno != ECONNREFUSED)
158				warn("recvfrom");
159			goto bad;
160		}
161		msg.tsp_seq = ntohs(msg.tsp_seq);
162		msg.tsp_time.tv_sec = ntohl(msg.tsp_time.tv_sec);
163		msg.tsp_time.tv_usec = ntohl(msg.tsp_time.tv_usec);
164		switch (msg.tsp_type) {
165		case TSP_ACK:
166			timed_ack = TSP_ACK;
167			waittime = WAITDATEACK;
168			goto loop;
169		case TSP_DATEACK:
170			(void)close(s);
171			return (0);
172		default:
173			warnx("wrong ack received from timed: %s",
174			    tsptype[msg.tsp_type]);
175			timed_ack = -1;
176			break;
177		}
178	}
179	if (timed_ack == -1)
180		warnx("can't reach time daemon, time set locally");
181
182bad:
183	(void)close(s);
184	return (retval = 2);
185}
186