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