⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 slave.c

📁 早期freebsd实现
💻 C
📖 第 1 页 / 共 2 页
字号:
#else			(void)strcpy(newdate, ctime(&msg->tsp_time.tv_sec));#endif /* sgi */			schgdate(msg, newdate);			break;		case TSP_SETDATEREQ:			if (fromnet->status != MASTER)				break;#ifdef sgi			(void)cftime(newdate, "%D %T", &msg->tsp_time.tv_sec);#else			(void)strcpy(newdate, ctime(&msg->tsp_time.tv_sec));#endif /* sgi */			htp = findhost(msg->tsp_name);			if (0 == htp) {				syslog(LOG_WARNING,				       "DATEREQ from uncontrolled machine");				break;			}			if (!htp->good) {				syslog(LOG_WARNING,				"attempted date change by untrusted %s to %s",				       htp->name, newdate);				spreadtime();				break;			}			schgdate(msg, newdate);			break;		case TSP_TRACEON:			traceon();			break;		case TSP_TRACEOFF:			traceoff("Tracing ended at %s\n");			break;		case TSP_SLAVEUP:			newslave(msg);			break;		case TSP_ELECTION:			if (fromnet->status == SLAVE) {				(void)gettimeofday(&ntime, 0);				electiontime = ntime.tv_sec + delay2;				fastelection = ntime.tv_sec + FASTTOUT;				seq = 0;				if (!good_host_name(msg->tsp_name)) {					syslog(LOG_NOTICE,					       "suppress election of %s",					       msg->tsp_name);					to.tsp_type = TSP_QUIT;					electiontime = fastelection;				} else if (cadr.s_addr != from.sin_addr.s_addr					   && ntime.tv_sec < refusetime) {/* if the candidate has to repeat itself, the old code would refuse it * the second time.  That would prevent elections. */					to.tsp_type = TSP_REFUSE;				} else {					cadr.s_addr = from.sin_addr.s_addr;					to.tsp_type = TSP_ACCEPT;					refusetime = ntime.tv_sec + 30;				}				taddr = from;				(void)strcpy(tname, msg->tsp_name);				(void)strcpy(to.tsp_name, hostname);				answerdelay();				if (!acksend(&to, &taddr, tname,					     TSP_ACK, 0, 0))					syslog(LOG_WARNING,					     "no answer from candidate %s\n",					       tname);			} else {	/* fromnet->status == MASTER */				htp = addmach(msg->tsp_name, &from,fromnet);				to.tsp_type = TSP_QUIT;				(void)strcpy(to.tsp_name, hostname);				if (!acksend(&to, &htp->addr, htp->name,					     TSP_ACK, 0, htp->noanswer)) {					syslog(LOG_ERR,					  "no reply from %s to ELECTION-QUIT",					       htp->name);					(void)remmach(htp);				}			}			break;		case TSP_CONFLICT:			if (fromnet->status != MASTER)				break;			/*			 * After a network partition, there can be			 * more than one master: the first slave to			 * come up will notify here the situation.			 */			(void)strcpy(to.tsp_name, hostname);			/* The other master often gets into the same state,			 * with boring results.			 */			ntp = fromnet;	/* (acksend() can leave fromnet=0 */			for (tries = 0; tries < 3; tries++) {				to.tsp_type = TSP_RESOLVE;				answer = acksend(&to, &ntp->dest_addr,						 ANYADDR, TSP_MASTERACK,						 ntp, 0);				if (answer == NULL)					break;				htp = addmach(answer->tsp_name,&from,ntp);				to.tsp_type = TSP_QUIT;				answer = acksend(&to, &htp->addr, htp->name,						 TSP_ACK, 0, htp->noanswer);				if (!answer) {					syslog(LOG_WARNING,				  "conflict error: no reply from %s to QUIT",						htp->name);					(void)remmach(htp);				}			}			masterup(ntp);			break;		case TSP_MSITE:			if (!slavenet)				break;			taddr = from;			to.tsp_type = TSP_MSITEREQ;			to.tsp_vers = TSPVERSION;			to.tsp_seq = 0;			(void)strcpy(to.tsp_name, hostname);			answer = acksend(&to, &slavenet->dest_addr,					 ANYADDR, TSP_ACK,					 slavenet, 0);			if (answer != NULL			    && good_host_name(answer->tsp_name)) {				setmaster(answer);				to.tsp_type = TSP_ACK;				(void)strcpy(to.tsp_name, answer->tsp_name);				bytenetorder(&to);				if (sendto(sock, (char *)&to,					   sizeof(struct tsp), 0,					   (struct sockaddr*)&taddr, sizeof(taddr)) < 0) {					trace_sendto_err(taddr.sin_addr);				}			}			break;		case TSP_MSITEREQ:			break;		case TSP_ACCEPT:		case TSP_REFUSE:		case TSP_RESOLVE:			break;		case TSP_QUIT:			doquit(msg);		/* become a slave */			break;		case TSP_TEST:			electiontime = 0;			break;		case TSP_LOOP:			/* looking for loops of masters */			if (!(status & MASTER))				break;			if (fromnet->status == SLAVE) {			    if (!strcmp(msg->tsp_name, hostname)) {				/*				 * Someone forwarded our message back to				 * us.  There must be a loop.  Tell the				 * master of this network to quit.				 *				 * The other master often gets into				 * the same state, with boring results.				 */				ntp = fromnet;				for (tries = 0; tries < 3; tries++) {				    to.tsp_type = TSP_RESOLVE;				    answer = acksend(&to, &ntp->dest_addr,						     ANYADDR, TSP_MASTERACK,						     ntp,0);				    if (answer == NULL)					break;				    taddr = from;				    (void)strcpy(tname, answer->tsp_name);				    to.tsp_type = TSP_QUIT;				    (void)strcpy(to.tsp_name, hostname);				    if (!acksend(&to, &taddr, tname,						 TSP_ACK, 0, 1)) {					syslog(LOG_ERR,					"no reply from %s to slave LOOP-QUIT",						 tname);				    } else {					electiontime = 0;				    }				}				(void)gettimeofday(&ntime, 0);				looptime = ntime.tv_sec + FASTTOUT;			    } else {				if (msg->tsp_hopcnt-- < 1)				    break;				bytenetorder(msg);				for (ntp = nettab; ntp != 0; ntp = ntp->next) {				    if (ntp->status == MASTER					&& 0 > sendto(sock, (char *)msg,						      sizeof(struct tsp), 0,					      (struct sockaddr*)&ntp->dest_addr,						      sizeof(ntp->dest_addr)))				    trace_sendto_err(ntp->dest_addr.sin_addr);				}			    }			} else {	/* fromnet->status == MASTER */			    /*			     * We should not have received this from a net			     * we are master on.  There must be two masters,			     * unless the packet was really from us.			     */			    if (from.sin_addr.s_addr				== fromnet->my_addr.s_addr) {				if (trace)				    fprintf(fd,"discarding forwarded LOOP\n");				break;			    }			    /*			     * The other master often gets into the same			     * state, with boring results.			     */			    ntp = fromnet;			    for (tries = 0; tries < 3; tries++) {				to.tsp_type = TSP_RESOLVE;				answer = acksend(&to, &ntp->dest_addr,						 ANYADDR, TSP_MASTERACK,						ntp,0);				if (!answer)					break;				htp = addmach(answer->tsp_name,					      &from,ntp);				to.tsp_type = TSP_QUIT;				(void)strcpy(to.tsp_name, hostname);				if (!acksend(&to,&htp->addr,htp->name,					     TSP_ACK, 0, htp->noanswer)) {					syslog(LOG_ERR,				    "no reply from %s to master LOOP-QUIT",					       htp->name);					(void)remmach(htp);				}			    }			    (void)gettimeofday(&ntime, 0);			    looptime = ntime.tv_sec + FASTTOUT;			}			break;		default:			if (trace) {				fprintf(fd, "garbage message: ");				print(msg, &from);			}			break;		}	}	goto loop;}/* * tell the world who our master is */static voidsetmaster(msg)	struct tsp *msg;{	if (slavenet	    && (slavenet != old_slavenet		|| strcmp(msg->tsp_name, master_name)		|| old_status != status)) {		(void)strcpy(master_name, msg->tsp_name);		old_slavenet = slavenet;		old_status = status;		if (status & MASTER) {			syslog(LOG_NOTICE, "submaster to %s", master_name);			if (trace)				fprintf(fd, "submaster to %s\n", master_name);		} else {			syslog(LOG_NOTICE, "slave to %s", master_name);			if (trace)				fprintf(fd, "slave to %s\n", master_name);		}	}}/* * handle date change request on a slave */static voidschgdate(msg, newdate)	struct tsp *msg;	char *newdate;{	struct tsp to;	u_short seq;	struct sockaddr_in taddr;	struct timeval otime;	if (!slavenet)		return;			/* no where to forward */	taddr = from;	seq = msg->tsp_seq;	syslog(LOG_INFO,	       "forwarding date change by %s to %s",	       msg->tsp_name, newdate);	/* adjust time for residence on the queue */	(void)gettimeofday(&otime, 0);	adj_msg_time(msg, &otime);	to.tsp_type = TSP_SETDATEREQ;	to.tsp_time = msg->tsp_time;	(void)strcpy(to.tsp_name, hostname);	if (!acksend(&to, &slavenet->dest_addr,		     ANYADDR, TSP_DATEACK,		     slavenet, 0))		return;			/* no answer */	xmit(TSP_DATEACK, seq, &taddr);}/* * Used before answering a broadcast message to avoid network * contention and likely collisions. */static voidanswerdelay(){#ifdef sgi	sginap(delay1);#else	struct timeval timeout;	timeout.tv_sec = 0;	timeout.tv_usec = delay1;	(void)select(0, (fd_set *)NULL, (fd_set *)NULL, (fd_set *)NULL,	    &timeout);	return;#endif /* sgi */}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -