xref: /original-bsd/usr.bin/telnet/utilities.c (revision 2bb802fc)
1 /*
2  * Copyright (c) 1988 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #ifndef lint
19 static char sccsid[] = "@(#)utilities.c	1.7 (Berkeley) 06/29/88";
20 #endif /* not lint */
21 
22 #define	TELOPTS
23 #include <arpa/telnet.h>
24 #include <sys/types.h>
25 
26 #include <ctype.h>
27 
28 #include "general.h"
29 
30 #include "ring.h"
31 
32 #include "externs.h"
33 
34 FILE	*NetTrace = 0;		/* Not in bss, since needs to stay */
35 
36 /*
37  * upcase()
38  *
39  *	Upcase (in place) the argument.
40  */
41 
42 void
43 upcase(argument)
44 register char *argument;
45 {
46     register int c;
47 
48     while ((c = *argument) != 0) {
49 	if (islower(c)) {
50 	    *argument = toupper(c);
51 	}
52 	argument++;
53     }
54 }
55 
56 /*
57  * SetSockOpt()
58  *
59  * Compensate for differences in 4.2 and 4.3 systems.
60  */
61 
62 int
63 SetSockOpt(fd, level, option, yesno)
64 int
65 	fd,
66 	level,
67 	option,
68 	yesno;
69 {
70 #ifndef	NOT43
71     return setsockopt(fd, level, option,
72 				(char *)&yesno, sizeof yesno);
73 #else	/* NOT43 */
74     if (yesno == 0) {		/* Can't do that in 4.2! */
75 	fprintf(stderr, "Error: attempt to turn off an option 0x%x.\n",
76 				option);
77 	return -1;
78     }
79     return setsockopt(fd, level, option, 0, 0);
80 #endif	/* NOT43 */
81 }
82 
83 /*
84  * The following are routines used to print out debugging information.
85  */
86 
87 
88 void
89 Dump(direction, buffer, length)
90 char	direction;
91 char	*buffer;
92 int	length;
93 {
94 #   define BYTES_PER_LINE	32
95 #   define min(x,y)	((x<y)? x:y)
96     char *pThis;
97     int offset;
98 
99     offset = 0;
100 
101     while (length) {
102 	/* print one line */
103 	fprintf(NetTrace, "%c 0x%x\t", direction, offset);
104 	pThis = buffer;
105 	buffer = buffer+min(length, BYTES_PER_LINE);
106 	while (pThis < buffer) {
107 	    fprintf(NetTrace, "%.2x", (*pThis)&0xff);
108 	    pThis++;
109 	}
110 	fprintf(NetTrace, "\n");
111 	length -= BYTES_PER_LINE;
112 	offset += BYTES_PER_LINE;
113 	if (length < 0) {
114 	    return;
115 	}
116 	/* find next unique line */
117     }
118 }
119 
120 
121 /*VARARGS*/
122 void
123 printoption(direction, fmt, option, what)
124 	char *direction, *fmt;
125 	int option, what;
126 {
127 	if (!showoptions)
128 		return;
129 	fprintf(NetTrace, "%s ", direction+1);
130 	if (fmt == doopt)
131 		fmt = "do";
132 	else if (fmt == dont)
133 		fmt = "dont";
134 	else if (fmt == will)
135 		fmt = "will";
136 	else if (fmt == wont)
137 		fmt = "wont";
138 	else
139 		fmt = "???";
140 	if (option < (sizeof telopts/sizeof telopts[0]))
141 		fprintf(NetTrace, "%s %s", fmt, telopts[option]);
142 	else
143 		fprintf(NetTrace, "%s %d", fmt, option);
144 	if (*direction == '<') {
145 		fprintf(NetTrace, "\r\n");
146 		return;
147 	}
148 	fprintf(NetTrace, " (%s)\r\n", what ? "reply" : "don't reply");
149 }
150 
151 void
152 printsub(direction, pointer, length)
153 char	*direction,		/* "<" or ">" */
154 	*pointer;		/* where suboption data sits */
155 int	length;			/* length of suboption data */
156 {
157     if (showoptions) {
158 	fprintf(NetTrace, "%s suboption ",
159 				(direction[0] == '<')? "Received":"Sent");
160 	switch (pointer[0]) {
161 	case TELOPT_TTYPE:
162 	    fprintf(NetTrace, "Terminal type ");
163 	    switch (pointer[1]) {
164 	    case TELQUAL_IS:
165 		{
166 		    char tmpbuf[SUBBUFSIZE];
167 		    int minlen = min(length, sizeof tmpbuf);
168 
169 		    memcpy(tmpbuf, pointer+2, minlen);
170 		    tmpbuf[minlen-1] = 0;
171 		    fprintf(NetTrace, "is %s.\n", tmpbuf);
172 		}
173 		break;
174 	    case TELQUAL_SEND:
175 		fprintf(NetTrace, "- request to send.\n");
176 		break;
177 	    default:
178 		fprintf(NetTrace,
179 				"- unknown qualifier %d (0x%x).\n",
180 				pointer[1], pointer[1]);
181 	    }
182 	    break;
183 	default:
184 	    fprintf(NetTrace, "Unknown option %d (0x%x)\n",
185 					pointer[0], pointer[0]);
186 	}
187     }
188 }
189