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
35 static char sccsid[] = "@(#)utilities.c 8.2 (Berkeley) 12/15/93";
41 #include <arpa/telnet.h>
42 #include <sys/types.h>
57 FILE *NetTrace = 0; /* Not in bss, since needs to stay */
63 * Upcase (in place) the argument.
68 register char *argument;
72 while ((c = *argument) != 0) {
74 *argument = toupper(c);
83 * Compensate for differences in 4.2 and 4.3 systems.
87 SetSockOpt(fd, level, option, yesno)
88 int fd, level, option, yesno;
91 return setsockopt(fd, level, option,
92 (char *)&yesno, sizeof yesno);
94 if (yesno == 0) { /* Can't do that in 4.2! */
95 fprintf(stderr, "Error: attempt to turn off an option 0x%x.\n",
99 return setsockopt(fd, level, option, 0, 0);
104 * The following are routines used to print out debugging information.
107 unsigned char NetTraceFile[256] = "(standard output)";
113 if (NetTrace && NetTrace != stdout)
115 if (file && (strcmp(file, "-") != 0)) {
116 NetTrace = fopen(file, "w");
118 strcpy((char *)NetTraceFile, file);
121 fprintf(stderr, "Cannot open %s.\n", file);
124 strcpy((char *)NetTraceFile, "(standard output)");
128 Dump(direction, buffer, length)
130 unsigned char *buffer;
133 # define BYTES_PER_LINE 32
134 # define min(x,y) ((x<y)? x:y)
135 unsigned char *pThis;
143 fprintf(NetTrace, "%c 0x%x\t", direction, offset);
146 buffer = buffer + min(length, BYTES_PER_LINE/2);
147 while (pThis < buffer) {
148 fprintf(NetTrace, "%c%.2x",
149 (((*pThis)&0xff) == 0xff) ? '*' : ' ',
153 length -= BYTES_PER_LINE/2;
154 offset += BYTES_PER_LINE/2;
156 buffer = buffer + min(length, BYTES_PER_LINE);
157 while (pThis < buffer) {
158 fprintf(NetTrace, "%.2x", (*pThis)&0xff);
161 length -= BYTES_PER_LINE;
162 offset += BYTES_PER_LINE;
164 if (NetTrace == stdout) {
165 fprintf(NetTrace, "\r\n");
167 fprintf(NetTrace, "\n");
173 /* find next unique line */
180 printoption(direction, cmd, option)
187 if (TELCMD_OK(option))
188 fprintf(NetTrace, "%s IAC %s", direction, TELCMD(option));
190 fprintf(NetTrace, "%s IAC %d", direction, option);
193 fmt = (cmd == WILL) ? "WILL" : (cmd == WONT) ? "WONT" :
194 (cmd == DO) ? "DO" : (cmd == DONT) ? "DONT" : 0;
196 fprintf(NetTrace, "%s %s ", direction, fmt);
197 if (TELOPT_OK(option))
198 fprintf(NetTrace, "%s", TELOPT(option));
199 else if (option == TELOPT_EXOPL)
200 fprintf(NetTrace, "EXOPL");
202 fprintf(NetTrace, "%d", option);
204 fprintf(NetTrace, "%s %d %d", direction, cmd, option);
206 if (NetTrace == stdout) {
207 fprintf(NetTrace, "\r\n");
210 fprintf(NetTrace, "\n");
219 extern char will_wont_resp[], do_dont_resp[];
221 for (i = 0; i < 256; i++) {
222 if (do_dont_resp[i]) {
224 printf("resp DO_DONT %s: %d\n", TELOPT(i), do_dont_resp[i]);
225 else if (TELCMD_OK(i))
226 printf("resp DO_DONT %s: %d\n", TELCMD(i), do_dont_resp[i]);
228 printf("resp DO_DONT %d: %d\n", i,
230 if (my_want_state_is_do(i)) {
232 printf("want DO %s\n", TELOPT(i));
233 else if (TELCMD_OK(i))
234 printf("want DO %s\n", TELCMD(i));
236 printf("want DO %d\n", i);
239 printf("want DONT %s\n", TELOPT(i));
240 else if (TELCMD_OK(i))
241 printf("want DONT %s\n", TELCMD(i));
243 printf("want DONT %d\n", i);
246 if (my_state_is_do(i)) {
248 printf(" DO %s\n", TELOPT(i));
249 else if (TELCMD_OK(i))
250 printf(" DO %s\n", TELCMD(i));
252 printf(" DO %d\n", i);
255 if (will_wont_resp[i]) {
257 printf("resp WILL_WONT %s: %d\n", TELOPT(i), will_wont_resp[i]);
258 else if (TELCMD_OK(i))
259 printf("resp WILL_WONT %s: %d\n", TELCMD(i), will_wont_resp[i]);
261 printf("resp WILL_WONT %d: %d\n",
262 i, will_wont_resp[i]);
263 if (my_want_state_is_will(i)) {
265 printf("want WILL %s\n", TELOPT(i));
266 else if (TELCMD_OK(i))
267 printf("want WILL %s\n", TELCMD(i));
269 printf("want WILL %d\n", i);
272 printf("want WONT %s\n", TELOPT(i));
273 else if (TELCMD_OK(i))
274 printf("want WONT %s\n", TELCMD(i));
276 printf("want WONT %d\n", i);
279 if (my_state_is_will(i)) {
281 printf(" WILL %s\n", TELOPT(i));
282 else if (TELCMD_OK(i))
283 printf(" WILL %s\n", TELCMD(i));
285 printf(" WILL %d\n", i);
293 printsub(direction, pointer, length)
294 char direction; /* '<' or '>' */
295 unsigned char *pointer; /* where suboption data sits */
296 int length; /* length of suboption data */
300 extern int want_status_response;
302 if (showoptions || direction == 0 ||
303 (want_status_response && (pointer[0] == TELOPT_STATUS))) {
305 fprintf(NetTrace, "%s IAC SB ",
306 (direction == '<')? "RCVD":"SENT");
310 i = pointer[length-2];
311 j = pointer[length-1];
313 if (i != IAC || j != SE) {
314 fprintf(NetTrace, "(terminated by ");
316 fprintf(NetTrace, "%s ", TELOPT(i));
317 else if (TELCMD_OK(i))
318 fprintf(NetTrace, "%s ", TELCMD(i));
320 fprintf(NetTrace, "%d ", i);
322 fprintf(NetTrace, "%s", TELOPT(j));
323 else if (TELCMD_OK(j))
324 fprintf(NetTrace, "%s", TELCMD(j));
326 fprintf(NetTrace, "%d", j);
327 fprintf(NetTrace, ", not IAC SE!) ");
333 fprintf(NetTrace, "(Empty suboption??\?)");
334 if (NetTrace == stdout)
338 switch (pointer[0]) {
340 fprintf(NetTrace, "TERMINAL-TYPE ");
341 switch (pointer[1]) {
343 fprintf(NetTrace, "IS \"%.*s\"", length-2, (char *)pointer+2);
346 fprintf(NetTrace, "SEND");
350 "- unknown qualifier %d (0x%x).",
351 pointer[1], pointer[1]);
355 fprintf(NetTrace, "TERMINAL-SPEED");
357 fprintf(NetTrace, " (empty suboption??\?)");
360 switch (pointer[1]) {
362 fprintf(NetTrace, " IS ");
363 fprintf(NetTrace, "%.*s", length-2, (char *)pointer+2);
367 fprintf(NetTrace, " SEND");
369 fprintf(NetTrace, " %d (unknown)", pointer[1]);
370 for (i = 2; i < length; i++)
371 fprintf(NetTrace, " ?%d?", pointer[i]);
377 fprintf(NetTrace, "TOGGLE-FLOW-CONTROL");
379 fprintf(NetTrace, " (empty suboption??\?)");
382 switch (pointer[1]) {
384 fprintf(NetTrace, " OFF"); break;
386 fprintf(NetTrace, " ON"); break;
387 case LFLOW_RESTART_ANY:
388 fprintf(NetTrace, " RESTART-ANY"); break;
389 case LFLOW_RESTART_XON:
390 fprintf(NetTrace, " RESTART-XON"); break;
392 fprintf(NetTrace, " %d (unknown)", pointer[1]);
394 for (i = 2; i < length; i++)
395 fprintf(NetTrace, " ?%d?", pointer[i]);
399 fprintf(NetTrace, "NAWS");
401 fprintf(NetTrace, " (empty suboption??\?)");
405 fprintf(NetTrace, " ?%d?", pointer[1]);
408 fprintf(NetTrace, " %d %d (%d)",
409 pointer[1], pointer[2],
410 (int)((((unsigned int)pointer[1])<<8)|((unsigned int)pointer[2])));
412 fprintf(NetTrace, " ?%d?", pointer[3]);
415 fprintf(NetTrace, " %d %d (%d)",
416 pointer[3], pointer[4],
417 (int)((((unsigned int)pointer[3])<<8)|((unsigned int)pointer[4])));
418 for (i = 5; i < length; i++)
419 fprintf(NetTrace, " ?%d?", pointer[i]);
422 #if defined(AUTHENTICATION)
423 case TELOPT_AUTHENTICATION:
424 fprintf(NetTrace, "AUTHENTICATION");
426 fprintf(NetTrace, " (empty suboption??\?)");
429 switch (pointer[1]) {
432 fprintf(NetTrace, " %s ", (pointer[1] == TELQUAL_IS) ?
434 if (AUTHTYPE_NAME_OK(pointer[2]))
435 fprintf(NetTrace, "%s ", AUTHTYPE_NAME(pointer[2]));
437 fprintf(NetTrace, "%d ", pointer[2]);
439 fprintf(NetTrace, "(partial suboption??\?)");
442 fprintf(NetTrace, "%s|%s",
443 ((pointer[3] & AUTH_WHO_MASK) == AUTH_WHO_CLIENT) ?
445 ((pointer[3] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) ?
446 "MUTUAL" : "ONE-WAY");
448 auth_printsub(&pointer[1], length - 1, buf, sizeof(buf));
449 fprintf(NetTrace, "%s", buf);
454 fprintf(NetTrace, " SEND ");
456 if (AUTHTYPE_NAME_OK(pointer[i]))
457 fprintf(NetTrace, "%s ", AUTHTYPE_NAME(pointer[i]));
459 fprintf(NetTrace, "%d ", pointer[i]);
461 fprintf(NetTrace, "(partial suboption??\?)");
464 fprintf(NetTrace, "%s|%s ",
465 ((pointer[i] & AUTH_WHO_MASK) == AUTH_WHO_CLIENT) ?
467 ((pointer[i] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) ?
468 "MUTUAL" : "ONE-WAY");
475 fprintf(NetTrace, " NAME \"");
477 putc(pointer[i++], NetTrace);
482 for (i = 2; i < length; i++)
483 fprintf(NetTrace, " ?%d?", pointer[i]);
490 case TELOPT_LINEMODE:
491 fprintf(NetTrace, "LINEMODE ");
493 fprintf(NetTrace, " (empty suboption??\?)");
496 switch (pointer[1]) {
498 fprintf(NetTrace, "WILL ");
501 fprintf(NetTrace, "WONT ");
504 fprintf(NetTrace, "DO ");
507 fprintf(NetTrace, "DONT ");
510 fprintf(NetTrace, "(no option??\?)");
513 switch (pointer[2]) {
515 fprintf(NetTrace, "Forward Mask");
516 for (i = 3; i < length; i++)
517 fprintf(NetTrace, " %x", pointer[i]);
520 fprintf(NetTrace, "%d (unknown)", pointer[2]);
521 for (i = 3; i < length; i++)
522 fprintf(NetTrace, " %d", pointer[i]);
528 fprintf(NetTrace, "SLC");
529 for (i = 2; i < length - 2; i += 3) {
530 if (SLC_NAME_OK(pointer[i+SLC_FUNC]))
531 fprintf(NetTrace, " %s", SLC_NAME(pointer[i+SLC_FUNC]));
533 fprintf(NetTrace, " %d", pointer[i+SLC_FUNC]);
534 switch (pointer[i+SLC_FLAGS]&SLC_LEVELBITS) {
536 fprintf(NetTrace, " NOSUPPORT"); break;
538 fprintf(NetTrace, " CANTCHANGE"); break;
540 fprintf(NetTrace, " VARIABLE"); break;
542 fprintf(NetTrace, " DEFAULT"); break;
544 fprintf(NetTrace, "%s%s%s",
545 pointer[i+SLC_FLAGS]&SLC_ACK ? "|ACK" : "",
546 pointer[i+SLC_FLAGS]&SLC_FLUSHIN ? "|FLUSHIN" : "",
547 pointer[i+SLC_FLAGS]&SLC_FLUSHOUT ? "|FLUSHOUT" : "");
548 if (pointer[i+SLC_FLAGS]& ~(SLC_ACK|SLC_FLUSHIN|
549 SLC_FLUSHOUT| SLC_LEVELBITS))
550 fprintf(NetTrace, "(0x%x)", pointer[i+SLC_FLAGS]);
551 fprintf(NetTrace, " %d;", pointer[i+SLC_VALUE]);
552 if ((pointer[i+SLC_VALUE] == IAC) &&
553 (pointer[i+SLC_VALUE+1] == IAC))
556 for (; i < length; i++)
557 fprintf(NetTrace, " ?%d?", pointer[i]);
561 fprintf(NetTrace, "MODE ");
563 fprintf(NetTrace, "(no mode??\?)");
568 sprintf(tbuf, "%s%s%s%s%s",
569 pointer[2]&MODE_EDIT ? "|EDIT" : "",
570 pointer[2]&MODE_TRAPSIG ? "|TRAPSIG" : "",
571 pointer[2]&MODE_SOFT_TAB ? "|SOFT_TAB" : "",
572 pointer[2]&MODE_LIT_ECHO ? "|LIT_ECHO" : "",
573 pointer[2]&MODE_ACK ? "|ACK" : "");
574 fprintf(NetTrace, "%s", tbuf[1] ? &tbuf[1] : "0");
576 if (pointer[2]&~(MODE_MASK))
577 fprintf(NetTrace, " (0x%x)", pointer[2]);
578 for (i = 3; i < length; i++)
579 fprintf(NetTrace, " ?0x%x?", pointer[i]);
582 fprintf(NetTrace, "%d (unknown)", pointer[1]);
583 for (i = 2; i < length; i++)
584 fprintf(NetTrace, " %d", pointer[i]);
588 case TELOPT_STATUS: {
592 fprintf(NetTrace, "STATUS");
594 switch (pointer[1]) {
596 if (pointer[1] == TELQUAL_SEND)
597 fprintf(NetTrace, " SEND");
599 fprintf(NetTrace, " %d (unknown)", pointer[1]);
600 for (i = 2; i < length; i++)
601 fprintf(NetTrace, " ?%d?", pointer[i]);
604 if (--want_status_response < 0)
605 want_status_response = 0;
606 if (NetTrace == stdout)
607 fprintf(NetTrace, " IS\r\n");
609 fprintf(NetTrace, " IS\n");
611 for (i = 2; i < length; i++) {
613 case DO: cp = "DO"; goto common2;
614 case DONT: cp = "DONT"; goto common2;
615 case WILL: cp = "WILL"; goto common2;
616 case WONT: cp = "WONT"; goto common2;
619 if (TELOPT_OK((int)pointer[i]))
620 fprintf(NetTrace, " %s %s", cp, TELOPT(pointer[i]));
622 fprintf(NetTrace, " %s %d", cp, pointer[i]);
624 if (NetTrace == stdout)
625 fprintf(NetTrace, "\r\n");
627 fprintf(NetTrace, "\n");
631 fprintf(NetTrace, " SB ");
635 if (pointer[j] == SE) {
638 if (pointer[j+1] == SE)
643 pointer[k++] = pointer[j++];
645 printsub(0, &pointer[i], k - i);
647 fprintf(NetTrace, " SE");
652 if (NetTrace == stdout)
653 fprintf(NetTrace, "\r\n");
655 fprintf(NetTrace, "\n");
660 fprintf(NetTrace, " %d", pointer[i]);
669 case TELOPT_XDISPLOC:
670 fprintf(NetTrace, "X-DISPLAY-LOCATION ");
671 switch (pointer[1]) {
673 fprintf(NetTrace, "IS \"%.*s\"", length-2, (char *)pointer+2);
676 fprintf(NetTrace, "SEND");
679 fprintf(NetTrace, "- unknown qualifier %d (0x%x).",
680 pointer[1], pointer[1]);
684 case TELOPT_NEW_ENVIRON:
685 fprintf(NetTrace, "NEW-ENVIRON ");
688 case TELOPT_OLD_ENVIRON:
689 fprintf(NetTrace, "OLD-ENVIRON");
692 switch (pointer[1]) {
694 fprintf(NetTrace, "IS ");
697 fprintf(NetTrace, "SEND ");
700 fprintf(NetTrace, "INFO ");
703 register int noquote = 2;
704 #if defined(ENV_HACK) && defined(OLD_ENVIRON)
705 extern int old_env_var, old_env_value;
707 for (i = 2; i < length; i++ ) {
708 switch (pointer[i]) {
711 /* case NEW_ENV_OVAR: */
712 if (pointer[0] == TELOPT_OLD_ENVIRON) {
714 if (old_env_var == OLD_ENV_VALUE)
715 fprintf(NetTrace, "\" (VALUE) " + noquote);
718 fprintf(NetTrace, "\" VAR " + noquote);
720 #endif /* OLD_ENVIRON */
721 fprintf(NetTrace, "\" VALUE " + noquote);
727 /* case OLD_ENV_VALUE: */
728 if (pointer[0] == TELOPT_OLD_ENVIRON) {
730 if (old_env_value == OLD_ENV_VAR)
731 fprintf(NetTrace, "\" (VAR) " + noquote);
734 fprintf(NetTrace, "\" VALUE " + noquote);
736 #endif /* OLD_ENVIRON */
737 fprintf(NetTrace, "\" VAR " + noquote);
742 fprintf(NetTrace, "\" ESC " + noquote);
747 fprintf(NetTrace, "\" USERVAR " + noquote);
753 if (isprint(pointer[i]) && pointer[i] != '"') {
758 putc(pointer[i], NetTrace);
760 fprintf(NetTrace, "\" %03o " + noquote,
775 if (TELOPT_OK(pointer[0]))
776 fprintf(NetTrace, "%s (unknown)", TELOPT(pointer[0]));
778 fprintf(NetTrace, "%d (unknown)", pointer[0]);
779 for (i = 1; i < length; i++)
780 fprintf(NetTrace, " %d", pointer[i]);
784 if (NetTrace == stdout)
785 fprintf(NetTrace, "\r\n");
787 fprintf(NetTrace, "\n");
789 if (NetTrace == stdout)
794 /* EmptyTerminal - called to make sure that the terminal buffer is empty.
795 * Note that we consider the buffer to run all the
796 * way to the kernel (thus the select).
806 #endif /* defined(unix) */
808 if (TTYBYTES() == 0) {
811 (void) select(tout+1, (fd_set *) 0, &o, (fd_set *) 0,
812 (struct timeval *) 0); /* wait for TTLOWAT */
813 #endif /* defined(unix) */
819 (void) select(tout+1, (fd_set *) 0, &o, (fd_set *) 0,
820 (struct timeval *) 0); /* wait for TTLOWAT */
821 #endif /* defined(unix) */
834 #else /* defined(TN3270) */
836 (void)telrcv(); /* Process any incoming data */
838 } while (ring_full_count(&netiring)); /* While there is any */
839 #endif /* defined(TN3270) */
847 #endif /* defined(TN3270) */
849 EmptyTerminal(); /* Flush the path to the tty */
862 ExitString(string, returnCode)
867 fwrite(string, 1, strlen(string), stderr);