For proto_tree_add_item(..., proto_xxx, ...)use ENC_NA as the encoding arg.
[obnox/wireshark/wip.git] / epan / dissectors / packet-tte.c
1 /* packet-tte.c
2  * Routines for Time Triggered Ethernet dissection
3  *
4  * Author: Valentin Ecker, valentin.ecker (AT) tttech.com
5  * Author: Benjamin Roch, benjamin.roch (AT) tttech.com
6  *
7  * TTTech Computertechnik AG, Austria.
8  * http://www.tttech.com/solutions/ttethernet/
9  *
10  * $Id$
11  *
12  * Wireshark - Network traffic analyzer
13  * By Gerald Combs <gerald@wireshark.org>
14  * Copyright 1998 Gerald Combs
15  *
16  * This program is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU General Public License
18  * as published by the Free Software Foundation; either version 2
19  * of the License, or (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
29  * USA.
30  */
31
32 #ifdef HAVE_CONFIG_H
33  #include "config.h"
34 #endif
35
36 #include <glib.h>
37
38 #include <epan/packet.h>
39 #include <epan/prefs.h>
40 #include <epan/etypes.h>
41
42 #include "packet-tte.h"
43
44 /* Initialize the protocol and registered fields */
45 static int proto_tte = -1;
46
47 static int hf_eth_dst       = -1;
48 static int hf_tte_dst_cf    = -1;
49 static int hf_tte_ctid      = -1;
50 static int hf_eth_src       = -1;
51 static int hf_eth_type      = -1;
52
53 /* preference value pointers */
54 static guint32    tte_pref_ct_marker    = 0xFFFFFFFF;
55 static guint32    tte_pref_ct_mask      = 0x0;
56
57 /* Initialize the subtree pointers */
58 static gint ett_tte = -1;
59 static gint ett_tte_macdest = -1;
60
61
62 /* Code to actually dissect the packets */
63 static int
64 dissect_tte(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
65 {
66     int is_frame_pcf;
67
68     /* Set up structures needed to add the protocol subtree and manage it */
69     proto_item *tte_root_item, *tte_macdest_item;
70     proto_tree *tte_tree, *tte_macdest_tree;
71
72     /* Check that there's enough data */
73     if (tvb_length(tvb) < TTE_HEADER_LENGTH)
74         return 0;
75
76     /* check if data of pcf frame */
77     is_frame_pcf =
78        (tvb_get_ntohs(tvb, TTE_MAC_LENGTH * 2) == ETHERTYPE_TTE_PCF);
79
80     /* return if no valid constant field is found */
81     if (!is_frame_pcf)
82     {
83         if ( (tvb_get_ntohl(tvb, 0) & tte_pref_ct_mask) != tte_pref_ct_marker)
84             return 0;
85     }
86
87     /* Make entries in Protocol column and Info column on summary display */
88     col_set_str(pinfo->cinfo, COL_PROTOCOL, "TTE ");
89
90     col_set_str(pinfo->cinfo, COL_INFO, "Bogus TTEthernet Frame");
91
92     if (tree) {
93
94         /* create display subtree for the protocol */
95         tte_root_item = proto_tree_add_item(tree, proto_tte, tvb, 0,
96             TTE_HEADER_LENGTH, ENC_NA);
97
98         tte_tree = proto_item_add_subtree(tte_root_item, ett_tte);
99
100         tte_macdest_item = proto_tree_add_item(tte_tree,
101             hf_eth_dst, tvb, 0, TTE_MAC_LENGTH, FALSE);
102
103         proto_tree_add_item(tte_tree,
104             hf_eth_src, tvb, TTE_MAC_LENGTH, TTE_MAC_LENGTH, FALSE);
105
106         proto_tree_add_item(tte_tree,
107             hf_eth_type, tvb, TTE_MAC_LENGTH*2, TTE_ETHERTYPE_LENGTH,
108
109             FALSE);
110
111         tte_macdest_tree = proto_item_add_subtree(tte_macdest_item,
112             ett_tte_macdest);
113
114         proto_tree_add_item(tte_macdest_tree,
115             hf_tte_dst_cf, tvb, 0, TTE_MACDEST_CF_LENGTH, ENC_BIG_ENDIAN);
116
117         proto_tree_add_item(tte_macdest_tree,
118             hf_tte_ctid, tvb, TTE_MACDEST_CF_LENGTH,
119             TTE_MACDEST_CTID_LENGTH, ENC_BIG_ENDIAN);
120     }
121
122     /* prevent clearing the Columns...appending cannot be prevented */
123     col_set_fence(pinfo->cinfo, COL_PROTOCOL);
124
125     /* call std Ethernet dissector */
126     ethertype (tvb_get_ntohs(tvb, TTE_MAC_LENGTH * 2), tvb
127         , 14, pinfo, tree, NULL, hf_eth_type, 0, 0 );
128
129     return tvb_length(tvb);
130 }
131
132
133 void
134 proto_register_tte(void)
135 {
136     module_t *tte_module;
137
138     static hf_register_info hf[] = {
139         { &hf_tte_dst_cf,
140           { "Constant Field",   "tte.cf",
141             FT_UINT32, BASE_HEX, NULL, 0x0,
142             NULL, HFILL }
143         },
144         { &hf_tte_ctid,
145           { "Critical Traffic Identifier", "tte.ctid",
146             FT_UINT16, BASE_HEX, NULL, 0x0,
147             NULL, HFILL }
148         }
149     };
150
151     /* Setup protocol subtree array */
152     static gint *ett[] = {
153         &ett_tte,
154         &ett_tte_macdest
155     };
156
157     /* Register the protocol name and description */
158     proto_tte = proto_register_protocol("TTEthernet", "TTE", "tte");
159
160     /* Required function calls to register header fields and subtrees used */
161     proto_register_field_array(proto_tte, hf, array_length(hf));
162     proto_register_subtree_array(ett, array_length(ett));
163
164     /* Register preferences module */
165     tte_module = prefs_register_protocol(proto_tte, NULL);
166
167     /* Register preferences */
168     prefs_register_uint_preference(tte_module, "ct_mask_value",
169         "CT Mask (in hex)",
170         "Critical Traffic Mask (base hex)",
171         16, &tte_pref_ct_mask);
172
173     prefs_register_uint_preference(tte_module, "ct_marker_value",
174         "CT Marker (in hex)",
175         "Critical Traffic Marker (base hex)",
176         16, &tte_pref_ct_marker);
177 }
178
179
180 void
181 proto_reg_handoff_tte(void)
182 {
183     heur_dissector_add("eth", dissect_tte, proto_tte);
184
185     hf_eth_dst  = proto_registrar_get_byname ("eth.dst")->id;
186     hf_eth_src  = proto_registrar_get_byname ("eth.src")->id;
187     hf_eth_type = proto_registrar_get_byname ("eth.type")->id;
188 }