Path: gmd.de!xlink.net!howland.reston.ans.net!vixen.cso.uiuc.edu!sdd.hp.com!
caen!msuinfo!harbinger.cc.monash.edu.au!yarrina.connect.com.au!
werple.apana.org.au!suburbia!proff
From: pr...@suburbia.apana.org.au (Julian Assange)
Newsgroups: alt.sources
Subject: passthru.c - termios based tty<->tty linker.
Date: 8 Jan 94 14:16:11 GMT
Organization: werple public-access unix, Melbourne
Lines: 101
Message-ID: <proff.758038571@suburbia>
NNTP-Posting-Host: suburbia.apana.org.au
Summary: small 'c' program to create a virtual connection between two tty's
Keywords: passthru, tty, link, virtual
X-Newsreader: NN version 6.5.0 #3


Just a quick hack, however it does the job it was designed for, namely to
connect two tty's together transparently. Works under linux, and should
work with most POSIX compliant systems. I'd appreciate a copy of any
ports to other OS's.

// cut here //


/* (c) 1993 Julian Assage (pr...@suburbia.apana.org.au)
 * All rights reserved, except that this program and its source code
 * maybe distrubuted freely and without profit in an unmodified form.
 */

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <fcntl.h>
#include <termios.h>
  
struct termios term;
struct termios saveterm;
int out;

char catch[]="quitpassthru";

void hand()
{
	tcsetattr(0, TCSAFLUSH, &saveterm);
	close(out);
	exit(0);
}

main(int argc, char **argv)
{
  register int n;
  register int cn=0;
  unsigned char buf[2048];
  int cc;
	if (argc!=2) {
		fprintf(stderr, "Usage: %s output-file.\n\n\
Example: %s /dev/ttyS1  -  route all input to user on /dev/ttyS1.\n\n\
Quit Sequence = \"%s\".\n\
\n\
This program redirects all input to the specified output, passing any and\n\
all special characters (e.g ^c ^z ^q ^s), NULLS, and the 8th bit to the\n\
specified output, which may be a file or a tty. Typically the program is\n\
used to form a virtual 8bit rs232 connection between two users/ttys. This\n\
allows the users to use the virtual link for such things as direct zmodem\n\
transfers, multi-player serial games, term/slap connections, etc. Send \n\
bug reports to pr...@suburbia.apana.org.au.\n",
			argv[0], argv[0], catch);
		exit(1);
	}
	if (!isatty(0)) {
		fprintf(stderr, "Must be executed from a real tty\n");
		exit(1);
	}
	if ((out=open(argv[1], O_WRONLY|O_CREAT, 0777))<0) {
		perror(argv[1]);
		exit(1);
	}
	signal(SIGINT, hand);
	signal(SIGHUP, hand);
	tcgetattr(0, &term);
	tcgetattr(0, &saveterm);
	term.c_iflag&=~(INPCK|IXOFF|IXON);
	term.c_iflag|=BRKINT;
	term.c_oflag&=~OPOST;
	term.c_cflag&=~(PARENB|CSIZE);
	term.c_cflag|=CS8;
	term.c_lflag&=~(ICANON|ISIG|IEXTEN|TOSTOP|ECHO);
        term.c_cc[VMIN] = 1;
        term.c_cc[VTIME] = 2; /** 1/5 th of a second */
	tcsetattr(0, TCSAFLUSH, &term);

	cn = 0;

	while ((cc=read(0, buf, sizeof(buf)))>-1)
	{
		if (cc)
		{
			for (n=0;n<cc; n++)
			{
				if (buf[n]==catch[cn])
				{
					if (++cn==(sizeof(catch)-1)) hand();
                    		}
				else
					cn=0;
				}
			}
			write(out, buf, cc);
		}
	exit(1);
}
--
-------------------------------------------------------------------------------
                            suburbia.apana.org.au 
    Suburbia Public Access Unix - 24 Hr Internet Connection - +61 3 596 8366
===============================================================================