xref: /freebsd/contrib/tcpdump/print-telnet.c (revision 5521ff5a4d1929056e7ffc982fac3341ca54df7c)
1 /*	$NetBSD: print-telnet.c,v 1.2 1999/10/11 12:40:12 sjg Exp $ 	*/
2 
3 /*-
4  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Simon J. Gerraty.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 /*
39  *      @(#)Copyright (c) 1994, Simon J. Gerraty.
40  *
41  *      This is free software.  It comes with NO WARRANTY.
42  *      Permission to use, modify and distribute this source code
43  *      is granted subject to the following conditions.
44  *      1/ that the above copyright notice and this notice
45  *      are preserved in all copies.
46  */
47 
48 #ifdef HAVE_CONFIG_H
49 #include "config.h"
50 #endif
51 
52 #ifndef lint
53 static const char rcsid[] =
54      "@(#) $Header: /tcpdump/master/tcpdump/print-telnet.c,v 1.12 2000/09/29 04:58:51 guy Exp $";
55 #endif
56 
57 #include <sys/param.h>
58 #include <sys/time.h>
59 #include <sys/types.h>
60 #include <ctype.h>
61 
62 #include <netinet/in.h>
63 
64 #define TELCMDS
65 #define TELOPTS
66 #include <arpa/telnet.h>
67 
68 #include <stdio.h>
69 #include <stdlib.h>
70 #include <unistd.h>
71 #include <string.h>
72 
73 #include "interface.h"
74 #include "addrtoname.h"
75 
76 #ifndef TELCMD_FIRST
77 # define TELCMD_FIRST SE
78 #endif
79 
80 void
81 telnet_print(register const u_char *sp, u_int length)
82 {
83 	static char tnet[128];
84 	register int i, c, x;
85 	register u_char *rcp;
86 	int off, first = 1;
87 	u_char	*osp;
88 
89 	off = 0;
90 	x = 0;
91 
92 	while (length > 0 && *sp == IAC) {
93 		osp = (u_char *) sp;
94 		tnet[0] = '\0';
95 
96 		c = *sp++;
97 		length--;
98 		switch (*sp) {
99 		case IAC:			/* <IAC><IAC>! */
100 			if (length > 1 && sp[1] == IAC) {
101 				(void)strcpy(tnet, "IAC IAC");
102 			} else {
103 				length = 0;
104 				continue;
105 			}
106 			break;
107 		default:
108 			c = *sp++;
109 			length--;
110 			if ((i = c - TELCMD_FIRST) < 0
111 			    || i > IAC - TELCMD_FIRST) {
112 				(void)printf("unknown: ff%02x\n", c);
113 				return;
114 			}
115 			switch (c) {
116 			case DONT:
117 			case DO:
118 			case WONT:
119 			case WILL:
120 			case SB:
121 				x = *sp++; /* option */
122 				length--;
123 				if (x >= 0 && x < NTELOPTS) {
124 					(void)snprintf(tnet, sizeof(tnet),
125 					    "%s %s", telcmds[i], telopts[x]);
126 				} else {
127 					(void)snprintf(tnet, sizeof(tnet),
128 					    "%s %#x", telcmds[i], x);
129 				}
130 				break;
131 			default:
132 				(void)snprintf(tnet, sizeof(tnet), "%s",
133 				    telcmds[i]);
134 			}
135 			if (c == SB) {
136 				c = *sp++;
137 				length--;
138 				(void)strcat(tnet, c ? " SEND" : " IS '");
139 				rcp = (u_char *) sp;
140 				i = strlen(tnet);
141 				while (length > 0 && (x = *sp++) != IAC)
142 					--length;
143 				if (x == IAC) {
144 					if (2 < vflag
145 					    && i + 16 + sp - rcp < sizeof(tnet)) {
146 						(void)strncpy(&tnet[i], rcp, sp - rcp);
147 						i += (sp - rcp) - 1;
148 						tnet[i] = '\0';
149 					} else if (i + 8 < sizeof(tnet)) {
150 						(void)strcat(&tnet[i], "...");
151 					}
152 					if (*sp++ == SE
153 					    && i + 4 < sizeof(tnet))
154 						(void)strcat(tnet, c ? " SE" : "' SE");
155 				} else if (i + 16 < sizeof(tnet)) {
156 					(void)strcat(tnet, " truncated!");
157 				}
158 			}
159 			break;
160 		}
161 		/*
162 		 * now print it
163 		 */
164 		if (Xflag && 2 < vflag) {
165 			if (first)
166 				printf("\nTelnet:\n");
167 			i = sp - osp;
168 			hex_print_with_offset(osp, i, off);
169 			off += i;
170 			if (i > 8)
171 				printf("\n\t\t\t\t");
172 			else
173 				printf("%*s\t", (8 - i) * 3, "");
174 			safeputs(tnet);
175 		} else {
176 			printf("%s", (first) ? " [telnet " : ", ");
177 			safeputs(tnet);
178 		}
179 		first = 0;
180 	}
181 	if (!first) {
182 		if (Xflag && 2 < vflag)
183 			printf("\n");
184 		else
185 			printf("]");
186 	}
187 }
188