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
33 * @(#)sys_bsd.c 8.4 (Berkeley) 5/30/95
34 * $FreeBSD: src/crypto/telnet/telnet/sys_bsd.c,v 1.2.8.4 2002/04/13 10:59:08 markm Exp $
35 * $DragonFly: src/crypto/telnet/telnet/sys_bsd.c,v 1.2 2003/06/17 04:24:37 dillon Exp $
39 #include <sys/cdefs.h>
44 * The following routines try to encapsulate what is system dependent
45 * (at least between 4.x and dos) which is used in telnet.c.
48 #include <sys/types.h>
49 #include <sys/socket.h>
55 #include <arpa/telnet.h>
64 tout, /* Output file descriptor */
65 tin, /* Input file descriptor */
69 struct tchars otc = { 0 }, ntc = { 0 };
70 struct ltchars oltc = { 0 }, nltc = { 0 };
71 struct sgttyb ottyb = { 0 }, nttyb = { 0 };
73 # define cfgetispeed(ptr) (ptr)->sg_ispeed
74 # define cfgetospeed(ptr) (ptr)->sg_ospeed
77 #else /* USE_TERMIO */
78 struct termio old_tc = { 0, 0, 0, 0, {}, 0, 0 };
82 # define TCSANOW TCSETS
83 # define TCSADRAIN TCSETSW
84 # define tcgetattr(f, t) ioctl(f, TCGETS, (char *)t)
87 # define TCSANOW TCSETA
88 # define TCSADRAIN TCSETAW
89 # define tcgetattr(f, t) ioctl(f, TCGETA, (char *)t)
91 # define TCSANOW TIOCSETA
92 # define TCSADRAIN TIOCSETAW
93 # define tcgetattr(f, t) ioctl(f, TIOCGETA, (char *)t)
96 # define tcsetattr(f, a, t) ioctl(f, a, (char *)t)
97 # define cfgetospeed(ptr) ((ptr)->c_cflag&CBAUD)
99 # define cfgetispeed(ptr) (((ptr)->c_cflag&CIBAUD) >> IBSHIFT)
101 # define cfgetispeed(ptr) cfgetospeed(ptr)
103 # endif /* TCSANOW */
105 # define TIOCFLUSH TC_PX_DRAIN
107 #endif /* USE_TERMIO */
109 static fd_set *ibitsp, *obitsp, *xbitsp;
113 static SIG_FUNC_RET intr(int);
116 static SIG_FUNC_RET intr2(int);
119 static SIG_FUNC_RET susp(int);
122 static SIG_FUNC_RET ayt(int);
128 tout = fileno(stdout);
134 TerminalWrite(char *buf, int n)
136 return write(tout, buf, n);
140 TerminalRead(char *buf, int n)
142 return read(tin, buf, n);
150 TerminalAutoFlush(void)
155 ioctl(0, TIOCLGET, (char *)&flush);
156 return !(flush&LNOFLSH); /* if LNOFLSH, no autoflush */
162 #ifdef KLUDGELINEMODE
163 extern int kludgelinemode;
166 * TerminalSpecialChars()
168 * Look at an input character to see if it is a special character
169 * and decide what to do.
173 * 0 Don't add this character.
174 * 1 Do add this character
178 TerminalSpecialChars(int c)
180 if (c == termIntChar) {
183 } else if (c == termQuitChar) {
184 #ifdef KLUDGELINEMODE
191 } else if (c == termEofChar) {
192 if (my_want_state_is_will(TELOPT_LINEMODE)) {
197 } else if (c == termSuspChar) {
200 } else if (c == termFlushChar) {
201 xmitAO(); /* Transmit Abort Output */
203 } else if (!MODE_LOCAL_CHARS(globalmode)) {
204 if (c == termKillChar) {
207 } else if (c == termEraseChar) {
208 xmitEC(); /* Transmit Erase Character */
217 * Flush output to the terminal
221 TerminalFlushOutput(void)
224 (void) ioctl(fileno(stdout), TIOCFLUSH, NULL);
226 (void) ioctl(fileno(stdout), TCFLSH, NULL);
231 TerminalSaveState(void)
234 ioctl(0, TIOCGETP, (char *)&ottyb);
235 ioctl(0, TIOCGETC, (char *)&otc);
236 ioctl(0, TIOCGLTC, (char *)&oltc);
237 ioctl(0, TIOCLGET, (char *)&olmode);
243 #else /* USE_TERMIO */
244 tcgetattr(0, &old_tc);
249 termFlushChar = CONTROL('O');
252 termWerasChar = CONTROL('W');
255 termRprntChar = CONTROL('R');
258 termLiteralNextChar = CONTROL('V');
261 termStartChar = CONTROL('Q');
264 termStopChar = CONTROL('S');
267 termAytChar = CONTROL('T');
269 #endif /* USE_TERMIO */
276 case SLC_IP: return(&termIntChar);
277 case SLC_ABORT: return(&termQuitChar);
278 case SLC_EOF: return(&termEofChar);
279 case SLC_EC: return(&termEraseChar);
280 case SLC_EL: return(&termKillChar);
281 case SLC_XON: return(&termStartChar);
282 case SLC_XOFF: return(&termStopChar);
283 case SLC_FORW1: return(&termForw1Char);
285 case SLC_FORW2: return(&termForw2Char);
287 case SLC_AO: return(&termFlushChar);
290 case SLC_SUSP: return(&termSuspChar);
293 case SLC_EW: return(&termWerasChar);
296 case SLC_RP: return(&termRprntChar);
299 case SLC_LNEXT: return(&termLiteralNextChar);
302 case SLC_AYT: return(&termAytChar);
315 TerminalDefaultChars(void)
320 nttyb.sg_kill = ottyb.sg_kill;
321 nttyb.sg_erase = ottyb.sg_erase;
322 #else /* USE_TERMIO */
323 memcpy(new_tc.c_cc, old_tc.c_cc, sizeof(old_tc.c_cc));
325 termFlushChar = CONTROL('O');
328 termWerasChar = CONTROL('W');
331 termRprntChar = CONTROL('R');
334 termLiteralNextChar = CONTROL('V');
337 termStartChar = CONTROL('Q');
340 termStopChar = CONTROL('S');
343 termAytChar = CONTROL('T');
345 #endif /* USE_TERMIO */
349 * TerminalNewMode - set up terminal to a specific mode.
350 * MODE_ECHO: do local terminal echo
351 * MODE_FLOW: do local flow control
352 * MODE_TRAPSIG: do local mapping to TELNET IAC sequences
353 * MODE_EDIT: do local line editing
356 * MODE_ECHO|MODE_EDIT|MODE_FLOW|MODE_TRAPSIG
360 * local signal mapping
364 * Both Linemode and Single Character mode:
367 * local/no signal mapping
371 TerminalNewMode(int f)
373 static int prevmode = 0;
379 #else /* USE_TERMIO */
380 struct termio tmp_tc;
381 #endif /* USE_TERMIO */
386 globalmode = f&~MODE_FORCE;
391 * Write any outstanding data before switching modes
392 * ttyflush() returns 0 only when there is no more data
393 * left to write out, it returns -1 if it couldn't do
394 * anything at all, otherwise it returns 1 + the number
395 * of characters left to write.
397 * We would really like ask the kernel to wait for the output
398 * to drain, like we can do with the TCSADRAIN, but we don't have
399 * that option. The only ioctl that waits for the output to
400 * drain, TIOCSETP, also flushes the input queue, which is NOT
401 * what we want (TIOCSETP is like TCSADFLUSH).
404 old = ttyflush(SYNCHing|flushout);
405 if (old < 0 || old > 1) {
407 tcgetattr(tin, &tmp_tc);
408 #endif /* USE_TERMIO */
411 * Wait for data to drain, then flush again.
414 tcsetattr(tin, TCSADRAIN, &tmp_tc);
415 #endif /* USE_TERMIO */
416 old = ttyflush(SYNCHing|flushout);
417 } while (old < 0 || old > 1);
421 prevmode = f&~MODE_FORCE;
435 tmp_tc.c_lflag |= ECHO;
436 tmp_tc.c_oflag |= ONLCR;
438 tmp_tc.c_iflag |= ICRNL;
442 sb.sg_flags &= ~ECHO;
444 tmp_tc.c_lflag &= ~ECHO;
445 tmp_tc.c_oflag &= ~ONLCR;
449 if ((f&MODE_FLOW) == 0) {
451 tc.t_startc = _POSIX_VDISABLE;
452 tc.t_stopc = _POSIX_VDISABLE;
454 tmp_tc.c_iflag &= ~(IXOFF|IXON); /* Leave the IXANY bit alone */
456 if (restartany < 0) {
457 tmp_tc.c_iflag |= IXOFF|IXON; /* Leave the IXANY bit alone */
458 } else if (restartany > 0) {
459 tmp_tc.c_iflag |= IXOFF|IXON|IXANY;
461 tmp_tc.c_iflag |= IXOFF|IXON;
462 tmp_tc.c_iflag &= ~IXANY;
467 if ((f&MODE_TRAPSIG) == 0) {
469 tc.t_intrc = _POSIX_VDISABLE;
470 tc.t_quitc = _POSIX_VDISABLE;
471 tc.t_eofc = _POSIX_VDISABLE;
472 ltc.t_suspc = _POSIX_VDISABLE;
473 ltc.t_dsuspc = _POSIX_VDISABLE;
475 tmp_tc.c_lflag &= ~ISIG;
480 tmp_tc.c_lflag |= ISIG;
487 sb.sg_flags &= ~CBREAK;
488 sb.sg_flags |= CRMOD;
490 tmp_tc.c_lflag |= ICANON;
494 sb.sg_flags |= CBREAK;
496 sb.sg_flags |= CRMOD;
498 sb.sg_flags &= ~CRMOD;
500 tmp_tc.c_lflag &= ~ICANON;
501 tmp_tc.c_iflag &= ~ICRNL;
502 tmp_tc.c_cc[VMIN] = 1;
503 tmp_tc.c_cc[VTIME] = 0;
507 if ((f&(MODE_EDIT|MODE_TRAPSIG)) == 0) {
509 ltc.t_lnextc = _POSIX_VDISABLE;
512 tmp_tc.c_cc[VLNEXT] = (cc_t)(_POSIX_VDISABLE);
517 if (f&MODE_SOFT_TAB) {
519 sb.sg_flags |= XTABS;
522 tmp_tc.c_oflag |= OXTABS;
525 tmp_tc.c_oflag &= ~TABDLY;
526 tmp_tc.c_oflag |= TAB3;
531 sb.sg_flags &= ~XTABS;
534 tmp_tc.c_oflag &= ~OXTABS;
537 tmp_tc.c_oflag &= ~TABDLY;
542 if (f&MODE_LIT_ECHO) {
547 tmp_tc.c_lflag &= ~ECHOCTL;
555 tmp_tc.c_lflag |= ECHOCTL;
575 tmp_tc.c_iflag &= ~ISTRIP;
577 tmp_tc.c_iflag |= ISTRIP;
578 if (f & MODE_OUTBIN) {
579 tmp_tc.c_cflag &= ~(CSIZE|PARENB);
580 tmp_tc.c_cflag |= CS8;
581 tmp_tc.c_oflag &= ~OPOST;
583 tmp_tc.c_cflag &= ~(CSIZE|PARENB);
584 tmp_tc.c_cflag |= old_tc.c_cflag & (CSIZE|PARENB);
585 tmp_tc.c_oflag |= OPOST;
593 (void) signal(SIGINT, intr);
596 (void) signal(SIGQUIT, intr2);
599 (void) signal(SIGTSTP, susp);
602 (void) signal(SIGINFO, ayt);
604 #if defined(USE_TERMIO) && defined(NOKERNINFO)
605 tmp_tc.c_lflag |= NOKERNINFO;
608 * We don't want to process ^Y here. It's just another
609 * character that we'll pass on to the back end. It has
610 * to process it because it will be processed when the
611 * user attempts to read it, not when we send it.
614 ltc.t_dsuspc = _POSIX_VDISABLE;
617 tmp_tc.c_cc[VDSUSP] = (cc_t)(_POSIX_VDISABLE);
622 * If the VEOL character is already set, then use VEOL2,
623 * otherwise use VEOL.
625 esc = (rlogin != _POSIX_VDISABLE) ? rlogin : escape;
626 if ((tmp_tc.c_cc[VEOL] != esc)
628 && (tmp_tc.c_cc[VEOL2] != esc)
631 if (tmp_tc.c_cc[VEOL] == (cc_t)(_POSIX_VDISABLE))
632 tmp_tc.c_cc[VEOL] = esc;
634 else if (tmp_tc.c_cc[VEOL2] == (cc_t)(_POSIX_VDISABLE))
635 tmp_tc.c_cc[VEOL2] = esc;
639 if (tc.t_brkc == (cc_t)(_POSIX_VDISABLE))
644 (void) signal(SIGINFO, (void (*)(int))ayt_status);
647 (void) signal(SIGINT, SIG_DFL);
650 (void) signal(SIGQUIT, SIG_DFL);
653 (void) signal(SIGTSTP, SIG_DFL);
655 (void) sigsetmask(sigblock(0) & ~(1<<(SIGTSTP-1)));
657 (void) sigrelse(SIGTSTP);
670 ioctl(tin, TIOCLSET, (char *)&lmode);
671 ioctl(tin, TIOCSLTC, (char *)<c);
672 ioctl(tin, TIOCSETC, (char *)&tc);
673 ioctl(tin, TIOCSETN, (char *)&sb);
675 if (tcsetattr(tin, TCSADRAIN, &tmp_tc) < 0)
676 tcsetattr(tin, TCSANOW, &tmp_tc);
679 ioctl(tin, FIONBIO, (char *)&onoff);
680 ioctl(tout, FIONBIO, (char *)&onoff);
685 * Try to guess whether speeds are "encoded" (4.2BSD) or just numeric (4.4BSD).
701 # define B19200 B14400
705 #define B28800 B19200
709 # define B38400 B28800
713 #define B57600 B38400
717 #define B76800 B57600
721 #define B115200 B76800
725 #define B230400 B115200
730 * This code assumes that the values B0, B50, B75...
731 * are in ascending order. They do not have to be
738 { 0, B0 }, { 50, B50 }, { 75, B75 },
739 { 110, B110 }, { 134, B134 }, { 150, B150 },
740 { 200, B200 }, { 300, B300 }, { 600, B600 },
741 { 1200, B1200 }, { 1800, B1800 }, { 2400, B2400 },
742 { 4800, B4800 }, { 7200, B7200 }, { 9600, B9600 },
743 { 14400, B14400 }, { 19200, B19200 }, { 28800, B28800 },
744 { 38400, B38400 }, { 57600, B57600 }, { 115200, B115200 },
745 { 230400, B230400 }, { -1, B230400 }
747 #endif /* DECODE_BAUD */
750 TerminalSpeeds(long *ispeed, long *ospeed)
753 struct termspeeds *tp;
754 #endif /* DECODE_BAUD */
757 out = cfgetospeed(&old_tc);
758 in = cfgetispeed(&old_tc);
764 while ((tp->speed != -1) && (tp->value < in))
769 while ((tp->speed != -1) && (tp->value < out))
772 #else /* DECODE_BAUD */
775 #endif /* DECODE_BAUD */
779 TerminalWindowSize(long *rows, long *cols)
784 if (ioctl(fileno(stdin), TIOCGWINSZ, (char *)&ws) >= 0) {
789 #endif /* TIOCGWINSZ */
800 NetNonblockingIO(int fd, int onoff)
802 ioctl(fd, FIONBIO, (char *)&onoff);
807 * Various signal handling routines.
812 deadpeer(int sig __unused)
815 longjmp(peerdied, -1);
820 intr(int sig __unused)
827 longjmp(toplevel, -1);
832 intr2(int sig __unused)
835 #ifdef KLUDGELINEMODE
848 susp(int sig __unused)
850 if ((rlogin != _POSIX_VDISABLE) && rlogin_susp())
860 sendwin(int sig __unused)
871 ayt(int sig __unused)
882 sys_telnet_init(void)
884 (void) signal(SIGINT, intr);
885 (void) signal(SIGQUIT, intr2);
886 (void) signal(SIGPIPE, deadpeer);
888 (void) signal(SIGWINCH, sendwin);
891 (void) signal(SIGTSTP, susp);
894 (void) signal(SIGINFO, ayt);
899 NetNonblockingIO(net, 1);
901 #if defined(SO_OOBINLINE)
902 if (SetSockOpt(net, SOL_SOCKET, SO_OOBINLINE, 1) == -1) {
903 perror("SetSockOpt");
905 #endif /* defined(SO_OOBINLINE) */
911 * This routine tries to fill up/empty our various rings.
913 * The parameter specifies whether this is a poll operation,
914 * or a block-until-something-happens operation.
916 * The return value is 1 if something happened, 0 if not.
920 process_rings(int netin, int netout, int netex, int ttyin, int ttyout, int poll)
924 static struct timeval TimeValue = { 0, 0 };
928 if ((netout || netin || netex) && net > maxfd)
931 if (ttyout && tout > maxfd)
933 if (ttyin && tin > maxfd)
935 tmp = howmany(maxfd+1, NFDBITS) * sizeof(fd_mask);
945 if ((ibitsp = (fd_set *)malloc(fdsn)) == NULL)
947 if ((obitsp = (fd_set *)malloc(fdsn)) == NULL)
949 if ((xbitsp = (fd_set *)malloc(fdsn)) == NULL)
951 memset(ibitsp, 0, fdsn);
952 memset(obitsp, 0, fdsn);
953 memset(xbitsp, 0, fdsn);
959 FD_SET(tout, obitsp);
966 if ((c = select(maxfd + 1, ibitsp, obitsp, xbitsp,
967 (poll == 0)? NULL : &TimeValue)) < 0) {
970 * we can get EINTR if we are in line mode,
971 * and the user does an escape (TSTP), or
972 * some other signal generator.
974 if (errno == EINTR) {
977 /* I don't like this, does it ever happen? */
978 printf("sleep(5) from telnet, after select: %s\r\n", strerror(errno));
987 if (FD_ISSET(net, xbitsp)) {
990 (void) ttyflush(1); /* flush already enqueued data */
994 * Something to read from the network...
996 if (FD_ISSET(net, ibitsp)) {
1000 canread = ring_empty_consecutive(&netiring);
1001 #if !defined(SO_OOBINLINE)
1003 * In 4.2 (and some early 4.3) systems, the
1004 * OOB indication and data handling in the kernel
1005 * is such that if two separate TCP Urgent requests
1006 * come in, one byte of TCP data will be overlaid.
1007 * This is fatal for Telnet, but we try to live
1010 * In addition, in 4.2 (and...), a special protocol
1011 * is needed to pick up the TCP Urgent data in
1012 * the correct sequence.
1014 * What we do is: if we think we are in urgent
1015 * mode, we look to see if we are "at the mark".
1016 * If we are, we do an OOB receive. If we run
1017 * this twice, we will do the OOB receive twice,
1018 * but the second will fail, since the second
1019 * time we were "at the mark", but there wasn't
1020 * any data there (the kernel doesn't reset
1021 * "at the mark" until we do a normal read).
1022 * Once we've read the OOB data, we go ahead
1023 * and do normal reads.
1025 * There is also another problem, which is that
1026 * since the OOB byte we read doesn't put us
1027 * out of OOB state, and since that byte is most
1028 * likely the TELNET DM (data mark), we would
1029 * stay in the TELNET SYNCH (SYNCHing) state.
1030 * So, clocks to the rescue. If we've "just"
1031 * received a DM, then we test for the
1032 * presence of OOB data when the receive OOB
1033 * fails (and AFTER we did the normal mode read
1034 * to clear "at the mark").
1038 static int bogus_oob = 0, first = 1;
1040 ioctl(net, SIOCATMARK, (char *)&atmark);
1042 c = recv(net, netiring.supply, canread, MSG_OOB);
1043 if ((c == -1) && (errno == EINVAL)) {
1044 c = recv(net, netiring.supply, canread, 0);
1045 if (clocks.didnetreceive < clocks.gotDM) {
1046 SYNCHing = stilloob(net);
1048 } else if (first && c > 0) {
1050 * Bogosity check. Systems based on 4.2BSD
1051 * do not return an error if you do a second
1052 * recv(MSG_OOB). So, we do one. If it
1053 * succeeds and returns exactly the same
1054 * data, then assume that we are running
1055 * on a broken system and set the bogus_oob
1056 * flag. (If the data was different, then
1057 * we probably got some valid new data, so
1058 * increment the count...)
1061 i = recv(net, netiring.supply + c, canread - c, MSG_OOB);
1063 memcmp(netiring.supply, netiring.supply + c, i) == 0) {
1072 if (bogus_oob && c > 0) {
1075 * Bogosity. We have to do the read
1076 * to clear the atmark to get out of
1079 i = read(net, netiring.supply + c, canread - c);
1084 c = recv(net, netiring.supply, canread, 0);
1087 c = recv(net, netiring.supply, canread, 0);
1089 settimer(didnetreceive);
1090 #else /* !defined(SO_OOBINLINE) */
1091 c = recv(net, (char *)netiring.supply, canread, 0);
1092 #endif /* !defined(SO_OOBINLINE) */
1093 if (c < 0 && errno == EWOULDBLOCK) {
1095 } else if (c <= 0) {
1099 Dump('<', netiring.supply, c);
1102 ring_supplied(&netiring, c);
1107 * Something to read from the tty...
1109 if (FD_ISSET(tin, ibitsp)) {
1110 FD_CLR(tin, ibitsp);
1111 c = TerminalRead(ttyiring.supply, ring_empty_consecutive(&ttyiring));
1112 if (c < 0 && errno == EIO)
1114 if (c < 0 && errno == EWOULDBLOCK) {
1117 /* EOF detection for line mode!!!! */
1118 if ((c == 0) && MODE_LOCAL_CHARS(globalmode) && isatty(tin)) {
1119 /* must be an EOF... */
1120 *ttyiring.supply = termEofChar;
1127 Dump('<', ttyiring.supply, c);
1129 ring_supplied(&ttyiring, c);
1131 returnValue = 1; /* did something useful */
1134 if (FD_ISSET(net, obitsp)) {
1135 FD_CLR(net, obitsp);
1136 returnValue |= netflush();
1138 if (FD_ISSET(tout, obitsp)) {
1139 FD_CLR(tout, obitsp);
1140 returnValue |= (ttyflush(SYNCHing|flushout) > 0);