slave.c revision 86644
11553Srgrimes/*-
21553Srgrimes * Copyright (c) 1985, 1993
31553Srgrimes *	The Regents of the University of California.  All rights reserved.
41553Srgrimes *
51553Srgrimes * Redistribution and use in source and binary forms, with or without
61553Srgrimes * modification, are permitted provided that the following conditions
71553Srgrimes * are met:
81553Srgrimes * 1. Redistributions of source code must retain the above copyright
91553Srgrimes *    notice, this list of conditions and the following disclaimer.
101553Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111553Srgrimes *    notice, this list of conditions and the following disclaimer in the
121553Srgrimes *    documentation and/or other materials provided with the distribution.
131553Srgrimes * 3. All advertising materials mentioning features or use of this software
141553Srgrimes *    must display the following acknowledgement:
151553Srgrimes *	This product includes software developed by the University of
161553Srgrimes *	California, Berkeley and its contributors.
171553Srgrimes * 4. Neither the name of the University nor the names of its contributors
181553Srgrimes *    may be used to endorse or promote products derived from this software
191553Srgrimes *    without specific prior written permission.
201553Srgrimes *
211553Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221553Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231553Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241553Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251553Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261553Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271553Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281553Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291553Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301553Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311553Srgrimes * SUCH DAMAGE.
321553Srgrimes */
331553Srgrimes
341553Srgrimes#ifndef lint
3530642Scharnier#if 0
361553Srgrimesstatic char sccsid[] = "@(#)slave.c	8.1 (Berkeley) 6/6/93";
3730642Scharnier#endif
3830642Scharnierstatic const char rcsid[] =
3950479Speter  "$FreeBSD: head/usr.sbin/timed/timed/slave.c 86644 2001-11-20 06:36:09Z jhb $";
401553Srgrimes#endif /* not lint */
411553Srgrimes
421553Srgrimes#include "globals.h"
431553Srgrimes#include <setjmp.h>
441553Srgrimes#include "pathnames.h"
451553Srgrimes
461553Srgrimesextern jmp_buf jmpenv;
471553Srgrimesextern int Mflag;
481553Srgrimesextern int justquit;
491553Srgrimes
501553Srgrimesextern u_short sequence;
511553Srgrimes
5230872Scharnierstatic char master_name[MAXHOSTNAMELEN];
531553Srgrimesstatic struct netinfo *old_slavenet;
541553Srgrimesstatic int old_status;
551553Srgrimes
561553Srgrimesstatic void schgdate __P((struct tsp *, char *));
571553Srgrimesstatic void setmaster __P((struct tsp *));
581553Srgrimesstatic void answerdelay __P((void));
591553Srgrimes
601553Srgrimes#ifdef sgi
611553Srgrimesextern void logwtmp __P((struct timeval *, struct timeval *));
621553Srgrimes#else
631553Srgrimesextern void logwtmp __P((char *, char *, char *));
641553Srgrimes#endif /* sgi */
651553Srgrimes
661553Srgrimesint
671553Srgrimesslave()
681553Srgrimes{
691553Srgrimes	int tries;
701553Srgrimes	long electiontime, refusetime, looktime, looptime, adjtime;
711553Srgrimes	u_short seq;
721553Srgrimes	long fastelection;
731553Srgrimes#define FASTTOUT 3
741553Srgrimes	struct in_addr cadr;
751553Srgrimes	struct timeval otime;
761553Srgrimes	struct sockaddr_in taddr;
771553Srgrimes	char tname[MAXHOSTNAMELEN];
781553Srgrimes	struct tsp *msg, to;
7986644Sjhb	struct timeval ntime, wait, tmptv;
8037267Sbde	time_t tsp_time_sec;
811553Srgrimes	struct tsp *answer;
821553Srgrimes	int timeout();
831553Srgrimes	char olddate[32];
841553Srgrimes	char newdate[32];
851553Srgrimes	struct netinfo *ntp;
861553Srgrimes	struct hosttbl *htp;
871553Srgrimes
881553Srgrimes
891553Srgrimes	old_slavenet = 0;
901553Srgrimes	seq = 0;
911553Srgrimes	refusetime = 0;
921553Srgrimes	adjtime = 0;
931553Srgrimes
941553Srgrimes	(void)gettimeofday(&ntime, 0);
951553Srgrimes	electiontime = ntime.tv_sec + delay2;
961553Srgrimes	fastelection = ntime.tv_sec + FASTTOUT;
971553Srgrimes	if (justquit)
981553Srgrimes		looktime = electiontime;
991553Srgrimes	else
1001553Srgrimes		looktime = fastelection;
1011553Srgrimes	looptime = fastelection;
1021553Srgrimes
1031553Srgrimes	if (slavenet)
1041553Srgrimes		xmit(TSP_SLAVEUP, 0, &slavenet->dest_addr);
1051553Srgrimes	if (status & MASTER) {
1061553Srgrimes		for (ntp = nettab; ntp != NULL; ntp = ntp->next) {
1071553Srgrimes			if (ntp->status == MASTER)
1081553Srgrimes				masterup(ntp);
1091553Srgrimes		}
1101553Srgrimes	}
1111553Srgrimes
1121553Srgrimesloop:
1131553Srgrimes	get_goodgroup(0);
1141553Srgrimes	(void)gettimeofday(&ntime, (struct timezone *)0);
1151553Srgrimes	if (ntime.tv_sec > electiontime) {
1161553Srgrimes		if (trace)
1171553Srgrimes			fprintf(fd, "election timer expired\n");
1181553Srgrimes		longjmp(jmpenv, 1);
1191553Srgrimes	}
1201553Srgrimes
1211553Srgrimes	if (ntime.tv_sec >= looktime) {
1221553Srgrimes		if (trace)
1231553Srgrimes			fprintf(fd, "Looking for nets to master\n");
1241553Srgrimes
1251553Srgrimes		if (Mflag && nignorednets > 0) {
1261553Srgrimes			for (ntp = nettab; ntp != NULL; ntp = ntp->next) {
1271553Srgrimes				if (ntp->status == IGNORE
1281553Srgrimes				    || ntp->status == NOMASTER) {
1291553Srgrimes					lookformaster(ntp);
1301553Srgrimes					if (ntp->status == MASTER) {
1311553Srgrimes						masterup(ntp);
1321553Srgrimes					} else if (ntp->status == MASTER) {
1331553Srgrimes						ntp->status = NOMASTER;
1341553Srgrimes					}
1351553Srgrimes				}
1361553Srgrimes				if (ntp->status == MASTER
1371553Srgrimes				    && --ntp->quit_count < 0)
1381553Srgrimes					ntp->quit_count = 0;
1391553Srgrimes			}
1401553Srgrimes			makeslave(slavenet);	/* prune extras */
1411553Srgrimes			setstatus();
1421553Srgrimes		}
1431553Srgrimes		(void)gettimeofday(&ntime, 0);
1441553Srgrimes		looktime = ntime.tv_sec + delay2;
1451553Srgrimes	}
1461553Srgrimes	if (ntime.tv_sec >= looptime) {
1471553Srgrimes		if (trace)
1481553Srgrimes			fprintf(fd, "Looking for loops\n");
1491553Srgrimes		for (ntp = nettab; ntp != NULL; ntp = ntp->next) {
1501553Srgrimes		    if (ntp->status == MASTER) {
1511553Srgrimes			to.tsp_type = TSP_LOOP;
1521553Srgrimes			to.tsp_vers = TSPVERSION;
1531553Srgrimes			to.tsp_seq = sequence++;
1541553Srgrimes			to.tsp_hopcnt = MAX_HOPCNT;
15530830Scharnier			(void)strcpy(to.tsp_name, hostname);
1561553Srgrimes			bytenetorder(&to);
1571553Srgrimes			if (sendto(sock, (char *)&to, sizeof(struct tsp), 0,
1581553Srgrimes				   (struct sockaddr*)&ntp->dest_addr,
1591553Srgrimes				   sizeof(ntp->dest_addr)) < 0) {
1601553Srgrimes				trace_sendto_err(ntp->dest_addr.sin_addr);
1611553Srgrimes			}
1621553Srgrimes		    }
1631553Srgrimes		}
1641553Srgrimes		(void)gettimeofday(&ntime, 0);
1651553Srgrimes		looptime = ntime.tv_sec + delay2;
1661553Srgrimes	}
1671553Srgrimes
1681553Srgrimes	wait.tv_sec = min(electiontime,min(looktime,looptime)) - ntime.tv_sec;
1691553Srgrimes	if (wait.tv_sec < 0)
1701553Srgrimes		wait.tv_sec = 0;
1711553Srgrimes	wait.tv_sec += FASTTOUT;
1721553Srgrimes	wait.tv_usec = 0;
1731553Srgrimes	msg = readmsg(TSP_ANY, ANYADDR, &wait, 0);
1741553Srgrimes
1751553Srgrimes	if (msg != NULL) {
1761553Srgrimes		/*
1771553Srgrimes		 * filter stuff not for us
1781553Srgrimes		 */
1791553Srgrimes		switch (msg->tsp_type) {
1801553Srgrimes		case TSP_SETDATE:
1811553Srgrimes		case TSP_TRACEOFF:
1821553Srgrimes		case TSP_TRACEON:
1831553Srgrimes			/*
1841553Srgrimes			 * XXX check to see they are from ourself
1851553Srgrimes			 */
1861553Srgrimes			break;
1871553Srgrimes
1881553Srgrimes		case TSP_TEST:
1891553Srgrimes		case TSP_MSITE:
1901553Srgrimes			break;
1911553Srgrimes
1921553Srgrimes		case TSP_MASTERUP:
1931553Srgrimes			if (!fromnet) {
1941553Srgrimes				if (trace) {
1951553Srgrimes					fprintf(fd, "slave ignored: ");
1961553Srgrimes					print(msg, &from);
1971553Srgrimes				}
1981553Srgrimes				goto loop;
1991553Srgrimes			}
2001553Srgrimes			break;
2011553Srgrimes
2021553Srgrimes		default:
2031553Srgrimes			if (!fromnet
2041553Srgrimes			    || fromnet->status == IGNORE
2051553Srgrimes			    || fromnet->status == NOMASTER) {
2061553Srgrimes				if (trace) {
2071553Srgrimes					fprintf(fd, "slave ignored: ");
2081553Srgrimes					print(msg, &from);
2091553Srgrimes				}
2101553Srgrimes				goto loop;
2111553Srgrimes			}
2121553Srgrimes			break;
2131553Srgrimes		}
2141553Srgrimes
2151553Srgrimes
2161553Srgrimes		/*
2171553Srgrimes		 * now process the message
2181553Srgrimes		 */
2191553Srgrimes		switch (msg->tsp_type) {
2201553Srgrimes
2211553Srgrimes		case TSP_ADJTIME:
2221553Srgrimes			if (fromnet != slavenet)
2231553Srgrimes				break;
2241553Srgrimes			if (!good_host_name(msg->tsp_name)) {
2251553Srgrimes				syslog(LOG_NOTICE,
2261553Srgrimes				   "attempted time adjustment by %s",
2271553Srgrimes				       msg->tsp_name);
2281553Srgrimes				suppress(&from, msg->tsp_name, fromnet);
2291553Srgrimes				break;
2301553Srgrimes			}
2311553Srgrimes			/*
2321553Srgrimes			 * Speed up loop detection in case we have a loop.
2331553Srgrimes			 * Otherwise the clocks can race until the loop
2341553Srgrimes			 * is found.
2351553Srgrimes			 */
2361553Srgrimes			(void)gettimeofday(&otime, 0);
2371553Srgrimes			if (adjtime < otime.tv_sec)
2381553Srgrimes				looptime -= (looptime-otime.tv_sec)/2 + 1;
2391553Srgrimes
2401553Srgrimes			setmaster(msg);
2411553Srgrimes			if (seq != msg->tsp_seq) {
2421553Srgrimes				seq = msg->tsp_seq;
2431553Srgrimes				synch(tvtomsround(msg->tsp_time));
2441553Srgrimes			}
2451553Srgrimes			(void)gettimeofday(&ntime, 0);
2461553Srgrimes			electiontime = ntime.tv_sec + delay2;
2471553Srgrimes			fastelection = ntime.tv_sec + FASTTOUT;
2481553Srgrimes			adjtime = ntime.tv_sec + SAMPLEINTVL*2;
2491553Srgrimes			break;
2501553Srgrimes
2511553Srgrimes		case TSP_SETTIME:
2521553Srgrimes			if (fromnet != slavenet)
2531553Srgrimes				break;
2541553Srgrimes			if (seq == msg->tsp_seq)
2551553Srgrimes				break;
2561553Srgrimes			seq = msg->tsp_seq;
2571553Srgrimes
2581553Srgrimes			/* adjust time for residence on the queue */
2591553Srgrimes			(void)gettimeofday(&otime, 0);
2601553Srgrimes			adj_msg_time(msg,&otime);
2611553Srgrimes#ifdef sgi
2621553Srgrimes			(void)cftime(newdate, "%D %T", &msg->tsp_time.tv_sec);
2631553Srgrimes			(void)cftime(olddate, "%D %T", &otime.tv_sec);
2641553Srgrimes#else
2651553Srgrimes			/*
2661553Srgrimes			 * the following line is necessary due to syslog
2671553Srgrimes			 * calling ctime() which clobbers the static buffer
2681553Srgrimes			 */
26930830Scharnier			(void)strcpy(olddate, date());
27037267Sbde			tsp_time_sec = msg->tsp_time.tv_sec;
27137267Sbde			(void)strcpy(newdate, ctime(&tsp_time_sec));
2721553Srgrimes#endif /* sgi */
2731553Srgrimes
2741553Srgrimes			if (!good_host_name(msg->tsp_name)) {
2751553Srgrimes				syslog(LOG_NOTICE,
2761553Srgrimes			    "attempted time setting by untrusted %s to %s",
2771553Srgrimes				       msg->tsp_name, newdate);
2781553Srgrimes				suppress(&from, msg->tsp_name, fromnet);
2791553Srgrimes				break;
2801553Srgrimes			}
2811553Srgrimes
2821553Srgrimes			setmaster(msg);
28386644Sjhb 			tmptv.tv_sec = msg->tsp_time.tv_sec;
28486644Sjhb 			tmptv.tv_usec = msg->tsp_time.tv_usec;
28586644Sjhb			timevalsub(&ntime, &tmptv, &otime);
2861553Srgrimes			if (ntime.tv_sec < MAXADJ && ntime.tv_sec > -MAXADJ) {
2871553Srgrimes				/*
2881553Srgrimes				 * do not change the clock if we can adjust it
2891553Srgrimes				 */
2901553Srgrimes				synch(tvtomsround(ntime));
2911553Srgrimes			} else {
2921553Srgrimes#ifdef sgi
2931553Srgrimes				if (0 > settimeofday(&msg->tsp_time, 0)) {
2941553Srgrimes					syslog(LOG_ERR,"settimeofdate(): %m");
2951553Srgrimes					break;
2961553Srgrimes				}
2971553Srgrimes				logwtmp(&otime, &msg->tsp_time);
2981553Srgrimes#else
2991553Srgrimes				logwtmp("|", "date", "");
30086644Sjhb 				(void)settimeofday(&tmptv, 0);
30119093Sscrappy				logwtmp("{", "date", "");
3021553Srgrimes#endif /* sgi */
3031553Srgrimes				syslog(LOG_NOTICE,
3041553Srgrimes				       "date changed by %s from %s",
3051553Srgrimes					msg->tsp_name, olddate);
3061553Srgrimes				if (status & MASTER)
3071553Srgrimes					spreadtime();
3081553Srgrimes			}
3091553Srgrimes			(void)gettimeofday(&ntime, 0);
3101553Srgrimes			electiontime = ntime.tv_sec + delay2;
3111553Srgrimes			fastelection = ntime.tv_sec + FASTTOUT;
3121553Srgrimes
3131553Srgrimes/* This patches a bad protocol bug.  Imagine a system with several networks,
3141553Srgrimes * where there are a pair of redundant gateways between a pair of networks,
3151553Srgrimes * each running timed.  Assume that we start with a third machine mastering
3161553Srgrimes * one of the networks, and one of the gateways mastering the other.
3171553Srgrimes * Imagine that the third machine goes away and the non-master gateway
3181553Srgrimes * decides to replace it.  If things are timed just 'right,' we will have
3191553Srgrimes * each gateway mastering one network for a little while.  If a SETTIME
3201553Srgrimes * message gets into the network at that time, perhaps from the newly
3211553Srgrimes * masterful gateway as it was taking control, the SETTIME will loop
3221553Srgrimes * forever.  Each time a gateway receives it on its slave side, it will
3231553Srgrimes * call spreadtime to forward it on its mastered network.  We are now in
3241553Srgrimes * a permanent loop, since the SETTIME msgs will keep any clock
3251553Srgrimes * in the network from advancing.  Normally, the 'LOOP' stuff will detect
3261553Srgrimes * and correct the situation.  However, with the clocks stopped, the
3271553Srgrimes * 'looptime' timer cannot expire.  While they are in this state, the
3281553Srgrimes * masters will try to saturate the network with SETTIME packets.
3291553Srgrimes */
3301553Srgrimes			looptime = ntime.tv_sec + (looptime-otime.tv_sec)/2-1;
3311553Srgrimes			break;
3321553Srgrimes
3331553Srgrimes		case TSP_MASTERUP:
3341553Srgrimes			if (slavenet && fromnet != slavenet)
3351553Srgrimes				break;
3361553Srgrimes			if (!good_host_name(msg->tsp_name)) {
3371553Srgrimes				suppress(&from, msg->tsp_name, fromnet);
3381553Srgrimes				if (electiontime > fastelection)
3391553Srgrimes					electiontime = fastelection;
3401553Srgrimes				break;
3411553Srgrimes			}
3421553Srgrimes			makeslave(fromnet);
3431553Srgrimes			setmaster(msg);
3441553Srgrimes			setstatus();
3451553Srgrimes			answerdelay();
3461553Srgrimes			xmit(TSP_SLAVEUP, 0, &from);
3471553Srgrimes			(void)gettimeofday(&ntime, 0);
3481553Srgrimes			electiontime = ntime.tv_sec + delay2;
3491553Srgrimes			fastelection = ntime.tv_sec + FASTTOUT;
3501553Srgrimes			refusetime = 0;
3511553Srgrimes			break;
3521553Srgrimes
3531553Srgrimes		case TSP_MASTERREQ:
3541553Srgrimes			if (fromnet->status != SLAVE)
3551553Srgrimes				break;
3561553Srgrimes			(void)gettimeofday(&ntime, 0);
3571553Srgrimes			electiontime = ntime.tv_sec + delay2;
3581553Srgrimes			break;
3591553Srgrimes
3601553Srgrimes		case TSP_SETDATE:
3611553Srgrimes#ifdef sgi
3621553Srgrimes			(void)cftime(newdate, "%D %T", &msg->tsp_time.tv_sec);
3631553Srgrimes#else
36437267Sbde			tsp_time_sec = msg->tsp_time.tv_sec;
36537267Sbde			(void)strcpy(newdate, ctime(&tsp_time_sec));
3661553Srgrimes#endif /* sgi */
3671553Srgrimes			schgdate(msg, newdate);
3681553Srgrimes			break;
3691553Srgrimes
3701553Srgrimes		case TSP_SETDATEREQ:
3711553Srgrimes			if (fromnet->status != MASTER)
3721553Srgrimes				break;
3731553Srgrimes#ifdef sgi
3741553Srgrimes			(void)cftime(newdate, "%D %T", &msg->tsp_time.tv_sec);
3751553Srgrimes#else
37637267Sbde			tsp_time_sec = msg->tsp_time.tv_sec;
37737267Sbde			(void)strcpy(newdate, ctime(&tsp_time_sec));
3781553Srgrimes#endif /* sgi */
3791553Srgrimes			htp = findhost(msg->tsp_name);
3801553Srgrimes			if (0 == htp) {
3811553Srgrimes				syslog(LOG_WARNING,
3821553Srgrimes				       "DATEREQ from uncontrolled machine");
3831553Srgrimes				break;
3841553Srgrimes			}
3851553Srgrimes			if (!htp->good) {
3861553Srgrimes				syslog(LOG_WARNING,
3871553Srgrimes				"attempted date change by untrusted %s to %s",
3881553Srgrimes				       htp->name, newdate);
3891553Srgrimes				spreadtime();
3901553Srgrimes				break;
3911553Srgrimes			}
3921553Srgrimes			schgdate(msg, newdate);
3931553Srgrimes			break;
3941553Srgrimes
3951553Srgrimes		case TSP_TRACEON:
3961553Srgrimes			traceon();
3971553Srgrimes			break;
3981553Srgrimes
3991553Srgrimes		case TSP_TRACEOFF:
4001553Srgrimes			traceoff("Tracing ended at %s\n");
4011553Srgrimes			break;
4021553Srgrimes
4031553Srgrimes		case TSP_SLAVEUP:
4041553Srgrimes			newslave(msg);
4051553Srgrimes			break;
4061553Srgrimes
4071553Srgrimes		case TSP_ELECTION:
4081553Srgrimes			if (fromnet->status == SLAVE) {
4091553Srgrimes				(void)gettimeofday(&ntime, 0);
4101553Srgrimes				electiontime = ntime.tv_sec + delay2;
4111553Srgrimes				fastelection = ntime.tv_sec + FASTTOUT;
4121553Srgrimes				seq = 0;
4131553Srgrimes				if (!good_host_name(msg->tsp_name)) {
4141553Srgrimes					syslog(LOG_NOTICE,
4151553Srgrimes					       "suppress election of %s",
4161553Srgrimes					       msg->tsp_name);
4171553Srgrimes					to.tsp_type = TSP_QUIT;
4181553Srgrimes					electiontime = fastelection;
4191553Srgrimes				} else if (cadr.s_addr != from.sin_addr.s_addr
4201553Srgrimes					   && ntime.tv_sec < refusetime) {
4211553Srgrimes/* if the candidate has to repeat itself, the old code would refuse it
4221553Srgrimes * the second time.  That would prevent elections.
4231553Srgrimes */
4241553Srgrimes					to.tsp_type = TSP_REFUSE;
4251553Srgrimes				} else {
4261553Srgrimes					cadr.s_addr = from.sin_addr.s_addr;
4271553Srgrimes					to.tsp_type = TSP_ACCEPT;
4281553Srgrimes					refusetime = ntime.tv_sec + 30;
4291553Srgrimes				}
4301553Srgrimes				taddr = from;
43130830Scharnier				(void)strcpy(tname, msg->tsp_name);
43230830Scharnier				(void)strcpy(to.tsp_name, hostname);
4331553Srgrimes				answerdelay();
4341553Srgrimes				if (!acksend(&to, &taddr, tname,
4351553Srgrimes					     TSP_ACK, 0, 0))
4361553Srgrimes					syslog(LOG_WARNING,
4371553Srgrimes					     "no answer from candidate %s\n",
4381553Srgrimes					       tname);
4391553Srgrimes
4401553Srgrimes			} else {	/* fromnet->status == MASTER */
4411553Srgrimes				htp = addmach(msg->tsp_name, &from,fromnet);
4421553Srgrimes				to.tsp_type = TSP_QUIT;
44330830Scharnier				(void)strcpy(to.tsp_name, hostname);
4441553Srgrimes				if (!acksend(&to, &htp->addr, htp->name,
4451553Srgrimes					     TSP_ACK, 0, htp->noanswer)) {
4461553Srgrimes					syslog(LOG_ERR,
4471553Srgrimes					  "no reply from %s to ELECTION-QUIT",
4481553Srgrimes					       htp->name);
4491553Srgrimes					(void)remmach(htp);
4501553Srgrimes				}
4511553Srgrimes			}
4521553Srgrimes			break;
4531553Srgrimes
4541553Srgrimes		case TSP_CONFLICT:
4551553Srgrimes			if (fromnet->status != MASTER)
4561553Srgrimes				break;
4571553Srgrimes			/*
4581553Srgrimes			 * After a network partition, there can be
4591553Srgrimes			 * more than one master: the first slave to
4601553Srgrimes			 * come up will notify here the situation.
4611553Srgrimes			 */
46230830Scharnier			(void)strcpy(to.tsp_name, hostname);
4631553Srgrimes
4641553Srgrimes			/* The other master often gets into the same state,
4651553Srgrimes			 * with boring results.
4661553Srgrimes			 */
4671553Srgrimes			ntp = fromnet;	/* (acksend() can leave fromnet=0 */
4681553Srgrimes			for (tries = 0; tries < 3; tries++) {
4691553Srgrimes				to.tsp_type = TSP_RESOLVE;
4701553Srgrimes				answer = acksend(&to, &ntp->dest_addr,
4711553Srgrimes						 ANYADDR, TSP_MASTERACK,
4721553Srgrimes						 ntp, 0);
4731553Srgrimes				if (answer == NULL)
4741553Srgrimes					break;
4751553Srgrimes				htp = addmach(answer->tsp_name,&from,ntp);
4761553Srgrimes				to.tsp_type = TSP_QUIT;
4771553Srgrimes				answer = acksend(&to, &htp->addr, htp->name,
4781553Srgrimes						 TSP_ACK, 0, htp->noanswer);
4791553Srgrimes				if (!answer) {
4801553Srgrimes					syslog(LOG_WARNING,
4811553Srgrimes				  "conflict error: no reply from %s to QUIT",
4821553Srgrimes						htp->name);
4831553Srgrimes					(void)remmach(htp);
4841553Srgrimes				}
4851553Srgrimes			}
4861553Srgrimes			masterup(ntp);
4871553Srgrimes			break;
4881553Srgrimes
4891553Srgrimes		case TSP_MSITE:
4901553Srgrimes			if (!slavenet)
4911553Srgrimes				break;
4921553Srgrimes			taddr = from;
4931553Srgrimes			to.tsp_type = TSP_MSITEREQ;
4941553Srgrimes			to.tsp_vers = TSPVERSION;
4951553Srgrimes			to.tsp_seq = 0;
49630830Scharnier			(void)strcpy(to.tsp_name, hostname);
4971553Srgrimes			answer = acksend(&to, &slavenet->dest_addr,
4981553Srgrimes					 ANYADDR, TSP_ACK,
4991553Srgrimes					 slavenet, 0);
5001553Srgrimes			if (answer != NULL
5011553Srgrimes			    && good_host_name(answer->tsp_name)) {
5021553Srgrimes				setmaster(answer);
5031553Srgrimes				to.tsp_type = TSP_ACK;
50430830Scharnier				(void)strcpy(to.tsp_name, answer->tsp_name);
5051553Srgrimes				bytenetorder(&to);
5061553Srgrimes				if (sendto(sock, (char *)&to,
5071553Srgrimes					   sizeof(struct tsp), 0,
50830642Scharnier					   (struct sockaddr*)&taddr,
50930830Scharnier					   sizeof(taddr)) < 0) {
5101553Srgrimes					trace_sendto_err(taddr.sin_addr);
5111553Srgrimes				}
5121553Srgrimes			}
5131553Srgrimes			break;
5141553Srgrimes
5151553Srgrimes		case TSP_MSITEREQ:
5161553Srgrimes			break;
5171553Srgrimes
5181553Srgrimes		case TSP_ACCEPT:
5191553Srgrimes		case TSP_REFUSE:
5201553Srgrimes		case TSP_RESOLVE:
5211553Srgrimes			break;
5221553Srgrimes
5231553Srgrimes		case TSP_QUIT:
5241553Srgrimes			doquit(msg);		/* become a slave */
5251553Srgrimes			break;
5261553Srgrimes
5271553Srgrimes		case TSP_TEST:
5281553Srgrimes			electiontime = 0;
5291553Srgrimes			break;
5301553Srgrimes
5311553Srgrimes		case TSP_LOOP:
5321553Srgrimes			/* looking for loops of masters */
5331553Srgrimes			if (!(status & MASTER))
5341553Srgrimes				break;
5351553Srgrimes			if (fromnet->status == SLAVE) {
5361553Srgrimes			    if (!strcmp(msg->tsp_name, hostname)) {
5371553Srgrimes				/*
5381553Srgrimes				 * Someone forwarded our message back to
5391553Srgrimes				 * us.  There must be a loop.  Tell the
5401553Srgrimes				 * master of this network to quit.
5411553Srgrimes				 *
5421553Srgrimes				 * The other master often gets into
5431553Srgrimes				 * the same state, with boring results.
5441553Srgrimes				 */
5451553Srgrimes				ntp = fromnet;
5461553Srgrimes				for (tries = 0; tries < 3; tries++) {
5471553Srgrimes				    to.tsp_type = TSP_RESOLVE;
5481553Srgrimes				    answer = acksend(&to, &ntp->dest_addr,
5491553Srgrimes						     ANYADDR, TSP_MASTERACK,
5501553Srgrimes						     ntp,0);
5511553Srgrimes				    if (answer == NULL)
5521553Srgrimes					break;
5531553Srgrimes				    taddr = from;
55430830Scharnier				    (void)strcpy(tname, answer->tsp_name);
5551553Srgrimes				    to.tsp_type = TSP_QUIT;
55630830Scharnier				    (void)strcpy(to.tsp_name, hostname);
5571553Srgrimes				    if (!acksend(&to, &taddr, tname,
5581553Srgrimes						 TSP_ACK, 0, 1)) {
5591553Srgrimes					syslog(LOG_ERR,
5601553Srgrimes					"no reply from %s to slave LOOP-QUIT",
5611553Srgrimes						 tname);
5621553Srgrimes				    } else {
5631553Srgrimes					electiontime = 0;
5641553Srgrimes				    }
5651553Srgrimes				}
5661553Srgrimes				(void)gettimeofday(&ntime, 0);
5671553Srgrimes				looptime = ntime.tv_sec + FASTTOUT;
5681553Srgrimes			    } else {
5691553Srgrimes				if (msg->tsp_hopcnt-- < 1)
5701553Srgrimes				    break;
5711553Srgrimes				bytenetorder(msg);
5721553Srgrimes				for (ntp = nettab; ntp != 0; ntp = ntp->next) {
5731553Srgrimes				    if (ntp->status == MASTER
5741553Srgrimes					&& 0 > sendto(sock, (char *)msg,
5751553Srgrimes						      sizeof(struct tsp), 0,
5761553Srgrimes					      (struct sockaddr*)&ntp->dest_addr,
5771553Srgrimes						      sizeof(ntp->dest_addr)))
5781553Srgrimes				    trace_sendto_err(ntp->dest_addr.sin_addr);
5791553Srgrimes				}
5801553Srgrimes			    }
5811553Srgrimes			} else {	/* fromnet->status == MASTER */
5821553Srgrimes			    /*
5831553Srgrimes			     * We should not have received this from a net
5841553Srgrimes			     * we are master on.  There must be two masters,
5851553Srgrimes			     * unless the packet was really from us.
5861553Srgrimes			     */
5871553Srgrimes			    if (from.sin_addr.s_addr
5881553Srgrimes				== fromnet->my_addr.s_addr) {
5891553Srgrimes				if (trace)
5901553Srgrimes				    fprintf(fd,"discarding forwarded LOOP\n");
5911553Srgrimes				break;
5921553Srgrimes			    }
5931553Srgrimes
5941553Srgrimes			    /*
5951553Srgrimes			     * The other master often gets into the same
5961553Srgrimes			     * state, with boring results.
5971553Srgrimes			     */
5981553Srgrimes			    ntp = fromnet;
5991553Srgrimes			    for (tries = 0; tries < 3; tries++) {
6001553Srgrimes				to.tsp_type = TSP_RESOLVE;
6011553Srgrimes				answer = acksend(&to, &ntp->dest_addr,
6021553Srgrimes						 ANYADDR, TSP_MASTERACK,
6031553Srgrimes						ntp,0);
6041553Srgrimes				if (!answer)
6051553Srgrimes					break;
6061553Srgrimes				htp = addmach(answer->tsp_name,
6071553Srgrimes					      &from,ntp);
6081553Srgrimes				to.tsp_type = TSP_QUIT;
60930830Scharnier				(void)strcpy(to.tsp_name, hostname);
6101553Srgrimes				if (!acksend(&to,&htp->addr,htp->name,
6111553Srgrimes					     TSP_ACK, 0, htp->noanswer)) {
6121553Srgrimes					syslog(LOG_ERR,
6131553Srgrimes				    "no reply from %s to master LOOP-QUIT",
6141553Srgrimes					       htp->name);
6151553Srgrimes					(void)remmach(htp);
6161553Srgrimes				}
6171553Srgrimes			    }
6181553Srgrimes			    (void)gettimeofday(&ntime, 0);
6191553Srgrimes			    looptime = ntime.tv_sec + FASTTOUT;
6201553Srgrimes			}
6211553Srgrimes			break;
6221553Srgrimes		default:
6231553Srgrimes			if (trace) {
6241553Srgrimes				fprintf(fd, "garbage message: ");
6251553Srgrimes				print(msg, &from);
6261553Srgrimes			}
6271553Srgrimes			break;
6281553Srgrimes		}
6291553Srgrimes	}
6301553Srgrimes	goto loop;
6311553Srgrimes}
6321553Srgrimes
6331553Srgrimes
6341553Srgrimes/*
6351553Srgrimes * tell the world who our master is
6361553Srgrimes */
6371553Srgrimesstatic void
6381553Srgrimessetmaster(msg)
6391553Srgrimes	struct tsp *msg;
6401553Srgrimes{
6411553Srgrimes	if (slavenet
6421553Srgrimes	    && (slavenet != old_slavenet
6431553Srgrimes		|| strcmp(msg->tsp_name, master_name)
6441553Srgrimes		|| old_status != status)) {
64530830Scharnier		(void)strcpy(master_name, msg->tsp_name);
6461553Srgrimes		old_slavenet = slavenet;
6471553Srgrimes		old_status = status;
6481553Srgrimes
6491553Srgrimes		if (status & MASTER) {
6501553Srgrimes			syslog(LOG_NOTICE, "submaster to %s", master_name);
6511553Srgrimes			if (trace)
6521553Srgrimes				fprintf(fd, "submaster to %s\n", master_name);
6531553Srgrimes
6541553Srgrimes		} else {
6551553Srgrimes			syslog(LOG_NOTICE, "slave to %s", master_name);
6561553Srgrimes			if (trace)
6571553Srgrimes				fprintf(fd, "slave to %s\n", master_name);
6581553Srgrimes		}
6591553Srgrimes	}
6601553Srgrimes}
6611553Srgrimes
6621553Srgrimes
6631553Srgrimes
6641553Srgrimes/*
6651553Srgrimes * handle date change request on a slave
6661553Srgrimes */
6671553Srgrimesstatic void
6681553Srgrimesschgdate(msg, newdate)
6691553Srgrimes	struct tsp *msg;
6701553Srgrimes	char *newdate;
6711553Srgrimes{
6721553Srgrimes	struct tsp to;
6731553Srgrimes	u_short seq;
6741553Srgrimes	struct sockaddr_in taddr;
6751553Srgrimes	struct timeval otime;
6761553Srgrimes
6771553Srgrimes	if (!slavenet)
6781553Srgrimes		return;			/* no where to forward */
6791553Srgrimes
6801553Srgrimes	taddr = from;
6811553Srgrimes	seq = msg->tsp_seq;
6821553Srgrimes
6831553Srgrimes	syslog(LOG_INFO,
6841553Srgrimes	       "forwarding date change by %s to %s",
6851553Srgrimes	       msg->tsp_name, newdate);
6861553Srgrimes
6871553Srgrimes	/* adjust time for residence on the queue */
6881553Srgrimes	(void)gettimeofday(&otime, 0);
6891553Srgrimes	adj_msg_time(msg, &otime);
6901553Srgrimes
6911553Srgrimes	to.tsp_type = TSP_SETDATEREQ;
6921553Srgrimes	to.tsp_time = msg->tsp_time;
69330830Scharnier	(void)strcpy(to.tsp_name, hostname);
6941553Srgrimes	if (!acksend(&to, &slavenet->dest_addr,
6951553Srgrimes		     ANYADDR, TSP_DATEACK,
6961553Srgrimes		     slavenet, 0))
6971553Srgrimes		return;			/* no answer */
6981553Srgrimes
6991553Srgrimes	xmit(TSP_DATEACK, seq, &taddr);
7001553Srgrimes}
7011553Srgrimes
7021553Srgrimes
7031553Srgrimes/*
7041553Srgrimes * Used before answering a broadcast message to avoid network
7051553Srgrimes * contention and likely collisions.
7061553Srgrimes */
7071553Srgrimesstatic void
7081553Srgrimesanswerdelay()
7091553Srgrimes{
7101553Srgrimes#ifdef sgi
7111553Srgrimes	sginap(delay1);
7121553Srgrimes#else
7131553Srgrimes	struct timeval timeout;
7141553Srgrimes
7151553Srgrimes	timeout.tv_sec = 0;
7161553Srgrimes	timeout.tv_usec = delay1;
7171553Srgrimes
7181553Srgrimes	(void)select(0, (fd_set *)NULL, (fd_set *)NULL, (fd_set *)NULL,
7191553Srgrimes	    &timeout);
7201553Srgrimes	return;
7211553Srgrimes#endif /* sgi */
7221553Srgrimes}
723