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