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"
40 RCSID("$Id: utilities.c,v 1.22.2.1 2000/10/10 13:10:27 assar Exp $");
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");
179 extern char will_wont_resp[], do_dont_resp[];
181 for (i = 0; i < 256; i++) {
182 if (do_dont_resp[i]) {
184 printf("resp DO_DONT %s: %d\n", TELOPT(i), do_dont_resp[i]);
185 else if (TELCMD_OK(i))
186 printf("resp DO_DONT %s: %d\n", TELCMD(i), do_dont_resp[i]);
188 printf("resp DO_DONT %d: %d\n", i,
190 if (my_want_state_is_do(i)) {
192 printf("want DO %s\n", TELOPT(i));
193 else if (TELCMD_OK(i))
194 printf("want DO %s\n", TELCMD(i));
196 printf("want DO %d\n", i);
199 printf("want DONT %s\n", TELOPT(i));
200 else if (TELCMD_OK(i))
201 printf("want DONT %s\n", TELCMD(i));
203 printf("want DONT %d\n", i);
206 if (my_state_is_do(i)) {
208 printf(" DO %s\n", TELOPT(i));
209 else if (TELCMD_OK(i))
210 printf(" DO %s\n", TELCMD(i));
212 printf(" DO %d\n", i);
215 if (will_wont_resp[i]) {
217 printf("resp WILL_WONT %s: %d\n", TELOPT(i), will_wont_resp[i]);
218 else if (TELCMD_OK(i))
219 printf("resp WILL_WONT %s: %d\n", TELCMD(i), will_wont_resp[i]);
221 printf("resp WILL_WONT %d: %d\n",
222 i, will_wont_resp[i]);
223 if (my_want_state_is_will(i)) {
225 printf("want WILL %s\n", TELOPT(i));
226 else if (TELCMD_OK(i))
227 printf("want WILL %s\n", TELCMD(i));
229 printf("want WILL %d\n", i);
232 printf("want WONT %s\n", TELOPT(i));
233 else if (TELCMD_OK(i))
234 printf("want WONT %s\n", TELCMD(i));
236 printf("want WONT %d\n", i);
239 if (my_state_is_will(i)) {
241 printf(" WILL %s\n", TELOPT(i));
242 else if (TELCMD_OK(i))
243 printf(" WILL %s\n", TELCMD(i));
245 printf(" WILL %d\n", i);
253 printsub(int direction, unsigned char *pointer, int length)
256 unsigned char buf[512];
257 extern int want_status_response;
259 if (showoptions || direction == 0 ||
260 (want_status_response && (pointer[0] == TELOPT_STATUS))) {
262 fprintf(NetTrace, "%s IAC SB ",
263 (direction == '<')? "RCVD":"SENT");
267 i = pointer[length-2];
268 j = pointer[length-1];
270 if (i != IAC || j != SE) {
271 fprintf(NetTrace, "(terminated by ");
273 fprintf(NetTrace, "%s ", TELOPT(i));
274 else if (TELCMD_OK(i))
275 fprintf(NetTrace, "%s ", TELCMD(i));
277 fprintf(NetTrace, "%d ", i);
279 fprintf(NetTrace, "%s", TELOPT(j));
280 else if (TELCMD_OK(j))
281 fprintf(NetTrace, "%s", TELCMD(j));
283 fprintf(NetTrace, "%d", j);
284 fprintf(NetTrace, ", not IAC SE!) ");
290 fprintf(NetTrace, "(Empty suboption??\?)");
291 if (NetTrace == stdout)
295 switch (pointer[0]) {
297 fprintf(NetTrace, "TERMINAL-TYPE ");
298 switch (pointer[1]) {
300 fprintf(NetTrace, "IS \"%.*s\"", length-2, (char *)pointer+2);
303 fprintf(NetTrace, "SEND");
307 "- unknown qualifier %d (0x%x).",
308 pointer[1], pointer[1]);
312 fprintf(NetTrace, "TERMINAL-SPEED");
314 fprintf(NetTrace, " (empty suboption??\?)");
317 switch (pointer[1]) {
319 fprintf(NetTrace, " IS ");
320 fprintf(NetTrace, "%.*s", length-2, (char *)pointer+2);
324 fprintf(NetTrace, " SEND");
326 fprintf(NetTrace, " %d (unknown)", pointer[1]);
327 for (i = 2; i < length; i++)
328 fprintf(NetTrace, " ?%d?", pointer[i]);
334 fprintf(NetTrace, "TOGGLE-FLOW-CONTROL");
336 fprintf(NetTrace, " (empty suboption??\?)");
339 switch (pointer[1]) {
341 fprintf(NetTrace, " OFF"); break;
343 fprintf(NetTrace, " ON"); break;
344 case LFLOW_RESTART_ANY:
345 fprintf(NetTrace, " RESTART-ANY"); break;
346 case LFLOW_RESTART_XON:
347 fprintf(NetTrace, " RESTART-XON"); break;
349 fprintf(NetTrace, " %d (unknown)", pointer[1]);
351 for (i = 2; i < length; i++)
352 fprintf(NetTrace, " ?%d?", pointer[i]);
356 fprintf(NetTrace, "NAWS");
358 fprintf(NetTrace, " (empty suboption??\?)");
362 fprintf(NetTrace, " ?%d?", pointer[1]);
365 fprintf(NetTrace, " %d %d (%d)",
366 pointer[1], pointer[2],
367 (int)((((unsigned int)pointer[1])<<8)|((unsigned int)pointer[2])));
369 fprintf(NetTrace, " ?%d?", pointer[3]);
372 fprintf(NetTrace, " %d %d (%d)",
373 pointer[3], pointer[4],
374 (int)((((unsigned int)pointer[3])<<8)|((unsigned int)pointer[4])));
375 for (i = 5; i < length; i++)
376 fprintf(NetTrace, " ?%d?", pointer[i]);
379 #if defined(AUTHENTICATION)
380 case TELOPT_AUTHENTICATION:
381 fprintf(NetTrace, "AUTHENTICATION");
383 fprintf(NetTrace, " (empty suboption??\?)");
386 switch (pointer[1]) {
389 fprintf(NetTrace, " %s ", (pointer[1] == TELQUAL_IS) ?
391 if (AUTHTYPE_NAME_OK(pointer[2]))
392 fprintf(NetTrace, "%s ", AUTHTYPE_NAME(pointer[2]));
394 fprintf(NetTrace, "%d ", pointer[2]);
396 fprintf(NetTrace, "(partial suboption??\?)");
399 fprintf(NetTrace, "%s|%s",
400 ((pointer[3] & AUTH_WHO_MASK) == AUTH_WHO_CLIENT) ?
402 ((pointer[3] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) ?
403 "MUTUAL" : "ONE-WAY");
405 auth_printsub(&pointer[1], length - 1, buf, sizeof(buf));
406 fprintf(NetTrace, "%s", buf);
411 fprintf(NetTrace, " SEND ");
413 if (AUTHTYPE_NAME_OK(pointer[i]))
414 fprintf(NetTrace, "%s ", AUTHTYPE_NAME(pointer[i]));
416 fprintf(NetTrace, "%d ", pointer[i]);
418 fprintf(NetTrace, "(partial suboption??\?)");
421 fprintf(NetTrace, "%s|%s ",
422 ((pointer[i] & AUTH_WHO_MASK) == AUTH_WHO_CLIENT) ?
424 ((pointer[i] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) ?
425 "MUTUAL" : "ONE-WAY");
432 fprintf(NetTrace, " NAME \"");
434 putc(pointer[i++], NetTrace);
439 for (i = 2; i < length; i++)
440 fprintf(NetTrace, " ?%d?", pointer[i]);
446 #if defined(ENCRYPTION)
448 fprintf(NetTrace, "ENCRYPT");
450 fprintf(NetTrace, " (empty suboption?)");
453 switch (pointer[1]) {
455 fprintf(NetTrace, " START");
459 fprintf(NetTrace, " END");
462 case ENCRYPT_REQSTART:
463 fprintf(NetTrace, " REQUEST-START");
467 fprintf(NetTrace, " REQUEST-END");
472 fprintf(NetTrace, " %s ", (pointer[1] == ENCRYPT_IS) ?
475 fprintf(NetTrace, " (partial suboption?)");
478 if (ENCTYPE_NAME_OK(pointer[2]))
479 fprintf(NetTrace, "%s ", ENCTYPE_NAME(pointer[2]));
481 fprintf(NetTrace, " %d (unknown)", pointer[2]);
483 encrypt_printsub(&pointer[1], length - 1, buf, sizeof(buf));
484 fprintf(NetTrace, "%s", buf);
487 case ENCRYPT_SUPPORT:
489 fprintf(NetTrace, " SUPPORT ");
491 if (ENCTYPE_NAME_OK(pointer[i]))
492 fprintf(NetTrace, "%s ", ENCTYPE_NAME(pointer[i]));
494 fprintf(NetTrace, "%d ", pointer[i]);
499 case ENCRYPT_ENC_KEYID:
500 fprintf(NetTrace, " ENC_KEYID ");
503 case ENCRYPT_DEC_KEYID:
504 fprintf(NetTrace, " DEC_KEYID ");
508 fprintf(NetTrace, " %d (unknown)", pointer[1]);
510 for (i = 2; i < length; i++)
511 fprintf(NetTrace, " %d", pointer[i]);
517 case TELOPT_LINEMODE:
518 fprintf(NetTrace, "LINEMODE ");
520 fprintf(NetTrace, " (empty suboption??\?)");
523 switch (pointer[1]) {
525 fprintf(NetTrace, "WILL ");
528 fprintf(NetTrace, "WONT ");
531 fprintf(NetTrace, "DO ");
534 fprintf(NetTrace, "DONT ");
537 fprintf(NetTrace, "(no option??\?)");
540 switch (pointer[2]) {
542 fprintf(NetTrace, "Forward Mask");
543 for (i = 3; i < length; i++)
544 fprintf(NetTrace, " %x", pointer[i]);
547 fprintf(NetTrace, "%d (unknown)", pointer[2]);
548 for (i = 3; i < length; i++)
549 fprintf(NetTrace, " %d", pointer[i]);
555 fprintf(NetTrace, "SLC");
556 for (i = 2; i < length - 2; i += 3) {
557 if (SLC_NAME_OK(pointer[i+SLC_FUNC]))
558 fprintf(NetTrace, " %s", SLC_NAME(pointer[i+SLC_FUNC]));
560 fprintf(NetTrace, " %d", pointer[i+SLC_FUNC]);
561 switch (pointer[i+SLC_FLAGS]&SLC_LEVELBITS) {
563 fprintf(NetTrace, " NOSUPPORT"); break;
565 fprintf(NetTrace, " CANTCHANGE"); break;
567 fprintf(NetTrace, " VARIABLE"); break;
569 fprintf(NetTrace, " DEFAULT"); break;
571 fprintf(NetTrace, "%s%s%s",
572 pointer[i+SLC_FLAGS]&SLC_ACK ? "|ACK" : "",
573 pointer[i+SLC_FLAGS]&SLC_FLUSHIN ? "|FLUSHIN" : "",
574 pointer[i+SLC_FLAGS]&SLC_FLUSHOUT ? "|FLUSHOUT" : "");
575 if (pointer[i+SLC_FLAGS]& ~(SLC_ACK|SLC_FLUSHIN|
576 SLC_FLUSHOUT| SLC_LEVELBITS))
577 fprintf(NetTrace, "(0x%x)", pointer[i+SLC_FLAGS]);
578 fprintf(NetTrace, " %d;", pointer[i+SLC_VALUE]);
579 if ((pointer[i+SLC_VALUE] == IAC) &&
580 (pointer[i+SLC_VALUE+1] == IAC))
583 for (; i < length; i++)
584 fprintf(NetTrace, " ?%d?", pointer[i]);
588 fprintf(NetTrace, "MODE ");
590 fprintf(NetTrace, "(no mode??\?)");
595 snprintf(tbuf, sizeof(tbuf),
597 pointer[2]&MODE_EDIT ? "|EDIT" : "",
598 pointer[2]&MODE_TRAPSIG ? "|TRAPSIG" : "",
599 pointer[2]&MODE_SOFT_TAB ? "|SOFT_TAB" : "",
600 pointer[2]&MODE_LIT_ECHO ? "|LIT_ECHO" : "",
601 pointer[2]&MODE_ACK ? "|ACK" : "");
602 fprintf(NetTrace, "%s", tbuf[1] ? &tbuf[1] : "0");
604 if (pointer[2]&~(MODE_MASK))
605 fprintf(NetTrace, " (0x%x)", pointer[2]);
606 for (i = 3; i < length; i++)
607 fprintf(NetTrace, " ?0x%x?", pointer[i]);
610 fprintf(NetTrace, "%d (unknown)", pointer[1]);
611 for (i = 2; i < length; i++)
612 fprintf(NetTrace, " %d", pointer[i]);
616 case TELOPT_STATUS: {
620 fprintf(NetTrace, "STATUS");
622 switch (pointer[1]) {
624 if (pointer[1] == TELQUAL_SEND)
625 fprintf(NetTrace, " SEND");
627 fprintf(NetTrace, " %d (unknown)", pointer[1]);
628 for (i = 2; i < length; i++)
629 fprintf(NetTrace, " ?%d?", pointer[i]);
632 if (--want_status_response < 0)
633 want_status_response = 0;
634 if (NetTrace == stdout)
635 fprintf(NetTrace, " IS\r\n");
637 fprintf(NetTrace, " IS\n");
639 for (i = 2; i < length; i++) {
641 case DO: cp = "DO"; goto common2;
642 case DONT: cp = "DONT"; goto common2;
643 case WILL: cp = "WILL"; goto common2;
644 case WONT: cp = "WONT"; goto common2;
647 if (TELOPT_OK((int)pointer[i]))
648 fprintf(NetTrace, " %s %s", cp, TELOPT(pointer[i]));
650 fprintf(NetTrace, " %s %d", cp, pointer[i]);
652 if (NetTrace == stdout)
653 fprintf(NetTrace, "\r\n");
655 fprintf(NetTrace, "\n");
659 fprintf(NetTrace, " SB ");
663 if (pointer[j] == SE) {
666 if (pointer[j+1] == SE)
671 pointer[k++] = pointer[j++];
673 printsub(0, &pointer[i], k - i);
675 fprintf(NetTrace, " SE");
680 if (NetTrace == stdout)
681 fprintf(NetTrace, "\r\n");
683 fprintf(NetTrace, "\n");
688 fprintf(NetTrace, " %d", pointer[i]);
697 case TELOPT_XDISPLOC:
698 fprintf(NetTrace, "X-DISPLAY-LOCATION ");
699 switch (pointer[1]) {
701 fprintf(NetTrace, "IS \"%.*s\"", length-2, (char *)pointer+2);
704 fprintf(NetTrace, "SEND");
707 fprintf(NetTrace, "- unknown qualifier %d (0x%x).",
708 pointer[1], pointer[1]);
712 case TELOPT_NEW_ENVIRON:
713 fprintf(NetTrace, "NEW-ENVIRON ");
716 case TELOPT_OLD_ENVIRON:
717 fprintf(NetTrace, "OLD-ENVIRON");
720 switch (pointer[1]) {
722 fprintf(NetTrace, "IS ");
725 fprintf(NetTrace, "SEND ");
728 fprintf(NetTrace, "INFO ");
732 for (i = 2; i < length; i++ ) {
733 switch (pointer[i]) {
736 /* case NEW_ENV_OVAR: */
737 if (pointer[0] == TELOPT_OLD_ENVIRON) {
738 fprintf(NetTrace, "\" VAR " + noquote);
740 #endif /* OLD_ENVIRON */
741 fprintf(NetTrace, "\" VALUE " + noquote);
747 /* case OLD_ENV_VALUE: */
748 if (pointer[0] == TELOPT_OLD_ENVIRON) {
749 fprintf(NetTrace, "\" VALUE " + noquote);
751 #endif /* OLD_ENVIRON */
752 fprintf(NetTrace, "\" VAR " + noquote);
757 fprintf(NetTrace, "\" ESC " + noquote);
762 fprintf(NetTrace, "\" USERVAR " + noquote);
767 if (isprint(pointer[i]) && pointer[i] != '"') {
772 putc(pointer[i], NetTrace);
774 fprintf(NetTrace, "\" %03o " + noquote,
789 if (TELOPT_OK(pointer[0]))
790 fprintf(NetTrace, "%s (unknown)", TELOPT(pointer[0]));
792 fprintf(NetTrace, "%d (unknown)", pointer[0]);
793 for (i = 1; i < length; i++)
794 fprintf(NetTrace, " %d", pointer[i]);
798 if (NetTrace == stdout)
799 fprintf(NetTrace, "\r\n");
801 fprintf(NetTrace, "\n");
803 if (NetTrace == stdout)
808 /* EmptyTerminal - called to make sure that the terminal buffer is empty.
809 * Note that we consider the buffer to run all the
810 * way to the kernel (thus the select).
820 if (tout >= FD_SETSIZE)
821 ExitString("fd too large", 1);
823 if (TTYBYTES() == 0) {
825 select(tout+1, 0, &outs, 0,
826 (struct timeval *) 0); /* wait for TTLOWAT */
831 select(tout+1, 0, &outs, 0,
832 (struct timeval *) 0); /* wait for TTLOWAT */
842 telrcv(); /* Process any incoming data */
844 } while (ring_full_count(&netiring)); /* While there is any */
849 EmptyTerminal(); /* Flush the path to the tty */
861 ExitString(char *string, int returnCode)
864 fwrite(string, 1, strlen(string), stderr);