fix usage of "if(tree) {" to display the right things, even if no coloring rule is set
[obnox/wireshark/wip.git] / epan / dissectors / packet-diffserv-mpls-common.c
1 /* packet-diffserv-mpls-common.c
2  * Routines for the common part of Diffserv MPLS signaling protocols
3  * Author: Endoh Akira (endoh@netmarks.co.jp)
4  *
5  * $Id$
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@ethereal.com>
9  * Copyright 1998 Gerald Combs
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  * 
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  * 
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24  */
25
26 /*
27  * This module defines routines only for the common part of LDP
28  * and RSVP to support for Diffserv MPLS as described in RFC 3270
29  * and RFC 3140. Protocol specific routines of each signaling
30  * protocol are defined in each dissector.
31  */
32
33 #ifdef HAVE_CONFIG_H
34 # include "config.h"
35 #endif
36
37 #include <glib.h>
38 #include <epan/packet.h>
39 #include "packet-diffserv-mpls-common.h"
40
41 #define hf_map         *hfindexes[0]
42 #define hf_exp         *hfindexes[1]
43 #define hf_phbid       *hfindexes[2]
44 #define hf_phbid_dscp  *hfindexes[3]
45 #define hf_phbid_code  *hfindexes[4]
46 #define hf_phbid_bit14 *hfindexes[5]
47 #define hf_phbid_bit15 *hfindexes[6]
48 #define ett_map        *etts[0]
49 #define ett_map_phbid  *etts[1]
50
51 const value_string phbid_bit14_vals[] = {
52     {0, "Single PHB"},
53     {1, "Set of PHBs"},
54     {0, NULL}
55 };
56
57 const value_string phbid_bit15_vals[] = {
58     {0, "PHBs defined by standards action"},
59     {1, "PHBs not defined by standards action"},
60     {0, NULL}
61 };
62
63 void
64 dissect_diffserv_mpls_common(tvbuff_t *tvb, proto_tree *tree, int type, 
65                              int offset, int **hfindexes, gint **etts)
66 {
67     proto_item  *ti = NULL, *sub_ti;
68     proto_tree  *tree2 = NULL, *phbid_subtree;
69     int exp;
70     guint16 phbid;
71
72     switch (type) {
73     case 1:  /* E-LSP */
74         ti = proto_tree_add_item(tree, hf_map, tvb, offset, 4, FALSE);
75         tree2 = proto_item_add_subtree(ti, ett_map);
76         proto_item_set_text(ti, "MAP: ");
77         offset ++;
78         exp = tvb_get_guint8(tvb, offset) & 7;
79         proto_tree_add_uint(tree2, hf_exp, tvb, offset, 1, exp);
80         proto_item_append_text(ti, "EXP %u, ", exp);
81         offset ++;
82         break;
83     case 2:  /* L-LSP */
84         tree2 = tree;
85         break;
86     default:
87         return;
88     }
89
90     /* PHBID subtree */
91     sub_ti = proto_tree_add_item(tree2, hf_phbid, tvb, offset, 2, FALSE);
92     phbid_subtree = proto_item_add_subtree(sub_ti, ett_map_phbid);
93     proto_item_set_text(sub_ti, "%s: ", (type == 1) ? "PHBID" : "PSC");
94     phbid = tvb_get_ntohs(tvb, offset);
95
96     if ((phbid & 1) == 0) {
97         /* Case 1 of RFC 3140 */
98         proto_tree_add_uint(phbid_subtree, hf_phbid_dscp,
99                             tvb, offset, 2, phbid);
100         if (type == 1)
101             proto_item_append_text(ti, "DSCP %u", phbid >> 10);
102         proto_item_append_text(sub_ti, "DSCP %u", phbid >> 10);
103     }
104     else {
105         /* Case 2 of RFC 3140 */
106         proto_tree_add_uint(phbid_subtree, hf_phbid_code,
107                             tvb, offset, 2, phbid);
108         if (type == 1)
109             proto_item_append_text(ti, "PHB id code %u", phbid >> 4);
110         proto_item_append_text(sub_ti, "PHB id code %u", phbid >> 4);
111     }
112     proto_tree_add_uint(phbid_subtree, hf_phbid_bit14, tvb, offset, 2, phbid);
113     proto_tree_add_uint(phbid_subtree, hf_phbid_bit15, tvb, offset, 2, phbid);
114 }