Exemple #1
0
int main (void)
{
	static const prog_char mess1[] = "test2.c software uart sample program\r\n";
	const prog_char *s;
	char c;

	/* Initialize I/O ports */
	PORTB = 0b11111110;	/* PB0:Tx/Rx */
	DDRB  = 0b00000000;
	PORTD = 0b01111111;	/* Pull-up Port D */


	/* Send message */
	s = mess1;
	for (;;) {
		c = pgm_read_byte(s++);
		if (!c) break;
		xmit(c);
	}

	/* Receives data and echos it in incremented data */
	for(;;) {
		c = rcvr();
		c++;
		xmit(c);
	}

}
Exemple #2
0
void xmit_dec(uint16_t chr)
{
    uint8_t d1;
    d1=chr/100;
    xmit(d1+0x30);
    d1=(chr % 100)/10;
    xmit(d1+0x30);
    d1=(chr % 100) % 10;
    xmit(d1+0x30);
}
Exemple #3
0
int main (void)
{
	char *cp;
	uint16_t m, n, s;
	uint16_t t1,t2,t3;


	DDRE = 0b00000010;	/* PE1:<conout>, PE0:<conin> in N81 38.4kbps */
	TCCR1B = 3;	/* clk/64 */

	xmitstr(PSTR("\r\nFFT sample program\r\n"));

	for(;;) {
		xmitstr(PSTR("\r\n>"));			/* Prompt */
		rcvrstr(pool, sizeof(pool));	/* Console input */
		cp = pool;

		switch (*cp++) {	/* Pick a header char (command) */
			case '\0' :		/* Blank line */
				break;

			case 'w' :		/* w: show waveform */
				capture_wave(capture, FFT_N);
				for (n = 0; n < FFT_N; n++) {
					s = capture[n];
					xmitf(PSTR("\r\n%4u:%6d "), n, s);
					s = (s + 32768) / 1024;
					for (m = 0; m < s; m++) xmit(' ');
					xmit('*');
				}
				break;

			case 's' :		/* s: show spectrum */
				capture_wave(capture, FFT_N);
				TCNT1 = 0;	/* performance counter */
				fft_input(capture, bfly_buff);
				t1 = TCNT1; TCNT1 = 0;
				fft_execute(bfly_buff);
				t2 = TCNT1; TCNT1 = 0;
				fft_output(bfly_buff, spektrum);
				t3 = TCNT1;
				for (n = 0; n < FFT_N / 2; n++) {
					s = spektrum[n];
					xmitf(PSTR("\r\n%4u:%5u "), n, s);
					s /= 512;
					for (m = 0; m < s; m++) xmit('*');
				}
				xmitf(PSTR("\r\ninput=%u, execute=%u, output=%u (x64clk)"), t1,t2,t3);
				break;

			default :		/* Unknown command */
				xmitstr(PSTR("\n???"));
		}
	}
}
Exemple #4
0
void xmit_hex(uint8_t chr)
{
    uint8_t wk;
//  xmitstr_p(PSTR("0x"));
    wk = chr /16;
    if (wk >9 ) xmit(wk + 0x37);
    else xmit(wk + 0x30);
    wk = chr % 16;
    if (wk >9) xmit(wk + 0x37);
    else xmit(wk + 0x30);
}
Exemple #5
0
/*
 * handle date change request on a slave
 */
static void
schgdate(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, NULL);
	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);
}
Exemple #6
0
/*
 * change the system date on the master
 */
static void
mchgdate(struct tsp *msg)
{
	char tname[MAXHOSTNAMELEN];
	char olddate[32];
	struct timeval otime, ntime;

	strlcpy(tname, msg->tsp_name, sizeof(tname));

	xmit(TSP_DATEACK, msg->tsp_seq, &from);

	strlcpy(olddate, date(), sizeof(olddate));

	/* adjust time for residence on the queue */
	gettimeofday(&otime, 0);
	adj_msg_time(msg,&otime);

	timevalsub(&ntime, &msg->tsp_time, &otime);
	if (ntime.tv_sec < MAXADJ && ntime.tv_sec > -MAXADJ) {
		/*
		 * do not change the clock if we can adjust it
		 */
		dictate = 3;
		synch(tvtomsround(ntime));
	} else {
		logwtmp("|", "date", "");
		settimeofday(&msg->tsp_time, 0);
		logwtmp("{", "date", "");
		spreadtime();
	}

	syslog(LOG_NOTICE, "date changed by %s from %s",
	       tname, olddate);
}
void
dnsreq::start (bool again)
{
  if (again && (srchno < 0 || !resp->srchlist (srchno))) {
    fail (NXDOMAIN);
    return;
  }

  if (again) {
    resp->reqtab.remove (this);
    if (!usetcp)
      resp->reqtoq.remove (this);
  }
  if (srchno >= 0) {
    const char *suffix = resp->srchlist (srchno++);
    if (*suffix)
      name = strbuf ("%s.%s", basename.cstr (), suffix);
    else
      name = basename;
  }
  id = resp->genid ();
  intable = true;
  resp->reqtab.insert (this);
  if (usetcp)
    xmit (0);
  else
    resp->reqtoq.start (this);
}
Exemple #8
0
/*
 * Send the necessary acknowledgements:
 * only the type ACK is to be sent by a slave
 */
