fix usage of "if(tree) {" to display the right things, even if no coloring rule is set
[obnox/wireshark/wip.git] / epan / dissectors / packet-rmt-common.c
1 /* packet-rmt-common.c
2  * Reliable Multicast Transport (RMT)
3  * Common RMT functions
4  * Copyright 2005, Stefano Pettini <spettini@users.sourceforge.net>
5  *
6  * $Id$
7  *
8  * Ethereal - Network traffic analyzer
9  * By Gerald Combs <gerald@ethereal.com>
10  * Copyright 1998 Gerald Combs
11  *
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.
16  * 
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.
21  * 
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.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 #include <glib.h>
36
37 #include <epan/packet.h>
38 #include <epan/strutil.h>
39
40 #include "packet-rmt-common.h"
41
42 /* Boolean string tables */
43 const true_false_string boolean_set_notset = { "Set", "Not set" };
44 const true_false_string boolean_yes_no = { "Yes", "No" };
45
46 /* Common RMT exported functions */
47 /* ============================= */
48
49 /* Scan the tvb and put extensions found in an array */
50 void rmt_ext_parse(GArray *a, tvbuff_t *tvb, guint *offset, guint offset_max)
51 {
52         struct _ext e;
53         
54         while (*offset < offset_max)
55         {
56                 /* Clear the temporary extension */
57                 memset(&e, 0, sizeof(struct _ext));
58                 
59                 /* Dissect the extension */
60                 e.offset = *offset;
61                 e.het = tvb_get_guint8(tvb, *offset);
62                 
63                 if (e.het <= 127) {
64                         /* If HET <= 127, we have a variable-size extention */
65                         e.hel = tvb_get_guint8(tvb, *offset+1);
66                         e.hec_offset = *offset + 2;
67                         e.hec_size = e.hel * 4 - 2;
68                         e.length = e.hel * 4;
69                 } else {
70                         /* If HET > 127, we have a short 32-bit extention */
71                         e.hel = 1;      /* even if HEL field is not defined for HET > 127 */
72                         e.hec_offset = *offset + 1;
73                         e.hec_size = 3;
74                         e.length = 4;
75                 }
76                         
77                 /* Prevents infinite loops */
78                 if (e.length == 0)
79                         break;
80                         
81                 g_array_append_val(a, e);
82                 *offset += e.length;
83         }
84 }
85
86 /* Add default items to a subtree */
87 void rmt_ext_decode_default_header(struct _ext *e, tvbuff_t *tvb, proto_tree *tree)
88 {
89         if (tree)
90         {
91                 proto_tree_add_text(tree, tvb, e->offset, 1, "Header Extension Type (HET): %u", e->het);
92                 if (e->het <= 127)
93                         proto_tree_add_text(tree, tvb, e->offset+1, 1, "Header Extension Length (HEL): %u", e->hel);
94         }
95 }
96
97 /* Add a default subtree to a tree item */
98 void rmt_ext_decode_default_subtree(struct _ext *e, tvbuff_t *tvb, proto_item *ti, gint ett)
99 {
100         proto_tree *ext_tree;
101         
102         ext_tree = proto_item_add_subtree(ti, ett);
103         rmt_ext_decode_default_header(e, tvb, ext_tree);
104                 
105         if (ext_tree)
106                 proto_tree_add_text(ext_tree, tvb, e->hec_offset, e->hec_size,
107                         "Header Extension Content (HEC): %s", tvb_bytes_to_str(tvb, e->hec_offset, e->hec_size));
108 }
109
110 /* Add a default subtree for unknown extensions */
111 void rmt_ext_decode_default(struct _ext *e, tvbuff_t *tvb, proto_tree *tree, gint ett)
112 {
113         proto_item *ti;
114         
115         if (tree)
116         {
117                 ti = proto_tree_add_text(tree, tvb, e->offset, e->length,
118                         "Unknown extension (%u)", e->het);
119                 
120                 rmt_ext_decode_default_subtree(e, tvb, ti, ett);
121         }
122 }