Rename the routines that handle dissector tables with unsigned integer
[obnox/wireshark/wip.git] / asn1 / pcap / packet-pcap-template.c
1 /* packet-pcap.c
2  * Routines for UTRAN Iupc interface Positioning Calculation Application Part (PCAP) packet dissection
3  *
4  * Copyright 2008, Anders Broman <anders.broman@ericsson.com>
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  * Based on the RANAP dissector
27  *
28  * References: ETSI TS 125 453 V7.9.0 (2008-02)
29  */
30
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34
35 #include <glib.h>
36 #include <epan/packet.h>
37 #include <epan/prefs.h>
38
39 #include <epan/strutil.h>
40 #include <epan/asn1.h>
41
42 #include "packet-ber.h"
43 #include "packet-per.h"
44 #include "packet-sccp.h"
45
46 #ifdef _MSC_VER
47 /* disable: "warning C4146: unary minus operator applied to unsigned type, result still unsigned" */
48 #pragma warning(disable:4146)
49 #endif
50
51 #define PNAME  "UTRAN Iupc interface Positioning Calculation Application Part (PCAP)"
52 #define PSNAME "PCAP"
53 #define PFNAME "pcap"
54
55 #define MAX_SSN 254
56 static range_t *global_ssn_range;
57
58 static dissector_table_t sccp_ssn_table;
59
60 #include "packet-pcap-val.h"
61
62 static dissector_handle_t pcap_handle = NULL;
63
64 /* Initialize the protocol and registered fields */
65 static int proto_pcap = -1;
66
67 #include "packet-pcap-hf.c"
68
69 /* Initialize the subtree pointers */
70 static int ett_pcap = -1;
71
72 #include "packet-pcap-ett.c"
73
74 /* Global variables */
75 static guint32 ProcedureCode;
76 static guint32 ProtocolIE_ID;
77 static guint32 ProtocolExtensionID;
78
79 /* Dissector tables */
80 static dissector_table_t pcap_ies_dissector_table;
81 static dissector_table_t pcap_ies_p1_dissector_table;
82 static dissector_table_t pcap_ies_p2_dissector_table;
83 static dissector_table_t pcap_extension_dissector_table;
84 static dissector_table_t pcap_proc_imsg_dissector_table;
85 static dissector_table_t pcap_proc_sout_dissector_table;
86 static dissector_table_t pcap_proc_uout_dissector_table;
87 static dissector_table_t pcap_proc_out_dissector_table;
88
89 static int dissect_ProtocolIEFieldValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
90 static int dissect_ProtocolExtensionFieldExtensionValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
91 static int dissect_InitiatingMessageValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
92 static int dissect_SuccessfulOutcomeValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
93 static int dissect_UnsuccessfulOutcomeValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
94 static int dissect_OutcomeValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
95
96 #include "packet-pcap-fn.c"
97
98 static int dissect_ProtocolIEFieldValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
99 {
100   return (dissector_try_uint(pcap_ies_dissector_table, ProtocolIE_ID, tvb, pinfo, tree)) ? tvb_length(tvb) : 0;
101 }
102
103 static int dissect_ProtocolExtensionFieldExtensionValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
104 {
105   return (dissector_try_uint(pcap_extension_dissector_table, ProtocolExtensionID, tvb, pinfo, tree)) ? tvb_length(tvb) : 0;
106 }
107
108 static int dissect_InitiatingMessageValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
109 {
110   return (dissector_try_uint(pcap_proc_imsg_dissector_table, ProcedureCode, tvb, pinfo, tree)) ? tvb_length(tvb) : 0;
111 }
112
113 static int dissect_SuccessfulOutcomeValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
114 {
115   return (dissector_try_uint(pcap_proc_sout_dissector_table, ProcedureCode, tvb, pinfo, tree)) ? tvb_length(tvb) : 0;
116 }
117
118 static int dissect_UnsuccessfulOutcomeValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
119 {
120   return (dissector_try_uint(pcap_proc_uout_dissector_table, ProcedureCode, tvb, pinfo, tree)) ? tvb_length(tvb) : 0;
121 }
122
123 static int dissect_OutcomeValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
124 {
125   return (dissector_try_uint(pcap_proc_out_dissector_table, ProcedureCode, tvb, pinfo, tree)) ? tvb_length(tvb) : 0;
126 }
127
128 static void
129 dissect_pcap(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
130 {
131         proto_item      *pcap_item = NULL;
132         proto_tree      *pcap_tree = NULL;
133
134         /* make entry in the Protocol column on summary display */
135         col_set_str(pinfo->cinfo, COL_PROTOCOL, "PCAP");
136
137         /* create the pcap protocol tree */
138         pcap_item = proto_tree_add_item(tree, proto_pcap, tvb, 0, -1, FALSE);
139         pcap_tree = proto_item_add_subtree(pcap_item, ett_pcap);
140         
141         dissect_PCAP_PDU_PDU(tvb, pinfo, pcap_tree);
142 }
143
144
145 static void range_delete_callback(guint32 ssn)
146 {
147     if ( ssn ) {
148         dissector_delete_uint("sccp.ssn", ssn, pcap_handle);
149     }
150 }
151
152 static void range_add_callback(guint32 ssn)
153 {
154     if (ssn) {
155         dissector_add_uint("sccp.ssn", ssn, pcap_handle);
156     }
157 }
158
159 /*--- proto_reg_handoff_pcap ---------------------------------------*/
160 void
161 proto_reg_handoff_pcap(void)
162 {
163     static gboolean prefs_initialized = FALSE;
164     static range_t *ssn_range;
165
166     if (! prefs_initialized) {
167         pcap_handle = find_dissector("pcap");
168         sccp_ssn_table = find_dissector_table("sccp.ssn");
169         prefs_initialized = TRUE;
170 #include "packet-pcap-dis-tab.c"
171     } else {
172         range_foreach(ssn_range, range_delete_callback);
173         g_free(ssn_range);
174     }
175     ssn_range = range_copy(global_ssn_range);
176     range_foreach(ssn_range, range_add_callback);
177 }
178
179 /*--- proto_register_pcap -------------------------------------------*/
180 void proto_register_pcap(void) {
181
182   /* List of fields */
183
184   static hf_register_info hf[] = {
185
186 #include "packet-pcap-hfarr.c"
187   };
188
189   /* List of subtrees */
190   static gint *ett[] = {
191                   &ett_pcap,
192 #include "packet-pcap-ettarr.c"
193   };
194
195   module_t *pcap_module;
196
197   /* Register protocol */
198   proto_pcap = proto_register_protocol(PNAME, PSNAME, PFNAME);
199   /* Register fields and subtrees */
200   proto_register_field_array(proto_pcap, hf, array_length(hf));
201   proto_register_subtree_array(ett, array_length(ett));
202
203   pcap_module = prefs_register_protocol(proto_pcap, proto_reg_handoff_pcap);
204  
205   /* Register dissector */
206   register_dissector("pcap", dissect_pcap, proto_pcap);
207
208   /* Register dissector tables */
209   pcap_ies_dissector_table = register_dissector_table("pcap.ies", "PCAP-PROTOCOL-IES", FT_UINT32, BASE_DEC);
210   pcap_ies_p1_dissector_table = register_dissector_table("pcap.ies.pair.first", "PCAP-PROTOCOL-IES-PAIR FirstValue", FT_UINT32, BASE_DEC);
211   pcap_ies_p2_dissector_table = register_dissector_table("pcap.ies.pair.second", "PCAP-PROTOCOL-IES-PAIR SecondValue", FT_UINT32, BASE_DEC);
212   pcap_extension_dissector_table = register_dissector_table("pcap.extension", "PCAP-PROTOCOL-EXTENSION", FT_UINT32, BASE_DEC);
213   pcap_proc_imsg_dissector_table = register_dissector_table("pcap.proc.imsg", "PCAP-ELEMENTARY-PROCEDURE InitiatingMessage", FT_UINT32, BASE_DEC);
214   pcap_proc_sout_dissector_table = register_dissector_table("pcap.proc.sout", "PCAP-ELEMENTARY-PROCEDURE SuccessfulOutcome", FT_UINT32, BASE_DEC);
215   pcap_proc_uout_dissector_table = register_dissector_table("pcap.proc.uout", "PCAP-ELEMENTARY-PROCEDURE UnsuccessfulOutcome", FT_UINT32, BASE_DEC);
216   pcap_proc_out_dissector_table = register_dissector_table("pcap.proc.out", "PCAP-ELEMENTARY-PROCEDURE Outcome", FT_UINT32, BASE_DEC);
217
218
219   /* Preferences */
220   /* Set default SSNs */
221   range_convert_str(&global_ssn_range, "", MAX_SSN);
222
223   prefs_register_range_preference(pcap_module, "ssn", "SCCP SSNs",
224                                   "SCCP (and SUA) SSNs to decode as PCAP",
225                                   &global_ssn_range, MAX_SSN);
226 }
227
228
229
230