void
slaveack(void)
{
	switch(msgin.tsp_type) {

	case TSP_ADJTIME:
	case TSP_SETTIME:
	case TSP_ACCEPT:
	case TSP_REFUSE:
	case TSP_TRACEON:
	case TSP_TRACEOFF:
	case TSP_QUIT:
		if (trace) {
			fprintf(fd, "Slaveack: ");
			print(&msgin, &from);
		}
		xmit(TSP_ACK,msgin.tsp_seq, &from);
		break;

	default:
		if (trace) {
			fprintf(fd, "Slaveack: no ack: ");
			print(&msgin, &from);
		}
		break;
	}
}
Exemple #9
0
void
masterup(struct netinfo *net)
{
	xmit(TSP_MASTERUP, 0, &net->dest_addr);

	/*
	 * Do not tell new slaves our time for a while.  This ensures
	 * we do not tell them to start using our time, before we have
	 * found a good master.
	 */
	(void)gettimeofday(&net->slvwait, NULL);
}
int main()
{
	uint16_t i,t;
	
	initSerial();
	
	xputs(PSTR("\r\n"));
	xputs(PSTR("> -------------------------------\r\n"));
	xputs(PSTR("> Hello Console!\r\n"));
	
	if(onewire_firstAddress())
	{
		xprintf(PSTR("> Addr: %X\t"),ROM_NO[0]);
		for(t=0;t<7;t++)
			xprintf(PSTR("%X\t"),ROM_NO[t+1]);			
		xmit('\r');
		xmit('\n');
		i=1;
	}

	while(onewire_nextAddress())
	{
		xprintf(PSTR("> Addr: %X\t"),ROM_NO[0]);
		for(t=0;t<7;t++)
			xprintf(PSTR("%X\t"),ROM_NO[t+1]);			
		xmit('\r');
		xmit('\n');
		i++;
	}
		
	xprintf(PSTR("> End of search with %d device(s) found\r\n"),i);
		
	while(1)
	{
		// ...
	}	
	
	return 0;
}
Exemple #11
0
/*
 * `masterack' sends the necessary acknowledgments
 * to the messages received by a master
 */
void
masterack(void)
{
	struct tsp resp;

	resp = msgin;
	resp.tsp_vers = TSPVERSION;
	(void)strcpy(resp.tsp_name, hostname);

	switch(msgin.tsp_type) {

	case TSP_QUIT:
	case TSP_TRACEON:
	case TSP_TRACEOFF:
	case TSP_MSITEREQ:
		if (trace) {
			fprintf(fd, "Masterack: ");
			print(&msgin, &from);
		}
		xmit(TSP_ACK,msgin.tsp_seq, &from);
		break;

	case TSP_RESOLVE:
	case TSP_MASTERREQ:
		if (trace) {
			fprintf(fd, "Masterack: ");
			print(&msgin, &from);
		}
		xmit(TSP_MASTERACK,msgin.tsp_seq, &from);
		break;

	default:
		if (trace) {
			fprintf(fd,"Masterack: no ack: ");
			print(&msgin, &from);
		}
		break;
	}
}
Exemple #12
0
void callback(const uint8_t * str, uint8_t len){
#ifdef NETMOD_DEBUG
	Serial.println("Received!");
	Serial.write(str, len);
	Serial.println("\n---");
#endif

#ifdef NETMOD_PRINTPKT
	printPkt((dataPkt *)str);
#endif

	xmit(str, len);

	return;
}
Exemple #13
0
/*
 * change the system date on the master
 */
