Update to the Response Time Statistics taps.
[obnox/wireshark/wip.git] / gtk / rpc_stat.c
1 /* rpc_stat.c
2  * rpc_stat   2002 Ronnie Sahlberg
3  *
4  * $Id: rpc_stat.c,v 1.13 2003/08/19 10:09:20 sahlberg Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
8  * Copyright 1998 Gerald Combs
9  * 
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  * 
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  * 
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 /* This module provides rpc call/reply SRT (Server Response Time) statistics 
26  * to ethereal.
27  *
28  * It serves as an example on how to use the tap api.
29  */
30
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34
35 #include <gtk/gtk.h>
36 #include "menu.h"
37 #include "epan/packet_info.h"
38 #include "simple_dialog.h"
39 #include "tap.h"
40 #include "../register.h"
41 #include "packet-rpc.h"
42 #include "../globals.h"
43 #include "compat_macros.h"
44 #include "service_response_time_table.h"
45
46 extern GtkWidget   *main_display_filter_widget;
47
48 /* used to keep track of the statistics for an entire program interface */
49 typedef struct _rpcstat_t {
50         GtkWidget *win;
51         srt_stat_table srt_table;
52         char *prog;
53         guint32 program;
54         guint32 version;
55         guint32 num_procedures;
56 } rpcstat_t;
57
58
59
60
61 static void
62 rpcstat_reset(rpcstat_t *rs)
63 {
64         reset_srt_table_data(&rs->srt_table);
65 }
66
67
68 static int
69 rpcstat_packet(rpcstat_t *rs, packet_info *pinfo, epan_dissect_t *edt _U_, rpc_call_info_value *ri)
70 {
71         if(ri->proc>=rs->num_procedures){
72                 /* dont handle this since its outside of known table */
73                 return 0;
74         }
75         /* we are only interested in reply packets */
76         if(ri->request){
77                 return 0;
78         }
79         /* we are only interested in certain program/versions */
80         if( (ri->prog!=rs->program) || (ri->vers!=rs->version) ){
81                 return 0;
82         }
83
84         add_srt_table_data(&rs->srt_table, ri->proc, &ri->req_time, pinfo);
85
86         return 1;
87 }
88
89 static void
90 rpcstat_draw(rpcstat_t *rs)
91 {
92         draw_srt_table_data(&rs->srt_table);
93 }
94
95
96
97 static guint32 rpc_program=0;
98 static guint32 rpc_version=0;
99 static gint32 rpc_min_vers=-1;
100 static gint32 rpc_max_vers=-1;
101 static gint32 rpc_min_proc=-1;
102 static gint32 rpc_max_proc=-1;
103
104 static void *
105 rpcstat_find_procs(gpointer *key, gpointer *value _U_, gpointer *user_data _U_)
106 {
107         rpc_proc_info_key *k=(rpc_proc_info_key *)key;
108
109         if(k->prog!=rpc_program){
110                 return NULL;
111         }
112         if(k->vers!=rpc_version){
113                 return NULL;
114         }
115         if(rpc_min_proc==-1){
116                 rpc_min_proc=k->proc;
117                 rpc_max_proc=k->proc;
118         }
119         if((gint32)k->proc<rpc_min_proc){
120                 rpc_min_proc=k->proc;
121         }
122         if((gint32)k->proc>rpc_max_proc){
123                 rpc_max_proc=k->proc;
124         }
125
126         return NULL;
127 }
128
129 static void *
130 rpcstat_find_vers(gpointer *key, gpointer *value _U_, gpointer *user_data _U_)
131 {
132         rpc_proc_info_key *k=(rpc_proc_info_key *)key;
133
134         if(k->prog!=rpc_program){
135                 return NULL;
136         }
137         if(rpc_min_vers==-1){
138                 rpc_min_vers=k->vers;
139                 rpc_max_vers=k->vers;
140         }
141         if((gint32)k->vers<rpc_min_vers){
142                 rpc_min_vers=k->vers;
143         }
144         if((gint32)k->vers>rpc_max_vers){
145                 rpc_max_vers=k->vers;
146         }
147
148         return NULL;
149 }
150
151 /* since the gtk2 implementation of tap is multithreaded we must protect
152  * remove_tap_listener() from modifying the list while draw_tap_listener()
153  * is running.  the other protected block is in main.c
154  *
155  * there should not be any other critical regions in gtk2
156  */
157 void protect_thread_critical_region(void);
158 void unprotect_thread_critical_region(void);
159 static void
160 win_destroy_cb(GtkWindow *win _U_, gpointer data)
161 {
162         rpcstat_t *rs=(rpcstat_t *)data;
163
164         protect_thread_critical_region();
165         remove_tap_listener(rs);
166         unprotect_thread_critical_region();
167
168         free_srt_table_data(&rs->srt_table);
169         g_free(rs);
170 }
171
172 /* When called, this function will create a new instance of gtk2-rpcstat.
173  */
174 static void
175 gtk_rpcstat_init(char *optarg)
176 {
177         rpcstat_t *rs;
178         guint32 i;
179         char title_string[60];
180         char filter_string[256];
181         GtkWidget *vbox;
182         GtkWidget *stat_label;
183         GtkWidget *filter_label;
184         int program, version, pos;
185         char *filter=NULL;
186         GString *error_string;
187
188         pos=0;
189         if(sscanf(optarg,"rpc,srt,%d,%d,%n",&program,&version,&pos)==2){
190                 if(pos){
191                         filter=optarg+pos;
192                 } else {
193                         filter=NULL;
194                 }
195         } else {
196                 fprintf(stderr, "ethereal: invalid \"-z rpc,srt,<program>,<version>[,<filter>]\" argument\n");
197                 exit(1);
198         }
199
200         rpc_program=program;
201         rpc_version=version;
202         rs=g_malloc(sizeof(rpcstat_t));
203         rs->prog=rpc_prog_name(rpc_program);
204         rs->program=rpc_program;
205         rs->version=rpc_version;
206
207         rs->win=gtk_window_new(GTK_WINDOW_TOPLEVEL);
208         gtk_window_set_default_size(GTK_WINDOW(rs->win), 550, 400);
209         sprintf(title_string,"ONC-RPC Service Response Time statistics for %s version %d", rs->prog, rs->version);
210         gtk_window_set_title(GTK_WINDOW(rs->win), title_string);
211         SIGNAL_CONNECT(rs->win, "destroy", win_destroy_cb, rs);
212
213         vbox=gtk_vbox_new(FALSE, 0);
214         gtk_container_add(GTK_CONTAINER(rs->win), vbox);
215         gtk_container_set_border_width(GTK_CONTAINER(vbox), 10);
216         gtk_widget_show(vbox);
217
218         stat_label=gtk_label_new(title_string);
219         gtk_box_pack_start(GTK_BOX(vbox), stat_label, FALSE, FALSE, 0);
220         gtk_widget_show(stat_label);
221
222         snprintf(filter_string,255,"Filter:%s",filter?filter:"");
223         filter_label=gtk_label_new(filter_string);
224         gtk_box_pack_start(GTK_BOX(vbox), filter_label, FALSE, FALSE, 0);
225         gtk_widget_show(filter_label);
226
227         rpc_min_proc=-1;
228         rpc_max_proc=-1;
229         g_hash_table_foreach(rpc_procs, (GHFunc)rpcstat_find_procs, NULL);
230         rs->num_procedures=rpc_max_proc+1;
231
232         /* We must display TOP LEVEL Widget before calling init_srt_table() */
233         gtk_widget_show(rs->win);
234
235         init_srt_table(&rs->srt_table, rpc_max_proc+1, vbox);
236
237         for(i=0;i<rs->num_procedures;i++){
238                 init_srt_table_row(&rs->srt_table, i, rpc_proc_name(rpc_program, rpc_version, i));
239         }
240
241
242         error_string=register_tap_listener("rpc", rs, filter, (void*)rpcstat_reset, (void*)rpcstat_packet, (void*)rpcstat_draw);
243         if(error_string){
244                 simple_dialog(ESD_TYPE_WARN, NULL, error_string->str);
245                 g_string_free(error_string, TRUE);
246                 free_srt_table_data(&rs->srt_table);
247                 g_free(rs);
248                 return;
249         }
250
251
252         gtk_widget_show_all(rs->win);
253         redissect_packets(&cfile);
254 }
255
256
257
258
259 static GtkWidget *dlg=NULL, *dlg_box;
260 static GtkWidget *prog_box;
261 static GtkWidget *prog_label, *prog_opt, *prog_menu;
262 static GtkWidget *vers_label, *vers_opt, *vers_menu;
263 static GtkWidget *filter_box;
264 static GtkWidget *filter_label, *filter_entry;
265 static GtkWidget *start_button;
266
267
268 static void
269 rpcstat_start_button_clicked(GtkWidget *item _U_, gpointer data _U_)
270 {
271         char *filter;
272         char str[256];
273
274         filter=(char *)gtk_entry_get_text(GTK_ENTRY(filter_entry));
275         if(filter[0]==0){
276                 sprintf(str, "rpc,srt,%d,%d", rpc_program, rpc_version);
277                 filter="";
278         } else {
279                 sprintf(str, "rpc,srt,%d,%d,%s", rpc_program, rpc_version, filter);
280         }
281         gtk_rpcstat_init(str);
282 }
283
284
285 static void
286 rpcstat_version_select(GtkWidget *item _U_, gpointer key)
287 {
288         int vers=(int)key;
289
290         rpc_version=vers;
291 }
292
293
294
295 static void
296 rpcstat_program_select(GtkWidget *item _U_, gpointer key)
297 {
298         rpc_prog_info_key *k=(rpc_prog_info_key *)key;
299         int i;
300
301         rpc_program=k->prog;
302
303         /* change version menu */
304         rpc_version=0;
305         gtk_object_destroy(GTK_OBJECT(vers_menu));
306         vers_menu=gtk_menu_new();
307         rpc_min_vers=-1;
308         rpc_max_vers=-1;
309         g_hash_table_foreach(rpc_procs, (GHFunc)rpcstat_find_vers, NULL);
310         rpc_version=rpc_min_vers;
311         for(i=rpc_min_vers;i<=rpc_max_vers;i++){
312                 GtkWidget *menu_item;
313                 char vs[5];
314                 sprintf(vs,"%d",i);
315                 menu_item=gtk_menu_item_new_with_label(vs);
316                 SIGNAL_CONNECT(menu_item, "activate", rpcstat_version_select,
317                                i);
318
319                 gtk_widget_show(menu_item);
320                 gtk_menu_append(GTK_MENU(vers_menu), menu_item);
321         }
322         gtk_option_menu_set_menu(GTK_OPTION_MENU(vers_opt), vers_menu);
323 }
324
325 static void *
326 rpcstat_list_programs(gpointer *key, gpointer *value, gpointer *user_data _U_)
327 {
328         rpc_prog_info_key *k=(rpc_prog_info_key *)key;
329         rpc_prog_info_value *v=(rpc_prog_info_value *)value;
330         GtkWidget *menu_item;
331
332         menu_item=gtk_menu_item_new_with_label(v->progname);
333         SIGNAL_CONNECT(menu_item, "activate", rpcstat_program_select, k);
334
335         gtk_widget_show(menu_item);
336         gtk_menu_append(GTK_MENU(prog_menu), menu_item);
337
338         if(!rpc_program){
339                 rpc_program=k->prog;
340         }
341
342         return NULL;
343 }
344
345 static void
346 dlg_destroy_cb(void)
347 {
348         dlg=NULL;
349 }
350
351 static void
352 gtk_rpcstat_cb(GtkWidget *w _U_, gpointer d _U_)
353 {
354         int i;
355         char *filter;
356
357         /* if the window is already open, bring it to front */
358         if(dlg){
359                 gdk_window_raise(dlg->window);
360                 return;
361         }
362
363         dlg=gtk_window_new(GTK_WINDOW_TOPLEVEL);
364         gtk_window_set_title(GTK_WINDOW(dlg), "ONC-RPC Service Response Time statistics");
365         SIGNAL_CONNECT(dlg, "destroy", dlg_destroy_cb, NULL);
366         dlg_box=gtk_vbox_new(FALSE, 0);
367         gtk_container_add(GTK_CONTAINER(dlg), dlg_box);
368         gtk_widget_show(dlg_box);
369
370
371         prog_box=gtk_hbox_new(FALSE, 10);
372         /* Program label */
373         gtk_container_set_border_width(GTK_CONTAINER(prog_box), 10);
374         prog_label=gtk_label_new("Program:");
375         gtk_box_pack_start(GTK_BOX(prog_box), prog_label, FALSE, FALSE, 0);
376         gtk_widget_show(prog_label);
377
378         /* Program menu */
379         prog_opt=gtk_option_menu_new();
380         prog_menu=gtk_menu_new();
381         g_hash_table_foreach(rpc_progs, (GHFunc)rpcstat_list_programs, NULL);
382         gtk_option_menu_set_menu(GTK_OPTION_MENU(prog_opt), prog_menu);
383         gtk_box_pack_start(GTK_BOX(prog_box), prog_opt, TRUE, TRUE, 0);
384         gtk_widget_show(prog_opt);
385
386         /* Version label */
387         gtk_container_set_border_width(GTK_CONTAINER(prog_box), 10);
388         vers_label=gtk_label_new("Version:");
389         gtk_box_pack_start(GTK_BOX(prog_box), vers_label, FALSE, FALSE, 0);
390         gtk_widget_show(vers_label);
391
392         /* Version menu */
393         vers_opt=gtk_option_menu_new();
394         vers_menu=gtk_menu_new();
395         rpc_min_vers=-1;
396         rpc_max_vers=-1;
397         g_hash_table_foreach(rpc_procs, (GHFunc)rpcstat_find_vers, NULL);
398         rpc_version=rpc_min_vers;
399         for(i=rpc_min_vers;i<=rpc_max_vers;i++){
400                 GtkWidget *menu_item;
401                 char vs[5];
402                 sprintf(vs,"%d",i);
403                 menu_item=gtk_menu_item_new_with_label(vs);
404                 SIGNAL_CONNECT(menu_item, "activate", rpcstat_version_select,
405                                i);
406
407                 gtk_widget_show(menu_item);
408                 gtk_menu_append(GTK_MENU(vers_menu), menu_item);
409         }
410
411         gtk_option_menu_set_menu(GTK_OPTION_MENU(vers_opt), vers_menu);
412         gtk_box_pack_start(GTK_BOX(prog_box), vers_opt, TRUE, TRUE, 0);
413         gtk_widget_show(vers_opt);
414
415         gtk_box_pack_start(GTK_BOX(dlg_box), prog_box, TRUE, TRUE, 0);
416         gtk_widget_show(prog_box);
417
418
419         /* filter box */
420         filter_box=gtk_hbox_new(FALSE, 10);
421         /* Filter label */
422         gtk_container_set_border_width(GTK_CONTAINER(filter_box), 10);
423         filter_label=gtk_label_new("Filter:");
424         gtk_box_pack_start(GTK_BOX(filter_box), filter_label, FALSE, FALSE, 0);
425         gtk_widget_show(filter_label);
426
427         filter_entry=gtk_entry_new_with_max_length(250);
428         gtk_box_pack_start(GTK_BOX(filter_box), filter_entry, FALSE, FALSE, 0);
429         filter=gtk_entry_get_text(GTK_ENTRY(main_display_filter_widget));
430         if(filter){
431                 gtk_entry_set_text(GTK_ENTRY(filter_entry), filter);
432         }
433         gtk_widget_show(filter_entry);
434
435         gtk_box_pack_start(GTK_BOX(dlg_box), filter_box, TRUE, TRUE, 0);
436         gtk_widget_show(filter_box);
437
438
439         /* the start button */
440         start_button=gtk_button_new_with_label("Create Stat");
441         SIGNAL_CONNECT_OBJECT(start_button, "clicked",
442                               rpcstat_start_button_clicked, NULL);
443         gtk_box_pack_start(GTK_BOX(dlg_box), start_button, TRUE, TRUE, 0);
444         gtk_widget_show(start_button);
445
446         gtk_widget_show_all(dlg);
447 }
448
449
450 void
451 register_tap_listener_gtkrpcstat(void)
452 {
453         register_ethereal_tap("rpc,srt,", gtk_rpcstat_init);
454 }
455
456 void
457 register_tap_menu_gtkrpcstat(void)
458 {
459         register_tap_menu_item("Service Response Time/ONC-RPC", gtk_rpcstat_cb);
460 }