]> The Tcpdump Group git mirrors - tcpdump/blob - print-ascii.c
Merge branch 'master' into fix_udp_frag_badlen
[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 /* \summary: ASCII packet dump printer */
40
41 #ifdef HAVE_CONFIG_H
42 #include <config.h>
43 #endif
44
45 #include "netdissect-stdinc.h"
46
47 #include <stdio.h>
48
49 #include "netdissect-ctype.h"
50
51 #include "netdissect.h"
52 #include "extract.h"
53
54 #define ASCII_LINELENGTH 300
55 #define HEXDUMP_BYTES_PER_LINE 16
56 #define HEXDUMP_SHORTS_PER_LINE (HEXDUMP_BYTES_PER_LINE / 2)
57 #define HEXDUMP_HEXSTUFF_PER_SHORT 5 /* 4 hex digits and a space */
58 #define HEXDUMP_HEXSTUFF_PER_LINE \
59 (HEXDUMP_HEXSTUFF_PER_SHORT * HEXDUMP_SHORTS_PER_LINE)
60
61 static void hex_and_ascii_print_with_offset(netdissect_options *, const char *, const u_char *, u_int, u_int);
62
63 void
64 ascii_print(netdissect_options *ndo,
65 const u_char *cp, u_int length)
66 {
67 u_int caplength;
68 u_char s;
69
70 ndo->ndo_protocol = "ascii";
71 caplength = (ndo->ndo_snapend > cp) ? ND_BYTES_AVAILABLE_AFTER(cp) : 0;
72 if (length > caplength)
73 length = caplength;
74 ND_PRINT("\n");
75 while (length > 0) {
76 s = GET_U_1(cp);
77 cp++;
78 length--;
79 if (s == '\r') {
80 /*
81 * Don't print CRs at the end of the line; they
82 * don't belong at the ends of lines on UN*X,
83 * and the standard I/O library will give us one
84 * on Windows so we don't need to print one
85 * ourselves.
86 *
87 * In the middle of a line, just print a '.'.
88 */
89 if (length > 1 && GET_U_1(cp) != '\n')
90 ND_PRINT(".");
91 } else {
92 if (!ND_ASCII_ISGRAPH(s) &&
93 (s != '\t' && s != ' ' && s != '\n'))
94 ND_PRINT(".");
95 else
96 ND_PRINT("%c", s);
97 }
98 }
99 }
100
101 static void
102 hex_and_ascii_print_with_offset(netdissect_options *ndo, const char *ident,
103 const u_char *cp, u_int length, u_int oset)
104 {
105 u_int caplength;
106 u_int i;
107 u_int s1, s2;
108 u_int nshorts;
109 char hexstuff[HEXDUMP_SHORTS_PER_LINE*HEXDUMP_HEXSTUFF_PER_SHORT+1], *hsp;
110 char asciistuff[ASCII_LINELENGTH+1], *asp;
111
112 caplength = (ndo->ndo_snapend > cp) ? ND_BYTES_AVAILABLE_AFTER(cp) : 0;
113 if (length > caplength)
114 length = caplength;
115 nshorts = length / sizeof(u_short);
116 i = 0;
117 hsp = hexstuff; asp = asciistuff;
118 while (nshorts != 0) {
119 s1 = GET_U_1(cp);
120 cp++;
121 s2 = GET_U_1(cp);
122 cp++;
123 (void)snprintf(hsp, sizeof(hexstuff) - (hsp - hexstuff),
124 " %02x%02x", s1, s2);
125 hsp += HEXDUMP_HEXSTUFF_PER_SHORT;
126 *(asp++) = (char)(ND_ASCII_ISGRAPH(s1) ? s1 : '.');
127 *(asp++) = (char)(ND_ASCII_ISGRAPH(s2) ? s2 : '.');
128 i++;
129 if (i >= HEXDUMP_SHORTS_PER_LINE) {
130 *hsp = *asp = '\0';
131 ND_PRINT("%s0x%04x: %-*s %s",
132 ident, oset, HEXDUMP_HEXSTUFF_PER_LINE,
133 hexstuff, asciistuff);
134 i = 0; hsp = hexstuff; asp = asciistuff;
135 oset += HEXDUMP_BYTES_PER_LINE;
136 }
137 nshorts--;
138 }
139 if (length & 1) {
140 s1 = GET_U_1(cp);
141 cp++;
142 (void)snprintf(hsp, sizeof(hexstuff) - (hsp - hexstuff),
143 " %02x", s1);
144 hsp += 3;
145 *(asp++) = (char)(ND_ASCII_ISGRAPH(s1) ? s1 : '.');
146 ++i;
147 }
148 if (i > 0) {
149 *hsp = *asp = '\0';
150 ND_PRINT("%s0x%04x: %-*s %s",
151 ident, oset, HEXDUMP_HEXSTUFF_PER_LINE,
152 hexstuff, asciistuff);
153 }
154 }
155
156 void
157 hex_and_ascii_print(netdissect_options *ndo, const char *ident,
158 const u_char *cp, u_int length)
159 {
160 hex_and_ascii_print_with_offset(ndo, ident, cp, length, 0);
161 }
162
163 /*
164 * telnet_print() wants this. It is essentially default_print_unaligned()
165 */
166 void
167 hex_print_with_offset(netdissect_options *ndo,
168 const char *ident, const u_char *cp, u_int length,
169 u_int oset)
170 {
171 u_int caplength;
172 u_int i, s;
173 u_int nshorts;
174
175 caplength = (ndo->ndo_snapend > cp) ? ND_BYTES_AVAILABLE_AFTER(cp) : 0;
176 if (length > caplength)
177 length = caplength;
178 nshorts = length / sizeof(u_short);
179 i = 0;
180 while (nshorts != 0) {
181 if ((i++ % 8) == 0) {
182 ND_PRINT("%s0x%04x: ", ident, oset);
183 oset += HEXDUMP_BYTES_PER_LINE;
184 }
185 s = GET_U_1(cp);
186 cp++;
187 ND_PRINT(" %02x%02x", s, GET_U_1(cp));
188 cp++;
189 nshorts--;
190 }
191 if (length & 1) {
192 if ((i % 8) == 0)
193 ND_PRINT("%s0x%04x: ", ident, oset);
194 ND_PRINT(" %02x", GET_U_1(cp));
195 }
196 }
197
198 /*
199 * just for completeness
200 */
201 void
202 hex_print(netdissect_options *ndo,const char *ident, const u_char *cp, u_int length)
203 {
204 hex_print_with_offset(ndo, ident, cp, length, 0);
205 }
206
207 #ifdef MAIN
208 int
209 main(int argc, char *argv[])
210 {
211 hex_print("\n\t", "Hello, World!\n", 14);
212 printf("\n");
213 hex_and_ascii_print("\n\t", "Hello, World!\n", 14);
214 printf("\n");
215 ascii_print("Hello, World!\n", 14);
216 printf("\n");
217 #define TMSG "Now is the winter of our discontent...\n"
218 hex_print_with_offset("\n\t", TMSG, sizeof(TMSG) - 1, 0x100);
219 printf("\n");
220 hex_and_ascii_print_with_offset("\n\t", TMSG, sizeof(TMSG) - 1, 0x100);
221 printf("\n");
222 exit(0);
223 }
224 #endif /* MAIN */