checkAPIs.pl: support for new-style dissectors in check_hf_entries
[metze/wireshark/wip.git] / epan / dissectors / packet-interlink.c
1 /* packet-interlink.c
2  * Routines for Interlink protocol packet disassembly
3  * By Uwe Girlich <uwe.girlich@philosys.de>
4  * Copyright 2010 Uwe Girlich
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * SPDX-License-Identifier: GPL-2.0-or-later
11  */
12
13 #include "config.h"
14
15 #include <epan/packet.h>
16
17 void proto_register_interlink(void);
18 void proto_reg_handoff_interlink(void);
19
20 /*
21  * No public information available.
22  */
23
24 static int proto_interlink = -1;
25
26 static int hf_interlink_id = -1;
27 static int hf_interlink_version = -1;
28 static int hf_interlink_cmd = -1;
29 static int hf_interlink_seq = -1;
30 static int hf_interlink_flags = -1;
31 static int hf_interlink_flags_req_ack = -1;
32 static int hf_interlink_flags_inc_ack_port = -1;
33 static int hf_interlink_block_type = -1;
34 static int hf_interlink_block_version = -1;
35 static int hf_interlink_block_length = -1;
36
37 static gint ett_interlink = -1;
38 static gint ett_interlink_header = -1;
39 static gint ett_interlink_flags = -1;
40 static gint ett_interlink_block = -1;
41
42 static dissector_handle_t data_handle;
43 static dissector_table_t subdissector_table;
44 static dissector_handle_t interlink_handle;
45
46 static const true_false_string flags_set_notset = {
47         "Set", "Not set"
48 };
49
50 static const value_string names_cmd[] = {
51         { 1, "Data" },
52         { 2, "Ack" },
53         { 0, NULL }
54 };
55
56
57 static int
58 dissect_interlink(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
59 {
60         int             offset = 0;
61         proto_tree      *il_tree;
62         proto_item      *il_item;
63         proto_tree      *ilh_tree = NULL;
64         proto_tree      *ilb_tree = NULL;
65         guint8          ilb_type;
66         guint8          ilb_version;
67         guint16         type_version = 0;
68         dissector_handle_t      handle;
69         tvbuff_t        *next_tvb;
70
71         col_set_str(pinfo->cinfo, COL_PROTOCOL, "INTERLINK");
72         col_clear(pinfo->cinfo, COL_INFO);
73
74         il_item = proto_tree_add_item(tree, proto_interlink,
75                                                         tvb, 0, 16, ENC_NA);
76         il_tree = proto_item_add_subtree(il_item, ett_interlink);
77
78         ilh_tree = proto_tree_add_subtree(il_tree, tvb, 0, 12, ett_interlink_header, NULL, "Interlink Header");
79
80         if (ilh_tree) {
81                 proto_tree_add_item(ilh_tree, hf_interlink_id, tvb, offset, 4, ENC_ASCII|ENC_NA);
82                 offset += 4;
83                 proto_tree_add_item(ilh_tree, hf_interlink_version, tvb, offset, 2, ENC_LITTLE_ENDIAN);
84                 offset += 2;
85                 proto_tree_add_item(ilh_tree, hf_interlink_cmd, tvb, offset, 2, ENC_LITTLE_ENDIAN);
86                 offset += 2;
87                 proto_tree_add_item(ilh_tree, hf_interlink_seq, tvb, offset, 2, ENC_LITTLE_ENDIAN);
88                 offset += 2;
89         } else {
90                 offset += 10;
91         }
92
93         if (ilh_tree) {
94                 static const int * flags[] = {
95                         &hf_interlink_flags_req_ack,
96                         &hf_interlink_flags_inc_ack_port,
97                         NULL
98                 };
99
100                 proto_tree_add_bitmask(ilh_tree, tvb, offset, hf_interlink_flags, ett_interlink_flags, flags, ENC_LITTLE_ENDIAN);
101
102         }
103         offset += 2;
104
105         ilb_tree = proto_tree_add_subtree(il_tree, tvb, offset, 4, ett_interlink_block, NULL, "Block Header");
106
107         ilb_type = tvb_get_guint8(tvb, offset);
108         ilb_version = tvb_get_guint8(tvb, offset + 1);
109         type_version = ilb_type << 8 | ilb_version;
110         col_append_fstr(pinfo->cinfo, COL_INFO, "Type: %d, Version: %d",
111                 ilb_type, ilb_version);
112
113         if (ilb_tree) {
114                 proto_tree_add_item(ilb_tree, hf_interlink_block_type, tvb, offset, 1, ENC_BIG_ENDIAN);
115                 offset += 1;
116                 proto_tree_add_item(ilb_tree, hf_interlink_block_version, tvb, offset, 1, ENC_BIG_ENDIAN);
117                 offset += 1;
118                 proto_tree_add_item(ilb_tree, hf_interlink_block_length, tvb, offset, 2, ENC_LITTLE_ENDIAN);
119                 offset += 2;
120         } else {
121                 offset += 4;
122         }
123
124         /* Generate a new tvb for the rest. */
125         next_tvb = tvb_new_subset_remaining(tvb, offset);
126
127         /* Probably a sub-dissector exists for this type/version combination. */
128         handle = dissector_get_uint_handle(subdissector_table, type_version);
129
130         /* Without a proper sub-dissector, we use "data". */
131         if (handle == NULL) handle = data_handle;
132
133         /* Call the sub-dissector. */
134         call_dissector(handle, next_tvb, pinfo, tree);
135
136         return tvb_captured_length(tvb);
137 }
138
139
140 static gboolean
141 dissect_interlink_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
142 {
143         if (!tvb_bytes_exist(tvb, 0, 4)) {
144                 return FALSE;
145         }
146         if (
147                 tvb_get_guint8(tvb,0) != 'I' ||
148                 tvb_get_guint8(tvb,1) != 'L' ||
149                 tvb_get_guint8(tvb,2) != 'N' ||
150                 tvb_get_guint8(tvb,3) != 'K'
151         )
152                 return FALSE;
153
154         dissect_interlink(tvb, pinfo, tree, data);
155         return TRUE;
156 }
157
158
159 void
160 proto_register_interlink(void)
161 {
162         static hf_register_info hf[] = {
163                 { &hf_interlink_id, {
164                         "Magic ID", "interlink.id", FT_STRING,
165                         BASE_NONE, NULL, 0, NULL, HFILL }},
166                 { &hf_interlink_version, {
167                         "Version", "interlink.version", FT_UINT16,
168                         BASE_DEC, NULL, 0, NULL, HFILL }},
169                 { &hf_interlink_cmd, {
170                         "Command", "interlink.cmd", FT_UINT16,
171                         BASE_DEC, VALS(names_cmd), 0, NULL, HFILL }},
172                 { &hf_interlink_seq, {
173                         "Sequence", "interlink.seq", FT_UINT16,
174                         BASE_DEC, NULL, 0, NULL, HFILL }},
175                 { &hf_interlink_flags, {
176                         "Flags", "interlink.flags", FT_UINT16,
177                         BASE_HEX, NULL, 0, NULL, HFILL }},
178                 { &hf_interlink_flags_req_ack, {
179                         "REQ_ACK", "interlink.flags.req_ack", FT_BOOLEAN,
180                         16, TFS(&flags_set_notset), 0x01, NULL, HFILL }},
181                 { &hf_interlink_flags_inc_ack_port, {
182                         "INC_ACK_PORT", "interlink.flags.inc_ack_port", FT_BOOLEAN,
183                         16, TFS(&flags_set_notset), 0x02, NULL, HFILL }},
184                 { &hf_interlink_block_type, {
185                         "Type", "interlink.type", FT_UINT8,
186                         BASE_DEC, NULL, 0, NULL, HFILL }},
187                 { &hf_interlink_block_version, {
188                         "Version", "interlink.block_version", FT_UINT8,
189                         BASE_DEC, NULL, 0, NULL, HFILL }},
190                 { &hf_interlink_block_length, {
191                         "Length", "interlink.length", FT_UINT16,
192                         BASE_DEC, NULL, 0, NULL, HFILL }},
193         };
194
195         static gint *ett[] = {
196                 &ett_interlink,
197                 &ett_interlink_header,
198                 &ett_interlink_flags,
199                 &ett_interlink_block,
200         };
201
202         proto_interlink = proto_register_protocol("Interlink Protocol",
203                                                         "Interlink",
204                                                         "interlink");
205         proto_register_field_array(proto_interlink, hf, array_length(hf));
206         proto_register_subtree_array(ett, array_length(ett));
207         interlink_handle = register_dissector("interlink", dissect_interlink, proto_interlink);
208
209         /* Probably someone will write sub-dissectors. You can never know. */
210         subdissector_table = register_dissector_table("interlink.type_version",
211                 "Interlink type_version", proto_interlink, FT_UINT16, BASE_HEX);
212 }
213
214
215 void
216 proto_reg_handoff_interlink(void)
217 {
218         /* Allow "Decode As" with any UDP packet. */
219         dissector_add_for_decode_as_with_preference("udp.port", interlink_handle);
220
221         /* Add our heuristic packet finder. */
222         heur_dissector_add("udp", dissect_interlink_heur, "Interlink over UDP", "interlink_udp", proto_interlink, HEURISTIC_ENABLE);
223
224         data_handle = find_dissector("data");
225 }
226
227 /*
228  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
229  *
230  * Local variables:
231  * c-basic-offset: 8
232  * tab-width: 8
233  * indent-tabs-mode: t
234  * End:
235  *
236  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
237  * :indentSize=8:tabSize=8:noTabs=false:
238  */