2 * Routines for printing packet analysis trees.
4 * $Id: print.c,v 1.68 2003/12/30 23:13:32 guy Exp $
6 * Gilbert Ramirez <gram@alumni.rice.edu>
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 #include <epan/epan.h>
35 #include <epan/epan_dissect.h>
36 #include <epan/tvbuff.h>
37 #include <epan/packet.h>
42 #include "packet-data.h"
43 #include "packet-frame.h"
45 #define PDML_VERSION "0"
47 static void proto_tree_print_node(proto_node *node, gpointer data);
48 static void proto_tree_print_node_pdml(proto_node *node, gpointer data);
49 static void print_hex_data_buffer(FILE *fh, register const guchar *cp,
50 register guint length, char_enc encoding, gint format);
51 static void ps_clean_string(unsigned char *out, const unsigned char *in,
58 print_dissections_e print_dissections;
59 gboolean print_hex_for_data;
65 static void print_pdml_geninfo(proto_tree *tree, print_data *pdata);
67 FILE *open_print_dest(int to_file, const char *dest)
71 /* Open the file or command for output */
73 fh = fopen(dest, "w");
75 fh = popen(dest, "w");
80 void close_print_dest(int to_file, FILE *fh)
82 /* Close the file or command */
91 proto_tree_print(print_args_t *print_args, epan_dissect_t *edt,
96 /* Create the output */
99 data.src_list = edt->pi.data_src;
100 data.encoding = edt->pi.fd->flags.encoding;
101 data.print_dissections = print_args->print_dissections;
102 data.print_hex_for_data = !print_args->print_hex;
103 /* If we're printing the entire packet in hex, don't
104 print uninterpreted data fields in hex as well. */
105 data.format = print_args->format;
108 if (data.format == PR_FMT_PDML) {
110 fprintf(fh, "<packet>\n");
112 /* Print a "geninfo" protocol as required by PDML */
113 print_pdml_geninfo(edt->tree, &data);
115 proto_tree_children_foreach(edt->tree, proto_tree_print_node_pdml, &data);
117 fprintf(fh, "</packet>\n\n");
120 proto_tree_children_foreach(edt->tree, proto_tree_print_node, &data);
125 * Find the data source for a specified field, and return a pointer
128 static const guint8 *
129 get_field_data(GSList *src_list, field_info *fi)
134 gint length, tvbuff_length;
136 for (src_le = src_list; src_le != NULL; src_le = src_le->next) {
139 if (fi->ds_tvb == src_tvb) {
143 * XXX - a field can have a length that runs past
144 * the end of the tvbuff. Ideally, that should
145 * be fixed when adding an item to the protocol
146 * tree, but checking the length when doing
147 * that could be expensive. Until we fix that,
148 * we'll do the check here.
151 tvbuff_length = tvb_length_remaining(src_tvb,
153 if (length > tvbuff_length)
154 length = tvbuff_length;
155 return tvb_get_ptr(src_tvb, fi->start, length);
158 g_assert_not_reached();
159 return NULL; /* not found */
162 #define MAX_INDENT 160
164 #define MAX_PS_LINE_LENGTH 256
166 /* Print a tree's data, and any child nodes. */
168 void proto_tree_print_node(proto_node *node, gpointer data)
170 field_info *fi = PITEM_FINFO(node);
171 print_data *pdata = (print_data*) data;
173 gchar label_str[ITEM_LABEL_LENGTH];
176 /* Don't print invisible entries. */
180 /* was a free format label produced? */
182 label_ptr = fi->rep->representation;
184 else { /* no, make a generic label */
185 label_ptr = label_str;
186 proto_item_fill_label(fi, label_str);
189 print_line(pdata->fh, pdata->level, pdata->format, label_ptr);
191 /* If it's uninterpreted data, dump it (unless our caller will
192 be printing the entire packet in hex). */
193 if (fi->hfinfo->id == proto_data && pdata->print_hex_for_data) {
195 * Find the data for this field.
197 pd = get_field_data(pdata->src_list, fi);
198 print_hex_data_buffer(pdata->fh, pd, fi->length,
199 pdata->encoding, pdata->format);
202 /* If we're printing all levels, or if this node is one with a
203 subtree and its subtree is expanded, recurse into the subtree,
205 g_assert(fi->tree_type >= -1 && fi->tree_type < num_tree_types);
206 if (pdata->print_dissections == print_dissections_expanded ||
207 (pdata->print_dissections == print_dissections_as_displayed &&
208 fi->tree_type >= 0 && tree_is_expanded[fi->tree_type])) {
209 if (node->first_child != NULL) {
211 proto_tree_children_foreach(node,
212 proto_tree_print_node, pdata);
218 /* Print a string, escaping out certain characters that need to
219 * escaped out for XML. */
221 print_escaped_xml(FILE *fh, char *unescaped_string)
225 for (p = unescaped_string; *p != '\0'; p++) {
246 print_field_hex_value(print_data *pdata, field_info *fi)
251 /* Find the data for this field. */
252 pd = get_field_data(pdata->src_list, fi);
254 /* Print a simple hex dump */
255 for (i = 0 ; i < fi->length; i++) {
256 fprintf(pdata->fh, "%02x", pd[i]);
261 /* Print a tree's data, and any child nodes, as PDML */
263 proto_tree_print_node_pdml(proto_node *node, gpointer data)
265 field_info *fi = PITEM_FINFO(node);
266 print_data *pdata = (print_data*) data;
268 gchar label_str[ITEM_LABEL_LENGTH];
269 char *dfilter_string;
273 for (i = -1; i < pdata->level; i++) {
274 fputs(" ", pdata->fh);
277 /* Text label. It's printed as a field with no name. */
278 if (fi->hfinfo->id == hf_text_only) {
281 label_ptr = fi->rep->representation;
287 fputs("<field show=\"", pdata->fh);
288 print_escaped_xml(pdata->fh, label_ptr);
290 fprintf(pdata->fh, "\" size=\"%d", fi->length);
291 fprintf(pdata->fh, "\" pos=\"%d", fi->start);
293 fputs("\" value=\"", pdata->fh);
294 print_field_hex_value(pdata, fi);
296 if (node->first_child != NULL) {
297 fputs("\">\n", pdata->fh);
300 fputs("\"/>\n", pdata->fh);
303 /* Uninterpreted data, i.e., the "Data" protocol, is
304 * printed as a field instead of a protocol. */
305 else if (fi->hfinfo->id == proto_data) {
307 fputs("<field name=\"data\" value=\"", pdata->fh);
309 print_field_hex_value(pdata, fi);
311 fputs("\"/>\n", pdata->fh);
314 /* Normal protocols and fields */
316 if (fi->hfinfo->type == FT_PROTOCOL) {
317 fputs("<proto name=\"", pdata->fh);
320 fputs("<field name=\"", pdata->fh);
322 print_escaped_xml(pdata->fh, fi->hfinfo->abbrev);
325 fputs("\" showname=\"", pdata->fh);
326 print_escaped_xml(pdata->fh, fi->rep->representation);
329 label_ptr = label_str;
330 proto_item_fill_label(fi, label_str);
331 fputs("\" showname=\"", pdata->fh);
332 print_escaped_xml(pdata->fh, label_ptr);
335 fprintf(pdata->fh, "\" size=\"%d", fi->length);
336 fprintf(pdata->fh, "\" pos=\"%d", fi->start);
337 /* fprintf(pdata->fh, "\" id=\"%d", fi->hfinfo->id);*/
339 if (fi->hfinfo->type != FT_PROTOCOL) {
342 /* XXX - this is a hack until we can juse call
343 * fvalue_to_string_repr() for *all* FT_* types. */
344 dfilter_string = proto_construct_dfilter_string(fi,
346 if (dfilter_string != NULL) {
347 chop_len = strlen(fi->hfinfo->abbrev) + 4; /* for " == " */
349 /* XXX - Remove double-quotes. Again, once we
350 * can call fvalue_to_string_repr(), we can
351 * ask it not to produce the version for
352 * display-filters, and thus, no
354 if (dfilter_string[strlen(dfilter_string)-1] == '"') {
355 dfilter_string[strlen(dfilter_string)-1] = '\0';
359 fputs("\" show=\"", pdata->fh);
360 print_escaped_xml(pdata->fh, &dfilter_string[chop_len]);
362 if (fi->length > 0) {
363 fputs("\" value=\"", pdata->fh);
364 print_field_hex_value(pdata, fi);
368 if (node->first_child != NULL) {
369 fputs("\">\n", pdata->fh);
371 else if (fi->hfinfo->id == proto_data) {
372 fputs("\">\n", pdata->fh);
375 fputs("\"/>\n", pdata->fh);
379 /* We always pring all levels for PDML. Recurse here. */
380 if (node->first_child != NULL) {
382 proto_tree_children_foreach(node,
383 proto_tree_print_node_pdml, pdata);
387 if (node->first_child != NULL) {
388 for (i = -1; i < pdata->level; i++) {
389 fputs(" ", pdata->fh);
391 if (fi->hfinfo->type == FT_PROTOCOL) {
392 fputs("</proto>\n", pdata->fh);
395 fputs("</field>\n", pdata->fh);
400 /* Print info for a 'geninfo' pseudo-protocol. This is required by
401 * the PDML spec. The information is contained in Ethereal's 'frame' protocol,
402 * but we produce a 'geninfo' protocol in the PDML to conform to spec.
403 * The 'frame' protocol follows the 'geninfo' protocol in the PDML. */
405 print_pdml_geninfo(proto_tree *tree, print_data *pdata)
407 guint32 num, len, caplen;
409 GPtrArray *finfo_array;
410 field_info *frame_finfo;
412 /* Get frame protocol's finfo. */
413 finfo_array = proto_find_finfo(tree, proto_frame);
414 if (g_ptr_array_len(finfo_array) < 1) {
417 frame_finfo = finfo_array->pdata[0];
418 g_ptr_array_free(finfo_array, FALSE);
420 /* frame.number --> geninfo.num */
421 finfo_array = proto_find_finfo(tree, hf_frame_number);
422 if (g_ptr_array_len(finfo_array) < 1) {
425 num = fvalue_get_integer(&((field_info*)finfo_array->pdata[0])->value);
426 g_ptr_array_free(finfo_array, FALSE);
428 /* frame.pkt_len --> geninfo.len */
429 finfo_array = proto_find_finfo(tree, hf_frame_packet_len);
430 if (g_ptr_array_len(finfo_array) < 1) {
433 len = fvalue_get_integer(&((field_info*)finfo_array->pdata[0])->value);
434 g_ptr_array_free(finfo_array, FALSE);
436 /* frame.cap_len --> geninfo.caplen */
437 finfo_array = proto_find_finfo(tree, hf_frame_capture_len);
438 if (g_ptr_array_len(finfo_array) < 1) {
441 caplen = fvalue_get_integer(&((field_info*)finfo_array->pdata[0])->value);
442 g_ptr_array_free(finfo_array, FALSE);
444 /* frame.time --> geninfo.timestamp */
445 finfo_array = proto_find_finfo(tree, hf_frame_arrival_time);
446 if (g_ptr_array_len(finfo_array) < 1) {
449 timestamp = fvalue_get(&((field_info*)finfo_array->pdata[0])->value);
450 g_ptr_array_free(finfo_array, FALSE);
452 /* Print geninfo start */
454 " <proto name=\"geninfo\" pos=\"0\" showname=\"General information\" size=\"%u\">\n",
455 frame_finfo->length);
457 /* Print geninfo.num */
459 " <field name=\"num\" pos=\"0\" show=\"%u\" showname=\"Number\" value=\"%x\" size=\"%u\"/>\n",
460 num, num, frame_finfo->length);
462 /* Print geninfo.len */
464 " <field name=\"len\" pos=\"0\" show=\"%u\" showname=\"Packet Length\" value=\"%x\" size=\"%u\"/>\n",
465 len, len, frame_finfo->length);
467 /* Print geninfo.caplen */
469 " <field name=\"caplen\" pos=\"0\" show=\"%u\" showname=\"Captured Length\" value=\"%x\" size=\"%u\"/>\n",
470 caplen, caplen, frame_finfo->length);
472 /* Print geninfo.timestamp */
474 " <field name=\"timestamp\" pos=\"0\" show=\"%s\" showname=\"Captured Time\" value=\"%d.%09d\" size=\"%u\"/>\n",
475 abs_time_to_str(timestamp), (int) timestamp->secs, timestamp->nsecs, frame_finfo->length);
477 /* Print geninfo end */
483 void print_hex_data(FILE *fh, gint format, epan_dissect_t *edt)
485 gboolean multiple_sources;
495 * Set "multiple_sources" iff this frame has more than one
496 * data source; if it does, we need to print the name of
497 * the data source before printing the data from the
500 multiple_sources = (edt->pi.data_src->next != NULL);
502 for (src_le = edt->pi.data_src; src_le != NULL;
503 src_le = src_le->next) {
506 if (multiple_sources) {
508 print_line(fh, 0, format, "");
509 line = g_malloc(strlen(name) + 2); /* <name>:\0 */
512 print_line(fh, 0, format, line);
515 length = tvb_length(tvb);
516 cp = tvb_get_ptr(tvb, 0, length);
517 print_hex_data_buffer(fh, cp, length,
518 edt->pi.fd->flags.encoding, format);
523 * This routine is based on a routine created by Dan Lasley
524 * <DLASLEY@PROMUS.com>.
526 * It was modified for Ethereal by Gilbert Ramirez and others.
529 #define MAX_OFFSET_LEN 8 /* max length of hex offset of bytes */
530 #define BYTES_PER_LINE 16 /* max byte values printed on a line */
531 #define HEX_DUMP_LEN (BYTES_PER_LINE*3)
532 /* max number of characters hex dump takes -
533 2 digits plus trailing blank */
534 #define DATA_DUMP_LEN (HEX_DUMP_LEN + 2 + BYTES_PER_LINE)
535 /* number of characters those bytes take;
536 3 characters per byte of hex dump,
537 2 blanks separating hex from ASCII,
538 1 character per byte of ASCII dump */
539 #define MAX_LINE_LEN (MAX_OFFSET_LEN + 2 + DATA_DUMP_LEN)
540 /* number of characters per line;
541 offset, 2 blanks separating offset
542 from data dump, data dump */
545 print_hex_data_buffer(FILE *fh, register const guchar *cp,
546 register guint length, char_enc encoding, gint format)
548 register unsigned int ad, i, j, k, l;
550 guchar line[MAX_LINE_LEN + 1];
551 unsigned int use_digits;
552 static guchar binhex[16] = {
553 '0', '1', '2', '3', '4', '5', '6', '7',
554 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
556 print_line(fh, 0, format, "");
559 * How many of the leading digits of the offset will we supply?
560 * We always supply at least 4 digits, but if the maximum offset
561 * won't fit in 4 digits, we use as many digits as will be needed.
563 if (((length - 1) & 0xF0000000) != 0)
564 use_digits = 8; /* need all 8 digits */
565 else if (((length - 1) & 0x0F000000) != 0)
566 use_digits = 7; /* need 7 digits */
567 else if (((length - 1) & 0x00F00000) != 0)
568 use_digits = 6; /* need 6 digits */
569 else if (((length - 1) & 0x000F0000) != 0)
570 use_digits = 5; /* need 5 digits */
572 use_digits = 4; /* we'll supply 4 digits */
581 * Start of a new line.
588 c = (ad >> (l*4)) & 0xF;
589 line[j++] = binhex[c];
593 memset(line+j, ' ', DATA_DUMP_LEN);
596 * Offset in line of ASCII dump.
598 k = j + HEX_DUMP_LEN + 2;
601 line[j++] = binhex[c>>4];
602 line[j++] = binhex[c&0xf];
604 if (encoding == CHAR_EBCDIC) {
605 c = EBCDIC_to_ASCII1(c);
607 line[k++] = c >= ' ' && c < 0x7f ? c : '.';
609 if ((i & 15) == 0 || i == length) {
611 * We'll be starting a new line, or
612 * we're finished printing this buffer;
613 * dump out the line we've constructed,
614 * and advance the offset.
617 print_line(fh, 0, format, line);
624 void ps_clean_string(unsigned char *out, const unsigned char *in,
630 for (rd = 0, wr = 0 ; wr < outbuf_size; rd++, wr++ ) {
651 /* Some formats need stuff at the beginning of the output */
653 print_preamble(FILE *fh, gint format)
655 if (format == PR_FMT_PS)
656 print_ps_preamble(fh);
657 else if (format == PR_FMT_PDML) {
658 fputs("<?xml version=\"1.0\"?>\n", fh);
659 fputs("<pdml version=\"" PDML_VERSION "\" ", fh);
660 fprintf(fh, "creator=\"%s/%s\">\n", PACKAGE, VERSION);
664 /* Some formats need stuff at the end of the output */
666 print_finale(FILE *fh, gint format)
668 if (format == PR_FMT_PS)
670 else if (format == PR_FMT_PDML) {
671 fputs("</pdml>\n", fh);
676 print_line(FILE *fh, int indent, gint format, char *line)
678 char space[MAX_INDENT+1];
681 char psbuffer[MAX_PS_LINE_LENGTH]; /* static sized buffer! */
683 if (format == PR_FMT_PS) {
684 ps_clean_string(psbuffer, line, MAX_PS_LINE_LENGTH);
685 fprintf(fh, "%d (%s) putline\n", indent, psbuffer);
687 else if (format == PR_FMT_TEXT) {
688 /* Prepare the tabs for printing, depending on tree level */
689 num_spaces = indent * 4;
690 if (num_spaces > MAX_INDENT) {
691 num_spaces = MAX_INDENT;
693 for (i = 0; i < num_spaces; i++) {
696 /* The string is NUL-terminated */
697 space[num_spaces] = '\0';
704 g_assert_not_reached();