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