smb2-dissector: learn the "REPLAY_OPERATION" flag
[obnox/wireshark/wip.git] / asn1 / rtse / packet-rtse-template.c
1 /* packet-rtse_asn1.c
2  * Routines for RTSE packet dissection
3  * Graeme Lunt 2005
4  *
5  * $Id$
6  *
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  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <glib.h>
31 #include <epan/packet.h>
32 #include <epan/conversation.h>
33 #include <epan/prefs.h>
34 #include <epan/reassemble.h>
35 #include <epan/asn1.h>
36 #include <epan/expert.h>
37
38 #include "packet-ber.h"
39 #include "packet-pres.h"
40 #include "packet-acse.h"
41 #include "packet-ros.h"
42 #include "packet-rtse.h"
43
44 #define PNAME  "X.228 OSI Reliable Transfer Service"
45 #define PSNAME "RTSE"
46 #define PFNAME "rtse"
47
48 /* Initialize the protocol and registered fields */
49 static int proto_rtse = -1;
50
51 static struct SESSION_DATA_STRUCTURE* session = NULL;
52
53 static gboolean open_request=FALSE;
54 static guint32 app_proto=0;
55
56 static proto_tree *top_tree=NULL;
57
58 /* Preferences */
59 static gboolean rtse_reassemble = TRUE;
60
61 #include "packet-rtse-hf.c"
62
63 /* Initialize the subtree pointers */
64 static gint ett_rtse = -1;
65 #include "packet-rtse-ett.c"
66
67
68 static dissector_table_t rtse_oid_dissector_table=NULL;
69 static GHashTable *oid_table=NULL;
70 static gint ett_rtse_unknown = -1;
71
72 static GHashTable *rtse_segment_table = NULL;
73 static GHashTable *rtse_reassembled_table = NULL;
74
75 static int hf_rtse_segment_data = -1;
76 static int hf_rtse_fragments = -1;
77 static int hf_rtse_fragment = -1;
78 static int hf_rtse_fragment_overlap = -1;
79 static int hf_rtse_fragment_overlap_conflicts = -1;
80 static int hf_rtse_fragment_multiple_tails = -1;
81 static int hf_rtse_fragment_too_long_fragment = -1;
82 static int hf_rtse_fragment_error = -1;
83 static int hf_rtse_fragment_count = -1;
84 static int hf_rtse_reassembled_in = -1;
85 static int hf_rtse_reassembled_length = -1;
86
87 static gint ett_rtse_fragment = -1;
88 static gint ett_rtse_fragments = -1;
89
90 static const fragment_items rtse_frag_items = {
91         /* Fragment subtrees */
92         &ett_rtse_fragment,
93         &ett_rtse_fragments,
94         /* Fragment fields */
95         &hf_rtse_fragments,
96         &hf_rtse_fragment,
97         &hf_rtse_fragment_overlap,
98         &hf_rtse_fragment_overlap_conflicts,
99         &hf_rtse_fragment_multiple_tails,
100         &hf_rtse_fragment_too_long_fragment,
101         &hf_rtse_fragment_error,
102         &hf_rtse_fragment_count,
103         /* Reassembled in field */
104         &hf_rtse_reassembled_in,
105         /* Reassembled length field */
106         &hf_rtse_reassembled_length,
107         /* Tag */
108         "RTSE fragments"
109 };
110
111 void
112 register_rtse_oid_dissector_handle(const char *oid, dissector_handle_t dissector, int proto, const char *name, gboolean uses_ros)
113 {
114 /* XXX: Note that this fcn is called from proto_reg_handoff in *other* dissectors ... */
115
116   static  dissector_handle_t rtse_handle = NULL;
117   static  dissector_handle_t ros_handle = NULL;
118
119   if (rtse_handle == NULL)
120     rtse_handle = find_dissector("rtse");
121   if (ros_handle == NULL)
122     ros_handle = find_dissector("ros");
123
124   /* save the name - but not used */
125   g_hash_table_insert(oid_table, (gpointer)oid, (gpointer)name);
126
127   /* register RTSE with the BER (ACSE) */
128   register_ber_oid_dissector_handle(oid, rtse_handle, proto, name);
129
130   if(uses_ros) {
131     /* make sure we call ROS ... */
132     dissector_add_string("rtse.oid", oid, ros_handle);
133
134     /* and then tell ROS how to dissect the AS*/
135     register_ros_oid_dissector_handle(oid, dissector, proto, name, TRUE);
136
137   } else {
138     /* otherwise we just remember how to dissect the AS */
139     dissector_add_string("rtse.oid", oid, dissector);
140   }
141 }
142
143 static int
144 call_rtse_oid_callback(const char *oid, tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree)
145 {
146         tvbuff_t *next_tvb;
147
148         next_tvb = tvb_new_subset(tvb, offset, tvb_length_remaining(tvb, offset), tvb_reported_length_remaining(tvb, offset));
149         if(!dissector_try_string(rtse_oid_dissector_table, oid, next_tvb, pinfo, tree)){
150                 proto_item *item=proto_tree_add_text(tree, next_tvb, 0, tvb_length_remaining(tvb, offset), "RTSE: Dissector for OID:%s not implemented. Contact Wireshark developers if you want this supported", oid);
151                 proto_tree *next_tree=proto_item_add_subtree(item, ett_rtse_unknown);
152
153                 expert_add_info_format (pinfo, item, PI_UNDECODED, PI_WARN,
154                                         "RTSE: Dissector for OID %s not implemented", oid);
155                 dissect_unknown_ber(pinfo, next_tvb, offset, next_tree);
156         }
157
158         /*XXX until we change the #.REGISTER signature for _PDU()s 
159          * into new_dissector_t   we have to do this kludge with
160          * manually step past the content in the ANY type.
161          */
162         offset+=tvb_length_remaining(tvb, offset);
163
164         return offset;
165 }
166
167 static int
168 call_rtse_external_type_callback(gboolean implicit_tag _U_, tvbuff_t *tvb, int offset, asn1_ctx_t *actx, proto_tree *tree, int hf_index _U_)
169 {
170         const char      *oid = NULL;
171
172         if (actx->external.indirect_ref_present) {
173                 oid = (const char *)find_oid_by_pres_ctx_id(actx->pinfo, actx->external.indirect_reference);
174         } else if (actx->external.direct_ref_present) {
175                 oid = actx->external.direct_reference;
176         }
177
178         if (oid)
179                 offset = call_rtse_oid_callback(oid, tvb, offset, actx->pinfo, top_tree ? top_tree : tree);
180
181         return offset;
182 }
183
184 #include "packet-rtse-fn.c"
185
186 /*
187 * Dissect RTSE PDUs inside a PPDU.
188 */
189 static void
190 dissect_rtse(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree)
191 {
192         int offset = 0;
193         int old_offset;
194         proto_item *item=NULL;
195         proto_tree *tree=NULL;
196         proto_tree *next_tree=NULL;
197         tvbuff_t *next_tvb = NULL;
198         tvbuff_t *data_tvb = NULL;
199         fragment_data *frag_msg = NULL;
200         guint32 fragment_length;
201         guint32 rtse_id = 0;
202         gboolean data_handled = FALSE;
203         conversation_t *conversation = NULL;
204         asn1_ctx_t asn1_ctx;
205         asn1_ctx_init(&asn1_ctx, ASN1_ENC_BER, TRUE, pinfo);
206
207         /* save parent_tree so subdissectors can create new top nodes */
208         top_tree=parent_tree;
209
210         /* do we have application context from the acse dissector?  */
211         if( !pinfo->private_data ){
212                 if(parent_tree){
213                         proto_tree_add_text(parent_tree, tvb, offset, -1,
214                                 "Internal error:can't get application context from ACSE dissector.");
215                 } 
216                 return  ;
217         } else {
218                 session  = ( (struct SESSION_DATA_STRUCTURE*)(pinfo->private_data) );
219
220         }
221
222         col_set_str(pinfo->cinfo, COL_PROTOCOL, "RTSE");
223         col_clear(pinfo->cinfo, COL_INFO);
224
225         if (rtse_reassemble && 
226             ((session->spdu_type == SES_DATA_TRANSFER) ||
227              (session->spdu_type == SES_MAJOR_SYNC_POINT))) {
228                 /* Use conversation index as fragment id */
229                 conversation  = find_conversation (pinfo->fd->num, 
230                                                    &pinfo->src, &pinfo->dst, pinfo->ptype, 
231                                                    pinfo->srcport, pinfo->destport, 0);
232                 if (conversation != NULL) { 
233                         rtse_id = conversation->index;
234                 } 
235                 session->rtse_reassemble = TRUE;
236         }
237         if (rtse_reassemble && session->spdu_type == SES_MAJOR_SYNC_POINT) {
238                 frag_msg = fragment_end_seq_next (pinfo, rtse_id, rtse_segment_table,
239                                                   rtse_reassembled_table);
240                 next_tvb = process_reassembled_data (tvb, offset, pinfo, "Reassembled RTSE", 
241                                                      frag_msg, &rtse_frag_items, NULL, parent_tree);
242         }
243         if(parent_tree){
244                 item = proto_tree_add_item(parent_tree, proto_rtse, next_tvb ? next_tvb : tvb, 0, -1, ENC_NA);
245                 tree = proto_item_add_subtree(item, ett_rtse);
246         }
247         if (rtse_reassemble && session->spdu_type == SES_DATA_TRANSFER) {
248                 /* strip off the OCTET STRING encoding - including any CONSTRUCTED OCTET STRING */
249                 dissect_ber_octet_string(FALSE, &asn1_ctx, tree, tvb, offset, hf_rtse_segment_data, &data_tvb);
250
251                 if (data_tvb) {
252                         fragment_length = tvb_length_remaining (data_tvb, 0);
253                         proto_item_append_text(asn1_ctx.created_item, " (%u byte%s)", fragment_length,
254                                       plurality(fragment_length, "", "s"));
255                         frag_msg = fragment_add_seq_next (data_tvb, 0, pinfo, 
256                                                           rtse_id, rtse_segment_table,
257                                                           rtse_reassembled_table, fragment_length, TRUE);
258                         if (frag_msg && pinfo->fd->num != frag_msg->reassembled_in) {
259                                 /* Add a "Reassembled in" link if not reassembled in this frame */
260                                 proto_tree_add_uint (tree, *(rtse_frag_items.hf_reassembled_in),
261                                                      data_tvb, 0, 0, frag_msg->reassembled_in);
262                         }
263                         pinfo->fragmented = TRUE;
264                         data_handled = TRUE;
265                 } else {
266                         fragment_length = tvb_length_remaining (tvb, offset);
267                 }
268
269                 if (check_col(pinfo->cinfo, COL_INFO))
270                         col_append_fstr(pinfo->cinfo, COL_INFO, "[RTSE fragment, %u byte%s]",
271                                         fragment_length, plurality(fragment_length, "", "s"));
272         } else if (rtse_reassemble && session->spdu_type == SES_MAJOR_SYNC_POINT) {
273                 if (next_tvb) {
274                         /* ROS won't do this for us */
275                         session->ros_op = (ROS_OP_INVOKE | ROS_OP_ARGUMENT);
276                         offset=dissect_ber_external_type(FALSE, tree, next_tvb, 0, &asn1_ctx, -1, call_rtse_external_type_callback);
277                 } else {
278                         offset = tvb_length (tvb);
279                 }
280                 pinfo->fragmented = FALSE;
281                 data_handled = TRUE;
282         } 
283
284         if (!data_handled) {
285                 while (tvb_reported_length_remaining(tvb, offset) > 0){
286                         old_offset=offset;
287                         offset=dissect_rtse_RTSE_apdus(TRUE, tvb, offset, &asn1_ctx, tree, -1);
288                         if(offset == old_offset){
289                                 item = proto_tree_add_text(tree, tvb, offset, -1, "Unknown RTSE PDU");
290
291                                 if(item){
292                                         expert_add_info_format (pinfo, item, PI_UNDECODED, PI_WARN, "Unknown RTSE PDU");
293                                         next_tree=proto_item_add_subtree(item, ett_rtse_unknown);
294                                         dissect_unknown_ber(pinfo, tvb, offset, next_tree);
295                                 }
296
297                                 break;
298                         }
299                 }
300         }
301
302         top_tree = NULL;
303 }
304
305 static void rtse_reassemble_init (void)
306 {
307         fragment_table_init (&rtse_segment_table);
308         reassembled_table_init (&rtse_reassembled_table);
309 }
310
311 /*--- proto_register_rtse -------------------------------------------*/
312 void proto_register_rtse(void) {
313
314   /* List of fields */
315   static hf_register_info hf[] =
316   {
317     /* Fragment entries */
318     { &hf_rtse_segment_data,
319       { "RTSE segment data", "rtse.segment", FT_NONE, BASE_NONE,
320         NULL, 0x00, NULL, HFILL } },
321     { &hf_rtse_fragments,
322       { "RTSE fragments", "rtse.fragments", FT_NONE, BASE_NONE,
323         NULL, 0x00, NULL, HFILL } },
324     { &hf_rtse_fragment,
325       { "RTSE fragment", "rtse.fragment", FT_FRAMENUM, BASE_NONE,
326         NULL, 0x00, NULL, HFILL } },
327     { &hf_rtse_fragment_overlap,
328       { "RTSE fragment overlap", "rtse.fragment.overlap", FT_BOOLEAN,
329         BASE_NONE, NULL, 0x0, NULL, HFILL } },
330     { &hf_rtse_fragment_overlap_conflicts,
331       { "RTSE fragment overlapping with conflicting data",
332         "rtse.fragment.overlap.conflicts", FT_BOOLEAN, BASE_NONE,
333         NULL, 0x0, NULL, HFILL } },
334     { &hf_rtse_fragment_multiple_tails,
335       { "RTSE has multiple tail fragments",
336         "rtse.fragment.multiple_tails", FT_BOOLEAN, BASE_NONE,
337         NULL, 0x0, NULL, HFILL } },
338     { &hf_rtse_fragment_too_long_fragment,
339       { "RTSE fragment too long", "rtse.fragment.too_long_fragment",
340         FT_BOOLEAN, BASE_NONE, NULL, 0x0, NULL, HFILL } },
341     { &hf_rtse_fragment_error,
342       { "RTSE defragmentation error", "rtse.fragment.error", FT_FRAMENUM,
343         BASE_NONE, NULL, 0x00, NULL, HFILL } },
344     { &hf_rtse_fragment_count,
345       { "RTSE fragment count", "rtse.fragment.count", FT_UINT32, BASE_DEC,
346         NULL, 0x00, NULL, HFILL } },
347     { &hf_rtse_reassembled_in,
348       { "Reassembled RTSE in frame", "rtse.reassembled.in", FT_FRAMENUM, BASE_NONE,
349         NULL, 0x00, "This RTSE packet is reassembled in this frame", HFILL } },
350     { &hf_rtse_reassembled_length,
351       { "Reassembled RTSE length", "rtse.reassembled.length", FT_UINT32, BASE_DEC,
352         NULL, 0x00, "The total length of the reassembled payload", HFILL } },
353
354 #include "packet-rtse-hfarr.c"
355   };
356
357   /* List of subtrees */
358   static gint *ett[] = {
359     &ett_rtse,
360     &ett_rtse_unknown,
361     &ett_rtse_fragment,
362     &ett_rtse_fragments,
363 #include "packet-rtse-ettarr.c"
364   };
365
366   module_t *rtse_module;
367
368   /* Register protocol */
369   proto_rtse = proto_register_protocol(PNAME, PSNAME, PFNAME);
370   register_dissector("rtse", dissect_rtse, proto_rtse);
371   /* Register fields and subtrees */
372   proto_register_field_array(proto_rtse, hf, array_length(hf));
373   proto_register_subtree_array(ett, array_length(ett));
374   register_init_routine (&rtse_reassemble_init);
375   rtse_module = prefs_register_protocol_subtree("OSI", proto_rtse, NULL);
376
377   prefs_register_bool_preference(rtse_module, "reassemble",
378                                  "Reassemble segmented RTSE datagrams",
379                                  "Whether segmented RTSE datagrams should be reassembled."
380                                  " To use this option, you must also enable"
381                                  " \"Allow subdissectors to reassemble TCP streams\""
382                                  " in the TCP protocol settings.", &rtse_reassemble);
383
384   rtse_oid_dissector_table = register_dissector_table("rtse.oid", "RTSE OID Dissectors", FT_STRING, BASE_NONE);
385   oid_table=g_hash_table_new(g_str_hash, g_str_equal);
386
387
388 }
389
390
391 /*--- proto_reg_handoff_rtse --- */
392 void proto_reg_handoff_rtse(void) {
393
394
395 }