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.3 2007/11/25 01:28:23 swildner Exp $
41 #include <arpa/telnet.h>
42 #include <sys/types.h>
43 #include <sys/socket.h>
60 FILE *NetTrace
= 0; /* Not in bss, since needs to stay */
66 * Upcase (in place) the argument.
70 upcase(char *argument
)
74 while ((c
= *argument
) != 0) {
76 *argument
= toupper(c
);
85 * Compensate for differences in 4.2 and 4.3 systems.
89 SetSockOpt(int fd
, int level
, int option
, int yesno
)
91 return setsockopt(fd
, level
, option
,
92 (char *)&yesno
, sizeof yesno
);
96 * The following are routines used to print out debugging information.
99 unsigned char NetTraceFile
[256] = "(standard output)";
102 SetNetTrace(char *file
)
104 if (NetTrace
&& NetTrace
!= stdout
)
106 if (file
&& (strcmp(file
, "-") != 0)) {
107 NetTrace
= fopen(file
, "w");
109 strcpy((char *)NetTraceFile
, file
);
112 fprintf(stderr
, "Cannot open %s.\n", file
);
115 strcpy((char *)NetTraceFile
, "(standard output)");
119 Dump(char direction
, unsigned char *buffer
, int length
)
121 # define BYTES_PER_LINE 32
122 # define min(x,y) ((x<y)? x:y)
123 unsigned char *pThis
;
130 fprintf(NetTrace
, "%c 0x%x\t", direction
, offset
);
133 buffer
= buffer
+ min(length
, BYTES_PER_LINE
/2);
134 while (pThis
< buffer
) {
135 fprintf(NetTrace
, "%c%.2x",
136 (((*pThis
)&0xff) == 0xff) ? '*' : ' ',
140 length
-= BYTES_PER_LINE
/2;
141 offset
+= BYTES_PER_LINE
/2;
143 buffer
= buffer
+ min(length
, BYTES_PER_LINE
);
144 while (pThis
< buffer
) {
145 fprintf(NetTrace
, "%.2x", (*pThis
)&0xff);
148 length
-= BYTES_PER_LINE
;
149 offset
+= BYTES_PER_LINE
;
151 if (NetTrace
== stdout
) {
152 fprintf(NetTrace
, "\r\n");
154 fprintf(NetTrace
, "\n");
160 /* find next unique line */
167 printoption(const char *direction
, int cmd
, int option
)
172 if (TELCMD_OK(option
))
173 fprintf(NetTrace
, "%s IAC %s", direction
, TELCMD(option
));
175 fprintf(NetTrace
, "%s IAC %d", direction
, option
);
178 fmt
= (cmd
== WILL
) ? "WILL" : (cmd
== WONT
) ? "WONT" :
179 (cmd
== DO
) ? "DO" : (cmd
== DONT
) ? "DONT" : 0;
181 fprintf(NetTrace
, "%s %s ", direction
, fmt
);
182 if (TELOPT_OK(option
))
183 fprintf(NetTrace
, "%s", TELOPT(option
));
184 else if (option
== TELOPT_EXOPL
)
185 fprintf(NetTrace
, "EXOPL");
187 fprintf(NetTrace
, "%d", option
);
189 fprintf(NetTrace
, "%s %d %d", direction
, cmd
, option
);
191 if (NetTrace
== stdout
) {
192 fprintf(NetTrace
, "\r\n");
195 fprintf(NetTrace
, "\n");
204 extern char will_wont_resp
[], do_dont_resp
[];
206 for (i
= 0; i
< 256; i
++) {
207 if (do_dont_resp
[i
]) {
209 printf("resp DO_DONT %s: %d\n", TELOPT(i
), do_dont_resp
[i
]);
210 else if (TELCMD_OK(i
))
211 printf("resp DO_DONT %s: %d\n", TELCMD(i
), do_dont_resp
[i
]);
213 printf("resp DO_DONT %d: %d\n", i
,
215 if (my_want_state_is_do(i
)) {
217 printf("want DO %s\n", TELOPT(i
));
218 else if (TELCMD_OK(i
))
219 printf("want DO %s\n", TELCMD(i
));
221 printf("want DO %d\n", i
);
224 printf("want DONT %s\n", TELOPT(i
));
225 else if (TELCMD_OK(i
))
226 printf("want DONT %s\n", TELCMD(i
));
228 printf("want DONT %d\n", i
);
231 if (my_state_is_do(i
)) {
233 printf(" DO %s\n", TELOPT(i
));
234 else if (TELCMD_OK(i
))
235 printf(" DO %s\n", TELCMD(i
));
237 printf(" DO %d\n", i
);
240 if (will_wont_resp
[i
]) {
242 printf("resp WILL_WONT %s: %d\n", TELOPT(i
), will_wont_resp
[i
]);
243 else if (TELCMD_OK(i
))
244 printf("resp WILL_WONT %s: %d\n", TELCMD(i
), will_wont_resp
[i
]);
246 printf("resp WILL_WONT %d: %d\n",
247 i
, will_wont_resp
[i
]);
248 if (my_want_state_is_will(i
)) {
250 printf("want WILL %s\n", TELOPT(i
));
251 else if (TELCMD_OK(i
))
252 printf("want WILL %s\n", TELCMD(i
));
254 printf("want WILL %d\n", i
);
257 printf("want WONT %s\n", TELOPT(i
));
258 else if (TELCMD_OK(i
))
259 printf("want WONT %s\n", TELCMD(i
));
261 printf("want WONT %d\n", i
);
264 if (my_state_is_will(i
)) {
266 printf(" WILL %s\n", TELOPT(i
));
267 else if (TELCMD_OK(i
))
268 printf(" WILL %s\n", TELCMD(i
));
270 printf(" WILL %d\n", i
);
278 printsub(char direction
, unsigned char *pointer
, int length
)
281 extern int want_status_response
;
283 if (showoptions
|| direction
== 0 ||
284 (want_status_response
&& (pointer
[0] == TELOPT_STATUS
))) {
286 fprintf(NetTrace
, "%s IAC SB ",
287 (direction
== '<')? "RCVD":"SENT");
291 i
= pointer
[length
-2];
292 j
= pointer
[length
-1];
294 if (i
!= IAC
|| j
!= SE
) {
295 fprintf(NetTrace
, "(terminated by ");
297 fprintf(NetTrace
, "%s ", TELOPT(i
));
298 else if (TELCMD_OK(i
))
299 fprintf(NetTrace
, "%s ", TELCMD(i
));
301 fprintf(NetTrace
, "%d ", i
);
303 fprintf(NetTrace
, "%s", TELOPT(j
));
304 else if (TELCMD_OK(j
))
305 fprintf(NetTrace
, "%s", TELCMD(j
));
307 fprintf(NetTrace
, "%d", j
);
308 fprintf(NetTrace
, ", not IAC SE!) ");
314 fprintf(NetTrace
, "(Empty suboption??\?)");
315 if (NetTrace
== stdout
)
319 switch (pointer
[0]) {
321 fprintf(NetTrace
, "TERMINAL-TYPE ");
322 switch (pointer
[1]) {
324 fprintf(NetTrace
, "IS \"%.*s\"", length
-2, (char *)pointer
+2);
327 fprintf(NetTrace
, "SEND");
331 "- unknown qualifier %d (0x%x).",
332 pointer
[1], pointer
[1]);
336 fprintf(NetTrace
, "TERMINAL-SPEED");
338 fprintf(NetTrace
, " (empty suboption??\?)");
341 switch (pointer
[1]) {
343 fprintf(NetTrace
, " IS ");
344 fprintf(NetTrace
, "%.*s", length
-2, (char *)pointer
+2);
348 fprintf(NetTrace
, " SEND");
350 fprintf(NetTrace
, " %d (unknown)", pointer
[1]);
351 for (i
= 2; i
< length
; i
++)
352 fprintf(NetTrace
, " ?%d?", pointer
[i
]);
358 fprintf(NetTrace
, "TOGGLE-FLOW-CONTROL");
360 fprintf(NetTrace
, " (empty suboption??\?)");
363 switch (pointer
[1]) {
365 fprintf(NetTrace
, " OFF"); break;
367 fprintf(NetTrace
, " ON"); break;
368 case LFLOW_RESTART_ANY
:
369 fprintf(NetTrace
, " RESTART-ANY"); break;
370 case LFLOW_RESTART_XON
:
371 fprintf(NetTrace
, " RESTART-XON"); break;
373 fprintf(NetTrace
, " %d (unknown)", pointer
[1]);
375 for (i
= 2; i
< length
; i
++)
376 fprintf(NetTrace
, " ?%d?", pointer
[i
]);
380 fprintf(NetTrace
, "NAWS");
382 fprintf(NetTrace
, " (empty suboption??\?)");
386 fprintf(NetTrace
, " ?%d?", pointer
[1]);
389 fprintf(NetTrace
, " %d %d (%d)",
390 pointer
[1], pointer
[2],
391 (int)((((unsigned int)pointer
[1])<<8)|((unsigned int)pointer
[2])));
393 fprintf(NetTrace
, " ?%d?", pointer
[3]);
396 fprintf(NetTrace
, " %d %d (%d)",
397 pointer
[3], pointer
[4],
398 (int)((((unsigned int)pointer
[3])<<8)|((unsigned int)pointer
[4])));
399 for (i
= 5; i
< length
; i
++)
400 fprintf(NetTrace
, " ?%d?", pointer
[i
]);
405 case TELOPT_LINEMODE
:
406 fprintf(NetTrace
, "LINEMODE ");
408 fprintf(NetTrace
, " (empty suboption??\?)");
411 switch (pointer
[1]) {
413 fprintf(NetTrace
, "WILL ");
416 fprintf(NetTrace
, "WONT ");
419 fprintf(NetTrace
, "DO ");
422 fprintf(NetTrace
, "DONT ");
425 fprintf(NetTrace
, "(no option??\?)");
428 switch (pointer
[2]) {
430 fprintf(NetTrace
, "Forward Mask");
431 for (i
= 3; i
< length
; i
++)
432 fprintf(NetTrace
, " %x", pointer
[i
]);
435 fprintf(NetTrace
, "%d (unknown)", pointer
[2]);
436 for (i
= 3; i
< length
; i
++)
437 fprintf(NetTrace
, " %d", pointer
[i
]);
443 fprintf(NetTrace
, "SLC");
444 for (i
= 2; i
< length
- 2; i
+= 3) {
445 if (SLC_NAME_OK(pointer
[i
+SLC_FUNC
]))
446 fprintf(NetTrace
, " %s", SLC_NAME(pointer
[i
+SLC_FUNC
]));
448 fprintf(NetTrace
, " %d", pointer
[i
+SLC_FUNC
]);
449 switch (pointer
[i
+SLC_FLAGS
]&SLC_LEVELBITS
) {
451 fprintf(NetTrace
, " NOSUPPORT"); break;
453 fprintf(NetTrace
, " CANTCHANGE"); break;
455 fprintf(NetTrace
, " VARIABLE"); break;
457 fprintf(NetTrace
, " DEFAULT"); break;
459 fprintf(NetTrace
, "%s%s%s",
460 pointer
[i
+SLC_FLAGS
]&SLC_ACK
? "|ACK" : "",
461 pointer
[i
+SLC_FLAGS
]&SLC_FLUSHIN
? "|FLUSHIN" : "",
462 pointer
[i
+SLC_FLAGS
]&SLC_FLUSHOUT
? "|FLUSHOUT" : "");
463 if (pointer
[i
+SLC_FLAGS
]& ~(SLC_ACK
|SLC_FLUSHIN
|
464 SLC_FLUSHOUT
| SLC_LEVELBITS
))
465 fprintf(NetTrace
, "(0x%x)", pointer
[i
+SLC_FLAGS
]);
466 fprintf(NetTrace
, " %d;", pointer
[i
+SLC_VALUE
]);
467 if ((pointer
[i
+SLC_VALUE
] == IAC
) &&
468 (pointer
[i
+SLC_VALUE
+1] == IAC
))
471 for (; i
< length
; i
++)
472 fprintf(NetTrace
, " ?%d?", pointer
[i
]);
476 fprintf(NetTrace
, "MODE ");
478 fprintf(NetTrace
, "(no mode??\?)");
483 sprintf(tbuf
, "%s%s%s%s%s",
484 pointer
[2]&MODE_EDIT
? "|EDIT" : "",
485 pointer
[2]&MODE_TRAPSIG
? "|TRAPSIG" : "",
486 pointer
[2]&MODE_SOFT_TAB
? "|SOFT_TAB" : "",
487 pointer
[2]&MODE_LIT_ECHO
? "|LIT_ECHO" : "",
488 pointer
[2]&MODE_ACK
? "|ACK" : "");
489 fprintf(NetTrace
, "%s", tbuf
[1] ? &tbuf
[1] : "0");
491 if (pointer
[2]&~(MODE_MASK
))
492 fprintf(NetTrace
, " (0x%x)", pointer
[2]);
493 for (i
= 3; i
< length
; i
++)
494 fprintf(NetTrace
, " ?0x%x?", pointer
[i
]);
497 fprintf(NetTrace
, "%d (unknown)", pointer
[1]);
498 for (i
= 2; i
< length
; i
++)
499 fprintf(NetTrace
, " %d", pointer
[i
]);
503 case TELOPT_STATUS
: {
507 fprintf(NetTrace
, "STATUS");
509 switch (pointer
[1]) {
511 if (pointer
[1] == TELQUAL_SEND
)
512 fprintf(NetTrace
, " SEND");
514 fprintf(NetTrace
, " %d (unknown)", pointer
[1]);
515 for (i
= 2; i
< length
; i
++)
516 fprintf(NetTrace
, " ?%d?", pointer
[i
]);
519 if (--want_status_response
< 0)
520 want_status_response
= 0;
521 if (NetTrace
== stdout
)
522 fprintf(NetTrace
, " IS\r\n");
524 fprintf(NetTrace
, " IS\n");
526 for (i
= 2; i
< length
; i
++) {
528 case DO
: cp
= "DO"; goto common2
;
529 case DONT
: cp
= "DONT"; goto common2
;
530 case WILL
: cp
= "WILL"; goto common2
;
531 case WONT
: cp
= "WONT"; goto common2
;
534 if (TELOPT_OK((int)pointer
[i
]))
535 fprintf(NetTrace
, " %s %s", cp
, TELOPT(pointer
[i
]));
537 fprintf(NetTrace
, " %s %d", cp
, pointer
[i
]);
539 if (NetTrace
== stdout
)
540 fprintf(NetTrace
, "\r\n");
542 fprintf(NetTrace
, "\n");
546 fprintf(NetTrace
, " SB ");
550 if (pointer
[j
] == SE
) {
553 if (pointer
[j
+1] == SE
)
558 pointer
[k
++] = pointer
[j
++];
560 printsub(0, &pointer
[i
], k
- i
);
562 fprintf(NetTrace
, " SE");
567 if (NetTrace
== stdout
)
568 fprintf(NetTrace
, "\r\n");
570 fprintf(NetTrace
, "\n");
575 fprintf(NetTrace
, " %d", pointer
[i
]);
584 case TELOPT_XDISPLOC
:
585 fprintf(NetTrace
, "X-DISPLAY-LOCATION ");
586 switch (pointer
[1]) {
588 fprintf(NetTrace
, "IS \"%.*s\"", length
-2, (char *)pointer
+2);
591 fprintf(NetTrace
, "SEND");
594 fprintf(NetTrace
, "- unknown qualifier %d (0x%x).",
595 pointer
[1], pointer
[1]);
599 case TELOPT_NEW_ENVIRON
:
600 fprintf(NetTrace
, "NEW-ENVIRON ");
603 case TELOPT_OLD_ENVIRON
:
604 fprintf(NetTrace
, "OLD-ENVIRON");
607 switch (pointer
[1]) {
609 fprintf(NetTrace
, "IS ");
612 fprintf(NetTrace
, "SEND ");
615 fprintf(NetTrace
, "INFO ");
619 #if defined(ENV_HACK) && defined(OLD_ENVIRON)
620 extern int old_env_var
, old_env_value
;
622 for (i
= 2; i
< length
; i
++ ) {
623 switch (pointer
[i
]) {
626 /* case NEW_ENV_OVAR: */
627 if (pointer
[0] == TELOPT_OLD_ENVIRON
) {
629 if (old_env_var
== OLD_ENV_VALUE
)
630 fprintf(NetTrace
, "\" (VALUE) " + noquote
);
633 fprintf(NetTrace
, "\" VAR " + noquote
);
635 #endif /* OLD_ENVIRON */
636 fprintf(NetTrace
, "\" VALUE " + noquote
);
642 /* case OLD_ENV_VALUE: */
643 if (pointer
[0] == TELOPT_OLD_ENVIRON
) {
645 if (old_env_value
== OLD_ENV_VAR
)
646 fprintf(NetTrace
, "\" (VAR) " + noquote
);
649 fprintf(NetTrace
, "\" VALUE " + noquote
);
651 #endif /* OLD_ENVIRON */
652 fprintf(NetTrace
, "\" VAR " + noquote
);
657 fprintf(NetTrace
, "\" ESC " + noquote
);
662 fprintf(NetTrace
, "\" USERVAR " + noquote
);
667 if (isprint(pointer
[i
]) && pointer
[i
] != '"') {
672 putc(pointer
[i
], NetTrace
);
674 fprintf(NetTrace
, "\" %03o " + noquote
,
689 if (TELOPT_OK(pointer
[0]))
690 fprintf(NetTrace
, "%s (unknown)", TELOPT(pointer
[0]));
692 fprintf(NetTrace
, "%d (unknown)", pointer
[0]);
693 for (i
= 1; i
< length
; i
++)
694 fprintf(NetTrace
, " %d", pointer
[i
]);
698 if (NetTrace
== stdout
)
699 fprintf(NetTrace
, "\r\n");
701 fprintf(NetTrace
, "\n");
703 if (NetTrace
== stdout
)
708 /* EmptyTerminal - called to make sure that the terminal buffer is empty.
709 * Note that we consider the buffer to run all the
710 * way to the kernel (thus the select).
720 if (TTYBYTES() == 0) {
722 (void) select(tout
+1, (fd_set
*) 0, &o
, (fd_set
*) 0,
723 (struct timeval
*) 0); /* wait for TTLOWAT */
728 (void) select(tout
+1, (fd_set
*) 0, &o
, (fd_set
*) 0,
729 (struct timeval
*) 0); /* wait for TTLOWAT */
739 (void)telrcv(); /* Process any incoming data */
741 } while (ring_full_count(&netiring
)); /* While there is any */
746 EmptyTerminal(); /* Flush the path to the tty */
758 ExitString(const char *string
, int returnCode
)
761 fwrite(string
, 1, strlen(string
), stderr
);