xref: /csrg-svn/usr.bin/telnet/utilities.c (revision 34898)
133686Sbostic /*
233686Sbostic  * Copyright (c) 1988 Regents of the University of California.
333686Sbostic  * All rights reserved.
433686Sbostic  *
533686Sbostic  * Redistribution and use in source and binary forms are permitted
6*34898Sbostic  * provided that the above copyright notice and this paragraph are
7*34898Sbostic  * duplicated in all such forms and that any documentation,
8*34898Sbostic  * advertising materials, and other materials related to such
9*34898Sbostic  * distribution and use acknowledge that the software was developed
10*34898Sbostic  * by the University of California, Berkeley.  The name of the
11*34898Sbostic  * University may not be used to endorse or promote products derived
12*34898Sbostic  * from this software without specific prior written permission.
13*34898Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14*34898Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15*34898Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1633686Sbostic  */
1733686Sbostic 
1833686Sbostic #ifndef lint
19*34898Sbostic static char sccsid[] = "@(#)utilities.c	1.7 (Berkeley) 06/29/88";
2033686Sbostic #endif /* not lint */
2133686Sbostic 
2232149Sminshall #define	TELOPTS
2332149Sminshall #include <arpa/telnet.h>
2432381Sminshall #include <sys/types.h>
2532149Sminshall 
2632149Sminshall #include <ctype.h>
2732149Sminshall 
2834305Sminshall #include "general.h"
2934305Sminshall 
3032381Sminshall #include "ring.h"
3132381Sminshall 
3232149Sminshall #include "externs.h"
3332149Sminshall 
3432149Sminshall FILE	*NetTrace = 0;		/* Not in bss, since needs to stay */
3532149Sminshall 
3632149Sminshall /*
3732149Sminshall  * upcase()
3832149Sminshall  *
3932149Sminshall  *	Upcase (in place) the argument.
4032149Sminshall  */
4132149Sminshall 
4232149Sminshall void
4332149Sminshall upcase(argument)
4432149Sminshall register char *argument;
4532149Sminshall {
4632149Sminshall     register int c;
4732149Sminshall 
4832149Sminshall     while ((c = *argument) != 0) {
4932149Sminshall 	if (islower(c)) {
5032149Sminshall 	    *argument = toupper(c);
5132149Sminshall 	}
5232149Sminshall 	argument++;
5332149Sminshall     }
5432149Sminshall }
5532149Sminshall 
5632149Sminshall /*
5732149Sminshall  * SetSockOpt()
5832149Sminshall  *
5932149Sminshall  * Compensate for differences in 4.2 and 4.3 systems.
6032149Sminshall  */
6132149Sminshall 
6232149Sminshall int
6332149Sminshall SetSockOpt(fd, level, option, yesno)
6432149Sminshall int
6532149Sminshall 	fd,
6632149Sminshall 	level,
6732149Sminshall 	option,
6832149Sminshall 	yesno;
6932149Sminshall {
7032149Sminshall #ifndef	NOT43
7132149Sminshall     return setsockopt(fd, level, option,
7232149Sminshall 				(char *)&yesno, sizeof yesno);
7332149Sminshall #else	/* NOT43 */
7432149Sminshall     if (yesno == 0) {		/* Can't do that in 4.2! */
7532149Sminshall 	fprintf(stderr, "Error: attempt to turn off an option 0x%x.\n",
7632149Sminshall 				option);
7732149Sminshall 	return -1;
7832149Sminshall     }
7932149Sminshall     return setsockopt(fd, level, option, 0, 0);
8032149Sminshall #endif	/* NOT43 */
8132149Sminshall }
8232149Sminshall 
8332149Sminshall /*
8432149Sminshall  * The following are routines used to print out debugging information.
8532149Sminshall  */
8632149Sminshall 
8732149Sminshall 
8832149Sminshall void
8932149Sminshall Dump(direction, buffer, length)
9032149Sminshall char	direction;
9132149Sminshall char	*buffer;
9232149Sminshall int	length;
9332149Sminshall {
9432149Sminshall #   define BYTES_PER_LINE	32
9532149Sminshall #   define min(x,y)	((x<y)? x:y)
9632149Sminshall     char *pThis;
9732149Sminshall     int offset;
9832149Sminshall 
9932149Sminshall     offset = 0;
10032149Sminshall 
10132149Sminshall     while (length) {
10232149Sminshall 	/* print one line */
10332149Sminshall 	fprintf(NetTrace, "%c 0x%x\t", direction, offset);
10432149Sminshall 	pThis = buffer;
10532149Sminshall 	buffer = buffer+min(length, BYTES_PER_LINE);
10632149Sminshall 	while (pThis < buffer) {
10732149Sminshall 	    fprintf(NetTrace, "%.2x", (*pThis)&0xff);
10832149Sminshall 	    pThis++;
10932149Sminshall 	}
11032149Sminshall 	fprintf(NetTrace, "\n");
11132149Sminshall 	length -= BYTES_PER_LINE;
11232149Sminshall 	offset += BYTES_PER_LINE;
11332149Sminshall 	if (length < 0) {
11432149Sminshall 	    return;
11532149Sminshall 	}
11632149Sminshall 	/* find next unique line */
11732149Sminshall     }
11832149Sminshall }
11932149Sminshall 
12032149Sminshall 
12132149Sminshall /*VARARGS*/
12232149Sminshall void
12332149Sminshall printoption(direction, fmt, option, what)
12432149Sminshall 	char *direction, *fmt;
12532149Sminshall 	int option, what;
12632149Sminshall {
12732149Sminshall 	if (!showoptions)
12832149Sminshall 		return;
12932149Sminshall 	fprintf(NetTrace, "%s ", direction+1);
13032149Sminshall 	if (fmt == doopt)
13132149Sminshall 		fmt = "do";
13232149Sminshall 	else if (fmt == dont)
13332149Sminshall 		fmt = "dont";
13432149Sminshall 	else if (fmt == will)
13532149Sminshall 		fmt = "will";
13632149Sminshall 	else if (fmt == wont)
13732149Sminshall 		fmt = "wont";
13832149Sminshall 	else
13932149Sminshall 		fmt = "???";
14032149Sminshall 	if (option < (sizeof telopts/sizeof telopts[0]))
14132149Sminshall 		fprintf(NetTrace, "%s %s", fmt, telopts[option]);
14232149Sminshall 	else
14332149Sminshall 		fprintf(NetTrace, "%s %d", fmt, option);
14432149Sminshall 	if (*direction == '<') {
14532149Sminshall 		fprintf(NetTrace, "\r\n");
14632149Sminshall 		return;
14732149Sminshall 	}
14832149Sminshall 	fprintf(NetTrace, " (%s)\r\n", what ? "reply" : "don't reply");
14932149Sminshall }
15032149Sminshall 
15132149Sminshall void
15232149Sminshall printsub(direction, pointer, length)
15332149Sminshall char	*direction,		/* "<" or ">" */
15432149Sminshall 	*pointer;		/* where suboption data sits */
15532149Sminshall int	length;			/* length of suboption data */
15632149Sminshall {
15732149Sminshall     if (showoptions) {
15832149Sminshall 	fprintf(NetTrace, "%s suboption ",
15932149Sminshall 				(direction[0] == '<')? "Received":"Sent");
16032149Sminshall 	switch (pointer[0]) {
16132149Sminshall 	case TELOPT_TTYPE:
16232149Sminshall 	    fprintf(NetTrace, "Terminal type ");
16332149Sminshall 	    switch (pointer[1]) {
16432149Sminshall 	    case TELQUAL_IS:
16532149Sminshall 		{
16633286Sminshall 		    char tmpbuf[SUBBUFSIZE];
16732149Sminshall 		    int minlen = min(length, sizeof tmpbuf);
16832149Sminshall 
16932149Sminshall 		    memcpy(tmpbuf, pointer+2, minlen);
17032149Sminshall 		    tmpbuf[minlen-1] = 0;
17132149Sminshall 		    fprintf(NetTrace, "is %s.\n", tmpbuf);
17232149Sminshall 		}
17332149Sminshall 		break;
17432149Sminshall 	    case TELQUAL_SEND:
17532149Sminshall 		fprintf(NetTrace, "- request to send.\n");
17632149Sminshall 		break;
17732149Sminshall 	    default:
17832149Sminshall 		fprintf(NetTrace,
17934849Sminshall 				"- unknown qualifier %d (0x%x).\n",
18034849Sminshall 				pointer[1], pointer[1]);
18132149Sminshall 	    }
18232149Sminshall 	    break;
18332149Sminshall 	default:
18432149Sminshall 	    fprintf(NetTrace, "Unknown option %d (0x%x)\n",
18532149Sminshall 					pointer[0], pointer[0]);
18632149Sminshall 	}
18732149Sminshall     }
18832149Sminshall }
189