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
35 static const char sccsid[] = "@(#)sys_bsd.c 8.4 (Berkeley) 5/30/95";
39 * The following routines try to encapsulate what is system dependent
40 * (at least between 4.x and dos) which is used in telnet.c.
45 #include <sys/types.h>
47 #include <sys/socket.h>
50 #include <arpa/telnet.h>
61 #if defined(CRAY) || (defined(USE_TERMIO) && !defined(SYSV_TERMIO))
62 #define SIG_FUNC_RET void
64 #define SIG_FUNC_RET int
68 extern SIG_FUNC_RET ayt_status();
72 tout, /* Output file descriptor */
73 tin, /* Input file descriptor */
77 struct tchars otc = { 0 }, ntc = { 0 };
78 struct ltchars oltc = { 0 }, nltc = { 0 };
79 struct sgttyb ottyb = { 0 }, nttyb = { 0 };
81 # define cfgetispeed(ptr) (ptr)->sg_ispeed
82 # define cfgetospeed(ptr) (ptr)->sg_ospeed
85 #else /* USE_TERMIO */
86 struct termio old_tc = { 0 };
87 extern struct termio new_tc;
91 # define TCSANOW TCSETS
92 # define TCSADRAIN TCSETSW
93 # define tcgetattr(f, t) ioctl(f, TCGETS, (char *)t)
96 # define TCSANOW TCSETA
97 # define TCSADRAIN TCSETAW
98 # define tcgetattr(f, t) ioctl(f, TCGETA, (char *)t)
100 # define TCSANOW TIOCSETA
101 # define TCSADRAIN TIOCSETAW
102 # define tcgetattr(f, t) ioctl(f, TIOCGETA, (char *)t)
105 # define tcsetattr(f, a, t) ioctl(f, a, (char *)t)
106 # define cfgetospeed(ptr) ((ptr)->c_cflag&CBAUD)
108 # define cfgetispeed(ptr) (((ptr)->c_cflag&CIBAUD) >> IBSHIFT)
110 # define cfgetispeed(ptr) cfgetospeed(ptr)
112 # endif /* TCSANOW */
114 # define TIOCFLUSH TC_PX_DRAIN
116 #endif /* USE_TERMIO */
118 static fd_set ibits, obits, xbits;
124 tout = fileno(stdout);
135 TerminalWrite(buf, n)
139 return write(tout, buf, n);
147 return read(tin, buf, n);
160 ioctl(0, TIOCLGET, (char *)&flush);
161 return !(flush&LNOFLSH); /* if LNOFLSH, no autoflush */
167 #ifdef KLUDGELINEMODE
168 extern int kludgelinemode;
171 * TerminalSpecialChars()
173 * Look at an input character to see if it is a special character
174 * and decide what to do.
178 * 0 Don't add this character.
179 * 1 Do add this character
182 extern void xmitAO(), xmitEL(), xmitEC(), intp(), sendbrk();
185 TerminalSpecialChars(c)
188 if (c == termIntChar) {
191 } else if (c == termQuitChar) {
192 #ifdef KLUDGELINEMODE
199 } else if (c == termEofChar) {
200 if (my_want_state_is_will(TELOPT_LINEMODE)) {
205 } else if (c == termSuspChar) {
208 } else if (c == termFlushChar) {
209 xmitAO(); /* Transmit Abort Output */
211 } else if (!MODE_LOCAL_CHARS(globalmode)) {
212 if (c == termKillChar) {
215 } else if (c == termEraseChar) {
216 xmitEC(); /* Transmit Erase Character */
225 * Flush output to the terminal
229 TerminalFlushOutput()
232 (void) ioctl(fileno(stdout), TIOCFLUSH, (char *) 0);
234 (void) ioctl(fileno(stdout), TCFLSH, (char *) 0);
242 ioctl(0, TIOCGETP, (char *)&ottyb);
243 ioctl(0, TIOCGETC, (char *)&otc);
244 ioctl(0, TIOCGLTC, (char *)&oltc);
245 ioctl(0, TIOCLGET, (char *)&olmode);
251 #else /* USE_TERMIO */
252 tcgetattr(0, &old_tc);
257 termFlushChar = CONTROL('O');
260 termWerasChar = CONTROL('W');
263 termRprntChar = CONTROL('R');
266 termLiteralNextChar = CONTROL('V');
269 termStartChar = CONTROL('Q');
272 termStopChar = CONTROL('S');
275 termAytChar = CONTROL('T');
277 #endif /* USE_TERMIO */
285 case SLC_IP: return(&termIntChar);
286 case SLC_ABORT: return(&termQuitChar);
287 case SLC_EOF: return(&termEofChar);
288 case SLC_EC: return(&termEraseChar);
289 case SLC_EL: return(&termKillChar);
290 case SLC_XON: return(&termStartChar);
291 case SLC_XOFF: return(&termStopChar);
292 case SLC_FORW1: return(&termForw1Char);
294 case SLC_FORW2: return(&termForw2Char);
296 case SLC_AO: return(&termFlushChar);
299 case SLC_SUSP: return(&termSuspChar);
302 case SLC_EW: return(&termWerasChar);
305 case SLC_RP: return(&termRprntChar);
308 case SLC_LNEXT: return(&termLiteralNextChar);
311 case SLC_AYT: return(&termAytChar);
324 TerminalDefaultChars()
329 nttyb.sg_kill = ottyb.sg_kill;
330 nttyb.sg_erase = ottyb.sg_erase;
331 #else /* USE_TERMIO */
332 memmove(new_tc.c_cc, old_tc.c_cc, sizeof(old_tc.c_cc));
334 termFlushChar = CONTROL('O');
337 termWerasChar = CONTROL('W');
340 termRprntChar = CONTROL('R');
343 termLiteralNextChar = CONTROL('V');
346 termStartChar = CONTROL('Q');
349 termStopChar = CONTROL('S');
352 termAytChar = CONTROL('T');
354 #endif /* USE_TERMIO */
359 TerminalRestoreState()
365 * TerminalNewMode - set up terminal to a specific mode.
366 * MODE_ECHO: do local terminal echo
367 * MODE_FLOW: do local flow control
368 * MODE_TRAPSIG: do local mapping to TELNET IAC sequences
369 * MODE_EDIT: do local line editing
372 * MODE_ECHO|MODE_EDIT|MODE_FLOW|MODE_TRAPSIG
376 * local signal mapping
380 * Both Linemode and Single Character mode:
383 * local/no signal mapping
391 static int prevmode = 0;
397 #else /* USE_TERMIO */
398 struct termio tmp_tc;
399 #endif /* USE_TERMIO */
404 globalmode = f&~MODE_FORCE;
409 * Write any outstanding data before switching modes
410 * ttyflush() returns 0 only when there is no more data
411 * left to write out, it returns -1 if it couldn't do
412 * anything at all, otherwise it returns 1 + the number
413 * of characters left to write.
415 * We would really like ask the kernel to wait for the output
416 * to drain, like we can do with the TCSADRAIN, but we don't have
417 * that option. The only ioctl that waits for the output to
418 * drain, TIOCSETP, also flushes the input queue, which is NOT
419 * what we want (TIOCSETP is like TCSADFLUSH).
422 old = ttyflush(SYNCHing|flushout);
423 if (old < 0 || old > 1) {
425 tcgetattr(tin, &tmp_tc);
426 #endif /* USE_TERMIO */
429 * Wait for data to drain, then flush again.
432 tcsetattr(tin, TCSADRAIN, &tmp_tc);
433 #endif /* USE_TERMIO */
434 old = ttyflush(SYNCHing|flushout);
435 } while (old < 0 || old > 1);
439 prevmode = f&~MODE_FORCE;
453 tmp_tc.c_lflag |= ECHO;
454 tmp_tc.c_oflag |= ONLCR;
456 tmp_tc.c_iflag |= ICRNL;
460 sb.sg_flags &= ~ECHO;
462 tmp_tc.c_lflag &= ~ECHO;
463 tmp_tc.c_oflag &= ~ONLCR;
466 tmp_tc.c_iflag &= ~ICRNL;
471 if ((f&MODE_FLOW) == 0) {
473 tc.t_startc = _POSIX_VDISABLE;
474 tc.t_stopc = _POSIX_VDISABLE;
476 tmp_tc.c_iflag &= ~(IXOFF|IXON); /* Leave the IXANY bit alone */
478 if (restartany < 0) {
479 tmp_tc.c_iflag |= IXOFF|IXON; /* Leave the IXANY bit alone */
480 } else if (restartany > 0) {
481 tmp_tc.c_iflag |= IXOFF|IXON|IXANY;
483 tmp_tc.c_iflag |= IXOFF|IXON;
484 tmp_tc.c_iflag &= ~IXANY;
489 if ((f&MODE_TRAPSIG) == 0) {
491 tc.t_intrc = _POSIX_VDISABLE;
492 tc.t_quitc = _POSIX_VDISABLE;
493 tc.t_eofc = _POSIX_VDISABLE;
494 ltc.t_suspc = _POSIX_VDISABLE;
495 ltc.t_dsuspc = _POSIX_VDISABLE;
497 tmp_tc.c_lflag &= ~ISIG;
502 tmp_tc.c_lflag |= ISIG;
509 sb.sg_flags &= ~CBREAK;
510 sb.sg_flags |= CRMOD;
512 tmp_tc.c_lflag |= ICANON;
516 sb.sg_flags |= CBREAK;
518 sb.sg_flags |= CRMOD;
520 sb.sg_flags &= ~CRMOD;
522 tmp_tc.c_lflag &= ~ICANON;
523 tmp_tc.c_iflag &= ~ICRNL;
524 tmp_tc.c_cc[VMIN] = 1;
525 tmp_tc.c_cc[VTIME] = 0;
529 if ((f&(MODE_EDIT|MODE_TRAPSIG)) == 0) {
531 ltc.t_lnextc = _POSIX_VDISABLE;
534 tmp_tc.c_cc[VLNEXT] = (cc_t)(_POSIX_VDISABLE);
539 if (f&MODE_SOFT_TAB) {
541 sb.sg_flags |= XTABS;
544 tmp_tc.c_oflag |= OXTABS;
547 tmp_tc.c_oflag &= ~TABDLY;
548 tmp_tc.c_oflag |= TAB3;
553 sb.sg_flags &= ~XTABS;
556 tmp_tc.c_oflag &= ~OXTABS;
559 tmp_tc.c_oflag &= ~TABDLY;
564 if (f&MODE_LIT_ECHO) {
569 tmp_tc.c_lflag &= ~ECHOCTL;
577 tmp_tc.c_lflag |= ECHOCTL;
597 tmp_tc.c_iflag &= ~ISTRIP;
599 tmp_tc.c_iflag |= ISTRIP;
600 if (f & MODE_OUTBIN) {
601 tmp_tc.c_cflag &= ~(CSIZE|PARENB);
602 tmp_tc.c_cflag |= CS8;
603 tmp_tc.c_oflag &= ~OPOST;
605 tmp_tc.c_cflag &= ~(CSIZE|PARENB);
606 tmp_tc.c_cflag |= old_tc.c_cflag & (CSIZE|PARENB);
607 tmp_tc.c_oflag |= OPOST;
622 (void) signal(SIGTSTP, susp);
625 (void) signal(SIGINFO, ayt);
627 #if defined(USE_TERMIO) && defined(NOKERNINFO)
628 tmp_tc.c_lflag |= NOKERNINFO;
631 * We don't want to process ^Y here. It's just another
632 * character that we'll pass on to the back end. It has
633 * to process it because it will be processed when the
634 * user attempts to read it, not when we send it.
637 ltc.t_dsuspc = _POSIX_VDISABLE;
640 tmp_tc.c_cc[VDSUSP] = (cc_t)(_POSIX_VDISABLE);
645 * If the VEOL character is already set, then use VEOL2,
646 * otherwise use VEOL.
648 esc = (rlogin != _POSIX_VDISABLE) ? rlogin : escape;
649 if ((tmp_tc.c_cc[VEOL] != esc)
651 && (tmp_tc.c_cc[VEOL2] != esc)
654 if (tmp_tc.c_cc[VEOL] == (cc_t)(_POSIX_VDISABLE))
655 tmp_tc.c_cc[VEOL] = esc;
657 else if (tmp_tc.c_cc[VEOL2] == (cc_t)(_POSIX_VDISABLE))
658 tmp_tc.c_cc[VEOL2] = esc;
662 if (tc.t_brkc == (cc_t)(_POSIX_VDISABLE))
667 SIG_FUNC_RET ayt_status();
669 (void) signal(SIGINFO, ayt_status);
672 (void) signal(SIGTSTP, SIG_DFL);
674 (void) sigsetmask(sigblock(0) & ~(1<<(SIGTSTP-1)));
676 (void) sigrelse(SIGTSTP);
689 ioctl(tin, TIOCLSET, (char *)&lmode);
690 ioctl(tin, TIOCSLTC, (char *)<c);
691 ioctl(tin, TIOCSETC, (char *)&tc);
692 ioctl(tin, TIOCSETN, (char *)&sb);
694 if (tcsetattr(tin, TCSADRAIN, &tmp_tc) < 0)
695 tcsetattr(tin, TCSANOW, &tmp_tc);
698 #if (!defined(TN3270)) || ((!defined(NOT43)) || defined(PUTCHAR))
699 # if !defined(sysV88)
700 ioctl(tin, FIONBIO, (char *)&onoff);
701 ioctl(tout, FIONBIO, (char *)&onoff);
703 #endif /* (!defined(TN3270)) || ((!defined(NOT43)) || defined(PUTCHAR)) */
705 if (noasynchtty == 0) {
706 ioctl(tin, FIOASYNC, (char *)&onoff);
708 #endif /* defined(TN3270) */
713 * Try to guess whether speeds are "encoded" (4.2BSD) or just numeric (4.4BSD).
729 # define B19200 B14400
733 #define B28800 B19200
737 # define B38400 B28800
741 #define B57600 B38400
745 #define B76800 B57600
749 #define B115200 B76800
753 #define B230400 B115200
758 * This code assumes that the values B0, B50, B75...
759 * are in ascending order. They do not have to be
766 { 0, B0 }, { 50, B50 }, { 75, B75 },
767 { 110, B110 }, { 134, B134 }, { 150, B150 },
768 { 200, B200 }, { 300, B300 }, { 600, B600 },
769 { 1200, B1200 }, { 1800, B1800 }, { 2400, B2400 },
770 { 4800, B4800 }, { 7200, B7200 }, { 9600, B9600 },
771 { 14400, B14400 }, { 19200, B19200 }, { 28800, B28800 },
772 { 38400, B38400 }, { 57600, B57600 }, { 115200, B115200 },
773 { 230400, B230400 }, { -1, B230400 }
775 #endif /* DECODE_BAUD */
778 TerminalSpeeds(ispeed, ospeed)
783 register struct termspeeds *tp;
784 #endif /* DECODE_BAUD */
785 register long in, out;
787 out = cfgetospeed(&old_tc);
788 in = cfgetispeed(&old_tc);
794 while ((tp->speed != -1) && (tp->value < in))
799 while ((tp->speed != -1) && (tp->value < out))
802 #else /* DECODE_BAUD */
805 #endif /* DECODE_BAUD */
809 TerminalWindowSize(rows, cols)
815 if (ioctl(fileno(stdin), TIOCGWINSZ, (char *)&ws) >= 0) {
820 #endif /* TIOCGWINSZ */
833 NetNonblockingIO(fd, onoff)
837 ioctl(fd, FIONBIO, (char *)&onoff);
846 ioctl(fd, FIOASYNC, (char *)&onoff); /* hear about input */
856 fcntl(fd, F_SETOWN, myPid);
858 #endif /*defined(TN3270)*/
861 * Various signal handling routines.
870 longjmp(peerdied, -1);
883 longjmp(toplevel, -1);
892 #ifdef KLUDGELINEMODE
908 if ((rlogin != _POSIX_VDISABLE) && rlogin_susp())
944 (void) signal(SIGINT, intr);
945 (void) signal(SIGQUIT, intr2);
946 (void) signal(SIGPIPE, deadpeer);
948 (void) signal(SIGWINCH, sendwin);
951 (void) signal(SIGTSTP, susp);
954 (void) signal(SIGINFO, ayt);
959 NetNonblockingIO(net, 1);
962 if (noasynchnet == 0) { /* DBX can't handle! */
966 #endif /* defined(TN3270) */
968 #if defined(SO_OOBINLINE)
969 if (SetSockOpt(net, SOL_SOCKET, SO_OOBINLINE, 1) == -1) {
970 perror("SetSockOpt");
972 #endif /* defined(SO_OOBINLINE) */
978 * This routine tries to fill up/empty our various rings.
980 * The parameter specifies whether this is a poll operation,
981 * or a block-until-something-happens operation.
983 * The return value is 1 if something happened, 0 if not.
987 process_rings(netin, netout, netex, ttyin, ttyout, poll)
988 int poll; /* If 0, then block until something to do */
991 /* One wants to be a bit careful about setting returnValue
992 * to one, since a one implies we did some useful work,
993 * and therefore probably won't be called to block next
994 * time (TN3270 mode only).
997 static struct timeval TimeValue = { 0 };
1000 FD_SET(net, &obits);
1003 FD_SET(tout, &obits);
1007 FD_SET(tin, &ibits);
1009 #else /* defined(TN3270) */
1011 FD_SET(tin, &ibits);
1013 #endif /* defined(TN3270) */
1016 FD_SET(net, &ibits);
1018 # else /* !defined(TN3270) */
1020 FD_SET(net, &ibits);
1022 # endif /* !defined(TN3270) */
1024 FD_SET(net, &xbits);
1026 if ((c = select(16, &ibits, &obits, &xbits,
1027 (poll == 0)? (struct timeval *)0 : &TimeValue)) < 0) {
1030 * we can get EINTR if we are in line mode,
1031 * and the user does an escape (TSTP), or
1032 * some other signal generator.
1034 if (errno == EINTR) {
1037 # if defined(TN3270)
1039 * we can get EBADF if we were in transparent
1040 * mode, and the transcom process died.
1042 if (errno == EBADF) {
1044 * zero the bits (even though kernel does it)
1045 * to make sure we are selecting on the right
1053 # endif /* defined(TN3270) */
1054 /* I don't like this, does it ever happen? */
1055 printf("sleep(5) from telnet, after select: %s\r\n", strerror(errno));
1064 if (FD_ISSET(net, &xbits)) {
1065 FD_CLR(net, &xbits);
1067 (void) ttyflush(1); /* flush already enqueued data */
1071 * Something to read from the network...
1073 if (FD_ISSET(net, &ibits)) {
1076 FD_CLR(net, &ibits);
1077 canread = ring_empty_consecutive(&netiring);
1078 #if !defined(SO_OOBINLINE)
1080 * In 4.2 (and some early 4.3) systems, the
1081 * OOB indication and data handling in the kernel
1082 * is such that if two separate TCP Urgent requests
1083 * come in, one byte of TCP data will be overlaid.
1084 * This is fatal for Telnet, but we try to live
1087 * In addition, in 4.2 (and...), a special protocol
1088 * is needed to pick up the TCP Urgent data in
1089 * the correct sequence.
1091 * What we do is: if we think we are in urgent
1092 * mode, we look to see if we are "at the mark".
1093 * If we are, we do an OOB receive. If we run
1094 * this twice, we will do the OOB receive twice,
1095 * but the second will fail, since the second
1096 * time we were "at the mark", but there wasn't
1097 * any data there (the kernel doesn't reset
1098 * "at the mark" until we do a normal read).
1099 * Once we've read the OOB data, we go ahead
1100 * and do normal reads.
1102 * There is also another problem, which is that
1103 * since the OOB byte we read doesn't put us
1104 * out of OOB state, and since that byte is most
1105 * likely the TELNET DM (data mark), we would
1106 * stay in the TELNET SYNCH (SYNCHing) state.
1107 * So, clocks to the rescue. If we've "just"
1108 * received a DM, then we test for the
1109 * presence of OOB data when the receive OOB
1110 * fails (and AFTER we did the normal mode read
1111 * to clear "at the mark").
1115 static int bogus_oob = 0, first = 1;
1117 ioctl(net, SIOCATMARK, (char *)&atmark);
1119 c = recv(net, netiring.supply, canread, MSG_OOB);
1120 if ((c == -1) && (errno == EINVAL)) {
1121 c = recv(net, netiring.supply, canread, 0);
1122 if (clocks.didnetreceive < clocks.gotDM) {
1123 SYNCHing = stilloob(net);
1125 } else if (first && c > 0) {
1127 * Bogosity check. Systems based on 4.2BSD
1128 * do not return an error if you do a second
1129 * recv(MSG_OOB). So, we do one. If it
1130 * succeeds and returns exactly the same
1131 * data, then assume that we are running
1132 * on a broken system and set the bogus_oob
1133 * flag. (If the data was different, then
1134 * we probably got some valid new data, so
1135 * increment the count...)
1138 i = recv(net, netiring.supply + c, canread - c, MSG_OOB);
1140 memcmp(netiring.supply, netiring.supply + c, i) == 0) {
1149 if (bogus_oob && c > 0) {
1152 * Bogosity. We have to do the read
1153 * to clear the atmark to get out of
1156 i = read(net, netiring.supply + c, canread - c);
1161 c = recv(net, netiring.supply, canread, 0);
1164 c = recv(net, netiring.supply, canread, 0);
1166 settimer(didnetreceive);
1167 #else /* !defined(SO_OOBINLINE) */
1168 c = recv(net, (char *)netiring.supply, canread, 0);
1169 #endif /* !defined(SO_OOBINLINE) */
1170 if (c < 0 && errno == EWOULDBLOCK) {
1172 } else if (c <= 0) {
1176 Dump('<', netiring.supply, c);
1179 ring_supplied(&netiring, c);
1184 * Something to read from the tty...
1186 if (FD_ISSET(tin, &ibits)) {
1187 FD_CLR(tin, &ibits);
1188 c = TerminalRead(ttyiring.supply, ring_empty_consecutive(&ttyiring));
1189 if (c < 0 && errno == EIO)
1191 if (c < 0 && errno == EWOULDBLOCK) {
1194 /* EOF detection for line mode!!!! */
1195 if ((c == 0) && MODE_LOCAL_CHARS(globalmode) && isatty(tin)) {
1196 /* must be an EOF... */
1197 *ttyiring.supply = termEofChar;
1204 Dump('<', ttyiring.supply, c);
1206 ring_supplied(&ttyiring, c);
1208 returnValue = 1; /* did something useful */
1211 if (FD_ISSET(net, &obits)) {
1212 FD_CLR(net, &obits);
1213 returnValue |= netflush();
1215 if (FD_ISSET(tout, &obits)) {
1216 FD_CLR(tout, &obits);
1217 returnValue |= (ttyflush(SYNCHing|flushout) > 0);