Debian Codegraph @

telnet/utilities.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, 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 #include <config.h>
49
50 #include <stdlib.h>
51
52 #define TELOPTS
53 #define TELCMDS
54 #define SLC_NAMES
55 #include <arpa/telnet.h>
56 #include <sys/types.h>
57 #include <sys/socket.h>
58 #include <sys/time.h>
59 #include <time.h>
60 #include <ctype.h>
61
62 #include "general.h"
63 #include "ring.h"
64 #include "defines.h"
65 #include "externs.h"
66
67 #ifdef AUTHENTICATION
68 # include <libtelnet/auth.h>
69 #endif
70 #ifdef ENCRYPTION
71 # include <libtelnet/encrypt.h>
72 #endif
73
74 FILE *NetTrace = 0; /* Not in bss, since needs to stay */
75 int prettydump;
76
77 /*
78 * upcase()
79 *
80 * Upcase (in place) the argument.
81 */
82
83 void
84 upcase (char *argument)
85 {
86 int c;
87
88 while ((c = *argument) != 0)
89 {
90 if (islower (c))
91 {
92 *argument = toupper (c);
93 }
94 argument++;
95 }
96 }
97
98 /*
99 * SetSockOpt()
100 *
101 * Compensate for differences in 4.2 and 4.3 systems.
102 */
103
104 int
105 SetSockOpt (int fd, int level, int option, int yesno)
106 {
107 #ifndef NOT43
108 return setsockopt (fd, level, option, (char *) &yesno, sizeof yesno);
109 #else /* NOT43 */
110 if (yesno == 0)
111 { /* Can't do that in 4.2! */
112 fprintf (stderr, "Error: attempt to turn off an option 0x%x.\n",
113 option);
114 return -1;
115 }
116 return setsockopt (fd, level, option, 0, 0);
117 #endif /* NOT43 */
118 }
119
120 /*
121 * The following are routines used to print out debugging information.
122 */
123
124 unsigned char NetTraceFile[256] = "(standard output)";
125
126 void
127 SetNetTrace (const char *file)
128 {
129 if (NetTrace && NetTrace != stdout)
130 fclose (NetTrace);
131 if (file && (strcmp (file, "-") != 0))
132 {
133 NetTrace = fopen (file, "w");
134 if (NetTrace)
135 {
136 strncpy ((char *) NetTraceFile, file, sizeof (NetTraceFile));
137 NetTraceFile[sizeof (NetTraceFile) - 1] = 0;
138 return;
139 }
140 fprintf (stderr, "Cannot open %s.\n", file);
141 }
142 NetTrace = stdout;
143 strcpy ((char *) NetTraceFile, "(standard output)");
144 }
145
146 void
147 Dump (char direction, unsigned char *buffer, int length)
148 {
149 #define BYTES_PER_LINE 32
150 #define min(x,y) ((x<y)? x:y)
151 unsigned char *pThis;
152 int offset;
153
154 offset = 0;
155
156 while (length)
157 {
158 /* print one line */
159 fprintf (NetTrace, "%c 0x%x\t", direction, offset);
160 pThis = buffer;
161 if (prettydump)
162 {
163 buffer = buffer + min (length, BYTES_PER_LINE / 2);
164 while (pThis < buffer)
165 {
166 fprintf (NetTrace, "%c%.2x",
167 (((*pThis) & 0xff) == 0xff) ? '*' : ' ',
168 (*pThis) & 0xff);
169 pThis++;
170 }
171 length -= BYTES_PER_LINE / 2;
172 offset += BYTES_PER_LINE / 2;
173 }
174 else
175 {
176 buffer = buffer + min (length, BYTES_PER_LINE);
177 while (pThis < buffer)
178 {
179 fprintf (NetTrace, "%.2x", (*pThis) & 0xff);
180 pThis++;
181 }
182 length -= BYTES_PER_LINE;
183 offset += BYTES_PER_LINE;
184 }
185 if (NetTrace == stdout)
186 {
187 fprintf (NetTrace, "\r\n");
188 }
189 else
190 {
191 fprintf (NetTrace, "\n");
192 }
193 if (length < 0)
194 {
195 fflush (NetTrace);
196 return;
197 }
198 /* find next unique line */
199 }
200 fflush (NetTrace);
201 }
202
203
204 void
205 printoption (char *direction, int cmd, int option)
206 {
207 if (!showoptions)
208 return;
209 if (cmd == IAC)
210 {
211 if (TELCMD_OK (option))
212 fprintf (NetTrace, "%s IAC %s", direction, TELCMD (option));
213 else
214 fprintf (NetTrace, "%s IAC %d", direction, option);
215 }
216 else
217 {
218 char *fmt;
219 fmt = (cmd == WILL)
220 ? "WILL" : (cmd == WONT)
221 ? "WONT" : (cmd == DO) ? "DO" : (cmd == DONT) ? "DONT" : 0;
222 if (fmt)
223 {
224 fprintf (NetTrace, "%s %s ", direction, fmt);
225 if (TELOPT_OK (option))
226 fprintf (NetTrace, "%s", TELOPT (option));
227 else if (option == TELOPT_EXOPL)
228 fprintf (NetTrace, "EXOPL");
229 else
230 fprintf (NetTrace, "%d", option);
231 }
232 else
233 fprintf (NetTrace, "%s %d %d", direction, cmd, option);
234 }
235 if (NetTrace == stdout)
236 {
237 fprintf (NetTrace, "\r\n");
238 fflush (NetTrace);
239 }
240 else
241 {
242 fprintf (NetTrace, "\n");
243 }
244 return;
245 }
246
247 void
248 optionstatus (void)
249 {
250 int i;
251 extern char will_wont_resp[], do_dont_resp[];
252
253 for (i = 0; i < 256; i++)
254 {
255 if (do_dont_resp[i])
256 {
257 if (TELOPT_OK (i))
258 printf ("resp DO_DONT %s: %d\n", TELOPT (i), do_dont_resp[i]);
259 else if (TELCMD_OK (i))
260 printf ("resp DO_DONT %s: %d\n", TELCMD (i), do_dont_resp[i]);
261 else
262 printf ("resp DO_DONT %d: %d\n", i, do_dont_resp[i]);
263 if (my_want_state_is_do (i))
264 {
265 if (TELOPT_OK (i))
266 printf ("want DO %s\n", TELOPT (i));
267 else if (TELCMD_OK (i))
268 printf ("want DO %s\n", TELCMD (i));
269 else
270 printf ("want DO %d\n", i);
271 }
272 else
273 {
274 if (TELOPT_OK (i))
275 printf ("want DONT %s\n", TELOPT (i));
276 else if (TELCMD_OK (i))
277 printf ("want DONT %s\n", TELCMD (i));
278 else
279 printf ("want DONT %d\n", i);
280 }
281 }
282 else
283 {
284 if (my_state_is_do (i))
285 {
286 if (TELOPT_OK (i))
287 printf (" DO %s\n", TELOPT (i));
288 else if (TELCMD_OK (i))
289 printf (" DO %s\n", TELCMD (i));
290 else
291 printf (" DO %d\n", i);
292 }
293 }
294 if (will_wont_resp[i])
295 {
296 if (TELOPT_OK (i))
297 printf ("resp WILL_WONT %s: %d\n", TELOPT (i), will_wont_resp[i]);
298 else if (TELCMD_OK (i))
299 printf ("resp WILL_WONT %s: %d\n", TELCMD (i), will_wont_resp[i]);
300 else
301 printf ("resp WILL_WONT %d: %d\n", i, will_wont_resp[i]);
302 if (my_want_state_is_will (i))
303 {
304 if (TELOPT_OK (i))
305 printf ("want WILL %s\n", TELOPT (i));
306 else if (TELCMD_OK (i))
307 printf ("want WILL %s\n", TELCMD (i));
308 else
309 printf ("want WILL %d\n", i);
310 }
311 else
312 {
313 if (TELOPT_OK (i))
314 printf ("want WONT %s\n", TELOPT (i));
315 else if (TELCMD_OK (i))
316 printf ("want WONT %s\n", TELCMD (i));
317 else
318 printf ("want WONT %d\n", i);
319 }
320 }
321 else
322 {
323 if (my_state_is_will (i))
324 {
325 if (TELOPT_OK (i))
326 printf (" WILL %s\n", TELOPT (i));
327 else if (TELCMD_OK (i))
328 printf (" WILL %s\n", TELCMD (i));
329 else
330 printf (" WILL %d\n", i);
331 }
332 }
333 }
334
335 }
336
337 /* char direction; '<' or '>' */
338 /* unsigned char *pointer; where suboption data sits */
339 /* int length; length of suboption data */
340 void
341 printsub (char direction, unsigned char *pointer, int length)
342 {
343 int i;
344 extern int want_status_response;
345
346 #if defined AUTHENTICATION || defined ENCRYPTION
347 char buf[512];
348 #endif
349
350 if (showoptions || direction == 0 ||
351 (want_status_response && (pointer[0] == TELOPT_STATUS)))
352 {
353 if (direction)
354 {
355 fprintf (NetTrace, "%s IAC SB ",
356 (direction == '<') ? "RCVD" : "SENT");
357 if (length >= 3)
358 {
359 int j;
360
361 i = pointer[length - 2];
362 j = pointer[length - 1];
363
364 if (i != IAC || j != SE)
365 {
366 fprintf (NetTrace, "(terminated by ");
367 if (TELOPT_OK (i))
368 fprintf (NetTrace, "%s ", TELOPT (i));
369 else if (TELCMD_OK (i))
370 fprintf (NetTrace, "%s ", TELCMD (i));
371 else
372 fprintf (NetTrace, "%d ", i);
373 if (TELOPT_OK (j))
374 fprintf (NetTrace, "%s", TELOPT (j));
375 else if (TELCMD_OK (j))
376 fprintf (NetTrace, "%s", TELCMD (j));
377 else
378 fprintf (NetTrace, "%d", j);
379 fprintf (NetTrace, ", not IAC SE!) ");
380 }
381 }
382 length -= 2;
383 }
384 if (length < 1)
385 {
386 fprintf (NetTrace, "(Empty suboption??\?)");
387 if (NetTrace == stdout)
388 fflush (NetTrace);
389 return;
390 }
391 switch (pointer[0])
392 {
393 case TELOPT_TTYPE:
394 fprintf (NetTrace, "TERMINAL-TYPE ");
395 switch (pointer[1])
396 {
397 case TELQUAL_IS:
398 fprintf (NetTrace, "IS \"%.*s\"", length - 2,
399 (char *) pointer + 2);
400 break;
401 case TELQUAL_SEND:
402 fprintf (NetTrace, "SEND");
403 break;
404 default:
405 fprintf (NetTrace, "- unknown qualifier %d (0x%x).",
406 pointer[1], pointer[1]);
407 }
408 break;
409 case TELOPT_TSPEED:
410 fprintf (NetTrace, "TERMINAL-SPEED");
411 if (length < 2)
412 {
413 fprintf (NetTrace, " (empty suboption??\?)");
414 break;
415 }
416 switch (pointer[1])
417 {
418 case TELQUAL_IS:
419 fprintf (NetTrace, " IS ");
420 fprintf (NetTrace, "%.*s", length - 2, (char *) pointer + 2);
421 break;
422 default:
423 if (pointer[1] == 1)
424 fprintf (NetTrace, " SEND");
425 else
426 fprintf (NetTrace, " %d (unknown)", pointer[1]);
427 for (i = 2; i < length; i++)
428 fprintf (NetTrace, " ?%d?", pointer[i]);
429 break;
430 }
431 break;
432
433 case TELOPT_LFLOW:
434 fprintf (NetTrace, "TOGGLE-FLOW-CONTROL");
435 if (length < 2)
436 {
437 fprintf (NetTrace, " (empty suboption??\?)");
438 break;
439 }
440 switch (pointer[1])
441 {
442 case LFLOW_OFF:
443 fprintf (NetTrace, " OFF");
444 break;
445 case LFLOW_ON:
446 fprintf (NetTrace, " ON");
447 break;
448 case LFLOW_RESTART_ANY:
449 fprintf (NetTrace, " RESTART-ANY");
450 break;
451 case LFLOW_RESTART_XON:
452 fprintf (NetTrace, " RESTART-XON");
453 break;
454 default:
455 fprintf (NetTrace, " %d (unknown)", pointer[1]);
456 }
457 for (i = 2; i < length; i++)
458 fprintf (NetTrace, " ?%d?", pointer[i]);
459 break;
460
461 case TELOPT_NAWS:
462 fprintf (NetTrace, "NAWS");
463 if (length < 2)
464 {
465 fprintf (NetTrace, " (empty suboption??\?)");
466 break;
467 }
468 if (length == 2)
469 {
470 fprintf (NetTrace, " ?%d?", pointer[1]);
471 break;
472 }
473 fprintf (NetTrace, " %d %d (%d)",
474 pointer[1], pointer[2],
475 (int) ((((unsigned int) pointer[1]) << 8) |
476 ((unsigned int) pointer[2])));
477 if (length == 4)
478 {
479 fprintf (NetTrace, " ?%d?", pointer[3]);
480 break;
481 }
482 fprintf (NetTrace, " %d %d (%d)",
483 pointer[3], pointer[4],
484 (int) ((((unsigned int) pointer[3]) << 8) |
485 ((unsigned int) pointer[4])));
486 for (i = 5; i < length; i++)
487 fprintf (NetTrace, " ?%d?", pointer[i]);
488 break;
489
490 #if defined AUTHENTICATION
491 case TELOPT_AUTHENTICATION:
492 fprintf (NetTrace, "AUTHENTICATION");
493 if (length < 2)
494 {
495 fprintf (NetTrace, " (empty suboption??\?)");
496 break;
497 }
498 switch (pointer[1])
499 {
500 case TELQUAL_REPLY:
501 case TELQUAL_IS:
502 fprintf (NetTrace, " %s ",
503 (pointer[1] == TELQUAL_IS) ? "IS" : "REPLY");
504 if (AUTHTYPE_NAME_OK (pointer[2]) && AUTHTYPE_NAME (pointer[2]))
505 fprintf (NetTrace, "%s ", AUTHTYPE_NAME (pointer[2]));
506 else
507 fprintf (NetTrace, "%d ", pointer[2]);
508 if (length < 3)
509 {
510 fprintf (NetTrace, "(partial suboption??\?)");
511 break;
512 }
513 fprintf (NetTrace, "%s|%s",
514 ((pointer[3] & AUTH_WHO_MASK) == AUTH_WHO_CLIENT)
515 ? "CLIENT" : "SERVER",
516 ((pointer[3] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL)
517 ? "MUTUAL" : "ONE-WAY");
518
519 auth_printsub (&pointer[1], length - 1, buf, sizeof (buf));
520 fprintf (NetTrace, "%s", buf);
521 break;
522
523 case TELQUAL_SEND:
524 i = 2;
525 fprintf (NetTrace, " SEND ");
526 while (i < length)
527 {
528 if (AUTHTYPE_NAME_OK (pointer[i])
529 && AUTHTYPE_NAME (pointer[i]))
530 fprintf (NetTrace, "%s ", AUTHTYPE_NAME (pointer[i]));
531 else
532 fprintf (NetTrace, "%d ", pointer[i]);
533 if (++i >= length)
534 {
535 fprintf (NetTrace, "(partial suboption??\?)");
536 break;
537 }
538 fprintf (NetTrace, "%s|%s ",
539 ((pointer[i] & AUTH_WHO_MASK) == AUTH_WHO_CLIENT)
540 ? "CLIENT" : "SERVER",
541 ((pointer[i] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL)
542 ? "MUTUAL" : "ONE-WAY");
543 ++i;
544 }
545 break;
546
547 case TELQUAL_NAME:
548 i = 2;
549 fprintf (NetTrace, " NAME \"");
550 while (i < length)
551 putc (pointer[i++], NetTrace);
552 putc ('"', NetTrace);
553 break;
554
555 default:
556 for (i = 2; i < length; i++)
557 fprintf (NetTrace, " ?%d?", pointer[i]);
558 break;
559 }
560 break;
561 #endif
562
563 #ifdef ENCRYPTION
564 case TELOPT_ENCRYPT:
565 fprintf (NetTrace, "ENCRYPT");
566 if (length < 2)
567 {
568 fprintf (NetTrace, " (empty suboption??\?)");
569 break;
570 }
571 switch (pointer[1])
572 {
573 case ENCRYPT_START:
574 fprintf (NetTrace, " START");
575 break;
576
577 case ENCRYPT_END:
578 fprintf (NetTrace, " END");
579 break;
580
581 case ENCRYPT_REQSTART:
582 fprintf (NetTrace, " REQUEST-START");
583 break;
584
585 case ENCRYPT_REQEND:
586 fprintf (NetTrace, " REQUEST-END");
587 break;
588
589 case ENCRYPT_IS:
590 case ENCRYPT_REPLY:
591 fprintf (NetTrace, " %s ",
592 (pointer[1] == ENCRYPT_IS) ? "IS" : "REPLY");
593 if (length < 3)
594 {
595 fprintf (NetTrace, " (partial suboption??\?)");
596 break;
597 }
598 if (ENCTYPE_NAME_OK (pointer[2]) && ENCTYPE_NAME (pointer[2]))
599 fprintf (NetTrace, "%s ", ENCTYPE_NAME (pointer[2]));
600 else
601 fprintf (NetTrace, " %d (unknown)", pointer[2]);
602
603 encrypt_printsub (&pointer[1], length - 1, buf, sizeof (buf));
604 fprintf (NetTrace, "%s", buf);
605 break;
606
607 case ENCRYPT_SUPPORT:
608 i = 2;
609 fprintf (NetTrace, " SUPPORT ");
610 while (i < length)
611 {
612 if (ENCTYPE_NAME_OK (pointer[i])
613 && ENCTYPE_NAME (pointer[i]))
614 fprintf (NetTrace, "%s ", ENCTYPE_NAME (pointer[i]));
615 else
616 fprintf (NetTrace, "%d ", pointer[i]);
617 i++;
618 }
619 break;
620
621 case ENCRYPT_ENC_KEYID:
622 fprintf (NetTrace, " ENC_KEYID ");
623 goto encommon;
624
625 case ENCRYPT_DEC_KEYID:
626 fprintf (NetTrace, " DEC_KEYID ");
627 goto encommon;
628
629 default:
630 fprintf (NetTrace, " %d (unknown)", pointer[1]);
631 encommon:
632 for (i = 2; i < length; i++)
633 fprintf (NetTrace, " %d", pointer[i]);
634 break;
635 }
636 break;
637 #endif /* ENCRYPTION */
638
639 case TELOPT_LINEMODE:
640 fprintf (NetTrace, "LINEMODE ");
641 if (length < 2)
642 {
643 fprintf (NetTrace, " (empty suboption??\?)");
644 break;
645 }
646 switch (pointer[1])
647 {
648 case WILL:
649 fprintf (NetTrace, "WILL ");
650 goto common;
651 case WONT:
652 fprintf (NetTrace, "WONT ");
653 goto common;
654 case DO:
655 fprintf (NetTrace, "DO ");
656 goto common;
657 case DONT:
658 fprintf (NetTrace, "DONT ");
659 common:
660 if (length < 3)
661 {
662 fprintf (NetTrace, "(no option??\?)");
663 break;
664 }
665 switch (pointer[2])
666 {
667 case LM_FORWARDMASK:
668 fprintf (NetTrace, "Forward Mask");
669 for (i = 3; i < length; i++)
670 fprintf (NetTrace, " %x", pointer[i]);
671 break;
672 default:
673 fprintf (NetTrace, "%d (unknown)", pointer[2]);
674 for (i = 3; i < length; i++)
675 fprintf (NetTrace, " %d", pointer[i]);
676 break;
677 }
678 break;
679
680 case LM_SLC:
681 fprintf (NetTrace, "SLC");
682 for (i = 2; i < length - 2; i += 3)
683 {
684 if (SLC_NAME_OK (pointer[i + SLC_FUNC]))
685 fprintf (NetTrace, " %s",
686 SLC_NAME (pointer[i + SLC_FUNC]));
687 else
688 fprintf (NetTrace, " %d", pointer[i + SLC_FUNC]);
689 switch (pointer[i + SLC_FLAGS] & SLC_LEVELBITS)
690 {
691 case SLC_NOSUPPORT:
692 fprintf (NetTrace, " NOSUPPORT");
693 break;
694 case SLC_CANTCHANGE:
695 fprintf (NetTrace, " CANTCHANGE");
696 break;
697 case SLC_VARIABLE:
698 fprintf (NetTrace, " VARIABLE");
699 break;
700 case SLC_DEFAULT:
701 fprintf (NetTrace, " DEFAULT");
702 break;
703 }
704 fprintf (NetTrace, "%s%s%s",
705 (pointer[i + SLC_FLAGS] & SLC_ACK)
706 ? "|ACK" : "",
707 (pointer[i + SLC_FLAGS] & SLC_FLUSHIN)
708 ? "|FLUSHIN" : "",
709 (pointer[i + SLC_FLAGS] & SLC_FLUSHOUT)
710 ? "|FLUSHOUT" : "");
711 if (pointer[i + SLC_FLAGS] &
712 ~(SLC_ACK | SLC_FLUSHIN | SLC_FLUSHOUT | SLC_LEVELBITS))
713 fprintf (NetTrace, "(0x%x)", pointer[i + SLC_FLAGS]);
714 fprintf (NetTrace, " %d;", pointer[i + SLC_VALUE]);
715 if ((pointer[i + SLC_VALUE] == IAC) &&
716 (pointer[i + SLC_VALUE + 1] == IAC))
717 i++;
718 }
719 for (; i < length; i++)
720 fprintf (NetTrace, " ?%d?", pointer[i]);
721 break;
722
723 case LM_MODE:
724 fprintf (NetTrace, "MODE ");
725 if (length < 3)
726 {
727 fprintf (NetTrace, "(no mode??\?)");
728 break;
729 }
730 {
731 char tbuf[64];
732
733 snprintf (tbuf, sizeof (tbuf), "%s%s%s%s%s",
734 pointer[2] & MODE_EDIT ? "|EDIT" : "",
735 pointer[2] & MODE_TRAPSIG ? "|TRAPSIG" : "",
736 pointer[2] & MODE_SOFT_TAB ? "|SOFT_TAB" : "",
737 pointer[2] & MODE_LIT_ECHO ? "|LIT_ECHO" : "",
738 pointer[2] & MODE_ACK ? "|ACK" : "");
739 fprintf (NetTrace, "%s", tbuf[0] ? &tbuf[1] : "0");
740 }
741 if (pointer[2] & ~(MODE_MASK))
742 fprintf (NetTrace, " (0x%x)", pointer[2]);
743 for (i = 3; i < length; i++)
744 fprintf (NetTrace, " ?0x%x?", pointer[i]);
745 break;
746 default:
747 fprintf (NetTrace, "%d (unknown)", pointer[1]);
748 for (i = 2; i < length; i++)
749 fprintf (NetTrace, " %d", pointer[i]);
750 }
751 break;
752
753 case TELOPT_STATUS:
754 {
755 char *cp;
756 int j, k;
757
758 fprintf (NetTrace, "STATUS");
759
760 switch (pointer[1])
761 {
762 default:
763 if (pointer[1] == TELQUAL_SEND)
764 fprintf (NetTrace, " SEND");
765 else
766 fprintf (NetTrace, " %d (unknown)", pointer[1]);
767 for (i = 2; i < length; i++)
768 fprintf (NetTrace, " ?%d?", pointer[i]);
769 break;
770 case TELQUAL_IS:
771 if (--want_status_response < 0)
772 want_status_response = 0;
773 if (NetTrace == stdout)
774 fprintf (NetTrace, " IS\r\n");
775 else
776 fprintf (NetTrace, " IS\n");
777
778 for (i = 2; i < length; i++)
779 {
780 switch (pointer[i])
781 {
782 case DO:
783 cp = "DO";
784 goto common2;
785 case DONT:
786 cp = "DONT";
787 goto common2;
788 case WILL:
789 cp = "WILL";
790 goto common2;
791 case WONT:
792 cp = "WONT";
793 goto common2;
794 common2:
795 i++;
796 if (TELOPT_OK ((int) pointer[i]))
797 fprintf (NetTrace, " %s %s", cp,
798 TELOPT (pointer[i]));
799 else
800 fprintf (NetTrace, " %s %d", cp, pointer[i]);
801
802 if (NetTrace == stdout)
803 fprintf (NetTrace, "\r\n");
804 else
805 fprintf (NetTrace, "\n");
806 break;
807
808 case SB:
809 fprintf (NetTrace, " SB ");
810 i++;
811 j = k = i;
812 while (j < length)
813 {
814 if (pointer[j] == SE)
815 {
816 if (j + 1 == length)
817 break;
818 if (pointer[j + 1] == SE)
819 j++;
820 else
821 break;
822 }
823 pointer[k++] = pointer[j++];
824 }
825 printsub (0, &pointer[i], k - i);
826 if (i < length)
827 {
828 fprintf (NetTrace, " SE");
829 i = j;
830 }
831 else
832 i = j - 1;
833
834 if (NetTrace == stdout)
835 fprintf (NetTrace, "\r\n");
836 else
837 fprintf (NetTrace, "\n");
838
839 break;
840
841 default:
842 fprintf (NetTrace, " %d", pointer[i]);
843 break;
844 }
845 }
846 break;
847 }
848 break;
849 }
850
851 case TELOPT_XDISPLOC:
852 fprintf (NetTrace, "X-DISPLAY-LOCATION ");
853 switch (pointer[1])
854 {
855 case TELQUAL_IS:
856 fprintf (NetTrace, "IS \"%.*s\"", length - 2,
857 (char *) pointer + 2);
858 break;
859 case TELQUAL_SEND:
860 fprintf (NetTrace, "SEND");
861 break;
862 default:
863 fprintf (NetTrace, "- unknown qualifier %d (0x%x).",
864 pointer[1], pointer[1]);
865 }
866 break;
867
868 case TELOPT_NEW_ENVIRON:
869 fprintf (NetTrace, "NEW-ENVIRON ");
870 #ifdef OLD_ENVIRON
871 goto env_common1;
872 case TELOPT_OLD_ENVIRON:
873 fprintf (NetTrace, "OLD-ENVIRON");
874 env_common1:
875 #endif
876 switch (pointer[1])
877 {
878 case TELQUAL_IS:
879 fprintf (NetTrace, "IS ");
880 goto env_common;
881 case TELQUAL_SEND:
882 fprintf (NetTrace, "SEND ");
883 goto env_common;
884 case TELQUAL_INFO:
885 fprintf (NetTrace, "INFO ");
886 env_common:
887 {
888 const char *quote = "";
889 #if defined ENV_HACK && defined OLD_ENVIRON
890 extern int old_env_var, old_env_value;
891 #endif
892 for (i = 2; i < length; i++)
893 {
894 switch (pointer[i])
895 {
896 case NEW_ENV_VALUE:
897 #ifdef OLD_ENVIRON
898 /* case NEW_ENV_OVAR: */
899 if (pointer[0] == TELOPT_OLD_ENVIRON)
900 {
901 # ifdef ENV_HACK
902 if (old_env_var == OLD_ENV_VALUE)
903 fprintf (NetTrace, "%s(VALUE) ", quote);
904 else
905 # endif
906 fprintf (NetTrace, "%sVAR ", quote);
907 }
908 else
909 #endif /* OLD_ENVIRON */
910 fprintf (NetTrace, "%sVALUE ", quote);
911 quote = "";
912 break;
913
914 case NEW_ENV_VAR:
915 #ifdef OLD_ENVIRON
916 /* case OLD_ENV_VALUE: */
917 if (pointer[0] == TELOPT_OLD_ENVIRON)
918 {
919 # ifdef ENV_HACK
920 if (old_env_value == OLD_ENV_VAR)
921 fprintf (NetTrace, "%s(VAR) ", quote);
922 else
923 # endif
924 fprintf (NetTrace, "%sVALUE ", quote);
925 }
926 else
927 #endif /* OLD_ENVIRON */
928 fprintf (NetTrace, "%sVAR ", quote);
929 quote = "";
930 break;
931
932 case ENV_ESC:
933 fprintf (NetTrace, "%sESC ", quote);
934 quote = "";
935 break;
936
937 case ENV_USERVAR:
938 fprintf (NetTrace, "%sUSERVAR ", quote);
939 quote = "";
940 break;
941
942 default:
943 if (isprint (pointer[i]) && pointer[i] != '"')
944 {
945 if (quote[0] == '\0')
946 {
947 putc ('"', NetTrace);
948 quote = "\" ";
949 }
950 putc (pointer[i], NetTrace);
951 }
952 else
953 {
954 fprintf (NetTrace, "%s%03o ", quote, pointer[i]);
955 quote = "";
956 }
957 break;
958 }
959 }
960 if (quote[0] != '\0')
961 putc ('"', NetTrace);
962 break;
963 }
964 }
965 break;
966
967 default:
968 if (TELOPT_OK (pointer[0]))
969 fprintf (NetTrace, "%s (unknown)", TELOPT (pointer[0]));
970 else
971 fprintf (NetTrace, "%d (unknown)", pointer[0]);
972 for (i = 1; i < length; i++)
973 fprintf (NetTrace, " %d", pointer[i]);
974 break;
975 }
976 if (direction)
977 {
978 if (NetTrace == stdout)
979 fprintf (NetTrace, "\r\n");
980 else
981 fprintf (NetTrace, "\n");
982 }
983 if (NetTrace == stdout)
984 fflush (NetTrace);
985 }
986 }
987
988 /* EmptyTerminal - called to make sure that the terminal buffer is empty.
989 * Note that we consider the buffer to run all the
990 * way to the kernel (thus the select).
991 */
992
993 void
994 EmptyTerminal (void)
995 {
996 #if defined unix || defined __unix || defined __unix__
997 fd_set o;
998
999 FD_ZERO (&o);
1000 #endif /* unix || __unix || __unix__ */
1001
1002 if (TTYBYTES () == 0)
1003 {
1004 #if defined unix || defined __unix || defined __unix__
1005 FD_SET (tout, &o);
1006 select (tout + 1, (fd_set *) 0, &o, (fd_set *) 0, (struct timeval *) 0); /* wait for TTLOWAT */
1007 #endif /* unix || __unix || __unix__ */
1008 }
1009 else
1010 {
1011 while (TTYBYTES ())
1012 {
1013 ttyflush (0);
1014 #if defined unix || defined __unix || defined __unix__
1015 FD_SET (tout, &o);
1016 select (tout + 1, (fd_set *) 0, &o, (fd_set *) 0, (struct timeval *) 0); /* wait for TTLOWAT */
1017 #endif /* unix || __unix || __unix__ */
1018 }
1019 }
1020 }
1021
1022 void
1023 SetForExit (void)
1024 {
1025 setconnmode (0);
1026 do
1027 {
1028 telrcv (); /* Process any incoming data */
1029 EmptyTerminal ();
1030 }
1031 while (ring_full_count (&netiring)); /* While there is any */
1032 setcommandmode ();
1033 fflush (stdout);
1034 fflush (stderr);
1035 setconnmode (0);
1036 EmptyTerminal (); /* Flush the path to the tty */
1037 setcommandmode ();
1038 }
1039
1040 void
1041 Exit (int returnCode)
1042 {
1043 SetForExit ();
1044 exit (returnCode);
1045 }
1046
1047 void
1048 ExitString (char *string, int returnCode)
1049 {
1050 fwrite (string, 1, strlen (string), stderr);
1051 exit (returnCode);
1052 }