name change
[gd/wireshark/.git] / asn1 / tcap / packet-tcap-template.c
1 /* packet-tcap-template.c
2  * Routines for  TCAP
3  * Copyright 2004 - 2005, Tim Endean <endeant@hotmail.com>
4  * Built from the gsm-map dissector Copyright 2004 - 2005, Anders Broman <anders.broman@ericsson.com>
5  *
6  * $Id$
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  * References: ETSI 300 374
25  */
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <glib.h>
32 #include <epan/packet.h>
33 #include <epan/prefs.h>
34 #include <epan/conversation.h>
35
36 #include <stdio.h>
37 #include <string.h>
38 #include "packet-ber.h"
39 #include "packet-tcap.h"
40
41 #define PNAME  "Transaction Capabilities Application Part"
42 #define PSNAME "TCAP"
43 #define PFNAME "tcap"
44
45 /* Initialize the protocol and registered fields */
46 int proto_tcap = -1;
47 static int hf_tcap_tag = -1; 
48 static int hf_tcap_length = -1; 
49 static int hf_tcap_data = -1;
50 static int hf_tcap_tid = -1;
51
52 #include "packet-tcap-hf.c"
53
54 /* Initialize the subtree pointers */
55 static gint ett_tcap = -1;
56 static gint ett_param = -1;
57
58 static gint ett_otid = -1;
59 static gint ett_dtid = -1;
60
61
62 #include "packet-tcap-ett.c"
63
64 #define MAX_SSN 254
65 static range_t *global_ssn_range;
66 static range_t *ssn_range;
67
68 dissector_handle_t      tcap_handle = NULL;
69 static dissector_table_t ber_oid_dissector_table=NULL;
70 static const char * cur_oid;
71 static const char * tcapext_oid;
72 static proto_tree       *tcap_top_tree=NULL;
73 static dissector_handle_t data_handle;
74
75 static dissector_table_t sccp_ssn_table;
76 static int dissect_tcap_param(packet_info *pinfo, proto_tree *tree, tvbuff_t *tvb, int offset);
77 static int dissect_tcap_UserInformation(gboolean implicit_tag _U_, tvbuff_t *tvb, int offset, packet_info *pinfo _U_, proto_tree *tree, int hf_index _U_);
78
79
80 static GHashTable* ansi_sub_dissectors = NULL;
81 static GHashTable* itu_sub_dissectors = NULL;
82
83 static void dissect_tcap(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree);
84
85 extern void add_ansi_tcap_subdissector(guint32 ssn, dissector_handle_t dissector) {
86     g_hash_table_insert(ansi_sub_dissectors,GUINT_TO_POINTER(ssn),dissector);
87     dissector_add("sccp.ssn",ssn,tcap_handle);
88 }
89
90 extern void add_itu_tcap_subdissector(guint32 ssn, dissector_handle_t dissector) {
91     g_hash_table_insert(itu_sub_dissectors,GUINT_TO_POINTER(ssn),dissector);
92     dissector_add("sccp.ssn",ssn,tcap_handle);
93 }
94
95 extern void delete_ansi_tcap_subdissector(guint32 ssn, dissector_handle_t dissector _U_) {
96     g_hash_table_remove(ansi_sub_dissectors,GUINT_TO_POINTER(ssn));
97     dissector_delete("sccp.ssn",ssn,tcap_handle);
98 }
99 extern void delete_itu_tcap_subdissector(guint32 ssn, dissector_handle_t dissector _U_) {
100     g_hash_table_remove(itu_sub_dissectors,GUINT_TO_POINTER(ssn));
101         dissector_delete("sccp.ssn", ssn,tcap_handle);
102 }
103
104 static dissector_handle_t get_ansi_tcap_subdissector(guint32 ssn) {
105     return g_hash_table_lookup(ansi_sub_dissectors,GUINT_TO_POINTER(ssn));
106 }
107
108 dissector_handle_t get_itu_tcap_subdissector(guint32 ssn) {
109     return g_hash_table_lookup(itu_sub_dissectors,GUINT_TO_POINTER(ssn));
110 }
111
112
113
114 #include "packet-tcap-fn.c"
115
116
117
118 const value_string tcap_component_type_str[] = {
119     { TCAP_COMP_INVOKE,         "Invoke" },
120     { TCAP_COMP_RRL,            "Return Result(L)" },
121     { TCAP_COMP_RE,                     "Return Error" },
122     { TCAP_COMP_REJECT,         "Reject" },
123     { TCAP_COMP_RRN,            "Return Result(NL)" },
124     { 0,                        NULL } };
125
126
127 static void
128 dissect_tcap(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree)
129 {
130     proto_item          *item=NULL;
131     proto_tree          *tree=NULL;
132
133     tcap_top_tree = parent_tree;
134     if (check_col(pinfo->cinfo, COL_PROTOCOL))
135     {
136                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "TCAP");
137     }
138
139     /* create display subtree for the protocol */
140     if(parent_tree){
141        item = proto_tree_add_item(parent_tree, proto_tcap, tvb, 0, -1, FALSE);
142        tree = proto_item_add_subtree(item, ett_tcap);
143     }
144     cur_oid = NULL;
145     tcapext_oid = NULL;
146     pinfo->private_data = NULL;
147     dissect_tcap_MessageType(FALSE, tvb, 0, pinfo, tree, -1);
148
149
150 }
151
152
153 void
154 proto_reg_handoff_tcap(void)
155 {
156     
157     static gboolean prefs_initialized = FALSE;
158     
159     if (! prefs_initialized) {
160         sccp_ssn_table = find_dissector_table("sccp.ssn");
161         prefs_initialized = TRUE;
162     }
163     
164     register_ber_oid_name("0.0.17.773.1.1.1",
165                           "itu-t(0) recommendation(0) q(17) 773 as(1) dialogue-as(1) version1(1)");
166
167     data_handle = find_dissector("data");    
168 }
169
170 static void init_tcap(void);
171
172 void
173 proto_register_tcap(void)
174 {
175
176 /* Setup list of header fields  See Section 1.6.1 for details*/
177     static hf_register_info hf[] = {
178         { &hf_tcap_tag,
179                 { "Tag",           "tcap.msgtype",
180                 FT_UINT8, BASE_HEX, NULL, 0,
181                 "", HFILL }
182         },
183         { &hf_tcap_length,
184                 { "Length", "tcap.len",
185                 FT_UINT8, BASE_HEX, NULL, 0,
186                 "", HFILL }
187         },
188         { &hf_tcap_data,
189                 { "Data", "tcap.data",
190                 FT_BYTES, BASE_HEX, NULL, 0,
191                 "", HFILL }
192         },
193                 { &hf_tcap_tid,
194                 { "Transaction Id", "tcap.tid",
195                 FT_BYTES, BASE_HEX, NULL, 0,
196                 "", HFILL }
197         },
198 #include "packet-tcap-hfarr.c"  
199     };
200
201 /* Setup protocol subtree array */
202     static gint *ett[] = {
203         &ett_tcap,
204         &ett_param,
205         &ett_otid,
206         &ett_dtid,
207         #include "packet-tcap-ettarr.c"
208     };
209
210     /*static enum_val_t tcap_options[] = {
211         { "itu", "ITU",  ITU_TCAP_STANDARD },
212         { "ansi", "ANSI", ANSI_TCAP_STANDARD },
213         { NULL, NULL, 0 }
214     };*/
215
216     module_t *tcap_module;
217
218 /* Register the protocol name and description */
219     proto_tcap = proto_register_protocol(PNAME, PSNAME, PFNAME);
220
221 /* Required function calls to register the header fields and subtrees used */
222     proto_register_field_array(proto_tcap, hf, array_length(hf));
223     proto_register_subtree_array(ett, array_length(ett));
224
225     tcap_module = prefs_register_protocol(proto_tcap, proto_reg_handoff_tcap);
226
227 #if 0
228     prefs_register_enum_preference(tcap_module, "standard", "ITU TCAP standard",
229         "The SS7 standard used in ITU TCAP packets",
230         &tcap_standard, tcap_options, FALSE);
231 #else
232     prefs_register_obsolete_preference(tcap_module, "standard");
233 #endif
234
235 #if 0
236     prefs_register_bool_preference(tcap_module, "lock_info_col", "Lock Info column",
237         "Always show TCAP in Info column",
238         &lock_info_col);
239 #else
240     prefs_register_obsolete_preference(tcap_module, "lock_info_col");
241 #endif
242
243     /* Set default SSNs */
244     range_convert_str(&global_ssn_range, "", MAX_SSN);
245     ssn_range = range_empty();
246
247     prefs_register_range_preference(tcap_module, "ssn", "SCCP SSNs",
248         "SCCP (and SUA) SSNs to decode as TCAP",
249         &global_ssn_range, MAX_SSN);
250     
251     ansi_sub_dissectors = g_hash_table_new(g_direct_hash,g_direct_equal);
252     itu_sub_dissectors = g_hash_table_new(g_direct_hash,g_direct_equal);
253
254     /* 'globally' register dissector */
255     register_dissector("tcap", dissect_tcap, proto_tcap);
256
257     tcap_handle = create_dissector_handle(dissect_tcap, proto_tcap);
258
259     register_init_routine(&init_tcap);
260 }
261
262
263 static void range_delete_callback(guint32 ssn)
264 {
265     if ( ssn && !get_ansi_tcap_subdissector(ssn) && !get_itu_tcap_subdissector(ssn) ) {
266         dissector_delete("sccp.ssn", ssn, tcap_handle);
267     }
268 }
269
270 static void range_add_callback(guint32 ssn)
271 {
272     if (ssn && !get_ansi_tcap_subdissector(ssn) && !get_itu_tcap_subdissector(ssn) ) {
273         dissector_add("sccp.ssn", ssn, tcap_handle);
274     }
275 }
276
277
278 static void init_tcap(void) {
279     if (ssn_range) {
280         range_foreach(ssn_range, range_delete_callback);
281         g_free(ssn_range);
282     }
283     
284     ssn_range = range_copy(global_ssn_range);
285     range_foreach(ssn_range, range_add_callback);
286     
287 }
288
289 static int
290 dissect_tcap_param(packet_info *pinfo, proto_tree *tree, tvbuff_t *tvb, int offset)
291 {
292     gint tag_offset, saved_offset, len_offset;
293     tvbuff_t    *next_tvb;
294     proto_tree *subtree;
295     proto_item *pi;
296     gint8 class;
297     gboolean pc;
298     gint32 tag;
299     guint32 len;
300     gboolean ind_field;
301     
302     while (tvb_reported_length_remaining(tvb, offset) > 0)
303     {
304         saved_offset = offset;
305     
306         offset = get_ber_identifier(tvb, offset, &class, &pc, &tag);
307         tag_offset = offset;
308         offset = get_ber_length(tree, tvb, offset, &len, &ind_field);
309         len_offset = offset;
310
311         if (pc)
312         {
313             pi =
314                 proto_tree_add_text(tree, tvb, saved_offset, len + (len_offset - saved_offset), "CONSTRUCTOR");
315             subtree = proto_item_add_subtree(pi, ett_param);
316             proto_tree_add_uint_format(subtree, hf_tcap_tag, tvb,
317                 saved_offset, tag_offset-saved_offset, tag, "CONSTRUCTOR Tag");
318             proto_tree_add_uint(subtree, hf_tcap_tag, tvb,
319                 saved_offset, tag_offset-saved_offset, class);
320
321             proto_tree_add_uint(subtree, hf_tcap_length, tvb,
322                 tag_offset, len_offset-tag_offset, len);
323                 if (len-(2*ind_field)) /*should always be positive unless we get an empty contructor pointless? */
324                 {
325                 next_tvb = tvb_new_subset(tvb, offset, len-(2*ind_field), len-(2*ind_field));           
326                         dissect_tcap_param(pinfo, subtree,next_tvb,0);
327             }           
328                 if (ind_field)
329                         proto_tree_add_text(subtree, tvb, offset+len-2, 2, "CONSTRUCTOR EOC");
330             offset += len;
331         }
332         else
333         {
334             pi = proto_tree_add_text(tree, tvb,
335                 saved_offset, len + (len_offset - saved_offset), "Parameter (0x%.2x)", tag);
336
337             subtree = proto_item_add_subtree(pi, ett_param);
338
339             proto_tree_add_uint(subtree, hf_tcap_tag, tvb,
340                 saved_offset, 1, tag);
341
342             proto_tree_add_uint(subtree, hf_tcap_length, tvb,
343                 saved_offset+1, 1, len);
344                 if (len) /* check for NULLS */
345                         {
346                 next_tvb = tvb_new_subset(tvb, offset, len, len);               
347                 dissect_ber_octet_string(TRUE, pinfo, tree, next_tvb, 0, hf_tcap_data,
348                                         NULL);
349                 }
350             offset += len;
351         }
352     }
353     return offset;
354 }