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
36 static const char sccsid[] = "@(#)sys_bsd.c 8.4 (Berkeley) 5/30/95";
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
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/param.h>
48 #include <sys/socket.h>
56 #include <arpa/telnet.h>
65 tout, /* Output file descriptor */
66 tin, /* Input file descriptor */
70 struct tchars otc = { 0 }, ntc = { 0 };
71 struct ltchars oltc = { 0 }, nltc = { 0 };
72 struct sgttyb ottyb = { 0 }, nttyb = { 0 };
74 # define cfgetispeed(ptr) (ptr)->sg_ispeed
75 # define cfgetospeed(ptr) (ptr)->sg_ospeed
78 #else /* USE_TERMIO */
79 struct termio old_tc = { 0, 0, 0, 0, {}, 0, 0 };
83 # define TCSANOW TCSETS
84 # define TCSADRAIN TCSETSW
85 # define tcgetattr(f, t) ioctl(f, TCGETS, (char *)t)
88 # define TCSANOW TCSETA
89 # define TCSADRAIN TCSETAW
90 # define tcgetattr(f, t) ioctl(f, TCGETA, (char *)t)
92 # define TCSANOW TIOCSETA
93 # define TCSADRAIN TIOCSETAW
94 # define tcgetattr(f, t) ioctl(f, TIOCGETA, (char *)t)
97 # define tcsetattr(f, a, t) ioctl(f, a, (char *)t)
98 # define cfgetospeed(ptr) ((ptr)->c_cflag&CBAUD)
100 # define cfgetispeed(ptr) (((ptr)->c_cflag&CIBAUD) >> IBSHIFT)
102 # define cfgetispeed(ptr) cfgetospeed(ptr)
104 # endif /* TCSANOW */
106 # define TIOCFLUSH TC_PX_DRAIN
108 #endif /* USE_TERMIO */
110 static fd_set *ibitsp, *obitsp, *xbitsp;
114 static SIG_FUNC_RET intr(int);
117 static SIG_FUNC_RET intr2(int);
120 static SIG_FUNC_RET susp(int);
123 static SIG_FUNC_RET ayt(int);
129 tout = fileno(stdout);
135 TerminalWrite(char *buf, int n)
137 return write(tout, buf, n);
141 TerminalRead(char *buf, int n)
143 return read(tin, buf, n);
151 TerminalAutoFlush(void)
156 ioctl(0, TIOCLGET, (char *)&flush);
157 return !(flush&LNOFLSH); /* if LNOFLSH, no autoflush */
163 #ifdef KLUDGELINEMODE
164 extern int kludgelinemode;
167 * TerminalSpecialChars()
169 * Look at an input character to see if it is a special character
170 * and decide what to do.
174 * 0 Don't add this character.
175 * 1 Do add this character
179 TerminalSpecialChars(int c)
181 if (c == termIntChar) {
184 } else if (c == termQuitChar) {
185 #ifdef KLUDGELINEMODE
192 } else if (c == termEofChar) {
193 if (my_want_state_is_will(TELOPT_LINEMODE)) {
198 } else if (c == termSuspChar) {
201 } else if (c == termFlushChar) {
202 xmitAO(); /* Transmit Abort Output */
204 } else if (!MODE_LOCAL_CHARS(globalmode)) {
205 if (c == termKillChar) {
208 } else if (c == termEraseChar) {
209 xmitEC(); /* Transmit Erase Character */
218 * Flush output to the terminal
222 TerminalFlushOutput(void)
225 (void) ioctl(fileno(stdout), TIOCFLUSH, (char *) 0);
227 (void) ioctl(fileno(stdout), TCFLSH, (char *) 0);
232 TerminalSaveState(void)
235 ioctl(0, TIOCGETP, (char *)&ottyb);
236 ioctl(0, TIOCGETC, (char *)&otc);
237 ioctl(0, TIOCGLTC, (char *)&oltc);
238 ioctl(0, TIOCLGET, (char *)&olmode);
244 #else /* USE_TERMIO */
245 tcgetattr(0, &old_tc);
250 termFlushChar = CONTROL('O');
253 termWerasChar = CONTROL('W');
256 termRprntChar = CONTROL('R');
259 termLiteralNextChar = CONTROL('V');
262 termStartChar = CONTROL('Q');
265 termStopChar = CONTROL('S');
268 termAytChar = CONTROL('T');
270 #endif /* USE_TERMIO */
277 case SLC_IP: return(&termIntChar);
278 case SLC_ABORT: return(&termQuitChar);
279 case SLC_EOF: return(&termEofChar);
280 case SLC_EC: return(&termEraseChar);
281 case SLC_EL: return(&termKillChar);
282 case SLC_XON: return(&termStartChar);
283 case SLC_XOFF: return(&termStopChar);
284 case SLC_FORW1: return(&termForw1Char);
286 case SLC_FORW2: return(&termForw2Char);
288 case SLC_AO: return(&termFlushChar);
291 case SLC_SUSP: return(&termSuspChar);
294 case SLC_EW: return(&termWerasChar);
297 case SLC_RP: return(&termRprntChar);
300 case SLC_LNEXT: return(&termLiteralNextChar);
303 case SLC_AYT: return(&termAytChar);
316 TerminalDefaultChars(void)
321 nttyb.sg_kill = ottyb.sg_kill;
322 nttyb.sg_erase = ottyb.sg_erase;
323 #else /* USE_TERMIO */
324 memcpy(new_tc.c_cc, old_tc.c_cc, sizeof(old_tc.c_cc));
326 termFlushChar = CONTROL('O');
329 termWerasChar = CONTROL('W');
332 termRprntChar = CONTROL('R');
335 termLiteralNextChar = CONTROL('V');
338 termStartChar = CONTROL('Q');
341 termStopChar = CONTROL('S');
344 termAytChar = CONTROL('T');
346 #endif /* USE_TERMIO */
350 * TerminalNewMode - set up terminal to a specific mode.
351 * MODE_ECHO: do local terminal echo
352 * MODE_FLOW: do local flow control
353 * MODE_TRAPSIG: do local mapping to TELNET IAC sequences
354 * MODE_EDIT: do local line editing
357 * MODE_ECHO|MODE_EDIT|MODE_FLOW|MODE_TRAPSIG
361 * local signal mapping
365 * Both Linemode and Single Character mode:
368 * local/no signal mapping
372 TerminalNewMode(int f)
374 static int prevmode = 0;
380 #else /* USE_TERMIO */
381 struct termio tmp_tc;
382 #endif /* USE_TERMIO */
387 globalmode = f&~MODE_FORCE;
392 * Write any outstanding data before switching modes
393 * ttyflush() returns 0 only when there is no more data
394 * left to write out, it returns -1 if it couldn't do
395 * anything at all, otherwise it returns 1 + the number
396 * of characters left to write.
398 * We would really like ask the kernel to wait for the output
399 * to drain, like we can do with the TCSADRAIN, but we don't have
400 * that option. The only ioctl that waits for the output to
401 * drain, TIOCSETP, also flushes the input queue, which is NOT
402 * what we want (TIOCSETP is like TCSADFLUSH).
405 old = ttyflush(SYNCHing|flushout);
406 if (old < 0 || old > 1) {
408 tcgetattr(tin, &tmp_tc);
409 #endif /* USE_TERMIO */
412 * Wait for data to drain, then flush again.
415 tcsetattr(tin, TCSADRAIN, &tmp_tc);
416 #endif /* USE_TERMIO */
417 old = ttyflush(SYNCHing|flushout);
418 } while (old < 0 || old > 1);
422 prevmode = f&~MODE_FORCE;
436 tmp_tc.c_lflag |= ECHO;
437 tmp_tc.c_oflag |= ONLCR;
439 tmp_tc.c_iflag |= ICRNL;
443 sb.sg_flags &= ~ECHO;
445 tmp_tc.c_lflag &= ~ECHO;
446 tmp_tc.c_oflag &= ~ONLCR;
450 if ((f&MODE_FLOW) == 0) {
452 tc.t_startc = _POSIX_VDISABLE;
453 tc.t_stopc = _POSIX_VDISABLE;
455 tmp_tc.c_iflag &= ~(IXOFF|IXON); /* Leave the IXANY bit alone */
457 if (restartany < 0) {
458 tmp_tc.c_iflag |= IXOFF|IXON; /* Leave the IXANY bit alone */
459 } else if (restartany > 0) {
460 tmp_tc.c_iflag |= IXOFF|IXON|IXANY;
462 tmp_tc.c_iflag |= IXOFF|IXON;
463 tmp_tc.c_iflag &= ~IXANY;
468 if ((f&MODE_TRAPSIG) == 0) {
470 tc.t_intrc = _POSIX_VDISABLE;
471 tc.t_quitc = _POSIX_VDISABLE;
472 tc.t_eofc = _POSIX_VDISABLE;
473 ltc.t_suspc = _POSIX_VDISABLE;
474 ltc.t_dsuspc = _POSIX_VDISABLE;
476 tmp_tc.c_lflag &= ~ISIG;
481 tmp_tc.c_lflag |= ISIG;
488 sb.sg_flags &= ~CBREAK;
489 sb.sg_flags |= CRMOD;
491 tmp_tc.c_lflag |= ICANON;
495 sb.sg_flags |= CBREAK;
497 sb.sg_flags |= CRMOD;
499 sb.sg_flags &= ~CRMOD;
501 tmp_tc.c_lflag &= ~ICANON;
502 tmp_tc.c_iflag &= ~ICRNL;
503 tmp_tc.c_cc[VMIN] = 1;
504 tmp_tc.c_cc[VTIME] = 0;
508 if ((f&(MODE_EDIT|MODE_TRAPSIG)) == 0) {
510 ltc.t_lnextc = _POSIX_VDISABLE;
513 tmp_tc.c_cc[VLNEXT] = (cc_t)(_POSIX_VDISABLE);
518 if (f&MODE_SOFT_TAB) {
520 sb.sg_flags |= XTABS;
523 tmp_tc.c_oflag |= OXTABS;
526 tmp_tc.c_oflag &= ~TABDLY;
527 tmp_tc.c_oflag |= TAB3;
532 sb.sg_flags &= ~XTABS;
535 tmp_tc.c_oflag &= ~OXTABS;
538 tmp_tc.c_oflag &= ~TABDLY;
543 if (f&MODE_LIT_ECHO) {
548 tmp_tc.c_lflag &= ~ECHOCTL;
556 tmp_tc.c_lflag |= ECHOCTL;
576 tmp_tc.c_iflag &= ~ISTRIP;
578 tmp_tc.c_iflag |= ISTRIP;
579 if (f & MODE_OUTBIN) {
580 tmp_tc.c_cflag &= ~(CSIZE|PARENB);
581 tmp_tc.c_cflag |= CS8;
582 tmp_tc.c_oflag &= ~OPOST;
584 tmp_tc.c_cflag &= ~(CSIZE|PARENB);
585 tmp_tc.c_cflag |= old_tc.c_cflag & (CSIZE|PARENB);
586 tmp_tc.c_oflag |= OPOST;
594 (void) signal(SIGINT, intr);
597 (void) signal(SIGQUIT, intr2);
600 (void) signal(SIGTSTP, susp);
603 (void) signal(SIGINFO, ayt);
605 #if defined(USE_TERMIO) && defined(NOKERNINFO)
606 tmp_tc.c_lflag |= NOKERNINFO;
609 * We don't want to process ^Y here. It's just another
610 * character that we'll pass on to the back end. It has
611 * to process it because it will be processed when the
612 * user attempts to read it, not when we send it.
615 ltc.t_dsuspc = _POSIX_VDISABLE;
618 tmp_tc.c_cc[VDSUSP] = (cc_t)(_POSIX_VDISABLE);
623 * If the VEOL character is already set, then use VEOL2,
624 * otherwise use VEOL.
626 esc = (rlogin != _POSIX_VDISABLE) ? rlogin : escape;
627 if ((tmp_tc.c_cc[VEOL] != esc)
629 && (tmp_tc.c_cc[VEOL2] != esc)
632 if (tmp_tc.c_cc[VEOL] == (cc_t)(_POSIX_VDISABLE))
633 tmp_tc.c_cc[VEOL] = esc;
635 else if (tmp_tc.c_cc[VEOL2] == (cc_t)(_POSIX_VDISABLE))
636 tmp_tc.c_cc[VEOL2] = esc;
640 if (tc.t_brkc == (cc_t)(_POSIX_VDISABLE))
645 (void) signal(SIGINFO, (void (*)(int))ayt_status);
648 (void) signal(SIGINT, SIG_DFL);
651 (void) signal(SIGQUIT, SIG_DFL);
654 (void) signal(SIGTSTP, SIG_DFL);
656 (void) sigsetmask(sigblock(0) & ~(1<<(SIGTSTP-1)));
658 (void) sigrelse(SIGTSTP);
659 # endif /* SOLARIS */
671 ioctl(tin, TIOCLSET, (char *)&lmode);
672 ioctl(tin, TIOCSLTC, (char *)<c);
673 ioctl(tin, TIOCSETC, (char *)&tc);
674 ioctl(tin, TIOCSETN, (char *)&sb);
676 if (tcsetattr(tin, TCSADRAIN, &tmp_tc) < 0)
677 tcsetattr(tin, TCSANOW, &tmp_tc);
680 ioctl(tin, FIONBIO, (char *)&onoff);
681 ioctl(tout, FIONBIO, (char *)&onoff);
686 * Try to guess whether speeds are "encoded" (4.2BSD) or just numeric (4.4BSD).
702 # define B19200 B14400
706 #define B28800 B19200
710 # define B38400 B28800
714 #define B57600 B38400
718 #define B76800 B57600
722 #define B115200 B76800
726 #define B230400 B115200
731 * This code assumes that the values B0, B50, B75...
732 * are in ascending order. They do not have to be
739 { 0, B0 }, { 50, B50 }, { 75, B75 },
740 { 110, B110 }, { 134, B134 }, { 150, B150 },
741 { 200, B200 }, { 300, B300 }, { 600, B600 },
742 { 1200, B1200 }, { 1800, B1800 }, { 2400, B2400 },
743 { 4800, B4800 }, { 7200, B7200 }, { 9600, B9600 },
744 { 14400, B14400 }, { 19200, B19200 }, { 28800, B28800 },
745 { 38400, B38400 }, { 57600, B57600 }, { 115200, B115200 },
746 { 230400, B230400 }, { -1, B230400 }
748 #endif /* DECODE_BAUD */
751 TerminalSpeeds(long *ispeed, long *ospeed)
754 struct termspeeds *tp;
755 #endif /* DECODE_BAUD */
758 out = cfgetospeed(&old_tc);
759 in = cfgetispeed(&old_tc);
765 while ((tp->speed != -1) && (tp->value < in))
770 while ((tp->speed != -1) && (tp->value < out))
773 #else /* DECODE_BAUD */
776 #endif /* DECODE_BAUD */
780 TerminalWindowSize(long *rows, long *cols)
785 if (ioctl(fileno(stdin), TIOCGWINSZ, (char *)&ws) >= 0) {
790 #endif /* TIOCGWINSZ */
801 NetNonblockingIO(int fd, int onoff)
803 ioctl(fd, FIONBIO, (char *)&onoff);
808 * Various signal handling routines.
813 intr(int sig __unused)
820 longjmp(toplevel, -1);
825 intr2(int sig __unused)
828 #ifdef KLUDGELINEMODE
841 susp(int sig __unused)
843 if ((rlogin != _POSIX_VDISABLE) && rlogin_susp())
853 sendwin(int sig __unused)
864 ayt(int sig __unused)
875 sys_telnet_init(void)
877 (void) signal(SIGINT, intr);
878 (void) signal(SIGQUIT, intr2);
879 (void) signal(SIGPIPE, SIG_IGN);
881 (void) signal(SIGWINCH, sendwin);
884 (void) signal(SIGTSTP, susp);
887 (void) signal(SIGINFO, ayt);
892 NetNonblockingIO(net, 1);
894 #if defined(SO_OOBINLINE)
895 if (SetSockOpt(net, SOL_SOCKET, SO_OOBINLINE, 1) == -1) {
896 perror("SetSockOpt");
898 #endif /* defined(SO_OOBINLINE) */
904 * This routine tries to fill up/empty our various rings.
906 * The parameter specifies whether this is a poll operation,
907 * or a block-until-something-happens operation.
909 * The return value is 1 if something happened, 0 if not.
913 process_rings(int netin, int netout, int netex, int ttyin, int ttyout, int poll)
917 static struct timeval TimeValue = { 0, 0 };
921 if ((netout || netin || netex) && net > maxfd)
924 if (ttyout && tout > maxfd)
926 if (ttyin && tin > maxfd)
928 tmp = howmany(maxfd+1, NFDBITS) * sizeof(fd_mask);
938 if ((ibitsp = (fd_set *)malloc(fdsn)) == NULL)
940 if ((obitsp = (fd_set *)malloc(fdsn)) == NULL)
942 if ((xbitsp = (fd_set *)malloc(fdsn)) == NULL)
944 memset(ibitsp, 0, fdsn);
945 memset(obitsp, 0, fdsn);
946 memset(xbitsp, 0, fdsn);
952 FD_SET(tout, obitsp);
959 if ((c = select(maxfd + 1, ibitsp, obitsp, xbitsp,
960 (poll == 0)? (struct timeval *)0 : &TimeValue)) < 0) {
963 * we can get EINTR if we are in line mode,
964 * and the user does an escape (TSTP), or
965 * some other signal generator.
967 if (errno == EINTR) {
970 /* I don't like this, does it ever happen? */
971 printf("sleep(5) from telnet, after select: %s\r\n", strerror(errno));
980 if (FD_ISSET(net, xbitsp)) {
983 (void) ttyflush(1); /* flush already enqueued data */
987 * Something to read from the network...
989 if (FD_ISSET(net, ibitsp)) {
993 canread = ring_empty_consecutive(&netiring);
994 #if !defined(SO_OOBINLINE)
996 * In 4.2 (and some early 4.3) systems, the
997 * OOB indication and data handling in the kernel
998 * is such that if two separate TCP Urgent requests
999 * come in, one byte of TCP data will be overlaid.
1000 * This is fatal for Telnet, but we try to live
1003 * In addition, in 4.2 (and...), a special protocol
1004 * is needed to pick up the TCP Urgent data in
1005 * the correct sequence.
1007 * What we do is: if we think we are in urgent
1008 * mode, we look to see if we are "at the mark".
1009 * If we are, we do an OOB receive. If we run
1010 * this twice, we will do the OOB receive twice,
1011 * but the second will fail, since the second
1012 * time we were "at the mark", but there wasn't
1013 * any data there (the kernel doesn't reset
1014 * "at the mark" until we do a normal read).
1015 * Once we've read the OOB data, we go ahead
1016 * and do normal reads.
1018 * There is also another problem, which is that
1019 * since the OOB byte we read doesn't put us
1020 * out of OOB state, and since that byte is most
1021 * likely the TELNET DM (data mark), we would
1022 * stay in the TELNET SYNCH (SYNCHing) state.
1023 * So, clocks to the rescue. If we've "just"
1024 * received a DM, then we test for the
1025 * presence of OOB data when the receive OOB
1026 * fails (and AFTER we did the normal mode read
1027 * to clear "at the mark").
1031 static int bogus_oob = 0, first = 1;
1033 ioctl(net, SIOCATMARK, (char *)&atmark);
1035 c = recv(net, netiring.supply, canread, MSG_OOB);
1036 if ((c == -1) && (errno == EINVAL)) {
1037 c = recv(net, netiring.supply, canread, 0);
1038 if (clocks.didnetreceive < clocks.gotDM) {
1039 SYNCHing = stilloob(net);
1041 } else if (first && c > 0) {
1043 * Bogosity check. Systems based on 4.2BSD
1044 * do not return an error if you do a second
1045 * recv(MSG_OOB). So, we do one. If it
1046 * succeeds and returns exactly the same
1047 * data, then assume that we are running
1048 * on a broken system and set the bogus_oob
1049 * flag. (If the data was different, then
1050 * we probably got some valid new data, so
1051 * increment the count...)
1054 i = recv(net, netiring.supply + c, canread - c, MSG_OOB);
1056 memcmp(netiring.supply, netiring.supply + c, i) == 0) {
1065 if (bogus_oob && c > 0) {
1068 * Bogosity. We have to do the read
1069 * to clear the atmark to get out of
1072 i = read(net, netiring.supply + c, canread - c);
1077 c = recv(net, netiring.supply, canread, 0);
1080 c = recv(net, netiring.supply, canread, 0);
1082 settimer(didnetreceive);
1083 #else /* !defined(SO_OOBINLINE) */
1084 c = recv(net, (char *)netiring.supply, canread, 0);
1085 #endif /* !defined(SO_OOBINLINE) */
1086 if (c < 0 && errno == EWOULDBLOCK) {
1088 } else if (c <= 0) {
1092 Dump('<', netiring.supply, c);
1095 ring_supplied(&netiring, c);
1100 * Something to read from the tty...
1102 if (FD_ISSET(tin, ibitsp)) {
1103 FD_CLR(tin, ibitsp);
1104 c = TerminalRead(ttyiring.supply, ring_empty_consecutive(&ttyiring));
1105 if (c < 0 && errno == EIO)
1107 if (c < 0 && errno == EWOULDBLOCK) {
1110 /* EOF detection for line mode!!!! */
1111 if ((c == 0) && MODE_LOCAL_CHARS(globalmode) && isatty(tin)) {
1112 /* must be an EOF... */
1113 *ttyiring.supply = termEofChar;
1120 Dump('<', ttyiring.supply, c);
1122 ring_supplied(&ttyiring, c);
1124 returnValue = 1; /* did something useful */
1127 if (FD_ISSET(net, obitsp)) {
1128 FD_CLR(net, obitsp);
1129 returnValue |= netflush();
1131 if (FD_ISSET(tout, obitsp)) {
1132 FD_CLR(tout, obitsp);
1133 returnValue |= (ttyflush(SYNCHing|flushout) > 0);