Ethereal->Wireshark
[obnox/wireshark/wip.git] / plugins / mate / packet-mate.c
1 /* packet-mate.c
2  * Routines for the mate Facility's Pseudo-Protocol dissection
3  *
4  * Copyright 2004, Luis E. Garcia Ontanon <gopo@webflies.org>
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
27
28 /**************************************************************************
29  * This is the pseudo protocol dissector for the mate module.          ***
30  * It is intended for this to be just the user interface to the module. ***
31  **************************************************************************/
32
33 #include "mate.h"
34
35 static int mate_tap_data = 0;
36 static mate_config* mc = NULL;
37
38 static int proto_mate = -1;
39
40 static const gchar* pref_mate_config_filename = "";
41 static const gchar* current_mate_config_filename = NULL;
42
43 static proto_item *mate_i = NULL;
44
45 static void pdu_attrs_tree(proto_tree* tree, tvbuff_t *tvb, mate_pdu* pdu) {
46         AVPN* c;
47         proto_item *avpl_i;
48         proto_tree *avpl_t;
49         int* hfi_p;
50         
51         avpl_i = proto_tree_add_text(tree,tvb,0,0,"%s Attributes",pdu->cfg->name);
52         avpl_t = proto_item_add_subtree(avpl_i, pdu->cfg->ett_attr);
53         
54         for ( c = pdu->avpl->null.next; c->avp; c = c->next) {
55                 hfi_p = g_hash_table_lookup(pdu->cfg->my_hfids,(char*)c->avp->n);
56                 
57                 if (hfi_p) {
58                         proto_tree_add_string(avpl_t,*hfi_p,tvb,0,0,c->avp->v);
59                 } else {
60                         g_warning("MATE: error: undefined attribute: mate.%s.%s",pdu->cfg->name,c->avp->n);
61                         proto_tree_add_text(avpl_t,tvb,0,0,"Undefined attribute: %s=%s",c->avp->n, c->avp->v);
62                 }
63         }
64 }
65
66 static void gop_attrs_tree(proto_tree* tree, tvbuff_t *tvb, mate_gop* gop) {
67         AVPN* c;
68         proto_item *avpl_i;
69         proto_tree *avpl_t;
70         int* hfi_p;
71         
72         avpl_i = proto_tree_add_text(tree,tvb,0,0,"%s Attributes",gop->cfg->name);
73         avpl_t = proto_item_add_subtree(avpl_i, gop->cfg->ett_attr);
74         
75         for ( c = gop->avpl->null.next; c->avp; c = c->next) {
76                 hfi_p = g_hash_table_lookup(gop->cfg->my_hfids,(char*)c->avp->n);
77                 
78                 if (hfi_p) {
79                         proto_tree_add_string(avpl_t,*hfi_p,tvb,0,0,c->avp->v);
80                 } else {
81                         g_warning("MATE: error: undefined attribute: mate.%s.%s",gop->cfg->name,c->avp->n);
82                         proto_tree_add_text(avpl_t,tvb,0,0,"Undefined attribute: %s=%s",c->avp->n, c->avp->v);
83                 }
84         }
85 }
86
87 static void gog_attrs_tree(proto_tree* tree, tvbuff_t *tvb, mate_gog* gog) {
88         AVPN* c;
89         proto_item *avpl_i;
90         proto_tree *avpl_t;
91         int* hfi_p;
92         
93         avpl_i = proto_tree_add_text(tree,tvb,0,0,"%s Attributes",gog->cfg->name);
94         avpl_t = proto_item_add_subtree(avpl_i, gog->cfg->ett_attr);
95         
96         for ( c = gog->avpl->null.next; c->avp; c = c->next) {
97                 hfi_p = g_hash_table_lookup(gog->cfg->my_hfids,(char*)c->avp->n);
98                 
99                 if (hfi_p) {
100                         proto_tree_add_string(avpl_t,*hfi_p,tvb,0,0,c->avp->v);
101                 } else {
102                         g_warning("MATE: error: undefined attribute: mate.%s.%s",gog->cfg->name,c->avp->n);
103                         proto_tree_add_text(avpl_t,tvb,0,0,"Undefined attribute: %s=%s",c->avp->n, c->avp->v);
104                 }
105         }
106 }
107
108 static void mate_gop_tree(proto_tree* pdu_tree, tvbuff_t *tvb, mate_gop* gop);
109
110 static void mate_gog_tree(proto_tree* tree, tvbuff_t *tvb, mate_gog* gog, mate_gop* gop) {
111         proto_item *gog_item;
112         proto_tree *gog_tree;
113         proto_item *gog_time_item;
114         proto_tree *gog_time_tree;
115         proto_item *gog_gops_item;
116         proto_tree *gog_gops_tree;
117         mate_gop* gog_gops;
118         proto_item *gog_gop_item;
119         proto_tree *gog_gop_tree;
120         mate_pdu* pdu;
121         
122 #ifdef _MATE_DEBUGGING
123         proto_item* gog_key_item;
124         proto_tree* gog_key_tree;
125         guint i;
126 #endif
127         
128         gog_item = proto_tree_add_uint(tree,gog->cfg->hfid,tvb,0,0,gog->id);
129         gog_tree = proto_item_add_subtree(gog_item,gog->cfg->ett);
130                         
131         gog_attrs_tree(gog_tree,tvb,gog);
132         
133         if (gog->cfg->show_times) {
134                 gog_time_item = proto_tree_add_text(gog_tree,tvb,0,0,"%s Times",gog->cfg->name);
135                 gog_time_tree = proto_item_add_subtree(gog_time_item, gog->cfg->ett_times);
136                 
137                 proto_tree_add_float(gog_time_tree, gog->cfg->hfid_start_time, tvb, 0, 0, gog->start_time);
138                 proto_tree_add_float(gog_time_tree, gog->cfg->hfid_last_time, tvb, 0, 0, gog->last_time - gog->start_time); 
139         }
140         
141         gog_gops_item = proto_tree_add_uint(gog_tree, gog->cfg->hfid_gog_num_of_gops,
142                                                                            tvb, 0, 0, gog->num_of_gops);
143         
144         gog_gops_tree = proto_item_add_subtree(gog_gops_item, gog->cfg->ett_children);
145         
146         for (gog_gops = gog->gops; gog_gops; gog_gops = gog_gops->next) {
147                 
148                 if (gop != gog_gops) {
149                         if (gog->cfg->gop_tree_mode == GOP_FULL_TREE) {
150                                 mate_gop_tree(gog_gops_tree, tvb, gog_gops);
151                         } else {
152                                 gog_gop_item = proto_tree_add_uint(gog_gops_tree,gog_gops->cfg->hfid,tvb,0,0,gog_gops->id);
153                                 
154                                 if (gog->cfg->gop_tree_mode == GOP_BASIC_TREE) {
155                                         gog_gop_tree = proto_item_add_subtree(gog_gop_item, gog->cfg->ett_gog_gop);
156                                         
157                                         proto_tree_add_text(gog_gop_tree, tvb,0,0, "Started at: %f", gog_gops->start_time);
158                                         
159                                         
160                                         proto_tree_add_text(gog_gop_tree, tvb,0,0, "%s Duration: %f",
161                                                                                 gog_gops->cfg->name, gog_gops->last_time - gog_gops->start_time);
162                                         
163                                         if (gog_gops->released)
164                                                 proto_tree_add_text(gog_gop_tree, tvb,0,0, "%s has been released, Time: %f",
165                                                                                         gog_gops->cfg->name, gog_gops->release_time - gog_gops->start_time);
166                                         
167                                         proto_tree_add_text(gog_gop_tree, tvb,0,0, "Number of Pdus: %u",gog_gops->num_of_pdus);
168                                         
169                                         if (gop->pdus && gop->cfg->pdu_tree_mode != GOP_NO_TREE) {
170                                                 proto_tree_add_uint(gog_gop_tree,gog->cfg->hfid_gog_gopstart,tvb,0,0,gog_gops->pdus->frame);
171                                                 
172                                                 for (pdu = gog_gops->pdus->next ; pdu; pdu = pdu->next) {
173                                                         if (pdu->is_stop) {
174                                                                 proto_tree_add_uint(gog_gop_tree,gog->cfg->hfid_gog_gopstop,tvb,0,0,pdu->frame);
175                                                                 break;
176                                                         }
177                                                 }
178                                         }
179                                 }
180                                 
181                         }
182                 } else {
183                          proto_tree_add_uint_format(gog_gops_tree,gop->cfg->hfid,tvb,0,0,gop->id,"current %s Gop: %d",gop->cfg->name,gop->id);
184                 }
185         }
186 }
187
188 static void mate_gop_tree(proto_tree* tree, tvbuff_t *tvb, mate_gop* gop) {
189         proto_item *gop_item;
190         proto_tree *gop_time_tree;
191         proto_item *gop_time_item;
192         proto_tree *gop_tree;
193         proto_item *gop_pdu_item;
194         proto_tree *gop_pdu_tree;
195         mate_pdu* gop_pdus;
196         float  rel_time;
197         float  gop_time;
198         float pdu_rel_time;
199         const gchar* pdu_str;
200         const gchar* type_str;
201         guint32 pdu_item;
202         
203         gop_item = proto_tree_add_uint(tree,gop->cfg->hfid,tvb,0,0,gop->id);
204         gop_tree = proto_item_add_subtree(gop_item, gop->cfg->ett);
205         
206         if (gop->gop_key) proto_tree_add_text(gop_tree,tvb,0,0,"GOP Key: %s",gop->gop_key);
207         
208         gop_attrs_tree(gop_tree,tvb,gop);
209         
210         if (gop->cfg->show_times) {
211                 gop_time_item = proto_tree_add_text(gop_tree,tvb,0,0,"%s Times",gop->cfg->name);
212                 gop_time_tree = proto_item_add_subtree(gop_time_item, gop->cfg->ett_times);
213                 
214                 proto_tree_add_float(gop_time_tree, gop->cfg->hfid_start_time, tvb, 0, 0, gop->start_time);
215                 
216                 if (gop->released) { 
217                         proto_tree_add_float(gop_time_tree, gop->cfg->hfid_stop_time, tvb, 0, 0, gop->release_time - gop->start_time);
218                         proto_tree_add_float(gop_time_tree, gop->cfg->hfid_last_time, tvb, 0, 0, gop->last_time - gop->start_time); 
219                 } else {
220                         proto_tree_add_float(gop_time_tree, gop->cfg->hfid_last_time, tvb, 0, 0, gop->last_time - gop->start_time); 
221                 }
222         }
223         
224         gop_pdu_item = proto_tree_add_uint(gop_tree, gop->cfg->hfid_gop_num_pdus, tvb, 0, 0,gop->num_of_pdus);
225
226         if (gop->cfg->pdu_tree_mode != GOP_NO_TREE) {
227                 
228                 gop_pdu_tree = proto_item_add_subtree(gop_pdu_item, gop->cfg->ett_children);
229
230                 rel_time = gop_time = gop->start_time;
231
232                 type_str = (gop->cfg->pdu_tree_mode == GOP_FRAME_TREE ) ? "in frame:" : "id:";
233                 
234                 for (gop_pdus = gop->pdus; gop_pdus; gop_pdus = gop_pdus->next) {
235
236                         pdu_item = (gop->cfg->pdu_tree_mode == GOP_FRAME_TREE ) ? gop_pdus->frame : gop_pdus->id;
237
238                         if (gop_pdus->is_start) {
239                                 pdu_str = "Start ";
240                         } else if (gop_pdus->is_stop) {
241                                 pdu_str = "Stop ";
242                         } else if (gop_pdus->after_release) {
243                                 pdu_str = "After stop ";
244                         } else {
245                                 pdu_str = "";
246                         }
247                         
248                         pdu_rel_time = gop_pdus->time_in_gop != 0.0 ? gop_pdus->time_in_gop - rel_time : (float) 0.0;
249                         
250                         proto_tree_add_uint_format(gop_pdu_tree,gop->cfg->hfid_gop_pdu,tvb,0,0,pdu_item,
251                                                                            "%sPDU: %s %i (%f : %f)",pdu_str, type_str,
252                                                                            pdu_item, gop_pdus->time_in_gop,
253                                                                            pdu_rel_time);
254                         
255                         rel_time = gop_pdus->time_in_gop;
256                         
257                 }
258         }
259 }
260
261
262 static void mate_pdu_tree(mate_pdu *pdu, tvbuff_t *tvb, proto_tree* tree) {
263         proto_item *pdu_item;
264         proto_tree *pdu_tree;
265         
266         if ( ! pdu ) return;
267         
268         if (pdu->gop && pdu->gop->gog) {
269                 proto_item_append_text(mate_i," %s:%d->%s:%d->%s:%d",
270                                                            pdu->cfg->name,pdu->id,
271                                                            pdu->gop->cfg->name,pdu->gop->id,
272                                                            pdu->gop->gog->cfg->name,pdu->gop->gog->id);
273         } else if (pdu->gop) {
274                 proto_item_append_text(mate_i," %s:%d->%s:%d",
275                                                            pdu->cfg->name,pdu->id,
276                                                            pdu->gop->cfg->name,pdu->gop->id);
277         } else {
278                 proto_item_append_text(mate_i," %s:%d",pdu->cfg->name,pdu->id);
279         }
280         
281         pdu_item = proto_tree_add_uint(tree,pdu->cfg->hfid,tvb,0,0,pdu->id);
282         pdu_tree = proto_item_add_subtree(pdu_item, pdu->cfg->ett);
283         proto_tree_add_float(pdu_tree,pdu->cfg->hfid_pdu_rel_time, tvb, 0, 0, pdu->rel_time);           
284
285         if (pdu->gop) {
286                 proto_tree_add_float(pdu_tree,pdu->cfg->hfid_pdu_time_in_gop, tvb, 0, 0, pdu->time_in_gop);             
287                 mate_gop_tree(tree,tvb,pdu->gop);
288
289                 if (pdu->gop->gog)
290                         mate_gog_tree(tree,tvb,pdu->gop->gog,pdu->gop);
291         }
292         
293         if (pdu->avpl) {
294                 pdu_attrs_tree(pdu_tree,tvb,pdu);
295         }
296 }
297
298 static void mate_tree(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) {
299         mate_pdu* pdus;
300         proto_tree *mate_t;
301         
302         if ( ! mc || ! tree ) return;
303
304         mate_analyze_frame(pinfo,tree);
305
306         if (( pdus = mate_get_pdus(pinfo->fd->num) )) {
307                 for ( ; pdus; pdus = pdus->next_in_frame) {
308                         mate_i = proto_tree_add_protocol_format(tree,mc->hfid_mate,tvb,0,0,"MATE");
309                         mate_t = proto_item_add_subtree(mate_i, mc->ett_root);                  
310                         mate_pdu_tree(pdus,tvb,mate_t);
311                 }
312         }
313 }
314
315 static int mate_packet(void *prs _U_,  packet_info* tree _U_, epan_dissect_t *edt _U_, const void *dummy _U_) {
316         /* nothing to do yet */
317         return 0;
318 }
319
320 extern
321 void
322 proto_reg_handoff_mate(void)
323 {
324         GString* tap_error = NULL;
325         
326         if ( *pref_mate_config_filename != '\0' ) {
327                 
328                 if (current_mate_config_filename) {
329                         report_failure("Mate cannot reconfigure itself.\n"
330                                                    "for changes to be applied you have to restart wireshark\n");
331                         return;
332                 } 
333                 
334                 if (!mc) { 
335                         mc = mate_make_config(pref_mate_config_filename,proto_mate);
336                         
337                         if (mc) {
338                                 /* XXX: alignment warnings, what do they mean? */
339                                 proto_register_field_array(proto_mate, (hf_register_info*) mc->hfrs->data, mc->hfrs->len );
340                                 proto_register_subtree_array((gint**) mc->ett->data, mc->ett->len);
341                                 register_init_routine(initialize_mate_runtime);
342                                 
343                                 tap_error = register_tap_listener("frame", &mate_tap_data,
344                                                                                                   (char*) mc->tap_filter,
345                                                                                                   (tap_reset_cb) NULL,
346                                                                                                   mate_packet,
347                                                                                                   (tap_draw_cb) NULL);
348                                 
349                                 if ( tap_error ) {
350                                         g_warning("mate: couldn't (re)register tap: %s",tap_error->str);
351                                         g_string_free(tap_error, TRUE);
352                                         mate_tap_data = 0;
353                                         return;
354                                 }                                       
355                                 
356                                 initialize_mate_runtime();
357                         }
358
359                         current_mate_config_filename = pref_mate_config_filename;
360
361                 }
362         }
363 }
364
365 extern
366 void
367 proto_register_mate(void)
368 {
369         module_t *mate_module;
370     dissector_handle_t mate_handle;
371     
372         proto_mate = proto_register_protocol("Meta Analysis Tracing Engine", "MATE", "mate");
373         register_dissector("mate",mate_tree,proto_mate);
374         mate_module = prefs_register_protocol(proto_mate, proto_reg_handoff_mate);
375         prefs_register_string_preference(mate_module, "config",
376                                                                          "Configuration Filename",
377                                                                          "The name of the file containing the mate module's configuration",
378                                                                          &pref_mate_config_filename);
379
380     mate_handle = create_dissector_handle(mate_tree, proto_mate);
381     
382     register_postdissector(mate_handle);
383 }
384