2 * Copyright (c) 1988, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include "telnet_locl.h"
39 * The following routines try to encapsulate what is system dependent
40 * (at least between 4.x and dos) which is used in telnet.c.
44 tout, /* Output file descriptor */
45 tin, /* Input file descriptor */
48 struct termios old_tc = { 0 };
49 extern struct termios new_tc;
53 # define TCSANOW TCSETS
54 # define TCSADRAIN TCSETSW
55 # define tcgetattr(f, t) ioctl(f, TCGETS, (char *)t)
58 # define TCSANOW TCSETA
59 # define TCSADRAIN TCSETAW
60 # define tcgetattr(f, t) ioctl(f, TCGETA, (char *)t)
62 # define TCSANOW TIOCSETA
63 # define TCSADRAIN TIOCSETAW
64 # define tcgetattr(f, t) ioctl(f, TIOCGETA, (char *)t)
67 # define tcsetattr(f, a, t) ioctl(f, a, (char *)t)
68 # define cfgetospeed(ptr) ((ptr)->c_cflag&CBAUD)
70 # define cfgetispeed(ptr) (((ptr)->c_cflag&CIBAUD) >> IBSHIFT)
72 # define cfgetispeed(ptr) cfgetospeed(ptr)
76 static fd_set ibits, obits, xbits;
82 tout = fileno(stdout);
93 TerminalWrite(char *buf, int n)
95 return write(tout, buf, n);
99 TerminalRead(unsigned char *buf, int n)
101 return read(tin, buf, n);
109 TerminalAutoFlush(void)
114 ioctl(0, TIOCLGET, (char *)&flush);
115 return !(flush&LNOFLSH); /* if LNOFLSH, no autoflush */
122 * TerminalSpecialChars()
124 * Look at an input character to see if it is a special character
125 * and decide what to do.
129 * 0 Don't add this character.
130 * 1 Do add this character
134 TerminalSpecialChars(int c)
136 if (c == termIntChar) {
139 } else if (c == termQuitChar) {
140 #ifdef KLUDGELINEMODE
147 } else if (c == termEofChar) {
148 if (my_want_state_is_will(TELOPT_LINEMODE)) {
153 } else if (c == termSuspChar) {
156 } else if (c == termFlushChar) {
157 xmitAO(); /* Transmit Abort Output */
159 } else if (!MODE_LOCAL_CHARS(globalmode)) {
160 if (c == termKillChar) {
163 } else if (c == termEraseChar) {
164 xmitEC(); /* Transmit Erase Character */
173 * Flush output to the terminal
177 TerminalFlushOutput(void)
180 ioctl(fileno(stdout), TIOCFLUSH, (char *) 0);
182 ioctl(fileno(stdout), TCFLSH, (char *) 0);
187 TerminalSaveState(void)
189 tcgetattr(0, &old_tc);
194 termFlushChar = CONTROL('O');
197 termWerasChar = CONTROL('W');
200 termRprntChar = CONTROL('R');
203 termLiteralNextChar = CONTROL('V');
206 termStartChar = CONTROL('Q');
209 termStopChar = CONTROL('S');
212 termAytChar = CONTROL('T');
220 case SLC_IP: return(&termIntChar);
221 case SLC_ABORT: return(&termQuitChar);
222 case SLC_EOF: return(&termEofChar);
223 case SLC_EC: return(&termEraseChar);
224 case SLC_EL: return(&termKillChar);
225 case SLC_XON: return(&termStartChar);
226 case SLC_XOFF: return(&termStopChar);
227 case SLC_FORW1: return(&termForw1Char);
228 case SLC_FORW2: return(&termForw2Char);
230 case SLC_AO: return(&termFlushChar);
233 case SLC_SUSP: return(&termSuspChar);
236 case SLC_EW: return(&termWerasChar);
239 case SLC_RP: return(&termRprntChar);
242 case SLC_LNEXT: return(&termLiteralNextChar);
245 case SLC_AYT: return(&termAytChar);
257 TerminalDefaultChars(void)
259 memmove(new_tc.c_cc, old_tc.c_cc, sizeof(old_tc.c_cc));
261 termFlushChar = CONTROL('O');
264 termWerasChar = CONTROL('W');
267 termRprntChar = CONTROL('R');
270 termLiteralNextChar = CONTROL('V');
273 termStartChar = CONTROL('Q');
276 termStopChar = CONTROL('S');
279 termAytChar = CONTROL('T');
285 TerminalRestoreState()
291 * TerminalNewMode - set up terminal to a specific mode.
292 * MODE_ECHO: do local terminal echo
293 * MODE_FLOW: do local flow control
294 * MODE_TRAPSIG: do local mapping to TELNET IAC sequences
295 * MODE_EDIT: do local line editing
298 * MODE_ECHO|MODE_EDIT|MODE_FLOW|MODE_TRAPSIG
302 * local signal mapping
306 * Both Linemode and Single Character mode:
309 * local/no signal mapping
314 static RETSIGTYPE susp(int);
317 static RETSIGTYPE ayt(int);
321 TerminalNewMode(int f)
323 static int prevmode = 0;
324 struct termios tmp_tc;
329 globalmode = f&~MODE_FORCE;
334 * Write any outstanding data before switching modes
335 * ttyflush() returns 0 only when there is no more data
336 * left to write out, it returns -1 if it couldn't do
337 * anything at all, otherwise it returns 1 + the number
338 * of characters left to write.
340 old = ttyflush(SYNCHing|flushout);
341 if (old < 0 || old > 1) {
342 tcgetattr(tin, &tmp_tc);
345 * Wait for data to drain, then flush again.
347 tcsetattr(tin, TCSADRAIN, &tmp_tc);
348 old = ttyflush(SYNCHing|flushout);
349 } while (old < 0 || old > 1);
353 prevmode = f&~MODE_FORCE;
357 tmp_tc.c_lflag |= ECHO;
358 tmp_tc.c_oflag |= ONLCR;
360 tmp_tc.c_iflag |= ICRNL;
362 tmp_tc.c_lflag &= ~ECHO;
363 tmp_tc.c_oflag &= ~ONLCR;
366 tmp_tc.c_iflag &= ~ICRNL;
370 if ((f&MODE_FLOW) == 0) {
371 tmp_tc.c_iflag &= ~(IXOFF|IXON); /* Leave the IXANY bit alone */
373 if (restartany < 0) {
374 tmp_tc.c_iflag |= IXOFF|IXON; /* Leave the IXANY bit alone */
375 } else if (restartany > 0) {
376 tmp_tc.c_iflag |= IXOFF|IXON|IXANY;
378 tmp_tc.c_iflag |= IXOFF|IXON;
379 tmp_tc.c_iflag &= ~IXANY;
383 if ((f&MODE_TRAPSIG) == 0) {
384 tmp_tc.c_lflag &= ~ISIG;
387 tmp_tc.c_lflag |= ISIG;
392 tmp_tc.c_lflag |= ICANON;
394 tmp_tc.c_lflag &= ~ICANON;
395 tmp_tc.c_iflag &= ~ICRNL;
396 tmp_tc.c_cc[VMIN] = 1;
397 tmp_tc.c_cc[VTIME] = 0;
400 if ((f&(MODE_EDIT|MODE_TRAPSIG)) == 0) {
402 tmp_tc.c_cc[VLNEXT] = (cc_t)(_POSIX_VDISABLE);
406 if (f&MODE_SOFT_TAB) {
408 tmp_tc.c_oflag |= OXTABS;
411 tmp_tc.c_oflag &= ~TABDLY;
412 tmp_tc.c_oflag |= TAB3;
416 tmp_tc.c_oflag &= ~OXTABS;
419 tmp_tc.c_oflag &= ~TABDLY;
423 if (f&MODE_LIT_ECHO) {
425 tmp_tc.c_lflag &= ~ECHOCTL;
429 tmp_tc.c_lflag |= ECHOCTL;
437 tmp_tc.c_iflag &= ~ISTRIP;
439 tmp_tc.c_iflag |= ISTRIP;
440 if ((f & MODE_OUTBIN) || (f & MODE_OUT8)) {
441 tmp_tc.c_cflag &= ~(CSIZE|PARENB);
442 tmp_tc.c_cflag |= CS8;
444 tmp_tc.c_oflag &= ~OPOST;
446 tmp_tc.c_oflag |= OPOST;
448 tmp_tc.c_cflag &= ~(CSIZE|PARENB);
449 tmp_tc.c_cflag |= old_tc.c_cflag & (CSIZE|PARENB);
450 tmp_tc.c_oflag |= OPOST;
458 signal(SIGTSTP, susp);
461 signal(SIGINFO, ayt);
464 tmp_tc.c_lflag |= NOKERNINFO;
467 * We don't want to process ^Y here. It's just another
468 * character that we'll pass on to the back end. It has
469 * to process it because it will be processed when the
470 * user attempts to read it, not when we send it.
473 tmp_tc.c_cc[VDSUSP] = (cc_t)(_POSIX_VDISABLE);
476 * If the VEOL character is already set, then use VEOL2,
477 * otherwise use VEOL.
479 esc = (rlogin != _POSIX_VDISABLE) ? rlogin : escape;
480 if ((tmp_tc.c_cc[VEOL] != esc)
482 && (tmp_tc.c_cc[VEOL2] != esc)
485 if (tmp_tc.c_cc[VEOL] == (cc_t)(_POSIX_VDISABLE))
486 tmp_tc.c_cc[VEOL] = esc;
488 else if (tmp_tc.c_cc[VEOL2] == (cc_t)(_POSIX_VDISABLE))
489 tmp_tc.c_cc[VEOL2] = esc;
496 signal(SIGINFO, ayt_status);
499 signal(SIGTSTP, SIG_DFL);
501 sigaddset(&sm, SIGTSTP);
502 sigprocmask(SIG_UNBLOCK, &sm, NULL);
506 if (tcsetattr(tin, TCSADRAIN, &tmp_tc) < 0)
507 tcsetattr(tin, TCSANOW, &tmp_tc);
509 ioctl(tin, FIONBIO, (char *)&onoff);
510 ioctl(tout, FIONBIO, (char *)&onoff);
515 * Try to guess whether speeds are "encoded" (4.2BSD) or just numeric (4.4BSD).
531 # define B19200 B14400
535 #define B28800 B19200
539 # define B38400 B28800
543 #define B57600 B38400
547 #define B76800 B57600
551 #define B115200 B76800
555 #define B230400 B115200
560 * This code assumes that the values B0, B50, B75...
561 * are in ascending order. They do not have to be
568 { 0, B0 }, { 50, B50 }, { 75, B75 },
569 { 110, B110 }, { 134, B134 }, { 150, B150 },
570 { 200, B200 }, { 300, B300 }, { 600, B600 },
571 { 1200, B1200 }, { 1800, B1800 }, { 2400, B2400 },
572 { 4800, B4800 }, { 7200, B7200 }, { 9600, B9600 },
573 { 14400, B14400 }, { 19200, B19200 }, { 28800, B28800 },
574 { 38400, B38400 }, { 57600, B57600 }, { 115200, B115200 },
575 { 230400, B230400 }, { -1, B230400 }
577 #endif /* DECODE_BAUD */
580 TerminalSpeeds(long *input_speed, long *output_speed)
583 struct termspeeds *tp;
584 #endif /* DECODE_BAUD */
587 out = cfgetospeed(&old_tc);
588 in = cfgetispeed(&old_tc);
594 while ((tp->speed != -1) && (tp->value < in))
596 *input_speed = tp->speed;
599 while ((tp->speed != -1) && (tp->value < out))
601 *output_speed = tp->speed;
602 #else /* DECODE_BAUD */
605 #endif /* DECODE_BAUD */
609 TerminalWindowSize(long *rows, long *cols)
613 if (get_window_size(STDIN_FILENO, &irows, &icols) == 0) {
629 NetNonblockingIO(int fd, int onoff)
631 ioctl(fd, FIONBIO, (char *)&onoff);
636 * Various signal handling routines.
639 static RETSIGTYPE deadpeer(int),
640 intr(int), intr2(int), susp(int), sendwin(int);
642 static RETSIGTYPE ayt(int);
651 longjmp(peerdied, -1);
654 int intr_happened = 0;
655 int intr_waiting = 0;
670 longjmp(toplevel, -1);
678 #ifdef KLUDGELINEMODE
693 if ((rlogin != _POSIX_VDISABLE) && rlogin_susp())
725 sys_telnet_init(void)
727 signal(SIGINT, intr);
728 signal(SIGQUIT, intr2);
729 signal(SIGPIPE, deadpeer);
731 signal(SIGWINCH, sendwin);
734 signal(SIGTSTP, susp);
737 signal(SIGINFO, ayt);
742 NetNonblockingIO(net, 1);
745 #if defined(SO_OOBINLINE)
746 if (SetSockOpt(net, SOL_SOCKET, SO_OOBINLINE, 1) == -1)
747 perror("setsockopt (SO_OOBINLINE) (ignored)");
748 #endif /* defined(SO_OOBINLINE) */
754 * This routine tries to fill up/empty our various rings.
756 * The parameter specifies whether this is a poll operation,
757 * or a block-until-something-happens operation.
759 * The return value is 1 if something happened, 0 if not.
763 process_rings(int netin,
768 int poll) /* If 0, then block until something to do */
771 /* One wants to be a bit careful about setting returnValue
772 * to one, since a one implies we did some useful work,
773 * and therefore probably won't be called to block next
774 * time (TN3270 mode only).
777 static struct timeval TimeValue = { 0 };
779 if (net >= FD_SETSIZE
780 || tout >= FD_SETSIZE
781 || tin >= FD_SETSIZE)
782 errx (1, "fd too large");
788 FD_SET(tout, &obits);
796 #if !defined(SO_OOBINLINE)
801 if ((c = select(FD_SETSIZE, &ibits, &obits, &xbits,
802 (poll == 0)? (struct timeval *)0 : &TimeValue)) < 0) {
805 * we can get EINTR if we are in line mode,
806 * and the user does an escape (TSTP), or
807 * some other signal generator.
809 if (errno == EINTR) {
812 /* I don't like this, does it ever happen? */
813 printf("sleep(5) from telnet, after select\r\n");
822 if (FD_ISSET(net, &xbits)) {
825 ttyflush(1); /* flush already enqueued data */
829 * Something to read from the network...
831 if (FD_ISSET(net, &ibits)) {
835 canread = ring_empty_consecutive(&netiring);
836 #if !defined(SO_OOBINLINE)
838 * In 4.2 (and some early 4.3) systems, the
839 * OOB indication and data handling in the kernel
840 * is such that if two separate TCP Urgent requests
841 * come in, one byte of TCP data will be overlaid.
842 * This is fatal for Telnet, but we try to live
845 * In addition, in 4.2 (and...), a special protocol
846 * is needed to pick up the TCP Urgent data in
847 * the correct sequence.
849 * What we do is: if we think we are in urgent
850 * mode, we look to see if we are "at the mark".
851 * If we are, we do an OOB receive. If we run
852 * this twice, we will do the OOB receive twice,
853 * but the second will fail, since the second
854 * time we were "at the mark", but there wasn't
855 * any data there (the kernel doesn't reset
856 * "at the mark" until we do a normal read).
857 * Once we've read the OOB data, we go ahead
858 * and do normal reads.
860 * There is also another problem, which is that
861 * since the OOB byte we read doesn't put us
862 * out of OOB state, and since that byte is most
863 * likely the TELNET DM (data mark), we would
864 * stay in the TELNET SYNCH (SYNCHing) state.
865 * So, clocks to the rescue. If we've "just"
866 * received a DM, then we test for the
867 * presence of OOB data when the receive OOB
868 * fails (and AFTER we did the normal mode read
869 * to clear "at the mark").
873 static int bogus_oob = 0, first = 1;
875 ioctl(net, SIOCATMARK, (char *)&atmark);
877 c = recv(net, netiring.supply, canread, MSG_OOB);
878 if ((c == -1) && (errno == EINVAL)) {
879 c = recv(net, netiring.supply, canread, 0);
880 if (clocks.didnetreceive < clocks.gotDM) {
881 SYNCHing = stilloob();
883 } else if (first && c > 0) {
885 * Bogosity check. Systems based on 4.2BSD
886 * do not return an error if you do a second
887 * recv(MSG_OOB). So, we do one. If it
888 * succeeds and returns exactly the same
889 * data, then assume that we are running
890 * on a broken system and set the bogus_oob
891 * flag. (If the data was different, then
892 * we probably got some valid new data, so
893 * increment the count...)
896 i = recv(net, netiring.supply + c, canread - c, MSG_OOB);
898 memcmp(netiring.supply, netiring.supply + c, i) == 0) {
907 if (bogus_oob && c > 0) {
910 * Bogosity. We have to do the read
911 * to clear the atmark to get out of
914 i = read(net, netiring.supply + c, canread - c);
919 c = recv(net, netiring.supply, canread, 0);
922 c = recv(net, netiring.supply, canread, 0);
924 settimer(didnetreceive);
925 #else /* !defined(SO_OOBINLINE) */
926 c = recv(net, (char *)netiring.supply, canread, 0);
927 #endif /* !defined(SO_OOBINLINE) */
928 if (c < 0 && errno == EWOULDBLOCK) {
934 Dump('<', netiring.supply, c);
937 ring_supplied(&netiring, c);
942 * Something to read from the tty...
944 if (FD_ISSET(tin, &ibits)) {
946 c = TerminalRead(ttyiring.supply, ring_empty_consecutive(&ttyiring));
947 if (c < 0 && errno == EIO)
949 if (c < 0 && errno == EWOULDBLOCK) {
952 /* EOF detection for line mode!!!! */
953 if ((c == 0) && MODE_LOCAL_CHARS(globalmode) && isatty(tin)) {
954 /* must be an EOF... */
955 *ttyiring.supply = termEofChar;
962 Dump('<', ttyiring.supply, c);
964 ring_supplied(&ttyiring, c);
966 returnValue = 1; /* did something useful */
969 if (FD_ISSET(net, &obits)) {
971 returnValue |= netflush();
973 if (FD_ISSET(tout, &obits)) {
974 FD_CLR(tout, &obits);
975 returnValue |= (ttyflush(SYNCHing|flushout) > 0);