Use MAC address documentation range in filter examples
[metze/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  * Wireshark - Network traffic analyzer
6  * By Gerald Combs <gerald@wireshark.org>
7  * Copyright 1998 Gerald Combs
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23
24 #include "config.h"
25
26 #include <epan/packet.h>
27 #include <epan/expert.h>
28
29 /* Telegram types*/
30 #define ESIO_TRANSFER                  0x01
31 #define ESIO_STATUS                    0x02
32
33 void proto_register_esio(void);
34 void proto_reg_handoff_esio(void);
35
36 /* Initialize the protocol and registered fields */
37 static int proto_esio = -1;
38 static int hf_esio_type = -1;
39 static int hf_esio_version = -1;
40 static int hf_esio_length = -1;
41 static int hf_esio_transaction_id = -1;
42 static int hf_esio_tlg_id = -1;
43 static int hf_esio_src_stn_id = -1;
44 static int hf_esio_data_nbr = -1;
45 static int hf_esio_data_flags = -1;
46 static int hf_esio_data_transfer_id = -1;
47 static int hf_esio_data_dest_id = -1;
48 static int hf_esio_data_length = -1;
49 static int hf_esio_data = -1;
50 static int hf_esio_sts_type = -1;
51 static int hf_esio_sts_size = -1;
52 static int hf_esio_rio_sts = -1;
53 static int hf_esio_rio_tlgs_lost = -1;
54 static int hf_esio_rio_diag = -1;
55 static int hf_esio_rio_flags = -1;
56
57 /* Initialize the subtree pointers */
58 static gint ett_esio = -1;
59 static gint ett_esio_header = -1;
60 static gint ett_esio_transfer_header = -1;
61 static gint ett_esio_transfer_data = -1;
62 static gint ett_esio_data = -1;
63
64 static expert_field ei_esio_telegram_lost = EI_INIT;
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_captured_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, void *data _U_)
119 {
120
121 /* Set up structures needed to add the protocol subtree and manage it */
122        proto_item *ti;
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
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_str( 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 /* create display subtree for the protocol */
173        offset = 0;
174        ti = proto_tree_add_item(tree, proto_esio, tvb, offset, -1, ENC_NA);
175        esio_tree = proto_item_add_subtree(ti, ett_esio);
176 /*Add subtree for Ether-S-I/O header*/
177        esio_header_tree = proto_tree_add_subtree(esio_tree, tvb, offset, 12, ett_esio_header, NULL, "Ether-S-I/O header");
178        offset += 4; /*first four bytes are "ESIO"*/
179 /* add items to the Ether-S-I/O header subtree*/
180        esio_tlg_type = tvb_get_ntohs(tvb,offset);
181        proto_tree_add_item(esio_header_tree,
182                            hf_esio_type, tvb, offset, 2, ENC_BIG_ENDIAN);
183        offset += 2;
184        proto_tree_add_item(esio_header_tree,
185                            hf_esio_version, tvb, offset, 2, ENC_BIG_ENDIAN);
186        offset += 2;
187        proto_tree_add_item(esio_header_tree,
188                            hf_esio_length, tvb, offset, 2, ENC_BIG_ENDIAN);
189        offset += 2;
190        proto_tree_add_item(esio_header_tree,
191                            hf_esio_transaction_id, tvb, offset, 2, ENC_BIG_ENDIAN);
192        offset += 2;
193        switch (esio_tlg_type) {
194        case ESIO_TRANSFER:
195               if (tree) {
196                      /*Add subtree for Ether-S-I/O header*/
197                      esio_transfer_header_tree = proto_tree_add_subtree(esio_tree, tvb, offset, 12,
198                                                         ett_esio_transfer_header, NULL, "Transfer header");
199                      proto_tree_add_item(esio_transfer_header_tree,
200                                          hf_esio_tlg_id, tvb, offset, 4, ENC_BIG_ENDIAN);
201                      offset += 4;
202                      proto_tree_add_item(esio_transfer_header_tree,
203                                          hf_esio_src_stn_id, tvb, offset, 4, ENC_BIG_ENDIAN);
204                      offset += 4;
205                      esio_nbr_data_transfers = tvb_get_guint8(tvb,offset);
206                      proto_tree_add_item(esio_transfer_header_tree,
207                                          hf_esio_data_nbr, tvb, offset, 1, ENC_BIG_ENDIAN);
208                      offset += 1;
209                      proto_tree_add_item(esio_transfer_header_tree,
210                                          hf_esio_data_flags, tvb, offset, 1, ENC_BIG_ENDIAN);
211                      offset += 1;
212                      for (i=((esio_nbr_data_transfers)); i>0; i--) {
213                             /*Add subtree(s) for Ether-S-I/O data transfers*/
214                             esio_transfer_dest_id = tvb_get_ntohl(tvb,(offset+4));
215                             esio_transfer_length = tvb_get_ntohs(tvb,(offset+8));
216                             esio_data_tansfer_tree = proto_tree_add_subtree_format(esio_tree, tvb, offset,
217                                                      (esio_transfer_length + 10), ett_esio_transfer_data, NULL,
218                                                      "Data transfer to ID: %d ", esio_transfer_dest_id);
219
220                             proto_tree_add_item(esio_data_tansfer_tree,
221                                                 hf_esio_data_transfer_id, tvb, offset, 4, ENC_BIG_ENDIAN);
222                             offset += 4;
223                             proto_tree_add_item(esio_data_tansfer_tree,
224                                                 hf_esio_data_dest_id, tvb, offset, 4, ENC_BIG_ENDIAN);
225                             offset += 4;
226                             proto_tree_add_item(esio_data_tansfer_tree,
227                                                 hf_esio_data_length, tvb, offset, 2, ENC_BIG_ENDIAN);
228                             offset += 2;
229                             /*here comes the data*/
230                             esio_data_tree = proto_tree_add_subtree(esio_data_tansfer_tree, tvb, offset,
231                                                      esio_transfer_length, ett_esio_data, NULL, "Data bytes ");
232                             for (i=((esio_transfer_length)); i>0; i--) {
233                                    proto_tree_add_item(esio_data_tree,
234                                                        hf_esio_data, tvb, offset,
235                                                        1, ENC_BIG_ENDIAN);
236                                    offset += 1;
237                             }
238                      }
239               } /* if (tree) */
240               break;
241        case ESIO_STATUS: {
242               proto_item *hi = NULL;
243               if (tree) {
244                      proto_tree_add_item(esio_tree,
245                                          hf_esio_sts_type, tvb, offset, 2, ENC_BIG_ENDIAN);
246                      proto_tree_add_item(esio_tree,
247                                          hf_esio_sts_size, tvb, offset+2, 2, ENC_BIG_ENDIAN);
248                      proto_tree_add_item(esio_tree,
249                                          hf_esio_src_stn_id, tvb, offset+4, 4, ENC_BIG_ENDIAN);
250                      proto_tree_add_item(esio_tree,
251                                          hf_esio_rio_sts, tvb, offset+8,
252                                          1, ENC_BIG_ENDIAN);
253                      hi = proto_tree_add_item(esio_tree,
254                                               hf_esio_rio_tlgs_lost, tvb, offset+9,
255                                               1, ENC_BIG_ENDIAN);
256                      proto_tree_add_item(esio_tree,
257                                          hf_esio_rio_diag, tvb, offset+10,
258                                          1, ENC_BIG_ENDIAN);
259                      proto_tree_add_item(esio_tree,
260                                          hf_esio_rio_flags, tvb, offset+11, 1, ENC_BIG_ENDIAN);
261               } /* if (tree) */
262               if (tvb_get_guint8(tvb, offset + 9) > 0) {
263                      expert_add_info(pinfo, hi, &ei_esio_telegram_lost);
264               }
265               break;
266        }
267        default:
268               break;
269        } /* switch() */
270
271        return tvb_captured_length(tvb);
272 /*End of dissect_sbus*/
273 }
274
275 /* Register the protocol with Wireshark */
276 void
277 proto_register_esio(void)
278 {
279 /* Setup list of header fields  See Section 1.6.1 for details*/
280        static hf_register_info hf[] = {
281               { &hf_esio_type,
282                      { "Telegram type", "esio.type",
283                      FT_UINT16, BASE_HEX, VALS(esio_tlg_types), 0,
284                      NULL, HFILL }
285               },
286
287               { &hf_esio_version,
288                      { "Version", "esio.vers",
289                      FT_UINT16, BASE_DEC, NULL, 0,
290                      NULL, HFILL }
291               },
292
293               { &hf_esio_length,
294                      { "Length (bytes)", "esio.len",
295                      FT_UINT16, BASE_DEC, NULL, 0,
296                      NULL, HFILL }
297               },
298
299               { &hf_esio_transaction_id,
300                      { "Transaction ID", "esio.transaction_id",
301                      FT_UINT16, BASE_DEC, NULL, 0,
302                      NULL, HFILL }
303               },
304
305               { &hf_esio_src_stn_id,
306                      { "Source station ID", "esio.src_stn_id",
307                      FT_UINT32, BASE_DEC, NULL, 0,
308                      NULL, HFILL }
309               },
310
311               { &hf_esio_tlg_id,
312                      { "Telegram ID", "esio.transfer.tlg_id",
313                      FT_UINT32, BASE_DEC, NULL, 0,
314                      NULL, HFILL }
315               },
316
317               { &hf_esio_data_nbr,
318                      { "Nbr. of data transfers", "esio.data.nbr",
319                      FT_UINT8, BASE_DEC, NULL, 0,
320                      NULL, HFILL }
321               },
322
323               { &hf_esio_data_flags,
324                      { "Transfer header flags", "esio.data.flags",
325                      FT_UINT8, BASE_HEX, NULL, 0,
326                      NULL, HFILL }
327               },
328
329               { &hf_esio_data_transfer_id,
330                      { "Data transfer ID", "esio.data.transfer_id",
331                      FT_UINT32, BASE_DEC, NULL, 0,
332                      NULL, HFILL }
333               },
334
335               { &hf_esio_data_dest_id,
336                      { "Data destination ID", "esio.data.destination_id",
337                      FT_UINT32, BASE_DEC, NULL, 0,
338                      NULL, HFILL }
339               },
340
341               { &hf_esio_data_length,
342                      { "Data transfer length", "esio.data.length",
343                      FT_UINT16, BASE_DEC, NULL, 0,
344                      NULL, HFILL }
345               },
346
347               { &hf_esio_data,
348                      { "Data", "esio.data",
349                      FT_UINT8, BASE_DEC, NULL, 0,
350                      NULL, HFILL }
351               },
352
353               { &hf_esio_sts_type,
354                      { "Status type", "esio.sts.type",
355                      FT_UINT16, BASE_HEX, VALS(esio_sts_types), 0,
356                      NULL, HFILL }
357               },
358
359               { &hf_esio_sts_size,
360                      { "Status length (bytes)", "esio.sts.length",
361                      FT_UINT16, BASE_DEC, NULL, 0,
362                      NULL, HFILL }
363               },
364
365               { &hf_esio_rio_sts,
366                      { "RIO status", "esio.sts.rio_sts",
367                      FT_UINT8, BASE_DEC, NULL, 0,
368                      NULL, HFILL }
369               },
370
371               { &hf_esio_rio_tlgs_lost,
372                      { "Lost telegrams to RIO", "esio.sts.rio_lost_tlg",
373                      FT_UINT8, BASE_DEC, NULL, 0,
374                      NULL, HFILL }
375               },
376
377               { &hf_esio_rio_diag,
378                      { "RIO diagnostics", "esio.sts.rio_diag",
379                      FT_UINT8, BASE_DEC, NULL, 0,
380                      NULL, HFILL }
381               },
382
383               { &hf_esio_rio_flags,
384                      { "RIO flags", "esio.sts.rio_flags",
385                      FT_UINT8, BASE_HEX, NULL, 0,
386                      NULL, HFILL }
387               }
388        };
389
390
391 /* Setup protocol subtree array */
392        static gint *ett[] = {
393               &ett_esio,
394               &ett_esio_header,
395               &ett_esio_transfer_header,
396               &ett_esio_transfer_data,
397               &ett_esio_data
398        };
399
400         static ei_register_info ei[] = {
401             { &ei_esio_telegram_lost, { "esio.telegram_lost", PI_SEQUENCE, PI_NOTE, "Telegram(s) lost", EXPFILL }},
402         };
403
404        expert_module_t* expert_esio;
405
406 /* Register the protocol name and description */
407        proto_esio = proto_register_protocol("SAIA Ether-S-I/O protocol", "ESIO", "esio");
408
409 /* Required function calls to register the header fields and subtrees used */
410        proto_register_field_array(proto_esio, hf, array_length(hf));
411        proto_register_subtree_array(ett, array_length(ett));
412        expert_esio = expert_register_protocol(proto_esio);
413        expert_register_field_array(expert_esio, ei, array_length(ei));
414 }
415
416 void
417 proto_reg_handoff_esio(void)
418 {
419        dissector_handle_t esio_handle;
420
421        esio_handle = new_create_dissector_handle(dissect_esio, proto_esio);
422        dissector_add_uint("udp.port", 6060, esio_handle);
423 }
424
425 /*
426  * Editor modelines
427  *
428  * Local Variables:
429  * c-basic-offset: 7
430  * tab-width: 8
431  * indent-tabs-mode: nil
432  * End:
433  *
434  * ex: set shiftwidth=7 tabstop=8 expandtab:
435  * :indentSize=7:tabSize=8:noTabs=true:
436  */