#include "telnet_locl.h"
RCSID("$Id$");
int
tout,
tin,
net;
struct termios old_tc = { 0 };
extern struct termios new_tc;
# ifndef TCSANOW
# ifdef TCSETS
# define TCSANOW TCSETS
# define TCSADRAIN TCSETSW
# define tcgetattr(f, t) ioctl(f, TCGETS, (char *)t)
# else
# ifdef TCSETA
# define TCSANOW TCSETA
# define TCSADRAIN TCSETAW
# define tcgetattr(f, t) ioctl(f, TCGETA, (char *)t)
# else
# define TCSANOW TIOCSETA
# define TCSADRAIN TIOCSETAW
# define tcgetattr(f, t) ioctl(f, TIOCGETA, (char *)t)
# endif
# endif
# define tcsetattr(f, a, t) ioctl(f, a, (char *)t)
# define cfgetospeed(ptr) ((ptr)->c_cflag&CBAUD)
# ifdef CIBAUD
# define cfgetispeed(ptr) (((ptr)->c_cflag&CIBAUD) >> IBSHIFT)
# else
# define cfgetispeed(ptr) cfgetospeed(ptr)
# endif
# endif
static fd_set ibits, obits, xbits;
void
init_sys(void)
{
tout = fileno(stdout);
tin = fileno(stdin);
FD_ZERO(&ibits);
FD_ZERO(&obits);
FD_ZERO(&xbits);
errno = 0;
}
int
TerminalWrite(char *buf, int n)
{
return write(tout, buf, n);
}
int
TerminalRead(unsigned char *buf, int n)
{
return read(tin, buf, n);
}
int
TerminalAutoFlush(void)
{
#if defined(LNOFLSH)
int flush;
ioctl(0, TIOCLGET, (char *)&flush);
return !(flush&LNOFLSH);
#else
return 1;
#endif
}
int
TerminalSpecialChars(int c)
{
if (c == termIntChar) {
intp();
return 0;
} else if (c == termQuitChar) {
#ifdef KLUDGELINEMODE
if (kludgelinemode)
sendbrk();
else
#endif
sendabort();
return 0;
} else if (c == termEofChar) {
if (my_want_state_is_will(TELOPT_LINEMODE)) {
sendeof();
return 0;
}
return 1;
} else if (c == termSuspChar) {
sendsusp();
return(0);
} else if (c == termFlushChar) {
xmitAO();
return 0;
} else if (!MODE_LOCAL_CHARS(globalmode)) {
if (c == termKillChar) {
xmitEL();
return 0;
} else if (c == termEraseChar) {
xmitEC();
return 0;
}
}
return 1;
}
void
TerminalFlushOutput(void)
{
#ifdef TIOCFLUSH
ioctl(fileno(stdout), TIOCFLUSH, (char *) 0);
#else
ioctl(fileno(stdout), TCFLSH, (char *) 0);
#endif
}
void
TerminalSaveState(void)
{
tcgetattr(0, &old_tc);
new_tc = old_tc;
#ifndef VDISCARD
termFlushChar = CONTROL('O');
#endif
#ifndef VWERASE
termWerasChar = CONTROL('W');
#endif
#ifndef VREPRINT
termRprntChar = CONTROL('R');
#endif
#ifndef VLNEXT
termLiteralNextChar = CONTROL('V');
#endif
#ifndef VSTART
termStartChar = CONTROL('Q');
#endif
#ifndef VSTOP
termStopChar = CONTROL('S');
#endif
#ifndef VSTATUS
termAytChar = CONTROL('T');
#endif
}
cc_t*
tcval(int func)
{
switch(func) {
case SLC_IP: return(&termIntChar);
case SLC_ABORT: return(&termQuitChar);
case SLC_EOF: return(&termEofChar);
case SLC_EC: return(&termEraseChar);
case SLC_EL: return(&termKillChar);
case SLC_XON: return(&termStartChar);
case SLC_XOFF: return(&termStopChar);
case SLC_FORW1: return(&termForw1Char);
case SLC_FORW2: return(&termForw2Char);
# ifdef VDISCARD
case SLC_AO: return(&termFlushChar);
# endif
# ifdef VSUSP
case SLC_SUSP: return(&termSuspChar);
# endif
# ifdef VWERASE
case SLC_EW: return(&termWerasChar);
# endif
# ifdef VREPRINT
case SLC_RP: return(&termRprntChar);
# endif
# ifdef VLNEXT
case SLC_LNEXT: return(&termLiteralNextChar);
# endif
# ifdef VSTATUS
case SLC_AYT: return(&termAytChar);
# endif
case SLC_SYNCH:
case SLC_BRK:
case SLC_EOR:
default:
return((cc_t *)0);
}
}
void
TerminalDefaultChars(void)
{
memmove(new_tc.c_cc, old_tc.c_cc, sizeof(old_tc.c_cc));
# ifndef VDISCARD
termFlushChar = CONTROL('O');
# endif
# ifndef VWERASE
termWerasChar = CONTROL('W');
# endif
# ifndef VREPRINT
termRprntChar = CONTROL('R');
# endif
# ifndef VLNEXT
termLiteralNextChar = CONTROL('V');
# endif
# ifndef VSTART
termStartChar = CONTROL('Q');
# endif
# ifndef VSTOP
termStopChar = CONTROL('S');
# endif
# ifndef VSTATUS
termAytChar = CONTROL('T');
# endif
}
#ifdef notdef
void
TerminalRestoreState()
{
}
#endif
#ifdef SIGTSTP
static RETSIGTYPE susp(int);
#endif
#ifdef SIGINFO
static RETSIGTYPE ayt(int);
#endif
void
TerminalNewMode(int f)
{
static int prevmode = 0;
struct termios tmp_tc;
int onoff;
int old;
cc_t esc;
globalmode = f&~MODE_FORCE;
if (prevmode == f)
return;
old = ttyflush(SYNCHing|flushout);
if (old < 0 || old > 1) {
tcgetattr(tin, &tmp_tc);
do {
tcsetattr(tin, TCSADRAIN, &tmp_tc);
old = ttyflush(SYNCHing|flushout);
} while (old < 0 || old > 1);
}
old = prevmode;
prevmode = f&~MODE_FORCE;
tmp_tc = new_tc;
if (f&MODE_ECHO) {
tmp_tc.c_lflag |= ECHO;
tmp_tc.c_oflag |= ONLCR;
if (crlf)
tmp_tc.c_iflag |= ICRNL;
} else {
tmp_tc.c_lflag &= ~ECHO;
tmp_tc.c_oflag &= ~ONLCR;
# ifdef notdef
if (crlf)
tmp_tc.c_iflag &= ~ICRNL;
# endif
}
if ((f&MODE_FLOW) == 0) {
tmp_tc.c_iflag &= ~(IXOFF|IXON);
} else {
if (restartany < 0) {
tmp_tc.c_iflag |= IXOFF|IXON;
} else if (restartany > 0) {
tmp_tc.c_iflag |= IXOFF|IXON|IXANY;
} else {
tmp_tc.c_iflag |= IXOFF|IXON;
tmp_tc.c_iflag &= ~IXANY;
}
}
if ((f&MODE_TRAPSIG) == 0) {
tmp_tc.c_lflag &= ~ISIG;
localchars = 0;
} else {
tmp_tc.c_lflag |= ISIG;
localchars = 1;
}
if (f&MODE_EDIT) {
tmp_tc.c_lflag |= ICANON;
} else {
tmp_tc.c_lflag &= ~ICANON;
tmp_tc.c_iflag &= ~ICRNL;
tmp_tc.c_cc[VMIN] = 1;
tmp_tc.c_cc[VTIME] = 0;
}
if ((f&(MODE_EDIT|MODE_TRAPSIG)) == 0) {
# ifdef VLNEXT
tmp_tc.c_cc[VLNEXT] = (cc_t)(_POSIX_VDISABLE);
# endif
}
if (f&MODE_SOFT_TAB) {
# ifdef OXTABS
tmp_tc.c_oflag |= OXTABS;
# endif
# ifdef TABDLY
tmp_tc.c_oflag &= ~TABDLY;
tmp_tc.c_oflag |= TAB3;
# endif
} else {
# ifdef OXTABS
tmp_tc.c_oflag &= ~OXTABS;
# endif
# ifdef TABDLY
tmp_tc.c_oflag &= ~TABDLY;
# endif
}
if (f&MODE_LIT_ECHO) {
# ifdef ECHOCTL
tmp_tc.c_lflag &= ~ECHOCTL;
# endif
} else {
# ifdef ECHOCTL
tmp_tc.c_lflag |= ECHOCTL;
# endif
}
if (f == -1) {
onoff = 0;
} else {
if (f & MODE_INBIN)
tmp_tc.c_iflag &= ~ISTRIP;
else
tmp_tc.c_iflag |= ISTRIP;
if ((f & MODE_OUTBIN) || (f & MODE_OUT8)) {
tmp_tc.c_cflag &= ~(CSIZE|PARENB);
tmp_tc.c_cflag |= CS8;
if(f & MODE_OUTBIN)
tmp_tc.c_oflag &= ~OPOST;
else
tmp_tc.c_oflag |= OPOST;
} else {
tmp_tc.c_cflag &= ~(CSIZE|PARENB);
tmp_tc.c_cflag |= old_tc.c_cflag & (CSIZE|PARENB);
tmp_tc.c_oflag |= OPOST;
}
onoff = 1;
}
if (f != -1) {
#ifdef SIGTSTP
signal(SIGTSTP, susp);
#endif
#ifdef SIGINFO
signal(SIGINFO, ayt);
#endif
#ifdef NOKERNINFO
tmp_tc.c_lflag |= NOKERNINFO;
#endif
# ifdef VDSUSP
tmp_tc.c_cc[VDSUSP] = (cc_t)(_POSIX_VDISABLE);
# endif
esc = (rlogin != _POSIX_VDISABLE) ? rlogin : escape;
if ((tmp_tc.c_cc[VEOL] != esc)
# ifdef VEOL2
&& (tmp_tc.c_cc[VEOL2] != esc)
# endif
) {
if (tmp_tc.c_cc[VEOL] == (cc_t)(_POSIX_VDISABLE))
tmp_tc.c_cc[VEOL] = esc;
# ifdef VEOL2
else if (tmp_tc.c_cc[VEOL2] == (cc_t)(_POSIX_VDISABLE))
tmp_tc.c_cc[VEOL2] = esc;
# endif
}
} else {
sigset_t sm;
#ifdef SIGINFO
signal(SIGINFO, ayt_status);
#endif
#ifdef SIGTSTP
signal(SIGTSTP, SIG_DFL);
sigemptyset(&sm);
sigaddset(&sm, SIGTSTP);
sigprocmask(SIG_UNBLOCK, &sm, NULL);
#endif
tmp_tc = old_tc;
}
if (tcsetattr(tin, TCSADRAIN, &tmp_tc) < 0)
tcsetattr(tin, TCSANOW, &tmp_tc);
ioctl(tin, FIONBIO, (char *)&onoff);
ioctl(tout, FIONBIO, (char *)&onoff);
}
#if B4800 != 4800
#define DECODE_BAUD
#endif
#ifdef DECODE_BAUD
#ifndef B7200
#define B7200 B4800
#endif
#ifndef B14400
#define B14400 B9600
#endif
#ifndef B19200
# define B19200 B14400
#endif
#ifndef B28800
#define B28800 B19200
#endif
#ifndef B38400
# define B38400 B28800
#endif
#ifndef B57600
#define B57600 B38400
#endif
#ifndef B76800
#define B76800 B57600
#endif
#ifndef B115200
#define B115200 B76800
#endif
#ifndef B230400
#define B230400 B115200
#endif
struct termspeeds {
long speed;
long value;
} termspeeds[] = {
{ 0, B0 }, { 50, B50 }, { 75, B75 },
{ 110, B110 }, { 134, B134 }, { 150, B150 },
{ 200, B200 }, { 300, B300 }, { 600, B600 },
{ 1200, B1200 }, { 1800, B1800 }, { 2400, B2400 },
{ 4800, B4800 }, { 7200, B7200 }, { 9600, B9600 },
{ 14400, B14400 }, { 19200, B19200 }, { 28800, B28800 },
{ 38400, B38400 }, { 57600, B57600 }, { 115200, B115200 },
{ 230400, B230400 }, { -1, B230400 }
};
#endif
void
TerminalSpeeds(long *input_speed, long *output_speed)
{
#ifdef DECODE_BAUD
struct termspeeds *tp;
#endif
long in, out;
out = cfgetospeed(&old_tc);
in = cfgetispeed(&old_tc);
if (in == 0)
in = out;
#ifdef DECODE_BAUD
tp = termspeeds;
while ((tp->speed != -1) && (tp->value < in))
tp++;
*input_speed = tp->speed;
tp = termspeeds;
while ((tp->speed != -1) && (tp->value < out))
tp++;
*output_speed = tp->speed;
#else
*input_speed = in;
*output_speed = out;
#endif
}
int
TerminalWindowSize(long *rows, long *cols)
{
int irows, icols;
if (rk_get_window_size(STDIN_FILENO, &irows, &icols) == 0) {
*rows = irows;
*cols = icols;
return 1;
} else
return 0;
}
int
NetClose(int fd)
{
return close(fd);
}
void
NetNonblockingIO(int fd, int onoff)
{
ioctl(fd, FIONBIO, (char *)&onoff);
}
static RETSIGTYPE deadpeer(int),
intr(int), intr2(int), susp(int), sendwin(int);
#ifdef SIGINFO
static RETSIGTYPE ayt(int);
#endif
static RETSIGTYPE
deadpeer(int sig)
{
setcommandmode();
longjmp(peerdied, -1);
}
int intr_happened = 0;
int intr_waiting = 0;
static RETSIGTYPE
intr(int sig)
{
if (intr_waiting) {
intr_happened = 1;
return;
}
if (localchars) {
intp();
return;
}
setcommandmode();
longjmp(toplevel, -1);
}
static RETSIGTYPE
intr2(int sig)
{
if (localchars) {
#ifdef KLUDGELINEMODE
if (kludgelinemode)
sendbrk();
else
#endif
sendabort();
return;
}
}
#ifdef SIGTSTP
static RETSIGTYPE
susp(int sig)
{
if ((rlogin != _POSIX_VDISABLE) && rlogin_susp())
return;
if (localchars)
sendsusp();
}
#endif
#ifdef SIGWINCH
static RETSIGTYPE
sendwin(int sig)
{
if (connected) {
sendnaws();
}
}
#endif
#ifdef SIGINFO
static RETSIGTYPE
ayt(int sig)
{
if (connected)
sendayt();
else
ayt_status(sig);
}
#endif
void
sys_telnet_init(void)
{
signal(SIGINT, intr);
signal(SIGQUIT, intr2);
signal(SIGPIPE, deadpeer);
#ifdef SIGWINCH
signal(SIGWINCH, sendwin);
#endif
#ifdef SIGTSTP
signal(SIGTSTP, susp);
#endif
#ifdef SIGINFO
signal(SIGINFO, ayt);
#endif
setconnmode(0);
NetNonblockingIO(net, 1);
#if defined(SO_OOBINLINE)
if (SetSockOpt(net, SOL_SOCKET, SO_OOBINLINE, 1) == -1)
perror("setsockopt (SO_OOBINLINE) (ignored)");
#endif
}
int
process_rings(int netin,
int netout,
int netex,
int ttyin,
int ttyout,
int poll)
{
int c;
int returnValue = 0;
static struct timeval TimeValue = { 0 };
if (net >= FD_SETSIZE
|| tout >= FD_SETSIZE
|| tin >= FD_SETSIZE)
errx (1, "fd too large");
if (netout) {
FD_SET(net, &obits);
}
if (ttyout) {
FD_SET(tout, &obits);
}
if (ttyin) {
FD_SET(tin, &ibits);
}
if (netin) {
FD_SET(net, &ibits);
}
#if !defined(SO_OOBINLINE)
if (netex) {
FD_SET(net, &xbits);
}
#endif
if ((c = select(FD_SETSIZE, &ibits, &obits, &xbits,
(poll == 0)? (struct timeval *)0 : &TimeValue)) < 0) {
if (c == -1) {
if (errno == EINTR) {
return 0;
}
printf("sleep(5) from telnet, after select\r\n");
sleep(5);
}
return 0;
}
if (FD_ISSET(net, &xbits)) {
FD_CLR(net, &xbits);
SYNCHing = 1;
ttyflush(1);
}
if (FD_ISSET(net, &ibits)) {
int canread;
FD_CLR(net, &ibits);
canread = ring_empty_consecutive(&netiring);
#if !defined(SO_OOBINLINE)
if (SYNCHing) {
int atmark;
static int bogus_oob = 0, first = 1;
ioctl(net, SIOCATMARK, (char *)&atmark);
if (atmark) {
c = recv(net, netiring.supply, canread, MSG_OOB);
if ((c == -1) && (errno == EINVAL)) {
c = recv(net, netiring.supply, canread, 0);
if (clocks.didnetreceive < clocks.gotDM) {
SYNCHing = stilloob();
}
} else if (first && c > 0) {
int i;
i = recv(net, netiring.supply + c, canread - c, MSG_OOB);
if (i == c &&
memcmp(netiring.supply, netiring.supply + c, i) == 0) {
bogus_oob = 1;
first = 0;
} else if (i < 0) {
bogus_oob = 0;
first = 0;
} else
c += i;
}
if (bogus_oob && c > 0) {
int i;
i = read(net, netiring.supply + c, canread - c);
if (i > 0)
c += i;
}
} else {
c = recv(net, netiring.supply, canread, 0);
}
} else {
c = recv(net, netiring.supply, canread, 0);
}
settimer(didnetreceive);
#else
c = recv(net, (char *)netiring.supply, canread, 0);
#endif
if (c < 0 && errno == EWOULDBLOCK) {
c = 0;
} else if (c <= 0) {
return -1;
}
if (netdata) {
Dump('<', netiring.supply, c);
}
if (c)
ring_supplied(&netiring, c);
returnValue = 1;
}
if (FD_ISSET(tin, &ibits)) {
FD_CLR(tin, &ibits);
c = TerminalRead(ttyiring.supply, ring_empty_consecutive(&ttyiring));
if (c < 0 && errno == EIO)
c = 0;
if (c < 0 && errno == EWOULDBLOCK) {
c = 0;
} else {
if ((c == 0) && MODE_LOCAL_CHARS(globalmode) && isatty(tin)) {
*ttyiring.supply = termEofChar;
c = 1;
}
if (c <= 0) {
return -1;
}
if (termdata) {
Dump('<', ttyiring.supply, c);
}
ring_supplied(&ttyiring, c);
}
returnValue = 1;
}
if (FD_ISSET(net, &obits)) {
FD_CLR(net, &obits);
returnValue |= netflush();
}
if (FD_ISSET(tout, &obits)) {
FD_CLR(tout, &obits);
returnValue |= (ttyflush(SYNCHing|flushout) > 0);
}
return returnValue;
}