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 10587 2001-08-29 00:45:23Z assar $");
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);
252 printsub(int direction, unsigned char *pointer, int length)
255 unsigned char buf[512];
257 if (showoptions || direction == 0 ||
258 (want_status_response && (pointer[0] == TELOPT_STATUS))) {
260 fprintf(NetTrace, "%s IAC SB ",
261 (direction == '<')? "RCVD":"SENT");
265 i = pointer[length-2];
266 j = pointer[length-1];
268 if (i != IAC || j != SE) {
269 fprintf(NetTrace, "(terminated by ");
271 fprintf(NetTrace, "%s ", TELOPT(i));
272 else if (TELCMD_OK(i))
273 fprintf(NetTrace, "%s ", TELCMD(i));
275 fprintf(NetTrace, "%d ", i);
277 fprintf(NetTrace, "%s", TELOPT(j));
278 else if (TELCMD_OK(j))
279 fprintf(NetTrace, "%s", TELCMD(j));
281 fprintf(NetTrace, "%d", j);
282 fprintf(NetTrace, ", not IAC SE!) ");
288 fprintf(NetTrace, "(Empty suboption??\?)");
289 if (NetTrace == stdout)
293 switch (pointer[0]) {
295 fprintf(NetTrace, "TERMINAL-TYPE ");
296 switch (pointer[1]) {
298 fprintf(NetTrace, "IS \"%.*s\"", length-2, (char *)pointer+2);
301 fprintf(NetTrace, "SEND");
305 "- unknown qualifier %d (0x%x).",
306 pointer[1], pointer[1]);
310 fprintf(NetTrace, "TERMINAL-SPEED");
312 fprintf(NetTrace, " (empty suboption??\?)");
315 switch (pointer[1]) {
317 fprintf(NetTrace, " IS ");
318 fprintf(NetTrace, "%.*s", length-2, (char *)pointer+2);
322 fprintf(NetTrace, " SEND");
324 fprintf(NetTrace, " %d (unknown)", pointer[1]);
325 for (i = 2; i < length; i++)
326 fprintf(NetTrace, " ?%d?", pointer[i]);
332 fprintf(NetTrace, "TOGGLE-FLOW-CONTROL");
334 fprintf(NetTrace, " (empty suboption??\?)");
337 switch (pointer[1]) {
339 fprintf(NetTrace, " OFF"); break;
341 fprintf(NetTrace, " ON"); break;
342 case LFLOW_RESTART_ANY:
343 fprintf(NetTrace, " RESTART-ANY"); break;
344 case LFLOW_RESTART_XON:
345 fprintf(NetTrace, " RESTART-XON"); break;
347 fprintf(NetTrace, " %d (unknown)", pointer[1]);
349 for (i = 2; i < length; i++)
350 fprintf(NetTrace, " ?%d?", pointer[i]);
354 fprintf(NetTrace, "NAWS");
356 fprintf(NetTrace, " (empty suboption??\?)");
360 fprintf(NetTrace, " ?%d?", pointer[1]);
363 fprintf(NetTrace, " %d %d (%d)",
364 pointer[1], pointer[2],
365 (int)((((unsigned int)pointer[1])<<8)|((unsigned int)pointer[2])));
367 fprintf(NetTrace, " ?%d?", pointer[3]);
370 fprintf(NetTrace, " %d %d (%d)",
371 pointer[3], pointer[4],
372 (int)((((unsigned int)pointer[3])<<8)|((unsigned int)pointer[4])));
373 for (i = 5; i < length; i++)
374 fprintf(NetTrace, " ?%d?", pointer[i]);
377 #if defined(AUTHENTICATION)
378 case TELOPT_AUTHENTICATION:
379 fprintf(NetTrace, "AUTHENTICATION");
381 fprintf(NetTrace, " (empty suboption??\?)");
384 switch (pointer[1]) {
387 fprintf(NetTrace, " %s ", (pointer[1] == TELQUAL_IS) ?
389 if (AUTHTYPE_NAME_OK(pointer[2]))
390 fprintf(NetTrace, "%s ", AUTHTYPE_NAME(pointer[2]));
392 fprintf(NetTrace, "%d ", pointer[2]);
394 fprintf(NetTrace, "(partial suboption??\?)");
397 fprintf(NetTrace, "%s|%s",
398 ((pointer[3] & AUTH_WHO_MASK) == AUTH_WHO_CLIENT) ?
400 ((pointer[3] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) ?
401 "MUTUAL" : "ONE-WAY");
403 auth_printsub(&pointer[1], length - 1, buf, sizeof(buf));
404 fprintf(NetTrace, "%s", buf);
409 fprintf(NetTrace, " SEND ");
411 if (AUTHTYPE_NAME_OK(pointer[i]))
412 fprintf(NetTrace, "%s ", AUTHTYPE_NAME(pointer[i]));
414 fprintf(NetTrace, "%d ", pointer[i]);
416 fprintf(NetTrace, "(partial suboption??\?)");
419 fprintf(NetTrace, "%s|%s ",
420 ((pointer[i] & AUTH_WHO_MASK) == AUTH_WHO_CLIENT) ?
422 ((pointer[i] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) ?
423 "MUTUAL" : "ONE-WAY");
430 fprintf(NetTrace, " NAME \"");
432 putc(pointer[i++], NetTrace);
437 for (i = 2; i < length; i++)
438 fprintf(NetTrace, " ?%d?", pointer[i]);
444 #if defined(ENCRYPTION)
446 fprintf(NetTrace, "ENCRYPT");
448 fprintf(NetTrace, " (empty suboption?)");
451 switch (pointer[1]) {
453 fprintf(NetTrace, " START");
457 fprintf(NetTrace, " END");
460 case ENCRYPT_REQSTART:
461 fprintf(NetTrace, " REQUEST-START");
465 fprintf(NetTrace, " REQUEST-END");
470 fprintf(NetTrace, " %s ", (pointer[1] == ENCRYPT_IS) ?
473 fprintf(NetTrace, " (partial suboption?)");
476 if (ENCTYPE_NAME_OK(pointer[2]))
477 fprintf(NetTrace, "%s ", ENCTYPE_NAME(pointer[2]));
479 fprintf(NetTrace, " %d (unknown)", pointer[2]);
481 encrypt_printsub(&pointer[1], length - 1, buf, sizeof(buf));
482 fprintf(NetTrace, "%s", buf);
485 case ENCRYPT_SUPPORT:
487 fprintf(NetTrace, " SUPPORT ");
489 if (ENCTYPE_NAME_OK(pointer[i]))
490 fprintf(NetTrace, "%s ", ENCTYPE_NAME(pointer[i]));
492 fprintf(NetTrace, "%d ", pointer[i]);
497 case ENCRYPT_ENC_KEYID:
498 fprintf(NetTrace, " ENC_KEYID ");
501 case ENCRYPT_DEC_KEYID:
502 fprintf(NetTrace, " DEC_KEYID ");
506 fprintf(NetTrace, " %d (unknown)", pointer[1]);
508 for (i = 2; i < length; i++)
509 fprintf(NetTrace, " %d", pointer[i]);
515 case TELOPT_LINEMODE:
516 fprintf(NetTrace, "LINEMODE ");
518 fprintf(NetTrace, " (empty suboption??\?)");
521 switch (pointer[1]) {
523 fprintf(NetTrace, "WILL ");
526 fprintf(NetTrace, "WONT ");
529 fprintf(NetTrace, "DO ");
532 fprintf(NetTrace, "DONT ");
535 fprintf(NetTrace, "(no option??\?)");
538 switch (pointer[2]) {
540 fprintf(NetTrace, "Forward Mask");
541 for (i = 3; i < length; i++)
542 fprintf(NetTrace, " %x", pointer[i]);
545 fprintf(NetTrace, "%d (unknown)", pointer[2]);
546 for (i = 3; i < length; i++)
547 fprintf(NetTrace, " %d", pointer[i]);
553 fprintf(NetTrace, "SLC");
554 for (i = 2; i < length - 2; i += 3) {
555 if (SLC_NAME_OK(pointer[i+SLC_FUNC]))
556 fprintf(NetTrace, " %s", SLC_NAME(pointer[i+SLC_FUNC]));
558 fprintf(NetTrace, " %d", pointer[i+SLC_FUNC]);
559 switch (pointer[i+SLC_FLAGS]&SLC_LEVELBITS) {
561 fprintf(NetTrace, " NOSUPPORT"); break;
563 fprintf(NetTrace, " CANTCHANGE"); break;
565 fprintf(NetTrace, " VARIABLE"); break;
567 fprintf(NetTrace, " DEFAULT"); break;
569 fprintf(NetTrace, "%s%s%s",
570 pointer[i+SLC_FLAGS]&SLC_ACK ? "|ACK" : "",
571 pointer[i+SLC_FLAGS]&SLC_FLUSHIN ? "|FLUSHIN" : "",
572 pointer[i+SLC_FLAGS]&SLC_FLUSHOUT ? "|FLUSHOUT" : "");
573 if (pointer[i+SLC_FLAGS]& ~(SLC_ACK|SLC_FLUSHIN|
574 SLC_FLUSHOUT| SLC_LEVELBITS))
575 fprintf(NetTrace, "(0x%x)", pointer[i+SLC_FLAGS]);
576 fprintf(NetTrace, " %d;", pointer[i+SLC_VALUE]);
577 if ((pointer[i+SLC_VALUE] == IAC) &&
578 (pointer[i+SLC_VALUE+1] == IAC))
581 for (; i < length; i++)
582 fprintf(NetTrace, " ?%d?", pointer[i]);
586 fprintf(NetTrace, "MODE ");
588 fprintf(NetTrace, "(no mode??\?)");
593 snprintf(tbuf, sizeof(tbuf),
595 pointer[2]&MODE_EDIT ? "|EDIT" : "",
596 pointer[2]&MODE_TRAPSIG ? "|TRAPSIG" : "",
597 pointer[2]&MODE_SOFT_TAB ? "|SOFT_TAB" : "",
598 pointer[2]&MODE_LIT_ECHO ? "|LIT_ECHO" : "",
599 pointer[2]&MODE_ACK ? "|ACK" : "");
600 fprintf(NetTrace, "%s", tbuf[1] ? &tbuf[1] : "0");
602 if (pointer[2]&~(MODE_MASK))
603 fprintf(NetTrace, " (0x%x)", pointer[2]);
604 for (i = 3; i < length; i++)
605 fprintf(NetTrace, " ?0x%x?", pointer[i]);
608 fprintf(NetTrace, "%d (unknown)", pointer[1]);
609 for (i = 2; i < length; i++)
610 fprintf(NetTrace, " %d", pointer[i]);
614 case TELOPT_STATUS: {
618 fprintf(NetTrace, "STATUS");
620 switch (pointer[1]) {
622 if (pointer[1] == TELQUAL_SEND)
623 fprintf(NetTrace, " SEND");
625 fprintf(NetTrace, " %d (unknown)", pointer[1]);
626 for (i = 2; i < length; i++)
627 fprintf(NetTrace, " ?%d?", pointer[i]);
630 if (--want_status_response < 0)
631 want_status_response = 0;
632 if (NetTrace == stdout)
633 fprintf(NetTrace, " IS\r\n");
635 fprintf(NetTrace, " IS\n");
637 for (i = 2; i < length; i++) {
639 case DO: cp = "DO"; goto common2;
640 case DONT: cp = "DONT"; goto common2;
641 case WILL: cp = "WILL"; goto common2;
642 case WONT: cp = "WONT"; goto common2;
645 if (TELOPT_OK((int)pointer[i]))
646 fprintf(NetTrace, " %s %s", cp, TELOPT(pointer[i]));
648 fprintf(NetTrace, " %s %d", cp, pointer[i]);
650 if (NetTrace == stdout)
651 fprintf(NetTrace, "\r\n");
653 fprintf(NetTrace, "\n");
657 fprintf(NetTrace, " SB ");
661 if (pointer[j] == SE) {
664 if (pointer[j+1] == SE)
669 pointer[k++] = pointer[j++];
671 printsub(0, &pointer[i], k - i);
673 fprintf(NetTrace, " SE");
678 if (NetTrace == stdout)
679 fprintf(NetTrace, "\r\n");
681 fprintf(NetTrace, "\n");
686 fprintf(NetTrace, " %d", pointer[i]);
695 case TELOPT_XDISPLOC:
696 fprintf(NetTrace, "X-DISPLAY-LOCATION ");
697 switch (pointer[1]) {
699 fprintf(NetTrace, "IS \"%.*s\"", length-2, (char *)pointer+2);
702 fprintf(NetTrace, "SEND");
705 fprintf(NetTrace, "- unknown qualifier %d (0x%x).",
706 pointer[1], pointer[1]);
710 case TELOPT_NEW_ENVIRON:
711 fprintf(NetTrace, "NEW-ENVIRON ");
714 case TELOPT_OLD_ENVIRON:
715 fprintf(NetTrace, "OLD-ENVIRON");
718 switch (pointer[1]) {
720 fprintf(NetTrace, "IS ");
723 fprintf(NetTrace, "SEND ");
726 fprintf(NetTrace, "INFO ");
730 for (i = 2; i < length; i++ ) {
731 switch (pointer[i]) {
734 /* case NEW_ENV_OVAR: */
735 if (pointer[0] == TELOPT_OLD_ENVIRON) {
736 fprintf(NetTrace, "\" VAR " + noquote);
738 #endif /* OLD_ENVIRON */
739 fprintf(NetTrace, "\" VALUE " + noquote);
745 /* case OLD_ENV_VALUE: */
746 if (pointer[0] == TELOPT_OLD_ENVIRON) {
747 fprintf(NetTrace, "\" VALUE " + noquote);
749 #endif /* OLD_ENVIRON */
750 fprintf(NetTrace, "\" VAR " + noquote);
755 fprintf(NetTrace, "\" ESC " + noquote);
760 fprintf(NetTrace, "\" USERVAR " + noquote);
765 if (isprint(pointer[i]) && pointer[i] != '"') {
770 putc(pointer[i], NetTrace);
772 fprintf(NetTrace, "\" %03o " + noquote,
787 if (TELOPT_OK(pointer[0]))
788 fprintf(NetTrace, "%s (unknown)", TELOPT(pointer[0]));
790 fprintf(NetTrace, "%d (unknown)", pointer[0]);
791 for (i = 1; i < length; i++)
792 fprintf(NetTrace, " %d", pointer[i]);
796 if (NetTrace == stdout)
797 fprintf(NetTrace, "\r\n");
799 fprintf(NetTrace, "\n");
801 if (NetTrace == stdout)
806 /* EmptyTerminal - called to make sure that the terminal buffer is empty.
807 * Note that we consider the buffer to run all the
808 * way to the kernel (thus the select).
818 if (tout >= FD_SETSIZE)
819 ExitString("fd too large", 1);
821 if (TTYBYTES() == 0) {
823 select(tout+1, 0, &outs, 0,
824 (struct timeval *) 0); /* wait for TTLOWAT */
829 select(tout+1, 0, &outs, 0,
830 (struct timeval *) 0); /* wait for TTLOWAT */
840 telrcv(); /* Process any incoming data */
842 } while (ring_full_count(&netiring)); /* While there is any */
847 EmptyTerminal(); /* Flush the path to the tty */
859 ExitString(char *string, int returnCode)
862 fwrite(string, 1, strlen(string), stderr);