static void
mchgdate(struct tsp *msg)
{
	char tname[MAXHOSTNAMELEN];
	char olddate[32];
	struct timeval otime, ntime, tmptv;
	struct utmpx utx;

	(void)strcpy(tname, msg->tsp_name);

	xmit(TSP_DATEACK, msg->tsp_seq, &from);

	(void)strcpy(olddate, date());

	/* adjust time for residence on the queue */
	(void)gettimeofday(&otime, NULL);
	adj_msg_time(msg,&otime);

 	tmptv.tv_sec = msg->tsp_time.tv_sec;
 	tmptv.tv_usec = msg->tsp_time.tv_usec;
	timevalsub(&ntime, &tmptv, &otime);
	if (ntime.tv_sec < MAXADJ && ntime.tv_sec > -MAXADJ) {
		/*
		 * do not change the clock if we can adjust it
		 */
		dictate = 3;
		synch(tvtomsround(ntime));
	} else {
		utx.ut_type = OLD_TIME;
		(void)gettimeofday(&utx.ut_tv, NULL);
		pututxline(&utx);
 		(void)settimeofday(&tmptv, 0);
		utx.ut_type = NEW_TIME;
		(void)gettimeofday(&utx.ut_tv, NULL);
		pututxline(&utx);
		spreadtime();
	}

	syslog(LOG_NOTICE, "date changed by %s from %s",
	       tname, olddate);
}
Exemple #14
0
	void Client::transmitmore( const void *data, int len )
	{
	  if ( !fpLog.empty() )
	  {
        fmt::Writer tmp;
        tmp << "Server -> Client (" << len << " bytes)\n";
        Clib::fdump( tmp, data, len );
        FLEXLOG( fpLog ) << tmp.c_str() << "\n";
	  }

	  if ( encrypt_server_stream )
	  {
		pause();
		transmit_encrypted( data, len );
	  }
	  else
	  {
		xmit( data, static_cast<unsigned short>( len ) );
		// _xmit( client->csocket, data, len );
	  }
	}
Exemple #15
0
void demosend(){
//calc current accel stats
	if(nsamples > 0) avgTilt /= (float) nsamples;
	else avgTilt = 0;

	if(nsamples < 2) varZ = 0;
	else varZ = m2/(float)(nsamples - 1);

//make a packet
	dataPkt pkt;

	pkt.hour = 0;
	pkt.min = 0;
	pkt.sec = 0;
	pkt.year = 0;
	pkt.mth = 0;
	pkt.day = 0;
	pkt.latitude = 0;
	pkt.longitude = 0;

	pkt.avgAngle = avgTilt;
	pkt.varVertical = varZ;


	pkt.seqId = 10000;
	pkt.srcId = millis()%1000;

//send it
	xmit((const uint8_t *)&pkt, sizeof(pkt));

//reset vars
	avgTilt = 0;
	varZ = 0;
	meanZ = 0;
	m2 = 0;
	nsamples = 0;

	return;

}
Exemple #16
0
/*
 * Certain packets may arrive from this machine on ignored networks.
 * These packets should be acknowledged.
 */
