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