Rename the routines that handle dissector tables with unsigned integer
[obnox/wireshark/wip.git] / plugins / ethercat / packet-ethercat-frame.c
1 /* packet-ethercat-frame.c
2  * Routines for ethercat packet disassembly
3  *
4  * $Id$
5  *
6  * Copyright (c) 2007 by Beckhoff Automation GmbH
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
10  * Copyright 1998 Gerald Combs
11  * 
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  * 
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  * 
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  */
26
27 /* Include files */
28
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #include <glib.h>
34
35 #include <epan/packet.h>
36 #include <epan/etypes.h>
37
38 #include "packet-ethercat-frame.h"
39
40 /* Define the Ethercat frame proto */
41 static int proto_ethercat_frame = -1;
42
43 static dissector_table_t ethercat_frame_dissector_table;
44 static dissector_handle_t ethercat_frame_data_handle;
45
46 /* Define the tree for the EtherCAT frame */
47 static int ett_ethercat_frame = -1;
48 static int hf_ethercat_frame_length = -1;
49 static int hf_ethercat_frame_reserved = -1;
50 static int hf_ethercat_frame_type = -1;
51
52 static const value_string EthercatFrameTypes[] =
53 {
54    { 1, "EtherCAT command", },
55    { 2, "ADS", },
56    { 3, "RAW-IO", },
57    { 4, "NV", },
58    { 0,  NULL }
59 };
60
61 static const value_string ethercat_frame_reserved_vals[] =
62 {
63    { 0, "Valid"},
64    { 1, "Invalid (must be zero for conformance with the protocol specification)"},
65    { 0, NULL}
66 };
67
68 /* Ethercat Frame */
69 static void dissect_ethercat_frame(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
70 {
71    tvbuff_t *next_tvb;
72    proto_item *ti;
73    proto_tree *ethercat_frame_tree;
74    gint offset = 0;
75    EtherCATFrameParserHDR hdr;
76
77    col_set_str(pinfo->cinfo, COL_PROTOCOL, "ECATF");
78
79    col_clear(pinfo->cinfo, COL_INFO);
80
81    if (tree)
82    {
83       ti = proto_tree_add_item(tree, proto_ethercat_frame, tvb, offset, EtherCATFrameParserHDR_Len, TRUE);
84       ethercat_frame_tree = proto_item_add_subtree(ti, ett_ethercat_frame);
85
86       proto_tree_add_item(ethercat_frame_tree, hf_ethercat_frame_length, tvb, offset, EtherCATFrameParserHDR_Len, TRUE);
87       proto_tree_add_item(ethercat_frame_tree, hf_ethercat_frame_reserved, tvb, offset, EtherCATFrameParserHDR_Len, TRUE);
88       proto_tree_add_item(ethercat_frame_tree, hf_ethercat_frame_type, tvb, offset, EtherCATFrameParserHDR_Len, TRUE);
89    }
90    hdr.hdr = tvb_get_letohs(tvb, offset);
91    offset = EtherCATFrameParserHDR_Len;
92
93    /* The EtherCAT frame header has now been processed, allow sub dissectors to
94       handle the rest of the PDU. */
95    next_tvb = tvb_new_subset_remaining (tvb, offset);
96
97    if (!dissector_try_uint(ethercat_frame_dissector_table, hdr.v.protocol,
98        next_tvb, pinfo, tree))
99    {
100       col_add_fstr (pinfo->cinfo, COL_PROTOCOL, "0x%04x", hdr.v.protocol);
101       /* No sub dissector wanted to handle this payload, decode it as general
102       data instead. */
103       call_dissector (ethercat_frame_data_handle, next_tvb, pinfo, tree);
104    }
105 }
106
107 void proto_register_ethercat_frame(void)
108 {
109    static hf_register_info hf[] =
110    {
111       { &hf_ethercat_frame_length,
112       { "Length", "ecatf.length",
113       FT_UINT16, BASE_HEX, NULL, 0x07FF,
114       NULL, HFILL }
115       },
116
117       { &hf_ethercat_frame_reserved,
118       { "Reserved", "ecatf.reserved",
119       FT_UINT16, BASE_HEX, VALS(ethercat_frame_reserved_vals), 0x0800,
120       NULL, HFILL}
121       },
122
123       { &hf_ethercat_frame_type,
124       { "Type", "ecatf.type",
125       FT_UINT16, BASE_HEX, VALS(EthercatFrameTypes), 0xF000,
126       "E88A4 Types", HFILL }
127       }
128    };
129
130    static gint *ett[] =
131    {
132       &ett_ethercat_frame
133    };
134
135    proto_ethercat_frame = proto_register_protocol("EtherCAT frame header",
136       "ETHERCAT","ethercat");
137    proto_register_field_array(proto_ethercat_frame,hf,array_length(hf));
138    proto_register_subtree_array(ett, array_length(ett));
139
140    register_dissector("ecatf", dissect_ethercat_frame, proto_ethercat_frame);
141
142    /* Define a handle (ecatf.type) for sub dissectors that want to dissect
143       the Ethercat frame ether type (E88A4) payload. */
144    ethercat_frame_dissector_table = register_dissector_table("ecatf.type",
145       "EtherCAT frame type", FT_UINT8, BASE_DEC);
146 }
147
148 void proto_reg_handoff_ethercat_frame(void)
149 {
150    dissector_handle_t ethercat_frame_handle;
151
152    ethercat_frame_handle = find_dissector("ecatf");
153    dissector_add_uint("ethertype", ETHERTYPE_ECATF, ethercat_frame_handle);
154    dissector_add_uint("udp.port", ETHERTYPE_ECATF, ethercat_frame_handle);
155    dissector_add_uint("tcp.port", ETHERTYPE_ECATF, ethercat_frame_handle);
156    ethercat_frame_data_handle = find_dissector("data");
157 }