xref: /original-bsd/usr.bin/telnet/utilities.c (revision 57124d5e)
1 #define	TELOPTS
2 #include <arpa/telnet.h>
3 
4 #include <ctype.h>
5 
6 #include "externs.h"
7 
8 FILE	*NetTrace = 0;		/* Not in bss, since needs to stay */
9 
10 /*
11  * upcase()
12  *
13  *	Upcase (in place) the argument.
14  */
15 
16 void
17 upcase(argument)
18 register char *argument;
19 {
20     register int c;
21 
22     while ((c = *argument) != 0) {
23 	if (islower(c)) {
24 	    *argument = toupper(c);
25 	}
26 	argument++;
27     }
28 }
29 
30 /*
31  * SetSockOpt()
32  *
33  * Compensate for differences in 4.2 and 4.3 systems.
34  */
35 
36 int
37 SetSockOpt(fd, level, option, yesno)
38 int
39 	fd,
40 	level,
41 	option,
42 	yesno;
43 {
44 #ifndef	NOT43
45     return setsockopt(fd, level, option,
46 				(char *)&yesno, sizeof yesno);
47 #else	/* NOT43 */
48     if (yesno == 0) {		/* Can't do that in 4.2! */
49 	fprintf(stderr, "Error: attempt to turn off an option 0x%x.\n",
50 				option);
51 	return -1;
52     }
53     return setsockopt(fd, level, option, 0, 0);
54 #endif	/* NOT43 */
55 }
56 
57 /*
58  * The following are routines used to print out debugging information.
59  */
60 
61 
62 void
63 Dump(direction, buffer, length)
64 char	direction;
65 char	*buffer;
66 int	length;
67 {
68 #   define BYTES_PER_LINE	32
69 #   define min(x,y)	((x<y)? x:y)
70     char *pThis;
71     int offset;
72 
73     offset = 0;
74 
75     while (length) {
76 	/* print one line */
77 	fprintf(NetTrace, "%c 0x%x\t", direction, offset);
78 	pThis = buffer;
79 	buffer = buffer+min(length, BYTES_PER_LINE);
80 	while (pThis < buffer) {
81 	    fprintf(NetTrace, "%.2x", (*pThis)&0xff);
82 	    pThis++;
83 	}
84 	fprintf(NetTrace, "\n");
85 	length -= BYTES_PER_LINE;
86 	offset += BYTES_PER_LINE;
87 	if (length < 0) {
88 	    return;
89 	}
90 	/* find next unique line */
91     }
92 }
93 
94 
95 /*VARARGS*/
96 void
97 printoption(direction, fmt, option, what)
98 	char *direction, *fmt;
99 	int option, what;
100 {
101 	if (!showoptions)
102 		return;
103 	fprintf(NetTrace, "%s ", direction+1);
104 	if (fmt == doopt)
105 		fmt = "do";
106 	else if (fmt == dont)
107 		fmt = "dont";
108 	else if (fmt == will)
109 		fmt = "will";
110 	else if (fmt == wont)
111 		fmt = "wont";
112 	else
113 		fmt = "???";
114 	if (option < (sizeof telopts/sizeof telopts[0]))
115 		fprintf(NetTrace, "%s %s", fmt, telopts[option]);
116 	else
117 		fprintf(NetTrace, "%s %d", fmt, option);
118 	if (*direction == '<') {
119 		fprintf(NetTrace, "\r\n");
120 		return;
121 	}
122 	fprintf(NetTrace, " (%s)\r\n", what ? "reply" : "don't reply");
123 }
124 
125 void
126 printsub(direction, pointer, length)
127 char	*direction,		/* "<" or ">" */
128 	*pointer;		/* where suboption data sits */
129 int	length;			/* length of suboption data */
130 {
131     if (showoptions) {
132 	fprintf(NetTrace, "%s suboption ",
133 				(direction[0] == '<')? "Received":"Sent");
134 	switch (pointer[0]) {
135 	case TELOPT_TTYPE:
136 	    fprintf(NetTrace, "Terminal type ");
137 	    switch (pointer[1]) {
138 	    case TELQUAL_IS:
139 		{
140 		    char tmpbuf[sizeof subbuffer];
141 		    int minlen = min(length, sizeof tmpbuf);
142 
143 		    memcpy(tmpbuf, pointer+2, minlen);
144 		    tmpbuf[minlen-1] = 0;
145 		    fprintf(NetTrace, "is %s.\n", tmpbuf);
146 		}
147 		break;
148 	    case TELQUAL_SEND:
149 		fprintf(NetTrace, "- request to send.\n");
150 		break;
151 	    default:
152 		fprintf(NetTrace,
153 				"- unknown qualifier %d (0x%x).\n", pointer[1]);
154 	    }
155 	    break;
156 	default:
157 	    fprintf(NetTrace, "Unknown option %d (0x%x)\n",
158 					pointer[0], pointer[0]);
159 	}
160     }
161 }
162