1 /*
2 * Copyright (c) 1988, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
20 *
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
31 * SUCH DAMAGE.
32 */
33
34 /* based on @(#)sys_bsd.c 8.1 (Berkeley) 6/6/93 */
35
36 /*
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.
39 */
40
41 #include <autoconf.h>
42
43 #include <fcntl.h>
44 #include <sys/types.h>
45 #include <sys/time.h>
46 #include <sys/socket.h>
47 #include <sys/ioctl.h>
48
49 #ifdef HAVE_SYS_SELECT_H
50 #include <sys/select.h>
51 #endif
52 #include <signal.h>
53 #ifdef POSIX_SIGNALS
54 #include <unistd.h>
55 #endif /* POSIX_SIGNALS */
56 #include <errno.h>
57 #include <arpa/telnet.h>
58
59 #include "ring.h"
60
61 #include "fdset.h"
62
63 #include "defines.h"
64 #include "externs.h"
65 #include "types.h"
66
67 #if defined(CRAY) || (defined(USE_TERMIO) && !defined(SYSV_TERMIO))
68 #define SIG_FUNC_RET void
69 #else
70 #define SIG_FUNC_RET int
71 #endif
72
73 #ifdef SIGTSTP
74 static SIG_FUNC_RET susp(int);
75 #endif /* SIGTSTP */
76 #ifdef SIGINFO
77 SIG_FUNC_RET ayt(int);
78 #endif
79 #ifdef SIGINFO
80 extern SIG_FUNC_RET ayt_status();
81 #endif
82
83 #ifdef POSIX_SIGNALS
84 static struct sigaction new_sa_rec, old_sa_rec;
85
86 #ifdef SA_INTERRUPT
87 #define SIGACTION_INTERRUPT SA_INTERRUPT
88 #else
89 #define SIGACTION_INTERRUPT 0
90 #endif
91
92 #ifdef SA_NOMASK
93 #define SIGACTION_NOMASK SA_NOMASK
94 #else
95 #define SIGACTION_NOMASK 0
96 #endif
97
98 #define signal(sig, func) ((new_sa_rec.sa_handler = func), \
99 sigemptyset(&new_sa_rec.sa_mask), \
100 (new_sa_rec.sa_flags = SIGACTION_INTERRUPT | \
101 SIGACTION_NOMASK), \
102 sigaction(sig, &new_sa_rec, &old_sa_rec), \
103 old_sa_rec.sa_handler)
104
105 #endif /* POSIX_SIGNALS */
106
107 int
108 tout, /* Output file descriptor */
109 tin, /* Input file descriptor */
110 net;
111
112 #ifndef USE_TERMIO
113 struct tchars otc = { 0 }, ntc = { 0 };
114 struct ltchars oltc = { 0 }, nltc = { 0 };
115 struct sgttyb ottyb = { 0 }, nttyb = { 0 };
116 int olmode = 0;
117 # define cfgetispeed(ptr) (ptr)->sg_ispeed
118 # define cfgetospeed(ptr) (ptr)->sg_ospeed
119 # define old_tc ottyb
120
121 #else /* USE_TERMIO */
122 struct termio old_tc = { 0 };
123 extern struct termio new_tc;
124
125 # ifndef TCSANOW
126 # ifdef TCSETS
127 # define TCSANOW TCSETS
128 # define TCSADRAIN TCSETSW
129 # define tcgetattr(f, t) ioctl(f, TCGETS, (char *)t)
130 # else
131 # ifdef TCSETA
132 # define TCSANOW TCSETA
133 # define TCSADRAIN TCSETAW
134 # define tcgetattr(f, t) ioctl(f, TCGETA, (char *)t)
135 # else
136 # define TCSANOW TIOCSETA
137 # define TCSADRAIN TIOCSETAW
138 # define tcgetattr(f, t) ioctl(f, TIOCGETA, (char *)t)
139 # endif
140 # endif
141 # define tcsetattr(f, a, t) ioctl(f, a, (char *)t)
142 # define cfgetospeed(ptr) ((ptr)->c_cflag&CBAUD)
143 # ifdef CIBAUD
144 # define cfgetispeed(ptr) (((ptr)->c_cflag&CIBAUD) >> IBSHIFT)
145 # else
146 # define cfgetispeed(ptr) cfgetospeed(ptr)
147 # endif
148 # endif /* TCSANOW */
149 # ifdef sysV88
150 # define TIOCFLUSH TC_PX_DRAIN
151 # endif
152 #endif /* USE_TERMIO */
153
154 static fd_set ibits, obits, xbits;
155
156
157 void
init_sys()158 init_sys()
159 {
160 tout = fileno(stdout);
161 tin = fileno(stdin);
162 FD_ZERO(&ibits);
163 FD_ZERO(&obits);
164 FD_ZERO(&xbits);
165
166 errno = 0;
167 }
168
169
170 int
TerminalWrite(buf,n)171 TerminalWrite(buf, n)
172 unsigned char *buf;
173 int n;
174 {
175 return write(tout, buf, n);
176 }
177
178 int
TerminalRead(buf,n)179 TerminalRead(buf, n)
180 unsigned char *buf;
181 int n;
182 {
183 return read(tin, buf, n);
184 }
185
186 /*
187 *
188 */
189
190 int
TerminalAutoFlush()191 TerminalAutoFlush()
192 {
193 #if defined(LNOFLSH)
194 int flush;
195
196 ioctl(0, TIOCLGET, (char *)&flush);
197 return !(flush&LNOFLSH); /* if LNOFLSH, no autoflush */
198 #else /* LNOFLSH */
199 return 1;
200 #endif /* LNOFLSH */
201 }
202
203 #ifdef KLUDGELINEMODE
204 extern int kludgelinemode;
205 #endif
206 /*
207 * TerminalSpecialChars()
208 *
209 * Look at an input character to see if it is a special character
210 * and decide what to do.
211 *
212 * Output:
213 *
214 * 0 Don't add this character.
215 * 1 Do add this character
216 */
217
218 int
TerminalSpecialChars(c)219 TerminalSpecialChars(c)
220 int c;
221 {
222 if (c == termIntChar) {
223 intp();
224 return 0;
225 } else if (c == termQuitChar) {
226 #ifdef KLUDGELINEMODE
227 if (kludgelinemode)
228 sendbrk();
229 else
230 #endif
231 sendabort();
232 return 0;
233 } else if (c == termEofChar) {
234 if (my_want_state_is_will(TELOPT_LINEMODE)) {
235 sendeof();
236 return 0;
237 }
238 return 1;
239 } else if (c == termSuspChar) {
240 sendsusp();
241 return(0);
242 } else if (c == termFlushChar) {
243 xmitAO(); /* Transmit Abort Output */
244 return 0;
245 } else if (!MODE_LOCAL_CHARS(globalmode)) {
246 if (c == termKillChar) {
247 xmitEL();
248 return 0;
249 } else if (c == termEraseChar) {
250 xmitEC(); /* Transmit Erase Character */
251 return 0;
252 }
253 }
254 return 1;
255 }
256
257
258 /*
259 * Flush output to the terminal
260 */
261 void
TerminalFlushOutput()262 TerminalFlushOutput()
263 {
264 #ifdef TIOCFLUSH
265 (void) ioctl(fileno(stdout), TIOCFLUSH, (char *) 0);
266 #else
267 (void) ioctl(fileno(stdout), TCFLSH, (char *) 0);
268 #endif
269 }
270
271 void
TerminalSaveState()272 TerminalSaveState()
273 {
274 #ifndef USE_TERMIO
275 ioctl(0, TIOCGETP, (char *)&ottyb);
276 ioctl(0, TIOCGETC, (char *)&otc);
277 ioctl(0, TIOCGLTC, (char *)&oltc);
278 ioctl(0, TIOCLGET, (char *)&olmode);
279
280 ntc = otc;
281 nltc = oltc;
282 nttyb = ottyb;
283
284 #else /* USE_TERMIO */
285 tcgetattr(0, &old_tc);
286
287 new_tc = old_tc;
288
289 #ifndef VDISCARD
290 termFlushChar = CONTROL('O');
291 #endif
292 #ifndef VWERASE
293 termWerasChar = CONTROL('W');
294 #endif
295 #ifndef VREPRINT
296 termRprntChar = CONTROL('R');
297 #endif
298 #ifndef VLNEXT
299 termLiteralNextChar = CONTROL('V');
300 #endif
301 #ifndef VSTART
302 termStartChar = CONTROL('Q');
303 #endif
304 #ifndef VSTOP
305 termStopChar = CONTROL('S');
306 #endif
307 #ifndef VSTATUS
308 termAytChar = CONTROL('T');
309 #endif
310 #endif /* USE_TERMIO */
311 }
312
313 cc_t *
tcval(func)314 tcval(func)
315 register int func;
316 {
317 switch(func) {
318 case SLC_IP: return(&termIntChar);
319 case SLC_ABORT: return(&termQuitChar);
320 case SLC_EOF: return(&termEofChar);
321 case SLC_EC: return(&termEraseChar);
322 case SLC_EL: return(&termKillChar);
323 case SLC_XON: return(&termStartChar);
324 case SLC_XOFF: return(&termStopChar);
325 case SLC_FORW1: return(&termForw1Char);
326 #ifdef USE_TERMIO
327 case SLC_FORW2: return(&termForw2Char);
328 # ifdef VDISCARD
329 case SLC_AO: return(&termFlushChar);
330 # endif
331 # ifdef VSUSP
332 case SLC_SUSP: return(&termSuspChar);
333 # endif
334 # ifdef VWERASE
335 case SLC_EW: return(&termWerasChar);
336 # endif
337 # ifdef VREPRINT
338 case SLC_RP: return(&termRprntChar);
339 # endif
340 # ifdef VLNEXT
341 case SLC_LNEXT: return(&termLiteralNextChar);
342 # endif
343 # ifdef VSTATUS
344 case SLC_AYT: return(&termAytChar);
345 # endif
346 #endif
347
348 case SLC_SYNCH:
349 case SLC_BRK:
350 case SLC_EOR:
351 default:
352 return((cc_t *)0);
353 }
354 }
355
356 void
TerminalDefaultChars()357 TerminalDefaultChars()
358 {
359 #ifndef USE_TERMIO
360 ntc = otc;
361 nltc = oltc;
362 nttyb.sg_kill = ottyb.sg_kill;
363 nttyb.sg_erase = ottyb.sg_erase;
364 #else /* USE_TERMIO */
365 memcpy(new_tc.c_cc, old_tc.c_cc, sizeof(old_tc.c_cc));
366 # ifndef VDISCARD
367 termFlushChar = CONTROL('O');
368 # endif
369 # ifndef VWERASE
370 termWerasChar = CONTROL('W');
371 # endif
372 # ifndef VREPRINT
373 termRprntChar = CONTROL('R');
374 # endif
375 # ifndef VLNEXT
376 termLiteralNextChar = CONTROL('V');
377 # endif
378 # ifndef VSTART
379 termStartChar = CONTROL('Q');
380 # endif
381 # ifndef VSTOP
382 termStopChar = CONTROL('S');
383 # endif
384 # ifndef VSTATUS
385 termAytChar = CONTROL('T');
386 # endif
387 #endif /* USE_TERMIO */
388 }
389
390 #ifdef notdef
391 void
TerminalRestoreState()392 TerminalRestoreState()
393 {
394 }
395 #endif
396
397 /*
398 * TerminalNewMode - set up terminal to a specific mode.
399 * MODE_ECHO: do local terminal echo
400 * MODE_FLOW: do local flow control
401 * MODE_TRAPSIG: do local mapping to TELNET IAC sequences
402 * MODE_EDIT: do local line editing
403 *
404 * Command mode:
405 * MODE_ECHO|MODE_EDIT|MODE_FLOW|MODE_TRAPSIG
406 * local echo
407 * local editing
408 * local xon/xoff
409 * local signal mapping
410 *
411 * Linemode:
412 * local/no editing
413 * Both Linemode and Single Character mode:
414 * local/remote echo
415 * local/no xon/xoff
416 * local/no signal mapping
417 */
418
419
420 void
TerminalNewMode(f)421 TerminalNewMode(f)
422 register int f;
423 {
424 static int prevmode = 0;
425 #ifndef USE_TERMIO
426 struct tchars tc;
427 struct ltchars ltc;
428 struct sgttyb sb;
429 int lmode;
430 #else /* USE_TERMIO */
431 struct termio tmp_tc;
432 #endif /* USE_TERMIO */
433 int onoff;
434 int old;
435 cc_t esc;
436
437 globalmode = f&~MODE_FORCE;
438 if (prevmode == f)
439 return;
440
441 /*
442 * Write any outstanding data before switching modes
443 * ttyflush() returns 0 only when there is no more data
444 * left to write out, it returns -1 if it couldn't do
445 * anything at all, otherwise it returns 1 + the number
446 * of characters left to write.
447 #ifndef USE_TERMIO
448 * We would really like ask the kernel to wait for the output
449 * to drain, like we can do with the TCSADRAIN, but we don't have
450 * that option. The only ioctl that waits for the output to
451 * drain, TIOCSETP, also flushes the input queue, which is NOT
452 * what we want (TIOCSETP is like TCSADFLUSH).
453 #endif
454 */
455 old = ttyflush(SYNCHing|flushout);
456 if (old < 0 || old > 1) {
457 #ifdef USE_TERMIO
458 tcgetattr(tin, &tmp_tc);
459 #endif /* USE_TERMIO */
460 do {
461 /*
462 * Wait for data to drain, then flush again.
463 */
464 #ifdef USE_TERMIO
465 tcsetattr(tin, TCSADRAIN, &tmp_tc);
466 #endif /* USE_TERMIO */
467 old = ttyflush(SYNCHing|flushout);
468 } while (old < 0 || old > 1);
469 }
470
471 old = prevmode;
472 prevmode = f&~MODE_FORCE;
473 #ifndef USE_TERMIO
474 sb = nttyb;
475 tc = ntc;
476 ltc = nltc;
477 lmode = olmode;
478 #else
479 tmp_tc = new_tc;
480 #endif
481
482 if (f&MODE_ECHO) {
483 #ifndef USE_TERMIO
484 sb.sg_flags |= ECHO;
485 #else
486 tmp_tc.c_lflag |= ECHO;
487 tmp_tc.c_oflag |= ONLCR;
488 if (crlf)
489 tmp_tc.c_iflag |= ICRNL;
490 #endif
491 } else {
492 #ifndef USE_TERMIO
493 sb.sg_flags &= ~ECHO;
494 #else
495 tmp_tc.c_lflag &= ~ECHO;
496 tmp_tc.c_oflag &= ~ONLCR;
497 # ifdef notdef
498 if (crlf)
499 tmp_tc.c_iflag &= ~ICRNL;
500 # endif
501 #endif
502 }
503
504 if ((f&MODE_FLOW) == 0) {
505 #ifndef USE_TERMIO
506 tc.t_startc = _POSIX_VDISABLE;
507 tc.t_stopc = _POSIX_VDISABLE;
508 #else
509 tmp_tc.c_iflag &= ~(IXOFF|IXON); /* Leave the IXANY bit alone */
510 } else {
511 if (restartany < 0) {
512 tmp_tc.c_iflag |= IXOFF|IXON; /* Leave the IXANY bit alone */
513 } else if (restartany > 0) {
514 tmp_tc.c_iflag |= IXOFF|IXON|IXANY;
515 } else {
516 tmp_tc.c_iflag |= IXOFF|IXON;
517 tmp_tc.c_iflag &= ~IXANY;
518 }
519 #endif
520 }
521
522 if ((f&MODE_TRAPSIG) == 0) {
523 #ifndef USE_TERMIO
524 tc.t_intrc = _POSIX_VDISABLE;
525 tc.t_quitc = _POSIX_VDISABLE;
526 tc.t_eofc = _POSIX_VDISABLE;
527 ltc.t_suspc = _POSIX_VDISABLE;
528 ltc.t_dsuspc = _POSIX_VDISABLE;
529 #else
530 tmp_tc.c_lflag &= ~ISIG;
531 #endif
532 localchars = 0;
533 } else {
534 #ifdef USE_TERMIO
535 tmp_tc.c_lflag |= ISIG;
536 #endif
537 localchars = 1;
538 }
539
540 if (f&MODE_EDIT) {
541 #ifndef USE_TERMIO
542 sb.sg_flags &= ~CBREAK;
543 sb.sg_flags |= CRMOD;
544 #else
545 tmp_tc.c_lflag |= ICANON;
546 #endif
547 } else {
548 #ifndef USE_TERMIO
549 sb.sg_flags |= CBREAK;
550 if (f&MODE_ECHO)
551 sb.sg_flags |= CRMOD;
552 else
553 sb.sg_flags &= ~CRMOD;
554 #else
555 tmp_tc.c_lflag &= ~ICANON;
556 tmp_tc.c_iflag &= ~ICRNL;
557 tmp_tc.c_cc[VMIN] = 1;
558 tmp_tc.c_cc[VTIME] = 0;
559 #endif
560 }
561
562 if ((f&(MODE_EDIT|MODE_TRAPSIG)) == 0) {
563 #ifndef USE_TERMIO
564 ltc.t_lnextc = _POSIX_VDISABLE;
565 #else
566 # ifdef VLNEXT
567 tmp_tc.c_cc[VLNEXT] = (cc_t)(_POSIX_VDISABLE);
568 # endif
569 #endif
570 }
571
572 if (f&MODE_SOFT_TAB) {
573 #ifndef USE_TERMIO
574 sb.sg_flags |= XTABS;
575 #else
576 # ifdef OXTABS
577 tmp_tc.c_oflag |= OXTABS;
578 # endif
579 # ifdef TABDLY
580 tmp_tc.c_oflag &= ~TABDLY;
581 tmp_tc.c_oflag |= TAB3;
582 # endif
583 #endif
584 } else {
585 #ifndef USE_TERMIO
586 sb.sg_flags &= ~XTABS;
587 #else
588 # ifdef OXTABS
589 tmp_tc.c_oflag &= ~OXTABS;
590 # endif
591 # ifdef TABDLY
592 tmp_tc.c_oflag &= ~TABDLY;
593 # endif
594 #endif
595 }
596
597 if (f&MODE_LIT_ECHO) {
598 #ifndef USE_TERMIO
599 lmode &= ~LCTLECH;
600 #else
601 # ifdef ECHOCTL
602 tmp_tc.c_lflag &= ~ECHOCTL;
603 # endif
604 #endif
605 } else {
606 #ifndef USE_TERMIO
607 lmode |= LCTLECH;
608 #else
609 # ifdef ECHOCTL
610 tmp_tc.c_lflag |= ECHOCTL;
611 # endif
612 #endif
613 }
614
615 if (f == -1) {
616 onoff = 0;
617 } else {
618 #ifndef USE_TERMIO
619 if (f & MODE_OUTBIN)
620 lmode |= LLITOUT;
621 else
622 lmode &= ~LLITOUT;
623
624 if (f & MODE_INBIN)
625 lmode |= LPASS8;
626 else
627 lmode &= ~LPASS8;
628 #else
629 if (f & MODE_INBIN)
630 tmp_tc.c_iflag &= ~ISTRIP;
631 else
632 tmp_tc.c_iflag |= ISTRIP;
633 if (f & MODE_OUTBIN) {
634 tmp_tc.c_cflag &= ~(CSIZE|PARENB);
635 tmp_tc.c_cflag |= CS8;
636 tmp_tc.c_oflag &= ~OPOST;
637 } else {
638 tmp_tc.c_cflag &= ~(CSIZE|PARENB);
639 tmp_tc.c_cflag |= old_tc.c_cflag & (CSIZE|PARENB);
640 tmp_tc.c_oflag |= OPOST;
641 }
642 #endif
643 onoff = 1;
644 }
645
646 if (f != -1) {
647
648 #ifdef SIGTSTP
649 (void) signal(SIGTSTP, susp);
650 #endif /* SIGTSTP */
651 #ifdef SIGINFO
652 (void) signal(SIGINFO, ayt);
653 #endif
654 #if defined(USE_TERMIO) && defined(NOKERNINFO)
655 tmp_tc.c_lflag |= NOKERNINFO;
656 #endif
657 /*
658 * We don't want to process ^Y here. It's just another
659 * character that we'll pass on to the back end. It has
660 * to process it because it will be processed when the
661 * user attempts to read it, not when we send it.
662 */
663 #ifndef USE_TERMIO
664 ltc.t_dsuspc = _POSIX_VDISABLE;
665 #else
666 # ifdef VDSUSP
667 tmp_tc.c_cc[VDSUSP] = (cc_t)(_POSIX_VDISABLE);
668 # endif
669 #endif
670 #ifdef USE_TERMIO
671 /*
672 * If the VEOL character is already set, then use VEOL2,
673 * otherwise use VEOL.
674 */
675 esc = (rlogin != _POSIX_VDISABLE) ? rlogin : escape;
676 if ((tmp_tc.c_cc[VEOL] != esc)
677 # ifdef VEOL2
678 && (tmp_tc.c_cc[VEOL2] != esc)
679 # endif
680 ) {
681 if (tmp_tc.c_cc[VEOL] == (cc_t)(_POSIX_VDISABLE))
682 tmp_tc.c_cc[VEOL] = esc;
683 # ifdef VEOL2
684 else if (tmp_tc.c_cc[VEOL2] == (cc_t)(_POSIX_VDISABLE))
685 tmp_tc.c_cc[VEOL2] = esc;
686 # endif
687 }
688 #else
689 if (tc.t_brkc == (cc_t)(_POSIX_VDISABLE))
690 tc.t_brkc = esc;
691 #endif
692 } else {
693 #ifdef SIGINFO
694 SIG_FUNC_RET ayt_status();
695
696 (void) signal(SIGINFO, ayt_status);
697 #endif
698 #ifdef SIGTSTP
699 (void) signal(SIGTSTP, SIG_DFL);
700 #ifdef POSIX_SIGNALS
701 {
702 sigset_t tmask;
703 sigemptyset(&tmask);
704 sigaddset(&tmask, SIGTSTP);
705 sigprocmask(SIG_UNBLOCK, &tmask, (sigset_t*)0);
706 }
707 #else
708 (void) sigsetmask(sigblock(0) & ~(1<<(SIGTSTP-1)));
709 #endif
710 #endif /* SIGTSTP */
711 #ifndef USE_TERMIO
712 ltc = oltc;
713 tc = otc;
714 sb = ottyb;
715 lmode = olmode;
716 #else
717 tmp_tc = old_tc;
718 #endif
719 }
720 #ifndef USE_TERMIO
721 ioctl(tin, TIOCLSET, (char *)&lmode);
722 ioctl(tin, TIOCSLTC, (char *)<c);
723 ioctl(tin, TIOCSETC, (char *)&tc);
724 ioctl(tin, TIOCSETN, (char *)&sb);
725 #else
726 if (tcsetattr(tin, TCSADRAIN, &tmp_tc) < 0)
727 tcsetattr(tin, TCSANOW, &tmp_tc);
728 #endif
729
730 #if (!defined(TN3270)) || ((!defined(NOT43)) || defined(PUTCHAR))
731 # if !defined(sysV88)
732 ioctl(tin, FIONBIO, (char *)&onoff);
733 ioctl(tout, FIONBIO, (char *)&onoff);
734 # endif
735 #endif /* (!defined(TN3270)) || ((!defined(NOT43)) || defined(PUTCHAR)) */
736 #if defined(TN3270)
737 if (noasynchtty == 0) {
738 ioctl(tin, FIOASYNC, (char *)&onoff);
739 }
740 #endif /* defined(TN3270) */
741
742 }
743
744 #ifndef B19200
745 # define B19200 B9600
746 #endif
747
748 #ifndef B38400
749 # define B38400 B19200
750 #endif
751
752 /*
753 * This code assumes that the values B0, B50, B75...
754 * are in ascending order. They do not have to be
755 * contiguous.
756 */
757 struct termspeeds {
758 long speed;
759 long value;
760 } termspeeds[] = {
761 { 0, B0 }, { 50, B50 }, { 75, B75 },
762 { 110, B110 }, { 134, B134 }, { 150, B150 },
763 { 200, B200 }, { 300, B300 }, { 600, B600 },
764 { 1200, B1200 }, { 1800, B1800 }, { 2400, B2400 },
765 { 4800, B4800 }, { 9600, B9600 }, { 19200, B19200 },
766 { 38400, B38400 }, { -1, B38400 }
767 };
768
769 void
TerminalSpeeds(ispeed,ospeed)770 TerminalSpeeds(ispeed, ospeed)
771 long *ispeed;
772 long *ospeed;
773 {
774 register struct termspeeds *tp;
775 register long in, out;
776
777 out = cfgetospeed(&old_tc);
778 in = cfgetispeed(&old_tc);
779 if (in == 0)
780 in = out;
781
782 tp = termspeeds;
783 while ((tp->speed != -1) && (tp->value < in))
784 tp++;
785 *ispeed = tp->speed;
786
787 tp = termspeeds;
788 while ((tp->speed != -1) && (tp->value < out))
789 tp++;
790 *ospeed = tp->speed;
791 }
792
793 int
TerminalWindowSize(rows,cols)794 TerminalWindowSize(rows, cols)
795 long *rows, *cols;
796 {
797 #ifdef TIOCGWINSZ
798 struct winsize ws;
799
800 if (ioctl(fileno(stdin), TIOCGWINSZ, (char *)&ws) >= 0) {
801 *rows = ws.ws_row;
802 *cols = ws.ws_col;
803 return 1;
804 }
805 #endif /* TIOCGWINSZ */
806 return 0;
807 }
808
809 int
NetClose(fd)810 NetClose(fd)
811 int fd;
812 {
813 return close(fd);
814 }
815
816
817 static void
NetNonblockingIO(fd,onoff)818 NetNonblockingIO(fd, onoff)
819 int fd;
820 int onoff;
821 {
822 ioctl(fd, FIONBIO, (char *)&onoff);
823 }
824
825 #if defined(TN3270)
826 void
NetSigIO(fd,onoff)827 NetSigIO(fd, onoff)
828 int fd;
829 int onoff;
830 {
831 ioctl(fd, FIOASYNC, (char *)&onoff); /* hear about input */
832 }
833
834 void
NetSetPgrp(fd)835 NetSetPgrp(fd)
836 int fd;
837 {
838 int myPid;
839
840 myPid = getpid();
841 fcntl(fd, F_SETOWN, myPid);
842 }
843 #endif /*defined(TN3270)*/
844
845 /*
846 * Various signal handling routines.
847 */
848
849 /* ARGSUSED */
850 static SIG_FUNC_RET
deadpeer(sig)851 deadpeer(sig)
852 int sig;
853 {
854 setcommandmode();
855 longjmp(peerdied, -1);
856 }
857
858 int intr_happened = 0;
859 int intr_waiting = 0;
860
861 /* ARGSUSED */
862 static SIG_FUNC_RET
intr(sig)863 intr(sig)
864 int sig;
865 {
866 if (intr_waiting) {
867 intr_happened = 1;
868 return;
869 }
870 if (localchars) {
871 intp();
872 return;
873 }
874 setcommandmode();
875 longjmp(toplevel, -1);
876 }
877
878 /* ARGSUSED */
879 static SIG_FUNC_RET
intr2(sig)880 intr2(sig)
881 int sig;
882 {
883 if (localchars) {
884 #ifdef KLUDGELINEMODE
885 if (kludgelinemode)
886 sendbrk();
887 else
888 #endif
889 sendabort();
890 return;
891 }
892 }
893
894 #ifdef SIGTSTP
895 /* ARGSUSED */
896 static SIG_FUNC_RET
susp(sig)897 susp(sig)
898 int sig;
899 {
900 if ((rlogin != _POSIX_VDISABLE) && rlogin_susp())
901 return;
902 if (localchars)
903 sendsusp();
904 }
905 #endif
906
907 #ifdef SIGWINCH
908 /* ARGSUSED */
909 static SIG_FUNC_RET
sendwin(sig)910 sendwin(sig)
911 int sig;
912 {
913 if (connected) {
914 sendnaws();
915 }
916 }
917 #endif
918
919 #ifdef SIGINFO
920 /* ARGSUSED */
921 SIG_FUNC_RET
ayt(sig)922 ayt(sig)
923 int sig;
924 {
925 if (connected)
926 sendayt();
927 else
928 ayt_status();
929 }
930 #endif
931
932
933 void
sys_telnet_init()934 sys_telnet_init()
935 {
936 (void) signal(SIGINT, intr);
937 (void) signal(SIGQUIT, intr2);
938 (void) signal(SIGPIPE, deadpeer);
939 #ifdef SIGWINCH
940 (void) signal(SIGWINCH, sendwin);
941 #endif
942 #ifdef SIGTSTP
943 (void) signal(SIGTSTP, susp);
944 #endif
945 #ifdef SIGINFO
946 (void) signal(SIGINFO, ayt);
947 #endif
948
949 setconnmode(0);
950
951 NetNonblockingIO(net, 1);
952
953 #if defined(TN3270)
954 if (noasynchnet == 0) { /* DBX can't handle! */
955 NetSigIO(net, 1);
956 NetSetPgrp(net);
957 }
958 #endif /* defined(TN3270) */
959
960 #if defined(SO_OOBINLINE)
961 if (SetSockOpt(net, SOL_SOCKET, SO_OOBINLINE, 1) == -1) {
962 perror("SetSockOpt");
963 }
964 #endif /* defined(SO_OOBINLINE) */
965 }
966
967 /*
968 * Process rings -
969 *
970 * This routine tries to fill up/empty our various rings.
971 *
972 * The parameter specifies whether this is a poll operation,
973 * or a block-until-something-happens operation.
974 *
975 * The return value is 1 if something happened, 0 if not.
976 */
977
978 int
process_rings(netin,netout,netex,ttyin,ttyout,poll)979 process_rings(netin, netout, netex, ttyin, ttyout, poll)
980 int poll; /* If 0, then block until something to do */
981 {
982 register int c;
983 /* One wants to be a bit careful about setting returnValue
984 * to one, since a one implies we did some useful work,
985 * and therefore probably won't be called to block next
986 * time (TN3270 mode only).
987 */
988 int returnValue = 0;
989 static struct timeval TimeValue = { 0 };
990
991 if (netout) {
992 FD_SET(net, &obits);
993 }
994 if (ttyout) {
995 FD_SET(tout, &obits);
996 }
997 #if defined(TN3270)
998 if (ttyin) {
999 FD_SET(tin, &ibits);
1000 }
1001 #else /* defined(TN3270) */
1002 if (ttyin) {
1003 FD_SET(tin, &ibits);
1004 }
1005 #endif /* defined(TN3270) */
1006 #if defined(TN3270)
1007 if (netin) {
1008 FD_SET(net, &ibits);
1009 }
1010 # else /* !defined(TN3270) */
1011 if (netin) {
1012 FD_SET(net, &ibits);
1013 }
1014 # endif /* !defined(TN3270) */
1015 if (netex) {
1016 FD_SET(net, &xbits);
1017 }
1018 if ((c = select(16, &ibits, &obits, &xbits,
1019 (poll == 0)? (struct timeval *)0 : &TimeValue)) < 0) {
1020 if (c == -1) {
1021 /*
1022 * we can get EINTR if we are in line mode,
1023 * and the user does an escape (TSTP), or
1024 * some other signal generator.
1025 */
1026 if (errno == EINTR) {
1027 return 0;
1028 }
1029 # if defined(TN3270)
1030 /*
1031 * we can get EBADF if we were in transparent
1032 * mode, and the transcom process died.
1033 */
1034 if (errno == EBADF) {
1035 /*
1036 * zero the bits (even though kernel does it)
1037 * to make sure we are selecting on the right
1038 * ones.
1039 */
1040 FD_ZERO(&ibits);
1041 FD_ZERO(&obits);
1042 FD_ZERO(&xbits);
1043 return 0;
1044 }
1045 # endif /* defined(TN3270) */
1046 /* I don't like this, does it ever happen? */
1047 printf("sleep(5) from telnet, after select\r\n");
1048 sleep(5);
1049 }
1050 return 0;
1051 }
1052
1053 /*
1054 * Any urgent data?
1055 */
1056 if (FD_ISSET(net, &xbits)) {
1057 FD_CLR(net, &xbits);
1058 SYNCHing = 1;
1059 (void) ttyflush(1); /* flush already enqueued data */
1060 }
1061
1062 /*
1063 * Something to read from the network...
1064 */
1065 if (FD_ISSET(net, &ibits)) {
1066 int canread;
1067
1068 FD_CLR(net, &ibits);
1069 canread = ring_empty_consecutive(&netiring);
1070 #if !defined(SO_OOBINLINE)
1071 /*
1072 * In 4.2 (and some early 4.3) systems, the
1073 * OOB indication and data handling in the kernel
1074 * is such that if two separate TCP Urgent requests
1075 * come in, one byte of TCP data will be overlaid.
1076 * This is fatal for Telnet, but we try to live
1077 * with it.
1078 *
1079 * In addition, in 4.2 (and...), a special protocol
1080 * is needed to pick up the TCP Urgent data in
1081 * the correct sequence.
1082 *
1083 * What we do is: if we think we are in urgent
1084 * mode, we look to see if we are "at the mark".
1085 * If we are, we do an OOB receive. If we run
1086 * this twice, we will do the OOB receive twice,
1087 * but the second will fail, since the second
1088 * time we were "at the mark", but there wasn't
1089 * any data there (the kernel doesn't reset
1090 * "at the mark" until we do a normal read).
1091 * Once we've read the OOB data, we go ahead
1092 * and do normal reads.
1093 *
1094 * There is also another problem, which is that
1095 * since the OOB byte we read doesn't put us
1096 * out of OOB state, and since that byte is most
1097 * likely the TELNET DM (data mark), we would
1098 * stay in the TELNET SYNCH (SYNCHing) state.
1099 * So, clocks to the rescue. If we've "just"
1100 * received a DM, then we test for the
1101 * presence of OOB data when the receive OOB
1102 * fails (and AFTER we did the normal mode read
1103 * to clear "at the mark").
1104 */
1105 if (SYNCHing) {
1106 int atmark;
1107 static int bogus_oob = 0, first = 1;
1108
1109 ioctl(net, SIOCATMARK, (char *)&atmark);
1110 if (atmark) {
1111 c = recv(net, netiring.supply, canread, MSG_OOB);
1112 if ((c == -1) && (errno == EINVAL)) {
1113 c = recv(net, netiring.supply, canread, 0);
1114 if (clocks.didnetreceive < clocks.gotDM) {
1115 SYNCHing = stilloob(net);
1116 }
1117 } else if (first && c > 0) {
1118 /*
1119 * Bogosity check. Systems based on 4.2BSD
1120 * do not return an error if you do a second
1121 * recv(MSG_OOB). So, we do one. If it
1122 * succeeds and returns exactly the same
1123 * data, then assume that we are running
1124 * on a broken system and set the bogus_oob
1125 * flag. (If the data was different, then
1126 * we probably got some valid new data, so
1127 * increment the count...)
1128 */
1129 int i;
1130 i = recv(net, netiring.supply + c, canread - c, MSG_OOB);
1131 if (i == c &&
1132 memcmp(netiring.supply, netiring.supply + c, i) == 0) {
1133 bogus_oob = 1;
1134 first = 0;
1135 } else if (i < 0) {
1136 bogus_oob = 0;
1137 first = 0;
1138 } else
1139 c += i;
1140 }
1141 if (bogus_oob && c > 0) {
1142 int i;
1143 /*
1144 * Bogosity. We have to do the read
1145 * to clear the atmark to get out of
1146 * an infinate loop.
1147 */
1148 i = read(net, netiring.supply + c, canread - c);
1149 if (i > 0)
1150 c += i;
1151 }
1152 } else {
1153 c = recv(net, netiring.supply, canread, 0);
1154 }
1155 } else {
1156 c = recv(net, netiring.supply, canread, 0);
1157 }
1158 settimer(didnetreceive);
1159 #else /* !defined(SO_OOBINLINE) */
1160 c = recv(net, (char *)netiring.supply, canread, 0);
1161 #endif /* !defined(SO_OOBINLINE) */
1162 if (c < 0 && errno == EWOULDBLOCK) {
1163 c = 0;
1164 } else if (c <= 0) {
1165 return -1;
1166 }
1167 if (netdata) {
1168 Dump('<', netiring.supply, c);
1169 }
1170 if (c)
1171 ring_supplied(&netiring, c);
1172 returnValue = 1;
1173 }
1174
1175 /*
1176 * Something to read from the tty...
1177 */
1178 if (FD_ISSET(tin, &ibits)) {
1179 FD_CLR(tin, &ibits);
1180 c = TerminalRead(ttyiring.supply, ring_empty_consecutive(&ttyiring));
1181 if (c < 0 && errno == EWOULDBLOCK) {
1182 c = 0;
1183 } else {
1184 /* EOF detection for line mode!!!! */
1185 if ((c == 0) && MODE_LOCAL_CHARS(globalmode) && isatty(tin)) {
1186 /* must be an EOF... */
1187 *ttyiring.supply = termEofChar;
1188 c = 1;
1189 }
1190 if (c <= 0) {
1191 return -1;
1192 }
1193 if (termdata) {
1194 Dump('<', ttyiring.supply, c);
1195 }
1196 ring_supplied(&ttyiring, c);
1197 }
1198 returnValue = 1; /* did something useful */
1199 }
1200
1201 if (FD_ISSET(net, &obits)) {
1202 FD_CLR(net, &obits);
1203 returnValue |= netflush();
1204 }
1205 if (FD_ISSET(tout, &obits)) {
1206 FD_CLR(tout, &obits);
1207 returnValue |= (ttyflush(SYNCHing|flushout) > 0);
1208 }
1209
1210 return returnValue;
1211 }
1212