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