name change
[obnox/wireshark/wip.git] / asn1 / ros / packet-ros-template.c
1 /* packet-ros_asn1.c
2  * Routines for ROS 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/emem.h>
34
35 #include <stdio.h>
36 #include <string.h>
37
38 #include "packet-ber.h"
39 #include "packet-pres.h"
40 #include "packet-ros.h"
41
42 #define PNAME  "X.880 OSI Remote Operations Service"
43 #define PSNAME "ROS"
44 #define PFNAME "ros"
45
46 /* Initialize the protocol and registered fields */
47 int proto_ros = -1;
48
49 static struct SESSION_DATA_STRUCTURE* session = NULL;
50
51 static proto_tree *top_tree=NULL;
52 static guint32 opcode;
53 static guint32 invokeid;
54
55 static  dissector_handle_t ros_handle = NULL;
56
57 typedef struct ros_conv_info_t {
58   struct ros_conv_info_t *next;
59   GHashTable *unmatched; /* unmatched operations */
60   GHashTable *matched;   /* matched operations */
61 } ros_conv_info_t;
62
63 static ros_conv_info_t *ros_info_items = NULL;
64
65 typedef struct ros_call_response {
66   gboolean is_request;
67   guint32 req_frame;
68   nstime_t req_time;
69   guint32 rep_frame;
70   guint invokeId;
71 } ros_call_response_t;
72
73 static int hf_ros_response_in = -1;
74 static int hf_ros_response_to = -1;
75 static int hf_ros_time = -1;
76
77
78 #include "packet-ros-hf.c"
79
80 /* Initialize the subtree pointers */
81 static gint ett_ros = -1;
82 #include "packet-ros-ett.c"
83
84 static dissector_table_t ros_oid_dissector_table=NULL;
85 static GHashTable *oid_table=NULL;
86 static gint ett_ros_unknown = -1;
87
88 void
89 register_ros_oid_dissector_handle(const char *oid, dissector_handle_t dissector, int proto _U_, const char *name, gboolean uses_rtse)
90 {
91         dissector_add_string("ros.oid", oid, dissector);
92         g_hash_table_insert(oid_table, (gpointer)oid, (gpointer)name);
93
94         if(!uses_rtse)
95           /* if we are not using RTSE, then we must register ROS with BER (ACSE) */
96           register_ber_oid_dissector_handle(oid, ros_handle, proto, name);
97 }
98
99 static int
100 call_ros_oid_callback(const char *oid, tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree)
101 {
102         tvbuff_t *next_tvb;
103
104         next_tvb = tvb_new_subset(tvb, offset, tvb_length_remaining(tvb, offset), tvb_reported_length_remaining(tvb, offset));
105         if(!dissector_try_string(ros_oid_dissector_table, oid, next_tvb, pinfo, tree)){
106                 proto_item *item=NULL;
107                 proto_tree *next_tree=NULL;
108
109                 item=proto_tree_add_text(tree, next_tvb, 0, tvb_length_remaining(tvb, offset), "ROS: Dissector for OID:%s not implemented. Contact Ethereal developers if you want this supported", oid);
110                 if(item){
111                         next_tree=proto_item_add_subtree(item, ett_ros_unknown);
112                 }
113                 dissect_unknown_ber(pinfo, next_tvb, offset, next_tree);
114         }
115
116         /*XXX until we change the #.REGISTER signature for _PDU()s 
117          * into new_dissector_t   we have to do this kludge with
118          * manually step past the content in the ANY type.
119          */
120         offset+=tvb_length_remaining(tvb, offset);
121
122         return offset;
123 }
124
125
126 static guint
127 ros_info_hash_matched(gconstpointer k)
128 {
129   const ros_call_response_t *key = k;
130
131   return key->invokeId;
132 }
133
134 static gint
135 ros_info_equal_matched(gconstpointer k1, gconstpointer k2)
136 {
137   const ros_call_response_t *key1 = k1;
138   const ros_call_response_t *key2 = k2;
139
140   if( key1->req_frame && key2->req_frame && (key1->req_frame!=key2->req_frame) ){
141     return 0;
142   }
143   /* a response may span multiple frames
144   if( key1->rep_frame && key2->rep_frame && (key1->rep_frame!=key2->rep_frame) ){
145     return 0;
146   }
147   */
148
149   return key1->invokeId==key2->invokeId;
150 }
151
152 static guint
153 ros_info_hash_unmatched(gconstpointer k)
154 {
155   const ros_call_response_t *key = k;
156
157   return key->invokeId;
158 }
159
160 static gint
161 ros_info_equal_unmatched(gconstpointer k1, gconstpointer k2)
162 {
163   const ros_call_response_t *key1 = k1;
164   const ros_call_response_t *key2 = k2;
165
166   return key1->invokeId==key2->invokeId;
167 }
168
169 static ros_call_response_t *
170 ros_match_call_response(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint invokeId, gboolean isInvoke)
171 {
172   ros_call_response_t rcr, *rcrp=NULL;
173   ros_conv_info_t *ros_info = ros_info_items;
174
175   /* first see if we have already matched this */
176
177   rcr.invokeId=invokeId;
178   rcr.is_request = isInvoke;
179
180   if(isInvoke) {
181     rcr.req_frame=pinfo->fd->num;
182     rcr.rep_frame=0;
183   } else {
184     rcr.req_frame=0;
185     rcr.rep_frame=pinfo->fd->num;
186   }
187   
188   rcrp=g_hash_table_lookup(ros_info->matched, &rcr);
189
190   if(rcrp) {
191     /* we have found a match */
192     rcrp->is_request=rcr.is_request;
193
194   } else {
195     
196     /* we haven't found a match - try and match it up */
197
198     if(isInvoke) {
199       /* this a a request - add it to the unmatched list */
200
201       /* check that we dont already have one of those in the
202          unmatched list and if so remove it */
203
204       rcr.invokeId=invokeId;
205
206       rcrp=g_hash_table_lookup(ros_info->unmatched, &rcr);
207
208       if(rcrp){
209         g_hash_table_remove(ros_info->unmatched, rcrp);
210       }
211       
212       /* if we cant reuse the old one, grab a new chunk */
213       if(!rcrp){
214         rcrp=se_alloc(sizeof(ros_call_response_t));
215       }
216       rcrp->invokeId=invokeId;
217       rcrp->req_frame=pinfo->fd->num;
218       rcrp->req_time=pinfo->fd->abs_ts;
219       rcrp->rep_frame=0;
220       rcrp->is_request=TRUE;
221       g_hash_table_insert(ros_info->unmatched, rcrp, rcrp);
222       return NULL;
223
224     } else {
225
226       /* this is a result - it should be in our unmatched list */
227
228       rcr.invokeId=invokeId;
229       rcrp=g_hash_table_lookup(ros_info->unmatched, &rcr);
230
231       if(rcrp){
232
233         if(!rcrp->rep_frame){
234           g_hash_table_remove(ros_info->unmatched, rcrp);
235           rcrp->rep_frame=pinfo->fd->num;
236           rcrp->is_request=FALSE;
237           g_hash_table_insert(ros_info->matched, rcrp, rcrp);
238         }
239       }
240     }
241   }
242
243   if(rcrp){ /* we have found a match */
244
245     if(rcrp->is_request){
246       proto_tree_add_uint(tree, hf_ros_response_in, tvb, 0, 0, rcrp->rep_frame);
247     } else {
248       nstime_t ns;
249       proto_tree_add_uint(tree, hf_ros_response_to, tvb, 0, 0, rcrp->req_frame);
250       nstime_delta(&ns, &pinfo->fd->abs_ts, &rcrp->req_time);
251       proto_tree_add_time(tree, hf_ros_time, tvb, 0, 0, &ns);
252     }
253   }
254   
255   return rcrp;
256 }
257
258 #include "packet-ros-fn.c"
259
260 /*
261 * Dissect ROS PDUs inside a PPDU.
262 */
263 static void
264 dissect_ros(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree)
265 {
266         int offset = 0;
267         int old_offset;
268         proto_item *item=NULL;
269         proto_tree *tree=NULL;
270         conversation_t *conversation;
271         ros_conv_info_t *ros_info = NULL;
272
273         /* save parent_tree so subdissectors can create new top nodes */
274         top_tree=parent_tree;
275
276         /* do we have application context from the acse dissector?  */
277         if( !pinfo->private_data ){
278                 if(parent_tree){
279                         proto_tree_add_text(parent_tree, tvb, offset, -1,
280                                 "Internal error:can't get application context from ACSE dissector.");
281                 } 
282                 return  ;
283         } else {
284                 session  = ( (struct SESSION_DATA_STRUCTURE*)(pinfo->private_data) );
285
286         }
287
288         /*
289          * Do we have a conversation for this connection?
290          */
291         conversation = find_conversation(pinfo->fd->num, &pinfo->src, &pinfo->dst,
292                                          pinfo->ptype, pinfo->srcport,
293                                          pinfo->destport, 0);
294         if (conversation == NULL) {
295           /* We don't yet have a conversation, so create one. */
296           conversation = conversation_new(pinfo->fd->num, &pinfo->src, &pinfo->dst,
297                                           pinfo->ptype, pinfo->srcport,
298                                           pinfo->destport, 0);
299
300         }
301
302         /*
303          * Do we already have our info
304          */
305         ros_info = conversation_get_proto_data(conversation, proto_ros);
306         if (ros_info == NULL) {
307
308           /* No.  Attach that information to the conversation. */
309
310           ros_info = se_alloc(sizeof(ros_conv_info_t));
311           ros_info->matched=g_hash_table_new(ros_info_hash_matched, ros_info_equal_matched);
312           ros_info->unmatched=g_hash_table_new(ros_info_hash_unmatched, ros_info_equal_unmatched);
313           
314           conversation_add_proto_data(conversation, proto_ros, ros_info);
315           
316           ros_info->next = ros_info_items;
317           ros_info_items = ros_info;
318           }
319
320         /* pinfo->private_data = ros_info; */
321
322         if(parent_tree){
323                 item = proto_tree_add_item(parent_tree, proto_ros, tvb, 0, -1, FALSE);
324                 tree = proto_item_add_subtree(item, ett_ros);
325         }
326         if (check_col(pinfo->cinfo, COL_PROTOCOL))
327                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "ROS");
328         if (check_col(pinfo->cinfo, COL_INFO))
329                 col_clear(pinfo->cinfo, COL_INFO);
330
331         while (tvb_reported_length_remaining(tvb, offset) > 0){
332                 old_offset=offset;
333                 offset=dissect_ros_ROS(FALSE, tvb, offset, pinfo , tree, -1);
334                 if(offset == old_offset){
335                         proto_tree_add_text(tree, tvb, offset, -1,"Internal error, zero-byte ROS PDU");
336                         offset = tvb_length(tvb);
337                         break;
338                 }
339         }
340 }
341
342 static void
343 ros_reinit(void)
344 {
345   ros_conv_info_t *ros_info;
346
347   /* Free up state attached to the ros_info structures */
348   for (ros_info = ros_info_items; ros_info != NULL; ros_info = ros_info->next) {
349     g_hash_table_destroy(ros_info->matched);
350     ros_info->matched=NULL;
351     g_hash_table_destroy(ros_info->unmatched);
352     ros_info->unmatched=NULL;
353   }
354
355   ros_info_items = NULL;
356
357 }
358
359 /*--- proto_register_ros -------------------------------------------*/
360 void proto_register_ros(void) {
361
362   /* List of fields */
363   static hf_register_info hf[] =
364   {
365     { &hf_ros_response_in,
366       { "Response In", "ros.response_in",
367         FT_FRAMENUM, BASE_DEC, NULL, 0x0,
368         "The response to this remote operation invocation is in this frame", HFILL }},
369     { &hf_ros_response_to,
370       { "Response To", "ros.response_to",
371         FT_FRAMENUM, BASE_DEC, NULL, 0x0,
372         "This is a response to the remote operation invocation in this frame", HFILL }},
373     { &hf_ros_time,
374       { "Time", "ros.time",
375         FT_RELATIVE_TIME, BASE_NONE, NULL, 0x0,
376         "The time between the Invoke and the Response", HFILL }},
377
378 #include "packet-ros-hfarr.c"
379   };
380
381   /* List of subtrees */
382   static gint *ett[] = {
383     &ett_ros,
384     &ett_ros_unknown,
385 #include "packet-ros-ettarr.c"
386   };
387
388   /* Register protocol */
389   proto_ros = proto_register_protocol(PNAME, PSNAME, PFNAME);
390   register_dissector("ros", dissect_ros, proto_ros);
391   /* Register fields and subtrees */
392   proto_register_field_array(proto_ros, hf, array_length(hf));
393   proto_register_subtree_array(ett, array_length(ett));
394
395   ros_oid_dissector_table = register_dissector_table("ros.oid", "ROS OID Dissectors", FT_STRING, BASE_NONE);
396   oid_table=g_hash_table_new(g_str_hash, g_str_equal);
397
398   ros_handle = find_dissector("ros");
399
400   register_init_routine(ros_reinit);
401 }
402
403
404 /*--- proto_reg_handoff_ros --- */
405 void proto_reg_handoff_ros(void) {
406
407
408 }