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