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>
66 tout, /* Output file descriptor */
67 tin, /* Input file descriptor */
71 struct tchars otc = { 0 }, ntc = { 0 };
72 struct ltchars oltc = { 0 }, nltc = { 0 };
73 struct sgttyb ottyb = { 0 }, nttyb = { 0 };
75 # define cfgetispeed(ptr) (ptr)->sg_ispeed
76 # define cfgetospeed(ptr) (ptr)->sg_ospeed
79 #else /* USE_TERMIO */
80 struct termio old_tc = { 0, 0, 0, 0, {}, 0, 0 };
84 # define TCSANOW TCSETS
85 # define TCSADRAIN TCSETSW
86 # define tcgetattr(f, t) ioctl(f, TCGETS, (char *)t)
89 # define TCSANOW TCSETA
90 # define TCSADRAIN TCSETAW
91 # define tcgetattr(f, t) ioctl(f, TCGETA, (char *)t)
93 # define TCSANOW TIOCSETA
94 # define TCSADRAIN TIOCSETAW
95 # define tcgetattr(f, t) ioctl(f, TIOCGETA, (char *)t)
98 # define tcsetattr(f, a, t) ioctl(f, a, (char *)t)
99 # define cfgetospeed(ptr) ((ptr)->c_cflag&CBAUD)
101 # define cfgetispeed(ptr) (((ptr)->c_cflag&CIBAUD) >> IBSHIFT)
103 # define cfgetispeed(ptr) cfgetospeed(ptr)
105 # endif /* TCSANOW */
107 # define TIOCFLUSH TC_PX_DRAIN
109 #endif /* USE_TERMIO */
111 static fd_set *ibitsp, *obitsp, *xbitsp;
115 static SIG_FUNC_RET intr(int);
118 static SIG_FUNC_RET intr2(int);
121 static SIG_FUNC_RET susp(int);
124 static SIG_FUNC_RET ayt(int);
130 tout = fileno(stdout);
136 TerminalWrite(char *buf, int n)
138 return write(tout, buf, n);
142 TerminalRead(char *buf, int n)
144 return read(tin, buf, n);
152 TerminalAutoFlush(void)
157 ioctl(0, TIOCLGET, (char *)&flush);
158 return !(flush&LNOFLSH); /* if LNOFLSH, no autoflush */
164 #ifdef KLUDGELINEMODE
165 extern int kludgelinemode;
168 * TerminalSpecialChars()
170 * Look at an input character to see if it is a special character
171 * and decide what to do.
175 * 0 Don't add this character.
176 * 1 Do add this character
180 TerminalSpecialChars(int c)
182 if (c == termIntChar) {
185 } else if (c == termQuitChar) {
186 #ifdef KLUDGELINEMODE
193 } else if (c == termEofChar) {
194 if (my_want_state_is_will(TELOPT_LINEMODE)) {
199 } else if (c == termSuspChar) {
202 } else if (c == termFlushChar) {
203 xmitAO(); /* Transmit Abort Output */
205 } else if (!MODE_LOCAL_CHARS(globalmode)) {
206 if (c == termKillChar) {
209 } else if (c == termEraseChar) {
210 xmitEC(); /* Transmit Erase Character */
219 * Flush output to the terminal
223 TerminalFlushOutput(void)
226 (void) ioctl(fileno(stdout), TIOCFLUSH, (char *) 0);
228 (void) ioctl(fileno(stdout), TCFLSH, (char *) 0);
233 TerminalSaveState(void)
236 ioctl(0, TIOCGETP, (char *)&ottyb);
237 ioctl(0, TIOCGETC, (char *)&otc);
238 ioctl(0, TIOCGLTC, (char *)&oltc);
239 ioctl(0, TIOCLGET, (char *)&olmode);
245 #else /* USE_TERMIO */
246 tcgetattr(0, &old_tc);
251 termFlushChar = CONTROL('O');
254 termWerasChar = CONTROL('W');
257 termRprntChar = CONTROL('R');
260 termLiteralNextChar = CONTROL('V');
263 termStartChar = CONTROL('Q');
266 termStopChar = CONTROL('S');
269 termAytChar = CONTROL('T');
271 #endif /* USE_TERMIO */
278 case SLC_IP: return(&termIntChar);
279 case SLC_ABORT: return(&termQuitChar);
280 case SLC_EOF: return(&termEofChar);
281 case SLC_EC: return(&termEraseChar);
282 case SLC_EL: return(&termKillChar);
283 case SLC_XON: return(&termStartChar);
284 case SLC_XOFF: return(&termStopChar);
285 case SLC_FORW1: return(&termForw1Char);
287 case SLC_FORW2: return(&termForw2Char);
289 case SLC_AO: return(&termFlushChar);
292 case SLC_SUSP: return(&termSuspChar);
295 case SLC_EW: return(&termWerasChar);
298 case SLC_RP: return(&termRprntChar);
301 case SLC_LNEXT: return(&termLiteralNextChar);
304 case SLC_AYT: return(&termAytChar);
317 TerminalDefaultChars(void)
322 nttyb.sg_kill = ottyb.sg_kill;
323 nttyb.sg_erase = ottyb.sg_erase;
324 #else /* USE_TERMIO */
325 memcpy(new_tc.c_cc, old_tc.c_cc, sizeof(old_tc.c_cc));
327 termFlushChar = CONTROL('O');
330 termWerasChar = CONTROL('W');
333 termRprntChar = CONTROL('R');
336 termLiteralNextChar = CONTROL('V');
339 termStartChar = CONTROL('Q');
342 termStopChar = CONTROL('S');
345 termAytChar = CONTROL('T');
347 #endif /* USE_TERMIO */
351 * TerminalNewMode - set up terminal to a specific mode.
352 * MODE_ECHO: do local terminal echo
353 * MODE_FLOW: do local flow control
354 * MODE_TRAPSIG: do local mapping to TELNET IAC sequences
355 * MODE_EDIT: do local line editing
358 * MODE_ECHO|MODE_EDIT|MODE_FLOW|MODE_TRAPSIG
362 * local signal mapping
366 * Both Linemode and Single Character mode:
369 * local/no signal mapping
373 TerminalNewMode(int f)
375 static int prevmode = 0;
381 #else /* USE_TERMIO */
382 struct termio tmp_tc;
383 #endif /* USE_TERMIO */
388 globalmode = f&~MODE_FORCE;
393 * Write any outstanding data before switching modes
394 * ttyflush() returns 0 only when there is no more data
395 * left to write out, it returns -1 if it couldn't do
396 * anything at all, otherwise it returns 1 + the number
397 * of characters left to write.
399 * We would really like ask the kernel to wait for the output
400 * to drain, like we can do with the TCSADRAIN, but we don't have
401 * that option. The only ioctl that waits for the output to
402 * drain, TIOCSETP, also flushes the input queue, which is NOT
403 * what we want (TIOCSETP is like TCSADFLUSH).
406 old = ttyflush(SYNCHing|flushout);
407 if (old < 0 || old > 1) {
409 tcgetattr(tin, &tmp_tc);
410 #endif /* USE_TERMIO */
413 * Wait for data to drain, then flush again.
416 tcsetattr(tin, TCSADRAIN, &tmp_tc);
417 #endif /* USE_TERMIO */
418 old = ttyflush(SYNCHing|flushout);
419 } while (old < 0 || old > 1);
423 prevmode = f&~MODE_FORCE;
437 tmp_tc.c_lflag |= ECHO;
438 tmp_tc.c_oflag |= ONLCR;
440 tmp_tc.c_iflag |= ICRNL;
444 sb.sg_flags &= ~ECHO;
446 tmp_tc.c_lflag &= ~ECHO;
447 tmp_tc.c_oflag &= ~ONLCR;
451 if ((f&MODE_FLOW) == 0) {
453 tc.t_startc = _POSIX_VDISABLE;
454 tc.t_stopc = _POSIX_VDISABLE;
456 tmp_tc.c_iflag &= ~(IXOFF|IXON); /* Leave the IXANY bit alone */
458 if (restartany < 0) {
459 tmp_tc.c_iflag |= IXOFF|IXON; /* Leave the IXANY bit alone */
460 } else if (restartany > 0) {
461 tmp_tc.c_iflag |= IXOFF|IXON|IXANY;
463 tmp_tc.c_iflag |= IXOFF|IXON;
464 tmp_tc.c_iflag &= ~IXANY;
469 if ((f&MODE_TRAPSIG) == 0) {
471 tc.t_intrc = _POSIX_VDISABLE;
472 tc.t_quitc = _POSIX_VDISABLE;
473 tc.t_eofc = _POSIX_VDISABLE;
474 ltc.t_suspc = _POSIX_VDISABLE;
475 ltc.t_dsuspc = _POSIX_VDISABLE;
477 tmp_tc.c_lflag &= ~ISIG;
482 tmp_tc.c_lflag |= ISIG;
489 sb.sg_flags &= ~CBREAK;
490 sb.sg_flags |= CRMOD;
492 tmp_tc.c_lflag |= ICANON;
496 sb.sg_flags |= CBREAK;
498 sb.sg_flags |= CRMOD;
500 sb.sg_flags &= ~CRMOD;
502 tmp_tc.c_lflag &= ~ICANON;
503 tmp_tc.c_iflag &= ~ICRNL;
504 tmp_tc.c_cc[VMIN] = 1;
505 tmp_tc.c_cc[VTIME] = 0;
509 if ((f&(MODE_EDIT|MODE_TRAPSIG)) == 0) {
511 ltc.t_lnextc = _POSIX_VDISABLE;
514 tmp_tc.c_cc[VLNEXT] = (cc_t)(_POSIX_VDISABLE);
519 if (f&MODE_SOFT_TAB) {
521 sb.sg_flags |= XTABS;
524 tmp_tc.c_oflag |= OXTABS;
527 tmp_tc.c_oflag &= ~TABDLY;
528 tmp_tc.c_oflag |= TAB3;
533 sb.sg_flags &= ~XTABS;
536 tmp_tc.c_oflag &= ~OXTABS;
539 tmp_tc.c_oflag &= ~TABDLY;
544 if (f&MODE_LIT_ECHO) {
549 tmp_tc.c_lflag &= ~ECHOCTL;
557 tmp_tc.c_lflag |= ECHOCTL;
577 tmp_tc.c_iflag &= ~ISTRIP;
579 tmp_tc.c_iflag |= ISTRIP;
580 if (f & MODE_OUTBIN) {
581 tmp_tc.c_cflag &= ~(CSIZE|PARENB);
582 tmp_tc.c_cflag |= CS8;
583 tmp_tc.c_oflag &= ~OPOST;
585 tmp_tc.c_cflag &= ~(CSIZE|PARENB);
586 tmp_tc.c_cflag |= old_tc.c_cflag & (CSIZE|PARENB);
587 tmp_tc.c_oflag |= OPOST;
595 (void) signal(SIGINT, intr);
598 (void) signal(SIGQUIT, intr2);
601 (void) signal(SIGTSTP, susp);
604 (void) signal(SIGINFO, ayt);
606 #if defined(USE_TERMIO) && defined(NOKERNINFO)
607 tmp_tc.c_lflag |= NOKERNINFO;
610 * We don't want to process ^Y here. It's just another
611 * character that we'll pass on to the back end. It has
612 * to process it because it will be processed when the
613 * user attempts to read it, not when we send it.
616 ltc.t_dsuspc = _POSIX_VDISABLE;
619 tmp_tc.c_cc[VDSUSP] = (cc_t)(_POSIX_VDISABLE);
624 * If the VEOL character is already set, then use VEOL2,
625 * otherwise use VEOL.
627 esc = (rlogin != _POSIX_VDISABLE) ? rlogin : escape;
628 if ((tmp_tc.c_cc[VEOL] != esc)
630 && (tmp_tc.c_cc[VEOL2] != esc)
633 if (tmp_tc.c_cc[VEOL] == (cc_t)(_POSIX_VDISABLE))
634 tmp_tc.c_cc[VEOL] = esc;
636 else if (tmp_tc.c_cc[VEOL2] == (cc_t)(_POSIX_VDISABLE))
637 tmp_tc.c_cc[VEOL2] = esc;
641 if (tc.t_brkc == (cc_t)(_POSIX_VDISABLE))
646 (void) signal(SIGINFO, (void (*)(int))ayt_status);
649 (void) signal(SIGINT, SIG_DFL);
652 (void) signal(SIGQUIT, SIG_DFL);
655 (void) signal(SIGTSTP, SIG_DFL);
657 (void) sigsetmask(sigblock(0) & ~(1<<(SIGTSTP-1)));
659 (void) sigrelse(SIGTSTP);
660 # endif /* SOLARIS */
672 ioctl(tin, TIOCLSET, (char *)&lmode);
673 ioctl(tin, TIOCSLTC, (char *)<c);
674 ioctl(tin, TIOCSETC, (char *)&tc);
675 ioctl(tin, TIOCSETN, (char *)&sb);
677 if (tcsetattr(tin, TCSADRAIN, &tmp_tc) < 0)
678 tcsetattr(tin, TCSANOW, &tmp_tc);
681 ioctl(tin, FIONBIO, (char *)&onoff);
682 ioctl(tout, FIONBIO, (char *)&onoff);
687 TerminalSpeeds(long *ispeed, long *ospeed)
690 struct termspeeds *tp;
691 #endif /* DECODE_BAUD */
694 out = cfgetospeed(&old_tc);
695 in = cfgetispeed(&old_tc);
701 while ((tp->speed != -1) && (tp->value < in))
706 while ((tp->speed != -1) && (tp->value < out))
709 #else /* DECODE_BAUD */
712 #endif /* DECODE_BAUD */
716 TerminalWindowSize(long *rows, long *cols)
721 if (ioctl(fileno(stdin), TIOCGWINSZ, (char *)&ws) >= 0) {
726 #endif /* TIOCGWINSZ */
737 NetNonblockingIO(int fd, int onoff)
739 ioctl(fd, FIONBIO, (char *)&onoff);
744 * Various signal handling routines.
749 intr(int sig __unused)
756 longjmp(toplevel, -1);
761 intr2(int sig __unused)
764 #ifdef KLUDGELINEMODE
777 susp(int sig __unused)
779 if ((rlogin != _POSIX_VDISABLE) && rlogin_susp())
789 sendwin(int sig __unused)
800 ayt(int sig __unused)
811 sys_telnet_init(void)
813 (void) signal(SIGINT, intr);
814 (void) signal(SIGQUIT, intr2);
815 (void) signal(SIGPIPE, SIG_IGN);
817 (void) signal(SIGWINCH, sendwin);
820 (void) signal(SIGTSTP, susp);
823 (void) signal(SIGINFO, ayt);
828 NetNonblockingIO(net, 1);
830 #if defined(SO_OOBINLINE)
831 if (SetSockOpt(net, SOL_SOCKET, SO_OOBINLINE, 1) == -1) {
832 perror("SetSockOpt");
834 #endif /* defined(SO_OOBINLINE) */
840 * This routine tries to fill up/empty our various rings.
842 * The parameter specifies whether this is a poll operation,
843 * or a block-until-something-happens operation.
845 * The return value is 1 if something happened, 0 if not.
849 process_rings(int netin, int netout, int netex, int ttyin, int ttyout, int poll)
853 static struct timeval TimeValue = { 0, 0 };
857 if ((netout || netin || netex) && net > maxfd)
860 if (ttyout && tout > maxfd)
862 if (ttyin && tin > maxfd)
864 tmp = howmany(maxfd+1, NFDBITS) * sizeof(fd_mask);
874 if ((ibitsp = (fd_set *)malloc(fdsn)) == NULL)
876 if ((obitsp = (fd_set *)malloc(fdsn)) == NULL)
878 if ((xbitsp = (fd_set *)malloc(fdsn)) == NULL)
880 memset(ibitsp, 0, fdsn);
881 memset(obitsp, 0, fdsn);
882 memset(xbitsp, 0, fdsn);
888 FD_SET(tout, obitsp);
895 if ((c = select(maxfd + 1, ibitsp, obitsp, xbitsp,
896 (poll == 0)? (struct timeval *)0 : &TimeValue)) < 0) {
899 * we can get EINTR if we are in line mode,
900 * and the user does an escape (TSTP), or
901 * some other signal generator.
903 if (errno == EINTR) {
906 /* I don't like this, does it ever happen? */
907 printf("sleep(5) from telnet, after select: %s\r\n", strerror(errno));
916 if (FD_ISSET(net, xbitsp)) {
919 (void) ttyflush(1); /* flush already enqueued data */
923 * Something to read from the network...
925 if (FD_ISSET(net, ibitsp)) {
929 canread = ring_empty_consecutive(&netiring);
930 #if !defined(SO_OOBINLINE)
932 * In 4.2 (and some early 4.3) systems, the
933 * OOB indication and data handling in the kernel
934 * is such that if two separate TCP Urgent requests
935 * come in, one byte of TCP data will be overlaid.
936 * This is fatal for Telnet, but we try to live
939 * In addition, in 4.2 (and...), a special protocol
940 * is needed to pick up the TCP Urgent data in
941 * the correct sequence.
943 * What we do is: if we think we are in urgent
944 * mode, we look to see if we are "at the mark".
945 * If we are, we do an OOB receive. If we run
946 * this twice, we will do the OOB receive twice,
947 * but the second will fail, since the second
948 * time we were "at the mark", but there wasn't
949 * any data there (the kernel doesn't reset
950 * "at the mark" until we do a normal read).
951 * Once we've read the OOB data, we go ahead
952 * and do normal reads.
954 * There is also another problem, which is that
955 * since the OOB byte we read doesn't put us
956 * out of OOB state, and since that byte is most
957 * likely the TELNET DM (data mark), we would
958 * stay in the TELNET SYNCH (SYNCHing) state.
959 * So, clocks to the rescue. If we've "just"
960 * received a DM, then we test for the
961 * presence of OOB data when the receive OOB
962 * fails (and AFTER we did the normal mode read
963 * to clear "at the mark").
967 static int bogus_oob = 0, first = 1;
969 ioctl(net, SIOCATMARK, (char *)&atmark);
971 c = recv(net, netiring.supply, canread, MSG_OOB);
972 if ((c == -1) && (errno == EINVAL)) {
973 c = recv(net, netiring.supply, canread, 0);
974 if (clocks.didnetreceive < clocks.gotDM) {
975 SYNCHing = stilloob(net);
977 } else if (first && c > 0) {
979 * Bogosity check. Systems based on 4.2BSD
980 * do not return an error if you do a second
981 * recv(MSG_OOB). So, we do one. If it
982 * succeeds and returns exactly the same
983 * data, then assume that we are running
984 * on a broken system and set the bogus_oob
985 * flag. (If the data was different, then
986 * we probably got some valid new data, so
987 * increment the count...)
990 i = recv(net, netiring.supply + c, canread - c, MSG_OOB);
992 memcmp(netiring.supply, netiring.supply + c, i) == 0) {
1001 if (bogus_oob && c > 0) {
1004 * Bogosity. We have to do the read
1005 * to clear the atmark to get out of
1008 i = read(net, netiring.supply + c, canread - c);
1013 c = recv(net, netiring.supply, canread, 0);
1016 c = recv(net, netiring.supply, canread, 0);
1018 settimer(didnetreceive);
1019 #else /* !defined(SO_OOBINLINE) */
1020 c = recv(net, (char *)netiring.supply, canread, 0);
1021 #endif /* !defined(SO_OOBINLINE) */
1022 if (c < 0 && errno == EWOULDBLOCK) {
1024 } else if (c <= 0) {
1028 Dump('<', netiring.supply, c);
1031 ring_supplied(&netiring, c);
1036 * Something to read from the tty...
1038 if (FD_ISSET(tin, ibitsp)) {
1039 FD_CLR(tin, ibitsp);
1040 c = TerminalRead(ttyiring.supply, ring_empty_consecutive(&ttyiring));
1041 if (c < 0 && errno == EIO)
1043 if (c < 0 && errno == EWOULDBLOCK) {
1046 /* EOF detection for line mode!!!! */
1047 if ((c == 0) && MODE_LOCAL_CHARS(globalmode) && isatty(tin)) {
1048 /* must be an EOF... */
1049 *ttyiring.supply = termEofChar;
1056 Dump('<', ttyiring.supply, c);
1058 ring_supplied(&ttyiring, c);
1060 returnValue = 1; /* did something useful */
1063 if (FD_ISSET(net, obitsp)) {
1064 FD_CLR(net, obitsp);
1065 returnValue |= netflush();
1067 if (FD_ISSET(tout, obitsp)) {
1068 FD_CLR(tout, obitsp);
1069 returnValue |= (ttyflush(SYNCHing|flushout) > 0);