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
32 static const char sccsid[] = "@(#)sys_bsd.c 8.4 (Berkeley) 5/30/95";
37 * The following routines try to encapsulate what is system dependent
38 * (at least between 4.x and dos) which is used in telnet.c.
41 #include <sys/param.h>
42 #include <sys/socket.h>
50 #include <arpa/telnet.h>
60 tout, /* Output file descriptor */
61 tin, /* Input file descriptor */
65 struct tchars otc = { 0 }, ntc = { 0 };
66 struct ltchars oltc = { 0 }, nltc = { 0 };
67 struct sgttyb ottyb = { 0 }, nttyb = { 0 };
69 # define cfgetispeed(ptr) (ptr)->sg_ispeed
70 # define cfgetospeed(ptr) (ptr)->sg_ospeed
73 #else /* USE_TERMIO */
74 struct termio old_tc = { 0, 0, 0, 0, {}, 0, 0 };
78 # define TCSANOW TCSETS
79 # define TCSADRAIN TCSETSW
80 # define tcgetattr(f, t) ioctl(f, TCGETS, (char *)t)
83 # define TCSANOW TCSETA
84 # define TCSADRAIN TCSETAW
85 # define tcgetattr(f, t) ioctl(f, TCGETA, (char *)t)
87 # define TCSANOW TIOCSETA
88 # define TCSADRAIN TIOCSETAW
89 # define tcgetattr(f, t) ioctl(f, TIOCGETA, (char *)t)
92 # define tcsetattr(f, a, t) ioctl(f, a, (char *)t)
93 # define cfgetospeed(ptr) ((ptr)->c_cflag&CBAUD)
95 # define cfgetispeed(ptr) (((ptr)->c_cflag&CIBAUD) >> IBSHIFT)
97 # define cfgetispeed(ptr) cfgetospeed(ptr)
101 # define TIOCFLUSH TC_PX_DRAIN
103 #endif /* USE_TERMIO */
105 static fd_set *ibitsp, *obitsp, *xbitsp;
109 static SIG_FUNC_RET intr(int);
112 static SIG_FUNC_RET intr2(int);
115 static SIG_FUNC_RET susp(int);
118 static SIG_FUNC_RET ayt(int);
124 tout = fileno(stdout);
130 TerminalWrite(char *buf, int n)
132 return write(tout, buf, n);
136 TerminalRead(char *buf, int n)
138 return read(tin, buf, n);
146 TerminalAutoFlush(void)
151 ioctl(0, TIOCLGET, (char *)&flush);
152 return !(flush&LNOFLSH); /* if LNOFLSH, no autoflush */
158 #ifdef KLUDGELINEMODE
159 extern int kludgelinemode;
162 * TerminalSpecialChars()
164 * Look at an input character to see if it is a special character
165 * and decide what to do.
169 * 0 Don't add this character.
170 * 1 Do add this character
174 TerminalSpecialChars(int c)
176 if (c == termIntChar) {
179 } else if (c == termQuitChar) {
180 #ifdef KLUDGELINEMODE
187 } else if (c == termEofChar) {
188 if (my_want_state_is_will(TELOPT_LINEMODE)) {
193 } else if (c == termSuspChar) {
196 } else if (c == termFlushChar) {
197 xmitAO(); /* Transmit Abort Output */
199 } else if (!MODE_LOCAL_CHARS(globalmode)) {
200 if (c == termKillChar) {
203 } else if (c == termEraseChar) {
204 xmitEC(); /* Transmit Erase Character */
213 * Flush output to the terminal
217 TerminalFlushOutput(void)
220 (void) ioctl(fileno(stdout), TIOCFLUSH, (char *) 0);
222 (void) ioctl(fileno(stdout), TCFLSH, (char *) 0);
227 TerminalSaveState(void)
230 ioctl(0, TIOCGETP, (char *)&ottyb);
231 ioctl(0, TIOCGETC, (char *)&otc);
232 ioctl(0, TIOCGLTC, (char *)&oltc);
233 ioctl(0, TIOCLGET, (char *)&olmode);
239 #else /* USE_TERMIO */
240 tcgetattr(0, &old_tc);
245 termFlushChar = CONTROL('O');
248 termWerasChar = CONTROL('W');
251 termRprntChar = CONTROL('R');
254 termLiteralNextChar = CONTROL('V');
257 termStartChar = CONTROL('Q');
260 termStopChar = CONTROL('S');
263 termAytChar = CONTROL('T');
265 #endif /* USE_TERMIO */
272 case SLC_IP: return(&termIntChar);
273 case SLC_ABORT: return(&termQuitChar);
274 case SLC_EOF: return(&termEofChar);
275 case SLC_EC: return(&termEraseChar);
276 case SLC_EL: return(&termKillChar);
277 case SLC_XON: return(&termStartChar);
278 case SLC_XOFF: return(&termStopChar);
279 case SLC_FORW1: return(&termForw1Char);
281 case SLC_FORW2: return(&termForw2Char);
283 case SLC_AO: return(&termFlushChar);
286 case SLC_SUSP: return(&termSuspChar);
289 case SLC_EW: return(&termWerasChar);
292 case SLC_RP: return(&termRprntChar);
295 case SLC_LNEXT: return(&termLiteralNextChar);
298 case SLC_AYT: return(&termAytChar);
311 TerminalDefaultChars(void)
316 nttyb.sg_kill = ottyb.sg_kill;
317 nttyb.sg_erase = ottyb.sg_erase;
318 #else /* USE_TERMIO */
319 memcpy(new_tc.c_cc, old_tc.c_cc, sizeof(old_tc.c_cc));
321 termFlushChar = CONTROL('O');
324 termWerasChar = CONTROL('W');
327 termRprntChar = CONTROL('R');
330 termLiteralNextChar = CONTROL('V');
333 termStartChar = CONTROL('Q');
336 termStopChar = CONTROL('S');
339 termAytChar = CONTROL('T');
341 #endif /* USE_TERMIO */
345 * TerminalNewMode - set up terminal to a specific mode.
346 * MODE_ECHO: do local terminal echo
347 * MODE_FLOW: do local flow control
348 * MODE_TRAPSIG: do local mapping to TELNET IAC sequences
349 * MODE_EDIT: do local line editing
352 * MODE_ECHO|MODE_EDIT|MODE_FLOW|MODE_TRAPSIG
356 * local signal mapping
360 * Both Linemode and Single Character mode:
363 * local/no signal mapping
367 TerminalNewMode(int f)
369 static int prevmode = 0;
375 #else /* USE_TERMIO */
376 struct termio tmp_tc;
377 #endif /* USE_TERMIO */
382 globalmode = f&~MODE_FORCE;
387 * Write any outstanding data before switching modes
388 * ttyflush() returns 0 only when there is no more data
389 * left to write out, it returns -1 if it couldn't do
390 * anything at all, otherwise it returns 1 + the number
391 * of characters left to write.
393 * We would really like ask the kernel to wait for the output
394 * to drain, like we can do with the TCSADRAIN, but we don't have
395 * that option. The only ioctl that waits for the output to
396 * drain, TIOCSETP, also flushes the input queue, which is NOT
397 * what we want (TIOCSETP is like TCSADFLUSH).
400 old = ttyflush(SYNCHing|flushout);
401 if (old < 0 || old > 1) {
403 tcgetattr(tin, &tmp_tc);
404 #endif /* USE_TERMIO */
407 * Wait for data to drain, then flush again.
410 tcsetattr(tin, TCSADRAIN, &tmp_tc);
411 #endif /* USE_TERMIO */
412 old = ttyflush(SYNCHing|flushout);
413 } while (old < 0 || old > 1);
417 prevmode = f&~MODE_FORCE;
431 tmp_tc.c_lflag |= ECHO;
432 tmp_tc.c_oflag |= ONLCR;
434 tmp_tc.c_iflag |= ICRNL;
438 sb.sg_flags &= ~ECHO;
440 tmp_tc.c_lflag &= ~ECHO;
441 tmp_tc.c_oflag &= ~ONLCR;
445 if ((f&MODE_FLOW) == 0) {
447 tc.t_startc = _POSIX_VDISABLE;
448 tc.t_stopc = _POSIX_VDISABLE;
450 tmp_tc.c_iflag &= ~(IXOFF|IXON); /* Leave the IXANY bit alone */
452 if (restartany < 0) {
453 tmp_tc.c_iflag |= IXOFF|IXON; /* Leave the IXANY bit alone */
454 } else if (restartany > 0) {
455 tmp_tc.c_iflag |= IXOFF|IXON|IXANY;
457 tmp_tc.c_iflag |= IXOFF|IXON;
458 tmp_tc.c_iflag &= ~IXANY;
463 if ((f&MODE_TRAPSIG) == 0) {
465 tc.t_intrc = _POSIX_VDISABLE;
466 tc.t_quitc = _POSIX_VDISABLE;
467 tc.t_eofc = _POSIX_VDISABLE;
468 ltc.t_suspc = _POSIX_VDISABLE;
469 ltc.t_dsuspc = _POSIX_VDISABLE;
471 tmp_tc.c_lflag &= ~ISIG;
476 tmp_tc.c_lflag |= ISIG;
483 sb.sg_flags &= ~CBREAK;
484 sb.sg_flags |= CRMOD;
486 tmp_tc.c_lflag |= ICANON;
490 sb.sg_flags |= CBREAK;
492 sb.sg_flags |= CRMOD;
494 sb.sg_flags &= ~CRMOD;
496 tmp_tc.c_lflag &= ~ICANON;
497 tmp_tc.c_iflag &= ~ICRNL;
498 tmp_tc.c_cc[VMIN] = 1;
499 tmp_tc.c_cc[VTIME] = 0;
503 if ((f&(MODE_EDIT|MODE_TRAPSIG)) == 0) {
505 ltc.t_lnextc = _POSIX_VDISABLE;
508 tmp_tc.c_cc[VLNEXT] = (cc_t)(_POSIX_VDISABLE);
513 if (f&MODE_SOFT_TAB) {
515 sb.sg_flags |= XTABS;
518 tmp_tc.c_oflag |= OXTABS;
521 tmp_tc.c_oflag &= ~TABDLY;
522 tmp_tc.c_oflag |= TAB3;
527 sb.sg_flags &= ~XTABS;
530 tmp_tc.c_oflag &= ~OXTABS;
533 tmp_tc.c_oflag &= ~TABDLY;
538 if (f&MODE_LIT_ECHO) {
543 tmp_tc.c_lflag &= ~ECHOCTL;
551 tmp_tc.c_lflag |= ECHOCTL;
571 tmp_tc.c_iflag &= ~ISTRIP;
573 tmp_tc.c_iflag |= ISTRIP;
574 if (f & MODE_OUTBIN) {
575 tmp_tc.c_cflag &= ~(CSIZE|PARENB);
576 tmp_tc.c_cflag |= CS8;
577 tmp_tc.c_oflag &= ~OPOST;
579 tmp_tc.c_cflag &= ~(CSIZE|PARENB);
580 tmp_tc.c_cflag |= old_tc.c_cflag & (CSIZE|PARENB);
581 tmp_tc.c_oflag |= OPOST;
589 (void) signal(SIGINT, intr);
592 (void) signal(SIGQUIT, intr2);
595 (void) signal(SIGTSTP, susp);
598 (void) signal(SIGINFO, ayt);
600 #if defined(USE_TERMIO) && defined(NOKERNINFO)
601 tmp_tc.c_lflag |= NOKERNINFO;
604 * We don't want to process ^Y here. It's just another
605 * character that we'll pass on to the back end. It has
606 * to process it because it will be processed when the
607 * user attempts to read it, not when we send it.
610 ltc.t_dsuspc = _POSIX_VDISABLE;
613 tmp_tc.c_cc[VDSUSP] = (cc_t)(_POSIX_VDISABLE);
618 * If the VEOL character is already set, then use VEOL2,
619 * otherwise use VEOL.
621 esc = (rlogin != _POSIX_VDISABLE) ? rlogin : escape;
622 if ((tmp_tc.c_cc[VEOL] != esc)
624 && (tmp_tc.c_cc[VEOL2] != esc)
627 if (tmp_tc.c_cc[VEOL] == (cc_t)(_POSIX_VDISABLE))
628 tmp_tc.c_cc[VEOL] = esc;
630 else if (tmp_tc.c_cc[VEOL2] == (cc_t)(_POSIX_VDISABLE))
631 tmp_tc.c_cc[VEOL2] = esc;
635 if (tc.t_brkc == (cc_t)(_POSIX_VDISABLE))
640 (void) signal(SIGINFO, (void (*)(int))ayt_status);
643 (void) signal(SIGINT, SIG_DFL);
646 (void) signal(SIGQUIT, SIG_DFL);
649 (void) signal(SIGTSTP, SIG_DFL);
651 (void) sigsetmask(sigblock(0) & ~(1<<(SIGTSTP-1)));
653 (void) sigrelse(SIGTSTP);
654 # endif /* SOLARIS */
666 ioctl(tin, TIOCLSET, (char *)&lmode);
667 ioctl(tin, TIOCSLTC, (char *)<c);
668 ioctl(tin, TIOCSETC, (char *)&tc);
669 ioctl(tin, TIOCSETN, (char *)&sb);
671 if (tcsetattr(tin, TCSADRAIN, &tmp_tc) < 0)
672 tcsetattr(tin, TCSANOW, &tmp_tc);
675 ioctl(tin, FIONBIO, (char *)&onoff);
676 ioctl(tout, FIONBIO, (char *)&onoff);
681 TerminalSpeeds(long *ispeed, long *ospeed)
684 struct termspeeds *tp;
685 #endif /* DECODE_BAUD */
688 out = cfgetospeed(&old_tc);
689 in = cfgetispeed(&old_tc);
695 while ((tp->speed != -1) && (tp->value < in))
700 while ((tp->speed != -1) && (tp->value < out))
703 #else /* DECODE_BAUD */
706 #endif /* DECODE_BAUD */
710 TerminalWindowSize(long *rows, long *cols)
715 if (ioctl(fileno(stdin), TIOCGWINSZ, (char *)&ws) >= 0) {
720 #endif /* TIOCGWINSZ */
731 NetNonblockingIO(int fd, int onoff)
733 ioctl(fd, FIONBIO, (char *)&onoff);
738 * Various signal handling routines.
743 intr(int sig __unused)
750 longjmp(toplevel, -1);
755 intr2(int sig __unused)
758 #ifdef KLUDGELINEMODE
771 susp(int sig __unused)
773 if ((rlogin != _POSIX_VDISABLE) && rlogin_susp())
783 sendwin(int sig __unused)
794 ayt(int sig __unused)
805 sys_telnet_init(void)
807 (void) signal(SIGINT, intr);
808 (void) signal(SIGQUIT, intr2);
809 (void) signal(SIGPIPE, SIG_IGN);
811 (void) signal(SIGWINCH, sendwin);
814 (void) signal(SIGTSTP, susp);
817 (void) signal(SIGINFO, ayt);
822 NetNonblockingIO(net, 1);
824 #if defined(SO_OOBINLINE)
825 if (SetSockOpt(net, SOL_SOCKET, SO_OOBINLINE, 1) == -1) {
826 perror("SetSockOpt");
828 #endif /* defined(SO_OOBINLINE) */
834 * This routine tries to fill up/empty our various rings.
836 * The parameter specifies whether this is a poll operation,
837 * or a block-until-something-happens operation.
839 * The return value is 1 if something happened, 0 if not.
843 process_rings(int netin, int netout, int netex, int ttyin, int ttyout, int poll)
847 static struct timeval TimeValue = { 0, 0 };
851 if ((netout || netin || netex) && net > maxfd)
854 if (ttyout && tout > maxfd)
856 if (ttyin && tin > maxfd)
858 tmp = howmany(maxfd+1, NFDBITS) * sizeof(fd_mask);
868 if ((ibitsp = (fd_set *)malloc(fdsn)) == NULL)
870 if ((obitsp = (fd_set *)malloc(fdsn)) == NULL)
872 if ((xbitsp = (fd_set *)malloc(fdsn)) == NULL)
874 memset(ibitsp, 0, fdsn);
875 memset(obitsp, 0, fdsn);
876 memset(xbitsp, 0, fdsn);
882 FD_SET(tout, obitsp);
889 if ((c = select(maxfd + 1, ibitsp, obitsp, xbitsp,
890 (poll == 0)? (struct timeval *)0 : &TimeValue)) < 0) {
893 * we can get EINTR if we are in line mode,
894 * and the user does an escape (TSTP), or
895 * some other signal generator.
897 if (errno == EINTR) {
900 /* I don't like this, does it ever happen? */
901 printf("sleep(5) from telnet, after select: %s\r\n", strerror(errno));
910 if (FD_ISSET(net, xbitsp)) {
913 (void) ttyflush(1); /* flush already enqueued data */
917 * Something to read from the network...
919 if (FD_ISSET(net, ibitsp)) {
923 canread = ring_empty_consecutive(&netiring);
924 #if !defined(SO_OOBINLINE)
926 * In 4.2 (and some early 4.3) systems, the
927 * OOB indication and data handling in the kernel
928 * is such that if two separate TCP Urgent requests
929 * come in, one byte of TCP data will be overlaid.
930 * This is fatal for Telnet, but we try to live
933 * In addition, in 4.2 (and...), a special protocol
934 * is needed to pick up the TCP Urgent data in
935 * the correct sequence.
937 * What we do is: if we think we are in urgent
938 * mode, we look to see if we are "at the mark".
939 * If we are, we do an OOB receive. If we run
940 * this twice, we will do the OOB receive twice,
941 * but the second will fail, since the second
942 * time we were "at the mark", but there wasn't
943 * any data there (the kernel doesn't reset
944 * "at the mark" until we do a normal read).
945 * Once we've read the OOB data, we go ahead
946 * and do normal reads.
948 * There is also another problem, which is that
949 * since the OOB byte we read doesn't put us
950 * out of OOB state, and since that byte is most
951 * likely the TELNET DM (data mark), we would
952 * stay in the TELNET SYNCH (SYNCHing) state.
953 * So, clocks to the rescue. If we've "just"
954 * received a DM, then we test for the
955 * presence of OOB data when the receive OOB
956 * fails (and AFTER we did the normal mode read
957 * to clear "at the mark").
961 static int bogus_oob = 0, first = 1;
963 ioctl(net, SIOCATMARK, (char *)&atmark);
965 c = recv(net, netiring.supply, canread, MSG_OOB);
966 if ((c == -1) && (errno == EINVAL)) {
967 c = recv(net, netiring.supply, canread, 0);
968 if (clocks.didnetreceive < clocks.gotDM) {
969 SYNCHing = stilloob(net);
971 } else if (first && c > 0) {
973 * Bogosity check. Systems based on 4.2BSD
974 * do not return an error if you do a second
975 * recv(MSG_OOB). So, we do one. If it
976 * succeeds and returns exactly the same
977 * data, then assume that we are running
978 * on a broken system and set the bogus_oob
979 * flag. (If the data was different, then
980 * we probably got some valid new data, so
981 * increment the count...)
984 i = recv(net, netiring.supply + c, canread - c, MSG_OOB);
986 memcmp(netiring.supply, netiring.supply + c, i) == 0) {
995 if (bogus_oob && c > 0) {
998 * Bogosity. We have to do the read
999 * to clear the atmark to get out of
1002 i = read(net, netiring.supply + c, canread - c);
1007 c = recv(net, netiring.supply, canread, 0);
1010 c = recv(net, netiring.supply, canread, 0);
1012 settimer(didnetreceive);
1013 #else /* !defined(SO_OOBINLINE) */
1014 c = recv(net, (char *)netiring.supply, canread, 0);
1015 #endif /* !defined(SO_OOBINLINE) */
1016 if (c < 0 && errno == EWOULDBLOCK) {
1018 } else if (c <= 0) {
1022 Dump('<', netiring.supply, c);
1025 ring_supplied(&netiring, c);
1030 * Something to read from the tty...
1032 if (FD_ISSET(tin, ibitsp)) {
1033 FD_CLR(tin, ibitsp);
1034 c = TerminalRead(ttyiring.supply, ring_empty_consecutive(&ttyiring));
1035 if (c < 0 && errno == EIO)
1037 if (c < 0 && errno == EWOULDBLOCK) {
1040 /* EOF detection for line mode!!!! */
1041 if ((c == 0) && MODE_LOCAL_CHARS(globalmode) && isatty(tin)) {
1042 /* must be an EOF... */
1043 *ttyiring.supply = termEofChar;
1050 Dump('<', ttyiring.supply, c);
1052 ring_supplied(&ttyiring, c);
1054 returnValue = 1; /* did something useful */
1057 if (FD_ISSET(net, obitsp)) {
1058 FD_CLR(net, obitsp);
1059 returnValue |= netflush();
1061 if (FD_ISSET(tout, obitsp)) {
1062 FD_CLR(tout, obitsp);
1063 returnValue |= (ttyflush(SYNCHing|flushout) > 0);