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";
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
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.
43 #include <sys/param.h>
44 #include <sys/socket.h>
52 #include <arpa/telnet.h>
62 tout, /* Output file descriptor */
63 tin, /* Input file descriptor */
67 struct tchars otc = { 0 }, ntc = { 0 };
68 struct ltchars oltc = { 0 }, nltc = { 0 };
69 struct sgttyb ottyb = { 0 }, nttyb = { 0 };
71 # define cfgetispeed(ptr) (ptr)->sg_ispeed
72 # define cfgetospeed(ptr) (ptr)->sg_ospeed
75 #else /* USE_TERMIO */
76 struct termio old_tc = { 0, 0, 0, 0, {}, 0, 0 };
80 # define TCSANOW TCSETS
81 # define TCSADRAIN TCSETSW
82 # define tcgetattr(f, t) ioctl(f, TCGETS, (char *)t)
85 # define TCSANOW TCSETA
86 # define TCSADRAIN TCSETAW
87 # define tcgetattr(f, t) ioctl(f, TCGETA, (char *)t)
89 # define TCSANOW TIOCSETA
90 # define TCSADRAIN TIOCSETAW
91 # define tcgetattr(f, t) ioctl(f, TIOCGETA, (char *)t)
94 # define tcsetattr(f, a, t) ioctl(f, a, (char *)t)
95 # define cfgetospeed(ptr) ((ptr)->c_cflag&CBAUD)
97 # define cfgetispeed(ptr) (((ptr)->c_cflag&CIBAUD) >> IBSHIFT)
99 # define cfgetispeed(ptr) cfgetospeed(ptr)
101 # endif /* TCSANOW */
103 # define TIOCFLUSH TC_PX_DRAIN
105 #endif /* USE_TERMIO */
107 static fd_set *ibitsp, *obitsp, *xbitsp;
111 static SIG_FUNC_RET intr(int);
114 static SIG_FUNC_RET intr2(int);
117 static SIG_FUNC_RET susp(int);
120 static SIG_FUNC_RET ayt(int);
126 tout = fileno(stdout);
132 TerminalWrite(char *buf, int n)
134 return write(tout, buf, n);
138 TerminalRead(char *buf, int n)
140 return read(tin, buf, n);
148 TerminalAutoFlush(void)
153 ioctl(0, TIOCLGET, (char *)&flush);
154 return !(flush&LNOFLSH); /* if LNOFLSH, no autoflush */
160 #ifdef KLUDGELINEMODE
161 extern int kludgelinemode;
164 * TerminalSpecialChars()
166 * Look at an input character to see if it is a special character
167 * and decide what to do.
171 * 0 Don't add this character.
172 * 1 Do add this character
176 TerminalSpecialChars(int c)
178 if (c == termIntChar) {
181 } else if (c == termQuitChar) {
182 #ifdef KLUDGELINEMODE
189 } else if (c == termEofChar) {
190 if (my_want_state_is_will(TELOPT_LINEMODE)) {
195 } else if (c == termSuspChar) {
198 } else if (c == termFlushChar) {
199 xmitAO(); /* Transmit Abort Output */
201 } else if (!MODE_LOCAL_CHARS(globalmode)) {
202 if (c == termKillChar) {
205 } else if (c == termEraseChar) {
206 xmitEC(); /* Transmit Erase Character */
215 * Flush output to the terminal
219 TerminalFlushOutput(void)
222 (void) ioctl(fileno(stdout), TIOCFLUSH, (char *) 0);
224 (void) ioctl(fileno(stdout), TCFLSH, (char *) 0);
229 TerminalSaveState(void)
232 ioctl(0, TIOCGETP, (char *)&ottyb);
233 ioctl(0, TIOCGETC, (char *)&otc);
234 ioctl(0, TIOCGLTC, (char *)&oltc);
235 ioctl(0, TIOCLGET, (char *)&olmode);
241 #else /* USE_TERMIO */
242 tcgetattr(0, &old_tc);
247 termFlushChar = CONTROL('O');
250 termWerasChar = CONTROL('W');
253 termRprntChar = CONTROL('R');
256 termLiteralNextChar = CONTROL('V');
259 termStartChar = CONTROL('Q');
262 termStopChar = CONTROL('S');
265 termAytChar = CONTROL('T');
267 #endif /* USE_TERMIO */
274 case SLC_IP: return(&termIntChar);
275 case SLC_ABORT: return(&termQuitChar);
276 case SLC_EOF: return(&termEofChar);
277 case SLC_EC: return(&termEraseChar);
278 case SLC_EL: return(&termKillChar);
279 case SLC_XON: return(&termStartChar);
280 case SLC_XOFF: return(&termStopChar);
281 case SLC_FORW1: return(&termForw1Char);
283 case SLC_FORW2: return(&termForw2Char);
285 case SLC_AO: return(&termFlushChar);
288 case SLC_SUSP: return(&termSuspChar);
291 case SLC_EW: return(&termWerasChar);
294 case SLC_RP: return(&termRprntChar);
297 case SLC_LNEXT: return(&termLiteralNextChar);
300 case SLC_AYT: return(&termAytChar);
313 TerminalDefaultChars(void)
318 nttyb.sg_kill = ottyb.sg_kill;
319 nttyb.sg_erase = ottyb.sg_erase;
320 #else /* USE_TERMIO */
321 memcpy(new_tc.c_cc, old_tc.c_cc, sizeof(old_tc.c_cc));
323 termFlushChar = CONTROL('O');
326 termWerasChar = CONTROL('W');
329 termRprntChar = CONTROL('R');
332 termLiteralNextChar = CONTROL('V');
335 termStartChar = CONTROL('Q');
338 termStopChar = CONTROL('S');
341 termAytChar = CONTROL('T');
343 #endif /* USE_TERMIO */
347 * TerminalNewMode - set up terminal to a specific mode.
348 * MODE_ECHO: do local terminal echo
349 * MODE_FLOW: do local flow control
350 * MODE_TRAPSIG: do local mapping to TELNET IAC sequences
351 * MODE_EDIT: do local line editing
354 * MODE_ECHO|MODE_EDIT|MODE_FLOW|MODE_TRAPSIG
358 * local signal mapping
362 * Both Linemode and Single Character mode:
365 * local/no signal mapping
369 TerminalNewMode(int f)
371 static int prevmode = 0;
377 #else /* USE_TERMIO */
378 struct termio tmp_tc;
379 #endif /* USE_TERMIO */
384 globalmode = f&~MODE_FORCE;
389 * Write any outstanding data before switching modes
390 * ttyflush() returns 0 only when there is no more data
391 * left to write out, it returns -1 if it couldn't do
392 * anything at all, otherwise it returns 1 + the number
393 * of characters left to write.
395 * We would really like ask the kernel to wait for the output
396 * to drain, like we can do with the TCSADRAIN, but we don't have
397 * that option. The only ioctl that waits for the output to
398 * drain, TIOCSETP, also flushes the input queue, which is NOT
399 * what we want (TIOCSETP is like TCSADFLUSH).
402 old = ttyflush(SYNCHing|flushout);
403 if (old < 0 || old > 1) {
405 tcgetattr(tin, &tmp_tc);
406 #endif /* USE_TERMIO */
409 * Wait for data to drain, then flush again.
412 tcsetattr(tin, TCSADRAIN, &tmp_tc);
413 #endif /* USE_TERMIO */
414 old = ttyflush(SYNCHing|flushout);
415 } while (old < 0 || old > 1);
419 prevmode = f&~MODE_FORCE;
433 tmp_tc.c_lflag |= ECHO;
434 tmp_tc.c_oflag |= ONLCR;
436 tmp_tc.c_iflag |= ICRNL;
440 sb.sg_flags &= ~ECHO;
442 tmp_tc.c_lflag &= ~ECHO;
443 tmp_tc.c_oflag &= ~ONLCR;
447 if ((f&MODE_FLOW) == 0) {
449 tc.t_startc = _POSIX_VDISABLE;
450 tc.t_stopc = _POSIX_VDISABLE;
452 tmp_tc.c_iflag &= ~(IXOFF|IXON); /* Leave the IXANY bit alone */
454 if (restartany < 0) {
455 tmp_tc.c_iflag |= IXOFF|IXON; /* Leave the IXANY bit alone */
456 } else if (restartany > 0) {
457 tmp_tc.c_iflag |= IXOFF|IXON|IXANY;
459 tmp_tc.c_iflag |= IXOFF|IXON;
460 tmp_tc.c_iflag &= ~IXANY;
465 if ((f&MODE_TRAPSIG) == 0) {
467 tc.t_intrc = _POSIX_VDISABLE;
468 tc.t_quitc = _POSIX_VDISABLE;
469 tc.t_eofc = _POSIX_VDISABLE;
470 ltc.t_suspc = _POSIX_VDISABLE;
471 ltc.t_dsuspc = _POSIX_VDISABLE;
473 tmp_tc.c_lflag &= ~ISIG;
478 tmp_tc.c_lflag |= ISIG;
485 sb.sg_flags &= ~CBREAK;
486 sb.sg_flags |= CRMOD;
488 tmp_tc.c_lflag |= ICANON;
492 sb.sg_flags |= CBREAK;
494 sb.sg_flags |= CRMOD;
496 sb.sg_flags &= ~CRMOD;
498 tmp_tc.c_lflag &= ~ICANON;
499 tmp_tc.c_iflag &= ~ICRNL;
500 tmp_tc.c_cc[VMIN] = 1;
501 tmp_tc.c_cc[VTIME] = 0;
505 if ((f&(MODE_EDIT|MODE_TRAPSIG)) == 0) {
507 ltc.t_lnextc = _POSIX_VDISABLE;
510 tmp_tc.c_cc[VLNEXT] = (cc_t)(_POSIX_VDISABLE);
515 if (f&MODE_SOFT_TAB) {
517 sb.sg_flags |= XTABS;
520 tmp_tc.c_oflag |= OXTABS;
523 tmp_tc.c_oflag &= ~TABDLY;
524 tmp_tc.c_oflag |= TAB3;
529 sb.sg_flags &= ~XTABS;
532 tmp_tc.c_oflag &= ~OXTABS;
535 tmp_tc.c_oflag &= ~TABDLY;
540 if (f&MODE_LIT_ECHO) {
545 tmp_tc.c_lflag &= ~ECHOCTL;
553 tmp_tc.c_lflag |= ECHOCTL;
573 tmp_tc.c_iflag &= ~ISTRIP;
575 tmp_tc.c_iflag |= ISTRIP;
576 if (f & MODE_OUTBIN) {
577 tmp_tc.c_cflag &= ~(CSIZE|PARENB);
578 tmp_tc.c_cflag |= CS8;
579 tmp_tc.c_oflag &= ~OPOST;
581 tmp_tc.c_cflag &= ~(CSIZE|PARENB);
582 tmp_tc.c_cflag |= old_tc.c_cflag & (CSIZE|PARENB);
583 tmp_tc.c_oflag |= OPOST;
591 (void) signal(SIGINT, intr);
594 (void) signal(SIGQUIT, intr2);
597 (void) signal(SIGTSTP, susp);
600 (void) signal(SIGINFO, ayt);
602 #if defined(USE_TERMIO) && defined(NOKERNINFO)
603 tmp_tc.c_lflag |= NOKERNINFO;
606 * We don't want to process ^Y here. It's just another
607 * character that we'll pass on to the back end. It has
608 * to process it because it will be processed when the
609 * user attempts to read it, not when we send it.
612 ltc.t_dsuspc = _POSIX_VDISABLE;
615 tmp_tc.c_cc[VDSUSP] = (cc_t)(_POSIX_VDISABLE);
620 * If the VEOL character is already set, then use VEOL2,
621 * otherwise use VEOL.
623 esc = (rlogin != _POSIX_VDISABLE) ? rlogin : escape;
624 if ((tmp_tc.c_cc[VEOL] != esc)
626 && (tmp_tc.c_cc[VEOL2] != esc)
629 if (tmp_tc.c_cc[VEOL] == (cc_t)(_POSIX_VDISABLE))
630 tmp_tc.c_cc[VEOL] = esc;
632 else if (tmp_tc.c_cc[VEOL2] == (cc_t)(_POSIX_VDISABLE))
633 tmp_tc.c_cc[VEOL2] = esc;
637 if (tc.t_brkc == (cc_t)(_POSIX_VDISABLE))
642 (void) signal(SIGINFO, (void (*)(int))ayt_status);
645 (void) signal(SIGINT, SIG_DFL);
648 (void) signal(SIGQUIT, SIG_DFL);
651 (void) signal(SIGTSTP, SIG_DFL);
653 (void) sigsetmask(sigblock(0) & ~(1<<(SIGTSTP-1)));
655 (void) sigrelse(SIGTSTP);
656 # endif /* SOLARIS */
668 ioctl(tin, TIOCLSET, (char *)&lmode);
669 ioctl(tin, TIOCSLTC, (char *)<c);
670 ioctl(tin, TIOCSETC, (char *)&tc);
671 ioctl(tin, TIOCSETN, (char *)&sb);
673 if (tcsetattr(tin, TCSADRAIN, &tmp_tc) < 0)
674 tcsetattr(tin, TCSANOW, &tmp_tc);
677 ioctl(tin, FIONBIO, (char *)&onoff);
678 ioctl(tout, FIONBIO, (char *)&onoff);
683 TerminalSpeeds(long *ispeed, long *ospeed)
686 struct termspeeds *tp;
687 #endif /* DECODE_BAUD */
690 out = cfgetospeed(&old_tc);
691 in = cfgetispeed(&old_tc);
697 while ((tp->speed != -1) && (tp->value < in))
702 while ((tp->speed != -1) && (tp->value < out))
705 #else /* DECODE_BAUD */
708 #endif /* DECODE_BAUD */
712 TerminalWindowSize(long *rows, long *cols)
717 if (ioctl(fileno(stdin), TIOCGWINSZ, (char *)&ws) >= 0) {
722 #endif /* TIOCGWINSZ */
733 NetNonblockingIO(int fd, int onoff)
735 ioctl(fd, FIONBIO, (char *)&onoff);
740 * Various signal handling routines.
745 intr(int sig __unused)
752 longjmp(toplevel, -1);
757 intr2(int sig __unused)
760 #ifdef KLUDGELINEMODE
773 susp(int sig __unused)
775 if ((rlogin != _POSIX_VDISABLE) && rlogin_susp())
785 sendwin(int sig __unused)
796 ayt(int sig __unused)
807 sys_telnet_init(void)
809 (void) signal(SIGINT, intr);
810 (void) signal(SIGQUIT, intr2);
811 (void) signal(SIGPIPE, SIG_IGN);
813 (void) signal(SIGWINCH, sendwin);
816 (void) signal(SIGTSTP, susp);
819 (void) signal(SIGINFO, ayt);
824 NetNonblockingIO(net, 1);
826 #if defined(SO_OOBINLINE)
827 if (SetSockOpt(net, SOL_SOCKET, SO_OOBINLINE, 1) == -1) {
828 perror("SetSockOpt");
830 #endif /* defined(SO_OOBINLINE) */
836 * This routine tries to fill up/empty our various rings.
838 * The parameter specifies whether this is a poll operation,
839 * or a block-until-something-happens operation.
841 * The return value is 1 if something happened, 0 if not.
845 process_rings(int netin, int netout, int netex, int ttyin, int ttyout, int poll)
849 static struct timeval TimeValue = { 0, 0 };
853 if ((netout || netin || netex) && net > maxfd)
856 if (ttyout && tout > maxfd)
858 if (ttyin && tin > maxfd)
860 tmp = howmany(maxfd+1, NFDBITS) * sizeof(fd_mask);
870 if ((ibitsp = (fd_set *)malloc(fdsn)) == NULL)
872 if ((obitsp = (fd_set *)malloc(fdsn)) == NULL)
874 if ((xbitsp = (fd_set *)malloc(fdsn)) == NULL)
876 memset(ibitsp, 0, fdsn);
877 memset(obitsp, 0, fdsn);
878 memset(xbitsp, 0, fdsn);
884 FD_SET(tout, obitsp);
891 if ((c = select(maxfd + 1, ibitsp, obitsp, xbitsp,
892 (poll == 0)? (struct timeval *)0 : &TimeValue)) < 0) {
895 * we can get EINTR if we are in line mode,
896 * and the user does an escape (TSTP), or
897 * some other signal generator.
899 if (errno == EINTR) {
902 /* I don't like this, does it ever happen? */
903 printf("sleep(5) from telnet, after select: %s\r\n", strerror(errno));
912 if (FD_ISSET(net, xbitsp)) {
915 (void) ttyflush(1); /* flush already enqueued data */
919 * Something to read from the network...
921 if (FD_ISSET(net, ibitsp)) {
925 canread = ring_empty_consecutive(&netiring);
926 #if !defined(SO_OOBINLINE)
928 * In 4.2 (and some early 4.3) systems, the
929 * OOB indication and data handling in the kernel
930 * is such that if two separate TCP Urgent requests
931 * come in, one byte of TCP data will be overlaid.
932 * This is fatal for Telnet, but we try to live
935 * In addition, in 4.2 (and...), a special protocol
936 * is needed to pick up the TCP Urgent data in
937 * the correct sequence.
939 * What we do is: if we think we are in urgent
940 * mode, we look to see if we are "at the mark".
941 * If we are, we do an OOB receive. If we run
942 * this twice, we will do the OOB receive twice,
943 * but the second will fail, since the second
944 * time we were "at the mark", but there wasn't
945 * any data there (the kernel doesn't reset
946 * "at the mark" until we do a normal read).
947 * Once we've read the OOB data, we go ahead
948 * and do normal reads.
950 * There is also another problem, which is that
951 * since the OOB byte we read doesn't put us
952 * out of OOB state, and since that byte is most
953 * likely the TELNET DM (data mark), we would
954 * stay in the TELNET SYNCH (SYNCHing) state.
955 * So, clocks to the rescue. If we've "just"
956 * received a DM, then we test for the
957 * presence of OOB data when the receive OOB
958 * fails (and AFTER we did the normal mode read
959 * to clear "at the mark").
963 static int bogus_oob = 0, first = 1;
965 ioctl(net, SIOCATMARK, (char *)&atmark);
967 c = recv(net, netiring.supply, canread, MSG_OOB);
968 if ((c == -1) && (errno == EINVAL)) {
969 c = recv(net, netiring.supply, canread, 0);
970 if (clocks.didnetreceive < clocks.gotDM) {
971 SYNCHing = stilloob(net);
973 } else if (first && c > 0) {
975 * Bogosity check. Systems based on 4.2BSD
976 * do not return an error if you do a second
977 * recv(MSG_OOB). So, we do one. If it
978 * succeeds and returns exactly the same
979 * data, then assume that we are running
980 * on a broken system and set the bogus_oob
981 * flag. (If the data was different, then
982 * we probably got some valid new data, so
983 * increment the count...)
986 i = recv(net, netiring.supply + c, canread - c, MSG_OOB);
988 memcmp(netiring.supply, netiring.supply + c, i) == 0) {
997 if (bogus_oob && c > 0) {
1000 * Bogosity. We have to do the read
1001 * to clear the atmark to get out of
1004 i = read(net, netiring.supply + c, canread - c);
1009 c = recv(net, netiring.supply, canread, 0);
1012 c = recv(net, netiring.supply, canread, 0);
1014 settimer(didnetreceive);
1015 #else /* !defined(SO_OOBINLINE) */
1016 c = recv(net, (char *)netiring.supply, canread, 0);
1017 #endif /* !defined(SO_OOBINLINE) */
1018 if (c < 0 && errno == EWOULDBLOCK) {
1020 } else if (c <= 0) {
1024 Dump('<', netiring.supply, c);
1027 ring_supplied(&netiring, c);
1032 * Something to read from the tty...
1034 if (FD_ISSET(tin, ibitsp)) {
1035 FD_CLR(tin, ibitsp);
1036 c = TerminalRead(ttyiring.supply, ring_empty_consecutive(&ttyiring));
1037 if (c < 0 && errno == EIO)
1039 if (c < 0 && errno == EWOULDBLOCK) {
1042 /* EOF detection for line mode!!!! */
1043 if ((c == 0) && MODE_LOCAL_CHARS(globalmode) && isatty(tin)) {
1044 /* must be an EOF... */
1045 *ttyiring.supply = termEofChar;
1052 Dump('<', ttyiring.supply, c);
1054 ring_supplied(&ttyiring, c);
1056 returnValue = 1; /* did something useful */
1059 if (FD_ISSET(net, obitsp)) {
1060 FD_CLR(net, obitsp);
1061 returnValue |= netflush();
1063 if (FD_ISSET(tout, obitsp)) {
1064 FD_CLR(tout, obitsp);
1065 returnValue |= (ttyflush(SYNCHing|flushout) > 0);