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