Home | History | Annotate | Line # | Download | only in telnet
      1 /*	$NetBSD: utilities.c,v 1.29 2019/01/05 06:47:24 maya Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1988, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 #if 0
     35 static char sccsid[] = "@(#)utilities.c	8.3 (Berkeley) 5/30/95";
     36 #else
     37 __RCSID("$NetBSD: utilities.c,v 1.29 2019/01/05 06:47:24 maya Exp $");
     38 #endif
     39 #endif /* not lint */
     40 
     41 #define	TELOPTS
     42 #define	TELCMDS
     43 #define	SLC_NAMES
     44 #include <arpa/telnet.h>
     45 #include <sys/types.h>
     46 #include <sys/time.h>
     47 #include <sys/socket.h>
     48 #include <unistd.h>
     49 #include <poll.h>
     50 
     51 #include <ctype.h>
     52 
     53 #include "general.h"
     54 #include "ring.h"
     55 #include "defines.h"
     56 #include "externs.h"
     57 
     58 
     59 #ifdef AUTHENTICATION
     60 #include <libtelnet/auth.h>
     61 #endif
     62 #ifdef ENCRYPTION
     63 #include <libtelnet/encrypt.h>
     64 #endif
     65 
     66 static void SetForExit(void);
     67 
     68 FILE	*NetTrace = 0;		/* Not in bss, since needs to stay */
     69 int	prettydump;
     70 
     71 /*
     72  * upcase()
     73  *
     74  *	Upcase (in place) the argument.
     75  */
     76 
     77 void
     78 upcase(char *argument)
     79 {
     80     unsigned char c;
     81 
     82     while ((c = *argument) != 0) {
     83 	if (islower(c)) {
     84 	    *argument = toupper(c);
     85 	}
     86 	argument++;
     87     }
     88 }
     89 
     90 /*
     91  * SetSockOpt()
     92  *
     93  * Compensate for differences in 4.2 and 4.3 systems.
     94  */
     95 
     96 int
     97 SetSockOpt(int fd, int level, int option, int yesno)
     98 {
     99     return setsockopt(fd, level, option, (char *)&yesno, sizeof yesno);
    100 }
    101 
    102 /*
    104  * The following are routines used to print out debugging information.
    105  */
    106 
    107 char NetTraceFile[256] = "(standard output)";
    108 
    109 void
    110 SetNetTrace(const char *file)
    111 {
    112     if (NetTrace && NetTrace != stdout)
    113 	fclose(NetTrace);
    114     if (file  && (strcmp(file, "-") != 0)) {
    115 	NetTrace = fopen(file, "w");
    116 	if (NetTrace) {
    117 	    strlcpy(NetTraceFile, file, sizeof(NetTraceFile));
    118 	    return;
    119 	}
    120 	fprintf(stderr, "Cannot open %s.\n", file);
    121     }
    122     NetTrace = stdout;
    123     strlcpy(NetTraceFile, "(standard output)", sizeof(NetTraceFile));
    124 }
    125 
    126 void
    127 Dump(int direction, unsigned char *buffer, int length)
    128 {
    129 #   define BYTES_PER_LINE	32
    130 #   define min(x,y)	((x<y)? x:y)
    131     unsigned char *pThis;
    132     int offset;
    133 
    134     offset = 0;
    135 
    136     while (length) {
    137 	/* print one line */
    138 	fprintf(NetTrace, "%c 0x%x\t", direction, offset);
    139 	pThis = buffer;
    140 	if (prettydump) {
    141 	    buffer = buffer + min(length, BYTES_PER_LINE/2);
    142 	    while (pThis < buffer) {
    143 		fprintf(NetTrace, "%c%.2x",
    144 		    (((*pThis)&0xff) == 0xff) ? '*' : ' ',
    145 		    (*pThis)&0xff);
    146 		pThis++;
    147 	    }
    148 	    length -= BYTES_PER_LINE/2;
    149 	    offset += BYTES_PER_LINE/2;
    150 	} else {
    151 	    buffer = buffer + min(length, BYTES_PER_LINE);
    152 	    while (pThis < buffer) {
    153 		fprintf(NetTrace, "%.2x", (*pThis)&0xff);
    154 		pThis++;
    155 	    }
    156 	    length -= BYTES_PER_LINE;
    157 	    offset += BYTES_PER_LINE;
    158 	}
    159 	if (NetTrace == stdout) {
    160 	    fprintf(NetTrace, "\r\n");
    161 	} else {
    162 	    fprintf(NetTrace, "\n");
    163 	}
    164 	if (length < 0) {
    165 	    fflush(NetTrace);
    166 	    return;
    167 	}
    168 	/* find next unique line */
    169     }
    170     fflush(NetTrace);
    171 }
    172 
    173 
    174 void
    175 printoption(const char *direction, int cmd, int option)
    176 {
    177 	if (!showoptions)
    178 		return;
    179 	if (cmd == IAC) {
    180 		if (TELCMD_OK(option))
    181 		    fprintf(NetTrace, "%s IAC %s", direction, TELCMD(option));
    182 		else
    183 		    fprintf(NetTrace, "%s IAC %d", direction, option);
    184 	} else {
    185 		const char *fmt;
    186 		fmt = (cmd == WILL) ? "WILL" : (cmd == WONT) ? "WONT" :
    187 			(cmd == DO) ? "DO" : (cmd == DONT) ? "DONT" : 0;
    188 		if (fmt) {
    189 		    fprintf(NetTrace, "%s %s ", direction, fmt);
    190 		    if (TELOPT_OK(option))
    191 			fprintf(NetTrace, "%s", TELOPT(option));
    192 		    else if (option == TELOPT_EXOPL)
    193 			fprintf(NetTrace, "EXOPL");
    194 		    else
    195 			fprintf(NetTrace, "%d", option);
    196 		} else
    197 		    fprintf(NetTrace, "%s %d %d", direction, cmd, option);
    198 	}
    199 	if (NetTrace == stdout) {
    200 	    fprintf(NetTrace, "\r\n");
    201 	    fflush(NetTrace);
    202 	} else {
    203 	    fprintf(NetTrace, "\n");
    204 	}
    205 	return;
    206 }
    207 
    208 void
    209 optionstatus(void)
    210 {
    211     int i;
    212     extern char will_wont_resp[], do_dont_resp[];
    213 
    214     for (i = 0; i < 256; i++) {
    215 	if (do_dont_resp[i]) {
    216 	    if (TELOPT_OK(i))
    217 		printf("resp DO_DONT %s: %d\n", TELOPT(i), do_dont_resp[i]);
    218 	    else if (TELCMD_OK(i))
    219 		printf("resp DO_DONT %s: %d\n", TELCMD(i), do_dont_resp[i]);
    220 	    else
    221 		printf("resp DO_DONT %d: %d\n", i,
    222 				do_dont_resp[i]);
    223 	    if (my_want_state_is_do(i)) {
    224 		if (TELOPT_OK(i))
    225 		    printf("want DO   %s\n", TELOPT(i));
    226 		else if (TELCMD_OK(i))
    227 		    printf("want DO   %s\n", TELCMD(i));
    228 		else
    229 		    printf("want DO   %d\n", i);
    230 	    } else {
    231 		if (TELOPT_OK(i))
    232 		    printf("want DONT %s\n", TELOPT(i));
    233 		else if (TELCMD_OK(i))
    234 		    printf("want DONT %s\n", TELCMD(i));
    235 		else
    236 		    printf("want DONT %d\n", i);
    237 	    }
    238 	} else {
    239 	    if (my_state_is_do(i)) {
    240 		if (TELOPT_OK(i))
    241 		    printf("     DO   %s\n", TELOPT(i));
    242 		else if (TELCMD_OK(i))
    243 		    printf("     DO   %s\n", TELCMD(i));
    244 		else
    245 		    printf("     DO   %d\n", i);
    246 	    }
    247 	}
    248 	if (will_wont_resp[i]) {
    249 	    if (TELOPT_OK(i))
    250 		printf("resp WILL_WONT %s: %d\n", TELOPT(i), will_wont_resp[i]);
    251 	    else if (TELCMD_OK(i))
    252 		printf("resp WILL_WONT %s: %d\n", TELCMD(i), will_wont_resp[i]);
    253 	    else
    254 		printf("resp WILL_WONT %d: %d\n",
    255 				i, will_wont_resp[i]);
    256 	    if (my_want_state_is_will(i)) {
    257 		if (TELOPT_OK(i))
    258 		    printf("want WILL %s\n", TELOPT(i));
    259 		else if (TELCMD_OK(i))
    260 		    printf("want WILL %s\n", TELCMD(i));
    261 		else
    262 		    printf("want WILL %d\n", i);
    263 	    } else {
    264 		if (TELOPT_OK(i))
    265 		    printf("want WONT %s\n", TELOPT(i));
    266 		else if (TELCMD_OK(i))
    267 		    printf("want WONT %s\n", TELCMD(i));
    268 		else
    269 		    printf("want WONT %d\n", i);
    270 	    }
    271 	} else {
    272 	    if (my_state_is_will(i)) {
    273 		if (TELOPT_OK(i))
    274 		    printf("     WILL %s\n", TELOPT(i));
    275 		else if (TELCMD_OK(i))
    276 		    printf("     WILL %s\n", TELCMD(i));
    277 		else
    278 		    printf("     WILL %d\n", i);
    279 	    }
    280 	}
    281     }
    282 
    283 }
    284 
    285 void
    286 printsub(
    287     int direction,	/* '<' or '>' */
    288     unsigned char *pointer,	/* where suboption data sits */
    289     int		  length)	/* length of suboption data */
    290 {
    291     int i;
    292 #ifdef	ENCRYPTION
    293     char buf[512];
    294 #endif	/* ENCRYPTION */
    295     extern int want_status_response;
    296 
    297     if (showoptions || direction == 0 ||
    298 	(want_status_response && (pointer[0] == TELOPT_STATUS))) {
    299 	if (direction) {
    300 	    fprintf(NetTrace, "%s IAC SB ",
    301 				(direction == '<')? "RCVD":"SENT");
    302 	    if (length >= 3) {
    303 		int j;
    304 
    305 		i = pointer[length-2];
    306 		j = pointer[length-1];
    307 
    308 		if (i != IAC || j != SE) {
    309 		    fprintf(NetTrace, "(terminated by ");
    310 		    if (TELOPT_OK(i))
    311 			fprintf(NetTrace, "%s ", TELOPT(i));
    312 		    else if (TELCMD_OK(i))
    313 			fprintf(NetTrace, "%s ", TELCMD(i));
    314 		    else
    315 			fprintf(NetTrace, "%d ", i);
    316 		    if (TELOPT_OK(j))
    317 			fprintf(NetTrace, "%s", TELOPT(j));
    318 		    else if (TELCMD_OK(j))
    319 			fprintf(NetTrace, "%s", TELCMD(j));
    320 		    else
    321 			fprintf(NetTrace, "%d", j);
    322 		    fprintf(NetTrace, ", not IAC SE!) ");
    323 		}
    324 	    }
    325 	    length -= 2;
    326 	}
    327 	if (length < 1) {
    328 	    fprintf(NetTrace, "(Empty suboption??\?)");
    329 	    if (NetTrace == stdout)
    330 		fflush(NetTrace);
    331 	    return;
    332 	}
    333 	switch (pointer[0]) {
    334 	case TELOPT_TTYPE:
    335 	    fprintf(NetTrace, "TERMINAL-TYPE ");
    336 	    switch (pointer[1]) {
    337 	    case TELQUAL_IS:
    338 		fprintf(NetTrace, "IS \"%.*s\"", length-2, (char *)pointer+2);
    339 		break;
    340 	    case TELQUAL_SEND:
    341 		fprintf(NetTrace, "SEND");
    342 		break;
    343 	    default:
    344 		fprintf(NetTrace,
    345 				"- unknown qualifier %d (0x%x).",
    346 				pointer[1], pointer[1]);
    347 	    }
    348 	    break;
    349 	case TELOPT_TSPEED:
    350 	    fprintf(NetTrace, "TERMINAL-SPEED");
    351 	    if (length < 2) {
    352 		fprintf(NetTrace, " (empty suboption??\?)");
    353 		break;
    354 	    }
    355 	    switch (pointer[1]) {
    356 	    case TELQUAL_IS:
    357 		fprintf(NetTrace, " IS ");
    358 		fprintf(NetTrace, "%.*s", length-2, (char *)pointer+2);
    359 		break;
    360 	    default:
    361 		if (pointer[1] == 1)
    362 		    fprintf(NetTrace, " SEND");
    363 		else
    364 		    fprintf(NetTrace, " %d (unknown)", pointer[1]);
    365 		for (i = 2; i < length; i++)
    366 		    fprintf(NetTrace, " ?%d?", pointer[i]);
    367 		break;
    368 	    }
    369 	    break;
    370 
    371 	case TELOPT_LFLOW:
    372 	    fprintf(NetTrace, "TOGGLE-FLOW-CONTROL");
    373 	    if (length < 2) {
    374 		fprintf(NetTrace, " (empty suboption??\?)");
    375 		break;
    376 	    }
    377 	    switch (pointer[1]) {
    378 	    case LFLOW_OFF:
    379 		fprintf(NetTrace, " OFF"); break;
    380 	    case LFLOW_ON:
    381 		fprintf(NetTrace, " ON"); break;
    382 	    case LFLOW_RESTART_ANY:
    383 		fprintf(NetTrace, " RESTART-ANY"); break;
    384 	    case LFLOW_RESTART_XON:
    385 		fprintf(NetTrace, " RESTART-XON"); break;
    386 	    default:
    387 		fprintf(NetTrace, " %d (unknown)", pointer[1]);
    388 	    }
    389 	    for (i = 2; i < length; i++)
    390 		fprintf(NetTrace, " ?%d?", pointer[i]);
    391 	    break;
    392 
    393 	case TELOPT_NAWS:
    394 	    fprintf(NetTrace, "NAWS");
    395 	    if (length < 2) {
    396 		fprintf(NetTrace, " (empty suboption??\?)");
    397 		break;
    398 	    }
    399 	    if (length == 2) {
    400 		fprintf(NetTrace, " ?%d?", pointer[1]);
    401 		break;
    402 	    }
    403 	    fprintf(NetTrace, " %d %d (%d)",
    404 		pointer[1], pointer[2],
    405 		(int)((((unsigned int)pointer[1])<<8)|((unsigned int)pointer[2])));
    406 	    if (length == 4) {
    407 		fprintf(NetTrace, " ?%d?", pointer[3]);
    408 		break;
    409 	    }
    410 	    fprintf(NetTrace, " %d %d (%d)",
    411 		pointer[3], pointer[4],
    412 		(int)((((unsigned int)pointer[3])<<8)|((unsigned int)pointer[4])));
    413 	    for (i = 5; i < length; i++)
    414 		fprintf(NetTrace, " ?%d?", pointer[i]);
    415 	    break;
    416 
    417 #ifdef AUTHENTICATION
    418 	case TELOPT_AUTHENTICATION:
    419 	    fprintf(NetTrace, "AUTHENTICATION");
    420 	    if (length < 2) {
    421 		fprintf(NetTrace, " (empty suboption??\?)");
    422 		break;
    423 	    }
    424 	    switch (pointer[1]) {
    425 	    case TELQUAL_REPLY:
    426 	    case TELQUAL_IS:
    427 		fprintf(NetTrace, " %s ", (pointer[1] == TELQUAL_IS) ?
    428 							"IS" : "REPLY");
    429 		if (AUTHTYPE_NAME_OK(pointer[2]))
    430 		    fprintf(NetTrace, "%s ", AUTHTYPE_NAME(pointer[2]));
    431 		else
    432 		    fprintf(NetTrace, "%d ", pointer[2]);
    433 		if (length < 3) {
    434 		    fprintf(NetTrace, "(partial suboption??\?)");
    435 		    break;
    436 		}
    437 		fprintf(NetTrace, "%s|%s",
    438 			((pointer[3] & AUTH_WHO_MASK) == AUTH_WHO_CLIENT) ?
    439 			"CLIENT" : "SERVER",
    440 			((pointer[3] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) ?
    441 			"MUTUAL" : "ONE-WAY");
    442 
    443 		auth_printsub(&pointer[1], length - 1, buf, sizeof(buf));
    444 		fprintf(NetTrace, "%s", buf);
    445 		break;
    446 
    447 	    case TELQUAL_SEND:
    448 		i = 2;
    449 		fprintf(NetTrace, " SEND ");
    450 		while (i < length) {
    451 		    if (AUTHTYPE_NAME_OK(pointer[i]))
    452 			fprintf(NetTrace, "%s ", AUTHTYPE_NAME(pointer[i]));
    453 		    else
    454 			fprintf(NetTrace, "%d ", pointer[i]);
    455 		    if (++i >= length) {
    456 			fprintf(NetTrace, "(partial suboption??\?)");
    457 			break;
    458 		    }
    459 		    fprintf(NetTrace, "%s|%s ",
    460 			((pointer[i] & AUTH_WHO_MASK) == AUTH_WHO_CLIENT) ?
    461 							"CLIENT" : "SERVER",
    462 			((pointer[i] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) ?
    463 							"MUTUAL" : "ONE-WAY");
    464 		    ++i;
    465 		}
    466 		break;
    467 
    468 	    case TELQUAL_NAME:
    469 		i = 2;
    470 		fprintf(NetTrace, " NAME \"");
    471 		while (i < length)
    472 		    putc(pointer[i++], NetTrace);
    473 		putc('"', NetTrace);
    474 		break;
    475 
    476 	    default:
    477 		    for (i = 2; i < length; i++)
    478 			fprintf(NetTrace, " ?%d?", pointer[i]);
    479 		    break;
    480 	    }
    481 	    break;
    482 #endif
    483 
    484 #ifdef	ENCRYPTION
    485 	case TELOPT_ENCRYPT:
    486 		fprintf(NetTrace, "ENCRYPT");
    487 		if (length < 2) {
    488 			fprintf(NetTrace, " (empty suboption??\?)");
    489 			break;
    490 		}
    491 		switch (pointer[1]) {
    492 		case ENCRYPT_START:
    493 			fprintf(NetTrace, " START");
    494 			break;
    495 
    496 		case ENCRYPT_END:
    497 			fprintf(NetTrace, " END");
    498 			break;
    499 
    500 		case ENCRYPT_REQSTART:
    501 			fprintf(NetTrace, " REQUEST-START");
    502 			break;
    503 
    504 		case ENCRYPT_REQEND:
    505 			fprintf(NetTrace, " REQUEST-END");
    506 			break;
    507 
    508 		case ENCRYPT_IS:
    509 		case ENCRYPT_REPLY:
    510 			fprintf(NetTrace, " %s ", (pointer[1] == ENCRYPT_IS) ?
    511 			    "IS" : "REPLY");
    512 			if (length < 3) {
    513 				fprintf(NetTrace, " (partial suboption??\?)");
    514 				break;
    515 			}
    516 			if (ENCTYPE_NAME_OK(pointer[2]))
    517 				fprintf(NetTrace, "%s ",
    518 				    ENCTYPE_NAME(pointer[2]));
    519 			else
    520 				fprintf(NetTrace, " %d (unknown)", pointer[2]);
    521 
    522 			encrypt_printsub(&pointer[1], length - 1, buf,
    523 			    sizeof(buf));
    524 			fprintf(NetTrace, "%s", buf);
    525 			break;
    526 
    527 		case ENCRYPT_SUPPORT:
    528 			i = 2;
    529 			fprintf(NetTrace, " SUPPORT ");
    530 			while (i < length) {
    531 				if (ENCTYPE_NAME_OK(pointer[i]))
    532 					fprintf(NetTrace, "%s ",
    533 					    ENCTYPE_NAME(pointer[i]));
    534 				else
    535 					fprintf(NetTrace, "%d ", pointer[i]);
    536 				i++;
    537 			}
    538 			break;
    539 
    540 		case ENCRYPT_ENC_KEYID:
    541 			fprintf(NetTrace, " ENC_KEYID ");
    542 			goto encommon;
    543 
    544 		case ENCRYPT_DEC_KEYID:
    545 			fprintf(NetTrace, " DEC_KEYID ");
    546 			goto encommon;
    547 
    548 		default:
    549 			fprintf(NetTrace, " %d (unknown)", pointer[1]);
    550 		encommon:
    551 			for (i = 2; i < length; i++)
    552 				fprintf(NetTrace, " %d", pointer[i]);
    553 			break;
    554 		}
    555 		break;
    556 #endif	/* ENCRYPTION */
    557 
    558 	case TELOPT_LINEMODE:
    559 	    fprintf(NetTrace, "LINEMODE ");
    560 	    if (length < 2) {
    561 		fprintf(NetTrace, " (empty suboption??\?)");
    562 		break;
    563 	    }
    564 	    switch (pointer[1]) {
    565 	    case WILL:
    566 		fprintf(NetTrace, "WILL ");
    567 		goto common;
    568 	    case WONT:
    569 		fprintf(NetTrace, "WONT ");
    570 		goto common;
    571 	    case DO:
    572 		fprintf(NetTrace, "DO ");
    573 		goto common;
    574 	    case DONT:
    575 		fprintf(NetTrace, "DONT ");
    576 	    common:
    577 		if (length < 3) {
    578 		    fprintf(NetTrace, "(no option??\?)");
    579 		    break;
    580 		}
    581 		switch (pointer[2]) {
    582 		case LM_FORWARDMASK:
    583 		    fprintf(NetTrace, "Forward Mask");
    584 		    for (i = 3; i < length; i++)
    585 			fprintf(NetTrace, " %x", pointer[i]);
    586 		    break;
    587 		default:
    588 		    fprintf(NetTrace, "%d (unknown)", pointer[2]);
    589 		    for (i = 3; i < length; i++)
    590 			fprintf(NetTrace, " %d", pointer[i]);
    591 		    break;
    592 		}
    593 		break;
    594 
    595 	    case LM_SLC:
    596 		fprintf(NetTrace, "SLC");
    597 		for (i = 2; i < length - 2; i += 3) {
    598 		    if (SLC_NAME_OK(pointer[i+SLC_FUNC]))
    599 			fprintf(NetTrace, " %s", SLC_NAME(pointer[i+SLC_FUNC]));
    600 		    else
    601 			fprintf(NetTrace, " %d", pointer[i+SLC_FUNC]);
    602 		    switch (pointer[i+SLC_FLAGS]&SLC_LEVELBITS) {
    603 		    case SLC_NOSUPPORT:
    604 			fprintf(NetTrace, " NOSUPPORT"); break;
    605 		    case SLC_CANTCHANGE:
    606 			fprintf(NetTrace, " CANTCHANGE"); break;
    607 		    case SLC_VARIABLE:
    608 			fprintf(NetTrace, " VARIABLE"); break;
    609 		    case SLC_DEFAULT:
    610 			fprintf(NetTrace, " DEFAULT"); break;
    611 		    }
    612 		    fprintf(NetTrace, "%s%s%s",
    613 			pointer[i+SLC_FLAGS]&SLC_ACK ? "|ACK" : "",
    614 			pointer[i+SLC_FLAGS]&SLC_FLUSHIN ? "|FLUSHIN" : "",
    615 			pointer[i+SLC_FLAGS]&SLC_FLUSHOUT ? "|FLUSHOUT" : "");
    616 		    if (pointer[i+SLC_FLAGS]& ~(SLC_ACK|SLC_FLUSHIN|
    617 						SLC_FLUSHOUT| SLC_LEVELBITS))
    618 			fprintf(NetTrace, "(0x%x)", pointer[i+SLC_FLAGS]);
    619 		    fprintf(NetTrace, " %d;", pointer[i+SLC_VALUE]);
    620 		    if ((pointer[i+SLC_VALUE] == IAC) &&
    621 			(pointer[i+SLC_VALUE+1] == IAC))
    622 				i++;
    623 		}
    624 		for (; i < length; i++)
    625 		    fprintf(NetTrace, " ?%d?", pointer[i]);
    626 		break;
    627 
    628 	    case LM_MODE:
    629 		fprintf(NetTrace, "MODE ");
    630 		if (length < 3) {
    631 		    fprintf(NetTrace, "(no mode??\?)");
    632 		    break;
    633 		}
    634 		{
    635 		    char tbuf[64];
    636 		    snprintf(tbuf, sizeof(tbuf), "%s%s%s%s%s",
    637 			pointer[2]&MODE_EDIT ? "|EDIT" : "",
    638 			pointer[2]&MODE_TRAPSIG ? "|TRAPSIG" : "",
    639 			pointer[2]&MODE_SOFT_TAB ? "|SOFT_TAB" : "",
    640 			pointer[2]&MODE_LIT_ECHO ? "|LIT_ECHO" : "",
    641 			pointer[2]&MODE_ACK ? "|ACK" : "");
    642 		    fprintf(NetTrace, "%s", tbuf[1] ? &tbuf[1] : "0");
    643 		}
    644 		if (pointer[2]&~(MODE_MASK))
    645 		    fprintf(NetTrace, " (0x%x)", pointer[2]);
    646 		for (i = 3; i < length; i++)
    647 		    fprintf(NetTrace, " ?0x%x?", pointer[i]);
    648 		break;
    649 	    default:
    650 		fprintf(NetTrace, "%d (unknown)", pointer[1]);
    651 		for (i = 2; i < length; i++)
    652 		    fprintf(NetTrace, " %d", pointer[i]);
    653 	    }
    654 	    break;
    655 
    656 	case TELOPT_STATUS: {
    657 	    const char *cp;
    658 	    int j, k;
    659 
    660 	    fprintf(NetTrace, "STATUS");
    661 
    662 	    switch (pointer[1]) {
    663 	    default:
    664 		if (pointer[1] == TELQUAL_SEND)
    665 		    fprintf(NetTrace, " SEND");
    666 		else
    667 		    fprintf(NetTrace, " %d (unknown)", pointer[1]);
    668 		for (i = 2; i < length; i++)
    669 		    fprintf(NetTrace, " ?%d?", pointer[i]);
    670 		break;
    671 	    case TELQUAL_IS:
    672 		if (--want_status_response < 0)
    673 		    want_status_response = 0;
    674 		if (NetTrace == stdout)
    675 		    fprintf(NetTrace, " IS\r\n");
    676 		else
    677 		    fprintf(NetTrace, " IS\n");
    678 
    679 		for (i = 2; i < length; i++) {
    680 		    switch(pointer[i]) {
    681 		    case DO:	cp = "DO"; goto common2;
    682 		    case DONT:	cp = "DONT"; goto common2;
    683 		    case WILL:	cp = "WILL"; goto common2;
    684 		    case WONT:	cp = "WONT"; goto common2;
    685 		    common2:
    686 			i++;
    687 			if (TELOPT_OK((int)pointer[i]))
    688 			    fprintf(NetTrace, " %s %s", cp, TELOPT(pointer[i]));
    689 			else
    690 			    fprintf(NetTrace, " %s %d", cp, pointer[i]);
    691 
    692 			if (NetTrace == stdout)
    693 			    fprintf(NetTrace, "\r\n");
    694 			else
    695 			    fprintf(NetTrace, "\n");
    696 			break;
    697 
    698 		    case SB:
    699 			fprintf(NetTrace, " SB ");
    700 			i++;
    701 			j = k = i;
    702 			while (j < length) {
    703 			    if (pointer[j] == SE) {
    704 				if (j+1 == length)
    705 				    break;
    706 				if (pointer[j+1] == SE)
    707 				    j++;
    708 				else
    709 				    break;
    710 			    }
    711 			    pointer[k++] = pointer[j++];
    712 			}
    713 			printsub(0, &pointer[i], k - i);
    714 			if (i < length) {
    715 			    fprintf(NetTrace, " SE");
    716 			    i = j;
    717 			} else
    718 			    i = j - 1;
    719 
    720 			if (NetTrace == stdout)
    721 			    fprintf(NetTrace, "\r\n");
    722 			else
    723 			    fprintf(NetTrace, "\n");
    724 
    725 			break;
    726 
    727 		    default:
    728 			fprintf(NetTrace, " %d", pointer[i]);
    729 			break;
    730 		    }
    731 		}
    732 		break;
    733 	    }
    734 	    break;
    735 	  }
    736 
    737 	case TELOPT_XDISPLOC:
    738 	    fprintf(NetTrace, "X-DISPLAY-LOCATION ");
    739 	    switch (pointer[1]) {
    740 	    case TELQUAL_IS:
    741 		fprintf(NetTrace, "IS \"%.*s\"", length-2, (char *)pointer+2);
    742 		break;
    743 	    case TELQUAL_SEND:
    744 		fprintf(NetTrace, "SEND");
    745 		break;
    746 	    default:
    747 		fprintf(NetTrace, "- unknown qualifier %d (0x%x).",
    748 				pointer[1], pointer[1]);
    749 	    }
    750 	    break;
    751 
    752 	case TELOPT_NEW_ENVIRON:
    753 	    fprintf(NetTrace, "NEW-ENVIRON ");
    754 	    switch (pointer[1]) {
    755 	    case TELQUAL_IS:
    756 		fprintf(NetTrace, "IS ");
    757 		goto env_common;
    758 	    case TELQUAL_SEND:
    759 		fprintf(NetTrace, "SEND ");
    760 		goto env_common;
    761 	    case TELQUAL_INFO:
    762 		fprintf(NetTrace, "INFO ");
    763 	    env_common:
    764 		{
    765 		    static const char NQ[] = "\" ";
    766 		    const char *noquote = NQ;
    767 		    for (i = 2; i < length; i++ ) {
    768 			switch (pointer[i]) {
    769 			case NEW_ENV_VALUE:
    770 			    fprintf(NetTrace, "%sVALUE ", noquote);
    771 			    noquote = NQ;
    772 			    break;
    773 
    774 			case NEW_ENV_VAR:
    775 			    fprintf(NetTrace, "%sVAR ", noquote);
    776 			    noquote = NQ;
    777 			    break;
    778 
    779 			case ENV_ESC:
    780 			    fprintf(NetTrace, "%sESC ", noquote);
    781 			    noquote = NQ;
    782 			    break;
    783 
    784 			case ENV_USERVAR:
    785 			    fprintf(NetTrace, "%sUSERVAR ", noquote);
    786 			    noquote = NQ;
    787 			    break;
    788 
    789 			default:
    790 			    if (isprint(pointer[i]) && pointer[i] != '"') {
    791 				if (*noquote) {
    792 				    putc('"', NetTrace);
    793 				    noquote = "";
    794 				}
    795 				putc(pointer[i], NetTrace);
    796 			    } else {
    797 				fprintf(NetTrace, "%s%03o ", noquote,
    798 							pointer[i]);
    799 				noquote = NQ;
    800 			    }
    801 			    break;
    802 			}
    803 		    }
    804 		    if (!noquote)
    805 			putc('"', NetTrace);
    806 		    break;
    807 		}
    808 	    }
    809 	    break;
    810 
    811 	default:
    812 	    if (TELOPT_OK(pointer[0]))
    813 		fprintf(NetTrace, "%s (unknown)", TELOPT(pointer[0]));
    814 	    else
    815 		fprintf(NetTrace, "%d (unknown)", pointer[0]);
    816 	    for (i = 1; i < length; i++)
    817 		fprintf(NetTrace, " %d", pointer[i]);
    818 	    break;
    819 	}
    820 	if (direction) {
    821 	    if (NetTrace == stdout)
    822 		fprintf(NetTrace, "\r\n");
    823 	    else
    824 		fprintf(NetTrace, "\n");
    825 	}
    826 	if (NetTrace == stdout)
    827 	    fflush(NetTrace);
    828     }
    829 }
    830 
    831 /* EmptyTerminal - called to make sure that the terminal buffer is empty.
    832  *			Note that we consider the buffer to run all the
    833  *			way to the kernel (thus the poll).
    834  */
    835 
    836 void
    837 EmptyTerminal(void)
    838 {
    839     struct pollfd set[1];
    840 
    841     set[0].fd = tout;
    842     set[0].events = POLLOUT;
    843 
    844     if (TTYBYTES() == 0) {
    845 	(void) poll(set, 1, INFTIM);
    846     } else {
    847 	while (TTYBYTES()) {
    848 	    (void) ttyflush(0);
    849 	    (void) poll(set, 1, INFTIM);
    850 	}
    851     }
    852 }
    853 
    854 static void
    855 SetForExit(void)
    856 {
    857     setconnmode(0);
    858     do {
    859 	(void)telrcv();			/* Process any incoming data */
    860 	EmptyTerminal();
    861     } while (ring_full_count(&netiring));	/* While there is any */
    862     setcommandmode();
    863     fflush(stdout);
    864     fflush(stderr);
    865     setconnmode(0);
    866     EmptyTerminal();			/* Flush the path to the tty */
    867     setcommandmode();
    868 }
    869 
    870 void
    871 Exit(int returnCode)
    872 {
    873     SetForExit();
    874     exit(returnCode);
    875 }
    876 
    877 void
    878 ExitString(const char *string, int returnCode)
    879 {
    880     fwrite(string, 1, strlen(string), stderr);
    881     exit(returnCode);
    882 }
    883