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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 static const char sccsid[] = "@(#)utilities.c 8.3 (Berkeley) 5/30/95";
39 #include <arpa/telnet.h>
40 #include <sys/types.h>
41 #include <sys/socket.h>
58 #include <libtelnet/auth.h>
61 #include <libtelnet/encrypt.h>
64 FILE *NetTrace = 0; /* Not in bss, since needs to stay */
70 * Upcase (in place) the argument.
74 upcase(char *argument)
78 while ((c = *argument) != 0) {
80 *argument = toupper(c);
89 * Compensate for differences in 4.2 and 4.3 systems.
93 SetSockOpt(int fd, int level, int option, int yesno)
95 return setsockopt(fd, level, option,
96 (char *)&yesno, sizeof yesno);
100 * The following are routines used to print out debugging information.
103 unsigned char NetTraceFile[256] = "(standard output)";
106 SetNetTrace(char *file)
108 if (NetTrace && NetTrace != stdout)
110 if (file && (strcmp(file, "-") != 0)) {
111 NetTrace = fopen(file, "w");
113 strcpy((char *)NetTraceFile, file);
116 fprintf(stderr, "Cannot open %s.\n", file);
119 strcpy((char *)NetTraceFile, "(standard output)");
123 Dump(char direction, unsigned char *buffer, int length)
125 # define BYTES_PER_LINE 32
126 # define min(x,y) ((x<y)? x:y)
127 unsigned char *pThis;
134 fprintf(NetTrace, "%c 0x%x\t", direction, offset);
137 buffer = buffer + min(length, BYTES_PER_LINE/2);
138 while (pThis < buffer) {
139 fprintf(NetTrace, "%c%.2x",
140 (((*pThis)&0xff) == 0xff) ? '*' : ' ',
144 length -= BYTES_PER_LINE/2;
145 offset += BYTES_PER_LINE/2;
147 buffer = buffer + min(length, BYTES_PER_LINE);
148 while (pThis < buffer) {
149 fprintf(NetTrace, "%.2x", (*pThis)&0xff);
152 length -= BYTES_PER_LINE;
153 offset += BYTES_PER_LINE;
155 if (NetTrace == stdout) {
156 fprintf(NetTrace, "\r\n");
158 fprintf(NetTrace, "\n");
164 /* find next unique line */
171 printoption(const char *direction, int cmd, int option)
176 if (TELCMD_OK(option))
177 fprintf(NetTrace, "%s IAC %s", direction, TELCMD(option));
179 fprintf(NetTrace, "%s IAC %d", direction, option);
182 fmt = (cmd == WILL) ? "WILL" : (cmd == WONT) ? "WONT" :
183 (cmd == DO) ? "DO" : (cmd == DONT) ? "DONT" : 0;
185 fprintf(NetTrace, "%s %s ", direction, fmt);
186 if (TELOPT_OK(option))
187 fprintf(NetTrace, "%s", TELOPT(option));
188 else if (option == TELOPT_EXOPL)
189 fprintf(NetTrace, "EXOPL");
191 fprintf(NetTrace, "%d", option);
193 fprintf(NetTrace, "%s %d %d", direction, cmd, option);
195 if (NetTrace == stdout) {
196 fprintf(NetTrace, "\r\n");
199 fprintf(NetTrace, "\n");
208 extern char will_wont_resp[], do_dont_resp[];
210 for (i = 0; i < 256; i++) {
211 if (do_dont_resp[i]) {
213 printf("resp DO_DONT %s: %d\n", TELOPT(i), do_dont_resp[i]);
214 else if (TELCMD_OK(i))
215 printf("resp DO_DONT %s: %d\n", TELCMD(i), do_dont_resp[i]);
217 printf("resp DO_DONT %d: %d\n", i,
219 if (my_want_state_is_do(i)) {
221 printf("want DO %s\n", TELOPT(i));
222 else if (TELCMD_OK(i))
223 printf("want DO %s\n", TELCMD(i));
225 printf("want DO %d\n", i);
228 printf("want DONT %s\n", TELOPT(i));
229 else if (TELCMD_OK(i))
230 printf("want DONT %s\n", TELCMD(i));
232 printf("want DONT %d\n", i);
235 if (my_state_is_do(i)) {
237 printf(" DO %s\n", TELOPT(i));
238 else if (TELCMD_OK(i))
239 printf(" DO %s\n", TELCMD(i));
241 printf(" DO %d\n", i);
244 if (will_wont_resp[i]) {
246 printf("resp WILL_WONT %s: %d\n", TELOPT(i), will_wont_resp[i]);
247 else if (TELCMD_OK(i))
248 printf("resp WILL_WONT %s: %d\n", TELCMD(i), will_wont_resp[i]);
250 printf("resp WILL_WONT %d: %d\n",
251 i, will_wont_resp[i]);
252 if (my_want_state_is_will(i)) {
254 printf("want WILL %s\n", TELOPT(i));
255 else if (TELCMD_OK(i))
256 printf("want WILL %s\n", TELCMD(i));
258 printf("want WILL %d\n", i);
261 printf("want WONT %s\n", TELOPT(i));
262 else if (TELCMD_OK(i))
263 printf("want WONT %s\n", TELCMD(i));
265 printf("want WONT %d\n", i);
268 if (my_state_is_will(i)) {
270 printf(" WILL %s\n", TELOPT(i));
271 else if (TELCMD_OK(i))
272 printf(" WILL %s\n", TELCMD(i));
274 printf(" WILL %d\n", i);
282 printsub(char direction, unsigned char *pointer, int length)
285 #ifdef AUTHENTICATION
288 extern int want_status_response;
290 if (showoptions || direction == 0 ||
291 (want_status_response && (pointer[0] == TELOPT_STATUS))) {
293 fprintf(NetTrace, "%s IAC SB ",
294 (direction == '<')? "RCVD":"SENT");
298 i = pointer[length-2];
299 j = pointer[length-1];
301 if (i != IAC || j != SE) {
302 fprintf(NetTrace, "(terminated by ");
304 fprintf(NetTrace, "%s ", TELOPT(i));
305 else if (TELCMD_OK(i))
306 fprintf(NetTrace, "%s ", TELCMD(i));
308 fprintf(NetTrace, "%d ", i);
310 fprintf(NetTrace, "%s", TELOPT(j));
311 else if (TELCMD_OK(j))
312 fprintf(NetTrace, "%s", TELCMD(j));
314 fprintf(NetTrace, "%d", j);
315 fprintf(NetTrace, ", not IAC SE!) ");
321 fprintf(NetTrace, "(Empty suboption??\?)");
322 if (NetTrace == stdout)
326 switch (pointer[0]) {
328 fprintf(NetTrace, "TERMINAL-TYPE ");
329 switch (pointer[1]) {
331 fprintf(NetTrace, "IS \"%.*s\"", length-2, (char *)pointer+2);
334 fprintf(NetTrace, "SEND");
338 "- unknown qualifier %d (0x%x).",
339 pointer[1], pointer[1]);
343 fprintf(NetTrace, "TERMINAL-SPEED");
345 fprintf(NetTrace, " (empty suboption??\?)");
348 switch (pointer[1]) {
350 fprintf(NetTrace, " IS ");
351 fprintf(NetTrace, "%.*s", length-2, (char *)pointer+2);
355 fprintf(NetTrace, " SEND");
357 fprintf(NetTrace, " %d (unknown)", pointer[1]);
358 for (i = 2; i < length; i++)
359 fprintf(NetTrace, " ?%d?", pointer[i]);
365 fprintf(NetTrace, "TOGGLE-FLOW-CONTROL");
367 fprintf(NetTrace, " (empty suboption??\?)");
370 switch (pointer[1]) {
372 fprintf(NetTrace, " OFF"); break;
374 fprintf(NetTrace, " ON"); break;
375 case LFLOW_RESTART_ANY:
376 fprintf(NetTrace, " RESTART-ANY"); break;
377 case LFLOW_RESTART_XON:
378 fprintf(NetTrace, " RESTART-XON"); break;
380 fprintf(NetTrace, " %d (unknown)", pointer[1]);
382 for (i = 2; i < length; i++)
383 fprintf(NetTrace, " ?%d?", pointer[i]);
387 fprintf(NetTrace, "NAWS");
389 fprintf(NetTrace, " (empty suboption??\?)");
393 fprintf(NetTrace, " ?%d?", pointer[1]);
396 fprintf(NetTrace, " %d %d (%d)",
397 pointer[1], pointer[2],
398 (int)((((unsigned int)pointer[1])<<8)|((unsigned int)pointer[2])));
400 fprintf(NetTrace, " ?%d?", pointer[3]);
403 fprintf(NetTrace, " %d %d (%d)",
404 pointer[3], pointer[4],
405 (int)((((unsigned int)pointer[3])<<8)|((unsigned int)pointer[4])));
406 for (i = 5; i < length; i++)
407 fprintf(NetTrace, " ?%d?", pointer[i]);
410 #ifdef AUTHENTICATION
411 case TELOPT_AUTHENTICATION:
412 fprintf(NetTrace, "AUTHENTICATION");
414 fprintf(NetTrace, " (empty suboption??\?)");
417 switch (pointer[1]) {
420 fprintf(NetTrace, " %s ", (pointer[1] == TELQUAL_IS) ?
422 if (AUTHTYPE_NAME_OK(pointer[2]))
423 fprintf(NetTrace, "%s ", AUTHTYPE_NAME(pointer[2]));
425 fprintf(NetTrace, "%d ", pointer[2]);
427 fprintf(NetTrace, "(partial suboption??\?)");
430 fprintf(NetTrace, "%s|%s",
431 ((pointer[3] & AUTH_WHO_MASK) == AUTH_WHO_CLIENT) ?
433 ((pointer[3] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) ?
434 "MUTUAL" : "ONE-WAY");
436 auth_printsub(&pointer[1], length - 1, buf, sizeof(buf));
437 fprintf(NetTrace, "%s", buf);
442 fprintf(NetTrace, " SEND ");
444 if (AUTHTYPE_NAME_OK(pointer[i]))
445 fprintf(NetTrace, "%s ", AUTHTYPE_NAME(pointer[i]));
447 fprintf(NetTrace, "%d ", pointer[i]);
449 fprintf(NetTrace, "(partial suboption??\?)");
452 fprintf(NetTrace, "%s|%s ",
453 ((pointer[i] & AUTH_WHO_MASK) == AUTH_WHO_CLIENT) ?
455 ((pointer[i] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) ?
456 "MUTUAL" : "ONE-WAY");
463 fprintf(NetTrace, " NAME \"");
465 putc(pointer[i++], NetTrace);
470 for (i = 2; i < length; i++)
471 fprintf(NetTrace, " ?%d?", pointer[i]);
479 fprintf(NetTrace, "ENCRYPT");
481 fprintf(NetTrace, " (empty suboption??\?)");
484 switch (pointer[1]) {
486 fprintf(NetTrace, " START");
490 fprintf(NetTrace, " END");
493 case ENCRYPT_REQSTART:
494 fprintf(NetTrace, " REQUEST-START");
498 fprintf(NetTrace, " REQUEST-END");
503 fprintf(NetTrace, " %s ", (pointer[1] == ENCRYPT_IS) ?
506 fprintf(NetTrace, " (partial suboption??\?)");
509 if (ENCTYPE_NAME_OK(pointer[2]))
510 fprintf(NetTrace, "%s ", ENCTYPE_NAME(pointer[2]));
512 fprintf(NetTrace, " %d (unknown)", pointer[2]);
514 encrypt_printsub(&pointer[1], length - 1, buf, sizeof(buf));
515 fprintf(NetTrace, "%s", buf);
518 case ENCRYPT_SUPPORT:
520 fprintf(NetTrace, " SUPPORT ");
522 if (ENCTYPE_NAME_OK(pointer[i]))
523 fprintf(NetTrace, "%s ", ENCTYPE_NAME(pointer[i]));
525 fprintf(NetTrace, "%d ", pointer[i]);
530 case ENCRYPT_ENC_KEYID:
531 fprintf(NetTrace, " ENC_KEYID ");
534 case ENCRYPT_DEC_KEYID:
535 fprintf(NetTrace, " DEC_KEYID ");
539 fprintf(NetTrace, " %d (unknown)", pointer[1]);
541 for (i = 2; i < length; i++)
542 fprintf(NetTrace, " %d", pointer[i]);
546 #endif /* ENCRYPTION */
548 case TELOPT_LINEMODE:
549 fprintf(NetTrace, "LINEMODE ");
551 fprintf(NetTrace, " (empty suboption??\?)");
554 switch (pointer[1]) {
556 fprintf(NetTrace, "WILL ");
559 fprintf(NetTrace, "WONT ");
562 fprintf(NetTrace, "DO ");
565 fprintf(NetTrace, "DONT ");
568 fprintf(NetTrace, "(no option??\?)");
571 switch (pointer[2]) {
573 fprintf(NetTrace, "Forward Mask");
574 for (i = 3; i < length; i++)
575 fprintf(NetTrace, " %x", pointer[i]);
578 fprintf(NetTrace, "%d (unknown)", pointer[2]);
579 for (i = 3; i < length; i++)
580 fprintf(NetTrace, " %d", pointer[i]);
586 fprintf(NetTrace, "SLC");
587 for (i = 2; i < length - 2; i += 3) {
588 if (SLC_NAME_OK(pointer[i+SLC_FUNC]))
589 fprintf(NetTrace, " %s", SLC_NAME(pointer[i+SLC_FUNC]));
591 fprintf(NetTrace, " %d", pointer[i+SLC_FUNC]);
592 switch (pointer[i+SLC_FLAGS]&SLC_LEVELBITS) {
594 fprintf(NetTrace, " NOSUPPORT"); break;
596 fprintf(NetTrace, " CANTCHANGE"); break;
598 fprintf(NetTrace, " VARIABLE"); break;
600 fprintf(NetTrace, " DEFAULT"); break;
602 fprintf(NetTrace, "%s%s%s",
603 pointer[i+SLC_FLAGS]&SLC_ACK ? "|ACK" : "",
604 pointer[i+SLC_FLAGS]&SLC_FLUSHIN ? "|FLUSHIN" : "",
605 pointer[i+SLC_FLAGS]&SLC_FLUSHOUT ? "|FLUSHOUT" : "");
606 if (pointer[i+SLC_FLAGS]& ~(SLC_ACK|SLC_FLUSHIN|
607 SLC_FLUSHOUT| SLC_LEVELBITS))
608 fprintf(NetTrace, "(0x%x)", pointer[i+SLC_FLAGS]);
609 fprintf(NetTrace, " %d;", pointer[i+SLC_VALUE]);
610 if ((pointer[i+SLC_VALUE] == IAC) &&
611 (pointer[i+SLC_VALUE+1] == IAC))
614 for (; i < length; i++)
615 fprintf(NetTrace, " ?%d?", pointer[i]);
619 fprintf(NetTrace, "MODE ");
621 fprintf(NetTrace, "(no mode??\?)");
626 snprintf(tbuf, sizeof(tbuf), "%s%s%s%s%s",
627 pointer[2]&MODE_EDIT ? "|EDIT" : "",
628 pointer[2]&MODE_TRAPSIG ? "|TRAPSIG" : "",
629 pointer[2]&MODE_SOFT_TAB ? "|SOFT_TAB" : "",
630 pointer[2]&MODE_LIT_ECHO ? "|LIT_ECHO" : "",
631 pointer[2]&MODE_ACK ? "|ACK" : "");
632 fprintf(NetTrace, "%s", tbuf[1] ? &tbuf[1] : "0");
634 if (pointer[2]&~(MODE_MASK))
635 fprintf(NetTrace, " (0x%x)", pointer[2]);
636 for (i = 3; i < length; i++)
637 fprintf(NetTrace, " ?0x%x?", pointer[i]);
640 fprintf(NetTrace, "%d (unknown)", pointer[1]);
641 for (i = 2; i < length; i++)
642 fprintf(NetTrace, " %d", pointer[i]);
646 case TELOPT_STATUS: {
650 fprintf(NetTrace, "STATUS");
652 switch (pointer[1]) {
654 if (pointer[1] == TELQUAL_SEND)
655 fprintf(NetTrace, " SEND");
657 fprintf(NetTrace, " %d (unknown)", pointer[1]);
658 for (i = 2; i < length; i++)
659 fprintf(NetTrace, " ?%d?", pointer[i]);
662 if (--want_status_response < 0)
663 want_status_response = 0;
664 if (NetTrace == stdout)
665 fprintf(NetTrace, " IS\r\n");
667 fprintf(NetTrace, " IS\n");
669 for (i = 2; i < length; i++) {
671 case DO: cp = "DO"; goto common2;
672 case DONT: cp = "DONT"; goto common2;
673 case WILL: cp = "WILL"; goto common2;
674 case WONT: cp = "WONT"; goto common2;
677 if (TELOPT_OK((int)pointer[i]))
678 fprintf(NetTrace, " %s %s", cp, TELOPT(pointer[i]));
680 fprintf(NetTrace, " %s %d", cp, pointer[i]);
682 if (NetTrace == stdout)
683 fprintf(NetTrace, "\r\n");
685 fprintf(NetTrace, "\n");
689 fprintf(NetTrace, " SB ");
693 if (pointer[j] == SE) {
696 if (pointer[j+1] == SE)
701 pointer[k++] = pointer[j++];
703 printsub(0, &pointer[i], k - i);
705 fprintf(NetTrace, " SE");
710 if (NetTrace == stdout)
711 fprintf(NetTrace, "\r\n");
713 fprintf(NetTrace, "\n");
718 fprintf(NetTrace, " %d", pointer[i]);
727 case TELOPT_XDISPLOC:
728 fprintf(NetTrace, "X-DISPLAY-LOCATION ");
729 switch (pointer[1]) {
731 fprintf(NetTrace, "IS \"%.*s\"", length-2, (char *)pointer+2);
734 fprintf(NetTrace, "SEND");
737 fprintf(NetTrace, "- unknown qualifier %d (0x%x).",
738 pointer[1], pointer[1]);
742 case TELOPT_NEW_ENVIRON:
743 fprintf(NetTrace, "NEW-ENVIRON ");
746 case TELOPT_OLD_ENVIRON:
747 fprintf(NetTrace, "OLD-ENVIRON");
750 switch (pointer[1]) {
752 fprintf(NetTrace, "IS ");
755 fprintf(NetTrace, "SEND ");
758 fprintf(NetTrace, "INFO ");
762 #if defined(ENV_HACK) && defined(OLD_ENVIRON)
763 extern int old_env_var, old_env_value;
765 for (i = 2; i < length; i++ ) {
766 switch (pointer[i]) {
769 /* case NEW_ENV_OVAR: */
770 if (pointer[0] == TELOPT_OLD_ENVIRON) {
772 if (old_env_var == OLD_ENV_VALUE)
773 fprintf(NetTrace, "\" (VALUE) " + noquote);
776 fprintf(NetTrace, "\" VAR " + noquote);
778 #endif /* OLD_ENVIRON */
779 fprintf(NetTrace, "%s", "\" VALUE " + noquote);
785 /* case OLD_ENV_VALUE: */
786 if (pointer[0] == TELOPT_OLD_ENVIRON) {
788 if (old_env_value == OLD_ENV_VAR)
789 fprintf(NetTrace, "\" (VAR) " + noquote);
792 fprintf(NetTrace, "\" VALUE " + noquote);
794 #endif /* OLD_ENVIRON */
795 fprintf(NetTrace, "%s", "\" VAR " + noquote);
800 fprintf(NetTrace, "%s", "\" ESC " + noquote);
805 fprintf(NetTrace, "%s", "\" USERVAR " + noquote);
810 if (isprint(pointer[i]) && pointer[i] != '"') {
815 putc(pointer[i], NetTrace);
817 fprintf(NetTrace, "\" %03o " + noquote,
832 if (TELOPT_OK(pointer[0]))
833 fprintf(NetTrace, "%s (unknown)", TELOPT(pointer[0]));
835 fprintf(NetTrace, "%d (unknown)", pointer[0]);
836 for (i = 1; i < length; i++)
837 fprintf(NetTrace, " %d", pointer[i]);
841 if (NetTrace == stdout)
842 fprintf(NetTrace, "\r\n");
844 fprintf(NetTrace, "\n");
846 if (NetTrace == stdout)
851 /* EmptyTerminal - called to make sure that the terminal buffer is empty.
852 * Note that we consider the buffer to run all the
853 * way to the kernel (thus the select).
863 if (TTYBYTES() == 0) {
865 (void) select(tout+1, (fd_set *) 0, &o, (fd_set *) 0,
866 (struct timeval *) 0); /* wait for TTLOWAT */
871 (void) select(tout+1, (fd_set *) 0, &o, (fd_set *) 0,
872 (struct timeval *) 0); /* wait for TTLOWAT */
882 (void)telrcv(); /* Process any incoming data */
884 } while (ring_full_count(&netiring)); /* While there is any */
889 EmptyTerminal(); /* Flush the path to the tty */
901 ExitString(const char *string, int returnCode)
904 fwrite(string, 1, strlen(string), stderr);