Get rid of some useless lines from the last patch
[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  * Ethereal - Network traffic analyzer
9  * By Gerald Combs <gerald@ethereal.com>
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 char* pref_mate_config_filename = "";
41 static char* current_mate_config_filename = NULL;
42
43 static proto_item *mate_i = NULL;
44
45 void attrs_tree(proto_tree* tree, tvbuff_t *tvb,mate_item* item) {
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",item->cfg->name);
52         avpl_t = proto_item_add_subtree(avpl_i, item->cfg->ett_attr);
53
54         for ( c = item->avpl->null.next; c->avp; c = c->next) {
55                 hfi_p = g_hash_table_lookup(item->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",item->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 void mate_gop_tree(proto_tree* pdu_tree, tvbuff_t *tvb, mate_gop* gop);
67
68 void mate_gog_tree(proto_tree* tree, tvbuff_t *tvb, mate_gog* gog, mate_gop* gop) {
69         proto_item *gog_item;
70         proto_tree *gog_tree;
71         proto_item *gog_time_item;
72         proto_tree *gog_time_tree;
73         proto_item *gog_gops_item;
74         proto_tree *gog_gops_tree;
75         mate_gop* gog_gops;
76         proto_item *gog_gop_item;
77         proto_tree *gog_gop_tree;
78
79 #ifdef _MATE_DEBUGGING
80         proto_item* gog_key_item;
81         proto_tree* gog_key_tree;
82         guint i;
83 #endif
84         
85         gog_item = proto_tree_add_uint(tree,gog->cfg->hfid,tvb,0,0,gog->id);
86         gog_tree = proto_item_add_subtree(gog_item,gog->cfg->ett);
87                         
88         attrs_tree(gog_tree,tvb,gog);
89         
90         if (gog->cfg->show_times) {
91                 gog_time_item = proto_tree_add_text(gog_tree,tvb,0,0,"%s Times",gog->cfg->name);
92                 gog_time_tree = proto_item_add_subtree(gog_time_item, gog->cfg->ett_times);
93                 
94                 proto_tree_add_float(gog_time_tree, gog->cfg->hfid_start_time, tvb, 0, 0, gog->start_time);
95                 proto_tree_add_float(gog_time_tree, gog->cfg->hfid_last_time, tvb, 0, 0, gog->last_time - gog->start_time); 
96         }
97         
98         gog_gops_item = proto_tree_add_uint(gog_tree, gog->cfg->hfid_gog_num_of_gops,
99                                                                            tvb, 0, 0, gog->num_of_gops);
100         
101         gog_gops_tree = proto_item_add_subtree(gog_gops_item, gog->cfg->ett_children);
102         
103         for (gog_gops = gog->gops; gog_gops; gog_gops = gog_gops->next) {
104                 
105                 if (gop != gog_gops) {
106                         if (gog->cfg->gop_as_subtree) {
107                                 mate_gop_tree(gog_gops_tree, tvb, gog_gops);
108                         } else {
109                                 gog_gop_item = proto_tree_add_uint(gog_gops_tree,gog_gops->cfg->hfid,tvb,0,0,gog_gops->id);
110                                 
111                                 if (gop->pdus && gop->cfg->show_pdu_tree == mc->frame_tree) {
112                                         gog_gop_tree = proto_item_add_subtree(gog_gop_item, gog->cfg->ett_gog_gop);
113                                         proto_tree_add_uint(gog_gop_tree,gog->cfg->hfid_gog_gopstart,tvb,0,0,gog_gops->pdus->frame);
114                                 }
115                                 
116                         }
117                 } else {
118                          proto_tree_add_uint_format(gog_gops_tree,gop->cfg->hfid,tvb,0,0,gop->id,"%s of current frame: %d",gop->cfg->name,gop->id);
119                 }
120         }
121 }
122
123 void mate_gop_tree(proto_tree* tree, tvbuff_t *tvb, mate_gop* gop) {
124         proto_item *gop_item;
125         proto_tree *gop_time_tree;
126         proto_item *gop_time_item;
127         proto_tree *gop_tree;
128         proto_item *gop_pdu_item;
129         proto_tree *gop_pdu_tree;
130         mate_pdu* gop_pdus;
131         float  rel_time;
132         float  gop_time;
133         float pdu_rel_time;
134         gchar* pdu_str;
135         gchar* type_str;
136         guint32 pdu_item;
137         
138         gop_item = proto_tree_add_uint(tree,gop->cfg->hfid,tvb,0,0,gop->id);
139         gop_tree = proto_item_add_subtree(gop_item, gop->cfg->ett);
140         
141         if (gop->gop_key) proto_tree_add_text(gop_tree,tvb,0,0,"GOP Key: %s",gop->gop_key);
142         
143         attrs_tree(gop_tree,tvb,gop);
144         
145         if (gop->cfg->show_times) {
146                 gop_time_item = proto_tree_add_text(gop_tree,tvb,0,0,"%s Times",gop->cfg->name);
147                 gop_time_tree = proto_item_add_subtree(gop_time_item, gop->cfg->ett_times);
148                 
149                 proto_tree_add_float(gop_time_tree, gop->cfg->hfid_start_time, tvb, 0, 0, gop->start_time);
150                 
151                 if (gop->released) { 
152                         proto_tree_add_float(gop_time_tree, gop->cfg->hfid_stop_time, tvb, 0, 0, gop->release_time - gop->start_time);
153                         proto_tree_add_float(gop_time_tree, gop->cfg->hfid_last_time, tvb, 0, 0, gop->last_time - gop->start_time); 
154                 } else {
155                         proto_tree_add_float(gop_time_tree, gop->cfg->hfid_last_time, tvb, 0, 0, gop->last_time - gop->start_time); 
156                 }
157         }
158         
159         gop_pdu_item = proto_tree_add_uint(gop_tree, gop->cfg->hfid_gop_num_pdus, tvb, 0, 0,gop->num_of_pdus);
160
161         if (gop->cfg->show_pdu_tree != mc->no_tree) {
162                 
163                 gop_pdu_tree = proto_item_add_subtree(gop_pdu_item, gop->cfg->ett_children);
164
165                 rel_time = gop_time = gop->start_time;
166
167                 type_str = (gop->cfg->show_pdu_tree == mc->frame_tree ) ? "in frame:" : "id:";
168                 
169                 for (gop_pdus = gop->pdus; gop_pdus; gop_pdus = gop_pdus->next) {
170
171                         pdu_item = (gop->cfg->show_pdu_tree == mc->frame_tree ) ? gop_pdus->frame : gop_pdus->id;
172
173                         if (gop_pdus->is_start) {
174                                 pdu_str = "Start ";
175                         } else if (gop_pdus->is_stop) {
176                                 pdu_str = "Stop ";
177                         } else if (gop_pdus->after_release) {
178                                 pdu_str = "After stop ";
179                         } else {
180                                 pdu_str = "";
181                         }
182                         
183                         pdu_rel_time = gop_pdus->time_in_gop != 0.0 ? gop_pdus->time_in_gop - rel_time : (float) 0.0;
184                         
185                         proto_tree_add_uint_format(gop_pdu_tree,gop->cfg->hfid_gop_pdu,tvb,0,0,pdu_item,
186                                                                            "%sPDU: %s %i (%f : %f)",pdu_str, type_str,
187                                                                            pdu_item, gop_pdus->time_in_gop,
188                                                                            pdu_rel_time);
189                         
190                         rel_time = gop_pdus->time_in_gop;
191                         
192                 }
193         }
194 }
195
196
197 void mate_pdu_tree(mate_pdu *pdu, tvbuff_t *tvb, proto_tree* tree) {
198         proto_item *pdu_item;
199         proto_tree *pdu_tree;
200         
201         if ( ! pdu ) return;
202         
203         if (pdu->gop && pdu->gop->gog) {
204                 proto_item_append_text(mate_i," %s:%d->%s:%d->%s:%d",
205                                                            pdu->cfg->name,pdu->id,
206                                                            pdu->gop->cfg->name,pdu->gop->id,
207                                                            pdu->gop->gog->cfg->name,pdu->gop->gog->id);
208         } else if (pdu->gop) {
209                 proto_item_append_text(mate_i," %s:%d->%s:%d",
210                                                            pdu->cfg->name,pdu->id,
211                                                            pdu->gop->cfg->name,pdu->gop->id);
212         } else {
213                 proto_item_append_text(mate_i," %s:%d",pdu->cfg->name,pdu->id);
214         }
215         
216         pdu_item = proto_tree_add_uint(tree,pdu->cfg->hfid,tvb,0,0,pdu->id);
217         pdu_tree = proto_item_add_subtree(pdu_item, pdu->cfg->ett);
218         proto_tree_add_float(pdu_tree,pdu->cfg->hfid_pdu_rel_time, tvb, 0, 0, pdu->rel_time);           
219
220         if (pdu->gop) {
221                 proto_tree_add_float(pdu_tree,pdu->cfg->hfid_pdu_time_in_gop, tvb, 0, 0, pdu->time_in_gop);             
222                 mate_gop_tree(pdu_tree,tvb,pdu->gop);
223
224                 if (pdu->gop->gog)
225                         mate_gog_tree(pdu_tree,tvb,pdu->gop->gog,pdu->gop);
226         }
227         
228         if (pdu->avpl) {
229                 attrs_tree(pdu_tree,tvb,pdu);
230         }
231 }
232
233 extern void mate_tree(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) {
234         mate_pdu* pdus;
235         proto_tree *mate_t;
236         
237         if ( ! mc || ! tree ) return;
238
239         mate_analyze_frame(pinfo,tree);
240
241         if (( pdus = mate_get_pdus(pinfo->fd->num) )) {
242                 for ( ; pdus; pdus = pdus->next_in_frame) {
243                         mate_i = proto_tree_add_protocol_format(tree,mc->hfid_mate,tvb,0,0,"MATE");
244                         mate_t = proto_item_add_subtree(mate_i, mc->ett_root);                  
245                         mate_pdu_tree(pdus,tvb,mate_t);
246                 }
247         }
248 }
249
250 static int mate_packet(void *prs _U_,  packet_info* tree _U_, epan_dissect_t *edt _U_, const void *dummy _U_) {
251         /* nothing to do yet */
252         return 0;
253 }
254
255 static void init_mate(void) {
256         GString* tap_error = NULL;
257
258         if ( ! mate_tap_data ) {
259                 tap_error = register_tap_listener("frame", &mate_tap_data,
260                                                                                   (char*) mc->tap_filter,
261                                                                                   (tap_reset_cb) NULL,
262                                                                                   mate_packet,
263                                                                                   (tap_draw_cb) NULL);
264         }
265         
266         if ( tap_error ) {
267                 g_warning("mate: couldn't (re)register tap: %s",tap_error->str);
268                 g_string_free(tap_error, TRUE);
269                 mate_tap_data = 0;
270                 return;
271         } else {
272                 mate_tap_data = 1;
273         }
274         
275         initialize_mate_runtime();
276 }
277
278 extern
279 void
280 proto_reg_handoff_mate(void)
281 {
282         if ( *pref_mate_config_filename != '\0' ) {
283                 
284                 if (current_mate_config_filename) {
285                         report_failure("Mate cannot reconfigure itself.\n"
286                                                    "for changes to be applied you have to save the preferences and restart ethereal\n");
287                         return;
288                 } 
289                 
290                 if (!mc) { 
291                         mc = mate_make_config((char*)pref_mate_config_filename,proto_mate);
292                         
293                         if (mc) {
294                                 /* XXX: alignment warnings, what do they mean? */
295                                 proto_register_field_array(proto_mate, (hf_register_info*) mc->hfrs->data, mc->hfrs->len );
296                                 proto_register_subtree_array((gint**) mc->ett->data, mc->ett->len);
297                                 register_init_routine(init_mate);
298                                 if (current_mate_config_filename == NULL) initialize_mate_runtime();
299                         }
300                         
301                         current_mate_config_filename = pref_mate_config_filename;
302
303                 }
304         }
305 }
306
307 extern
308 void
309 proto_register_mate(void)
310 {
311         module_t *mate_module;
312
313         proto_mate = proto_register_protocol("Meta Analysis Tracing Engine", "MATE", "mate");
314         register_dissector("mate",mate_tree,proto_mate);
315         mate_module = prefs_register_protocol(proto_mate, proto_reg_handoff_mate);
316         prefs_register_string_preference(mate_module, "config_filename",
317                                                                          "Configuration Filename",
318                                                                          "The name of the file containing the mate module's configuration",
319                                                                          &pref_mate_config_filename);
320 }
321