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/usr.bin/telnet/sys_bsd.c,v 1.5.6.1 2002/04/13 11:07:13 markm Exp $
35 * $DragonFly: src/usr.bin/telnet/sys_bsd.c,v 1.3 2005/02/28 16:55:39 joerg Exp $
38 #include <sys/cdefs.h>
43 * The following routines try to encapsulate what is system dependent
44 * (at least between 4.x and dos) which is used in telnet.c.
47 #include <sys/types.h>
48 #include <sys/socket.h>
54 #include <arpa/telnet.h>
63 tout, /* Output file descriptor */
64 tin, /* Input file descriptor */
68 struct tchars otc = { 0 }, ntc = { 0 };
69 struct ltchars oltc = { 0 }, nltc = { 0 };
70 struct sgttyb ottyb = { 0 }, nttyb = { 0 };
72 # define cfgetispeed(ptr) (ptr)->sg_ispeed
73 # define cfgetospeed(ptr) (ptr)->sg_ospeed
76 #else /* USE_TERMIO */
77 struct termio old_tc = { 0, 0, 0, 0, {}, 0, 0 };
81 # define TCSANOW TCSETS
82 # define TCSADRAIN TCSETSW
83 # define tcgetattr(f, t) ioctl(f, TCGETS, (char *)t)
86 # define TCSANOW TCSETA
87 # define TCSADRAIN TCSETAW
88 # define tcgetattr(f, t) ioctl(f, TCGETA, (char *)t)
90 # define TCSANOW TIOCSETA
91 # define TCSADRAIN TIOCSETAW
92 # define tcgetattr(f, t) ioctl(f, TIOCGETA, (char *)t)
95 # define tcsetattr(f, a, t) ioctl(f, a, (char *)t)
96 # define cfgetospeed(ptr) ((ptr)->c_cflag&CBAUD)
98 # define cfgetispeed(ptr) (((ptr)->c_cflag&CIBAUD) >> IBSHIFT)
100 # define cfgetispeed(ptr) cfgetospeed(ptr)
102 # endif /* TCSANOW */
104 # define TIOCFLUSH TC_PX_DRAIN
106 #endif /* USE_TERMIO */
108 static fd_set *ibitsp, *obitsp, *xbitsp;
112 static SIG_FUNC_RET intr(int);
115 static SIG_FUNC_RET intr2(int);
118 static SIG_FUNC_RET susp(int);
121 static SIG_FUNC_RET ayt(int);
127 tout = fileno(stdout);
133 TerminalWrite(char *buf, int n)
135 return write(tout, buf, n);
139 TerminalRead(char *buf, int n)
141 return read(tin, buf, n);
149 TerminalAutoFlush(void)
154 ioctl(0, TIOCLGET, (char *)&flush);
155 return !(flush&LNOFLSH); /* if LNOFLSH, no autoflush */
161 #ifdef KLUDGELINEMODE
162 extern int kludgelinemode;
165 * TerminalSpecialChars()
167 * Look at an input character to see if it is a special character
168 * and decide what to do.
172 * 0 Don't add this character.
173 * 1 Do add this character
177 TerminalSpecialChars(int c)
179 if (c == termIntChar) {
182 } else if (c == termQuitChar) {
183 #ifdef KLUDGELINEMODE
190 } else if (c == termEofChar) {
191 if (my_want_state_is_will(TELOPT_LINEMODE)) {
196 } else if (c == termSuspChar) {
199 } else if (c == termFlushChar) {
200 xmitAO(); /* Transmit Abort Output */
202 } else if (!MODE_LOCAL_CHARS(globalmode)) {
203 if (c == termKillChar) {
206 } else if (c == termEraseChar) {
207 xmitEC(); /* Transmit Erase Character */
216 * Flush output to the terminal
220 TerminalFlushOutput(void)
223 (void) ioctl(fileno(stdout), TIOCFLUSH, (char *) 0);
225 (void) ioctl(fileno(stdout), TCFLSH, (char *) 0);
230 TerminalSaveState(void)
233 ioctl(0, TIOCGETP, (char *)&ottyb);
234 ioctl(0, TIOCGETC, (char *)&otc);
235 ioctl(0, TIOCGLTC, (char *)&oltc);
236 ioctl(0, TIOCLGET, (char *)&olmode);
242 #else /* USE_TERMIO */
243 tcgetattr(0, &old_tc);
248 termFlushChar = CONTROL('O');
251 termWerasChar = CONTROL('W');
254 termRprntChar = CONTROL('R');
257 termLiteralNextChar = CONTROL('V');
260 termStartChar = CONTROL('Q');
263 termStopChar = CONTROL('S');
266 termAytChar = CONTROL('T');
268 #endif /* USE_TERMIO */
275 case SLC_IP: return(&termIntChar);
276 case SLC_ABORT: return(&termQuitChar);
277 case SLC_EOF: return(&termEofChar);
278 case SLC_EC: return(&termEraseChar);
279 case SLC_EL: return(&termKillChar);
280 case SLC_XON: return(&termStartChar);
281 case SLC_XOFF: return(&termStopChar);
282 case SLC_FORW1: return(&termForw1Char);
284 case SLC_FORW2: return(&termForw2Char);
286 case SLC_AO: return(&termFlushChar);
289 case SLC_SUSP: return(&termSuspChar);
292 case SLC_EW: return(&termWerasChar);
295 case SLC_RP: return(&termRprntChar);
298 case SLC_LNEXT: return(&termLiteralNextChar);
301 case SLC_AYT: return(&termAytChar);
314 TerminalDefaultChars(void)
319 nttyb.sg_kill = ottyb.sg_kill;
320 nttyb.sg_erase = ottyb.sg_erase;
321 #else /* USE_TERMIO */
322 memcpy(new_tc.c_cc, old_tc.c_cc, sizeof(old_tc.c_cc));
324 termFlushChar = CONTROL('O');
327 termWerasChar = CONTROL('W');
330 termRprntChar = CONTROL('R');
333 termLiteralNextChar = CONTROL('V');
336 termStartChar = CONTROL('Q');
339 termStopChar = CONTROL('S');
342 termAytChar = CONTROL('T');
344 #endif /* USE_TERMIO */
348 * TerminalNewMode - set up terminal to a specific mode.
349 * MODE_ECHO: do local terminal echo
350 * MODE_FLOW: do local flow control
351 * MODE_TRAPSIG: do local mapping to TELNET IAC sequences
352 * MODE_EDIT: do local line editing
355 * MODE_ECHO|MODE_EDIT|MODE_FLOW|MODE_TRAPSIG
359 * local signal mapping
363 * Both Linemode and Single Character mode:
366 * local/no signal mapping
370 TerminalNewMode(int f)
372 static int prevmode = 0;
378 #else /* USE_TERMIO */
379 struct termio tmp_tc;
380 #endif /* USE_TERMIO */
385 globalmode = f&~MODE_FORCE;
390 * Write any outstanding data before switching modes
391 * ttyflush() returns 0 only when there is no more data
392 * left to write out, it returns -1 if it couldn't do
393 * anything at all, otherwise it returns 1 + the number
394 * of characters left to write.
396 * We would really like ask the kernel to wait for the output
397 * to drain, like we can do with the TCSADRAIN, but we don't have
398 * that option. The only ioctl that waits for the output to
399 * drain, TIOCSETP, also flushes the input queue, which is NOT
400 * what we want (TIOCSETP is like TCSADFLUSH).
403 old = ttyflush(SYNCHing|flushout);
404 if (old < 0 || old > 1) {
406 tcgetattr(tin, &tmp_tc);
407 #endif /* USE_TERMIO */
410 * Wait for data to drain, then flush again.
413 tcsetattr(tin, TCSADRAIN, &tmp_tc);
414 #endif /* USE_TERMIO */
415 old = ttyflush(SYNCHing|flushout);
416 } while (old < 0 || old > 1);
420 prevmode = f&~MODE_FORCE;
434 tmp_tc.c_lflag |= ECHO;
435 tmp_tc.c_oflag |= ONLCR;
437 tmp_tc.c_iflag |= ICRNL;
441 sb.sg_flags &= ~ECHO;
443 tmp_tc.c_lflag &= ~ECHO;
444 tmp_tc.c_oflag &= ~ONLCR;
448 if ((f&MODE_FLOW) == 0) {
450 tc.t_startc = _POSIX_VDISABLE;
451 tc.t_stopc = _POSIX_VDISABLE;
453 tmp_tc.c_iflag &= ~(IXOFF|IXON); /* Leave the IXANY bit alone */
455 if (restartany < 0) {
456 tmp_tc.c_iflag |= IXOFF|IXON; /* Leave the IXANY bit alone */
457 } else if (restartany > 0) {
458 tmp_tc.c_iflag |= IXOFF|IXON|IXANY;
460 tmp_tc.c_iflag |= IXOFF|IXON;
461 tmp_tc.c_iflag &= ~IXANY;
466 if ((f&MODE_TRAPSIG) == 0) {
468 tc.t_intrc = _POSIX_VDISABLE;
469 tc.t_quitc = _POSIX_VDISABLE;
470 tc.t_eofc = _POSIX_VDISABLE;
471 ltc.t_suspc = _POSIX_VDISABLE;
472 ltc.t_dsuspc = _POSIX_VDISABLE;
474 tmp_tc.c_lflag &= ~ISIG;
479 tmp_tc.c_lflag |= ISIG;
486 sb.sg_flags &= ~CBREAK;
487 sb.sg_flags |= CRMOD;
489 tmp_tc.c_lflag |= ICANON;
493 sb.sg_flags |= CBREAK;
495 sb.sg_flags |= CRMOD;
497 sb.sg_flags &= ~CRMOD;
499 tmp_tc.c_lflag &= ~ICANON;
500 tmp_tc.c_iflag &= ~ICRNL;
501 tmp_tc.c_cc[VMIN] = 1;
502 tmp_tc.c_cc[VTIME] = 0;
506 if ((f&(MODE_EDIT|MODE_TRAPSIG)) == 0) {
508 ltc.t_lnextc = _POSIX_VDISABLE;
511 tmp_tc.c_cc[VLNEXT] = (cc_t)(_POSIX_VDISABLE);
516 if (f&MODE_SOFT_TAB) {
518 sb.sg_flags |= XTABS;
521 tmp_tc.c_oflag |= OXTABS;
524 tmp_tc.c_oflag &= ~TABDLY;
525 tmp_tc.c_oflag |= TAB3;
530 sb.sg_flags &= ~XTABS;
533 tmp_tc.c_oflag &= ~OXTABS;
536 tmp_tc.c_oflag &= ~TABDLY;
541 if (f&MODE_LIT_ECHO) {
546 tmp_tc.c_lflag &= ~ECHOCTL;
554 tmp_tc.c_lflag |= ECHOCTL;
574 tmp_tc.c_iflag &= ~ISTRIP;
576 tmp_tc.c_iflag |= ISTRIP;
577 if (f & MODE_OUTBIN) {
578 tmp_tc.c_cflag &= ~(CSIZE|PARENB);
579 tmp_tc.c_cflag |= CS8;
580 tmp_tc.c_oflag &= ~OPOST;
582 tmp_tc.c_cflag &= ~(CSIZE|PARENB);
583 tmp_tc.c_cflag |= old_tc.c_cflag & (CSIZE|PARENB);
584 tmp_tc.c_oflag |= OPOST;
592 (void) signal(SIGINT, intr);
595 (void) signal(SIGQUIT, intr2);
598 (void) signal(SIGTSTP, susp);
601 (void) signal(SIGINFO, ayt);
603 #if defined(USE_TERMIO) && defined(NOKERNINFO)
604 tmp_tc.c_lflag |= NOKERNINFO;
607 * We don't want to process ^Y here. It's just another
608 * character that we'll pass on to the back end. It has
609 * to process it because it will be processed when the
610 * user attempts to read it, not when we send it.
613 ltc.t_dsuspc = _POSIX_VDISABLE;
616 tmp_tc.c_cc[VDSUSP] = (cc_t)(_POSIX_VDISABLE);
621 * If the VEOL character is already set, then use VEOL2,
622 * otherwise use VEOL.
624 esc = (rlogin != _POSIX_VDISABLE) ? rlogin : escape;
625 if ((tmp_tc.c_cc[VEOL] != esc)
627 && (tmp_tc.c_cc[VEOL2] != esc)
630 if (tmp_tc.c_cc[VEOL] == (cc_t)(_POSIX_VDISABLE))
631 tmp_tc.c_cc[VEOL] = esc;
633 else if (tmp_tc.c_cc[VEOL2] == (cc_t)(_POSIX_VDISABLE))
634 tmp_tc.c_cc[VEOL2] = esc;
638 if (tc.t_brkc == (cc_t)(_POSIX_VDISABLE))
643 (void) signal(SIGINFO, (void (*)(int))ayt_status);
646 (void) signal(SIGINT, SIG_DFL);
649 (void) signal(SIGQUIT, SIG_DFL);
652 (void) signal(SIGTSTP, SIG_DFL);
654 (void) sigsetmask(sigblock(0) & ~(1<<(SIGTSTP-1)));
656 (void) sigrelse(SIGTSTP);
669 ioctl(tin, TIOCLSET, (char *)&lmode);
670 ioctl(tin, TIOCSLTC, (char *)<c);
671 ioctl(tin, TIOCSETC, (char *)&tc);
672 ioctl(tin, TIOCSETN, (char *)&sb);
674 if (tcsetattr(tin, TCSADRAIN, &tmp_tc) < 0)
675 tcsetattr(tin, TCSANOW, &tmp_tc);
678 ioctl(tin, FIONBIO, (char *)&onoff);
679 ioctl(tout, FIONBIO, (char *)&onoff);
684 * Try to guess whether speeds are "encoded" (4.2BSD) or just numeric (4.4BSD).
700 # define B19200 B14400
704 #define B28800 B19200
708 # define B38400 B28800
712 #define B57600 B38400
716 #define B76800 B57600
720 #define B115200 B76800
724 #define B230400 B115200
729 * This code assumes that the values B0, B50, B75...
730 * are in ascending order. They do not have to be
737 { 0, B0 }, { 50, B50 }, { 75, B75 },
738 { 110, B110 }, { 134, B134 }, { 150, B150 },
739 { 200, B200 }, { 300, B300 }, { 600, B600 },
740 { 1200, B1200 }, { 1800, B1800 }, { 2400, B2400 },
741 { 4800, B4800 }, { 7200, B7200 }, { 9600, B9600 },
742 { 14400, B14400 }, { 19200, B19200 }, { 28800, B28800 },
743 { 38400, B38400 }, { 57600, B57600 }, { 115200, B115200 },
744 { 230400, B230400 }, { -1, B230400 }
746 #endif /* DECODE_BAUD */
749 TerminalSpeeds(long *ispeed, long *ospeed)
752 struct termspeeds *tp;
753 #endif /* DECODE_BAUD */
756 out = cfgetospeed(&old_tc);
757 in = cfgetispeed(&old_tc);
763 while ((tp->speed != -1) && (tp->value < in))
768 while ((tp->speed != -1) && (tp->value < out))
771 #else /* DECODE_BAUD */
774 #endif /* DECODE_BAUD */
778 TerminalWindowSize(long *rows, long *cols)
783 if (ioctl(fileno(stdin), TIOCGWINSZ, (char *)&ws) >= 0) {
788 #endif /* TIOCGWINSZ */
799 NetNonblockingIO(int fd, int onoff)
801 ioctl(fd, FIONBIO, (char *)&onoff);
806 * Various signal handling routines.
811 deadpeer(int sig __unused)
814 longjmp(peerdied, -1);
819 intr(int sig __unused)
826 longjmp(toplevel, -1);
831 intr2(int sig __unused)
834 #ifdef KLUDGELINEMODE
847 susp(int sig __unused)
849 if ((rlogin != _POSIX_VDISABLE) && rlogin_susp())
859 sendwin(int sig __unused)
870 ayt(int sig __unused)
881 sys_telnet_init(void)
883 (void) signal(SIGINT, intr);
884 (void) signal(SIGQUIT, intr2);
885 (void) signal(SIGPIPE, deadpeer);
887 (void) signal(SIGWINCH, sendwin);
890 (void) signal(SIGTSTP, susp);
893 (void) signal(SIGINFO, ayt);
898 NetNonblockingIO(net, 1);
900 #if defined(SO_OOBINLINE)
901 if (SetSockOpt(net, SOL_SOCKET, SO_OOBINLINE, 1) == -1) {
902 perror("SetSockOpt");
904 #endif /* defined(SO_OOBINLINE) */
910 * This routine tries to fill up/empty our various rings.
912 * The parameter specifies whether this is a poll operation,
913 * or a block-until-something-happens operation.
915 * The return value is 1 if something happened, 0 if not.
919 process_rings(int netin, int netout, int netex, int ttyin, int ttyout, int poll)
923 static struct timeval TimeValue = { 0, 0 };
927 if ((netout || netin || netex) && net > maxfd)
930 if (ttyout && tout > maxfd)
932 if (ttyin && tin > maxfd)
934 tmp = howmany(maxfd+1, NFDBITS) * sizeof(fd_mask);
944 if ((ibitsp = (fd_set *)malloc(fdsn)) == NULL)
946 if ((obitsp = (fd_set *)malloc(fdsn)) == NULL)
948 if ((xbitsp = (fd_set *)malloc(fdsn)) == NULL)
950 memset(ibitsp, 0, fdsn);
951 memset(obitsp, 0, fdsn);
952 memset(xbitsp, 0, fdsn);
958 FD_SET(tout, obitsp);
965 if ((c = select(maxfd + 1, ibitsp, obitsp, xbitsp,
966 (poll == 0)? (struct timeval *)0 : &TimeValue)) < 0) {
969 * we can get EINTR if we are in line mode,
970 * and the user does an escape (TSTP), or
971 * some other signal generator.
973 if (errno == EINTR) {
976 /* I don't like this, does it ever happen? */
977 printf("sleep(5) from telnet, after select: %s\r\n", strerror(errno));
986 if (FD_ISSET(net, xbitsp)) {
989 (void) ttyflush(1); /* flush already enqueued data */
993 * Something to read from the network...
995 if (FD_ISSET(net, ibitsp)) {
999 canread = ring_empty_consecutive(&netiring);
1000 #if !defined(SO_OOBINLINE)
1002 * In 4.2 (and some early 4.3) systems, the
1003 * OOB indication and data handling in the kernel
1004 * is such that if two separate TCP Urgent requests
1005 * come in, one byte of TCP data will be overlaid.
1006 * This is fatal for Telnet, but we try to live
1009 * In addition, in 4.2 (and...), a special protocol
1010 * is needed to pick up the TCP Urgent data in
1011 * the correct sequence.
1013 * What we do is: if we think we are in urgent
1014 * mode, we look to see if we are "at the mark".
1015 * If we are, we do an OOB receive. If we run
1016 * this twice, we will do the OOB receive twice,
1017 * but the second will fail, since the second
1018 * time we were "at the mark", but there wasn't
1019 * any data there (the kernel doesn't reset
1020 * "at the mark" until we do a normal read).
1021 * Once we've read the OOB data, we go ahead
1022 * and do normal reads.
1024 * There is also another problem, which is that
1025 * since the OOB byte we read doesn't put us
1026 * out of OOB state, and since that byte is most
1027 * likely the TELNET DM (data mark), we would
1028 * stay in the TELNET SYNCH (SYNCHing) state.
1029 * So, clocks to the rescue. If we've "just"
1030 * received a DM, then we test for the
1031 * presence of OOB data when the receive OOB
1032 * fails (and AFTER we did the normal mode read
1033 * to clear "at the mark").
1037 static int bogus_oob = 0, first = 1;
1039 ioctl(net, SIOCATMARK, (char *)&atmark);
1041 c = recv(net, netiring.supply, canread, MSG_OOB);
1042 if ((c == -1) && (errno == EINVAL)) {
1043 c = recv(net, netiring.supply, canread, 0);
1044 if (clocks.didnetreceive < clocks.gotDM) {
1045 SYNCHing = stilloob(net);
1047 } else if (first && c > 0) {
1049 * Bogosity check. Systems based on 4.2BSD
1050 * do not return an error if you do a second
1051 * recv(MSG_OOB). So, we do one. If it
1052 * succeeds and returns exactly the same
1053 * data, then assume that we are running
1054 * on a broken system and set the bogus_oob
1055 * flag. (If the data was different, then
1056 * we probably got some valid new data, so
1057 * increment the count...)
1060 i = recv(net, netiring.supply + c, canread - c, MSG_OOB);
1062 memcmp(netiring.supply, netiring.supply + c, i) == 0) {
1071 if (bogus_oob && c > 0) {
1074 * Bogosity. We have to do the read
1075 * to clear the atmark to get out of
1078 i = read(net, netiring.supply + c, canread - c);
1083 c = recv(net, netiring.supply, canread, 0);
1086 c = recv(net, netiring.supply, canread, 0);
1088 settimer(didnetreceive);
1089 #else /* !defined(SO_OOBINLINE) */
1090 c = recv(net, (char *)netiring.supply, canread, 0);
1091 #endif /* !defined(SO_OOBINLINE) */
1092 if (c < 0 && errno == EWOULDBLOCK) {
1094 } else if (c <= 0) {
1098 Dump('<', netiring.supply, c);
1101 ring_supplied(&netiring, c);
1106 * Something to read from the tty...
1108 if (FD_ISSET(tin, ibitsp)) {
1109 FD_CLR(tin, ibitsp);
1110 c = TerminalRead(ttyiring.supply, ring_empty_consecutive(&ttyiring));
1111 if (c < 0 && errno == EIO)
1113 if (c < 0 && errno == EWOULDBLOCK) {
1116 /* EOF detection for line mode!!!! */
1117 if ((c == 0) && MODE_LOCAL_CHARS(globalmode) && isatty(tin)) {
1118 /* must be an EOF... */
1119 *ttyiring.supply = termEofChar;
1126 Dump('<', ttyiring.supply, c);
1128 ring_supplied(&ttyiring, c);
1130 returnValue = 1; /* did something useful */
1133 if (FD_ISSET(net, obitsp)) {
1134 FD_CLR(net, obitsp);
1135 returnValue |= netflush();
1137 if (FD_ISSET(tout, obitsp)) {
1138 FD_CLR(tout, obitsp);
1139 returnValue |= (ttyflush(SYNCHing|flushout) > 0);