2 * Routines for printing packet analysis trees.
4 * $Id: print.c,v 1.66 2003/12/09 22:27:28 ulfl 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)
135 for (src_le = src_list; src_le != NULL; src_le = src_le->next) {
138 if (fi->ds_tvb == src_tvb) {
142 return tvb_get_ptr(src_tvb, fi->start, fi->length);
145 g_assert_not_reached();
146 return NULL; /* not found */
149 #define MAX_INDENT 160
151 #define MAX_PS_LINE_LENGTH 256
153 /* Print a tree's data, and any child nodes. */
155 void proto_tree_print_node(proto_node *node, gpointer data)
157 field_info *fi = PITEM_FINFO(node);
158 print_data *pdata = (print_data*) data;
160 gchar label_str[ITEM_LABEL_LENGTH];
163 /* Don't print invisible entries. */
167 /* was a free format label produced? */
169 label_ptr = fi->rep->representation;
171 else { /* no, make a generic label */
172 label_ptr = label_str;
173 proto_item_fill_label(fi, label_str);
176 print_line(pdata->fh, pdata->level, pdata->format, label_ptr);
178 /* If it's uninterpreted data, dump it (unless our caller will
179 be printing the entire packet in hex). */
180 if (fi->hfinfo->id == proto_data && pdata->print_hex_for_data) {
182 * Find the data for this field.
184 pd = get_field_data(pdata->src_list, fi);
185 print_hex_data_buffer(pdata->fh, pd, fi->length,
186 pdata->encoding, pdata->format);
189 /* If we're printing all levels, or if this node is one with a
190 subtree and its subtree is expanded, recurse into the subtree,
192 g_assert(fi->tree_type >= -1 && fi->tree_type < num_tree_types);
193 if (pdata->print_dissections == print_dissections_expanded ||
194 (pdata->print_dissections == print_dissections_as_displayed &&
195 fi->tree_type >= 0 && tree_is_expanded[fi->tree_type])) {
196 if (node->first_child != NULL) {
198 proto_tree_children_foreach(node,
199 proto_tree_print_node, pdata);
205 /* Print a string, escaping out certain characters that need to
206 * escaped out for XML. */
208 print_escaped_xml(FILE *fh, char *unescaped_string)
212 for (p = unescaped_string; *p != '\0'; p++) {
233 print_field_hex_value(print_data *pdata, field_info *fi)
238 /* Find the data for this field. */
239 pd = get_field_data(pdata->src_list, fi);
241 /* Print a simple hex dump */
242 for (i = 0 ; i < fi->length; i++) {
243 fprintf(pdata->fh, "%02x", pd[i]);
248 /* Print a tree's data, and any child nodes, as PDML */
250 proto_tree_print_node_pdml(proto_node *node, gpointer data)
252 field_info *fi = PITEM_FINFO(node);
253 print_data *pdata = (print_data*) data;
255 gchar label_str[ITEM_LABEL_LENGTH];
256 char *dfilter_string;
260 for (i = -1; i < pdata->level; i++) {
261 fputs(" ", pdata->fh);
264 /* Text label. It's printed as a field with no name. */
265 if (fi->hfinfo->id == hf_text_only) {
268 label_ptr = fi->rep->representation;
274 fputs("<field show=\"", pdata->fh);
275 print_escaped_xml(pdata->fh, label_ptr);
277 fprintf(pdata->fh, "\" size=\"%d", fi->length);
278 fprintf(pdata->fh, "\" pos=\"%d", fi->start);
280 fputs("\" value=\"", pdata->fh);
281 print_field_hex_value(pdata, fi);
283 if (node->first_child != NULL) {
284 fputs("\">\n", pdata->fh);
287 fputs("\"/>\n", pdata->fh);
290 /* Uninterpreted data, i.e., the "Data" protocol, is
291 * printed as a field instead of a protocol. */
292 else if (fi->hfinfo->id == proto_data) {
294 fputs("<field name=\"data\" value=\"", pdata->fh);
296 print_field_hex_value(pdata, fi);
298 fputs("\"/>\n", pdata->fh);
301 /* Normal protocols and fields */
303 if (fi->hfinfo->type == FT_PROTOCOL) {
304 fputs("<proto name=\"", pdata->fh);
307 fputs("<field name=\"", pdata->fh);
309 print_escaped_xml(pdata->fh, fi->hfinfo->abbrev);
312 fputs("\" showname=\"", pdata->fh);
313 print_escaped_xml(pdata->fh, fi->rep->representation);
316 label_ptr = label_str;
317 proto_item_fill_label(fi, label_str);
318 fputs("\" showname=\"", pdata->fh);
319 print_escaped_xml(pdata->fh, label_ptr);
322 fprintf(pdata->fh, "\" size=\"%d", fi->length);
323 fprintf(pdata->fh, "\" pos=\"%d", fi->start);
324 /* fprintf(pdata->fh, "\" id=\"%d", fi->hfinfo->id);*/
326 if (fi->hfinfo->type != FT_PROTOCOL) {
329 /* XXX - this is a hack until we can juse call
330 * fvalue_to_string_repr() for *all* FT_* types. */
331 dfilter_string = proto_construct_dfilter_string(fi,
333 if (dfilter_string != NULL) {
334 chop_len = strlen(fi->hfinfo->abbrev) + 4; /* for " == " */
336 /* XXX - Remove double-quotes. Again, once we
337 * can call fvalue_to_string_repr(), we can
338 * ask it not to produce the version for
339 * display-filters, and thus, no
341 if (dfilter_string[strlen(dfilter_string)-1] == '"') {
342 dfilter_string[strlen(dfilter_string)-1] = '\0';
346 fputs("\" show=\"", pdata->fh);
347 print_escaped_xml(pdata->fh, &dfilter_string[chop_len]);
349 if (fi->length > 0) {
350 fputs("\" value=\"", pdata->fh);
351 print_field_hex_value(pdata, fi);
355 if (node->first_child != NULL) {
356 fputs("\">\n", pdata->fh);
358 else if (fi->hfinfo->id == proto_data) {
359 fputs("\">\n", pdata->fh);
362 fputs("\"/>\n", pdata->fh);
366 /* We always pring all levels for PDML. Recurse here. */
367 if (node->first_child != NULL) {
369 proto_tree_children_foreach(node,
370 proto_tree_print_node_pdml, pdata);
374 if (node->first_child != NULL) {
375 for (i = -1; i < pdata->level; i++) {
376 fputs(" ", pdata->fh);
378 if (fi->hfinfo->type == FT_PROTOCOL) {
379 fputs("</proto>\n", pdata->fh);
382 fputs("</field>\n", pdata->fh);
387 /* Print info for a 'geninfo' pseudo-protocol. This is required by
388 * the PDML spec. The information is contained in Ethereal's 'frame' protocol,
389 * but we produce a 'geninfo' protocol in the PDML to conform to spec.
390 * The 'frame' protocol follows the 'geninfo' protocol in the PDML. */
392 print_pdml_geninfo(proto_tree *tree, print_data *pdata)
394 guint32 num, len, caplen;
396 GPtrArray *finfo_array;
397 field_info *frame_finfo;
399 /* Get frame protocol's finfo. */
400 finfo_array = proto_find_finfo(tree, proto_frame);
401 if (g_ptr_array_len(finfo_array) < 1) {
404 frame_finfo = finfo_array->pdata[0];
405 g_ptr_array_free(finfo_array, FALSE);
407 /* frame.number --> geninfo.num */
408 finfo_array = proto_find_finfo(tree, hf_frame_number);
409 if (g_ptr_array_len(finfo_array) < 1) {
412 num = fvalue_get_integer(&((field_info*)finfo_array->pdata[0])->value);
413 g_ptr_array_free(finfo_array, FALSE);
415 /* frame.pkt_len --> geninfo.len */
416 finfo_array = proto_find_finfo(tree, hf_frame_packet_len);
417 if (g_ptr_array_len(finfo_array) < 1) {
420 len = fvalue_get_integer(&((field_info*)finfo_array->pdata[0])->value);
421 g_ptr_array_free(finfo_array, FALSE);
423 /* frame.cap_len --> geninfo.caplen */
424 finfo_array = proto_find_finfo(tree, hf_frame_capture_len);
425 if (g_ptr_array_len(finfo_array) < 1) {
428 caplen = fvalue_get_integer(&((field_info*)finfo_array->pdata[0])->value);
429 g_ptr_array_free(finfo_array, FALSE);
431 /* frame.time --> geninfo.timestamp */
432 finfo_array = proto_find_finfo(tree, hf_frame_arrival_time);
433 if (g_ptr_array_len(finfo_array) < 1) {
436 timestamp = fvalue_get(&((field_info*)finfo_array->pdata[0])->value);
437 g_ptr_array_free(finfo_array, FALSE);
439 /* Print geninfo start */
441 " <proto name=\"geninfo\" pos=\"1\" showname=\"General information\" size=\"%u\">\n",
442 frame_finfo->length);
444 /* Print geninfo.num */
446 " <field name=\"num\" pos=\"1\" show=\"%u\" showname=\"Number\" value=\"%x\" size=\"%u\"/>\n",
447 num, num, frame_finfo->length);
449 /* Print geninfo.len */
451 " <field name=\"len\" pos=\"1\" show=\"%u\" showname=\"Packet Length\" value=\"%x\" size=\"%u\"/>\n",
452 len, len, frame_finfo->length);
454 /* Print geninfo.caplen */
456 " <field name=\"caplen\" pos=\"1\" show=\"%u\" showname=\"Captured Length\" value=\"%x\" size=\"%u\"/>\n",
457 caplen, caplen, frame_finfo->length);
459 /* Print geninfo.timestamp */
461 " <field name=\"timestamp\" pos=\"1\" show=\"%s\" showname=\"Captured Time\" value=\"%d.%09d\" size=\"%u\"/>\n",
462 abs_time_to_str(timestamp), (int) timestamp->secs, timestamp->nsecs, frame_finfo->length);
464 /* Print geninfo end */
470 void print_hex_data(FILE *fh, gint format, epan_dissect_t *edt)
472 gboolean multiple_sources;
482 * Set "multiple_sources" iff this frame has more than one
483 * data source; if it does, we need to print the name of
484 * the data source before printing the data from the
487 multiple_sources = (edt->pi.data_src->next != NULL);
489 for (src_le = edt->pi.data_src; src_le != NULL;
490 src_le = src_le->next) {
493 if (multiple_sources) {
495 print_line(fh, 0, format, "");
496 line = g_malloc(strlen(name) + 2); /* <name>:\0 */
499 print_line(fh, 0, format, line);
502 length = tvb_length(tvb);
503 cp = tvb_get_ptr(tvb, 0, length);
504 print_hex_data_buffer(fh, cp, length,
505 edt->pi.fd->flags.encoding, format);
510 * This routine is based on a routine created by Dan Lasley
511 * <DLASLEY@PROMUS.com>.
513 * It was modified for Ethereal by Gilbert Ramirez and others.
516 #define MAX_OFFSET_LEN 8 /* max length of hex offset of bytes */
517 #define BYTES_PER_LINE 16 /* max byte values printed on a line */
518 #define HEX_DUMP_LEN (BYTES_PER_LINE*3)
519 /* max number of characters hex dump takes -
520 2 digits plus trailing blank */
521 #define DATA_DUMP_LEN (HEX_DUMP_LEN + 2 + BYTES_PER_LINE)
522 /* number of characters those bytes take;
523 3 characters per byte of hex dump,
524 2 blanks separating hex from ASCII,
525 1 character per byte of ASCII dump */
526 #define MAX_LINE_LEN (MAX_OFFSET_LEN + 2 + DATA_DUMP_LEN)
527 /* number of characters per line;
528 offset, 2 blanks separating offset
529 from data dump, data dump */
532 print_hex_data_buffer(FILE *fh, register const guchar *cp,
533 register guint length, char_enc encoding, gint format)
535 register unsigned int ad, i, j, k, l;
537 guchar line[MAX_LINE_LEN + 1];
538 unsigned int use_digits;
539 static guchar binhex[16] = {
540 '0', '1', '2', '3', '4', '5', '6', '7',
541 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
543 print_line(fh, 0, format, "");
546 * How many of the leading digits of the offset will we supply?
547 * We always supply at least 4 digits, but if the maximum offset
548 * won't fit in 4 digits, we use as many digits as will be needed.
550 if (((length - 1) & 0xF0000000) != 0)
551 use_digits = 8; /* need all 8 digits */
552 else if (((length - 1) & 0x0F000000) != 0)
553 use_digits = 7; /* need 7 digits */
554 else if (((length - 1) & 0x00F00000) != 0)
555 use_digits = 6; /* need 6 digits */
556 else if (((length - 1) & 0x000F0000) != 0)
557 use_digits = 5; /* need 5 digits */
559 use_digits = 4; /* we'll supply 4 digits */
568 * Start of a new line.
575 c = (ad >> (l*4)) & 0xF;
576 line[j++] = binhex[c];
580 memset(line+j, ' ', DATA_DUMP_LEN);
583 * Offset in line of ASCII dump.
585 k = j + HEX_DUMP_LEN + 2;
588 line[j++] = binhex[c>>4];
589 line[j++] = binhex[c&0xf];
591 if (encoding == CHAR_EBCDIC) {
592 c = EBCDIC_to_ASCII1(c);
594 line[k++] = c >= ' ' && c < 0x7f ? c : '.';
596 if ((i & 15) == 0 || i == length) {
598 * We'll be starting a new line, or
599 * we're finished printing this buffer;
600 * dump out the line we've constructed,
601 * and advance the offset.
604 print_line(fh, 0, format, line);
611 void ps_clean_string(unsigned char *out, const unsigned char *in,
617 for (rd = 0, wr = 0 ; wr < outbuf_size; rd++, wr++ ) {
638 /* Some formats need stuff at the beginning of the output */
640 print_preamble(FILE *fh, gint format)
642 if (format == PR_FMT_PS)
643 print_ps_preamble(fh);
644 else if (format == PR_FMT_PDML) {
645 fputs("<?xml version=\"1.0\"?>\n", fh);
646 fputs("<pdml version=\"" PDML_VERSION "\" ", fh);
647 fprintf(fh, "creator=\"%s/%s\">\n", PACKAGE, VERSION);
651 /* Some formats need stuff at the end of the output */
653 print_finale(FILE *fh, gint format)
655 if (format == PR_FMT_PS)
657 else if (format == PR_FMT_PDML) {
658 fputs("</pdml>\n", fh);
663 print_line(FILE *fh, int indent, gint format, char *line)
665 char space[MAX_INDENT+1];
668 char psbuffer[MAX_PS_LINE_LENGTH]; /* static sized buffer! */
670 if (format == PR_FMT_PS) {
671 ps_clean_string(psbuffer, line, MAX_PS_LINE_LENGTH);
672 fprintf(fh, "%d (%s) putline\n", indent, psbuffer);
674 else if (format == PR_FMT_TEXT) {
675 /* Prepare the tabs for printing, depending on tree level */
676 num_spaces = indent * 4;
677 if (num_spaces > MAX_INDENT) {
678 num_spaces = MAX_INDENT;
680 for (i = 0; i < num_spaces; i++) {
683 /* The string is NUL-terminated */
684 space[num_spaces] = '\0';
691 g_assert_not_reached();