2 * Routines for printing packet analysis trees.
4 * $Id: print.c,v 1.35 2001/06/08 10:07:55 guy Exp $
6 * Gilbert Ramirez <gram@xiexie.org>
8 * Ethereal - Network traffic analyzer
9 * By Gerald Combs <gerald@ethereal.com>
10 * Copyright 1998 Gerald Combs
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
34 #ifdef HAVE_SYS_TYPES_H
35 # include <sys/types.h>
44 static void proto_tree_print_node_text(GNode *node, gpointer data);
45 static void proto_tree_print_node_ps(GNode *node, gpointer data);
46 static void ps_clean_string(unsigned char *out, const unsigned char *in,
48 static void print_hex_data_text(FILE *fh, register const u_char *cp,
49 register u_int length, char_enc encoding);
50 static void print_hex_data_ps(FILE *fh, register const u_char *cp,
51 register u_int length, char_enc encoding);
53 extern int proto_data; /* in packet-data.c */
60 gboolean print_all_levels;
61 gboolean print_hex_for_data;
65 FILE *open_print_dest(int to_file, const char *dest)
69 /* Open the file or command for output */
71 fh = fopen(dest, "w");
73 fh = popen(dest, "w");
78 void close_print_dest(int to_file, FILE *fh)
80 /* Close the file or command */
87 void print_preamble(FILE *fh, gint format)
89 if (format == PR_FMT_PS)
90 print_ps_preamble(fh);
93 void print_finale(FILE *fh, gint format)
95 if (format == PR_FMT_PS)
99 void proto_tree_print(gboolean print_one_packet, print_args_t *print_args,
100 GNode *protocol_tree, frame_data *fd, FILE *fh)
104 /* Create the output */
107 data.src_list = fd->data_src;
108 data.encoding = fd->flags.encoding;
109 data.print_all_levels = print_args->expand_all;
110 data.print_hex_for_data = !print_args->print_hex;
111 /* If we're printing the entire packet in hex, don't
112 print uninterpreted data fields in hex as well. */
114 if (print_args->format == PR_FMT_TEXT) {
115 g_node_children_foreach((GNode*) protocol_tree, G_TRAVERSE_ALL,
116 proto_tree_print_node_text, &data);
118 g_node_children_foreach((GNode*) protocol_tree, G_TRAVERSE_ALL,
119 proto_tree_print_node_ps, &data);
124 * Find the data source tvbuff with a specified name, and return a
125 * pointer to the data in it.
127 static const guint8 *
128 get_field_data(GSList *src_list, field_info *fi)
133 for (src = src_list; src != NULL; src = g_slist_next(src)) {
135 if (strcmp(fi->ds_name, tvb_get_name(src_tvb)) == 0) {
139 return tvb_get_ptr(src_tvb, fi->start, fi->length);
142 g_assert_not_reached();
143 return NULL; /* not found */
146 /* Print a tree's data, and any child nodes, in plain text */
148 void proto_tree_print_node_text(GNode *node, gpointer data)
150 field_info *fi = (field_info*) (node->data);
151 print_data *pdata = (print_data*) data;
156 gchar label_str[ITEM_LABEL_LENGTH];
159 /* Don't print invisible entries. */
163 /* was a free format label produced? */
164 if (fi->representation) {
165 label_ptr = fi->representation;
167 else { /* no, make a generic label */
168 label_ptr = label_str;
169 proto_item_fill_label(fi, label_str);
172 /* Prepare the tabs for printing, depending on tree level */
173 num_spaces = pdata->level * 4;
174 if (num_spaces > 40) {
177 for (i = 0; i < num_spaces; i++) {
180 /* The string is NUL-terminated */
181 space[num_spaces] = 0;
184 fprintf(pdata->fh, "%s%s\n", space, label_ptr);
186 /* If it's uninterpreted data, dump it (unless our caller will
187 be printing the entire packet in hex). */
188 if (fi->hfinfo->id == proto_data && pdata->print_hex_for_data) {
190 * Find the data for this field.
192 pd = get_field_data(pdata->src_list, fi);
193 print_hex_data_text(pdata->fh, pd, fi->length, pdata->encoding);
196 /* If we're printing all levels, or if this level is expanded,
197 recurse into the subtree, if it exists. */
198 if (pdata->print_all_levels || tree_is_expanded[fi->tree_type]) {
199 if (g_node_n_children(node) > 0) {
201 g_node_children_foreach(node, G_TRAVERSE_ALL,
202 proto_tree_print_node_text, pdata);
208 void print_hex_data(FILE *fh, gint format, frame_data *fd)
210 gboolean multiple_sources;
219 * Set "multiple_sources" iff this frame has more than one
220 * data source; if it does, we need to print the name of
221 * the data source before printing the data from the
224 multiple_sources = (fd->data_src->next != NULL);
226 for (src = fd->data_src; src != NULL; src = src->next) {
228 if (multiple_sources) {
229 name = tvb_get_name(tvb);
230 print_line(fh, format, "\n");
231 line = g_malloc(strlen(name) + 3); /* <name>:\n\0 */
234 print_line(fh, format, line);
237 length = tvb_length(tvb);
238 cp = tvb_get_ptr(tvb, 0, length);
239 if (format == PR_FMT_PS)
240 print_hex_data_ps(fh, cp, length, fd->flags.encoding);
242 print_hex_data_text(fh, cp, length, fd->flags.encoding);
246 /* This routine was created by Dan Lasley <DLASLEY@PROMUS.com>, and
247 only slightly modified for ethereal by Gilbert Ramirez. */
249 void print_hex_data_text(FILE *fh, register const u_char *cp,
250 register u_int length, char_enc encoding)
252 register int ad, i, j, k;
255 static u_char binhex[16] = {
256 '0', '1', '2', '3', '4', '5', '6', '7',
257 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
259 memset (line, ' ', sizeof line);
260 line[sizeof (line)-1] = 0;
261 for (ad=i=j=k=0; i<length; i++) {
263 line[j++] = binhex[c>>4];
264 line[j++] = binhex[c&0xf];
266 if (encoding == CHAR_EBCDIC) {
267 c = EBCDIC_to_ASCII1(c);
269 line[50+k++] = c >= ' ' && c < 0x7f ? c : '.';
270 if ((i & 15) == 15) {
271 fprintf (fh, "\n%04x %s", ad, line);
272 /*if (i==15) printf (" %d", length);*/
273 memset (line, ' ', sizeof line);
274 line[sizeof (line)-1] = j = k = 0;
279 if (line[0] != ' ') fprintf (fh, "\n%04x %s", ad, line);
285 #define MAX_LINE_LENGTH 256
287 /* Print a node's data, and any child nodes, in PostScript */
289 void proto_tree_print_node_ps(GNode *node, gpointer data)
291 field_info *fi = (field_info*) (node->data);
292 print_data *pdata = (print_data*) data;
293 gchar label_str[ITEM_LABEL_LENGTH];
296 char psbuffer[MAX_LINE_LENGTH]; /* static sized buffer! */
301 /* was a free format label produced? */
302 if (fi->representation) {
303 label_ptr = fi->representation;
305 else { /* no, make a generic label */
306 label_ptr = label_str;
307 proto_item_fill_label(fi, label_str);
311 ps_clean_string(psbuffer, label_ptr, MAX_LINE_LENGTH);
312 fprintf(pdata->fh, "%d (%s) putline\n", pdata->level, psbuffer);
314 /* If it's uninterpreted data, dump it (unless our caller will
315 be printing the entire packet in hex). */
316 if (fi->hfinfo->id == proto_data && pdata->print_hex_for_data) {
318 * Find the data for this field.
320 pd = get_field_data(pdata->src_list, fi);
321 print_hex_data_ps(pdata->fh, pd, fi->length, pdata->encoding);
324 /* Recurse into the subtree, if it exists */
325 if (g_node_n_children(node) > 0) {
327 g_node_children_foreach(node, G_TRAVERSE_ALL,
328 proto_tree_print_node_ps, pdata);
334 void ps_clean_string(unsigned char *out, const unsigned char *in,
340 for (rd = 0, wr = 0 ; wr < outbuf_size; rd++, wr++ ) {
362 void print_hex_data_ps(FILE *fh, register const u_char *cp,
363 register u_int length, char_enc encoding)
365 register int ad, i, j, k;
368 static u_char binhex[16] = {
369 '0', '1', '2', '3', '4', '5', '6', '7',
370 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
371 u_char psline[MAX_LINE_LENGTH];
374 memset (line, ' ', sizeof line);
375 line[sizeof (line)-1] = 0;
376 for (ad=i=j=k=0; i<length; i++) {
378 line[j++] = binhex[c>>4];
379 line[j++] = binhex[c&0xf];
381 if (encoding == CHAR_EBCDIC) {
382 c = EBCDIC_to_ASCII1(c);
384 line[42+k++] = c >= ' ' && c < 0x7f ? c : '.';
385 if ((i & 15) == 15) {
386 ps_clean_string(psline, line, MAX_LINE_LENGTH);
387 fprintf (fh, "(%4x %s) hexdump\n", ad, psline);
388 memset (line, ' ', sizeof line);
389 line[sizeof (line)-1] = j = k = 0;
394 if (line[0] != ' ') {
395 ps_clean_string(psline, line, MAX_LINE_LENGTH);
396 fprintf (fh, "(%4x %s) hexdump\n", ad, psline);
402 void print_line(FILE *fh, gint format, char *line)
404 char psbuffer[MAX_LINE_LENGTH]; /* static sized buffer! */
406 if (format == PR_FMT_PS) {
407 ps_clean_string(psbuffer, line, MAX_LINE_LENGTH);
408 fprintf(fh, "(%s) hexdump\n", psbuffer);