slave.c revision 37267
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[] =
3937267Sbde	"$Id: slave.c,v 1.5 1997/10/31 12:33:06 charnier Exp $";
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;
791553Srgrimes	struct timeval ntime, wait;
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);
2831553Srgrimes			timevalsub(&ntime, &msg->tsp_time, &otime);
2841553Srgrimes			if (ntime.tv_sec < MAXADJ && ntime.tv_sec > -MAXADJ) {
2851553Srgrimes				/*
2861553Srgrimes				 * do not change the clock if we can adjust it
2871553Srgrimes				 */
2881553Srgrimes				synch(tvtomsround(ntime));
2891553Srgrimes			} else {
2901553Srgrimes#ifdef sgi
2911553Srgrimes				if (0 > settimeofday(&msg->tsp_time, 0)) {
2921553Srgrimes					syslog(LOG_ERR,"settimeofdate(): %m");
2931553Srgrimes					break;
2941553Srgrimes				}
2951553Srgrimes				logwtmp(&otime, &msg->tsp_time);
2961553Srgrimes#else
2971553Srgrimes				logwtmp("|", "date", "");
2981553Srgrimes				(void)settimeofday(&msg->tsp_time, 0);
29919093Sscrappy				logwtmp("{", "date", "");
3001553Srgrimes#endif /* sgi */
3011553Srgrimes				syslog(LOG_NOTICE,
3021553Srgrimes				       "date changed by %s from %s",
3031553Srgrimes					msg->tsp_name, olddate);
3041553Srgrimes				if (status & MASTER)
3051553Srgrimes					spreadtime();
3061553Srgrimes			}
3071553Srgrimes			(void)gettimeofday(&ntime, 0);
3081553Srgrimes			electiontime = ntime.tv_sec + delay2;
3091553Srgrimes			fastelection = ntime.tv_sec + FASTTOUT;
3101553Srgrimes
3111553Srgrimes/* This patches a bad protocol bug.  Imagine a system with several networks,
3121553Srgrimes * where there are a pair of redundant gateways between a pair of networks,
3131553Srgrimes * each running timed.  Assume that we start with a third machine mastering
3141553Srgrimes * one of the networks, and one of the gateways mastering the other.
3151553Srgrimes * Imagine that the third machine goes away and the non-master gateway
3161553Srgrimes * decides to replace it.  If things are timed just 'right,' we will have
3171553Srgrimes * each gateway mastering one network for a little while.  If a SETTIME
3181553Srgrimes * message gets into the network at that time, perhaps from the newly
3191553Srgrimes * masterful gateway as it was taking control, the SETTIME will loop
3201553Srgrimes * forever.  Each time a gateway receives it on its slave side, it will
3211553Srgrimes * call spreadtime to forward it on its mastered network.  We are now in
3221553Srgrimes * a permanent loop, since the SETTIME msgs will keep any clock
3231553Srgrimes * in the network from advancing.  Normally, the 'LOOP' stuff will detect
3241553Srgrimes * and correct the situation.  However, with the clocks stopped, the
3251553Srgrimes * 'looptime' timer cannot expire.  While they are in this state, the
3261553Srgrimes * masters will try to saturate the network with SETTIME packets.
3271553Srgrimes */
3281553Srgrimes			looptime = ntime.tv_sec + (looptime-otime.tv_sec)/2-1;
3291553Srgrimes			break;
3301553Srgrimes
3311553Srgrimes		case TSP_MASTERUP:
3321553Srgrimes			if (slavenet && fromnet != slavenet)
3331553Srgrimes				break;
3341553Srgrimes			if (!good_host_name(msg->tsp_name)) {
3351553Srgrimes				suppress(&from, msg->tsp_name, fromnet);
3361553Srgrimes				if (electiontime > fastelection)
3371553Srgrimes					electiontime = fastelection;
3381553Srgrimes				break;
3391553Srgrimes			}
3401553Srgrimes			makeslave(fromnet);
3411553Srgrimes			setmaster(msg);
3421553Srgrimes			setstatus();
3431553Srgrimes			answerdelay();
3441553Srgrimes			xmit(TSP_SLAVEUP, 0, &from);
3451553Srgrimes			(void)gettimeofday(&ntime, 0);
3461553Srgrimes			electiontime = ntime.tv_sec + delay2;
3471553Srgrimes			fastelection = ntime.tv_sec + FASTTOUT;
3481553Srgrimes			refusetime = 0;
3491553Srgrimes			break;
3501553Srgrimes
3511553Srgrimes		case TSP_MASTERREQ:
3521553Srgrimes			if (fromnet->status != SLAVE)
3531553Srgrimes				break;
3541553Srgrimes			(void)gettimeofday(&ntime, 0);
3551553Srgrimes			electiontime = ntime.tv_sec + delay2;
3561553Srgrimes			break;
3571553Srgrimes
3581553Srgrimes		case TSP_SETDATE:
3591553Srgrimes#ifdef sgi
3601553Srgrimes			(void)cftime(newdate, "%D %T", &msg->tsp_time.tv_sec);
3611553Srgrimes#else
36237267Sbde			tsp_time_sec = msg->tsp_time.tv_sec;
36337267Sbde			(void)strcpy(newdate, ctime(&tsp_time_sec));
3641553Srgrimes#endif /* sgi */
3651553Srgrimes			schgdate(msg, newdate);
3661553Srgrimes			break;
3671553Srgrimes
3681553Srgrimes		case TSP_SETDATEREQ:
3691553Srgrimes			if (fromnet->status != MASTER)
3701553Srgrimes				break;
3711553Srgrimes#ifdef sgi
3721553Srgrimes			(void)cftime(newdate, "%D %T", &msg->tsp_time.tv_sec);
3731553Srgrimes#else
37437267Sbde			tsp_time_sec = msg->tsp_time.tv_sec;
37537267Sbde			(void)strcpy(newdate, ctime(&tsp_time_sec));
3761553Srgrimes#endif /* sgi */
3771553Srgrimes			htp = findhost(msg->tsp_name);
3781553Srgrimes			if (0 == htp) {
3791553Srgrimes				syslog(LOG_WARNING,
3801553Srgrimes				       "DATEREQ from uncontrolled machine");
3811553Srgrimes				break;
3821553Srgrimes			}
3831553Srgrimes			if (!htp->good) {
3841553Srgrimes				syslog(LOG_WARNING,
3851553Srgrimes				"attempted date change by untrusted %s to %s",
3861553Srgrimes				       htp->name, newdate);
3871553Srgrimes				spreadtime();
3881553Srgrimes				break;
3891553Srgrimes			}
3901553Srgrimes			schgdate(msg, newdate);
3911553Srgrimes			break;
3921553Srgrimes
3931553Srgrimes		case TSP_TRACEON:
3941553Srgrimes			traceon();
3951553Srgrimes			break;
3961553Srgrimes
3971553Srgrimes		case TSP_TRACEOFF:
3981553Srgrimes			traceoff("Tracing ended at %s\n");
3991553Srgrimes			break;
4001553Srgrimes
4011553Srgrimes		case TSP_SLAVEUP:
4021553Srgrimes			newslave(msg);
4031553Srgrimes			break;
4041553Srgrimes
4051553Srgrimes		case TSP_ELECTION:
4061553Srgrimes			if (fromnet->status == SLAVE) {
4071553Srgrimes				(void)gettimeofday(&ntime, 0);
4081553Srgrimes				electiontime = ntime.tv_sec + delay2;
4091553Srgrimes				fastelection = ntime.tv_sec + FASTTOUT;
4101553Srgrimes				seq = 0;
4111553Srgrimes				if (!good_host_name(msg->tsp_name)) {
4121553Srgrimes					syslog(LOG_NOTICE,
4131553Srgrimes					       "suppress election of %s",
4141553Srgrimes					       msg->tsp_name);
4151553Srgrimes					to.tsp_type = TSP_QUIT;
4161553Srgrimes					electiontime = fastelection;
4171553Srgrimes				} else if (cadr.s_addr != from.sin_addr.s_addr
4181553Srgrimes					   && ntime.tv_sec < refusetime) {
4191553Srgrimes/* if the candidate has to repeat itself, the old code would refuse it
4201553Srgrimes * the second time.  That would prevent elections.
4211553Srgrimes */
4221553Srgrimes					to.tsp_type = TSP_REFUSE;
4231553Srgrimes				} else {
4241553Srgrimes					cadr.s_addr = from.sin_addr.s_addr;
4251553Srgrimes					to.tsp_type = TSP_ACCEPT;
4261553Srgrimes					refusetime = ntime.tv_sec + 30;
4271553Srgrimes				}
4281553Srgrimes				taddr = from;
42930830Scharnier				(void)strcpy(tname, msg->tsp_name);
43030830Scharnier				(void)strcpy(to.tsp_name, hostname);
4311553Srgrimes				answerdelay();
4321553Srgrimes				if (!acksend(&to, &taddr, tname,
4331553Srgrimes					     TSP_ACK, 0, 0))
4341553Srgrimes					syslog(LOG_WARNING,
4351553Srgrimes					     "no answer from candidate %s\n",
4361553Srgrimes					       tname);
4371553Srgrimes
4381553Srgrimes			} else {	/* fromnet->status == MASTER */
4391553Srgrimes				htp = addmach(msg->tsp_name, &from,fromnet);
4401553Srgrimes				to.tsp_type = TSP_QUIT;
44130830Scharnier				(void)strcpy(to.tsp_name, hostname);
4421553Srgrimes				if (!acksend(&to, &htp->addr, htp->name,
4431553Srgrimes					     TSP_ACK, 0, htp->noanswer)) {
4441553Srgrimes					syslog(LOG_ERR,
4451553Srgrimes					  "no reply from %s to ELECTION-QUIT",
4461553Srgrimes					       htp->name);
4471553Srgrimes					(void)remmach(htp);
4481553Srgrimes				}
4491553Srgrimes			}
4501553Srgrimes			break;
4511553Srgrimes
4521553Srgrimes		case TSP_CONFLICT:
4531553Srgrimes			if (fromnet->status != MASTER)
4541553Srgrimes				break;
4551553Srgrimes			/*
4561553Srgrimes			 * After a network partition, there can be
4571553Srgrimes			 * more than one master: the first slave to
4581553Srgrimes			 * come up will notify here the situation.
4591553Srgrimes			 */
46030830Scharnier			(void)strcpy(to.tsp_name, hostname);
4611553Srgrimes
4621553Srgrimes			/* The other master often gets into the same state,
4631553Srgrimes			 * with boring results.
4641553Srgrimes			 */
4651553Srgrimes			ntp = fromnet;	/* (acksend() can leave fromnet=0 */
4661553Srgrimes			for (tries = 0; tries < 3; tries++) {
4671553Srgrimes				to.tsp_type = TSP_RESOLVE;
4681553Srgrimes				answer = acksend(&to, &ntp->dest_addr,
4691553Srgrimes						 ANYADDR, TSP_MASTERACK,
4701553Srgrimes						 ntp, 0);
4711553Srgrimes				if (answer == NULL)
4721553Srgrimes					break;
4731553Srgrimes				htp = addmach(answer->tsp_name,&from,ntp);
4741553Srgrimes				to.tsp_type = TSP_QUIT;
4751553Srgrimes				answer = acksend(&to, &htp->addr, htp->name,
4761553Srgrimes						 TSP_ACK, 0, htp->noanswer);
4771553Srgrimes				if (!answer) {
4781553Srgrimes					syslog(LOG_WARNING,
4791553Srgrimes				  "conflict error: no reply from %s to QUIT",
4801553Srgrimes						htp->name);
4811553Srgrimes					(void)remmach(htp);
4821553Srgrimes				}
4831553Srgrimes			}
4841553Srgrimes			masterup(ntp);
4851553Srgrimes			break;
4861553Srgrimes
4871553Srgrimes		case TSP_MSITE:
4881553Srgrimes			if (!slavenet)
4891553Srgrimes				break;
4901553Srgrimes			taddr = from;
4911553Srgrimes			to.tsp_type = TSP_MSITEREQ;
4921553Srgrimes			to.tsp_vers = TSPVERSION;
4931553Srgrimes			to.tsp_seq = 0;
49430830Scharnier			(void)strcpy(to.tsp_name, hostname);
4951553Srgrimes			answer = acksend(&to, &slavenet->dest_addr,
4961553Srgrimes					 ANYADDR, TSP_ACK,
4971553Srgrimes					 slavenet, 0);
4981553Srgrimes			if (answer != NULL
4991553Srgrimes			    && good_host_name(answer->tsp_name)) {
5001553Srgrimes				setmaster(answer);
5011553Srgrimes				to.tsp_type = TSP_ACK;
50230830Scharnier				(void)strcpy(to.tsp_name, answer->tsp_name);
5031553Srgrimes				bytenetorder(&to);
5041553Srgrimes				if (sendto(sock, (char *)&to,
5051553Srgrimes					   sizeof(struct tsp), 0,
50630642Scharnier					   (struct sockaddr*)&taddr,
50730830Scharnier					   sizeof(taddr)) < 0) {
5081553Srgrimes					trace_sendto_err(taddr.sin_addr);
5091553Srgrimes				}
5101553Srgrimes			}
5111553Srgrimes			break;
5121553Srgrimes
5131553Srgrimes		case TSP_MSITEREQ:
5141553Srgrimes			break;
5151553Srgrimes
5161553Srgrimes		case TSP_ACCEPT:
5171553Srgrimes		case TSP_REFUSE:
5181553Srgrimes		case TSP_RESOLVE:
5191553Srgrimes			break;
5201553Srgrimes
5211553Srgrimes		case TSP_QUIT:
5221553Srgrimes			doquit(msg);		/* become a slave */
5231553Srgrimes			break;
5241553Srgrimes
5251553Srgrimes		case TSP_TEST:
5261553Srgrimes			electiontime = 0;
5271553Srgrimes			break;
5281553Srgrimes
5291553Srgrimes		case TSP_LOOP:
5301553Srgrimes			/* looking for loops of masters */
5311553Srgrimes			if (!(status & MASTER))
5321553Srgrimes				break;
5331553Srgrimes			if (fromnet->status == SLAVE) {
5341553Srgrimes			    if (!strcmp(msg->tsp_name, hostname)) {
5351553Srgrimes				/*
5361553Srgrimes				 * Someone forwarded our message back to
5371553Srgrimes				 * us.  There must be a loop.  Tell the
5381553Srgrimes				 * master of this network to quit.
5391553Srgrimes				 *
5401553Srgrimes				 * The other master often gets into
5411553Srgrimes				 * the same state, with boring results.
5421553Srgrimes				 */
5431553Srgrimes				ntp = fromnet;
5441553Srgrimes				for (tries = 0; tries < 3; tries++) {
5451553Srgrimes				    to.tsp_type = TSP_RESOLVE;
5461553Srgrimes				    answer = acksend(&to, &ntp->dest_addr,
5471553Srgrimes						     ANYADDR, TSP_MASTERACK,
5481553Srgrimes						     ntp,0);
5491553Srgrimes				    if (answer == NULL)
5501553Srgrimes					break;
5511553Srgrimes				    taddr = from;
55230830Scharnier				    (void)strcpy(tname, answer->tsp_name);
5531553Srgrimes				    to.tsp_type = TSP_QUIT;
55430830Scharnier				    (void)strcpy(to.tsp_name, hostname);
5551553Srgrimes				    if (!acksend(&to, &taddr, tname,
5561553Srgrimes						 TSP_ACK, 0, 1)) {
5571553Srgrimes					syslog(LOG_ERR,
5581553Srgrimes					"no reply from %s to slave LOOP-QUIT",
5591553Srgrimes						 tname);
5601553Srgrimes				    } else {
5611553Srgrimes					electiontime = 0;
5621553Srgrimes				    }
5631553Srgrimes				}
5641553Srgrimes				(void)gettimeofday(&ntime, 0);
5651553Srgrimes				looptime = ntime.tv_sec + FASTTOUT;
5661553Srgrimes			    } else {
5671553Srgrimes				if (msg->tsp_hopcnt-- < 1)
5681553Srgrimes				    break;
5691553Srgrimes				bytenetorder(msg);
5701553Srgrimes				for (ntp = nettab; ntp != 0; ntp = ntp->next) {
5711553Srgrimes				    if (ntp->status == MASTER
5721553Srgrimes					&& 0 > sendto(sock, (char *)msg,
5731553Srgrimes						      sizeof(struct tsp), 0,
5741553Srgrimes					      (struct sockaddr*)&ntp->dest_addr,
5751553Srgrimes						      sizeof(ntp->dest_addr)))
5761553Srgrimes				    trace_sendto_err(ntp->dest_addr.sin_addr);
5771553Srgrimes				}
5781553Srgrimes			    }
5791553Srgrimes			} else {	/* fromnet->status == MASTER */
5801553Srgrimes			    /*
5811553Srgrimes			     * We should not have received this from a net
5821553Srgrimes			     * we are master on.  There must be two masters,
5831553Srgrimes			     * unless the packet was really from us.
5841553Srgrimes			     */
5851553Srgrimes			    if (from.sin_addr.s_addr
5861553Srgrimes				== fromnet->my_addr.s_addr) {
5871553Srgrimes				if (trace)
5881553Srgrimes				    fprintf(fd,"discarding forwarded LOOP\n");
5891553Srgrimes				break;
5901553Srgrimes			    }
5911553Srgrimes
5921553Srgrimes			    /*
5931553Srgrimes			     * The other master often gets into the same
5941553Srgrimes			     * state, with boring results.
5951553Srgrimes			     */
5961553Srgrimes			    ntp = fromnet;
5971553Srgrimes			    for (tries = 0; tries < 3; tries++) {
5981553Srgrimes				to.tsp_type = TSP_RESOLVE;
5991553Srgrimes				answer = acksend(&to, &ntp->dest_addr,
6001553Srgrimes						 ANYADDR, TSP_MASTERACK,
6011553Srgrimes						ntp,0);
6021553Srgrimes				if (!answer)
6031553Srgrimes					break;
6041553Srgrimes				htp = addmach(answer->tsp_name,
6051553Srgrimes					      &from,ntp);
6061553Srgrimes				to.tsp_type = TSP_QUIT;
60730830Scharnier				(void)strcpy(to.tsp_name, hostname);
6081553Srgrimes				if (!acksend(&to,&htp->addr,htp->name,
6091553Srgrimes					     TSP_ACK, 0, htp->noanswer)) {
6101553Srgrimes					syslog(LOG_ERR,
6111553Srgrimes				    "no reply from %s to master LOOP-QUIT",
6121553Srgrimes					       htp->name);
6131553Srgrimes					(void)remmach(htp);
6141553Srgrimes				}
6151553Srgrimes			    }
6161553Srgrimes			    (void)gettimeofday(&ntime, 0);
6171553Srgrimes			    looptime = ntime.tv_sec + FASTTOUT;
6181553Srgrimes			}
6191553Srgrimes			break;
6201553Srgrimes		default:
6211553Srgrimes			if (trace) {
6221553Srgrimes				fprintf(fd, "garbage message: ");
6231553Srgrimes				print(msg, &from);
6241553Srgrimes			}
6251553Srgrimes			break;
6261553Srgrimes		}
6271553Srgrimes	}
6281553Srgrimes	goto loop;
6291553Srgrimes}
6301553Srgrimes
6311553Srgrimes
6321553Srgrimes/*
6331553Srgrimes * tell the world who our master is
6341553Srgrimes */
6351553Srgrimesstatic void
6361553Srgrimessetmaster(msg)
6371553Srgrimes	struct tsp *msg;
6381553Srgrimes{
6391553Srgrimes	if (slavenet
6401553Srgrimes	    && (slavenet != old_slavenet
6411553Srgrimes		|| strcmp(msg->tsp_name, master_name)
6421553Srgrimes		|| old_status != status)) {
64330830Scharnier		(void)strcpy(master_name, msg->tsp_name);
6441553Srgrimes		old_slavenet = slavenet;
6451553Srgrimes		old_status = status;
6461553Srgrimes
6471553Srgrimes		if (status & MASTER) {
6481553Srgrimes			syslog(LOG_NOTICE, "submaster to %s", master_name);
6491553Srgrimes			if (trace)
6501553Srgrimes				fprintf(fd, "submaster to %s\n", master_name);
6511553Srgrimes
6521553Srgrimes		} else {
6531553Srgrimes			syslog(LOG_NOTICE, "slave to %s", master_name);
6541553Srgrimes			if (trace)
6551553Srgrimes				fprintf(fd, "slave to %s\n", master_name);
6561553Srgrimes		}
6571553Srgrimes	}
6581553Srgrimes}
6591553Srgrimes
6601553Srgrimes
6611553Srgrimes
6621553Srgrimes/*
6631553Srgrimes * handle date change request on a slave
6641553Srgrimes */
6651553Srgrimesstatic void
6661553Srgrimesschgdate(msg, newdate)
6671553Srgrimes	struct tsp *msg;
6681553Srgrimes	char *newdate;
6691553Srgrimes{
6701553Srgrimes	struct tsp to;
6711553Srgrimes	u_short seq;
6721553Srgrimes	struct sockaddr_in taddr;
6731553Srgrimes	struct timeval otime;
6741553Srgrimes
6751553Srgrimes	if (!slavenet)
6761553Srgrimes		return;			/* no where to forward */
6771553Srgrimes
6781553Srgrimes	taddr = from;
6791553Srgrimes	seq = msg->tsp_seq;
6801553Srgrimes
6811553Srgrimes	syslog(LOG_INFO,
6821553Srgrimes	       "forwarding date change by %s to %s",
6831553Srgrimes	       msg->tsp_name, newdate);
6841553Srgrimes
6851553Srgrimes	/* adjust time for residence on the queue */
6861553Srgrimes	(void)gettimeofday(&otime, 0);
6871553Srgrimes	adj_msg_time(msg, &otime);
6881553Srgrimes
6891553Srgrimes	to.tsp_type = TSP_SETDATEREQ;
6901553Srgrimes	to.tsp_time = msg->tsp_time;
69130830Scharnier	(void)strcpy(to.tsp_name, hostname);
6921553Srgrimes	if (!acksend(&to, &slavenet->dest_addr,
6931553Srgrimes		     ANYADDR, TSP_DATEACK,
6941553Srgrimes		     slavenet, 0))
6951553Srgrimes		return;			/* no answer */
6961553Srgrimes
6971553Srgrimes	xmit(TSP_DATEACK, seq, &taddr);
6981553Srgrimes}
6991553Srgrimes
7001553Srgrimes
7011553Srgrimes/*
7021553Srgrimes * Used before answering a broadcast message to avoid network
7031553Srgrimes * contention and likely collisions.
7041553Srgrimes */
7051553Srgrimesstatic void
7061553Srgrimesanswerdelay()
7071553Srgrimes{
7081553Srgrimes#ifdef sgi
7091553Srgrimes	sginap(delay1);
7101553Srgrimes#else
7111553Srgrimes	struct timeval timeout;
7121553Srgrimes
7131553Srgrimes	timeout.tv_sec = 0;
7141553Srgrimes	timeout.tv_usec = delay1;
7151553Srgrimes
7161553Srgrimes	(void)select(0, (fd_set *)NULL, (fd_set *)NULL, (fd_set *)NULL,
7171553Srgrimes	    &timeout);
7181553Srgrimes	return;
7191553Srgrimes#endif /* sgi */
7201553Srgrimes}
721