void
ignoreack(void)
{
	switch(msgin.tsp_type) {

	case TSP_TRACEON:
	case TSP_TRACEOFF:
	case TSP_QUIT:
		if (trace) {
			fprintf(fd, "Ignoreack: ");
			print(&msgin, &from);
		}
		xmit(TSP_ACK,msgin.tsp_seq, &from);
		break;

	default:
		if (trace) {
			fprintf(fd, "Ignoreack: no ack: ");
			print(&msgin, &from);
		}
		break;
	}
}
Exemple #17
0
int
slave()
{
	int tries;
	long electiontime, refusetime, looktime, looptime, adjtime;
	u_short seq;
	long fastelection;
#define FASTTOUT 3
	struct in_addr cadr;
	struct timeval otime;
	struct sockaddr_in taddr;
	char tname[MAXHOSTNAMELEN];
	struct tsp *msg, to;
	struct timeval ntime, wait, tmptv;
	time_t tsp_time_sec;
	struct tsp *answer;
	int timeout();
	char olddate[32];
	char newdate[32];
	struct netinfo *ntp;
	struct hosttbl *htp;
	struct utmpx utx;


	old_slavenet = 0;
	seq = 0;
	refusetime = 0;
	adjtime = 0;

	(void)gettimeofday(&ntime, NULL);
	electiontime = ntime.tv_sec + delay2;
	fastelection = ntime.tv_sec + FASTTOUT;
	if (justquit)
		looktime = electiontime;
	else
		looktime = fastelection;
	looptime = fastelection;

	if (slavenet)
		xmit(TSP_SLAVEUP, 0, &slavenet->dest_addr);
	if (status & MASTER) {
		for (ntp = nettab; ntp != NULL; ntp = ntp->next) {
			if (ntp->status == MASTER)
				masterup(ntp);
		}
	}

loop:
	get_goodgroup(0);
	(void)gettimeofday(&ntime, NULL);
	if (ntime.tv_sec > electiontime) {
		if (trace)
			fprintf(fd, "election timer expired\n");
		longjmp(jmpenv, 1);
	}

	if (ntime.tv_sec >= looktime) {
		if (trace)
			fprintf(fd, "Looking for nets to master\n");

		if (Mflag && nignorednets > 0) {
			for (ntp = nettab; ntp != NULL; ntp = ntp->next) {
				if (ntp->status == IGNORE
				    || ntp->status == NOMASTER) {
					lookformaster(ntp);
					if (ntp->status == MASTER) {
						masterup(ntp);
					} else if (ntp->status == MASTER) {
						ntp->status = NOMASTER;
					}
				}
				if (ntp->status == MASTER
				    && --ntp->quit_count < 0)
					ntp->quit_count = 0;
			}
			makeslave(slavenet);	/* prune extras */
			setstatus();
		}
		(void)gettimeofday(&ntime, NULL);
		looktime = ntime.tv_sec + delay2;
	}
	if (ntime.tv_sec >= looptime) {
		if (trace)
			fprintf(fd, "Looking for loops\n");
		for (ntp = nettab; ntp != NULL; ntp = ntp->next) {
		    if (ntp->status == MASTER) {
			to.tsp_type = TSP_LOOP;
			to.tsp_vers = TSPVERSION;
			to.tsp_seq = sequence++;
			to.tsp_hopcnt = MAX_HOPCNT;
			(void)strcpy(to.tsp_name, hostname);
			bytenetorder(&to);
			if (sendto(sock, (char *)&to, sizeof(struct tsp), 0,
				   (struct sockaddr*)&ntp->dest_addr,
				   sizeof(ntp->dest_addr)) < 0) {
				trace_sendto_err(ntp->dest_addr.sin_addr);
			}
		    }
		}
		(void)gettimeofday(&ntime, NULL);
		looptime = ntime.tv_sec + delay2;
	}

	wait.tv_sec = min(electiontime,min(looktime,looptime)) - ntime.tv_sec;
	if (wait.tv_sec < 0)
		wait.tv_sec = 0;
	wait.tv_sec += FASTTOUT;
	wait.tv_usec = 0;
	msg = readmsg(TSP_ANY, ANYADDR, &wait, 0);

	if (msg != NULL) {
		/*
		 * filter stuff not for us
		 */
		switch (msg->tsp_type) {
		case TSP_SETDATE:
		case TSP_TRACEOFF:
		case TSP_TRACEON:
			/*
			 * XXX check to see they are from ourself
			 */
			break;

		case TSP_TEST:
		case TSP_MSITE:
			break;

		case TSP_MASTERUP:
			if (!fromnet) {
				if (trace) {
					fprintf(fd, "slave ignored: ");
					print(msg, &from);
				}
				goto loop;
			}
			break;

		default:
			if (!fromnet
			    || fromnet->status == IGNORE
			    || fromnet->status == NOMASTER) {
				if (trace) {
					fprintf(fd, "slave ignored: ");
					print(msg, &from);
				}
				goto loop;
			}
			break;
		}


		/*
		 * now process the message
		 */
		switch (msg->tsp_type) {

		case TSP_ADJTIME:
			if (fromnet != slavenet)
				break;
			if (!good_host_name(msg->tsp_name)) {
				syslog(LOG_NOTICE,
				   "attempted time adjustment by %s",
				       msg->tsp_name);
				suppress(&from, msg->tsp_name, fromnet);
				break;
			}
			/*
			 * Speed up loop detection in case we have a loop.
			 * Otherwise the clocks can race until the loop
			 * is found.
			 */
			(void)gettimeofday(&otime, NULL);
			if (adjtime < otime.tv_sec)
				looptime -= (looptime-otime.tv_sec)/2 + 1;

			setmaster(msg);
			if (seq != msg->tsp_seq) {
				seq = msg->tsp_seq;
				synch(tvtomsround(msg->tsp_time));
			}
			(void)gettimeofday(&ntime, NULL);
			electiontime = ntime.tv_sec + delay2;
			fastelection = ntime.tv_sec + FASTTOUT;
			adjtime = ntime.tv_sec + SAMPLEINTVL*2;
			break;

		case TSP_SETTIME:
			if (fromnet != slavenet)
				break;
			if (seq == msg->tsp_seq)
				break;
			seq = msg->tsp_seq;

			/* adjust time for residence on the queue */
			(void)gettimeofday(&otime, NULL);
			adj_msg_time(msg,&otime);
			/*
			 * the following line is necessary due to syslog
			 * calling ctime() which clobbers the static buffer
			 */
			(void)strcpy(olddate, date());
			tsp_time_sec = msg->tsp_time.tv_sec;
			(void)strcpy(newdate, ctime(&tsp_time_sec));

			if (!good_host_name(msg->tsp_name)) {
				syslog(LOG_NOTICE,
			    "attempted time setting by untrusted %s to %s",
				       msg->tsp_name, newdate);
				suppress(&from, msg->tsp_name, fromnet);
				break;
			}

			setmaster(msg);
 			tmptv.tv_sec = msg->tsp_time.tv_sec;
 			tmptv.tv_usec = msg->tsp_time.tv_usec;
			timevalsub(&ntime, &tmptv, &otime);
			if (ntime.tv_sec < MAXADJ && ntime.tv_sec > -MAXADJ) {
				/*
				 * do not change the clock if we can adjust it
				 */
				synch(tvtomsround(ntime));
			} else {
				utx.ut_type = OLD_TIME;
				gettimeofday(&utx.ut_tv, NULL);
				pututxline(&utx);
				(void)settimeofday(&tmptv, 0);
				utx.ut_type = NEW_TIME;
				gettimeofday(&utx.ut_tv, NULL);
				pututxline(&utx);
				syslog(LOG_NOTICE,
				       "date changed by %s from %s",
					msg->tsp_name, olddate);
				if (status & MASTER)
					spreadtime();
			}
			(void)gettimeofday(&ntime, NULL);
			electiontime = ntime.tv_sec + delay2;
			fastelection = ntime.tv_sec + FASTTOUT;

/* This patches a bad protocol bug.  Imagine a system with several networks,
 * where there are a pair of redundant gateways between a pair of networks,
 * each running timed.  Assume that we start with a third machine mastering
 * one of the networks, and one of the gateways mastering the other.
 * Imagine that the third machine goes away and the non-master gateway
 * decides to replace it.  If things are timed just 'right,' we will have
 * each gateway mastering one network for a little while.  If a SETTIME
 * message gets into the network at that time, perhaps from the newly
 * masterful gateway as it was taking control, the SETTIME will loop
 * forever.  Each time a gateway receives it on its slave side, it will
 * call spreadtime to forward it on its mastered network.  We are now in
 * a permanent loop, since the SETTIME msgs will keep any clock
 * in the network from advancing.  Normally, the 'LOOP' stuff will detect
 * and correct the situation.  However, with the clocks stopped, the
 * 'looptime' timer cannot expire.  While they are in this state, the
 * masters will try to saturate the network with SETTIME packets.
 */
			looptime = ntime.tv_sec + (looptime-otime.tv_sec)/2-1;
			break;

		case TSP_MASTERUP:
			if (slavenet && fromnet != slavenet)
				break;
			if (!good_host_name(msg->tsp_name)) {
				suppress(&from, msg->tsp_name, fromnet);
				if (electiontime > fastelection)
					electiontime = fastelection;
				break;
			}
			makeslave(fromnet);
			setmaster(msg);
			setstatus();
			answerdelay();
			xmit(TSP_SLAVEUP, 0, &from);
			(void)gettimeofday(&ntime, NULL);
			electiontime = ntime.tv_sec + delay2;
			fastelection = ntime.tv_sec + FASTTOUT;
			refusetime = 0;
			break;

		case TSP_MASTERREQ:
			if (fromnet->status != SLAVE)
				break;
			(void)gettimeofday(&ntime, NULL);
			electiontime = ntime.tv_sec + delay2;
			break;

		case TSP_SETDATE:
			tsp_time_sec = msg->tsp_time.tv_sec;
			(void)strcpy(newdate, ctime(&tsp_time_sec));
			schgdate(msg, newdate);
			break;

		case TSP_SETDATEREQ:
			if (fromnet->status != MASTER)
				break;
			tsp_time_sec = msg->tsp_time.tv_sec;
			(void)strcpy(newdate, ctime(&tsp_time_sec));
			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, NULL);
				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, NULL);
				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, NULL);
			    looptime = ntime.tv_sec + FASTTOUT;
			}
			break;
		default:
			if (trace) {
				fprintf(fd, "garbage message: ");
				print(msg, &from);
			}
			break;
		}
	}
	goto loop;
}
Exemple #18
0
/* NOTE: If this changes, code in client.cpp must change - pause() and restart() use
   pre-encrypted values of 33 00 and 33 01.
   */
