v14i075: terminal server program for terminal servers

uucp at relay.eu.net uucp at relay.eu.net
Fri Aug 31 10:28:15 AEST 1990


Posting-number: Volume 14, Issue 75
Submitted-by: uucp at relay.eu.net@@uunet.uu.net:tuvie.UUCP
Archive-name: termserv/part01

I wrote that little program, because in out office there is a need
to determine the real terminal of a user(terminal-type) and all
out terminals are connected to our hosts via ethernet and terminal servers'
So, coming thru a telnetd it's not easy to determine the terminal type

OK, that is our solution of that problem. 

BTW:  our name of the program is termserv, see the code for description

Pleschutznig Andreas
Bel Computer Systems
(andy at mssx)

-----------CUT HERE--------

#! /bin/sh
# This file was wrapped with "dummyshar".  "sh" this file to extract.
# Contents:  termserv.c
echo extracting 'termserv.c'
if test -f 'termserv.c' -a -z "$1"; then echo Not overwriting 'termserv.c'; else
sed 's/^X//' << \EOF > 'termserv.c'
X#include <stdio.h>
X#include <fcntl.h>
X#include <sys/signal.h>
X#include <sys/types.h>
X#include <sys/socket.h>
X#include <netinet/in.h>
X#include <netdb.h>
X
X/* for systems that don't have a modern select() */
X#ifndef FD_SETSIZE
X#define FD_SETSIZE	NOFILE
X#endif 
X
Xstruct servent *getservbyname();
X
X
Xvoid
Xcatch(sig)
Xint sig;
X{
X	fprintf(stderr,"termserv: catched signal %d\n",sig);
X	fprintf(stderr,"Exiting ...\n");
X}
X
Xmain(argc, argv)
Xint argc;
Xchar *argv[];
X{
X	char buffer[1024];
X	register int sfd, len;
X	struct sockaddr_in inaddr;
X	struct hostent *hp;
X	struct servent *service;
X	char 	buf;
X	int	inout;
X	int 	sigs;
X
X	/* catch all types of signals before exit */
X	for ( sigs = 0; sigs < NSIG; sigs++)
X		signal(sigs, catch);
X
X	/* check number of arguments */
X	if (argc != 4) {
X		perror(argv[0]);
X		exit(1);
X	}
X
X	/* create a socket */
X	if ((sfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
X		fprintf(stderr, "%s: can't open socket\n", argv[0]);
X		exit(2);
X	}
X
X	/* resolve network address of NCD unit */
X	inaddr.sin_family = AF_INET;
X	if ((service = getservbyname(argv[2],"tcp")) == NULL)
X		{
X		fprintf(stderr,"%s: unable to get service %s\n",argv[0],argv[2]);
X		exit(1);
X		}
X#ifdef DEBUG
Xfprintf(stderr,"Service Port ID: %d\n",ntohs(service->s_port));
X#endif
X	inaddr.sin_port = service->s_port;
X	if ((inaddr.sin_addr.s_addr = inet_addr(argv[1])) == -1) {
X		if ((hp = gethostbyname(argv[1])) == NULL
X		    || hp->h_addrtype != AF_INET) {
X			fprintf(stderr, "%s: can't resolve name %s\n", 
X				argv[0], argv[1]);
X			exit(3);
X		}
X		bcopy(hp->h_addr, &inaddr.sin_addr.s_addr, hp->h_length);
X	}
X
X	/* connect to NCD RS-232 daemon */
X	if (connect(sfd, &inaddr, sizeof(inaddr)) < 0) {
X		perror(argv[0]);
X		exit(4);
X	}
X	
X	inout= open(argv[3],O_RDWR);
X	if (inout < 0) {
X		fprintf(stderr,"%s can't open %s for communication",argv[0],argv[3]);
X		perror(argv[0]);
X		exit(5);
X		}
X
X	close(0);
X	if (dup(inout) != 0) {
X		perror(argv[0]);
X		exit(6);
X		}
X#ifdef DEBUG
Xfprintf(stderr,"stdin redirected\n");
X#endif
X	close(1);
X	if (dup(inout) != 1) {
X		perror(argv[0]);
X		exit(7);
X		}
X#ifdef DEBUG
Xfprintf(stderr,"stdout redirected\n");
X#endif
X
X	/* 
X	 * do bi-directional I/O until EOF or error 
X	 *
X	 * The actions on EOF and error should be customized
X	 * for the specific device and application. For example,
X	 * one might want to continue reading from the network,
X	 * even after EOF has been reached on stdin.
X	 */
X	while (1) {
X		fd_set rfds;
X
X		FD_ZERO(&rfds);
X		FD_SET(sfd, &rfds);
X		FD_SET(0, &rfds);
X		if (select(FD_SETSIZE, &rfds, NULL, NULL, NULL) < 0)
X			break;
X
X		/* read stdin, write output to network connection */
X		if (FD_ISSET(0, &rfds)) {
X			if ((len = read(0, buffer, sizeof(buffer))) > 0) 
X				write(sfd, buffer, len);
X
X/*			else EOF or error
X				break;
X*/
X		}
X
X		/*
X		 * read any messages from the network, and write them to stdout
X		 *
X		 * Some printers, such as Laserwriters send back logging info.
X		 */
X		if (FD_ISSET(sfd, &rfds)) 
X			{
X			if ((len = read(sfd, &buf, 1)) > 0)
X				if ( buf != '\0' ) 
X				{ 
X					write(1, &buf, 1  );
X#ifdef DEBUG
X	fprintf(stderr,"%02x\n",(int)buf);
X#endif
X				}
X/*
X			else EOF or error
X				break;
X*/
X		}
X	}
X
X	/* clean up */
X	exit(0);
X}
EOF
chars=`wc -c < 'termserv.c'`
if test $chars !=     3163; then echo 'termserv.c' is $chars characters, should be     3163 characters!; fi
fi
exit 0



More information about the Comp.sources.misc mailing list