]> The Tcpdump Group git mirrors - tcpdump/blob - print-ascii.c
Check not just the capture length but the on-the-network length.
[tcpdump] / print-ascii.c
1 /* $NetBSD: print-ascii.c,v 1.1 1999/09/30 14:49: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 Alan Barrett and 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 #ifdef HAVE_CONFIG_H
40 #include "config.h"
41 #endif
42
43 #ifndef lint
44 static const char rcsid[] _U_ =
45 "@(#) $Header: /tcpdump/master/tcpdump/print-ascii.c,v 1.17 2005-07-06 20:53:32 guy Exp $";
46 #endif
47 #include <tcpdump-stdinc.h>
48 #include <stdio.h>
49
50 #include "interface.h"
51
52 #define ASCII_LINELENGTH 300
53 #define HEXDUMP_BYTES_PER_LINE 16
54 #define HEXDUMP_SHORTS_PER_LINE (HEXDUMP_BYTES_PER_LINE / 2)
55 #define HEXDUMP_HEXSTUFF_PER_SHORT 5 /* 4 hex digits and a space */
56 #define HEXDUMP_HEXSTUFF_PER_LINE \
57 (HEXDUMP_HEXSTUFF_PER_SHORT * HEXDUMP_SHORTS_PER_LINE)
58
59 void
60 ascii_print(register const u_char *cp, register u_int length)
61 {
62 u_int caplength;
63 register int s;
64
65 caplength = (snapend >= cp) ? snapend - cp : 0;
66 if (length > caplength)
67 length = caplength;
68 putchar('\n');
69 while (length > 0) {
70 s = *cp++;
71 length--;
72 if (!isgraph(s) &&
73 (s != '\t' && s != ' ' && s != '\n' && s != '\r'))
74 putchar('.');
75 else
76 putchar(s);
77 }
78 }
79
80 void
81 hex_and_ascii_print_with_offset(register const char *ident,
82 register const u_char *cp, register u_int length, register u_int oset)
83 {
84 u_int caplength;
85 register u_int i;
86 register int s1, s2;
87 register int nshorts;
88 char hexstuff[HEXDUMP_SHORTS_PER_LINE*HEXDUMP_HEXSTUFF_PER_SHORT+1], *hsp;
89 char asciistuff[ASCII_LINELENGTH+1], *asp;
90
91 caplength = (snapend >= cp) ? snapend - cp : 0;
92 if (length > caplength)
93 length = caplength;
94 nshorts = length / sizeof(u_short);
95 i = 0;
96 hsp = hexstuff; asp = asciistuff;
97 while (--nshorts >= 0) {
98 s1 = *cp++;
99 s2 = *cp++;
100 (void)snprintf(hsp, sizeof(hexstuff) - (hsp - hexstuff),
101 " %02x%02x", s1, s2);
102 hsp += HEXDUMP_HEXSTUFF_PER_SHORT;
103 *(asp++) = (isgraph(s1) ? s1 : '.');
104 *(asp++) = (isgraph(s2) ? s2 : '.');
105 i++;
106 if (i >= HEXDUMP_SHORTS_PER_LINE) {
107 *hsp = *asp = '\0';
108 (void)printf("%s0x%04x: %-*s %s",
109 ident, oset, HEXDUMP_HEXSTUFF_PER_LINE,
110 hexstuff, asciistuff);
111 i = 0; hsp = hexstuff; asp = asciistuff;
112 oset += HEXDUMP_BYTES_PER_LINE;
113 }
114 }
115 if (length & 1) {
116 s1 = *cp++;
117 (void)snprintf(hsp, sizeof(hexstuff) - (hsp - hexstuff),
118 " %02x", s1);
119 hsp += 3;
120 *(asp++) = (isgraph(s1) ? s1 : '.');
121 ++i;
122 }
123 if (i > 0) {
124 *hsp = *asp = '\0';
125 (void)printf("%s0x%04x: %-*s %s",
126 ident, oset, HEXDUMP_HEXSTUFF_PER_LINE,
127 hexstuff, asciistuff);
128 }
129 }
130
131 void
132 hex_and_ascii_print(register const char *ident, register const u_char *cp,
133 register u_int length)
134 {
135 hex_and_ascii_print_with_offset(ident, cp, length, 0);
136 }
137
138 /*
139 * telnet_print() wants this. It is essentially default_print_unaligned()
140 */
141 void
142 hex_print_with_offset(register const char *ident, register const u_char *cp, register u_int length,
143 register u_int oset)
144 {
145 u_int caplength;
146 register u_int i, s;
147 register int nshorts;
148
149 caplength = (snapend >= cp) ? snapend - cp : 0;
150 if (length > caplength)
151 length = caplength;
152 nshorts = (u_int) length / sizeof(u_short);
153 i = 0;
154 while (--nshorts >= 0) {
155 if ((i++ % 8) == 0) {
156 (void)printf("%s0x%04x: ", ident, oset);
157 oset += HEXDUMP_BYTES_PER_LINE;
158 }
159 s = *cp++;
160 (void)printf(" %02x%02x", s, *cp++);
161 }
162 if (length & 1) {
163 if ((i % 8) == 0)
164 (void)printf("%s0x%04x: ", ident, oset);
165 (void)printf(" %02x", *cp);
166 }
167 }
168
169 /*
170 * just for completeness
171 */
172 void
173 hex_print(register const char *ident, register const u_char *cp, register u_int length)
174 {
175 hex_print_with_offset(ident, cp, length, 0);
176 }
177
178 #ifdef MAIN
179 int
180 main(int argc, char *argv[])
181 {
182 hex_print("\n\t", "Hello, World!\n", 14);
183 printf("\n");
184 hex_and_ascii_print("\n\t", "Hello, World!\n", 14);
185 printf("\n");
186 ascii_print("Hello, World!\n", 14);
187 printf("\n");
188 #define TMSG "Now is the winter of our discontent...\n"
189 hex_print_with_offset("\n\t", TMSG, sizeof(TMSG) - 1, 0x100);
190 printf("\n");
191 hex_and_ascii_print_with_offset("\n\t", TMSG, sizeof(TMSG) - 1, 0x100);
192 printf("\n");
193 exit(0);
194 }
195 #endif /* MAIN */