Debian Codegraph @

telnet/sys_bsd.c

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