HACK: pinfo->private_data points to smb_info again
[wireshark-wip.git] / epan / dissectors / packet-socketcan.c
blob5c2b0c17ad80641641ec9f5c7ceb6d5904078f09
1 /* packet-socketcan.c
2 * Routines for disassembly of packets from SocketCAN
3 * Felix Obenhuber <felix@obenhuber.de>
5 * $Id$
7 * Wireshark - Network traffic analyzer
8 * By Gerald Combs <gerald@wireshark.org>
9 * Copyright 1998 Gerald Combs
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include "config.h"
28 #include <glib.h>
30 #include <epan/packet.h>
31 #include <prefs.h>
32 #include "packet-sll.h"
34 /* controller area network (CAN) kernel definitions
35 * These masks are usually defined within <linux/can.h> but are not
36 * available on non-Linux platforms; that's the reason for the
37 * redefinitions below
39 * special address description flags for the CAN_ID */
40 #define CAN_EFF_FLAG 0x80000000U /* EFF/SFF is set in the MSB */
41 #define CAN_RTR_FLAG 0x40000000U /* remote transmission request */
42 #define CAN_ERR_FLAG 0x20000000U /* error frame */
43 #define CAN_EFF_MASK 0x1FFFFFFFU /* extended frame format (EFF) */
46 static int hf_can_len = -1;
47 static int hf_can_ident = -1;
48 static int hf_can_extflag = -1;
49 static int hf_can_rtrflag = -1;
50 static int hf_can_errflag = -1;
52 static gint ett_can = -1;
54 static int proto_can = -1;
56 static dissector_handle_t data_handle;
57 static dissector_handle_t canopen_handle;
59 #define LINUX_CAN_STD 0
60 #define LINUX_CAN_EXT 1
61 #define LINUX_CAN_RTR 2
62 #define LINUX_CAN_ERR 3
64 #define CAN_LEN_OFFSET 4
65 #define CAN_DATA_OFFSET 8
67 typedef enum {
68 CAN_DATA_DISSECTOR = 1,
69 CAN_CANOPEN_DISSECTOR = 2
70 } Dissector_Option;
72 static const enum_val_t can_high_level_protocol_dissector_options[] = {
73 { "raw", "Raw data (no further dissection)", CAN_DATA_DISSECTOR },
74 { "CANopen", "CANopen protocol", CAN_CANOPEN_DISSECTOR },
75 { NULL, NULL, 0 }
78 static guint can_high_level_protocol_dissector = CAN_DATA_DISSECTOR;
80 static const value_string frame_type_vals[] =
82 { LINUX_CAN_STD, "STD" },
83 { LINUX_CAN_EXT, "XTD" },
84 { LINUX_CAN_RTR, "RTR" },
85 { LINUX_CAN_ERR, "ERR" },
86 { 0, NULL }
89 static void
90 dissect_socketcan(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
92 proto_tree *can_tree;
93 proto_item *ti;
94 guint8 frame_type;
95 gint frame_len;
96 guint32 id;
98 col_set_str(pinfo->cinfo, COL_PROTOCOL, "CAN");
99 col_clear(pinfo->cinfo,COL_INFO);
101 frame_len = tvb_get_guint8( tvb, CAN_LEN_OFFSET);
102 id = tvb_get_ntohl(tvb, 0);
104 if (id & CAN_RTR_FLAG)
106 frame_type = LINUX_CAN_RTR;
108 else if (id & CAN_ERR_FLAG)
110 frame_type = LINUX_CAN_ERR;
112 else if (id & CAN_EFF_FLAG)
114 frame_type = LINUX_CAN_EXT;
116 else
118 frame_type = LINUX_CAN_STD;
121 id &= CAN_EFF_MASK;
123 col_add_fstr(pinfo->cinfo, COL_INFO, "%s: 0x%08x",
124 val_to_str(frame_type, frame_type_vals, "Unknown (0x%02x)"), id);
125 col_append_fstr(pinfo->cinfo, COL_INFO, " %s",
126 tvb_bytes_to_str_punct(tvb, CAN_DATA_OFFSET, frame_len, ' '));
128 ti = proto_tree_add_item(tree, proto_can, tvb, 0, -1, ENC_NA);
129 can_tree = proto_item_add_subtree(ti, ett_can);
131 proto_tree_add_item(can_tree, hf_can_ident, tvb, 0, 4, ENC_BIG_ENDIAN);
132 proto_tree_add_item(can_tree, hf_can_extflag, tvb, 0, 4, ENC_BIG_ENDIAN);
133 proto_tree_add_item(can_tree, hf_can_rtrflag, tvb, 0, 4, ENC_BIG_ENDIAN);
134 proto_tree_add_item(can_tree, hf_can_errflag, tvb, 0, 4, ENC_BIG_ENDIAN);
136 proto_tree_add_item(can_tree, hf_can_len, tvb, CAN_LEN_OFFSET, 1, ENC_BIG_ENDIAN);
138 switch (can_high_level_protocol_dissector)
140 case CAN_DATA_DISSECTOR:
141 call_dissector(data_handle,
142 tvb_new_subset(tvb, CAN_DATA_OFFSET, frame_len, frame_len),
143 pinfo, tree);
144 break;
145 case CAN_CANOPEN_DISSECTOR:
146 /* XXX: The canopen dissector *redissects in a different manner* */
147 /* the same header bytes dissected above. */
148 /* What's the right way to handle SocketCan dissection ? */
149 call_dissector(canopen_handle, tvb, pinfo, tree);
150 break;
154 void
155 proto_register_socketcan(void)
157 static hf_register_info hf[] = {
159 &hf_can_ident,
161 "Identifier", "can.id",
162 FT_UINT32, BASE_HEX,
163 NULL, CAN_EFF_MASK,
164 NULL, HFILL
168 &hf_can_extflag,
170 "Extended Flag", "can.flags.xtd",
171 FT_BOOLEAN, 32,
172 NULL, CAN_EFF_FLAG,
173 NULL, HFILL
177 &hf_can_rtrflag,
179 "Remote Transmission Request Flag", "can.flags.rtr",
180 FT_BOOLEAN, 32,
181 NULL, CAN_RTR_FLAG,
182 NULL, HFILL
186 &hf_can_errflag,
188 "Error Flag", "can.flags.err",
189 FT_BOOLEAN, 32,
190 NULL, CAN_ERR_FLAG,
191 NULL, HFILL
195 &hf_can_len,
197 "Frame-Length", "can.len",
198 FT_UINT8, BASE_DEC,
199 NULL, 0x0,
200 NULL, HFILL
205 /* Setup protocol subtree array */
206 static gint *ett[] =
208 &ett_can
210 module_t *can_module;
212 proto_can = proto_register_protocol(
213 "Controller Area Network", /* name */
214 "CAN", /* short name */
215 "can" /* abbrev */
218 proto_register_field_array(proto_can, hf, array_length(hf));
219 proto_register_subtree_array(ett, array_length(ett));
221 can_module = prefs_register_protocol(proto_can, NULL);
223 prefs_register_enum_preference(
224 can_module,
225 "protocol",
226 "Next level protocol",
227 "Next level protocol like CANopen etc.",
228 (gint *)&can_high_level_protocol_dissector,
229 can_high_level_protocol_dissector_options,
230 FALSE
234 void
235 proto_reg_handoff_socketcan(void)
237 dissector_handle_t can_handle;
239 can_handle = create_dissector_handle(dissect_socketcan, proto_can);
240 dissector_add_uint("wtap_encap", WTAP_ENCAP_SOCKETCAN, can_handle);
241 dissector_add_uint("sll.ltype", LINUX_SLL_P_CAN, can_handle);
243 canopen_handle = find_dissector("canopen");
244 data_handle = find_dissector("data");