Rename the routines that handle dissector tables with unsigned integer
[obnox/wireshark/wip.git] / epan / dissectors / packet-gsm_ipa.c
1 /* packet-gsm_ipa.c
2  * Routines for packet dissection of ip.access GSM over IP
3  * Copyright 2009 by Harald Welte <laforge@gnumonks.org>
4  * Copyright 2009, 2010 by Holger Hans Peter Freyther <zecke@selfish.org>
5  *
6  * $Id$
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 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <glib.h>
32
33 #include <epan/packet.h>
34 #include <epan/ipproto.h>
35 #include <epan/prefs.h>
36
37 #define IPA_TCP_PORTS "3002,3003,3006,5000"
38 #define IPA_UDP_PORTS "3006"
39
40 static dissector_handle_t ipa_handle;
41 static range_t *global_ipa_tcp_ports = NULL;
42 static range_t *global_ipa_udp_ports = NULL;
43 static gboolean global_ipa_in_root = FALSE;
44 static gboolean global_ipa_in_info = FALSE;
45
46 /* Initialize the protocol and registered fields */
47 static int proto_ipa = -1;
48 static int proto_ipaccess = -1;
49
50 static int hf_ipa_data_len = -1;
51 static int hf_ipa_protocol = -1;
52 static int hf_ipa_hsl_debug = -1;
53
54 static int hf_ipaccess_msgtype = -1;
55 static int hf_ipaccess_attr_tag = -1;
56 static int hf_ipaccess_attr_string = -1;
57
58 /* Initialize the subtree pointers */
59 static gint ett_ipa = -1;
60 static gint ett_ipaccess = -1;
61
62 enum {
63         SUB_OML,
64         SUB_RSL,
65         SUB_SCCP,
66         SUB_MGCP,
67 /*      SUB_IPACCESS, */
68
69         SUB_MAX
70 };
71
72 static dissector_handle_t sub_handles[SUB_MAX];
73
74 #define ABISIP_RSL_MAX  0x20
75 #define HSL_DEBUG       0xdd
76 #define IPA_MGCP        0xfc
77 #define AIP_SCCP        0xfd
78 #define ABISIP_IPACCESS 0xfe
79 #define ABISIP_OML      0xff
80
81 static const value_string ipa_protocol_vals[] = {
82         { 0x00,         "RSL" },
83         { 0xdd,         "HSL Debug" },
84         { 0xfc,         "MGCP" },
85         { 0xfd,         "SCCP" },
86         { 0xfe,         "IPA" },
87         { 0xff,         "OML" },
88         { 0,            NULL }
89 };
90
91 static const value_string ipaccess_msgtype_vals[] = {
92         { 0x00,         "PING?" },
93         { 0x01,         "PONG!" },
94         { 0x04,         "IDENTITY REQUEST" },
95         { 0x05,         "IDENTITY RESPONSE" },
96         { 0x06,         "IDENTITY ACK" },
97         { 0x07,         "IDENTITY NACK" },
98         { 0x08,         "PROXY REQUEST" },
99         { 0x09,         "PROXY ACK" },
100         { 0x0a,         "PROXY NACK" },
101         { 0,            NULL }
102 };
103
104 static const value_string ipaccess_idtag_vals[] = {
105         { 0x00,         "Serial Number" },
106         { 0x01,         "Unit Name" },
107         { 0x02,         "Location" },
108         { 0x03,         "Unit Type" },
109         { 0x04,         "Equipment Version" },
110         { 0x05,         "Software Version" },
111         { 0x06,         "IP Address" },
112         { 0x07,         "MAC Address" },
113         { 0x08,         "Unit ID" },
114         { 0,            NULL }
115 };
116
117 static gint
118 dissect_ipa_attr(tvbuff_t *tvb, int base_offs, proto_tree *tree)
119 {
120         guint8 len, attr_type;
121
122         int offset = base_offs;
123
124         while (tvb_reported_length_remaining(tvb, offset) > 0) {
125                 attr_type = tvb_get_guint8(tvb, offset);
126
127                 switch (attr_type) {
128                 case 0x00:      /* a string prefixed by its length */
129                         len = tvb_get_guint8(tvb, offset+1);
130                         proto_tree_add_item(tree, hf_ipaccess_attr_tag,
131                                             tvb, offset+2, 1, FALSE);
132                         proto_tree_add_item(tree, hf_ipaccess_attr_string,
133                                             tvb, offset+3, len-1, FALSE);
134                         break;
135                 case 0x01:      /* a single-byte reqest for a certain attr */
136                         len = 0;
137                         proto_tree_add_item(tree, hf_ipaccess_attr_tag,
138                                             tvb, offset+1, 1, FALSE);
139                         break;
140                 default:
141                         len = 0;
142                         proto_tree_add_text(tree, tvb, offset+1, 1,
143                                             "unknown attribute type 0x%02x",
144                                             attr_type);
145                         break;
146                 };
147                 offset += len + 2;
148         };
149         return offset;
150 }
151
152 /* Dissect an ip.access specific message */
153 static gint
154 dissect_ipaccess(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
155 {
156         proto_item *ti;
157         proto_tree *ipaccess_tree;
158         guint8 msg_type;
159
160         msg_type = tvb_get_guint8(tvb, 0);
161
162         col_append_fstr(pinfo->cinfo, COL_INFO, "%s ",
163                         val_to_str(msg_type, ipaccess_msgtype_vals,
164                                    "unknown 0x%02x"));
165         if (tree) {
166                 ti = proto_tree_add_item(tree, proto_ipaccess, tvb, 0, -1, FALSE);
167                 ipaccess_tree = proto_item_add_subtree(ti, ett_ipaccess);
168                 proto_tree_add_item(ipaccess_tree, hf_ipaccess_msgtype,
169                                     tvb, 0, 1, FALSE);
170                 switch (msg_type) {
171                 case 4:
172                 case 5:
173                         dissect_ipa_attr(tvb, 1, ipaccess_tree);
174                         break;
175                 }
176         }
177
178         return 1;
179 }
180
181
182 /* Code to actually dissect the packets */
183 static void
184 dissect_ipa(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
185 {
186         gint remaining;
187         gint header_length = 3;
188         int offset = 0;
189
190         col_set_str(pinfo->cinfo, COL_PROTOCOL, "IPA");
191         col_clear(pinfo->cinfo, COL_INFO);
192
193         while ((remaining = tvb_reported_length_remaining(tvb, offset)) > 0) {
194                 proto_item *ti;
195                 proto_tree *ipa_tree = NULL;
196                 guint16 len, msg_type;
197                 tvbuff_t *next_tvb;
198
199                 len = tvb_get_ntohs(tvb, offset);
200                 msg_type = tvb_get_guint8(tvb, offset+2);
201
202                 col_append_fstr(pinfo->cinfo, COL_INFO, "%s ",
203                                 val_to_str(msg_type, ipa_protocol_vals,
204                                            "unknown 0x%02x"));
205
206                 /*
207                  * The IPA header is different depending on the transport protocol.
208                  * With UDP there seems to be a fourth byte for the IPA header.
209                  * We attempt to detect this by checking if the length from the
210                  * header + four bytes of the IPA header equals the remaining size.
211                  */
212                 if ((pinfo->ipproto == IP_PROTO_UDP) && (len + 4 == remaining)) {
213                         header_length++;
214                 }
215
216                 if (tree) {
217                         ti = proto_tree_add_protocol_format(tree, proto_ipa,
218                                         tvb, offset, len+header_length,
219                                         "IPA protocol ip.access, type: %s",
220                                         val_to_str(msg_type, ipa_protocol_vals,
221                                                    "unknown 0x%02x"));
222                         ipa_tree = proto_item_add_subtree(ti, ett_ipa);
223                         proto_tree_add_item(ipa_tree, hf_ipa_data_len,
224                                             tvb, offset, 2, FALSE);
225                         proto_tree_add_item(ipa_tree, hf_ipa_protocol,
226                                             tvb, offset+2, 1, FALSE);
227                 }
228
229                 next_tvb = tvb_new_subset(tvb, offset+header_length, len, len);
230
231                 switch (msg_type) {
232                 case ABISIP_OML:
233                         /* hand this off to the standard A-bis OML dissector */
234                         if (sub_handles[SUB_OML])
235                                 call_dissector(sub_handles[SUB_OML], next_tvb,
236                                                  pinfo, tree);
237                         break;
238                 case ABISIP_IPACCESS:
239                         dissect_ipaccess(next_tvb, pinfo, tree);
240                         break;
241                 case AIP_SCCP:
242                         /* hand this off to the standard SCCP dissector */
243                         call_dissector(sub_handles[SUB_SCCP], next_tvb, pinfo, tree);
244                         break;
245                 case IPA_MGCP:
246                         /* hand this off to the standard MGCP dissector */
247                         call_dissector(sub_handles[SUB_MGCP], next_tvb, pinfo, tree);
248                         break;
249                 case HSL_DEBUG:
250                         if (tree) {
251                                 proto_tree_add_item(ipa_tree, hf_ipa_hsl_debug,
252                                                     next_tvb, 0, len, FALSE);
253                                 if (global_ipa_in_root == TRUE)
254                                         proto_tree_add_item(tree, hf_ipa_hsl_debug,
255                                                             next_tvb, 0, len, FALSE);
256                         }
257                         if (global_ipa_in_info == TRUE)
258                                 col_append_fstr(pinfo->cinfo, COL_INFO, "%s ",
259                                                 tvb_get_stringz(next_tvb, 0, NULL));
260                         break;
261                 default:
262                         if (msg_type < ABISIP_RSL_MAX) {
263                                 /* hand this off to the standard A-bis RSL dissector */
264                                 call_dissector(sub_handles[SUB_RSL], next_tvb, pinfo, tree);
265                         }
266                         break;
267                 }
268                 offset += len + header_length;
269         }
270 }
271
272 void proto_reg_handoff_gsm_ipa(void);
273
274 void proto_register_ipa(void)
275 {
276         module_t *ipa_module;
277
278         static hf_register_info hf[] = {
279                 {&hf_ipa_data_len,
280                  {"DataLen", "ipa.data_len",
281                   FT_UINT16, BASE_DEC, NULL, 0x0,
282                   "The length of the data (in bytes)", HFILL}
283                  },
284                 {&hf_ipa_protocol,
285                  {"Protocol", "ipa.protocol",
286                   FT_UINT8, BASE_HEX, VALS(ipa_protocol_vals), 0x0,
287                   "The IPA Sub-Protocol", HFILL}
288                  },
289                 {&hf_ipa_hsl_debug,
290                  {"Debug Message", "ipa.hsl_debug",
291                   FT_STRING, BASE_NONE, NULL, 0,
292                   "Hay Systems Limited debug message", HFILL}
293                 },
294         };
295         static hf_register_info hf_ipa[] = {
296                 {&hf_ipaccess_msgtype,
297                  {"MessageType", "ipaccess.msg_type",
298                   FT_UINT8, BASE_HEX, VALS(ipaccess_msgtype_vals), 0x0,
299                   "Type of ip.access messsage", HFILL}
300                  },
301                 {&hf_ipaccess_attr_tag,
302                  {"Tag", "ipaccess.attr_tag",
303                   FT_UINT8, BASE_HEX, VALS(ipaccess_idtag_vals), 0x0,
304                   "Attribute Tag", HFILL}
305                  },
306                 {&hf_ipaccess_attr_string,
307                  {"String", "ipaccess.attr_string",
308                   FT_STRING, BASE_NONE, NULL, 0x0,
309                   "String attribute", HFILL}
310                  },
311         };
312
313         static gint *ett[] = {
314                 &ett_ipa,
315                 &ett_ipaccess,
316         };
317
318         proto_ipa =
319             proto_register_protocol("GSM over IP protocol as used by ip.access",
320                                     "GSM over IP", "gsm_ipa");
321         proto_ipaccess =
322             proto_register_protocol("GSM over IP ip.access CCM sub-protocol",
323                                     "IPA", "ipaccess");
324
325         proto_register_field_array(proto_ipa, hf, array_length(hf));
326         proto_register_field_array(proto_ipaccess, hf_ipa, array_length(hf_ipa));
327         proto_register_subtree_array(ett, array_length(ett));
328
329         register_dissector("gsm_ipa", dissect_ipa, proto_ipa);
330
331         range_convert_str(&global_ipa_tcp_ports, IPA_TCP_PORTS, MAX_TCP_PORT);
332         range_convert_str(&global_ipa_udp_ports, IPA_UDP_PORTS, MAX_UDP_PORT);
333         ipa_module = prefs_register_protocol(proto_ipa,
334                                              proto_reg_handoff_gsm_ipa);
335
336         prefs_register_range_preference(ipa_module, "tcp_ports",
337                                         "GSM IPA TCP Port(s)",
338                                         "Set the port(s) for ip.access IPA"
339                                         " (default: " IPA_TCP_PORTS ")",
340                                         &global_ipa_tcp_ports, MAX_TCP_PORT);
341         prefs_register_range_preference(ipa_module, "udp_ports",
342                                         "GSM IPA UDP Port(s)",
343                                         "Set the port(s) for ip.access IPA"
344                                         " (default: " IPA_UDP_PORTS ")",
345                                         &global_ipa_udp_ports, MAX_UDP_PORT);
346
347         prefs_register_bool_preference(ipa_module, "hsl_debug_in_root_tree",
348                                         "HSL Debug messages in root protocol tree",
349                                         NULL, &global_ipa_in_root);
350         prefs_register_bool_preference(ipa_module, "hsl_debug_in_info",
351                                         "HSL Debug messages in INFO column",
352                                         NULL, &global_ipa_in_info);
353 }
354
355 static void ipa_tcp_delete_callback(guint32 port)
356 {
357         if (port)
358                 dissector_delete_uint("tcp.port", port, ipa_handle);
359 }
360
361 static void ipa_udp_delete_callback(guint32 port)
362 {
363         if (port)
364                 dissector_delete_uint("udp.port", port, ipa_handle);
365 }
366
367 static void ipa_tcp_add_callback(guint32 port)
368 {
369         if (port)
370                 dissector_add_uint("tcp.port", port, ipa_handle);
371 }
372
373 static void ipa_udp_add_callback(guint32 port)
374 {
375         if (port)
376                 dissector_add_uint("udp.port", port, ipa_handle);
377 }
378
379 void proto_reg_handoff_gsm_ipa(void)
380 {
381         static gboolean ipa_initialized = FALSE;
382         static range_t *ipa_tcp_ports, *ipa_udp_ports;
383
384         if (!ipa_initialized) {
385                 sub_handles[SUB_RSL] = find_dissector("gsm_abis_rsl");
386                 sub_handles[SUB_OML] = find_dissector("gsm_abis_oml");
387                 sub_handles[SUB_SCCP] = find_dissector("sccp");
388                 sub_handles[SUB_MGCP] = find_dissector("mgcp");
389
390                 ipa_handle = create_dissector_handle(dissect_ipa, proto_ipa);
391                 ipa_initialized = TRUE;
392         } else {
393                 range_foreach(ipa_tcp_ports, ipa_tcp_delete_callback);
394                 g_free(ipa_tcp_ports);
395                 range_foreach(ipa_udp_ports, ipa_udp_delete_callback);
396                 g_free(ipa_udp_ports);
397         }
398
399         ipa_tcp_ports = range_copy(global_ipa_tcp_ports);
400         ipa_udp_ports = range_copy(global_ipa_udp_ports);
401
402         range_foreach(ipa_tcp_ports, ipa_tcp_add_callback);
403         range_foreach(ipa_udp_ports, ipa_udp_add_callback);
404 }