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