2 * Copyright (c) 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #include "telnet_locl.h"
42 FILE *NetTrace = 0; /* Not in bss, since needs to stay */
48 * Compensate for differences in 4.2 and 4.3 systems.
52 SetSockOpt(int fd, int level, int option, int yesno)
54 #ifdef HAVE_SETSOCKOPT
56 return setsockopt(fd, level, option,
57 (void *)&yesno, sizeof yesno);
59 if (yesno == 0) { /* Can't do that in 4.2! */
60 fprintf(stderr, "Error: attempt to turn off an option 0x%x.\n",
64 return setsockopt(fd, level, option, 0, 0);
72 * The following are routines used to print out debugging information.
75 char NetTraceFile[256] = "(standard output)";
78 SetNetTrace(char *file)
80 if (NetTrace && NetTrace != stdout)
82 if (file && (strcmp(file, "-") != 0)) {
83 NetTrace = fopen(file, "w");
85 strlcpy(NetTraceFile, file, sizeof(NetTraceFile));
88 fprintf(stderr, "Cannot open %s.\n", file);
91 strlcpy(NetTraceFile, "(standard output)", sizeof(NetTraceFile));
95 Dump(char direction, unsigned char *buffer, int length)
97 # define BYTES_PER_LINE 32
105 fprintf(NetTrace, "%c 0x%x\t", direction, offset);
108 buffer = buffer + min(length, BYTES_PER_LINE/2);
109 while (pThis < buffer) {
110 fprintf(NetTrace, "%c%.2x",
111 (((*pThis)&0xff) == 0xff) ? '*' : ' ',
115 length -= BYTES_PER_LINE/2;
116 offset += BYTES_PER_LINE/2;
118 buffer = buffer + min(length, BYTES_PER_LINE);
119 while (pThis < buffer) {
120 fprintf(NetTrace, "%.2x", (*pThis)&0xff);
123 length -= BYTES_PER_LINE;
124 offset += BYTES_PER_LINE;
126 if (NetTrace == stdout) {
127 fprintf(NetTrace, "\r\n");
129 fprintf(NetTrace, "\n");
135 /* find next unique line */
142 printoption(char *direction, int cmd, int option)
147 if (TELCMD_OK(option))
148 fprintf(NetTrace, "%s IAC %s", direction, TELCMD(option));
150 fprintf(NetTrace, "%s IAC %d", direction, option);
153 fmt = (cmd == WILL) ? "WILL" : (cmd == WONT) ? "WONT" :
154 (cmd == DO) ? "DO" : (cmd == DONT) ? "DONT" : 0;
156 fprintf(NetTrace, "%s %s ", direction, fmt);
157 if (TELOPT_OK(option))
158 fprintf(NetTrace, "%s", TELOPT(option));
159 else if (option == TELOPT_EXOPL)
160 fprintf(NetTrace, "EXOPL");
162 fprintf(NetTrace, "%d", option);
164 fprintf(NetTrace, "%s %d %d", direction, cmd, option);
166 if (NetTrace == stdout) {
167 fprintf(NetTrace, "\r\n");
170 fprintf(NetTrace, "\n");
180 for (i = 0; i < 256; i++) {
181 if (do_dont_resp[i]) {
183 printf("resp DO_DONT %s: %d\n", TELOPT(i), do_dont_resp[i]);
184 else if (TELCMD_OK(i))
185 printf("resp DO_DONT %s: %d\n", TELCMD(i), do_dont_resp[i]);
187 printf("resp DO_DONT %d: %d\n", i,
189 if (my_want_state_is_do(i)) {
191 printf("want DO %s\n", TELOPT(i));
192 else if (TELCMD_OK(i))
193 printf("want DO %s\n", TELCMD(i));
195 printf("want DO %d\n", i);
198 printf("want DONT %s\n", TELOPT(i));
199 else if (TELCMD_OK(i))
200 printf("want DONT %s\n", TELCMD(i));
202 printf("want DONT %d\n", i);
205 if (my_state_is_do(i)) {
207 printf(" DO %s\n", TELOPT(i));
208 else if (TELCMD_OK(i))
209 printf(" DO %s\n", TELCMD(i));
211 printf(" DO %d\n", i);
214 if (will_wont_resp[i]) {
216 printf("resp WILL_WONT %s: %d\n", TELOPT(i), will_wont_resp[i]);
217 else if (TELCMD_OK(i))
218 printf("resp WILL_WONT %s: %d\n", TELCMD(i), will_wont_resp[i]);
220 printf("resp WILL_WONT %d: %d\n",
221 i, will_wont_resp[i]);
222 if (my_want_state_is_will(i)) {
224 printf("want WILL %s\n", TELOPT(i));
225 else if (TELCMD_OK(i))
226 printf("want WILL %s\n", TELCMD(i));
228 printf("want WILL %d\n", i);
231 printf("want WONT %s\n", TELOPT(i));
232 else if (TELCMD_OK(i))
233 printf("want WONT %s\n", TELCMD(i));
235 printf("want WONT %d\n", i);
238 if (my_state_is_will(i)) {
240 printf(" WILL %s\n", TELOPT(i));
241 else if (TELCMD_OK(i))
242 printf(" WILL %s\n", TELCMD(i));
244 printf(" WILL %d\n", i);
251 static void __attribute__((format (printf, 3, 4)))
252 qprintf(int quote, FILE *f, const char *fmt, ...)
259 vfprintf(f, fmt, va);
264 printsub(int direction, unsigned char *pointer, size_t length)
267 unsigned char buf[512];
269 if (showoptions || direction == 0 ||
270 (want_status_response && (pointer[0] == TELOPT_STATUS))) {
272 fprintf(NetTrace, "%s IAC SB ",
273 (direction == '<')? "RCVD":"SENT");
277 i = pointer[length-2];
278 j = pointer[length-1];
280 if (i != IAC || j != SE) {
281 fprintf(NetTrace, "(terminated by ");
283 fprintf(NetTrace, "%s ", TELOPT(i));
284 else if (TELCMD_OK(i))
285 fprintf(NetTrace, "%s ", TELCMD(i));
287 fprintf(NetTrace, "%d ", i);
289 fprintf(NetTrace, "%s", TELOPT(j));
290 else if (TELCMD_OK(j))
291 fprintf(NetTrace, "%s", TELCMD(j));
293 fprintf(NetTrace, "%d", j);
294 fprintf(NetTrace, ", not IAC SE!) ");
300 fprintf(NetTrace, "(Empty suboption??\?)");
301 if (NetTrace == stdout)
305 switch (pointer[0]) {
307 fprintf(NetTrace, "TERMINAL-TYPE ");
308 switch (pointer[1]) {
310 fprintf(NetTrace, "IS \"%.*s\"",
315 fprintf(NetTrace, "SEND");
319 "- unknown qualifier %d (0x%x).",
320 pointer[1], pointer[1]);
324 fprintf(NetTrace, "TERMINAL-SPEED");
326 fprintf(NetTrace, " (empty suboption??\?)");
329 switch (pointer[1]) {
331 fprintf(NetTrace, " IS ");
332 fprintf(NetTrace, "%.*s", (int)(length-2), (char *)pointer+2);
336 fprintf(NetTrace, " SEND");
338 fprintf(NetTrace, " %d (unknown)", pointer[1]);
339 for (i = 2; i < length; i++)
340 fprintf(NetTrace, " ?%d?", pointer[i]);
346 fprintf(NetTrace, "TOGGLE-FLOW-CONTROL");
348 fprintf(NetTrace, " (empty suboption??\?)");
351 switch (pointer[1]) {
353 fprintf(NetTrace, " OFF"); break;
355 fprintf(NetTrace, " ON"); break;
356 case LFLOW_RESTART_ANY:
357 fprintf(NetTrace, " RESTART-ANY"); break;
358 case LFLOW_RESTART_XON:
359 fprintf(NetTrace, " RESTART-XON"); break;
361 fprintf(NetTrace, " %d (unknown)", pointer[1]);
363 for (i = 2; i < length; i++)
364 fprintf(NetTrace, " ?%d?", pointer[i]);
368 fprintf(NetTrace, "NAWS");
370 fprintf(NetTrace, " (empty suboption??\?)");
374 fprintf(NetTrace, " ?%d?", pointer[1]);
377 fprintf(NetTrace, " %d %d (%d)",
378 pointer[1], pointer[2],
379 (int)((((unsigned int)pointer[1])<<8)|((unsigned int)pointer[2])));
381 fprintf(NetTrace, " ?%d?", pointer[3]);
384 fprintf(NetTrace, " %d %d (%d)",
385 pointer[3], pointer[4],
386 (int)((((unsigned int)pointer[3])<<8)|((unsigned int)pointer[4])));
387 for (i = 5; i < length; i++)
388 fprintf(NetTrace, " ?%d?", pointer[i]);
391 #if defined(AUTHENTICATION)
392 case TELOPT_AUTHENTICATION:
393 fprintf(NetTrace, "AUTHENTICATION");
395 fprintf(NetTrace, " (empty suboption??\?)");
398 switch (pointer[1]) {
401 fprintf(NetTrace, " %s ", (pointer[1] == TELQUAL_IS) ?
403 if (AUTHTYPE_NAME_OK(pointer[2]))
404 fprintf(NetTrace, "%s ", AUTHTYPE_NAME(pointer[2]));
406 fprintf(NetTrace, "%d ", pointer[2]);
408 fprintf(NetTrace, "(partial suboption??\?)");
411 fprintf(NetTrace, "%s|%s",
412 ((pointer[3] & AUTH_WHO_MASK) == AUTH_WHO_CLIENT) ?
414 ((pointer[3] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) ?
415 "MUTUAL" : "ONE-WAY");
417 auth_printsub(&pointer[1], length - 1, buf, sizeof(buf));
418 fprintf(NetTrace, "%s", buf);
423 fprintf(NetTrace, " SEND ");
425 if (AUTHTYPE_NAME_OK(pointer[i]))
426 fprintf(NetTrace, "%s ", AUTHTYPE_NAME(pointer[i]));
428 fprintf(NetTrace, "%d ", pointer[i]);
430 fprintf(NetTrace, "(partial suboption??\?)");
433 fprintf(NetTrace, "%s|%s ",
434 ((pointer[i] & AUTH_WHO_MASK) == AUTH_WHO_CLIENT) ?
436 ((pointer[i] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) ?
437 "MUTUAL" : "ONE-WAY");
444 fprintf(NetTrace, " NAME \"");
446 putc(pointer[i++], NetTrace);
451 for (i = 2; i < length; i++)
452 fprintf(NetTrace, " ?%d?", pointer[i]);
458 #if defined(ENCRYPTION)
460 fprintf(NetTrace, "ENCRYPT");
462 fprintf(NetTrace, " (empty suboption?)");
465 switch (pointer[1]) {
467 fprintf(NetTrace, " START");
471 fprintf(NetTrace, " END");
474 case ENCRYPT_REQSTART:
475 fprintf(NetTrace, " REQUEST-START");
479 fprintf(NetTrace, " REQUEST-END");
484 fprintf(NetTrace, " %s ", (pointer[1] == ENCRYPT_IS) ?
487 fprintf(NetTrace, " (partial suboption?)");
490 if (ENCTYPE_NAME_OK(pointer[2]))
491 fprintf(NetTrace, "%s ", ENCTYPE_NAME(pointer[2]));
493 fprintf(NetTrace, " %d (unknown)", pointer[2]);
495 encrypt_printsub(&pointer[1], length - 1, buf, sizeof(buf));
496 fprintf(NetTrace, "%s", buf);
499 case ENCRYPT_SUPPORT:
501 fprintf(NetTrace, " SUPPORT ");
503 if (ENCTYPE_NAME_OK(pointer[i]))
504 fprintf(NetTrace, "%s ", ENCTYPE_NAME(pointer[i]));
506 fprintf(NetTrace, "%d ", pointer[i]);
511 case ENCRYPT_ENC_KEYID:
512 fprintf(NetTrace, " ENC_KEYID ");
515 case ENCRYPT_DEC_KEYID:
516 fprintf(NetTrace, " DEC_KEYID ");
520 fprintf(NetTrace, " %d (unknown)", pointer[1]);
522 for (i = 2; i < length; i++)
523 fprintf(NetTrace, " %d", pointer[i]);
529 case TELOPT_LINEMODE:
530 fprintf(NetTrace, "LINEMODE ");
532 fprintf(NetTrace, " (empty suboption??\?)");
535 switch (pointer[1]) {
537 fprintf(NetTrace, "WILL ");
540 fprintf(NetTrace, "WONT ");
543 fprintf(NetTrace, "DO ");
546 fprintf(NetTrace, "DONT ");
549 fprintf(NetTrace, "(no option??\?)");
552 switch (pointer[2]) {
554 fprintf(NetTrace, "Forward Mask");
555 for (i = 3; i < length; i++)
556 fprintf(NetTrace, " %x", pointer[i]);
559 fprintf(NetTrace, "%d (unknown)", pointer[2]);
560 for (i = 3; i < length; i++)
561 fprintf(NetTrace, " %d", pointer[i]);
567 fprintf(NetTrace, "SLC");
568 for (i = 2; i < length - 2; i += 3) {
569 if (SLC_NAME_OK(pointer[i+SLC_FUNC]))
570 fprintf(NetTrace, " %s", SLC_NAME(pointer[i+SLC_FUNC]));
572 fprintf(NetTrace, " %d", pointer[i+SLC_FUNC]);
573 switch (pointer[i+SLC_FLAGS]&SLC_LEVELBITS) {
575 fprintf(NetTrace, " NOSUPPORT"); break;
577 fprintf(NetTrace, " CANTCHANGE"); break;
579 fprintf(NetTrace, " VARIABLE"); break;
581 fprintf(NetTrace, " DEFAULT"); break;
583 fprintf(NetTrace, "%s%s%s",
584 pointer[i+SLC_FLAGS]&SLC_ACK ? "|ACK" : "",
585 pointer[i+SLC_FLAGS]&SLC_FLUSHIN ? "|FLUSHIN" : "",
586 pointer[i+SLC_FLAGS]&SLC_FLUSHOUT ? "|FLUSHOUT" : "");
587 if (pointer[i+SLC_FLAGS]& ~(SLC_ACK|SLC_FLUSHIN|
588 SLC_FLUSHOUT| SLC_LEVELBITS))
589 fprintf(NetTrace, "(0x%x)", pointer[i+SLC_FLAGS]);
590 fprintf(NetTrace, " %d;", pointer[i+SLC_VALUE]);
591 if ((pointer[i+SLC_VALUE] == IAC) &&
592 (pointer[i+SLC_VALUE+1] == IAC))
595 for (; i < length; i++)
596 fprintf(NetTrace, " ?%d?", pointer[i]);
600 fprintf(NetTrace, "MODE ");
602 fprintf(NetTrace, "(no mode??\?)");
607 snprintf(tbuf, sizeof(tbuf),
609 pointer[2]&MODE_EDIT ? "|EDIT" : "",
610 pointer[2]&MODE_TRAPSIG ? "|TRAPSIG" : "",
611 pointer[2]&MODE_SOFT_TAB ? "|SOFT_TAB" : "",
612 pointer[2]&MODE_LIT_ECHO ? "|LIT_ECHO" : "",
613 pointer[2]&MODE_ACK ? "|ACK" : "");
614 fprintf(NetTrace, "%s", tbuf[1] ? &tbuf[1] : "0");
616 if (pointer[2]&~(MODE_MASK))
617 fprintf(NetTrace, " (0x%x)", pointer[2]);
618 for (i = 3; i < length; i++)
619 fprintf(NetTrace, " ?0x%x?", pointer[i]);
622 fprintf(NetTrace, "%d (unknown)", pointer[1]);
623 for (i = 2; i < length; i++)
624 fprintf(NetTrace, " %d", pointer[i]);
628 case TELOPT_STATUS: {
632 fprintf(NetTrace, "STATUS");
634 switch (pointer[1]) {
636 if (pointer[1] == TELQUAL_SEND)
637 fprintf(NetTrace, " SEND");
639 fprintf(NetTrace, " %d (unknown)", pointer[1]);
640 for (i = 2; i < length; i++)
641 fprintf(NetTrace, " ?%d?", pointer[i]);
644 if (--want_status_response < 0)
645 want_status_response = 0;
646 if (NetTrace == stdout)
647 fprintf(NetTrace, " IS\r\n");
649 fprintf(NetTrace, " IS\n");
651 for (i = 2; i < length; i++) {
653 case DO: cp = "DO"; goto common2;
654 case DONT: cp = "DONT"; goto common2;
655 case WILL: cp = "WILL"; goto common2;
656 case WONT: cp = "WONT"; goto common2;
659 if (TELOPT_OK((int)pointer[i]))
660 fprintf(NetTrace, " %s %s", cp, TELOPT(pointer[i]));
662 fprintf(NetTrace, " %s %d", cp, pointer[i]);
664 if (NetTrace == stdout)
665 fprintf(NetTrace, "\r\n");
667 fprintf(NetTrace, "\n");
671 fprintf(NetTrace, " SB ");
675 if (pointer[j] == SE) {
678 if (pointer[j+1] == SE)
683 pointer[k++] = pointer[j++];
685 printsub(0, &pointer[i], k - i);
687 fprintf(NetTrace, " SE");
692 if (NetTrace == stdout)
693 fprintf(NetTrace, "\r\n");
695 fprintf(NetTrace, "\n");
700 fprintf(NetTrace, " %d", pointer[i]);
709 case TELOPT_XDISPLOC:
710 fprintf(NetTrace, "X-DISPLAY-LOCATION ");
711 switch (pointer[1]) {
713 fprintf(NetTrace, "IS \"%.*s\"", (int)(length-2), (char *)pointer+2);
716 fprintf(NetTrace, "SEND");
719 fprintf(NetTrace, "- unknown qualifier %d (0x%x).",
720 pointer[1], pointer[1]);
724 case TELOPT_NEW_ENVIRON:
725 fprintf(NetTrace, "NEW-ENVIRON ");
728 case TELOPT_OLD_ENVIRON:
729 fprintf(NetTrace, "OLD-ENVIRON");
732 switch (pointer[1]) {
734 fprintf(NetTrace, "IS ");
737 fprintf(NetTrace, "SEND ");
740 fprintf(NetTrace, "INFO ");
744 for (i = 2; i < length; i++ ) {
745 switch (pointer[i]) {
747 qprintf(quote, NetTrace, "VAR ");
752 qprintf(quote, NetTrace, "VALUE");
757 qprintf(quote, NetTrace, "ESC ");
762 qprintf(quote, NetTrace, "USERVAR ");
767 if (isprint(pointer[i]) && pointer[i] != '"') {
772 putc(pointer[i], NetTrace);
774 qprintf(quote, NetTrace, "%03o ", pointer[i]);
788 if (TELOPT_OK(pointer[0]))
789 fprintf(NetTrace, "%s (unknown)", TELOPT(pointer[0]));
791 fprintf(NetTrace, "%d (unknown)", pointer[0]);
792 for (i = 1; i < length; i++)
793 fprintf(NetTrace, " %d", pointer[i]);
797 if (NetTrace == stdout)
798 fprintf(NetTrace, "\r\n");
800 fprintf(NetTrace, "\n");
802 if (NetTrace == stdout)
807 /* EmptyTerminal - called to make sure that the terminal buffer is empty.
808 * Note that we consider the buffer to run all the
809 * way to the kernel (thus the select).
819 if (tout >= FD_SETSIZE)
820 ExitString("fd too large", 1);
822 if (TTYBYTES() == 0) {
824 select(tout+1, 0, &outs, 0,
825 (struct timeval *) 0); /* wait for TTLOWAT */
830 select(tout+1, 0, &outs, 0,
831 (struct timeval *) 0); /* wait for TTLOWAT */
841 telrcv(); /* Process any incoming data */
843 } while (ring_full_count(&netiring)); /* While there is any */
848 EmptyTerminal(); /* Flush the path to the tty */
860 ExitString(char *string, int returnCode)
863 fwrite(string, 1, strlen(string), stderr);