slave.c revision 1553
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
351553Srgrimesstatic char sccsid[] = "@(#)slave.c	8.1 (Berkeley) 6/6/93";
361553Srgrimes#endif /* not lint */
371553Srgrimes
381553Srgrimes#ifdef sgi
391553Srgrimes#ident "$Revision: 1.20 $"
401553Srgrimes#endif
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
521553Srgrimesstatic char master_name[MAXHOSTNAMELEN+1];
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;
801553Srgrimes	struct tsp *answer;
811553Srgrimes	int timeout();
821553Srgrimes	char olddate[32];
831553Srgrimes	char newdate[32];
841553Srgrimes	struct netinfo *ntp;
851553Srgrimes	struct hosttbl *htp;
861553Srgrimes
871553Srgrimes
881553Srgrimes	old_slavenet = 0;
891553Srgrimes	seq = 0;
901553Srgrimes	refusetime = 0;
911553Srgrimes	adjtime = 0;
921553Srgrimes
931553Srgrimes	(void)gettimeofday(&ntime, 0);
941553Srgrimes	electiontime = ntime.tv_sec + delay2;
951553Srgrimes	fastelection = ntime.tv_sec + FASTTOUT;
961553Srgrimes	if (justquit)
971553Srgrimes		looktime = electiontime;
981553Srgrimes	else
991553Srgrimes		looktime = fastelection;
1001553Srgrimes	looptime = fastelection;
1011553Srgrimes
1021553Srgrimes	if (slavenet)
1031553Srgrimes		xmit(TSP_SLAVEUP, 0, &slavenet->dest_addr);
1041553Srgrimes	if (status & MASTER) {
1051553Srgrimes		for (ntp = nettab; ntp != NULL; ntp = ntp->next) {
1061553Srgrimes			if (ntp->status == MASTER)
1071553Srgrimes				masterup(ntp);
1081553Srgrimes		}
1091553Srgrimes	}
1101553Srgrimes
1111553Srgrimesloop:
1121553Srgrimes	get_goodgroup(0);
1131553Srgrimes	(void)gettimeofday(&ntime, (struct timezone *)0);
1141553Srgrimes	if (ntime.tv_sec > electiontime) {
1151553Srgrimes		if (trace)
1161553Srgrimes			fprintf(fd, "election timer expired\n");
1171553Srgrimes		longjmp(jmpenv, 1);
1181553Srgrimes	}
1191553Srgrimes
1201553Srgrimes	if (ntime.tv_sec >= looktime) {
1211553Srgrimes		if (trace)
1221553Srgrimes			fprintf(fd, "Looking for nets to master\n");
1231553Srgrimes
1241553Srgrimes		if (Mflag && nignorednets > 0) {
1251553Srgrimes			for (ntp = nettab; ntp != NULL; ntp = ntp->next) {
1261553Srgrimes				if (ntp->status == IGNORE
1271553Srgrimes				    || ntp->status == NOMASTER) {
1281553Srgrimes					lookformaster(ntp);
1291553Srgrimes					if (ntp->status == MASTER) {
1301553Srgrimes						masterup(ntp);
1311553Srgrimes					} else if (ntp->status == MASTER) {
1321553Srgrimes						ntp->status = NOMASTER;
1331553Srgrimes					}
1341553Srgrimes				}
1351553Srgrimes				if (ntp->status == MASTER
1361553Srgrimes				    && --ntp->quit_count < 0)
1371553Srgrimes					ntp->quit_count = 0;
1381553Srgrimes			}
1391553Srgrimes			makeslave(slavenet);	/* prune extras */
1401553Srgrimes			setstatus();
1411553Srgrimes		}
1421553Srgrimes		(void)gettimeofday(&ntime, 0);
1431553Srgrimes		looktime = ntime.tv_sec + delay2;
1441553Srgrimes	}
1451553Srgrimes	if (ntime.tv_sec >= looptime) {
1461553Srgrimes		if (trace)
1471553Srgrimes			fprintf(fd, "Looking for loops\n");
1481553Srgrimes		for (ntp = nettab; ntp != NULL; ntp = ntp->next) {
1491553Srgrimes		    if (ntp->status == MASTER) {
1501553Srgrimes			to.tsp_type = TSP_LOOP;
1511553Srgrimes			to.tsp_vers = TSPVERSION;
1521553Srgrimes			to.tsp_seq = sequence++;
1531553Srgrimes			to.tsp_hopcnt = MAX_HOPCNT;
1541553Srgrimes			(void)strcpy(to.tsp_name, hostname);
1551553Srgrimes			bytenetorder(&to);
1561553Srgrimes			if (sendto(sock, (char *)&to, sizeof(struct tsp), 0,
1571553Srgrimes				   (struct sockaddr*)&ntp->dest_addr,
1581553Srgrimes				   sizeof(ntp->dest_addr)) < 0) {
1591553Srgrimes				trace_sendto_err(ntp->dest_addr.sin_addr);
1601553Srgrimes			}
1611553Srgrimes		    }
1621553Srgrimes		}
1631553Srgrimes		(void)gettimeofday(&ntime, 0);
1641553Srgrimes		looptime = ntime.tv_sec + delay2;
1651553Srgrimes	}
1661553Srgrimes
1671553Srgrimes	wait.tv_sec = min(electiontime,min(looktime,looptime)) - ntime.tv_sec;
1681553Srgrimes	if (wait.tv_sec < 0)
1691553Srgrimes		wait.tv_sec = 0;
1701553Srgrimes	wait.tv_sec += FASTTOUT;
1711553Srgrimes	wait.tv_usec = 0;
1721553Srgrimes	msg = readmsg(TSP_ANY, ANYADDR, &wait, 0);
1731553Srgrimes
1741553Srgrimes	if (msg != NULL) {
1751553Srgrimes		/*
1761553Srgrimes		 * filter stuff not for us
1771553Srgrimes		 */
1781553Srgrimes		switch (msg->tsp_type) {
1791553Srgrimes		case TSP_SETDATE:
1801553Srgrimes		case TSP_TRACEOFF:
1811553Srgrimes		case TSP_TRACEON:
1821553Srgrimes			/*
1831553Srgrimes			 * XXX check to see they are from ourself
1841553Srgrimes			 */
1851553Srgrimes			break;
1861553Srgrimes
1871553Srgrimes		case TSP_TEST:
1881553Srgrimes		case TSP_MSITE:
1891553Srgrimes			break;
1901553Srgrimes
1911553Srgrimes		case TSP_MASTERUP:
1921553Srgrimes			if (!fromnet) {
1931553Srgrimes				if (trace) {
1941553Srgrimes					fprintf(fd, "slave ignored: ");
1951553Srgrimes					print(msg, &from);
1961553Srgrimes				}
1971553Srgrimes				goto loop;
1981553Srgrimes			}
1991553Srgrimes			break;
2001553Srgrimes
2011553Srgrimes		default:
2021553Srgrimes			if (!fromnet
2031553Srgrimes			    || fromnet->status == IGNORE
2041553Srgrimes			    || fromnet->status == NOMASTER) {
2051553Srgrimes				if (trace) {
2061553Srgrimes					fprintf(fd, "slave ignored: ");
2071553Srgrimes					print(msg, &from);
2081553Srgrimes				}
2091553Srgrimes				goto loop;
2101553Srgrimes			}
2111553Srgrimes			break;
2121553Srgrimes		}
2131553Srgrimes
2141553Srgrimes
2151553Srgrimes		/*
2161553Srgrimes		 * now process the message
2171553Srgrimes		 */
2181553Srgrimes		switch (msg->tsp_type) {
2191553Srgrimes
2201553Srgrimes		case TSP_ADJTIME:
2211553Srgrimes			if (fromnet != slavenet)
2221553Srgrimes				break;
2231553Srgrimes			if (!good_host_name(msg->tsp_name)) {
2241553Srgrimes				syslog(LOG_NOTICE,
2251553Srgrimes				   "attempted time adjustment by %s",
2261553Srgrimes				       msg->tsp_name);
2271553Srgrimes				suppress(&from, msg->tsp_name, fromnet);
2281553Srgrimes				break;
2291553Srgrimes			}
2301553Srgrimes			/*
2311553Srgrimes			 * Speed up loop detection in case we have a loop.
2321553Srgrimes			 * Otherwise the clocks can race until the loop
2331553Srgrimes			 * is found.
2341553Srgrimes			 */
2351553Srgrimes			(void)gettimeofday(&otime, 0);
2361553Srgrimes			if (adjtime < otime.tv_sec)
2371553Srgrimes				looptime -= (looptime-otime.tv_sec)/2 + 1;
2381553Srgrimes
2391553Srgrimes			setmaster(msg);
2401553Srgrimes			if (seq != msg->tsp_seq) {
2411553Srgrimes				seq = msg->tsp_seq;
2421553Srgrimes				synch(tvtomsround(msg->tsp_time));
2431553Srgrimes			}
2441553Srgrimes			(void)gettimeofday(&ntime, 0);
2451553Srgrimes			electiontime = ntime.tv_sec + delay2;
2461553Srgrimes			fastelection = ntime.tv_sec + FASTTOUT;
2471553Srgrimes			adjtime = ntime.tv_sec + SAMPLEINTVL*2;
2481553Srgrimes			break;
2491553Srgrimes
2501553Srgrimes		case TSP_SETTIME:
2511553Srgrimes			if (fromnet != slavenet)
2521553Srgrimes				break;
2531553Srgrimes			if (seq == msg->tsp_seq)
2541553Srgrimes				break;
2551553Srgrimes			seq = msg->tsp_seq;
2561553Srgrimes
2571553Srgrimes			/* adjust time for residence on the queue */
2581553Srgrimes			(void)gettimeofday(&otime, 0);
2591553Srgrimes			adj_msg_time(msg,&otime);
2601553Srgrimes#ifdef sgi
2611553Srgrimes			(void)cftime(newdate, "%D %T", &msg->tsp_time.tv_sec);
2621553Srgrimes			(void)cftime(olddate, "%D %T", &otime.tv_sec);
2631553Srgrimes#else
2641553Srgrimes			/*
2651553Srgrimes			 * the following line is necessary due to syslog
2661553Srgrimes			 * calling ctime() which clobbers the static buffer
2671553Srgrimes			 */
2681553Srgrimes			(void)strcpy(olddate, date());
2691553Srgrimes			(void)strcpy(newdate, ctime(&msg->tsp_time.tv_sec));
2701553Srgrimes#endif /* sgi */
2711553Srgrimes
2721553Srgrimes			if (!good_host_name(msg->tsp_name)) {
2731553Srgrimes				syslog(LOG_NOTICE,
2741553Srgrimes			    "attempted time setting by untrusted %s to %s",
2751553Srgrimes				       msg->tsp_name, newdate);
2761553Srgrimes				suppress(&from, msg->tsp_name, fromnet);
2771553Srgrimes				break;
2781553Srgrimes			}
2791553Srgrimes
2801553Srgrimes			setmaster(msg);
2811553Srgrimes			timevalsub(&ntime, &msg->tsp_time, &otime);
2821553Srgrimes			if (ntime.tv_sec < MAXADJ && ntime.tv_sec > -MAXADJ) {
2831553Srgrimes				/*
2841553Srgrimes				 * do not change the clock if we can adjust it
2851553Srgrimes				 */
2861553Srgrimes				synch(tvtomsround(ntime));
2871553Srgrimes			} else {
2881553Srgrimes#ifdef sgi
2891553Srgrimes				if (0 > settimeofday(&msg->tsp_time, 0)) {
2901553Srgrimes					syslog(LOG_ERR,"settimeofdate(): %m");
2911553Srgrimes					break;
2921553Srgrimes				}
2931553Srgrimes				logwtmp(&otime, &msg->tsp_time);
2941553Srgrimes#else
2951553Srgrimes				logwtmp("|", "date", "");
2961553Srgrimes				(void)settimeofday(&msg->tsp_time, 0);
2971553Srgrimes				logwtmp("}", "date", "");
2981553Srgrimes#endif /* sgi */
2991553Srgrimes				syslog(LOG_NOTICE,
3001553Srgrimes				       "date changed by %s from %s",
3011553Srgrimes					msg->tsp_name, olddate);
3021553Srgrimes				if (status & MASTER)
3031553Srgrimes					spreadtime();
3041553Srgrimes			}
3051553Srgrimes			(void)gettimeofday(&ntime, 0);
3061553Srgrimes			electiontime = ntime.tv_sec + delay2;
3071553Srgrimes			fastelection = ntime.tv_sec + FASTTOUT;
3081553Srgrimes
3091553Srgrimes/* This patches a bad protocol bug.  Imagine a system with several networks,
3101553Srgrimes * where there are a pair of redundant gateways between a pair of networks,
3111553Srgrimes * each running timed.  Assume that we start with a third machine mastering
3121553Srgrimes * one of the networks, and one of the gateways mastering the other.
3131553Srgrimes * Imagine that the third machine goes away and the non-master gateway
3141553Srgrimes * decides to replace it.  If things are timed just 'right,' we will have
3151553Srgrimes * each gateway mastering one network for a little while.  If a SETTIME
3161553Srgrimes * message gets into the network at that time, perhaps from the newly
3171553Srgrimes * masterful gateway as it was taking control, the SETTIME will loop
3181553Srgrimes * forever.  Each time a gateway receives it on its slave side, it will
3191553Srgrimes * call spreadtime to forward it on its mastered network.  We are now in
3201553Srgrimes * a permanent loop, since the SETTIME msgs will keep any clock
3211553Srgrimes * in the network from advancing.  Normally, the 'LOOP' stuff will detect
3221553Srgrimes * and correct the situation.  However, with the clocks stopped, the
3231553Srgrimes * 'looptime' timer cannot expire.  While they are in this state, the
3241553Srgrimes * masters will try to saturate the network with SETTIME packets.
3251553Srgrimes */
3261553Srgrimes			looptime = ntime.tv_sec + (looptime-otime.tv_sec)/2-1;
3271553Srgrimes			break;
3281553Srgrimes
3291553Srgrimes		case TSP_MASTERUP:
3301553Srgrimes			if (slavenet && fromnet != slavenet)
3311553Srgrimes				break;
3321553Srgrimes			if (!good_host_name(msg->tsp_name)) {
3331553Srgrimes				suppress(&from, msg->tsp_name, fromnet);
3341553Srgrimes				if (electiontime > fastelection)
3351553Srgrimes					electiontime = fastelection;
3361553Srgrimes				break;
3371553Srgrimes			}
3381553Srgrimes			makeslave(fromnet);
3391553Srgrimes			setmaster(msg);
3401553Srgrimes			setstatus();
3411553Srgrimes			answerdelay();
3421553Srgrimes			xmit(TSP_SLAVEUP, 0, &from);
3431553Srgrimes			(void)gettimeofday(&ntime, 0);
3441553Srgrimes			electiontime = ntime.tv_sec + delay2;
3451553Srgrimes			fastelection = ntime.tv_sec + FASTTOUT;
3461553Srgrimes			refusetime = 0;
3471553Srgrimes			break;
3481553Srgrimes
3491553Srgrimes		case TSP_MASTERREQ:
3501553Srgrimes			if (fromnet->status != SLAVE)
3511553Srgrimes				break;
3521553Srgrimes			(void)gettimeofday(&ntime, 0);
3531553Srgrimes			electiontime = ntime.tv_sec + delay2;
3541553Srgrimes			break;
3551553Srgrimes
3561553Srgrimes		case TSP_SETDATE:
3571553Srgrimes#ifdef sgi
3581553Srgrimes			(void)cftime(newdate, "%D %T", &msg->tsp_time.tv_sec);
3591553Srgrimes#else
3601553Srgrimes			(void)strcpy(newdate, ctime(&msg->tsp_time.tv_sec));
3611553Srgrimes#endif /* sgi */
3621553Srgrimes			schgdate(msg, newdate);
3631553Srgrimes			break;
3641553Srgrimes
3651553Srgrimes		case TSP_SETDATEREQ:
3661553Srgrimes			if (fromnet->status != MASTER)
3671553Srgrimes				break;
3681553Srgrimes#ifdef sgi
3691553Srgrimes			(void)cftime(newdate, "%D %T", &msg->tsp_time.tv_sec);
3701553Srgrimes#else
3711553Srgrimes			(void)strcpy(newdate, ctime(&msg->tsp_time.tv_sec));
3721553Srgrimes#endif /* sgi */
3731553Srgrimes			htp = findhost(msg->tsp_name);
3741553Srgrimes			if (0 == htp) {
3751553Srgrimes				syslog(LOG_WARNING,
3761553Srgrimes				       "DATEREQ from uncontrolled machine");
3771553Srgrimes				break;
3781553Srgrimes			}
3791553Srgrimes			if (!htp->good) {
3801553Srgrimes				syslog(LOG_WARNING,
3811553Srgrimes				"attempted date change by untrusted %s to %s",
3821553Srgrimes				       htp->name, newdate);
3831553Srgrimes				spreadtime();
3841553Srgrimes				break;
3851553Srgrimes			}
3861553Srgrimes			schgdate(msg, newdate);
3871553Srgrimes			break;
3881553Srgrimes
3891553Srgrimes		case TSP_TRACEON:
3901553Srgrimes			traceon();
3911553Srgrimes			break;
3921553Srgrimes
3931553Srgrimes		case TSP_TRACEOFF:
3941553Srgrimes			traceoff("Tracing ended at %s\n");
3951553Srgrimes			break;
3961553Srgrimes
3971553Srgrimes		case TSP_SLAVEUP:
3981553Srgrimes			newslave(msg);
3991553Srgrimes			break;
4001553Srgrimes
4011553Srgrimes		case TSP_ELECTION:
4021553Srgrimes			if (fromnet->status == SLAVE) {
4031553Srgrimes				(void)gettimeofday(&ntime, 0);
4041553Srgrimes				electiontime = ntime.tv_sec + delay2;
4051553Srgrimes				fastelection = ntime.tv_sec + FASTTOUT;
4061553Srgrimes				seq = 0;
4071553Srgrimes				if (!good_host_name(msg->tsp_name)) {
4081553Srgrimes					syslog(LOG_NOTICE,
4091553Srgrimes					       "suppress election of %s",
4101553Srgrimes					       msg->tsp_name);
4111553Srgrimes					to.tsp_type = TSP_QUIT;
4121553Srgrimes					electiontime = fastelection;
4131553Srgrimes				} else if (cadr.s_addr != from.sin_addr.s_addr
4141553Srgrimes					   && ntime.tv_sec < refusetime) {
4151553Srgrimes/* if the candidate has to repeat itself, the old code would refuse it
4161553Srgrimes * the second time.  That would prevent elections.
4171553Srgrimes */
4181553Srgrimes					to.tsp_type = TSP_REFUSE;
4191553Srgrimes				} else {
4201553Srgrimes					cadr.s_addr = from.sin_addr.s_addr;
4211553Srgrimes					to.tsp_type = TSP_ACCEPT;
4221553Srgrimes					refusetime = ntime.tv_sec + 30;
4231553Srgrimes				}
4241553Srgrimes				taddr = from;
4251553Srgrimes				(void)strcpy(tname, msg->tsp_name);
4261553Srgrimes				(void)strcpy(to.tsp_name, hostname);
4271553Srgrimes				answerdelay();
4281553Srgrimes				if (!acksend(&to, &taddr, tname,
4291553Srgrimes					     TSP_ACK, 0, 0))
4301553Srgrimes					syslog(LOG_WARNING,
4311553Srgrimes					     "no answer from candidate %s\n",
4321553Srgrimes					       tname);
4331553Srgrimes
4341553Srgrimes			} else {	/* fromnet->status == MASTER */
4351553Srgrimes				htp = addmach(msg->tsp_name, &from,fromnet);
4361553Srgrimes				to.tsp_type = TSP_QUIT;
4371553Srgrimes				(void)strcpy(to.tsp_name, hostname);
4381553Srgrimes				if (!acksend(&to, &htp->addr, htp->name,
4391553Srgrimes					     TSP_ACK, 0, htp->noanswer)) {
4401553Srgrimes					syslog(LOG_ERR,
4411553Srgrimes					  "no reply from %s to ELECTION-QUIT",
4421553Srgrimes					       htp->name);
4431553Srgrimes					(void)remmach(htp);
4441553Srgrimes				}
4451553Srgrimes			}
4461553Srgrimes			break;
4471553Srgrimes
4481553Srgrimes		case TSP_CONFLICT:
4491553Srgrimes			if (fromnet->status != MASTER)
4501553Srgrimes				break;
4511553Srgrimes			/*
4521553Srgrimes			 * After a network partition, there can be
4531553Srgrimes			 * more than one master: the first slave to
4541553Srgrimes			 * come up will notify here the situation.
4551553Srgrimes			 */
4561553Srgrimes			(void)strcpy(to.tsp_name, hostname);
4571553Srgrimes
4581553Srgrimes			/* The other master often gets into the same state,
4591553Srgrimes			 * with boring results.
4601553Srgrimes			 */
4611553Srgrimes			ntp = fromnet;	/* (acksend() can leave fromnet=0 */
4621553Srgrimes			for (tries = 0; tries < 3; tries++) {
4631553Srgrimes				to.tsp_type = TSP_RESOLVE;
4641553Srgrimes				answer = acksend(&to, &ntp->dest_addr,
4651553Srgrimes						 ANYADDR, TSP_MASTERACK,
4661553Srgrimes						 ntp, 0);
4671553Srgrimes				if (answer == NULL)
4681553Srgrimes					break;
4691553Srgrimes				htp = addmach(answer->tsp_name,&from,ntp);
4701553Srgrimes				to.tsp_type = TSP_QUIT;
4711553Srgrimes				answer = acksend(&to, &htp->addr, htp->name,
4721553Srgrimes						 TSP_ACK, 0, htp->noanswer);
4731553Srgrimes				if (!answer) {
4741553Srgrimes					syslog(LOG_WARNING,
4751553Srgrimes				  "conflict error: no reply from %s to QUIT",
4761553Srgrimes						htp->name);
4771553Srgrimes					(void)remmach(htp);
4781553Srgrimes				}
4791553Srgrimes			}
4801553Srgrimes			masterup(ntp);
4811553Srgrimes			break;
4821553Srgrimes
4831553Srgrimes		case TSP_MSITE:
4841553Srgrimes			if (!slavenet)
4851553Srgrimes				break;
4861553Srgrimes			taddr = from;
4871553Srgrimes			to.tsp_type = TSP_MSITEREQ;
4881553Srgrimes			to.tsp_vers = TSPVERSION;
4891553Srgrimes			to.tsp_seq = 0;
4901553Srgrimes			(void)strcpy(to.tsp_name, hostname);
4911553Srgrimes			answer = acksend(&to, &slavenet->dest_addr,
4921553Srgrimes					 ANYADDR, TSP_ACK,
4931553Srgrimes					 slavenet, 0);
4941553Srgrimes			if (answer != NULL
4951553Srgrimes			    && good_host_name(answer->tsp_name)) {
4961553Srgrimes				setmaster(answer);
4971553Srgrimes				to.tsp_type = TSP_ACK;
4981553Srgrimes				(void)strcpy(to.tsp_name, answer->tsp_name);
4991553Srgrimes				bytenetorder(&to);
5001553Srgrimes				if (sendto(sock, (char *)&to,
5011553Srgrimes					   sizeof(struct tsp), 0,
5021553Srgrimes					   (struct sockaddr*)&taddr, sizeof(taddr)) < 0) {
5031553Srgrimes					trace_sendto_err(taddr.sin_addr);
5041553Srgrimes				}
5051553Srgrimes			}
5061553Srgrimes			break;
5071553Srgrimes
5081553Srgrimes		case TSP_MSITEREQ:
5091553Srgrimes			break;
5101553Srgrimes
5111553Srgrimes		case TSP_ACCEPT:
5121553Srgrimes		case TSP_REFUSE:
5131553Srgrimes		case TSP_RESOLVE:
5141553Srgrimes			break;
5151553Srgrimes
5161553Srgrimes		case TSP_QUIT:
5171553Srgrimes			doquit(msg);		/* become a slave */
5181553Srgrimes			break;
5191553Srgrimes
5201553Srgrimes		case TSP_TEST:
5211553Srgrimes			electiontime = 0;
5221553Srgrimes			break;
5231553Srgrimes
5241553Srgrimes		case TSP_LOOP:
5251553Srgrimes			/* looking for loops of masters */
5261553Srgrimes			if (!(status & MASTER))
5271553Srgrimes				break;
5281553Srgrimes			if (fromnet->status == SLAVE) {
5291553Srgrimes			    if (!strcmp(msg->tsp_name, hostname)) {
5301553Srgrimes				/*
5311553Srgrimes				 * Someone forwarded our message back to
5321553Srgrimes				 * us.  There must be a loop.  Tell the
5331553Srgrimes				 * master of this network to quit.
5341553Srgrimes				 *
5351553Srgrimes				 * The other master often gets into
5361553Srgrimes				 * the same state, with boring results.
5371553Srgrimes				 */
5381553Srgrimes				ntp = fromnet;
5391553Srgrimes				for (tries = 0; tries < 3; tries++) {
5401553Srgrimes				    to.tsp_type = TSP_RESOLVE;
5411553Srgrimes				    answer = acksend(&to, &ntp->dest_addr,
5421553Srgrimes						     ANYADDR, TSP_MASTERACK,
5431553Srgrimes						     ntp,0);
5441553Srgrimes				    if (answer == NULL)
5451553Srgrimes					break;
5461553Srgrimes				    taddr = from;
5471553Srgrimes				    (void)strcpy(tname, answer->tsp_name);
5481553Srgrimes				    to.tsp_type = TSP_QUIT;
5491553Srgrimes				    (void)strcpy(to.tsp_name, hostname);
5501553Srgrimes				    if (!acksend(&to, &taddr, tname,
5511553Srgrimes						 TSP_ACK, 0, 1)) {
5521553Srgrimes					syslog(LOG_ERR,
5531553Srgrimes					"no reply from %s to slave LOOP-QUIT",
5541553Srgrimes						 tname);
5551553Srgrimes				    } else {
5561553Srgrimes					electiontime = 0;
5571553Srgrimes				    }
5581553Srgrimes				}
5591553Srgrimes				(void)gettimeofday(&ntime, 0);
5601553Srgrimes				looptime = ntime.tv_sec + FASTTOUT;
5611553Srgrimes			    } else {
5621553Srgrimes				if (msg->tsp_hopcnt-- < 1)
5631553Srgrimes				    break;
5641553Srgrimes				bytenetorder(msg);
5651553Srgrimes				for (ntp = nettab; ntp != 0; ntp = ntp->next) {
5661553Srgrimes				    if (ntp->status == MASTER
5671553Srgrimes					&& 0 > sendto(sock, (char *)msg,
5681553Srgrimes						      sizeof(struct tsp), 0,
5691553Srgrimes					      (struct sockaddr*)&ntp->dest_addr,
5701553Srgrimes						      sizeof(ntp->dest_addr)))
5711553Srgrimes				    trace_sendto_err(ntp->dest_addr.sin_addr);
5721553Srgrimes				}
5731553Srgrimes			    }
5741553Srgrimes			} else {	/* fromnet->status == MASTER */
5751553Srgrimes			    /*
5761553Srgrimes			     * We should not have received this from a net
5771553Srgrimes			     * we are master on.  There must be two masters,
5781553Srgrimes			     * unless the packet was really from us.
5791553Srgrimes			     */
5801553Srgrimes			    if (from.sin_addr.s_addr
5811553Srgrimes				== fromnet->my_addr.s_addr) {
5821553Srgrimes				if (trace)
5831553Srgrimes				    fprintf(fd,"discarding forwarded LOOP\n");
5841553Srgrimes				break;
5851553Srgrimes			    }
5861553Srgrimes
5871553Srgrimes			    /*
5881553Srgrimes			     * The other master often gets into the same
5891553Srgrimes			     * state, with boring results.
5901553Srgrimes			     */
5911553Srgrimes			    ntp = fromnet;
5921553Srgrimes			    for (tries = 0; tries < 3; tries++) {
5931553Srgrimes				to.tsp_type = TSP_RESOLVE;
5941553Srgrimes				answer = acksend(&to, &ntp->dest_addr,
5951553Srgrimes						 ANYADDR, TSP_MASTERACK,
5961553Srgrimes						ntp,0);
5971553Srgrimes				if (!answer)
5981553Srgrimes					break;
5991553Srgrimes				htp = addmach(answer->tsp_name,
6001553Srgrimes					      &from,ntp);
6011553Srgrimes				to.tsp_type = TSP_QUIT;
6021553Srgrimes				(void)strcpy(to.tsp_name, hostname);
6031553Srgrimes				if (!acksend(&to,&htp->addr,htp->name,
6041553Srgrimes					     TSP_ACK, 0, htp->noanswer)) {
6051553Srgrimes					syslog(LOG_ERR,
6061553Srgrimes				    "no reply from %s to master LOOP-QUIT",
6071553Srgrimes					       htp->name);
6081553Srgrimes					(void)remmach(htp);
6091553Srgrimes				}
6101553Srgrimes			    }
6111553Srgrimes			    (void)gettimeofday(&ntime, 0);
6121553Srgrimes			    looptime = ntime.tv_sec + FASTTOUT;
6131553Srgrimes			}
6141553Srgrimes			break;
6151553Srgrimes		default:
6161553Srgrimes			if (trace) {
6171553Srgrimes				fprintf(fd, "garbage message: ");
6181553Srgrimes				print(msg, &from);
6191553Srgrimes			}
6201553Srgrimes			break;
6211553Srgrimes		}
6221553Srgrimes	}
6231553Srgrimes	goto loop;
6241553Srgrimes}
6251553Srgrimes
6261553Srgrimes
6271553Srgrimes/*
6281553Srgrimes * tell the world who our master is
6291553Srgrimes */
6301553Srgrimesstatic void
6311553Srgrimessetmaster(msg)
6321553Srgrimes	struct tsp *msg;
6331553Srgrimes{
6341553Srgrimes	if (slavenet
6351553Srgrimes	    && (slavenet != old_slavenet
6361553Srgrimes		|| strcmp(msg->tsp_name, master_name)
6371553Srgrimes		|| old_status != status)) {
6381553Srgrimes		(void)strcpy(master_name, msg->tsp_name);
6391553Srgrimes		old_slavenet = slavenet;
6401553Srgrimes		old_status = status;
6411553Srgrimes
6421553Srgrimes		if (status & MASTER) {
6431553Srgrimes			syslog(LOG_NOTICE, "submaster to %s", master_name);
6441553Srgrimes			if (trace)
6451553Srgrimes				fprintf(fd, "submaster to %s\n", master_name);
6461553Srgrimes
6471553Srgrimes		} else {
6481553Srgrimes			syslog(LOG_NOTICE, "slave to %s", master_name);
6491553Srgrimes			if (trace)
6501553Srgrimes				fprintf(fd, "slave to %s\n", master_name);
6511553Srgrimes		}
6521553Srgrimes	}
6531553Srgrimes}
6541553Srgrimes
6551553Srgrimes
6561553Srgrimes
6571553Srgrimes/*
6581553Srgrimes * handle date change request on a slave
6591553Srgrimes */
6601553Srgrimesstatic void
6611553Srgrimesschgdate(msg, newdate)
6621553Srgrimes	struct tsp *msg;
6631553Srgrimes	char *newdate;
6641553Srgrimes{
6651553Srgrimes	struct tsp to;
6661553Srgrimes	u_short seq;
6671553Srgrimes	struct sockaddr_in taddr;
6681553Srgrimes	struct timeval otime;
6691553Srgrimes
6701553Srgrimes	if (!slavenet)
6711553Srgrimes		return;			/* no where to forward */
6721553Srgrimes
6731553Srgrimes	taddr = from;
6741553Srgrimes	seq = msg->tsp_seq;
6751553Srgrimes
6761553Srgrimes	syslog(LOG_INFO,
6771553Srgrimes	       "forwarding date change by %s to %s",
6781553Srgrimes	       msg->tsp_name, newdate);
6791553Srgrimes
6801553Srgrimes	/* adjust time for residence on the queue */
6811553Srgrimes	(void)gettimeofday(&otime, 0);
6821553Srgrimes	adj_msg_time(msg, &otime);
6831553Srgrimes
6841553Srgrimes	to.tsp_type = TSP_SETDATEREQ;
6851553Srgrimes	to.tsp_time = msg->tsp_time;
6861553Srgrimes	(void)strcpy(to.tsp_name, hostname);
6871553Srgrimes	if (!acksend(&to, &slavenet->dest_addr,
6881553Srgrimes		     ANYADDR, TSP_DATEACK,
6891553Srgrimes		     slavenet, 0))
6901553Srgrimes		return;			/* no answer */
6911553Srgrimes
6921553Srgrimes	xmit(TSP_DATEACK, seq, &taddr);
6931553Srgrimes}
6941553Srgrimes
6951553Srgrimes
6961553Srgrimes/*
6971553Srgrimes * Used before answering a broadcast message to avoid network
6981553Srgrimes * contention and likely collisions.
6991553Srgrimes */
7001553Srgrimesstatic void
7011553Srgrimesanswerdelay()
7021553Srgrimes{
7031553Srgrimes#ifdef sgi
7041553Srgrimes	sginap(delay1);
7051553Srgrimes#else
7061553Srgrimes	struct timeval timeout;
7071553Srgrimes
7081553Srgrimes	timeout.tv_sec = 0;
7091553Srgrimes	timeout.tv_usec = delay1;
7101553Srgrimes
7111553Srgrimes	(void)select(0, (fd_set *)NULL, (fd_set *)NULL, (fd_set *)NULL,
7121553Srgrimes	    &timeout);
7131553Srgrimes	return;
7141553Srgrimes#endif /* sgi */
7151553Srgrimes}
716