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
33 * @(#)utilities.c 8.3 (Berkeley) 5/30/95
34 * $FreeBSD: src/usr.bin/telnet/utilities.c,v 1.3.12.1 2002/04/13 11:07:13 markm Exp $
35 * $DragonFly: src/usr.bin/telnet/utilities.c,v 1.2 2003/06/17 04:29:32 dillon Exp $
41 #include <arpa/telnet.h>
42 #include <sys/types.h>
43 #include <sys/socket.h>
59 FILE *NetTrace
= 0; /* Not in bss, since needs to stay */
65 * Upcase (in place) the argument.
69 upcase(char *argument
)
73 while ((c
= *argument
) != 0) {
75 *argument
= toupper(c
);
84 * Compensate for differences in 4.2 and 4.3 systems.
88 SetSockOpt(int fd
, int level
, int option
, int yesno
)
90 return setsockopt(fd
, level
, option
,
91 (char *)&yesno
, sizeof yesno
);
95 * The following are routines used to print out debugging information.
98 unsigned char NetTraceFile
[256] = "(standard output)";
101 SetNetTrace(char *file
)
103 if (NetTrace
&& NetTrace
!= stdout
)
105 if (file
&& (strcmp(file
, "-") != 0)) {
106 NetTrace
= fopen(file
, "w");
108 strcpy((char *)NetTraceFile
, file
);
111 fprintf(stderr
, "Cannot open %s.\n", file
);
114 strcpy((char *)NetTraceFile
, "(standard output)");
118 Dump(char direction
, unsigned char *buffer
, int length
)
120 # define BYTES_PER_LINE 32
121 # define min(x,y) ((x<y)? x:y)
122 unsigned char *pThis
;
129 fprintf(NetTrace
, "%c 0x%x\t", direction
, offset
);
132 buffer
= buffer
+ min(length
, BYTES_PER_LINE
/2);
133 while (pThis
< buffer
) {
134 fprintf(NetTrace
, "%c%.2x",
135 (((*pThis
)&0xff) == 0xff) ? '*' : ' ',
139 length
-= BYTES_PER_LINE
/2;
140 offset
+= BYTES_PER_LINE
/2;
142 buffer
= buffer
+ min(length
, BYTES_PER_LINE
);
143 while (pThis
< buffer
) {
144 fprintf(NetTrace
, "%.2x", (*pThis
)&0xff);
147 length
-= BYTES_PER_LINE
;
148 offset
+= BYTES_PER_LINE
;
150 if (NetTrace
== stdout
) {
151 fprintf(NetTrace
, "\r\n");
153 fprintf(NetTrace
, "\n");
159 /* find next unique line */
166 printoption(const char *direction
, int cmd
, int option
)
171 if (TELCMD_OK(option
))
172 fprintf(NetTrace
, "%s IAC %s", direction
, TELCMD(option
));
174 fprintf(NetTrace
, "%s IAC %d", direction
, option
);
177 fmt
= (cmd
== WILL
) ? "WILL" : (cmd
== WONT
) ? "WONT" :
178 (cmd
== DO
) ? "DO" : (cmd
== DONT
) ? "DONT" : 0;
180 fprintf(NetTrace
, "%s %s ", direction
, fmt
);
181 if (TELOPT_OK(option
))
182 fprintf(NetTrace
, "%s", TELOPT(option
));
183 else if (option
== TELOPT_EXOPL
)
184 fprintf(NetTrace
, "EXOPL");
186 fprintf(NetTrace
, "%d", option
);
188 fprintf(NetTrace
, "%s %d %d", direction
, cmd
, option
);
190 if (NetTrace
== stdout
) {
191 fprintf(NetTrace
, "\r\n");
194 fprintf(NetTrace
, "\n");
203 extern char will_wont_resp
[], do_dont_resp
[];
205 for (i
= 0; i
< 256; i
++) {
206 if (do_dont_resp
[i
]) {
208 printf("resp DO_DONT %s: %d\n", TELOPT(i
), do_dont_resp
[i
]);
209 else if (TELCMD_OK(i
))
210 printf("resp DO_DONT %s: %d\n", TELCMD(i
), do_dont_resp
[i
]);
212 printf("resp DO_DONT %d: %d\n", i
,
214 if (my_want_state_is_do(i
)) {
216 printf("want DO %s\n", TELOPT(i
));
217 else if (TELCMD_OK(i
))
218 printf("want DO %s\n", TELCMD(i
));
220 printf("want DO %d\n", i
);
223 printf("want DONT %s\n", TELOPT(i
));
224 else if (TELCMD_OK(i
))
225 printf("want DONT %s\n", TELCMD(i
));
227 printf("want DONT %d\n", i
);
230 if (my_state_is_do(i
)) {
232 printf(" DO %s\n", TELOPT(i
));
233 else if (TELCMD_OK(i
))
234 printf(" DO %s\n", TELCMD(i
));
236 printf(" DO %d\n", i
);
239 if (will_wont_resp
[i
]) {
241 printf("resp WILL_WONT %s: %d\n", TELOPT(i
), will_wont_resp
[i
]);
242 else if (TELCMD_OK(i
))
243 printf("resp WILL_WONT %s: %d\n", TELCMD(i
), will_wont_resp
[i
]);
245 printf("resp WILL_WONT %d: %d\n",
246 i
, will_wont_resp
[i
]);
247 if (my_want_state_is_will(i
)) {
249 printf("want WILL %s\n", TELOPT(i
));
250 else if (TELCMD_OK(i
))
251 printf("want WILL %s\n", TELCMD(i
));
253 printf("want WILL %d\n", i
);
256 printf("want WONT %s\n", TELOPT(i
));
257 else if (TELCMD_OK(i
))
258 printf("want WONT %s\n", TELCMD(i
));
260 printf("want WONT %d\n", i
);
263 if (my_state_is_will(i
)) {
265 printf(" WILL %s\n", TELOPT(i
));
266 else if (TELCMD_OK(i
))
267 printf(" WILL %s\n", TELCMD(i
));
269 printf(" WILL %d\n", i
);
277 printsub(char direction
, unsigned char *pointer
, int length
)
280 extern int want_status_response
;
282 if (showoptions
|| direction
== 0 ||
283 (want_status_response
&& (pointer
[0] == TELOPT_STATUS
))) {
285 fprintf(NetTrace
, "%s IAC SB ",
286 (direction
== '<')? "RCVD":"SENT");
290 i
= pointer
[length
-2];
291 j
= pointer
[length
-1];
293 if (i
!= IAC
|| j
!= SE
) {
294 fprintf(NetTrace
, "(terminated by ");
296 fprintf(NetTrace
, "%s ", TELOPT(i
));
297 else if (TELCMD_OK(i
))
298 fprintf(NetTrace
, "%s ", TELCMD(i
));
300 fprintf(NetTrace
, "%d ", i
);
302 fprintf(NetTrace
, "%s", TELOPT(j
));
303 else if (TELCMD_OK(j
))
304 fprintf(NetTrace
, "%s", TELCMD(j
));
306 fprintf(NetTrace
, "%d", j
);
307 fprintf(NetTrace
, ", not IAC SE!) ");
313 fprintf(NetTrace
, "(Empty suboption??\?)");
314 if (NetTrace
== stdout
)
318 switch (pointer
[0]) {
320 fprintf(NetTrace
, "TERMINAL-TYPE ");
321 switch (pointer
[1]) {
323 fprintf(NetTrace
, "IS \"%.*s\"", length
-2, (char *)pointer
+2);
326 fprintf(NetTrace
, "SEND");
330 "- unknown qualifier %d (0x%x).",
331 pointer
[1], pointer
[1]);
335 fprintf(NetTrace
, "TERMINAL-SPEED");
337 fprintf(NetTrace
, " (empty suboption??\?)");
340 switch (pointer
[1]) {
342 fprintf(NetTrace
, " IS ");
343 fprintf(NetTrace
, "%.*s", length
-2, (char *)pointer
+2);
347 fprintf(NetTrace
, " SEND");
349 fprintf(NetTrace
, " %d (unknown)", pointer
[1]);
350 for (i
= 2; i
< length
; i
++)
351 fprintf(NetTrace
, " ?%d?", pointer
[i
]);
357 fprintf(NetTrace
, "TOGGLE-FLOW-CONTROL");
359 fprintf(NetTrace
, " (empty suboption??\?)");
362 switch (pointer
[1]) {
364 fprintf(NetTrace
, " OFF"); break;
366 fprintf(NetTrace
, " ON"); break;
367 case LFLOW_RESTART_ANY
:
368 fprintf(NetTrace
, " RESTART-ANY"); break;
369 case LFLOW_RESTART_XON
:
370 fprintf(NetTrace
, " RESTART-XON"); break;
372 fprintf(NetTrace
, " %d (unknown)", pointer
[1]);
374 for (i
= 2; i
< length
; i
++)
375 fprintf(NetTrace
, " ?%d?", pointer
[i
]);
379 fprintf(NetTrace
, "NAWS");
381 fprintf(NetTrace
, " (empty suboption??\?)");
385 fprintf(NetTrace
, " ?%d?", pointer
[1]);
388 fprintf(NetTrace
, " %d %d (%d)",
389 pointer
[1], pointer
[2],
390 (int)((((unsigned int)pointer
[1])<<8)|((unsigned int)pointer
[2])));
392 fprintf(NetTrace
, " ?%d?", pointer
[3]);
395 fprintf(NetTrace
, " %d %d (%d)",
396 pointer
[3], pointer
[4],
397 (int)((((unsigned int)pointer
[3])<<8)|((unsigned int)pointer
[4])));
398 for (i
= 5; i
< length
; i
++)
399 fprintf(NetTrace
, " ?%d?", pointer
[i
]);
404 case TELOPT_LINEMODE
:
405 fprintf(NetTrace
, "LINEMODE ");
407 fprintf(NetTrace
, " (empty suboption??\?)");
410 switch (pointer
[1]) {
412 fprintf(NetTrace
, "WILL ");
415 fprintf(NetTrace
, "WONT ");
418 fprintf(NetTrace
, "DO ");
421 fprintf(NetTrace
, "DONT ");
424 fprintf(NetTrace
, "(no option??\?)");
427 switch (pointer
[2]) {
429 fprintf(NetTrace
, "Forward Mask");
430 for (i
= 3; i
< length
; i
++)
431 fprintf(NetTrace
, " %x", pointer
[i
]);
434 fprintf(NetTrace
, "%d (unknown)", pointer
[2]);
435 for (i
= 3; i
< length
; i
++)
436 fprintf(NetTrace
, " %d", pointer
[i
]);
442 fprintf(NetTrace
, "SLC");
443 for (i
= 2; i
< length
- 2; i
+= 3) {
444 if (SLC_NAME_OK(pointer
[i
+SLC_FUNC
]))
445 fprintf(NetTrace
, " %s", SLC_NAME(pointer
[i
+SLC_FUNC
]));
447 fprintf(NetTrace
, " %d", pointer
[i
+SLC_FUNC
]);
448 switch (pointer
[i
+SLC_FLAGS
]&SLC_LEVELBITS
) {
450 fprintf(NetTrace
, " NOSUPPORT"); break;
452 fprintf(NetTrace
, " CANTCHANGE"); break;
454 fprintf(NetTrace
, " VARIABLE"); break;
456 fprintf(NetTrace
, " DEFAULT"); break;
458 fprintf(NetTrace
, "%s%s%s",
459 pointer
[i
+SLC_FLAGS
]&SLC_ACK
? "|ACK" : "",
460 pointer
[i
+SLC_FLAGS
]&SLC_FLUSHIN
? "|FLUSHIN" : "",
461 pointer
[i
+SLC_FLAGS
]&SLC_FLUSHOUT
? "|FLUSHOUT" : "");
462 if (pointer
[i
+SLC_FLAGS
]& ~(SLC_ACK
|SLC_FLUSHIN
|
463 SLC_FLUSHOUT
| SLC_LEVELBITS
))
464 fprintf(NetTrace
, "(0x%x)", pointer
[i
+SLC_FLAGS
]);
465 fprintf(NetTrace
, " %d;", pointer
[i
+SLC_VALUE
]);
466 if ((pointer
[i
+SLC_VALUE
] == IAC
) &&
467 (pointer
[i
+SLC_VALUE
+1] == IAC
))
470 for (; i
< length
; i
++)
471 fprintf(NetTrace
, " ?%d?", pointer
[i
]);
475 fprintf(NetTrace
, "MODE ");
477 fprintf(NetTrace
, "(no mode??\?)");
482 sprintf(tbuf
, "%s%s%s%s%s",
483 pointer
[2]&MODE_EDIT
? "|EDIT" : "",
484 pointer
[2]&MODE_TRAPSIG
? "|TRAPSIG" : "",
485 pointer
[2]&MODE_SOFT_TAB
? "|SOFT_TAB" : "",
486 pointer
[2]&MODE_LIT_ECHO
? "|LIT_ECHO" : "",
487 pointer
[2]&MODE_ACK
? "|ACK" : "");
488 fprintf(NetTrace
, "%s", tbuf
[1] ? &tbuf
[1] : "0");
490 if (pointer
[2]&~(MODE_MASK
))
491 fprintf(NetTrace
, " (0x%x)", pointer
[2]);
492 for (i
= 3; i
< length
; i
++)
493 fprintf(NetTrace
, " ?0x%x?", pointer
[i
]);
496 fprintf(NetTrace
, "%d (unknown)", pointer
[1]);
497 for (i
= 2; i
< length
; i
++)
498 fprintf(NetTrace
, " %d", pointer
[i
]);
502 case TELOPT_STATUS
: {
506 fprintf(NetTrace
, "STATUS");
508 switch (pointer
[1]) {
510 if (pointer
[1] == TELQUAL_SEND
)
511 fprintf(NetTrace
, " SEND");
513 fprintf(NetTrace
, " %d (unknown)", pointer
[1]);
514 for (i
= 2; i
< length
; i
++)
515 fprintf(NetTrace
, " ?%d?", pointer
[i
]);
518 if (--want_status_response
< 0)
519 want_status_response
= 0;
520 if (NetTrace
== stdout
)
521 fprintf(NetTrace
, " IS\r\n");
523 fprintf(NetTrace
, " IS\n");
525 for (i
= 2; i
< length
; i
++) {
527 case DO
: cp
= "DO"; goto common2
;
528 case DONT
: cp
= "DONT"; goto common2
;
529 case WILL
: cp
= "WILL"; goto common2
;
530 case WONT
: cp
= "WONT"; goto common2
;
533 if (TELOPT_OK((int)pointer
[i
]))
534 fprintf(NetTrace
, " %s %s", cp
, TELOPT(pointer
[i
]));
536 fprintf(NetTrace
, " %s %d", cp
, pointer
[i
]);
538 if (NetTrace
== stdout
)
539 fprintf(NetTrace
, "\r\n");
541 fprintf(NetTrace
, "\n");
545 fprintf(NetTrace
, " SB ");
549 if (pointer
[j
] == SE
) {
552 if (pointer
[j
+1] == SE
)
557 pointer
[k
++] = pointer
[j
++];
559 printsub(0, &pointer
[i
], k
- i
);
561 fprintf(NetTrace
, " SE");
566 if (NetTrace
== stdout
)
567 fprintf(NetTrace
, "\r\n");
569 fprintf(NetTrace
, "\n");
574 fprintf(NetTrace
, " %d", pointer
[i
]);
583 case TELOPT_XDISPLOC
:
584 fprintf(NetTrace
, "X-DISPLAY-LOCATION ");
585 switch (pointer
[1]) {
587 fprintf(NetTrace
, "IS \"%.*s\"", length
-2, (char *)pointer
+2);
590 fprintf(NetTrace
, "SEND");
593 fprintf(NetTrace
, "- unknown qualifier %d (0x%x).",
594 pointer
[1], pointer
[1]);
598 case TELOPT_NEW_ENVIRON
:
599 fprintf(NetTrace
, "NEW-ENVIRON ");
602 case TELOPT_OLD_ENVIRON
:
603 fprintf(NetTrace
, "OLD-ENVIRON");
606 switch (pointer
[1]) {
608 fprintf(NetTrace
, "IS ");
611 fprintf(NetTrace
, "SEND ");
614 fprintf(NetTrace
, "INFO ");
618 #if defined(ENV_HACK) && defined(OLD_ENVIRON)
619 extern int old_env_var
, old_env_value
;
621 for (i
= 2; i
< length
; i
++ ) {
622 switch (pointer
[i
]) {
625 /* case NEW_ENV_OVAR: */
626 if (pointer
[0] == TELOPT_OLD_ENVIRON
) {
628 if (old_env_var
== OLD_ENV_VALUE
)
629 fprintf(NetTrace
, "\" (VALUE) " + noquote
);
632 fprintf(NetTrace
, "\" VAR " + noquote
);
634 #endif /* OLD_ENVIRON */
635 fprintf(NetTrace
, "\" VALUE " + noquote
);
641 /* case OLD_ENV_VALUE: */
642 if (pointer
[0] == TELOPT_OLD_ENVIRON
) {
644 if (old_env_value
== OLD_ENV_VAR
)
645 fprintf(NetTrace
, "\" (VAR) " + noquote
);
648 fprintf(NetTrace
, "\" VALUE " + noquote
);
650 #endif /* OLD_ENVIRON */
651 fprintf(NetTrace
, "\" VAR " + noquote
);
656 fprintf(NetTrace
, "\" ESC " + noquote
);
661 fprintf(NetTrace
, "\" USERVAR " + noquote
);
666 if (isprint(pointer
[i
]) && pointer
[i
] != '"') {
671 putc(pointer
[i
], NetTrace
);
673 fprintf(NetTrace
, "\" %03o " + noquote
,
688 if (TELOPT_OK(pointer
[0]))
689 fprintf(NetTrace
, "%s (unknown)", TELOPT(pointer
[0]));
691 fprintf(NetTrace
, "%d (unknown)", pointer
[0]);
692 for (i
= 1; i
< length
; i
++)
693 fprintf(NetTrace
, " %d", pointer
[i
]);
697 if (NetTrace
== stdout
)
698 fprintf(NetTrace
, "\r\n");
700 fprintf(NetTrace
, "\n");
702 if (NetTrace
== stdout
)
707 /* EmptyTerminal - called to make sure that the terminal buffer is empty.
708 * Note that we consider the buffer to run all the
709 * way to the kernel (thus the select).
719 if (TTYBYTES() == 0) {
721 (void) select(tout
+1, (fd_set
*) 0, &o
, (fd_set
*) 0,
722 (struct timeval
*) 0); /* wait for TTLOWAT */
727 (void) select(tout
+1, (fd_set
*) 0, &o
, (fd_set
*) 0,
728 (struct timeval
*) 0); /* wait for TTLOWAT */
738 (void)telrcv(); /* Process any incoming data */
740 } while (ring_full_count(&netiring
)); /* While there is any */
745 EmptyTerminal(); /* Flush the path to the tty */
757 ExitString(const char *string
, int returnCode
)
760 fwrite(string
, 1, strlen(string
), stderr
);