void Client::transmit_encrypted( const void* data, int len )
{
  THREAD_CHECKPOINT( active_client, 100 );
  const unsigned char* cdata = (const unsigned char*)data;
  unsigned char* pch;
  int i;
  int bidx;  // Offset in output byte
  EncryptedPktBuffer* outbuffer =
      PktHelper::RequestPacket<EncryptedPktBuffer>( ENCRYPTEDPKTBUFFER );
  pch = reinterpret_cast<unsigned char*>( outbuffer->getBuffer() );
  bidx = 0;
  THREAD_CHECKPOINT( active_client, 101 );
  for ( i = 0; i < len; i++ )
  {
    THREAD_CHECKPOINT( active_client, 102 );
    unsigned char ch = cdata[i];
    int nbits = Core::keydesc[ch].nbits;
    unsigned short inval = Core::keydesc[ch].bits_reversed;

    THREAD_CHECKPOINT( active_client, 103 );

    while ( nbits-- )
    {
      THREAD_CHECKPOINT( active_client, 104 );
      *pch <<= 1;
      if ( inval & 1 )
        *pch |= 1;
      bidx++;
      if ( bidx == 8 )
      {
        THREAD_CHECKPOINT( active_client, 105 );
        pch++;
        bidx = 0;
      }
      THREAD_CHECKPOINT( active_client, 106 );

      inval >>= 1;
    }
    THREAD_CHECKPOINT( active_client, 107 );
  }
  THREAD_CHECKPOINT( active_client, 108 );

  {
    int nbits = Core::keydesc[0x100].nbits;
    unsigned short inval = Core::keydesc[0x100].bits_reversed;

    THREAD_CHECKPOINT( active_client, 109 );

    while ( nbits-- )
    {
      THREAD_CHECKPOINT( active_client, 110 );
      *pch <<= 1;
      if ( inval & 1 )
        *pch |= 1;
      bidx++;
      THREAD_CHECKPOINT( active_client, 111 );
      if ( bidx == 8 )
      {
        pch++;
        bidx = 0;
      }
      THREAD_CHECKPOINT( active_client, 112 );

      inval >>= 1;
    }
  }
  THREAD_CHECKPOINT( active_client, 113 );

  if ( bidx == 0 )
  {
    pch--;
  }
  else
  {
    *pch <<= ( 8 - bidx );
  }
  THREAD_CHECKPOINT( active_client, 114 );

  passert_always( pch - reinterpret_cast<unsigned char*>( outbuffer->buffer ) + 1 <=
                  int( sizeof outbuffer->buffer ) );
  THREAD_CHECKPOINT( active_client, 115 );
  xmit( &outbuffer->buffer, static_cast<unsigned short>(
                                pch - reinterpret_cast<unsigned char*>( outbuffer->buffer ) + 1 ) );
  PktHelper::ReAddPacket( outbuffer );
  THREAD_CHECKPOINT( active_client, 116 );
}
Exemple #19
0
static int8_t sendchar_func(uint8_t c)
{
    xmit(c);        // SUART
    sendchar(c);    // LUFA
    return 0;
}
Exemple #20
0
/*
 * The main function of `master' is to periodically compute the differences
 * (deltas) between its clock and the clocks of the slaves, to compute the
 * network average delta, and to send to the slaves the differences between
 * their individual deltas and the network delta.
 * While waiting, it receives messages from the slaves (i.e. requests for
 * master's name, remote requests to set the network time, ...), and
 * takes the appropriate action.
 */
