]> The Tcpdump Group git mirrors - tcpdump/blob - print-telnet.c
5be7e6f1d386cc4d9089fa47eff5d32a6ac88672
[tcpdump] / print-telnet.c
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.9 2000-07-01 03:39:11 assar 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 #include <netinet/in_systm.h>
64 #include <netinet/ip.h>
65 #include <netinet/ip_var.h>
66 #include <netinet/tcp.h>
67
68 #define TELCMDS
69 #define TELOPTS
70 #include <arpa/telnet.h>
71
72 #include <stdio.h>
73 #include <stdlib.h>
74 #include <unistd.h>
75 #include <string.h>
76
77 #include "interface.h"
78 #include "addrtoname.h"
79
80 #ifndef TELCMD_FIRST
81 # define TELCMD_FIRST SE
82 #endif
83
84 void
85 telnet_print(register const u_char *sp, u_int length)
86 {
87 static char tnet[128];
88 register int i, c, x;
89 register u_char *rcp;
90 int off, first = 1;
91 u_char *osp;
92
93 off = 0;
94 x = 0;
95
96 while (length > 0 && *sp == IAC) {
97 osp = (u_char *) sp;
98 tnet[0] = '\0';
99
100 c = *sp++;
101 length--;
102 switch (*sp) {
103 case IAC: /* <IAC><IAC>! */
104 if (length > 1 && sp[1] == IAC) {
105 (void)strcpy(tnet, "IAC IAC");
106 } else {
107 length = 0;
108 continue;
109 }
110 break;
111 default:
112 c = *sp++;
113 length--;
114 if ((i = c - TELCMD_FIRST) < 0
115 || i > IAC - TELCMD_FIRST) {
116 (void)printf("unknown: ff%02x\n", c);
117 return;
118 }
119 switch (c) {
120 case DONT:
121 case DO:
122 case WONT:
123 case WILL:
124 case SB:
125 x = *sp++; /* option */
126 length--;
127 if (x >= 0 && x < NTELOPTS) {
128 (void)snprintf(tnet, sizeof(tnet),
129 "%s %s", telcmds[i], telopts[x]);
130 } else {
131 (void)snprintf(tnet, sizeof(tnet),
132 "%s %#x", telcmds[i], x);
133 }
134 break;
135 default:
136 (void)snprintf(tnet, sizeof(tnet), "%s",
137 telcmds[i]);
138 }
139 if (c == SB) {
140 c = *sp++;
141 length--;
142 (void)strcat(tnet, c ? " SEND" : " IS '");
143 rcp = (u_char *) sp;
144 i = strlen(tnet);
145 while (length > 0 && (x = *sp++) != IAC)
146 --length;
147 if (x == IAC) {
148 if (2 < vflag
149 && i + 16 + sp - rcp < sizeof(tnet)) {
150 (void)strncpy(&tnet[i], rcp, sp - rcp);
151 i += (sp - rcp) - 1;
152 tnet[i] = '\0';
153 } else if (i + 8 < sizeof(tnet)) {
154 (void)strcat(&tnet[i], "...");
155 }
156 if (*sp++ == SE
157 && i + 4 < sizeof(tnet))
158 (void)strcat(tnet, c ? " SE" : "' SE");
159 } else if (i + 16 < sizeof(tnet)) {
160 (void)strcat(tnet, " truncated!");
161 }
162 }
163 break;
164 }
165 /*
166 * now print it
167 */
168 if (Xflag && 2 < vflag) {
169 if (first)
170 printf("\nTelnet:\n");
171 i = sp - osp;
172 hex_print_with_offset(osp, i, off);
173 off += i;
174 if (i > 8)
175 printf("\n\t\t\t\t");
176 else
177 printf("%*s\t", (8 - i) * 3, "");
178 safeputs(tnet);
179 } else {
180 printf("%s", (first) ? " [telnet " : ", ");
181 safeputs(tnet);
182 }
183 first = 0;
184 }
185 if (!first) {
186 if (Xflag && 2 < vflag)
187 printf("\n");
188 else
189 printf("]");
190 }
191 }