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
36 static const char sccsid[] = "@(#)utilities.c 8.3 (Berkeley) 5/30/95";
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
45 #include <arpa/telnet.h>
46 #include <sys/types.h>
47 #include <sys/socket.h>
64 #include <libtelnet/auth.h>
67 #include <libtelnet/encrypt.h>
70 FILE *NetTrace = 0; /* Not in bss, since needs to stay */
76 * Upcase (in place) the argument.
80 upcase(char *argument)
84 while ((c = *argument) != 0) {
86 *argument = toupper(c);
95 * Compensate for differences in 4.2 and 4.3 systems.
99 SetSockOpt(int fd, int level, int option, int yesno)
101 return setsockopt(fd, level, option,
102 (char *)&yesno, sizeof yesno);
106 * The following are routines used to print out debugging information.
109 unsigned char NetTraceFile[256] = "(standard output)";
112 SetNetTrace(char *file)
114 if (NetTrace && NetTrace != stdout)
116 if (file && (strcmp(file, "-") != 0)) {
117 NetTrace = fopen(file, "w");
119 strcpy((char *)NetTraceFile, file);
122 fprintf(stderr, "Cannot open %s.\n", file);
125 strcpy((char *)NetTraceFile, "(standard output)");
129 Dump(char direction, unsigned char *buffer, int length)
131 # define BYTES_PER_LINE 32
132 # define min(x,y) ((x<y)? x:y)
133 unsigned char *pThis;
140 fprintf(NetTrace, "%c 0x%x\t", direction, offset);
143 buffer = buffer + min(length, BYTES_PER_LINE/2);
144 while (pThis < buffer) {
145 fprintf(NetTrace, "%c%.2x",
146 (((*pThis)&0xff) == 0xff) ? '*' : ' ',
150 length -= BYTES_PER_LINE/2;
151 offset += BYTES_PER_LINE/2;
153 buffer = buffer + min(length, BYTES_PER_LINE);
154 while (pThis < buffer) {
155 fprintf(NetTrace, "%.2x", (*pThis)&0xff);
158 length -= BYTES_PER_LINE;
159 offset += BYTES_PER_LINE;
161 if (NetTrace == stdout) {
162 fprintf(NetTrace, "\r\n");
164 fprintf(NetTrace, "\n");
170 /* find next unique line */
177 printoption(const char *direction, int cmd, int option)
182 if (TELCMD_OK(option))
183 fprintf(NetTrace, "%s IAC %s", direction, TELCMD(option));
185 fprintf(NetTrace, "%s IAC %d", direction, option);
188 fmt = (cmd == WILL) ? "WILL" : (cmd == WONT) ? "WONT" :
189 (cmd == DO) ? "DO" : (cmd == DONT) ? "DONT" : 0;
191 fprintf(NetTrace, "%s %s ", direction, fmt);
192 if (TELOPT_OK(option))
193 fprintf(NetTrace, "%s", TELOPT(option));
194 else if (option == TELOPT_EXOPL)
195 fprintf(NetTrace, "EXOPL");
197 fprintf(NetTrace, "%d", option);
199 fprintf(NetTrace, "%s %d %d", direction, cmd, option);
201 if (NetTrace == stdout) {
202 fprintf(NetTrace, "\r\n");
205 fprintf(NetTrace, "\n");
214 extern char will_wont_resp[], do_dont_resp[];
216 for (i = 0; i < 256; i++) {
217 if (do_dont_resp[i]) {
219 printf("resp DO_DONT %s: %d\n", TELOPT(i), do_dont_resp[i]);
220 else if (TELCMD_OK(i))
221 printf("resp DO_DONT %s: %d\n", TELCMD(i), do_dont_resp[i]);
223 printf("resp DO_DONT %d: %d\n", i,
225 if (my_want_state_is_do(i)) {
227 printf("want DO %s\n", TELOPT(i));
228 else if (TELCMD_OK(i))
229 printf("want DO %s\n", TELCMD(i));
231 printf("want DO %d\n", i);
234 printf("want DONT %s\n", TELOPT(i));
235 else if (TELCMD_OK(i))
236 printf("want DONT %s\n", TELCMD(i));
238 printf("want DONT %d\n", i);
241 if (my_state_is_do(i)) {
243 printf(" DO %s\n", TELOPT(i));
244 else if (TELCMD_OK(i))
245 printf(" DO %s\n", TELCMD(i));
247 printf(" DO %d\n", i);
250 if (will_wont_resp[i]) {
252 printf("resp WILL_WONT %s: %d\n", TELOPT(i), will_wont_resp[i]);
253 else if (TELCMD_OK(i))
254 printf("resp WILL_WONT %s: %d\n", TELCMD(i), will_wont_resp[i]);
256 printf("resp WILL_WONT %d: %d\n",
257 i, will_wont_resp[i]);
258 if (my_want_state_is_will(i)) {
260 printf("want WILL %s\n", TELOPT(i));
261 else if (TELCMD_OK(i))
262 printf("want WILL %s\n", TELCMD(i));
264 printf("want WILL %d\n", i);
267 printf("want WONT %s\n", TELOPT(i));
268 else if (TELCMD_OK(i))
269 printf("want WONT %s\n", TELCMD(i));
271 printf("want WONT %d\n", i);
274 if (my_state_is_will(i)) {
276 printf(" WILL %s\n", TELOPT(i));
277 else if (TELCMD_OK(i))
278 printf(" WILL %s\n", TELCMD(i));
280 printf(" WILL %d\n", i);
288 printsub(char direction, unsigned char *pointer, int length)
291 #ifdef AUTHENTICATION
294 extern int want_status_response;
296 if (showoptions || direction == 0 ||
297 (want_status_response && (pointer[0] == TELOPT_STATUS))) {
299 fprintf(NetTrace, "%s IAC SB ",
300 (direction == '<')? "RCVD":"SENT");
304 i = pointer[length-2];
305 j = pointer[length-1];
307 if (i != IAC || j != SE) {
308 fprintf(NetTrace, "(terminated by ");
310 fprintf(NetTrace, "%s ", TELOPT(i));
311 else if (TELCMD_OK(i))
312 fprintf(NetTrace, "%s ", TELCMD(i));
314 fprintf(NetTrace, "%d ", i);
316 fprintf(NetTrace, "%s", TELOPT(j));
317 else if (TELCMD_OK(j))
318 fprintf(NetTrace, "%s", TELCMD(j));
320 fprintf(NetTrace, "%d", j);
321 fprintf(NetTrace, ", not IAC SE!) ");
327 fprintf(NetTrace, "(Empty suboption??\?)");
328 if (NetTrace == stdout)
332 switch (pointer[0]) {
334 fprintf(NetTrace, "TERMINAL-TYPE ");
335 switch (pointer[1]) {
337 fprintf(NetTrace, "IS \"%.*s\"", length-2, (char *)pointer+2);
340 fprintf(NetTrace, "SEND");
344 "- unknown qualifier %d (0x%x).",
345 pointer[1], pointer[1]);
349 fprintf(NetTrace, "TERMINAL-SPEED");
351 fprintf(NetTrace, " (empty suboption??\?)");
354 switch (pointer[1]) {
356 fprintf(NetTrace, " IS ");
357 fprintf(NetTrace, "%.*s", length-2, (char *)pointer+2);
361 fprintf(NetTrace, " SEND");
363 fprintf(NetTrace, " %d (unknown)", pointer[1]);
364 for (i = 2; i < length; i++)
365 fprintf(NetTrace, " ?%d?", pointer[i]);
371 fprintf(NetTrace, "TOGGLE-FLOW-CONTROL");
373 fprintf(NetTrace, " (empty suboption??\?)");
376 switch (pointer[1]) {
378 fprintf(NetTrace, " OFF"); break;
380 fprintf(NetTrace, " ON"); break;
381 case LFLOW_RESTART_ANY:
382 fprintf(NetTrace, " RESTART-ANY"); break;
383 case LFLOW_RESTART_XON:
384 fprintf(NetTrace, " RESTART-XON"); break;
386 fprintf(NetTrace, " %d (unknown)", pointer[1]);
388 for (i = 2; i < length; i++)
389 fprintf(NetTrace, " ?%d?", pointer[i]);
393 fprintf(NetTrace, "NAWS");
395 fprintf(NetTrace, " (empty suboption??\?)");
399 fprintf(NetTrace, " ?%d?", pointer[1]);
402 fprintf(NetTrace, " %d %d (%d)",
403 pointer[1], pointer[2],
404 (int)((((unsigned int)pointer[1])<<8)|((unsigned int)pointer[2])));
406 fprintf(NetTrace, " ?%d?", pointer[3]);
409 fprintf(NetTrace, " %d %d (%d)",
410 pointer[3], pointer[4],
411 (int)((((unsigned int)pointer[3])<<8)|((unsigned int)pointer[4])));
412 for (i = 5; i < length; i++)
413 fprintf(NetTrace, " ?%d?", pointer[i]);
416 #ifdef AUTHENTICATION
417 case TELOPT_AUTHENTICATION:
418 fprintf(NetTrace, "AUTHENTICATION");
420 fprintf(NetTrace, " (empty suboption??\?)");
423 switch (pointer[1]) {
426 fprintf(NetTrace, " %s ", (pointer[1] == TELQUAL_IS) ?
428 if (AUTHTYPE_NAME_OK(pointer[2]))
429 fprintf(NetTrace, "%s ", AUTHTYPE_NAME(pointer[2]));
431 fprintf(NetTrace, "%d ", pointer[2]);
433 fprintf(NetTrace, "(partial suboption??\?)");
436 fprintf(NetTrace, "%s|%s",
437 ((pointer[3] & AUTH_WHO_MASK) == AUTH_WHO_CLIENT) ?
439 ((pointer[3] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) ?
440 "MUTUAL" : "ONE-WAY");
442 auth_printsub(&pointer[1], length - 1, buf, sizeof(buf));
443 fprintf(NetTrace, "%s", buf);
448 fprintf(NetTrace, " SEND ");
450 if (AUTHTYPE_NAME_OK(pointer[i]))
451 fprintf(NetTrace, "%s ", AUTHTYPE_NAME(pointer[i]));
453 fprintf(NetTrace, "%d ", pointer[i]);
455 fprintf(NetTrace, "(partial suboption??\?)");
458 fprintf(NetTrace, "%s|%s ",
459 ((pointer[i] & AUTH_WHO_MASK) == AUTH_WHO_CLIENT) ?
461 ((pointer[i] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) ?
462 "MUTUAL" : "ONE-WAY");
469 fprintf(NetTrace, " NAME \"");
471 putc(pointer[i++], NetTrace);
476 for (i = 2; i < length; i++)
477 fprintf(NetTrace, " ?%d?", pointer[i]);
485 fprintf(NetTrace, "ENCRYPT");
487 fprintf(NetTrace, " (empty suboption??\?)");
490 switch (pointer[1]) {
492 fprintf(NetTrace, " START");
496 fprintf(NetTrace, " END");
499 case ENCRYPT_REQSTART:
500 fprintf(NetTrace, " REQUEST-START");
504 fprintf(NetTrace, " REQUEST-END");
509 fprintf(NetTrace, " %s ", (pointer[1] == ENCRYPT_IS) ?
512 fprintf(NetTrace, " (partial suboption??\?)");
515 if (ENCTYPE_NAME_OK(pointer[2]))
516 fprintf(NetTrace, "%s ", ENCTYPE_NAME(pointer[2]));
518 fprintf(NetTrace, " %d (unknown)", pointer[2]);
520 encrypt_printsub(&pointer[1], length - 1, buf, sizeof(buf));
521 fprintf(NetTrace, "%s", buf);
524 case ENCRYPT_SUPPORT:
526 fprintf(NetTrace, " SUPPORT ");
528 if (ENCTYPE_NAME_OK(pointer[i]))
529 fprintf(NetTrace, "%s ", ENCTYPE_NAME(pointer[i]));
531 fprintf(NetTrace, "%d ", pointer[i]);
536 case ENCRYPT_ENC_KEYID:
537 fprintf(NetTrace, " ENC_KEYID ");
540 case ENCRYPT_DEC_KEYID:
541 fprintf(NetTrace, " DEC_KEYID ");
545 fprintf(NetTrace, " %d (unknown)", pointer[1]);
547 for (i = 2; i < length; i++)
548 fprintf(NetTrace, " %d", pointer[i]);
552 #endif /* ENCRYPTION */
554 case TELOPT_LINEMODE:
555 fprintf(NetTrace, "LINEMODE ");
557 fprintf(NetTrace, " (empty suboption??\?)");
560 switch (pointer[1]) {
562 fprintf(NetTrace, "WILL ");
565 fprintf(NetTrace, "WONT ");
568 fprintf(NetTrace, "DO ");
571 fprintf(NetTrace, "DONT ");
574 fprintf(NetTrace, "(no option??\?)");
577 switch (pointer[2]) {
579 fprintf(NetTrace, "Forward Mask");
580 for (i = 3; i < length; i++)
581 fprintf(NetTrace, " %x", pointer[i]);
584 fprintf(NetTrace, "%d (unknown)", pointer[2]);
585 for (i = 3; i < length; i++)
586 fprintf(NetTrace, " %d", pointer[i]);
592 fprintf(NetTrace, "SLC");
593 for (i = 2; i < length - 2; i += 3) {
594 if (SLC_NAME_OK(pointer[i+SLC_FUNC]))
595 fprintf(NetTrace, " %s", SLC_NAME(pointer[i+SLC_FUNC]));
597 fprintf(NetTrace, " %d", pointer[i+SLC_FUNC]);
598 switch (pointer[i+SLC_FLAGS]&SLC_LEVELBITS) {
600 fprintf(NetTrace, " NOSUPPORT"); break;
602 fprintf(NetTrace, " CANTCHANGE"); break;
604 fprintf(NetTrace, " VARIABLE"); break;
606 fprintf(NetTrace, " DEFAULT"); break;
608 fprintf(NetTrace, "%s%s%s",
609 pointer[i+SLC_FLAGS]&SLC_ACK ? "|ACK" : "",
610 pointer[i+SLC_FLAGS]&SLC_FLUSHIN ? "|FLUSHIN" : "",
611 pointer[i+SLC_FLAGS]&SLC_FLUSHOUT ? "|FLUSHOUT" : "");
612 if (pointer[i+SLC_FLAGS]& ~(SLC_ACK|SLC_FLUSHIN|
613 SLC_FLUSHOUT| SLC_LEVELBITS))
614 fprintf(NetTrace, "(0x%x)", pointer[i+SLC_FLAGS]);
615 fprintf(NetTrace, " %d;", pointer[i+SLC_VALUE]);
616 if ((pointer[i+SLC_VALUE] == IAC) &&
617 (pointer[i+SLC_VALUE+1] == IAC))
620 for (; i < length; i++)
621 fprintf(NetTrace, " ?%d?", pointer[i]);
625 fprintf(NetTrace, "MODE ");
627 fprintf(NetTrace, "(no mode??\?)");
632 sprintf(tbuf, "%s%s%s%s%s",
633 pointer[2]&MODE_EDIT ? "|EDIT" : "",
634 pointer[2]&MODE_TRAPSIG ? "|TRAPSIG" : "",
635 pointer[2]&MODE_SOFT_TAB ? "|SOFT_TAB" : "",
636 pointer[2]&MODE_LIT_ECHO ? "|LIT_ECHO" : "",
637 pointer[2]&MODE_ACK ? "|ACK" : "");
638 fprintf(NetTrace, "%s", tbuf[1] ? &tbuf[1] : "0");
640 if (pointer[2]&~(MODE_MASK))
641 fprintf(NetTrace, " (0x%x)", pointer[2]);
642 for (i = 3; i < length; i++)
643 fprintf(NetTrace, " ?0x%x?", pointer[i]);
646 fprintf(NetTrace, "%d (unknown)", pointer[1]);
647 for (i = 2; i < length; i++)
648 fprintf(NetTrace, " %d", pointer[i]);
652 case TELOPT_STATUS: {
656 fprintf(NetTrace, "STATUS");
658 switch (pointer[1]) {
660 if (pointer[1] == TELQUAL_SEND)
661 fprintf(NetTrace, " SEND");
663 fprintf(NetTrace, " %d (unknown)", pointer[1]);
664 for (i = 2; i < length; i++)
665 fprintf(NetTrace, " ?%d?", pointer[i]);
668 if (--want_status_response < 0)
669 want_status_response = 0;
670 if (NetTrace == stdout)
671 fprintf(NetTrace, " IS\r\n");
673 fprintf(NetTrace, " IS\n");
675 for (i = 2; i < length; i++) {
677 case DO: cp = "DO"; goto common2;
678 case DONT: cp = "DONT"; goto common2;
679 case WILL: cp = "WILL"; goto common2;
680 case WONT: cp = "WONT"; goto common2;
683 if (TELOPT_OK((int)pointer[i]))
684 fprintf(NetTrace, " %s %s", cp, TELOPT(pointer[i]));
686 fprintf(NetTrace, " %s %d", cp, pointer[i]);
688 if (NetTrace == stdout)
689 fprintf(NetTrace, "\r\n");
691 fprintf(NetTrace, "\n");
695 fprintf(NetTrace, " SB ");
699 if (pointer[j] == SE) {
702 if (pointer[j+1] == SE)
707 pointer[k++] = pointer[j++];
709 printsub(0, &pointer[i], k - i);
711 fprintf(NetTrace, " SE");
716 if (NetTrace == stdout)
717 fprintf(NetTrace, "\r\n");
719 fprintf(NetTrace, "\n");
724 fprintf(NetTrace, " %d", pointer[i]);
733 case TELOPT_XDISPLOC:
734 fprintf(NetTrace, "X-DISPLAY-LOCATION ");
735 switch (pointer[1]) {
737 fprintf(NetTrace, "IS \"%.*s\"", length-2, (char *)pointer+2);
740 fprintf(NetTrace, "SEND");
743 fprintf(NetTrace, "- unknown qualifier %d (0x%x).",
744 pointer[1], pointer[1]);
748 case TELOPT_NEW_ENVIRON:
749 fprintf(NetTrace, "NEW-ENVIRON ");
752 case TELOPT_OLD_ENVIRON:
753 fprintf(NetTrace, "OLD-ENVIRON");
756 switch (pointer[1]) {
758 fprintf(NetTrace, "IS ");
761 fprintf(NetTrace, "SEND ");
764 fprintf(NetTrace, "INFO ");
768 #if defined(ENV_HACK) && defined(OLD_ENVIRON)
769 extern int old_env_var, old_env_value;
771 for (i = 2; i < length; i++ ) {
772 switch (pointer[i]) {
775 /* case NEW_ENV_OVAR: */
776 if (pointer[0] == TELOPT_OLD_ENVIRON) {
778 if (old_env_var == OLD_ENV_VALUE)
779 fprintf(NetTrace, "\" (VALUE) " + noquote);
782 fprintf(NetTrace, "\" VAR " + noquote);
784 #endif /* OLD_ENVIRON */
785 fprintf(NetTrace, "%s", "\" VALUE " + noquote);
791 /* case OLD_ENV_VALUE: */
792 if (pointer[0] == TELOPT_OLD_ENVIRON) {
794 if (old_env_value == OLD_ENV_VAR)
795 fprintf(NetTrace, "\" (VAR) " + noquote);
798 fprintf(NetTrace, "\" VALUE " + noquote);
800 #endif /* OLD_ENVIRON */
801 fprintf(NetTrace, "%s", "\" VAR " + noquote);
806 fprintf(NetTrace, "%s", "\" ESC " + noquote);
811 fprintf(NetTrace, "%s", "\" USERVAR " + noquote);
816 if (isprint(pointer[i]) && pointer[i] != '"') {
821 putc(pointer[i], NetTrace);
823 fprintf(NetTrace, "\" %03o " + noquote,
838 if (TELOPT_OK(pointer[0]))
839 fprintf(NetTrace, "%s (unknown)", TELOPT(pointer[0]));
841 fprintf(NetTrace, "%d (unknown)", pointer[0]);
842 for (i = 1; i < length; i++)
843 fprintf(NetTrace, " %d", pointer[i]);
847 if (NetTrace == stdout)
848 fprintf(NetTrace, "\r\n");
850 fprintf(NetTrace, "\n");
852 if (NetTrace == stdout)
857 /* EmptyTerminal - called to make sure that the terminal buffer is empty.
858 * Note that we consider the buffer to run all the
859 * way to the kernel (thus the select).
869 if (TTYBYTES() == 0) {
871 (void) select(tout+1, (fd_set *) 0, &o, (fd_set *) 0,
872 (struct timeval *) 0); /* wait for TTLOWAT */
877 (void) select(tout+1, (fd_set *) 0, &o, (fd_set *) 0,
878 (struct timeval *) 0); /* wait for TTLOWAT */
888 (void)telrcv(); /* Process any incoming data */
890 } while (ring_full_count(&netiring)); /* While there is any */
895 EmptyTerminal(); /* Flush the path to the tty */
907 ExitString(const char *string, int returnCode)
910 fwrite(string, 1, strlen(string), stderr);