Fix for bug 5422:
[obnox/wireshark/wip.git] / epan / dissectors / packet-esio.c
1 /* packet-esio.c
2  * Routines for Ether-S-I/O dissection (from Saia Burgess Controls AG )
3  * Copyright 2010, Christian Durrer <christian.durrer@sensemail.ch>
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
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 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include <glib.h>
31 #include <epan/packet.h>
32 #include <epan/expert.h>
33
34 /* Telegram types*/
35 #define ESIO_TRANSFER                  0x01
36 #define ESIO_STATUS                    0x02
37
38 /* Initialize the protocol and registered fields */
39 static int proto_esio = -1;
40 static int hf_esio_type = -1;
41 static int hf_esio_version = -1;
42 static int hf_esio_length = -1;
43 static int hf_esio_transaction_id = -1;
44 static int hf_esio_tlg_id = -1;
45 static int hf_esio_src_stn_id = -1;
46 static int hf_esio_data_nbr = -1;
47 static int hf_esio_data_flags = -1;
48 static int hf_esio_data_transfer_id = -1;
49 static int hf_esio_data_dest_id = -1;
50 static int hf_esio_data_length = -1;
51 static int hf_esio_data = -1;
52 static int hf_esio_sts_type = -1;
53 static int hf_esio_sts_size = -1;
54 static int hf_esio_rio_sts = -1;
55 static int hf_esio_rio_tlgs_lost = -1;
56 static int hf_esio_rio_diag = -1;
57 static int hf_esio_rio_flags = -1;
58
59 /* Initialize the subtree pointers */
60 static gint ett_esio = -1;
61 static gint ett_esio_header = -1;
62 static gint ett_esio_transfer_header = -1;
63 static gint ett_esio_transfer_data = -1;
64 static gint ett_esio_data = -1;
65
66 /* value to string definitions*/
67 /* Ether-S-I/O telegram types*/
68 static const value_string esio_tlg_types[] = {
69        {0, "Reserved"},
70        {1, "Data transfer telegram"},
71        {2, "Status/Diag telegram"},
72        {0, NULL}
73 };
74
75 /* Status telegram types*/
76 static const value_string esio_sts_types[] = {
77        {0, "None"},
78        {1, "RIO status"},
79        {0, NULL}
80 };
81
82 /* check whether the packet looks like SBUS or not */
83 static gboolean
84 is_esio_pdu(tvbuff_t *tvb)
85 {
86        /* we need at least 8 bytes to determine whether this is 
87           Ether-S-I/O or not*/
88        /* minimal length is 20 bytes*/
89        if (tvb_length(tvb) < 20) {
90               return FALSE;
91        }
92        /* First four bytes must be "ESIO"*/
93        if (tvb_strneql(tvb, 0, "ESIO", 4) != 0) {
94               return FALSE;
95        }
96        /* fifth byte must be 0*/
97        if (tvb_get_guint8(tvb, 4) > 0x00) {
98               return FALSE;
99        }
100        /* sixth byte indicates telegram type and must be 0, 1 or 2*/
101        if (tvb_get_guint8(tvb, 5) > 0x02) {
102               return FALSE;
103        }
104        /* seventh byte must be 0*/
105        if (tvb_get_guint8(tvb, 6) > 0x00) {
106               return FALSE;
107        }
108        /* eight byte indicates telegram version and must be 0 (up to now)*/
109        if (tvb_get_guint8(tvb, 7) > 0x00) {
110               return FALSE;
111        }
112        /*header seems to be Ether-S-I/O*/
113        return TRUE;
114 }
115
116 /*Dissect the telegram*/
117 static int
118 dissect_esio(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
119 {
120
121 /* Set up structures needed to add the protocol subtree and manage it */
122        proto_item *ti, *et, *hi;
123        proto_tree *esio_tree, *esio_header_tree, *esio_transfer_header_tree,
124             *esio_data_tansfer_tree, *esio_data_tree;
125
126        gint    i;
127        gint    offset;
128        guint8  esio_nbr_data_transfers;
129        guint16 esio_telegram_type;
130        guint16 esio_tlg_type;
131        guint16 esio_transfer_length;
132        guint32 esio_transfer_dest_id;
133        guint32 esio_src_id;
134        guint32 esio_dst_id;
135
136 /* does this look like an sbus pdu? */
137        if (!is_esio_pdu(tvb)) {
138               return 0;
139        }
140
141 /* Make entries in Protocol column and Info column on summary display */
142        col_set_str(pinfo->cinfo, COL_PROTOCOL, "ESIO");
143        col_clear(pinfo->cinfo, COL_INFO);
144        esio_telegram_type = tvb_get_guint8(tvb,5);
145        if (check_col(pinfo->cinfo, COL_INFO)) {
146               switch (esio_telegram_type) {
147                      case ESIO_TRANSFER:
148                             esio_src_id = tvb_get_ntohl(tvb,16);
149                             esio_nbr_data_transfers = tvb_get_guint8(tvb, 20);
150                             esio_dst_id = tvb_get_ntohl(tvb,26);
151                             col_add_fstr( pinfo->cinfo, COL_INFO,
152                                           "Data transfer: Src ID: %d, Dst ID(s): %d",
153                                           esio_src_id, esio_dst_id);
154                             if (esio_nbr_data_transfers > 1) {
155                                    col_append_fstr( pinfo->cinfo, COL_INFO,
156                                                     " ...");
157                             }
158                             break;
159                      case ESIO_STATUS:
160                             esio_src_id = tvb_get_ntohl(tvb,16);
161                             col_add_fstr( pinfo->cinfo, COL_INFO,
162                                           "Status/diag telegram: Src ID: %d",
163                                           esio_src_id);
164                             break;
165                      default:
166                             /* All other telegrams */
167                             col_set_str( pinfo->cinfo, COL_INFO,
168                                          "Unknown telegram");
169                             break;
170               }
171
172        }
173 /* create display subtree for the protocol */
174        if (tree) {
175               offset =0;
176               ti = proto_tree_add_item(tree, proto_esio, tvb, offset, -1, FALSE);
177               esio_tree = proto_item_add_subtree(ti, ett_esio);
178 /*Add subtree for Ether-S-I/O header*/
179               et = proto_tree_add_text(esio_tree, tvb, offset, 12, "Ether-S-I/O header");
180               esio_header_tree = proto_item_add_subtree(et, ett_esio_header);
181               offset +=4; /*first four bytes are "ESIO"*/
182 /* add items to the Ether-S-I/O header subtree*/
183               esio_tlg_type = tvb_get_ntohs(tvb,offset);
184               proto_tree_add_item(esio_header_tree,
185                                   hf_esio_type, tvb, offset, 2, FALSE);
186               offset += 2;
187               proto_tree_add_item(esio_header_tree,
188                                   hf_esio_version, tvb, offset, 2, FALSE);
189               offset += 2;
190               proto_tree_add_item(esio_header_tree,
191                                   hf_esio_length, tvb, offset, 2, FALSE);
192               offset += 2;
193               proto_tree_add_item(esio_header_tree,
194                                   hf_esio_transaction_id, tvb, offset, 2, FALSE);
195               offset += 2;
196               switch (esio_tlg_type) {
197                      case ESIO_TRANSFER:
198                             /*Add subtree for Ether-S-I/O header*/
199                             et = proto_tree_add_text(esio_tree, tvb, offset, 12, "Transfer header");
200                             esio_transfer_header_tree = proto_item_add_subtree(et, ett_esio_transfer_header);
201                             proto_tree_add_item(esio_transfer_header_tree,
202                                                 hf_esio_tlg_id, tvb, offset, 4, FALSE);
203                             offset += 4;
204                             proto_tree_add_item(esio_transfer_header_tree,
205                                                 hf_esio_src_stn_id, tvb, offset, 4, FALSE);
206                             offset += 4;
207                             esio_nbr_data_transfers = tvb_get_guint8(tvb,offset);
208                             proto_tree_add_item(esio_transfer_header_tree,
209                                                 hf_esio_data_nbr, tvb, offset, 1, FALSE);
210                             offset += 1;
211                             proto_tree_add_item(esio_transfer_header_tree,
212                                                 hf_esio_data_flags, tvb, offset, 1, FALSE);
213                             offset += 1;
214                             for (i=((esio_nbr_data_transfers)); i>0; i--) {
215                                    /*Add subtree(s) for Ether-S-I/O data transfers*/
216                                    esio_transfer_dest_id = tvb_get_ntohl(tvb,(offset+4));
217                                    esio_transfer_length = tvb_get_ntohs(tvb,(offset+8));
218                                    et = proto_tree_add_text(esio_tree, tvb, offset,
219                                                             (esio_transfer_length + 10), "Data transfer to ID: %d ",
220                                                             esio_transfer_dest_id);
221                                    esio_data_tansfer_tree = proto_item_add_subtree(et, ett_esio_transfer_data);
222                                    proto_tree_add_item(esio_data_tansfer_tree,
223                                                        hf_esio_data_transfer_id, tvb, offset, 4, FALSE);
224                                    offset += 4;
225                                    proto_tree_add_item(esio_data_tansfer_tree,
226                                                        hf_esio_data_dest_id, tvb, offset, 4, FALSE);
227                                    offset += 4;
228                                    proto_tree_add_item(esio_data_tansfer_tree,
229                                                        hf_esio_data_length, tvb, offset, 2, FALSE);
230                                    offset += 2;
231                                    /*here comes the data*/
232                                    et = proto_tree_add_text(esio_data_tansfer_tree, tvb, offset,
233                                                             esio_transfer_length, "Data bytes ");
234                                    esio_data_tree = proto_item_add_subtree(et, ett_esio_data);
235                                    for (i=((esio_transfer_length)); i>0; i--) {
236                                           proto_tree_add_item(esio_data_tree,
237                                                               hf_esio_data, tvb, offset,
238                                                               1, FALSE);
239                                           offset += 1;
240                                    }
241                             }
242                             break;
243                      case ESIO_STATUS:
244                             proto_tree_add_item(esio_tree,
245                                                 hf_esio_sts_type, tvb, offset, 2, FALSE);
246                             offset += 2;
247                             proto_tree_add_item(esio_tree,
248                                                 hf_esio_sts_size, tvb, offset, 2, FALSE);
249                             offset += 2;
250                             proto_tree_add_item(esio_tree,
251                                                 hf_esio_src_stn_id, tvb, offset, 4, FALSE);
252                             offset += 4;
253                             proto_tree_add_item(esio_tree,
254                                                 hf_esio_rio_sts, tvb, offset,
255                                                 1, FALSE);
256                             offset += 1;
257                             hi = proto_tree_add_item(esio_tree,
258                                                      hf_esio_rio_tlgs_lost, tvb, offset,
259                                                      1, FALSE);
260                             expert_add_info_format(pinfo, hi, PI_SEQUENCE, PI_NOTE,
261                                                    "Telegram(s) lost");
262                             offset += 1;
263                             proto_tree_add_item(esio_tree,
264                                                 hf_esio_rio_diag, tvb, offset,
265                                                 1, FALSE);
266                             offset += 1;
267                             proto_tree_add_item(esio_tree,
268                                                 hf_esio_rio_flags, tvb, offset, 1, FALSE);
269                             offset += 1;
270                             break;
271                      default:
272                             break;
273               }
274        } /*end of tree*/
275        return tvb_length(tvb);
276 /*End of dissect_sbus*/
277 }
278
279 /* Register the protocol with Wireshark */
280 void
281 proto_register_esio(void)
282 {
283 /* Setup list of header fields  See Section 1.6.1 for details*/
284        static hf_register_info hf[] = {
285               { &hf_esio_type,
286                      { "Telegram type", "esio.type",
287                      FT_UINT16, BASE_HEX, VALS(esio_tlg_types), 0,
288                      NULL, HFILL }
289               },
290
291               { &hf_esio_version,
292                      { "Version", "esio.vers",
293                      FT_UINT16, BASE_DEC, NULL, 0,
294                      NULL, HFILL }
295               },
296
297               { &hf_esio_length,
298                      { "Length (bytes)", "esio.len",
299                      FT_UINT16, BASE_DEC, NULL, 0,
300                      NULL, HFILL }
301               },
302
303               { &hf_esio_transaction_id,
304                      { "Transaction ID", "esio.transaction_id",
305                      FT_UINT16, BASE_DEC, NULL, 0,
306                      NULL, HFILL }
307               },
308
309               { &hf_esio_src_stn_id,
310                      { "Source station ID", "esio.src_stn_id",
311                      FT_UINT32, BASE_DEC, NULL, 0,
312                      NULL, HFILL }
313               },
314
315               { &hf_esio_tlg_id,
316                      { "Telegram ID", "esio.transfer.tlg_id",
317                      FT_UINT32, BASE_DEC, NULL, 0,
318                      NULL, HFILL }
319               },
320
321               { &hf_esio_data_nbr,
322                      { "Nbr. of data transfers", "esio.data.nbr",
323                      FT_UINT8, BASE_DEC, NULL, 0,
324                      NULL, HFILL }
325               },
326
327               { &hf_esio_data_flags,
328                      { "Transfer header flags", "esio.data.flags",
329                      FT_UINT8, BASE_HEX, NULL, 0,
330                      NULL, HFILL }
331               },
332
333               { &hf_esio_data_transfer_id,
334                      { "Data transfer ID", "esio.data.transfer_id",
335                      FT_UINT32, BASE_DEC, NULL, 0,
336                      NULL, HFILL }
337               },
338
339               { &hf_esio_data_dest_id,
340                      { "Data destination ID", "esio.data.destination_id",
341                      FT_UINT32, BASE_DEC, NULL, 0,
342                      NULL, HFILL }
343               },
344
345               { &hf_esio_data_length,
346                      { "Data transfer length", "esio.data.length",
347                      FT_UINT16, BASE_DEC, NULL, 0,
348                      NULL, HFILL }
349               },
350
351               { &hf_esio_data,
352                      { "Data", "esio.data",
353                      FT_UINT8, BASE_DEC, NULL, 0,
354                      NULL, HFILL }
355               },
356
357               { &hf_esio_sts_type,
358                      { "Status type", "esio.sts.type",
359                      FT_UINT16, BASE_HEX, VALS(esio_sts_types), 0,
360                      NULL, HFILL }
361               },
362
363               { &hf_esio_sts_size,
364                      { "Status length (bytes)", "esio.sts.length",
365                      FT_UINT16, BASE_DEC, NULL, 0,
366                      NULL, HFILL }
367               },
368
369               { &hf_esio_rio_sts,
370                      { "RIO status", "esio.sts.rio_sts",
371                      FT_UINT8, BASE_DEC, NULL, 0,
372                      NULL, HFILL }
373               },
374
375               { &hf_esio_rio_tlgs_lost,
376                      { "Lost telegrams to RIO", "esio.sts.rio_lost_tlg",
377                      FT_UINT8, BASE_DEC, NULL, 0,
378                      NULL, HFILL }
379               },
380
381               { &hf_esio_rio_diag,
382                      { "RIO diagnostics", "esio.sts.rio_diag",
383                      FT_UINT8, BASE_DEC, NULL, 0,
384                      NULL, HFILL }
385               },
386
387               { &hf_esio_rio_flags,
388                      { "RIO flags", "esio.sts.rio_flags",
389                      FT_UINT8, BASE_HEX, NULL, 0,
390                      NULL, HFILL }
391               }
392        };
393
394
395 /* Setup protocol subtree array */
396        static gint *ett[] = {
397               &ett_esio,
398               &ett_esio_header,
399               &ett_esio_transfer_header,
400               &ett_esio_transfer_data,
401               &ett_esio_data
402        };
403
404 /* Register the protocol name and description */
405        proto_esio = proto_register_protocol("SAIA Ether-S-I/O protocol", "ESIO", "esio");
406
407 /* Required function calls to register the header fields and subtrees used */
408        proto_register_field_array(proto_esio, hf, array_length(hf));
409        proto_register_subtree_array(ett, array_length(ett));
410 }
411
412 void
413 proto_reg_handoff_esio(void)
414 {
415        dissector_handle_t esio_handle;
416
417        esio_handle = new_create_dissector_handle(dissect_esio, proto_esio);
418        dissector_add("udp.port", 6060, esio_handle);
419 }
420
421 /*
422  * Editor modelines
423  *
424  * Local Variables:
425  * c-basic-offset: 7
426  * tab-width: 8
427  * indent-tabs-mode: nil
428  * End:
429  *
430  * ex: set shiftwidth=7 tabstop=8 expandtab
431  * :indentSize=7:tabSize=8:noTabs=true:
432  */