2 * Routines for printing packet analysis trees.
4 * $Id: print.c,v 1.71 2004/01/25 00:58:12 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>
43 #include "packet-data.h"
44 #include "packet-frame.h"
46 #define PDML_VERSION "0"
48 static void proto_tree_print_node(proto_node *node, gpointer data);
49 static void proto_tree_print_node_pdml(proto_node *node, gpointer data);
50 static void print_hex_data_buffer(FILE *fh, register const guchar *cp,
51 register guint length, char_enc encoding, gint format);
52 static void ps_clean_string(unsigned char *out, const unsigned char *in,
59 print_dissections_e print_dissections;
60 gboolean print_hex_for_data;
66 static void print_pdml_geninfo(proto_tree *tree, print_data *pdata);
68 FILE *open_print_dest(int to_file, const char *dest)
72 /* Open the file or command for output */
74 fh = fopen(dest, "w");
76 fh = popen(dest, "w");
81 gboolean close_print_dest(int to_file, FILE *fh)
83 /* Close the file or command */
85 return (fclose(fh) == 0);
87 return (pclose(fh) == 0);
92 proto_tree_print(print_args_t *print_args, epan_dissect_t *edt,
97 /* Create the output */
100 data.src_list = edt->pi.data_src;
101 data.encoding = edt->pi.fd->flags.encoding;
102 data.print_dissections = print_args->print_dissections;
103 data.print_hex_for_data = !print_args->print_hex;
104 /* If we're printing the entire packet in hex, don't
105 print uninterpreted data fields in hex as well. */
106 data.format = print_args->format;
109 if (data.format == PR_FMT_PDML) {
111 fprintf(fh, "<packet>\n");
113 /* Print a "geninfo" protocol as required by PDML */
114 print_pdml_geninfo(edt->tree, &data);
116 proto_tree_children_foreach(edt->tree, proto_tree_print_node_pdml, &data);
118 fprintf(fh, "</packet>\n\n");
121 proto_tree_children_foreach(edt->tree, proto_tree_print_node, &data);
126 * Find the data source for a specified field, and return a pointer
129 static const guint8 *
130 get_field_data(GSList *src_list, field_info *fi)
135 gint length, tvbuff_length;
137 for (src_le = src_list; src_le != NULL; src_le = src_le->next) {
140 if (fi->ds_tvb == src_tvb) {
144 * XXX - a field can have a length that runs past
145 * the end of the tvbuff. Ideally, that should
146 * be fixed when adding an item to the protocol
147 * tree, but checking the length when doing
148 * that could be expensive. Until we fix that,
149 * we'll do the check here.
152 tvbuff_length = tvb_length_remaining(src_tvb,
154 if (length > tvbuff_length)
155 length = tvbuff_length;
156 return tvb_get_ptr(src_tvb, fi->start, length);
159 g_assert_not_reached();
160 return NULL; /* not found */
163 #define MAX_INDENT 160
165 #define MAX_PS_LINE_LENGTH 256
167 /* Print a tree's data, and any child nodes. */
169 void proto_tree_print_node(proto_node *node, gpointer data)
171 field_info *fi = PITEM_FINFO(node);
172 print_data *pdata = (print_data*) data;
174 gchar label_str[ITEM_LABEL_LENGTH];
177 /* Don't print invisible entries. */
181 /* was a free format label produced? */
183 label_ptr = fi->rep->representation;
185 else { /* no, make a generic label */
186 label_ptr = label_str;
187 proto_item_fill_label(fi, label_str);
190 print_line(pdata->fh, pdata->level, pdata->format, label_ptr);
192 /* If it's uninterpreted data, dump it (unless our caller will
193 be printing the entire packet in hex). */
194 if (fi->hfinfo->id == proto_data && pdata->print_hex_for_data) {
196 * Find the data for this field.
198 pd = get_field_data(pdata->src_list, fi);
199 print_hex_data_buffer(pdata->fh, pd, fi->length,
200 pdata->encoding, pdata->format);
203 /* If we're printing all levels, or if this node is one with a
204 subtree and its subtree is expanded, recurse into the subtree,
206 g_assert(fi->tree_type >= -1 && fi->tree_type < num_tree_types);
207 if (pdata->print_dissections == print_dissections_expanded ||
208 (pdata->print_dissections == print_dissections_as_displayed &&
209 fi->tree_type >= 0 && tree_is_expanded[fi->tree_type])) {
210 if (node->first_child != NULL) {
212 proto_tree_children_foreach(node,
213 proto_tree_print_node, pdata);
219 /* Print a string, escaping out certain characters that need to
220 * escaped out for XML. */
222 print_escaped_xml(FILE *fh, char *unescaped_string)
226 for (p = unescaped_string; *p != '\0'; p++) {
247 print_field_hex_value(print_data *pdata, field_info *fi)
252 /* Find the data for this field. */
253 pd = get_field_data(pdata->src_list, fi);
255 /* Print a simple hex dump */
256 for (i = 0 ; i < fi->length; i++) {
257 fprintf(pdata->fh, "%02x", pd[i]);
262 /* Print a tree's data, and any child nodes, as PDML */
264 proto_tree_print_node_pdml(proto_node *node, gpointer data)
266 field_info *fi = PITEM_FINFO(node);
267 print_data *pdata = (print_data*) data;
269 gchar label_str[ITEM_LABEL_LENGTH];
270 char *dfilter_string;
274 for (i = -1; i < pdata->level; i++) {
275 fputs(" ", pdata->fh);
278 /* Text label. It's printed as a field with no name. */
279 if (fi->hfinfo->id == hf_text_only) {
282 label_ptr = fi->rep->representation;
288 fputs("<field show=\"", pdata->fh);
289 print_escaped_xml(pdata->fh, label_ptr);
291 fprintf(pdata->fh, "\" size=\"%d", fi->length);
292 fprintf(pdata->fh, "\" pos=\"%d", fi->start);
294 fputs("\" value=\"", pdata->fh);
295 print_field_hex_value(pdata, fi);
297 if (node->first_child != NULL) {
298 fputs("\">\n", pdata->fh);
301 fputs("\"/>\n", pdata->fh);
304 /* Uninterpreted data, i.e., the "Data" protocol, is
305 * printed as a field instead of a protocol. */
306 else if (fi->hfinfo->id == proto_data) {
308 fputs("<field name=\"data\" value=\"", pdata->fh);
310 print_field_hex_value(pdata, fi);
312 fputs("\"/>\n", pdata->fh);
315 /* Normal protocols and fields */
317 if (fi->hfinfo->type == FT_PROTOCOL) {
318 fputs("<proto name=\"", pdata->fh);
321 fputs("<field name=\"", pdata->fh);
323 print_escaped_xml(pdata->fh, fi->hfinfo->abbrev);
326 fputs("\" showname=\"", pdata->fh);
327 print_escaped_xml(pdata->fh, fi->rep->representation);
330 label_ptr = label_str;
331 proto_item_fill_label(fi, label_str);
332 fputs("\" showname=\"", pdata->fh);
333 print_escaped_xml(pdata->fh, label_ptr);
336 fprintf(pdata->fh, "\" size=\"%d", fi->length);
337 fprintf(pdata->fh, "\" pos=\"%d", fi->start);
338 /* fprintf(pdata->fh, "\" id=\"%d", fi->hfinfo->id);*/
340 if (fi->hfinfo->type != FT_PROTOCOL) {
343 /* XXX - this is a hack until we can juse call
344 * fvalue_to_string_repr() for *all* FT_* types. */
345 dfilter_string = proto_construct_dfilter_string(fi,
347 if (dfilter_string != NULL) {
348 chop_len = strlen(fi->hfinfo->abbrev) + 4; /* for " == " */
350 /* XXX - Remove double-quotes. Again, once we
351 * can call fvalue_to_string_repr(), we can
352 * ask it not to produce the version for
353 * display-filters, and thus, no
355 if (dfilter_string[strlen(dfilter_string)-1] == '"') {
356 dfilter_string[strlen(dfilter_string)-1] = '\0';
360 fputs("\" show=\"", pdata->fh);
361 print_escaped_xml(pdata->fh, &dfilter_string[chop_len]);
363 if (fi->length > 0) {
364 fputs("\" value=\"", pdata->fh);
365 print_field_hex_value(pdata, fi);
369 if (node->first_child != NULL) {
370 fputs("\">\n", pdata->fh);
372 else if (fi->hfinfo->id == proto_data) {
373 fputs("\">\n", pdata->fh);
376 fputs("\"/>\n", pdata->fh);
380 /* We always pring all levels for PDML. Recurse here. */
381 if (node->first_child != NULL) {
383 proto_tree_children_foreach(node,
384 proto_tree_print_node_pdml, pdata);
388 if (node->first_child != NULL) {
389 for (i = -1; i < pdata->level; i++) {
390 fputs(" ", pdata->fh);
392 if (fi->hfinfo->type == FT_PROTOCOL) {
393 fputs("</proto>\n", pdata->fh);
396 fputs("</field>\n", pdata->fh);
401 /* Print info for a 'geninfo' pseudo-protocol. This is required by
402 * the PDML spec. The information is contained in Ethereal's 'frame' protocol,
403 * but we produce a 'geninfo' protocol in the PDML to conform to spec.
404 * The 'frame' protocol follows the 'geninfo' protocol in the PDML. */
406 print_pdml_geninfo(proto_tree *tree, print_data *pdata)
408 guint32 num, len, caplen;
410 GPtrArray *finfo_array;
411 field_info *frame_finfo;
413 /* Get frame protocol's finfo. */
414 finfo_array = proto_find_finfo(tree, proto_frame);
415 if (g_ptr_array_len(finfo_array) < 1) {
418 frame_finfo = finfo_array->pdata[0];
419 g_ptr_array_free(finfo_array, FALSE);
421 /* frame.number --> geninfo.num */
422 finfo_array = proto_find_finfo(tree, hf_frame_number);
423 if (g_ptr_array_len(finfo_array) < 1) {
426 num = fvalue_get_integer(&((field_info*)finfo_array->pdata[0])->value);
427 g_ptr_array_free(finfo_array, FALSE);
429 /* frame.pkt_len --> geninfo.len */
430 finfo_array = proto_find_finfo(tree, hf_frame_packet_len);
431 if (g_ptr_array_len(finfo_array) < 1) {
434 len = fvalue_get_integer(&((field_info*)finfo_array->pdata[0])->value);
435 g_ptr_array_free(finfo_array, FALSE);
437 /* frame.cap_len --> geninfo.caplen */
438 finfo_array = proto_find_finfo(tree, hf_frame_capture_len);
439 if (g_ptr_array_len(finfo_array) < 1) {
442 caplen = fvalue_get_integer(&((field_info*)finfo_array->pdata[0])->value);
443 g_ptr_array_free(finfo_array, FALSE);
445 /* frame.time --> geninfo.timestamp */
446 finfo_array = proto_find_finfo(tree, hf_frame_arrival_time);
447 if (g_ptr_array_len(finfo_array) < 1) {
450 timestamp = fvalue_get(&((field_info*)finfo_array->pdata[0])->value);
451 g_ptr_array_free(finfo_array, FALSE);
453 /* Print geninfo start */
455 " <proto name=\"geninfo\" pos=\"0\" showname=\"General information\" size=\"%u\">\n",
456 frame_finfo->length);
458 /* Print geninfo.num */
460 " <field name=\"num\" pos=\"0\" show=\"%u\" showname=\"Number\" value=\"%x\" size=\"%u\"/>\n",
461 num, num, frame_finfo->length);
463 /* Print geninfo.len */
465 " <field name=\"len\" pos=\"0\" show=\"%u\" showname=\"Packet Length\" value=\"%x\" size=\"%u\"/>\n",
466 len, len, frame_finfo->length);
468 /* Print geninfo.caplen */
470 " <field name=\"caplen\" pos=\"0\" show=\"%u\" showname=\"Captured Length\" value=\"%x\" size=\"%u\"/>\n",
471 caplen, caplen, frame_finfo->length);
473 /* Print geninfo.timestamp */
475 " <field name=\"timestamp\" pos=\"0\" show=\"%s\" showname=\"Captured Time\" value=\"%d.%09d\" size=\"%u\"/>\n",
476 abs_time_to_str(timestamp), (int) timestamp->secs, timestamp->nsecs, frame_finfo->length);
478 /* Print geninfo end */
484 void print_hex_data(FILE *fh, gint format, epan_dissect_t *edt)
486 gboolean multiple_sources;
496 * Set "multiple_sources" iff this frame has more than one
497 * data source; if it does, we need to print the name of
498 * the data source before printing the data from the
501 multiple_sources = (edt->pi.data_src->next != NULL);
503 for (src_le = edt->pi.data_src; src_le != NULL;
504 src_le = src_le->next) {
507 if (multiple_sources) {
509 print_line(fh, 0, format, "");
510 line = g_malloc(strlen(name) + 2); /* <name>:\0 */
513 print_line(fh, 0, format, line);
516 length = tvb_length(tvb);
517 cp = tvb_get_ptr(tvb, 0, length);
518 print_hex_data_buffer(fh, cp, length,
519 edt->pi.fd->flags.encoding, format);
524 * This routine is based on a routine created by Dan Lasley
525 * <DLASLEY@PROMUS.com>.
527 * It was modified for Ethereal by Gilbert Ramirez and others.
530 #define MAX_OFFSET_LEN 8 /* max length of hex offset of bytes */
531 #define BYTES_PER_LINE 16 /* max byte values printed on a line */
532 #define HEX_DUMP_LEN (BYTES_PER_LINE*3)
533 /* max number of characters hex dump takes -
534 2 digits plus trailing blank */
535 #define DATA_DUMP_LEN (HEX_DUMP_LEN + 2 + BYTES_PER_LINE)
536 /* number of characters those bytes take;
537 3 characters per byte of hex dump,
538 2 blanks separating hex from ASCII,
539 1 character per byte of ASCII dump */
540 #define MAX_LINE_LEN (MAX_OFFSET_LEN + 2 + DATA_DUMP_LEN)
541 /* number of characters per line;
542 offset, 2 blanks separating offset
543 from data dump, data dump */
546 print_hex_data_buffer(FILE *fh, register const guchar *cp,
547 register guint length, char_enc encoding, gint format)
549 register unsigned int ad, i, j, k, l;
551 guchar line[MAX_LINE_LEN + 1];
552 unsigned int use_digits;
553 static guchar binhex[16] = {
554 '0', '1', '2', '3', '4', '5', '6', '7',
555 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
557 print_line(fh, 0, format, "");
560 * How many of the leading digits of the offset will we supply?
561 * We always supply at least 4 digits, but if the maximum offset
562 * won't fit in 4 digits, we use as many digits as will be needed.
564 if (((length - 1) & 0xF0000000) != 0)
565 use_digits = 8; /* need all 8 digits */
566 else if (((length - 1) & 0x0F000000) != 0)
567 use_digits = 7; /* need 7 digits */
568 else if (((length - 1) & 0x00F00000) != 0)
569 use_digits = 6; /* need 6 digits */
570 else if (((length - 1) & 0x000F0000) != 0)
571 use_digits = 5; /* need 5 digits */
573 use_digits = 4; /* we'll supply 4 digits */
582 * Start of a new line.
589 c = (ad >> (l*4)) & 0xF;
590 line[j++] = binhex[c];
594 memset(line+j, ' ', DATA_DUMP_LEN);
597 * Offset in line of ASCII dump.
599 k = j + HEX_DUMP_LEN + 2;
602 line[j++] = binhex[c>>4];
603 line[j++] = binhex[c&0xf];
605 if (encoding == CHAR_EBCDIC) {
606 c = EBCDIC_to_ASCII1(c);
608 line[k++] = c >= ' ' && c < 0x7f ? c : '.';
610 if ((i & 15) == 0 || i == length) {
612 * We'll be starting a new line, or
613 * we're finished printing this buffer;
614 * dump out the line we've constructed,
615 * and advance the offset.
618 print_line(fh, 0, format, line);
625 void ps_clean_string(unsigned char *out, const unsigned char *in,
631 for (rd = 0, wr = 0 ; wr < outbuf_size; rd++, wr++ ) {
652 /* Some formats need stuff at the beginning of the output */
654 print_preamble(FILE *fh, gint format)
656 if (format == PR_FMT_PS)
657 print_ps_preamble(fh);
658 else if (format == PR_FMT_PDML) {
659 fputs("<?xml version=\"1.0\"?>\n", fh);
660 fputs("<pdml version=\"" PDML_VERSION "\" ", fh);
661 fprintf(fh, "creator=\"%s/%s\">\n", PACKAGE, VERSION);
665 /* Some formats need stuff at the end of the output */
667 print_finale(FILE *fh, gint format)
669 if (format == PR_FMT_PS)
671 else if (format == PR_FMT_PDML) {
672 fputs("</pdml>\n", fh);
677 print_line(FILE *fh, int indent, gint format, char *line)
679 char space[MAX_INDENT+1];
682 char psbuffer[MAX_PS_LINE_LENGTH]; /* static sized buffer! */
684 if (format == PR_FMT_PS) {
685 ps_clean_string(psbuffer, line, MAX_PS_LINE_LENGTH);
686 fprintf(fh, "%d (%s) putline\n", indent, psbuffer);
688 else if (format == PR_FMT_TEXT) {
689 /* Prepare the tabs for printing, depending on tree level */
690 num_spaces = indent * 4;
691 if (num_spaces > MAX_INDENT) {
692 num_spaces = MAX_INDENT;
694 for (i = 0; i < num_spaces; i++) {
697 /* The string is NUL-terminated */
698 space[num_spaces] = '\0';
705 g_assert_not_reached();