Convert a few more dissectors to wmem
[metze/wireshark/wip.git] / asn1 / x2ap / packet-x2ap-template.c
1 /* packet-x2ap.c
2  * Routines for dissecting Evolved Universal Terrestrial Radio Access Network (EUTRAN);
3  * X2 Application Protocol (X2AP);
4  * 3GPP TS 36.423 packet dissection
5  * Copyright 2007-2010, Anders Broman <anders.broman@ericsson.com>
6  *
7  * $Id$
8  *
9  * Wireshark - Network traffic analyzer
10  * By Gerald Combs <gerald@wireshark.org>
11  * Copyright 1998 Gerald Combs
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26  *
27  * Ref:
28  * 3GPP TS 36.423 V9.2.0 (2010-03)
29  */
30
31 #include "config.h"
32
33 #include <glib.h>
34 #include <epan/packet.h>
35
36 #include <epan/asn1.h>
37 #include <epan/prefs.h>
38 #include <epan/sctpppids.h>
39
40 #include "packet-per.h"
41 #include "packet-e212.h"
42 #include "packet-lte-rrc.h"
43
44 #ifdef _MSC_VER
45 /* disable: "warning C4146: unary minus operator applied to unsigned type, result still unsigned" */
46 #pragma warning(disable:4146)
47 #endif
48
49 #define PNAME  "EUTRAN X2 Application Protocol (X2AP)"
50 #define PSNAME "X2AP"
51 #define PFNAME "x2ap"
52
53 void proto_register_x2ap(void);
54
55 /* Dissector will use SCTP PPID 27 or SCTP port. IANA assigned port = 36422 */
56 #define SCTP_PORT_X2AP  36422
57
58 #include "packet-x2ap-val.h"
59
60 /* Initialize the protocol and registered fields */
61 static int proto_x2ap = -1;
62 static int hf_x2ap_transportLayerAddressIPv4 = -1;
63 static int hf_x2ap_transportLayerAddressIPv6 = -1;
64 #include "packet-x2ap-hf.c"
65
66 /* Initialize the subtree pointers */
67 static int ett_x2ap = -1;
68 static int ett_x2ap_TransportLayerAddress = -1;
69 #include "packet-x2ap-ett.c"
70
71 /* Global variables */
72 static guint32 ProcedureCode;
73 static guint32 ProtocolIE_ID;
74 static guint gbl_x2apSctpPort=SCTP_PORT_X2AP;
75
76 /* Dissector tables */
77 static dissector_table_t x2ap_ies_dissector_table;
78 static dissector_table_t x2ap_extension_dissector_table;
79 static dissector_table_t x2ap_proc_imsg_dissector_table;
80 static dissector_table_t x2ap_proc_sout_dissector_table;
81 static dissector_table_t x2ap_proc_uout_dissector_table;
82
83 static int dissect_ProtocolIEFieldValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *);
84 static int dissect_ProtocolExtensionFieldExtensionValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *);
85 static int dissect_InitiatingMessageValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *);
86 static int dissect_SuccessfulOutcomeValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *);
87 static int dissect_UnsuccessfulOutcomeValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *);
88 void proto_reg_handoff_x2ap(void);
89
90 #include "packet-x2ap-fn.c"
91
92 static int dissect_ProtocolIEFieldValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
93 {
94   return (dissector_try_uint(x2ap_ies_dissector_table, ProtocolIE_ID, tvb, pinfo, tree)) ? tvb_length(tvb) : 0;
95 }
96
97 static int dissect_ProtocolExtensionFieldExtensionValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
98 {
99   return (dissector_try_uint(x2ap_extension_dissector_table, ProtocolIE_ID, tvb, pinfo, tree)) ? tvb_length(tvb) : 0;
100 }
101
102 static int dissect_InitiatingMessageValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
103 {
104   return (dissector_try_uint(x2ap_proc_imsg_dissector_table, ProcedureCode, tvb, pinfo, tree)) ? tvb_length(tvb) : 0;
105 }
106
107 static int dissect_SuccessfulOutcomeValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
108 {
109   return (dissector_try_uint(x2ap_proc_sout_dissector_table, ProcedureCode, tvb, pinfo, tree)) ? tvb_length(tvb) : 0;
110 }
111
112 static int dissect_UnsuccessfulOutcomeValue(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
113 {
114   return (dissector_try_uint(x2ap_proc_uout_dissector_table, ProcedureCode, tvb, pinfo, tree)) ? tvb_length(tvb) : 0;
115 }
116
117 static void
118 dissect_x2ap(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
119 {
120         proto_item      *x2ap_item = NULL;
121         proto_tree      *x2ap_tree = NULL;
122
123         /* make entry in the Protocol column on summary display */
124         col_set_str(pinfo->cinfo, COL_PROTOCOL, "X2AP");
125
126         /* create the x2ap protocol tree */
127         x2ap_item = proto_tree_add_item(tree, proto_x2ap, tvb, 0, -1, ENC_NA);
128         x2ap_tree = proto_item_add_subtree(x2ap_item, ett_x2ap);
129
130         dissect_X2AP_PDU_PDU(tvb, pinfo, x2ap_tree);
131 }
132
133 /*--- proto_register_x2ap -------------------------------------------*/
134 void proto_register_x2ap(void) {
135
136   /* List of fields */
137
138   static hf_register_info hf[] = {
139     { &hf_x2ap_transportLayerAddressIPv4,
140       { "transportLayerAddress(IPv4)", "x2ap.transportLayerAddressIPv4",
141         FT_IPv4, BASE_NONE, NULL, 0,
142         NULL, HFILL }},
143     { &hf_x2ap_transportLayerAddressIPv6,
144       { "transportLayerAddress(IPv6)", "x2ap.transportLayerAddressIPv6",
145         FT_IPv6, BASE_NONE, NULL, 0,
146         NULL, HFILL }},
147
148 #include "packet-x2ap-hfarr.c"
149   };
150
151   /* List of subtrees */
152   static gint *ett[] = {
153                   &ett_x2ap,
154                   &ett_x2ap_TransportLayerAddress,
155 #include "packet-x2ap-ettarr.c"
156   };
157
158   module_t *x2ap_module;
159
160   /* Register protocol */
161   proto_x2ap = proto_register_protocol(PNAME, PSNAME, PFNAME);
162   /* Register fields and subtrees */
163   proto_register_field_array(proto_x2ap, hf, array_length(hf));
164   proto_register_subtree_array(ett, array_length(ett));
165
166   /* Register dissector */
167   register_dissector("x2ap", dissect_x2ap, proto_x2ap);
168
169   /* Register dissector tables */
170   x2ap_ies_dissector_table = register_dissector_table("x2ap.ies", "X2AP-PROTOCOL-IES", FT_UINT32, BASE_DEC);
171   x2ap_extension_dissector_table = register_dissector_table("x2ap.extension", "X2AP-PROTOCOL-EXTENSION", FT_UINT32, BASE_DEC);
172   x2ap_proc_imsg_dissector_table = register_dissector_table("x2ap.proc.imsg", "X2AP-ELEMENTARY-PROCEDURE InitiatingMessage", FT_UINT32, BASE_DEC);
173   x2ap_proc_sout_dissector_table = register_dissector_table("x2ap.proc.sout", "X2AP-ELEMENTARY-PROCEDURE SuccessfulOutcome", FT_UINT32, BASE_DEC);
174   x2ap_proc_uout_dissector_table = register_dissector_table("x2ap.proc.uout", "X2AP-ELEMENTARY-PROCEDURE UnsuccessfulOutcome", FT_UINT32, BASE_DEC);
175
176   /* Register configuration options for ports */
177   x2ap_module = prefs_register_protocol(proto_x2ap, proto_reg_handoff_x2ap);
178
179   prefs_register_uint_preference(x2ap_module, "sctp.port",
180                                  "X2AP SCTP Port",
181                                  "Set the SCTP port for X2AP messages",
182                                  10,
183                                  &gbl_x2apSctpPort);
184
185 }
186
187
188 /*--- proto_reg_handoff_x2ap ---------------------------------------*/
189 void
190 proto_reg_handoff_x2ap(void)
191 {
192         dissector_handle_t x2ap_handle;
193         static gboolean Initialized=FALSE;
194         static guint SctpPort;
195
196         x2ap_handle = find_dissector("x2ap");
197         if (!Initialized) {
198                 dissector_add_handle("sctp.port", x2ap_handle);  /* for "decode-as" */
199                 dissector_add_uint("sctp.ppi", X2AP_PAYLOAD_PROTOCOL_ID, x2ap_handle);
200                 Initialized=TRUE;
201 #include "packet-x2ap-dis-tab.c"
202         } else {
203                 if (SctpPort != 0) {
204                         dissector_delete_uint("sctp.port", SctpPort, x2ap_handle);
205                 }
206         }
207
208         SctpPort=gbl_x2apSctpPort;
209         if (SctpPort != 0) {
210                 dissector_add_uint("sctp.port", SctpPort, x2ap_handle);
211         }
212
213 }
214
215