Move the tap infrastructure to the epan directory.
[obnox/wireshark/wip.git] / gtk / rpc_stat.c
1 /* rpc_stat.c
2  * rpc_stat   2002 Ronnie Sahlberg
3  *
4  * $Id$
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
37 #include <epan/packet_info.h>
38 #include <epan/epan.h>
39
40 #include "tap_menu.h"
41 #include "simple_dialog.h"
42 #include "ui_util.h"
43 #include "dlg_utils.h"
44 #include <epan/tap.h>
45 #include "../register.h"
46 #include <epan/dissectors/packet-rpc.h>
47 #include "../globals.h"
48 #include "filter_dlg.h"
49 #include "compat_macros.h"
50 #include "service_response_time_table.h"
51 #include "gtkglobals.h"
52
53
54 /* used to keep track of the statistics for an entire program interface */
55 typedef struct _rpcstat_t {
56         GtkWidget *win;
57         srt_stat_table srt_table;
58         char *prog;
59         guint32 program;
60         guint32 version;
61         guint32 num_procedures;
62 } rpcstat_t;
63
64 static char *
65 rpcstat_gen_title(rpcstat_t *rs)
66 {
67         char *title;
68
69         title = g_strdup_printf("ONC-RPC Service Response Time statistics for %s version %d: %s",
70             rs->prog, rs->version, cf_get_display_name(&cfile));
71         return title;
72 }
73
74 static void
75 rpcstat_set_title(rpcstat_t *rs)
76 {
77         char *title;
78
79         title = rpcstat_gen_title(rs);
80         gtk_window_set_title(GTK_WINDOW(rs->win), title);
81         g_free(title);
82 }
83
84 static void
85 rpcstat_reset(rpcstat_t *rs)
86 {
87         reset_srt_table_data(&rs->srt_table);
88         rpcstat_set_title(rs);
89 }
90
91
92 static int
93 rpcstat_packet(rpcstat_t *rs, packet_info *pinfo, epan_dissect_t *edt _U_, rpc_call_info_value *ri)
94 {
95         /* we are only interested in reply packets */
96         if(ri->request){
97                 return 0;
98         }
99         /* we are only interested in certain program/versions */
100         if( (ri->prog!=rs->program) || (ri->vers!=rs->version) ){
101                 return 0;
102         }
103         /* maybe we have discovered a new procedure? 
104          * then we might need to extend our tables 
105          */
106         if(ri->proc>=rs->num_procedures){
107                 guint32 i;
108                 if(ri->proc>256){
109                         /* no program have probably ever more than this many 
110                          * procedures anyway and it prevent us from allocating
111                          * infinite memory if passed a garbage procedure id 
112                          */
113                         return 0;
114                 }
115                 for(i=rs->num_procedures;i<=ri->proc;i++){
116                         init_srt_table_row(&rs->srt_table, i, rpc_proc_name(rs->program, rs->version, i));
117                 }
118                 rs->num_procedures=ri->proc+1;
119         }
120         add_srt_table_data(&rs->srt_table, ri->proc, &ri->req_time, pinfo);
121
122         return 1;
123 }
124
125 static void
126 rpcstat_draw(rpcstat_t *rs)
127 {
128         draw_srt_table_data(&rs->srt_table);
129 }
130
131
132
133 static guint32 rpc_program=0;
134 static guint32 rpc_version=0;
135 static gint32 rpc_min_vers=-1;
136 static gint32 rpc_max_vers=-1;
137 static gint32 rpc_min_proc=-1;
138 static gint32 rpc_max_proc=-1;
139
140 static void *
141 rpcstat_find_procs(gpointer *key, gpointer *value _U_, gpointer *user_data _U_)
142 {
143         rpc_proc_info_key *k=(rpc_proc_info_key *)key;
144
145         if(k->prog!=rpc_program){
146                 return NULL;
147         }
148         if(k->vers!=rpc_version){
149                 return NULL;
150         }
151         if(rpc_min_proc==-1){
152                 rpc_min_proc=k->proc;
153                 rpc_max_proc=k->proc;
154         }
155         if((gint32)k->proc<rpc_min_proc){
156                 rpc_min_proc=k->proc;
157         }
158         if((gint32)k->proc>rpc_max_proc){
159                 rpc_max_proc=k->proc;
160         }
161
162         return NULL;
163 }
164
165 static void *
166 rpcstat_find_vers(gpointer *key, gpointer *value _U_, gpointer *user_data _U_)
167 {
168         rpc_proc_info_key *k=(rpc_proc_info_key *)key;
169
170         if(k->prog!=rpc_program){
171                 return NULL;
172         }
173         if(rpc_min_vers==-1){
174                 rpc_min_vers=k->vers;
175                 rpc_max_vers=k->vers;
176         }
177         if((gint32)k->vers<rpc_min_vers){
178                 rpc_min_vers=k->vers;
179         }
180         if((gint32)k->vers>rpc_max_vers){
181                 rpc_max_vers=k->vers;
182         }
183
184         return NULL;
185 }
186
187 /* since the gtk2 implementation of tap is multithreaded we must protect
188  * remove_tap_listener() from modifying the list while draw_tap_listener()
189  * is running.  the other protected block is in main.c
190  *
191  * there should not be any other critical regions in gtk2
192  */
193 void protect_thread_critical_region(void);
194 void unprotect_thread_critical_region(void);
195 static void
196 win_destroy_cb(GtkWindow *win _U_, gpointer data)
197 {
198         rpcstat_t *rs=(rpcstat_t *)data;
199
200         protect_thread_critical_region();
201         remove_tap_listener(rs);
202         unprotect_thread_critical_region();
203
204         free_srt_table_data(&rs->srt_table);
205         g_free(rs);
206 }
207
208 /* When called, this function will create a new instance of gtk2-rpcstat.
209  */
210 static void
211 gtk_rpcstat_init(char *optarg)
212 {
213         rpcstat_t *rs;
214         guint32 i;
215         char *title_string;
216         char filter_string[256];
217         GtkWidget *vbox;
218         GtkWidget *stat_label;
219         GtkWidget *filter_label;
220     GtkWidget *bbox;
221     GtkWidget *close_bt;
222         int program, version, pos;
223         char *filter=NULL;
224         GString *error_string;
225         int hf_index;
226         header_field_info *hfi;
227
228         pos=0;
229         if(sscanf(optarg,"rpc,srt,%d,%d,%n",&program,&version,&pos)==2){
230                 if(pos){
231                         filter=optarg+pos;
232                 } else {
233                         filter=NULL;
234                 }
235         } else {
236                 fprintf(stderr, "ethereal: invalid \"-z rpc,srt,<program>,<version>[,<filter>]\" argument\n");
237                 exit(1);
238         }
239
240         rpc_program=program;
241         rpc_version=version;
242         rs=g_malloc(sizeof(rpcstat_t));
243         rs->prog=rpc_prog_name(rpc_program);
244         rs->program=rpc_program;
245         rs->version=rpc_version;
246         hf_index=rpc_prog_hf(rpc_program, rpc_version);
247         hfi=proto_registrar_get_nth(hf_index);
248
249         rs->win=window_new(GTK_WINDOW_TOPLEVEL, "rpc-stat");
250         gtk_window_set_default_size(GTK_WINDOW(rs->win), 550, 400);
251         rpcstat_set_title(rs);
252
253         vbox=gtk_vbox_new(FALSE, 3);
254         gtk_container_add(GTK_CONTAINER(rs->win), vbox);
255         gtk_container_set_border_width(GTK_CONTAINER(vbox), 12);
256         
257     title_string = rpcstat_gen_title(rs);
258         stat_label=gtk_label_new(title_string);
259         g_free(title_string);
260         gtk_box_pack_start(GTK_BOX(vbox), stat_label, FALSE, FALSE, 0);
261
262         g_snprintf(filter_string,255,"Filter:%s",filter?filter:"");
263         filter_label=gtk_label_new(filter_string);
264         gtk_box_pack_start(GTK_BOX(vbox), filter_label, FALSE, FALSE, 0);
265
266         rpc_min_proc=-1;
267         rpc_max_proc=-1;
268         g_hash_table_foreach(rpc_procs, (GHFunc)rpcstat_find_procs, NULL);
269         rs->num_procedures=rpc_max_proc+1;
270
271         /* We must display TOP LEVEL Widget before calling init_srt_table() */
272         gtk_widget_show_all(rs->win);
273
274         init_srt_table(&rs->srt_table, rpc_max_proc+1, vbox, hfi->abbrev);
275
276         for(i=0;i<rs->num_procedures;i++){
277                 init_srt_table_row(&rs->srt_table, i, rpc_proc_name(rpc_program, rpc_version, i));
278         }
279
280
281         error_string=register_tap_listener("rpc", rs, filter, (void*)rpcstat_reset, (void*)rpcstat_packet, (void*)rpcstat_draw);
282         if(error_string){
283                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, error_string->str);
284                 g_string_free(error_string, TRUE);
285                 free_srt_table_data(&rs->srt_table);
286                 g_free(rs);
287                 return;
288         }
289
290         /* Button row. */
291         bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
292         gtk_box_pack_end(GTK_BOX(vbox), bbox, FALSE, FALSE, 0);
293
294         close_bt = OBJECT_GET_DATA(bbox, GTK_STOCK_CLOSE);
295     window_set_cancel_button(rs->win, close_bt, window_cancel_button_cb);
296
297     SIGNAL_CONNECT(rs->win, "delete_event", window_delete_event_cb, NULL);
298         SIGNAL_CONNECT(rs->win, "destroy", win_destroy_cb, rs);
299
300     gtk_widget_show_all(rs->win);
301     window_present(rs->win);
302
303     retap_packets(&cfile);
304 }
305
306
307
308
309 static GtkWidget *dlg=NULL;
310 static GtkWidget *prog_menu;
311 static GtkWidget *vers_opt, *vers_menu;
312 static GtkWidget *filter_entry;
313
314
315 static void
316 rpcstat_start_button_clicked(GtkWidget *item _U_, gpointer data _U_)
317 {
318         GString *str;
319         char *filter;
320
321         str = g_string_new("rpc,srt");
322         g_string_sprintfa(str, ",%d,%d", rpc_program, rpc_version);
323         filter=(char *)gtk_entry_get_text(GTK_ENTRY(filter_entry));
324         if(filter[0]!=0){
325                 g_string_sprintfa(str, ",%s", filter);
326         }
327
328         gtk_rpcstat_init(str->str);
329         g_string_free(str, TRUE);
330 }
331
332
333 static void
334 rpcstat_version_select(GtkWidget *item _U_, gpointer key)
335 {
336         int vers=(int)key;
337
338         rpc_version=vers;
339 }
340
341
342
343 static void
344 rpcstat_program_select(GtkWidget *item _U_, gpointer key)
345 {
346         rpc_prog_info_key *k=(rpc_prog_info_key *)key;
347         int i;
348
349         rpc_program=k->prog;
350
351         /* change version menu */
352         rpc_version=0;
353         gtk_object_destroy(GTK_OBJECT(vers_menu));
354         vers_menu=gtk_menu_new();
355         rpc_min_vers=-1;
356         rpc_max_vers=-1;
357         g_hash_table_foreach(rpc_procs, (GHFunc)rpcstat_find_vers, NULL);
358         rpc_version=rpc_min_vers;
359         for(i=rpc_min_vers;i<=rpc_max_vers;i++){
360                 GtkWidget *menu_item;
361                 char vs[5];
362                 g_snprintf(vs, 5, "%d",i);
363                 menu_item=gtk_menu_item_new_with_label(vs);
364                 SIGNAL_CONNECT(menu_item, "activate", rpcstat_version_select,
365                                i);
366
367                 gtk_widget_show(menu_item);
368                 gtk_menu_append(GTK_MENU(vers_menu), menu_item);
369         }
370         gtk_option_menu_set_menu(GTK_OPTION_MENU(vers_opt), vers_menu);
371 }
372
373 static void *
374 rpcstat_list_programs(gpointer *key, gpointer *value, gpointer *user_data _U_)
375 {
376         rpc_prog_info_key *k=(rpc_prog_info_key *)key;
377         rpc_prog_info_value *v=(rpc_prog_info_value *)value;
378         GtkWidget *menu_item;
379
380         menu_item=gtk_menu_item_new_with_label(v->progname);
381         SIGNAL_CONNECT(menu_item, "activate", rpcstat_program_select, k);
382
383         gtk_widget_show(menu_item);
384         gtk_menu_append(GTK_MENU(prog_menu), menu_item);
385
386         if(!rpc_program){
387                 rpc_program=k->prog;
388         }
389
390         return NULL;
391 }
392
393 static void
394 dlg_destroy_cb(void)
395 {
396         dlg=NULL;
397 }
398
399 static void
400 gtk_rpcstat_cb(GtkWidget *w _U_, gpointer d _U_)
401 {
402         GtkWidget *dlg_box;
403         GtkWidget *prog_box, *prog_label, *prog_opt;
404         GtkWidget *vers_label;
405         GtkWidget *filter_box, *filter_bt;
406         GtkWidget *bbox, *start_button, *cancel_button;
407         int i;
408         const char *filter;
409         static construct_args_t args = {
410           "Service Response Time Statistics Filter",
411           TRUE,
412           FALSE
413         };
414
415         /* if the window is already open, bring it to front */
416         if(dlg){
417                 gdk_window_raise(dlg->window);
418                 return;
419         }
420
421         dlg=dlg_window_new("Ethereal: Compute ONC-RPC SRT statistics");
422         gtk_window_set_default_size(GTK_WINDOW(dlg), 300, -1);
423
424         dlg_box=gtk_vbox_new(FALSE, 10);
425         gtk_container_border_width(GTK_CONTAINER(dlg_box), 10);
426         gtk_container_add(GTK_CONTAINER(dlg), dlg_box);
427         gtk_widget_show(dlg_box);
428
429         /* Program box */
430         prog_box=gtk_hbox_new(FALSE, 10);
431
432         /* Program label */
433         gtk_container_set_border_width(GTK_CONTAINER(prog_box), 10);
434         prog_label=gtk_label_new("Program:");
435         gtk_box_pack_start(GTK_BOX(prog_box), prog_label, FALSE, FALSE, 0);
436         gtk_widget_show(prog_label);
437
438         /* Program menu */
439         prog_opt=gtk_option_menu_new();
440         prog_menu=gtk_menu_new();
441         g_hash_table_foreach(rpc_progs, (GHFunc)rpcstat_list_programs, NULL);
442         gtk_option_menu_set_menu(GTK_OPTION_MENU(prog_opt), prog_menu);
443         gtk_box_pack_start(GTK_BOX(prog_box), prog_opt, TRUE, TRUE, 0);
444         gtk_widget_show(prog_opt);
445
446         /* Version label */
447         gtk_container_set_border_width(GTK_CONTAINER(prog_box), 10);
448         vers_label=gtk_label_new("Version:");
449         gtk_box_pack_start(GTK_BOX(prog_box), vers_label, FALSE, FALSE, 0);
450         gtk_widget_show(vers_label);
451
452         /* Version menu */
453         vers_opt=gtk_option_menu_new();
454         vers_menu=gtk_menu_new();
455         rpc_min_vers=-1;
456         rpc_max_vers=-1;
457         g_hash_table_foreach(rpc_procs, (GHFunc)rpcstat_find_vers, NULL);
458         rpc_version=rpc_min_vers;
459         for(i=rpc_min_vers;i<=rpc_max_vers;i++){
460                 GtkWidget *menu_item;
461                 char vs[5];
462                 g_snprintf(vs, 5, "%d",i);
463                 menu_item=gtk_menu_item_new_with_label(vs);
464                 SIGNAL_CONNECT(menu_item, "activate", rpcstat_version_select,
465                                i);
466
467                 gtk_widget_show(menu_item);
468                 gtk_menu_append(GTK_MENU(vers_menu), menu_item);
469         }
470         gtk_option_menu_set_menu(GTK_OPTION_MENU(vers_opt), vers_menu);
471         gtk_box_pack_start(GTK_BOX(prog_box), vers_opt, TRUE, TRUE, 0);
472         gtk_widget_show(vers_opt);
473
474         gtk_box_pack_start(GTK_BOX(dlg_box), prog_box, TRUE, TRUE, 0);
475         gtk_widget_show(prog_box);
476
477         /* Filter box */
478         filter_box=gtk_hbox_new(FALSE, 3);
479
480         /* Filter label */
481         filter_bt=BUTTON_NEW_FROM_STOCK(ETHEREAL_STOCK_DISPLAY_FILTER_ENTRY);
482         SIGNAL_CONNECT(filter_bt, "clicked", display_filter_construct_cb, &args);
483         gtk_box_pack_start(GTK_BOX(filter_box), filter_bt, FALSE, FALSE, 0);
484         gtk_widget_show(filter_bt);
485
486         /* Filter entry */
487         filter_entry=gtk_entry_new();
488     SIGNAL_CONNECT(filter_entry, "changed", filter_te_syntax_check_cb, NULL);
489
490         /* filter prefs dialog */
491         OBJECT_SET_DATA(filter_bt, E_FILT_TE_PTR_KEY, filter_entry);
492         /* filter prefs dialog */
493
494         gtk_box_pack_start(GTK_BOX(filter_box), filter_entry, TRUE, TRUE, 0);
495         filter=gtk_entry_get_text(GTK_ENTRY(main_display_filter_widget));
496         if(filter){
497                 gtk_entry_set_text(GTK_ENTRY(filter_entry), filter);
498         }
499         gtk_widget_show(filter_entry);
500
501         gtk_box_pack_start(GTK_BOX(dlg_box), filter_box, TRUE, TRUE, 0);
502         gtk_widget_show(filter_box);
503
504         /* button box */
505     bbox = dlg_button_row_new(ETHEREAL_STOCK_CREATE_STAT, GTK_STOCK_CANCEL, NULL);
506         gtk_box_pack_start(GTK_BOX(dlg_box), bbox, FALSE, FALSE, 0);
507     gtk_widget_show(bbox);
508
509     start_button = OBJECT_GET_DATA(bbox, ETHEREAL_STOCK_CREATE_STAT);
510     SIGNAL_CONNECT_OBJECT(start_button, "clicked",
511                               rpcstat_start_button_clicked, NULL);
512
513     cancel_button = OBJECT_GET_DATA(bbox, GTK_STOCK_CANCEL);
514     window_set_cancel_button(dlg, cancel_button, window_cancel_button_cb);
515
516         /* Give the initial focus to the "Filter" entry box. */
517         gtk_widget_grab_focus(filter_entry);
518
519     gtk_widget_grab_default(start_button );
520
521     SIGNAL_CONNECT(dlg, "delete_event", window_delete_event_cb, NULL);
522         SIGNAL_CONNECT(dlg, "destroy", dlg_destroy_cb, NULL);
523
524     gtk_widget_show_all(dlg);
525     window_present(dlg);
526 }
527
528
529 void
530 register_tap_listener_gtkrpcstat(void)
531 {
532         register_ethereal_tap("rpc,srt,", gtk_rpcstat_init);
533
534         register_tap_menu_item("ONC-RPC...", REGISTER_TAP_GROUP_RESPONSE_TIME,
535             gtk_rpcstat_cb, NULL, NULL, NULL);
536 }
537