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
34 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD: src/usr.bin/telnet/utilities.c,v 1.3.12.1 2002/04/13 11:07:13 markm Exp $");
39 static const char sccsid[] = "@(#)utilities.c 8.3 (Berkeley) 5/30/95";
45 #include <arpa/telnet.h>
46 #include <sys/types.h>
47 #include <sys/socket.h>
63 FILE *NetTrace = 0; /* Not in bss, since needs to stay */
69 * Upcase (in place) the argument.
73 upcase(char *argument)
77 while ((c = *argument) != 0) {
79 *argument = toupper(c);
88 * Compensate for differences in 4.2 and 4.3 systems.
92 SetSockOpt(int fd, int level, int option, int yesno)
94 return setsockopt(fd, level, option,
95 (char *)&yesno, sizeof yesno);
99 * The following are routines used to print out debugging information.
102 unsigned char NetTraceFile[256] = "(standard output)";
105 SetNetTrace(char *file)
107 if (NetTrace && NetTrace != stdout)
109 if (file && (strcmp(file, "-") != 0)) {
110 NetTrace = fopen(file, "w");
112 strcpy((char *)NetTraceFile, file);
115 fprintf(stderr, "Cannot open %s.\n", file);
118 strcpy((char *)NetTraceFile, "(standard output)");
122 Dump(char direction, unsigned char *buffer, int length)
124 # define BYTES_PER_LINE 32
125 # define min(x,y) ((x<y)? x:y)
126 unsigned char *pThis;
133 fprintf(NetTrace, "%c 0x%x\t", direction, offset);
136 buffer = buffer + min(length, BYTES_PER_LINE/2);
137 while (pThis < buffer) {
138 fprintf(NetTrace, "%c%.2x",
139 (((*pThis)&0xff) == 0xff) ? '*' : ' ',
143 length -= BYTES_PER_LINE/2;
144 offset += BYTES_PER_LINE/2;
146 buffer = buffer + min(length, BYTES_PER_LINE);
147 while (pThis < buffer) {
148 fprintf(NetTrace, "%.2x", (*pThis)&0xff);
151 length -= BYTES_PER_LINE;
152 offset += BYTES_PER_LINE;
154 if (NetTrace == stdout) {
155 fprintf(NetTrace, "\r\n");
157 fprintf(NetTrace, "\n");
163 /* find next unique line */
170 printoption(const char *direction, int cmd, int option)
175 if (TELCMD_OK(option))
176 fprintf(NetTrace, "%s IAC %s", direction, TELCMD(option));
178 fprintf(NetTrace, "%s IAC %d", direction, option);
181 fmt = (cmd == WILL) ? "WILL" : (cmd == WONT) ? "WONT" :
182 (cmd == DO) ? "DO" : (cmd == DONT) ? "DONT" : 0;
184 fprintf(NetTrace, "%s %s ", direction, fmt);
185 if (TELOPT_OK(option))
186 fprintf(NetTrace, "%s", TELOPT(option));
187 else if (option == TELOPT_EXOPL)
188 fprintf(NetTrace, "EXOPL");
190 fprintf(NetTrace, "%d", option);
192 fprintf(NetTrace, "%s %d %d", direction, cmd, option);
194 if (NetTrace == stdout) {
195 fprintf(NetTrace, "\r\n");
198 fprintf(NetTrace, "\n");
207 extern char will_wont_resp[], do_dont_resp[];
209 for (i = 0; i < 256; i++) {
210 if (do_dont_resp[i]) {
212 printf("resp DO_DONT %s: %d\n", TELOPT(i), do_dont_resp[i]);
213 else if (TELCMD_OK(i))
214 printf("resp DO_DONT %s: %d\n", TELCMD(i), do_dont_resp[i]);
216 printf("resp DO_DONT %d: %d\n", i,
218 if (my_want_state_is_do(i)) {
220 printf("want DO %s\n", TELOPT(i));
221 else if (TELCMD_OK(i))
222 printf("want DO %s\n", TELCMD(i));
224 printf("want DO %d\n", i);
227 printf("want DONT %s\n", TELOPT(i));
228 else if (TELCMD_OK(i))
229 printf("want DONT %s\n", TELCMD(i));
231 printf("want DONT %d\n", i);
234 if (my_state_is_do(i)) {
236 printf(" DO %s\n", TELOPT(i));
237 else if (TELCMD_OK(i))
238 printf(" DO %s\n", TELCMD(i));
240 printf(" DO %d\n", i);
243 if (will_wont_resp[i]) {
245 printf("resp WILL_WONT %s: %d\n", TELOPT(i), will_wont_resp[i]);
246 else if (TELCMD_OK(i))
247 printf("resp WILL_WONT %s: %d\n", TELCMD(i), will_wont_resp[i]);
249 printf("resp WILL_WONT %d: %d\n",
250 i, will_wont_resp[i]);
251 if (my_want_state_is_will(i)) {
253 printf("want WILL %s\n", TELOPT(i));
254 else if (TELCMD_OK(i))
255 printf("want WILL %s\n", TELCMD(i));
257 printf("want WILL %d\n", i);
260 printf("want WONT %s\n", TELOPT(i));
261 else if (TELCMD_OK(i))
262 printf("want WONT %s\n", TELCMD(i));
264 printf("want WONT %d\n", i);
267 if (my_state_is_will(i)) {
269 printf(" WILL %s\n", TELOPT(i));
270 else if (TELCMD_OK(i))
271 printf(" WILL %s\n", TELCMD(i));
273 printf(" WILL %d\n", i);
281 printsub(char direction, unsigned char *pointer, int length)
284 extern int want_status_response;
286 if (showoptions || direction == 0 ||
287 (want_status_response && (pointer[0] == TELOPT_STATUS))) {
289 fprintf(NetTrace, "%s IAC SB ",
290 (direction == '<')? "RCVD":"SENT");
294 i = pointer[length-2];
295 j = pointer[length-1];
297 if (i != IAC || j != SE) {
298 fprintf(NetTrace, "(terminated by ");
300 fprintf(NetTrace, "%s ", TELOPT(i));
301 else if (TELCMD_OK(i))
302 fprintf(NetTrace, "%s ", TELCMD(i));
304 fprintf(NetTrace, "%d ", i);
306 fprintf(NetTrace, "%s", TELOPT(j));
307 else if (TELCMD_OK(j))
308 fprintf(NetTrace, "%s", TELCMD(j));
310 fprintf(NetTrace, "%d", j);
311 fprintf(NetTrace, ", not IAC SE!) ");
317 fprintf(NetTrace, "(Empty suboption??\?)");
318 if (NetTrace == stdout)
322 switch (pointer[0]) {
324 fprintf(NetTrace, "TERMINAL-TYPE ");
325 switch (pointer[1]) {
327 fprintf(NetTrace, "IS \"%.*s\"", length-2, (char *)pointer+2);
330 fprintf(NetTrace, "SEND");
334 "- unknown qualifier %d (0x%x).",
335 pointer[1], pointer[1]);
339 fprintf(NetTrace, "TERMINAL-SPEED");
341 fprintf(NetTrace, " (empty suboption??\?)");
344 switch (pointer[1]) {
346 fprintf(NetTrace, " IS ");
347 fprintf(NetTrace, "%.*s", length-2, (char *)pointer+2);
351 fprintf(NetTrace, " SEND");
353 fprintf(NetTrace, " %d (unknown)", pointer[1]);
354 for (i = 2; i < length; i++)
355 fprintf(NetTrace, " ?%d?", pointer[i]);
361 fprintf(NetTrace, "TOGGLE-FLOW-CONTROL");
363 fprintf(NetTrace, " (empty suboption??\?)");
366 switch (pointer[1]) {
368 fprintf(NetTrace, " OFF"); break;
370 fprintf(NetTrace, " ON"); break;
371 case LFLOW_RESTART_ANY:
372 fprintf(NetTrace, " RESTART-ANY"); break;
373 case LFLOW_RESTART_XON:
374 fprintf(NetTrace, " RESTART-XON"); break;
376 fprintf(NetTrace, " %d (unknown)", pointer[1]);
378 for (i = 2; i < length; i++)
379 fprintf(NetTrace, " ?%d?", pointer[i]);
383 fprintf(NetTrace, "NAWS");
385 fprintf(NetTrace, " (empty suboption??\?)");
389 fprintf(NetTrace, " ?%d?", pointer[1]);
392 fprintf(NetTrace, " %d %d (%d)",
393 pointer[1], pointer[2],
394 (int)((((unsigned int)pointer[1])<<8)|((unsigned int)pointer[2])));
396 fprintf(NetTrace, " ?%d?", pointer[3]);
399 fprintf(NetTrace, " %d %d (%d)",
400 pointer[3], pointer[4],
401 (int)((((unsigned int)pointer[3])<<8)|((unsigned int)pointer[4])));
402 for (i = 5; i < length; i++)
403 fprintf(NetTrace, " ?%d?", pointer[i]);
408 case TELOPT_LINEMODE:
409 fprintf(NetTrace, "LINEMODE ");
411 fprintf(NetTrace, " (empty suboption??\?)");
414 switch (pointer[1]) {
416 fprintf(NetTrace, "WILL ");
419 fprintf(NetTrace, "WONT ");
422 fprintf(NetTrace, "DO ");
425 fprintf(NetTrace, "DONT ");
428 fprintf(NetTrace, "(no option??\?)");
431 switch (pointer[2]) {
433 fprintf(NetTrace, "Forward Mask");
434 for (i = 3; i < length; i++)
435 fprintf(NetTrace, " %x", pointer[i]);
438 fprintf(NetTrace, "%d (unknown)", pointer[2]);
439 for (i = 3; i < length; i++)
440 fprintf(NetTrace, " %d", pointer[i]);
446 fprintf(NetTrace, "SLC");
447 for (i = 2; i < length - 2; i += 3) {
448 if (SLC_NAME_OK(pointer[i+SLC_FUNC]))
449 fprintf(NetTrace, " %s", SLC_NAME(pointer[i+SLC_FUNC]));
451 fprintf(NetTrace, " %d", pointer[i+SLC_FUNC]);
452 switch (pointer[i+SLC_FLAGS]&SLC_LEVELBITS) {
454 fprintf(NetTrace, " NOSUPPORT"); break;
456 fprintf(NetTrace, " CANTCHANGE"); break;
458 fprintf(NetTrace, " VARIABLE"); break;
460 fprintf(NetTrace, " DEFAULT"); break;
462 fprintf(NetTrace, "%s%s%s",
463 pointer[i+SLC_FLAGS]&SLC_ACK ? "|ACK" : "",
464 pointer[i+SLC_FLAGS]&SLC_FLUSHIN ? "|FLUSHIN" : "",
465 pointer[i+SLC_FLAGS]&SLC_FLUSHOUT ? "|FLUSHOUT" : "");
466 if (pointer[i+SLC_FLAGS]& ~(SLC_ACK|SLC_FLUSHIN|
467 SLC_FLUSHOUT| SLC_LEVELBITS))
468 fprintf(NetTrace, "(0x%x)", pointer[i+SLC_FLAGS]);
469 fprintf(NetTrace, " %d;", pointer[i+SLC_VALUE]);
470 if ((pointer[i+SLC_VALUE] == IAC) &&
471 (pointer[i+SLC_VALUE+1] == IAC))
474 for (; i < length; i++)
475 fprintf(NetTrace, " ?%d?", pointer[i]);
479 fprintf(NetTrace, "MODE ");
481 fprintf(NetTrace, "(no mode??\?)");
486 sprintf(tbuf, "%s%s%s%s%s",
487 pointer[2]&MODE_EDIT ? "|EDIT" : "",
488 pointer[2]&MODE_TRAPSIG ? "|TRAPSIG" : "",
489 pointer[2]&MODE_SOFT_TAB ? "|SOFT_TAB" : "",
490 pointer[2]&MODE_LIT_ECHO ? "|LIT_ECHO" : "",
491 pointer[2]&MODE_ACK ? "|ACK" : "");
492 fprintf(NetTrace, "%s", tbuf[1] ? &tbuf[1] : "0");
494 if (pointer[2]&~(MODE_MASK))
495 fprintf(NetTrace, " (0x%x)", pointer[2]);
496 for (i = 3; i < length; i++)
497 fprintf(NetTrace, " ?0x%x?", pointer[i]);
500 fprintf(NetTrace, "%d (unknown)", pointer[1]);
501 for (i = 2; i < length; i++)
502 fprintf(NetTrace, " %d", pointer[i]);
506 case TELOPT_STATUS: {
510 fprintf(NetTrace, "STATUS");
512 switch (pointer[1]) {
514 if (pointer[1] == TELQUAL_SEND)
515 fprintf(NetTrace, " SEND");
517 fprintf(NetTrace, " %d (unknown)", pointer[1]);
518 for (i = 2; i < length; i++)
519 fprintf(NetTrace, " ?%d?", pointer[i]);
522 if (--want_status_response < 0)
523 want_status_response = 0;
524 if (NetTrace == stdout)
525 fprintf(NetTrace, " IS\r\n");
527 fprintf(NetTrace, " IS\n");
529 for (i = 2; i < length; i++) {
531 case DO: cp = "DO"; goto common2;
532 case DONT: cp = "DONT"; goto common2;
533 case WILL: cp = "WILL"; goto common2;
534 case WONT: cp = "WONT"; goto common2;
537 if (TELOPT_OK((int)pointer[i]))
538 fprintf(NetTrace, " %s %s", cp, TELOPT(pointer[i]));
540 fprintf(NetTrace, " %s %d", cp, pointer[i]);
542 if (NetTrace == stdout)
543 fprintf(NetTrace, "\r\n");
545 fprintf(NetTrace, "\n");
549 fprintf(NetTrace, " SB ");
553 if (pointer[j] == SE) {
556 if (pointer[j+1] == SE)
561 pointer[k++] = pointer[j++];
563 printsub(0, &pointer[i], k - i);
565 fprintf(NetTrace, " SE");
570 if (NetTrace == stdout)
571 fprintf(NetTrace, "\r\n");
573 fprintf(NetTrace, "\n");
578 fprintf(NetTrace, " %d", pointer[i]);
587 case TELOPT_XDISPLOC:
588 fprintf(NetTrace, "X-DISPLAY-LOCATION ");
589 switch (pointer[1]) {
591 fprintf(NetTrace, "IS \"%.*s\"", length-2, (char *)pointer+2);
594 fprintf(NetTrace, "SEND");
597 fprintf(NetTrace, "- unknown qualifier %d (0x%x).",
598 pointer[1], pointer[1]);
602 case TELOPT_NEW_ENVIRON:
603 fprintf(NetTrace, "NEW-ENVIRON ");
606 case TELOPT_OLD_ENVIRON:
607 fprintf(NetTrace, "OLD-ENVIRON");
610 switch (pointer[1]) {
612 fprintf(NetTrace, "IS ");
615 fprintf(NetTrace, "SEND ");
618 fprintf(NetTrace, "INFO ");
622 #if defined(ENV_HACK) && defined(OLD_ENVIRON)
623 extern int old_env_var, old_env_value;
625 for (i = 2; i < length; i++ ) {
626 switch (pointer[i]) {
629 /* case NEW_ENV_OVAR: */
630 if (pointer[0] == TELOPT_OLD_ENVIRON) {
632 if (old_env_var == OLD_ENV_VALUE)
633 fprintf(NetTrace, "\" (VALUE) " + noquote);
636 fprintf(NetTrace, "\" VAR " + noquote);
638 #endif /* OLD_ENVIRON */
639 fprintf(NetTrace, "\" VALUE " + noquote);
645 /* case OLD_ENV_VALUE: */
646 if (pointer[0] == TELOPT_OLD_ENVIRON) {
648 if (old_env_value == OLD_ENV_VAR)
649 fprintf(NetTrace, "\" (VAR) " + noquote);
652 fprintf(NetTrace, "\" VALUE " + noquote);
654 #endif /* OLD_ENVIRON */
655 fprintf(NetTrace, "\" VAR " + noquote);
660 fprintf(NetTrace, "\" ESC " + noquote);
665 fprintf(NetTrace, "\" USERVAR " + noquote);
670 if (isprint(pointer[i]) && pointer[i] != '"') {
675 putc(pointer[i], NetTrace);
677 fprintf(NetTrace, "\" %03o " + noquote,
692 if (TELOPT_OK(pointer[0]))
693 fprintf(NetTrace, "%s (unknown)", TELOPT(pointer[0]));
695 fprintf(NetTrace, "%d (unknown)", pointer[0]);
696 for (i = 1; i < length; i++)
697 fprintf(NetTrace, " %d", pointer[i]);
701 if (NetTrace == stdout)
702 fprintf(NetTrace, "\r\n");
704 fprintf(NetTrace, "\n");
706 if (NetTrace == stdout)
711 /* EmptyTerminal - called to make sure that the terminal buffer is empty.
712 * Note that we consider the buffer to run all the
713 * way to the kernel (thus the select).
723 if (TTYBYTES() == 0) {
725 (void) select(tout+1, (fd_set *) 0, &o, (fd_set *) 0,
726 (struct timeval *) 0); /* wait for TTLOWAT */
731 (void) select(tout+1, (fd_set *) 0, &o, (fd_set *) 0,
732 (struct timeval *) 0); /* wait for TTLOWAT */
742 (void)telrcv(); /* Process any incoming data */
744 } while (ring_full_count(&netiring)); /* While there is any */
749 EmptyTerminal(); /* Flush the path to the tty */
761 ExitString(const char *string, int returnCode)
764 fwrite(string, 1, strlen(string), stderr);