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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)sys_bsd.c 8.4 (Berkeley) 5/30/95
30 * $FreeBSD: src/crypto/telnet/telnet/sys_bsd.c,v 1.2.8.4 2002/04/13 10:59:08 markm Exp $
38 * The following routines try to encapsulate what is system dependent
39 * (at least between 4.x and dos) which is used in telnet.c.
42 #include <sys/types.h>
43 #include <sys/socket.h>
49 #include <arpa/telnet.h>
58 tout, /* Output file descriptor */
59 tin, /* Input file descriptor */
63 struct tchars otc = { 0 }, ntc = { 0 };
64 struct ltchars oltc = { 0 }, nltc = { 0 };
65 struct sgttyb ottyb = { 0 }, nttyb = { 0 };
67 # define cfgetispeed(ptr) (ptr)->sg_ispeed
68 # define cfgetospeed(ptr) (ptr)->sg_ospeed
71 #else /* USE_TERMIO */
72 struct termio old_tc = { 0, 0, 0, 0, {}, 0, 0 };
76 # define TCSANOW TCSETS
77 # define TCSADRAIN TCSETSW
78 # define tcgetattr(f, t) ioctl(f, TCGETS, (char *)t)
81 # define TCSANOW TCSETA
82 # define TCSADRAIN TCSETAW
83 # define tcgetattr(f, t) ioctl(f, TCGETA, (char *)t)
85 # define TCSANOW TIOCSETA
86 # define TCSADRAIN TIOCSETAW
87 # define tcgetattr(f, t) ioctl(f, TIOCGETA, (char *)t)
90 # define tcsetattr(f, a, t) ioctl(f, a, (char *)t)
91 # define cfgetospeed(ptr) ((ptr)->c_cflag&CBAUD)
93 # define cfgetispeed(ptr) (((ptr)->c_cflag&CIBAUD) >> IBSHIFT)
95 # define cfgetispeed(ptr) cfgetospeed(ptr)
99 # define TIOCFLUSH TC_PX_DRAIN
101 #endif /* USE_TERMIO */
103 static fd_set *ibitsp, *obitsp, *xbitsp;
107 static SIG_FUNC_RET intr(int);
110 static SIG_FUNC_RET intr2(int);
113 static SIG_FUNC_RET susp(int);
116 static SIG_FUNC_RET ayt(int);
122 tout = fileno(stdout);
128 TerminalWrite(char *buf, int n)
130 return write(tout, buf, n);
134 TerminalRead(char *buf, int n)
136 return read(tin, buf, n);
144 TerminalAutoFlush(void)
149 ioctl(0, TIOCLGET, (char *)&flush);
150 return !(flush&LNOFLSH); /* if LNOFLSH, no autoflush */
156 #ifdef KLUDGELINEMODE
157 extern int kludgelinemode;
160 * TerminalSpecialChars()
162 * Look at an input character to see if it is a special character
163 * and decide what to do.
167 * 0 Don't add this character.
168 * 1 Do add this character
172 TerminalSpecialChars(int c)
174 if (c == termIntChar) {
177 } else if (c == termQuitChar) {
178 #ifdef KLUDGELINEMODE
185 } else if (c == termEofChar) {
186 if (my_want_state_is_will(TELOPT_LINEMODE)) {
191 } else if (c == termSuspChar) {
194 } else if (c == termFlushChar) {
195 xmitAO(); /* Transmit Abort Output */
197 } else if (!MODE_LOCAL_CHARS(globalmode)) {
198 if (c == termKillChar) {
201 } else if (c == termEraseChar) {
202 xmitEC(); /* Transmit Erase Character */
211 * Flush output to the terminal
215 TerminalFlushOutput(void)
218 (void) ioctl(fileno(stdout), TIOCFLUSH, NULL);
220 (void) ioctl(fileno(stdout), TCFLSH, NULL);
225 TerminalSaveState(void)
228 ioctl(0, TIOCGETP, (char *)&ottyb);
229 ioctl(0, TIOCGETC, (char *)&otc);
230 ioctl(0, TIOCGLTC, (char *)&oltc);
231 ioctl(0, TIOCLGET, (char *)&olmode);
237 #else /* USE_TERMIO */
238 tcgetattr(0, &old_tc);
243 termFlushChar = CONTROL('O');
246 termWerasChar = CONTROL('W');
249 termRprntChar = CONTROL('R');
252 termLiteralNextChar = CONTROL('V');
255 termStartChar = CONTROL('Q');
258 termStopChar = CONTROL('S');
261 termAytChar = CONTROL('T');
263 #endif /* USE_TERMIO */
270 case SLC_IP: return(&termIntChar);
271 case SLC_ABORT: return(&termQuitChar);
272 case SLC_EOF: return(&termEofChar);
273 case SLC_EC: return(&termEraseChar);
274 case SLC_EL: return(&termKillChar);
275 case SLC_XON: return(&termStartChar);
276 case SLC_XOFF: return(&termStopChar);
277 case SLC_FORW1: return(&termForw1Char);
279 case SLC_FORW2: return(&termForw2Char);
281 case SLC_AO: return(&termFlushChar);
284 case SLC_SUSP: return(&termSuspChar);
287 case SLC_EW: return(&termWerasChar);
290 case SLC_RP: return(&termRprntChar);
293 case SLC_LNEXT: return(&termLiteralNextChar);
296 case SLC_AYT: return(&termAytChar);
309 TerminalDefaultChars(void)
314 nttyb.sg_kill = ottyb.sg_kill;
315 nttyb.sg_erase = ottyb.sg_erase;
316 #else /* USE_TERMIO */
317 memcpy(new_tc.c_cc, old_tc.c_cc, sizeof(old_tc.c_cc));
319 termFlushChar = CONTROL('O');
322 termWerasChar = CONTROL('W');
325 termRprntChar = CONTROL('R');
328 termLiteralNextChar = CONTROL('V');
331 termStartChar = CONTROL('Q');
334 termStopChar = CONTROL('S');
337 termAytChar = CONTROL('T');
339 #endif /* USE_TERMIO */
343 * TerminalNewMode - set up terminal to a specific mode.
344 * MODE_ECHO: do local terminal echo
345 * MODE_FLOW: do local flow control
346 * MODE_TRAPSIG: do local mapping to TELNET IAC sequences
347 * MODE_EDIT: do local line editing
350 * MODE_ECHO|MODE_EDIT|MODE_FLOW|MODE_TRAPSIG
354 * local signal mapping
358 * Both Linemode and Single Character mode:
361 * local/no signal mapping
365 TerminalNewMode(int f)
367 static int prevmode = 0;
373 #else /* USE_TERMIO */
374 struct termio tmp_tc;
375 #endif /* USE_TERMIO */
380 globalmode = f&~MODE_FORCE;
385 * Write any outstanding data before switching modes
386 * ttyflush() returns 0 only when there is no more data
387 * left to write out, it returns -1 if it couldn't do
388 * anything at all, otherwise it returns 1 + the number
389 * of characters left to write.
391 * We would really like ask the kernel to wait for the output
392 * to drain, like we can do with the TCSADRAIN, but we don't have
393 * that option. The only ioctl that waits for the output to
394 * drain, TIOCSETP, also flushes the input queue, which is NOT
395 * what we want (TIOCSETP is like TCSADFLUSH).
398 old = ttyflush(SYNCHing|flushout);
399 if (old < 0 || old > 1) {
401 tcgetattr(tin, &tmp_tc);
402 #endif /* USE_TERMIO */
405 * Wait for data to drain, then flush again.
408 tcsetattr(tin, TCSADRAIN, &tmp_tc);
409 #endif /* USE_TERMIO */
410 old = ttyflush(SYNCHing|flushout);
411 } while (old < 0 || old > 1);
415 prevmode = f&~MODE_FORCE;
429 tmp_tc.c_lflag |= ECHO;
430 tmp_tc.c_oflag |= ONLCR;
432 tmp_tc.c_iflag |= ICRNL;
436 sb.sg_flags &= ~ECHO;
438 tmp_tc.c_lflag &= ~ECHO;
439 tmp_tc.c_oflag &= ~ONLCR;
443 if ((f&MODE_FLOW) == 0) {
445 tc.t_startc = _POSIX_VDISABLE;
446 tc.t_stopc = _POSIX_VDISABLE;
448 tmp_tc.c_iflag &= ~(IXOFF|IXON); /* Leave the IXANY bit alone */
450 if (restartany < 0) {
451 tmp_tc.c_iflag |= IXOFF|IXON; /* Leave the IXANY bit alone */
452 } else if (restartany > 0) {
453 tmp_tc.c_iflag |= IXOFF|IXON|IXANY;
455 tmp_tc.c_iflag |= IXOFF|IXON;
456 tmp_tc.c_iflag &= ~IXANY;
461 if ((f&MODE_TRAPSIG) == 0) {
463 tc.t_intrc = _POSIX_VDISABLE;
464 tc.t_quitc = _POSIX_VDISABLE;
465 tc.t_eofc = _POSIX_VDISABLE;
466 ltc.t_suspc = _POSIX_VDISABLE;
467 ltc.t_dsuspc = _POSIX_VDISABLE;
469 tmp_tc.c_lflag &= ~ISIG;
474 tmp_tc.c_lflag |= ISIG;
481 sb.sg_flags &= ~CBREAK;
482 sb.sg_flags |= CRMOD;
484 tmp_tc.c_lflag |= ICANON;
488 sb.sg_flags |= CBREAK;
490 sb.sg_flags |= CRMOD;
492 sb.sg_flags &= ~CRMOD;
494 tmp_tc.c_lflag &= ~ICANON;
495 tmp_tc.c_iflag &= ~ICRNL;
496 tmp_tc.c_cc[VMIN] = 1;
497 tmp_tc.c_cc[VTIME] = 0;
501 if ((f&(MODE_EDIT|MODE_TRAPSIG)) == 0) {
503 ltc.t_lnextc = _POSIX_VDISABLE;
506 tmp_tc.c_cc[VLNEXT] = (cc_t)(_POSIX_VDISABLE);
511 if (f&MODE_SOFT_TAB) {
513 sb.sg_flags |= XTABS;
516 tmp_tc.c_oflag |= OXTABS;
519 tmp_tc.c_oflag &= ~TABDLY;
520 tmp_tc.c_oflag |= TAB3;
525 sb.sg_flags &= ~XTABS;
528 tmp_tc.c_oflag &= ~OXTABS;
531 tmp_tc.c_oflag &= ~TABDLY;
536 if (f&MODE_LIT_ECHO) {
541 tmp_tc.c_lflag &= ~ECHOCTL;
549 tmp_tc.c_lflag |= ECHOCTL;
569 tmp_tc.c_iflag &= ~ISTRIP;
571 tmp_tc.c_iflag |= ISTRIP;
572 if (f & MODE_OUTBIN) {
573 tmp_tc.c_cflag &= ~(CSIZE|PARENB);
574 tmp_tc.c_cflag |= CS8;
575 tmp_tc.c_oflag &= ~OPOST;
577 tmp_tc.c_cflag &= ~(CSIZE|PARENB);
578 tmp_tc.c_cflag |= old_tc.c_cflag & (CSIZE|PARENB);
579 tmp_tc.c_oflag |= OPOST;
587 (void) signal(SIGINT, intr);
590 (void) signal(SIGQUIT, intr2);
593 (void) signal(SIGTSTP, susp);
596 (void) signal(SIGINFO, ayt);
598 #if defined(USE_TERMIO) && defined(NOKERNINFO)
599 tmp_tc.c_lflag |= NOKERNINFO;
602 * We don't want to process ^Y here. It's just another
603 * character that we'll pass on to the back end. It has
604 * to process it because it will be processed when the
605 * user attempts to read it, not when we send it.
608 ltc.t_dsuspc = _POSIX_VDISABLE;
611 tmp_tc.c_cc[VDSUSP] = (cc_t)(_POSIX_VDISABLE);
616 * If the VEOL character is already set, then use VEOL2,
617 * otherwise use VEOL.
619 esc = (rlogin != _POSIX_VDISABLE) ? rlogin : escape;
620 if ((tmp_tc.c_cc[VEOL] != esc)
622 && (tmp_tc.c_cc[VEOL2] != esc)
625 if (tmp_tc.c_cc[VEOL] == (cc_t)(_POSIX_VDISABLE))
626 tmp_tc.c_cc[VEOL] = esc;
628 else if (tmp_tc.c_cc[VEOL2] == (cc_t)(_POSIX_VDISABLE))
629 tmp_tc.c_cc[VEOL2] = esc;
633 if (tc.t_brkc == (cc_t)(_POSIX_VDISABLE))
638 (void) signal(SIGINFO, (void (*)(int))ayt_status);
641 (void) signal(SIGINT, SIG_DFL);
644 (void) signal(SIGQUIT, SIG_DFL);
647 (void) signal(SIGTSTP, SIG_DFL);
649 (void) sigsetmask(sigblock(0) & ~(1<<(SIGTSTP-1)));
651 (void) sigrelse(SIGTSTP);
664 ioctl(tin, TIOCLSET, (char *)&lmode);
665 ioctl(tin, TIOCSLTC, (char *)<c);
666 ioctl(tin, TIOCSETC, (char *)&tc);
667 ioctl(tin, TIOCSETN, (char *)&sb);
669 if (tcsetattr(tin, TCSADRAIN, &tmp_tc) < 0)
670 tcsetattr(tin, TCSANOW, &tmp_tc);
673 ioctl(tin, FIONBIO, (char *)&onoff);
674 ioctl(tout, FIONBIO, (char *)&onoff);
679 * Try to guess whether speeds are "encoded" (4.2BSD) or just numeric (4.4BSD).
695 # define B19200 B14400
699 #define B28800 B19200
703 # define B38400 B28800
707 #define B57600 B38400
711 #define B76800 B57600
715 #define B115200 B76800
719 #define B230400 B115200
724 * This code assumes that the values B0, B50, B75...
725 * are in ascending order. They do not have to be
732 { 0, B0 }, { 50, B50 }, { 75, B75 },
733 { 110, B110 }, { 134, B134 }, { 150, B150 },
734 { 200, B200 }, { 300, B300 }, { 600, B600 },
735 { 1200, B1200 }, { 1800, B1800 }, { 2400, B2400 },
736 { 4800, B4800 }, { 7200, B7200 }, { 9600, B9600 },
737 { 14400, B14400 }, { 19200, B19200 }, { 28800, B28800 },
738 { 38400, B38400 }, { 57600, B57600 }, { 115200, B115200 },
739 { 230400, B230400 }, { -1, B230400 }
741 #endif /* DECODE_BAUD */
744 TerminalSpeeds(long *ispeed, long *ospeed)
747 struct termspeeds *tp;
748 #endif /* DECODE_BAUD */
751 out = cfgetospeed(&old_tc);
752 in = cfgetispeed(&old_tc);
758 while ((tp->speed != -1) && (tp->value < in))
763 while ((tp->speed != -1) && (tp->value < out))
766 #else /* DECODE_BAUD */
769 #endif /* DECODE_BAUD */
773 TerminalWindowSize(long *rows, long *cols)
778 if (ioctl(fileno(stdin), TIOCGWINSZ, (char *)&ws) >= 0) {
783 #endif /* TIOCGWINSZ */
794 NetNonblockingIO(int fd, int onoff)
796 ioctl(fd, FIONBIO, (char *)&onoff);
801 * Various signal handling routines.
806 deadpeer(int sig __unused)
809 longjmp(peerdied, -1);
814 intr(int sig __unused)
821 longjmp(toplevel, -1);
826 intr2(int sig __unused)
829 #ifdef KLUDGELINEMODE
842 susp(int sig __unused)
844 if ((rlogin != _POSIX_VDISABLE) && rlogin_susp())
854 sendwin(int sig __unused)
865 ayt(int sig __unused)
876 sys_telnet_init(void)
878 (void) signal(SIGINT, intr);
879 (void) signal(SIGQUIT, intr2);
880 (void) signal(SIGPIPE, deadpeer);
882 (void) signal(SIGWINCH, sendwin);
885 (void) signal(SIGTSTP, susp);
888 (void) signal(SIGINFO, ayt);
893 NetNonblockingIO(net, 1);
895 #if defined(SO_OOBINLINE)
896 if (SetSockOpt(net, SOL_SOCKET, SO_OOBINLINE, 1) == -1) {
897 perror("SetSockOpt");
899 #endif /* defined(SO_OOBINLINE) */
905 * This routine tries to fill up/empty our various rings.
907 * The parameter specifies whether this is a poll operation,
908 * or a block-until-something-happens operation.
910 * The return value is 1 if something happened, 0 if not.
914 process_rings(int netin, int netout, int netex, int ttyin, int ttyout, int poll)
918 static struct timeval TimeValue = { 0, 0 };
922 if ((netout || netin || netex) && net > maxfd)
925 if (ttyout && tout > maxfd)
927 if (ttyin && tin > maxfd)
929 tmp = howmany(maxfd+1, NFDBITS) * sizeof(fd_mask);
939 if ((ibitsp = (fd_set *)malloc(fdsn)) == NULL)
941 if ((obitsp = (fd_set *)malloc(fdsn)) == NULL)
943 if ((xbitsp = (fd_set *)malloc(fdsn)) == NULL)
945 memset(ibitsp, 0, fdsn);
946 memset(obitsp, 0, fdsn);
947 memset(xbitsp, 0, fdsn);
953 FD_SET(tout, obitsp);
960 if ((c = select(maxfd + 1, ibitsp, obitsp, xbitsp,
961 (poll == 0)? NULL : &TimeValue)) < 0) {
964 * we can get EINTR if we are in line mode,
965 * and the user does an escape (TSTP), or
966 * some other signal generator.
968 if (errno == EINTR) {
971 /* I don't like this, does it ever happen? */
972 printf("sleep(5) from telnet, after select: %s\r\n", strerror(errno));
981 if (FD_ISSET(net, xbitsp)) {
984 (void) ttyflush(1); /* flush already enqueued data */
988 * Something to read from the network...
990 if (FD_ISSET(net, ibitsp)) {
994 canread = ring_empty_consecutive(&netiring);
995 #if !defined(SO_OOBINLINE)
997 * In 4.2 (and some early 4.3) systems, the
998 * OOB indication and data handling in the kernel
999 * is such that if two separate TCP Urgent requests
1000 * come in, one byte of TCP data will be overlaid.
1001 * This is fatal for Telnet, but we try to live
1004 * In addition, in 4.2 (and...), a special protocol
1005 * is needed to pick up the TCP Urgent data in
1006 * the correct sequence.
1008 * What we do is: if we think we are in urgent
1009 * mode, we look to see if we are "at the mark".
1010 * If we are, we do an OOB receive. If we run
1011 * this twice, we will do the OOB receive twice,
1012 * but the second will fail, since the second
1013 * time we were "at the mark", but there wasn't
1014 * any data there (the kernel doesn't reset
1015 * "at the mark" until we do a normal read).
1016 * Once we've read the OOB data, we go ahead
1017 * and do normal reads.
1019 * There is also another problem, which is that
1020 * since the OOB byte we read doesn't put us
1021 * out of OOB state, and since that byte is most
1022 * likely the TELNET DM (data mark), we would
1023 * stay in the TELNET SYNCH (SYNCHing) state.
1024 * So, clocks to the rescue. If we've "just"
1025 * received a DM, then we test for the
1026 * presence of OOB data when the receive OOB
1027 * fails (and AFTER we did the normal mode read
1028 * to clear "at the mark").
1032 static int bogus_oob = 0, first = 1;
1034 ioctl(net, SIOCATMARK, (char *)&atmark);
1036 c = recv(net, netiring.supply, canread, MSG_OOB);
1037 if ((c == -1) && (errno == EINVAL)) {
1038 c = recv(net, netiring.supply, canread, 0);
1039 if (clocks.didnetreceive < clocks.gotDM) {
1040 SYNCHing = stilloob(net);
1042 } else if (first && c > 0) {
1044 * Bogosity check. Systems based on 4.2BSD
1045 * do not return an error if you do a second
1046 * recv(MSG_OOB). So, we do one. If it
1047 * succeeds and returns exactly the same
1048 * data, then assume that we are running
1049 * on a broken system and set the bogus_oob
1050 * flag. (If the data was different, then
1051 * we probably got some valid new data, so
1052 * increment the count...)
1055 i = recv(net, netiring.supply + c, canread - c, MSG_OOB);
1057 memcmp(netiring.supply, netiring.supply + c, i) == 0) {
1066 if (bogus_oob && c > 0) {
1069 * Bogosity. We have to do the read
1070 * to clear the atmark to get out of
1073 i = read(net, netiring.supply + c, canread - c);
1078 c = recv(net, netiring.supply, canread, 0);
1081 c = recv(net, netiring.supply, canread, 0);
1083 settimer(didnetreceive);
1084 #else /* !defined(SO_OOBINLINE) */
1085 c = recv(net, (char *)netiring.supply, canread, 0);
1086 #endif /* !defined(SO_OOBINLINE) */
1087 if (c < 0 && errno == EWOULDBLOCK) {
1089 } else if (c <= 0) {
1093 Dump('<', netiring.supply, c);
1096 ring_supplied(&netiring, c);
1101 * Something to read from the tty...
1103 if (FD_ISSET(tin, ibitsp)) {
1104 FD_CLR(tin, ibitsp);
1105 c = TerminalRead(ttyiring.supply, ring_empty_consecutive(&ttyiring));
1106 if (c < 0 && errno == EIO)
1108 if (c < 0 && errno == EWOULDBLOCK) {
1111 /* EOF detection for line mode!!!! */
1112 if ((c == 0) && MODE_LOCAL_CHARS(globalmode) && isatty(tin)) {
1113 /* must be an EOF... */
1114 *ttyiring.supply = termEofChar;
1121 Dump('<', ttyiring.supply, c);
1123 ring_supplied(&ttyiring, c);
1125 returnValue = 1; /* did something useful */
1128 if (FD_ISSET(net, obitsp)) {
1129 FD_CLR(net, obitsp);
1130 returnValue |= netflush();
1132 if (FD_ISSET(tout, obitsp)) {
1133 FD_CLR(tout, obitsp);
1134 returnValue |= (ttyflush(SYNCHing|flushout) > 0);