int
master()
{
	struct hosttbl *htp;
	long pollingtime;
#define POLLRATE 4
	int polls;
	struct timeval wait, ntime;
	time_t tsp_time_sec;
	struct tsp *msg, *answer, to;
	char newdate[32];
	struct sockaddr_in taddr;
	char tname[MAXHOSTNAMELEN];
	struct netinfo *ntp;
	int i;

	syslog(LOG_NOTICE, "This machine is master");
	if (trace)
		fprintf(fd, "This machine is master\n");
	for (ntp = nettab; ntp != NULL; ntp = ntp->next) {
		if (ntp->status == MASTER)
			masterup(ntp);
	}
	(void)gettimeofday(&ntime, 0);
	pollingtime = ntime.tv_sec+3;
	if (justquit)
		polls = 0;
	else
		polls = POLLRATE-1;

/* Process all outstanding messages before spending the long time necessary
 *	to update all timers.
 */
loop:
	(void)gettimeofday(&ntime, 0);
	wait.tv_sec = pollingtime - ntime.tv_sec;
	if (wait.tv_sec < 0)
		wait.tv_sec = 0;
	wait.tv_usec = 0;
	msg = readmsg(TSP_ANY, ANYADDR, &wait, 0);
	if (!msg) {
		(void)gettimeofday(&ntime, 0);
		if (ntime.tv_sec >= pollingtime) {
			pollingtime = ntime.tv_sec + SAMPLEINTVL;
			get_goodgroup(0);

/* If a bogus master told us to quit, we can have decided to ignore a
 * network.  Therefore, periodically try to take over everything.
 */
			polls = (polls + 1) % POLLRATE;
			if (0 == polls && nignorednets > 0) {
				trace_msg("Looking for nets to re-master\n");
				for (ntp = nettab; ntp; ntp = ntp->next) {
					if (ntp->status == IGNORE
					    || ntp->status == NOMASTER) {
						lookformaster(ntp);
						if (ntp->status == MASTER) {
							masterup(ntp);
							polls = POLLRATE-1;
						}
					}
					if (ntp->status == MASTER
					    && --ntp->quit_count < 0)
						ntp->quit_count = 0;
				}
				if (polls != 0)
					setstatus();
			}

			synch(0L);

			for (ntp = nettab; ntp != NULL; ntp = ntp->next) {
				to.tsp_type = TSP_LOOP;
				to.tsp_vers = TSPVERSION;
				to.tsp_seq = sequence++;
				to.tsp_hopcnt = MAX_HOPCNT;
				(void)strcpy(to.tsp_name, hostname);
				bytenetorder(&to);
				if (sendto(sock, (char *)&to,
					   sizeof(struct tsp), 0,
					   (struct sockaddr*)&ntp->dest_addr,
					   sizeof(ntp->dest_addr)) < 0) {
				   trace_sendto_err(ntp->dest_addr.sin_addr);
				}
			}
		}


	} else {
		switch (msg->tsp_type) {

		case TSP_MASTERREQ:
			break;

		case TSP_SLAVEUP:
			newslave(msg);
			break;

		case TSP_SETDATE:
			/*
			 * XXX check to see it is from ourself
			 */
			tsp_time_sec = msg->tsp_time.tv_sec;
			(void)strcpy(newdate, ctime(&tsp_time_sec));
			if (!good_host_name(msg->tsp_name)) {
				syslog(LOG_NOTICE,
				       "attempted date change by %s to %s",
				       msg->tsp_name, newdate);
				spreadtime();
				break;
			}

			mchgdate(msg);
			(void)gettimeofday(&ntime, 0);
			pollingtime = ntime.tv_sec + SAMPLEINTVL;
			break;

		case TSP_SETDATEREQ:
			if (!fromnet || fromnet->status != MASTER)
				break;
			tsp_time_sec = msg->tsp_time.tv_sec;
			(void)strcpy(newdate, ctime(&tsp_time_sec));
			htp = findhost(msg->tsp_name);
			if (htp == 0) {
				syslog(LOG_ERR,
				       "attempted SET DATEREQ by uncontrolled %s to %s",
				       msg->tsp_name, newdate);
				break;
			}
			if (htp->seq == msg->tsp_seq)
				break;
			htp->seq = msg->tsp_seq;
			if (!htp->good) {
				syslog(LOG_NOTICE,
				"attempted SET DATEREQ by untrusted %s to %s",
				       msg->tsp_name, newdate);
				spreadtime();
				break;
			}

			mchgdate(msg);
			(void)gettimeofday(&ntime, 0);
			pollingtime = ntime.tv_sec + SAMPLEINTVL;
			break;

		case TSP_MSITE:
			xmit(TSP_ACK, msg->tsp_seq, &from);
			break;

		case TSP_MSITEREQ:
			break;

		case TSP_TRACEON:
			traceon();
			break;

		case TSP_TRACEOFF:
			traceoff("Tracing ended at %s\n");
			break;

		case TSP_ELECTION:
			if (!fromnet)
				break;
			if (fromnet->status == MASTER) {
				pollingtime = 0;
				(void)addmach(msg->tsp_name, &from,fromnet);
			}
			taddr = from;
			(void)strcpy(tname, msg->tsp_name);
			to.tsp_type = TSP_QUIT;
			(void)strcpy(to.tsp_name, hostname);
			answer = acksend(&to, &taddr, tname,
					 TSP_ACK, 0, 1);
			if (answer == NULL) {
				syslog(LOG_ERR, "election error by %s",
				       tname);
			}
			break;

		case TSP_CONFLICT:
			/*
			 * After a network partition, there can be
			 * more than one master: the first slave to
			 * come up will notify here the situation.
			 */
			if (!fromnet || fromnet->status != MASTER)
				break;
			(void)strcpy(to.tsp_name, hostname);

			/* The other master often gets into the same state,
			 * with boring results if we stay at it forever.
			 */
			ntp = fromnet;	/* (acksend() can leave fromnet=0 */
			for (i = 0; i < 3; i++) {
				to.tsp_type = TSP_RESOLVE;
				(void)strcpy(to.tsp_name, hostname);
				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;
				msg = acksend(&to, &htp->addr, htp->name,
					      TSP_ACK, 0, htp->noanswer);
				if (msg == NULL) {
					syslog(LOG_ERR,
				    "no response from %s to CONFLICT-QUIT",
					       htp->name);
				}
			}
			masterup(ntp);
			pollingtime = 0;
			break;

		case TSP_RESOLVE:
			if (!fromnet || fromnet->status != MASTER)
				break;
			/*
			 * do not want to call synch() while waiting
			 * to be killed!
			 */
			(void)gettimeofday(&ntime, (struct timezone *)0);
			pollingtime = ntime.tv_sec + SAMPLEINTVL;
			break;

		case TSP_QUIT:
			doquit(msg);		/* become a slave */
			break;

		case TSP_LOOP:
			if (!fromnet || fromnet->status != MASTER
			    || !strcmp(msg->tsp_name, hostname))
				break;
			/*
			 * We should not have received this from a net
			 * we are master on.  There must be two masters.
			 */
			htp = addmach(msg->tsp_name, &from,fromnet);
			to.tsp_type = TSP_QUIT;
			(void)strcpy(to.tsp_name, hostname);
			answer = acksend(&to, &htp->addr, htp->name,
					 TSP_ACK, 0, 1);
			if (!answer) {
				syslog(LOG_WARNING,
				"loop breakage: no reply from %s=%s to QUIT",
				    htp->name, inet_ntoa(htp->addr.sin_addr));
				(void)remmach(htp);
			}

		case TSP_TEST:
			if (trace) {
				fprintf(fd,
		"\tnets = %d, masters = %d, slaves = %d, ignored = %d\n",
		nnets, nmasternets, nslavenets, nignorednets);
				setstatus();
			}
			pollingtime = 0;
			polls = POLLRATE-1;
			break;

		default:
			if (trace) {
				fprintf(fd, "garbage message: ");
				print(msg, &from);
			}
			break;
		}
	}
	goto loop;
}
Exemple #21
0
void Client::transmit( const void* data, int len, bool needslock )
{
  ref_ptr<Core::BPacket> p;
  bool handled = false;
  // see if the outgoing packet has a SendFunction installed. If so call it. It may or may not
  // want us to continue sending the packet. If it does, handled will be false, and data, len, and p
  // will be altered. data has the new packet data to send, len the new length, and p, a ref counted
  // pointer to the packet object.
  //
  // If there is no outgoing packet script, handled will be false, and the passed params will be
  // unchanged.
  {
    PacketHookData* phd = nullptr;
    handled = GetAndCheckPacketHooked( this, data, phd );
    if ( handled )
    {
      if ( needslock )
      {
        Core::PolLock lock;
        std::lock_guard<std::mutex> guard( _SocketMutex );
        CallOutgoingPacketExportedFunction( this, data, len, p, phd, handled );
      }
      else
      {
        std::lock_guard<std::mutex> guard( _SocketMutex );
        CallOutgoingPacketExportedFunction( this, data, len, p, phd, handled );
      }
    }
  }

  if ( handled )
    return;

  unsigned char msgtype = *(const char*)data;

  {
    Clib::SpinLockGuard guard( _fpLog_lock );
    if ( !fpLog.empty() )
    {
      fmt::Writer tmp;
      tmp << "Server -> Client: 0x" << fmt::hexu( msgtype ) << ", " << len << " bytes\n";
      Clib::fdump( tmp, data, len );
      FLEXLOG( fpLog ) << tmp.str() << "\n";
    }
  }

  std::lock_guard<std::mutex> guard( _SocketMutex );
  if ( disconnect )
  {
    POLLOG_INFO << "Warning: Trying to send to a disconnected client! \n";
    fmt::Writer tmp;
    tmp << "Server -> Client: 0x" << fmt::hexu( msgtype ) << ", " << len << " bytes\n";
    Clib::fdump( tmp, data, len );
    POLLOG_INFO << tmp.str() << "\n";
    return;
  }

  if ( last_xmit_buffer )
  {
    Core::networkManager.queuedmode_iostats.sent[msgtype].count++;
    Core::networkManager.queuedmode_iostats.sent[msgtype].bytes += len;
  }
  Core::networkManager.iostats.sent[msgtype].count++;
  Core::networkManager.iostats.sent[msgtype].bytes += len;

  if ( encrypt_server_stream )
  {
    pause();
    transmit_encrypted( data, len );
  }
  else
  {
    xmit( data, static_cast<unsigned short>( len ) );
    // _xmit( client->csocket, data, len );
  }
}
Exemple #22
0
void xmitstr_p(const char *s)
{
    char c;
    while ((c=pgm_read_byte(s++))!=0) xmit(c);
}