Make the signatures of functions passed to "register_tap_listener()"
[obnox/wireshark/wip.git] / gtk / rpc_progs.c
1 /* rpc_progs.c
2  * rpc_progs   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 statistics to tethereal.
26  * It is only used by tethereal and not 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 <epan/tap.h>
42 #include "../register.h"
43 #include <epan/dissectors/packet-rpc.h>
44 #include "../globals.h"
45 #include "ui_util.h"
46 #include "dlg_utils.h"
47 #include "compat_macros.h"
48
49 static GtkWidget *win=NULL;
50 static GtkWidget *table=NULL;
51 static int num_progs=0;
52
53 /* used to keep track of statistics for a specific program/version */
54 typedef struct _rpc_program_t {
55         struct _rpc_program_t *next;
56         guint32 program;
57         GtkWidget *wprogram;
58         gchar sprogram[24];
59
60         guint32 version;
61         GtkWidget *wversion;
62         gchar sversion[16];
63
64         int num;
65         GtkWidget *wnum;
66         gchar snum[16];
67
68         nstime_t min;
69         GtkWidget *wmin;
70         gchar smin[16];
71
72         nstime_t max;
73         GtkWidget *wmax;
74         gchar smax[16];
75
76         nstime_t tot;
77         GtkWidget *wavg;
78         gchar savg[16];
79 } rpc_program_t;
80
81 static rpc_program_t *prog_list=NULL;
82
83
84 static char *
85 rpcprogs_gen_title(void)
86 {
87         char *title;
88
89         title = g_strdup_printf("ONC-RPC Program Statistics: %s",
90             cf_get_display_name(&cfile));
91         return title;
92 }
93
94 static void
95 rpcprogs_reset(void *dummy _U_)
96 {
97         rpc_program_t *rp;
98
99         while(prog_list){
100                 rp=prog_list;
101                 prog_list=prog_list->next;
102
103                 gtk_widget_destroy(rp->wprogram);
104                 rp->wprogram=NULL;
105                 gtk_widget_destroy(rp->wversion);
106                 rp->wversion=NULL;
107                 gtk_widget_destroy(rp->wnum);
108                 rp->wnum=NULL;
109                 gtk_widget_destroy(rp->wmin);
110                 rp->wmin=NULL;
111                 gtk_widget_destroy(rp->wmax);
112                 rp->wmax=NULL;
113                 gtk_widget_destroy(rp->wavg);
114                 rp->wavg=NULL;
115                 g_free(rp);
116         }
117         gtk_table_resize(GTK_TABLE(table), 1, 6);
118         num_progs=0;
119 }
120
121 static void
122 add_new_program(rpc_program_t *rp)
123 {
124         num_progs++;
125         gtk_table_resize(GTK_TABLE(table), num_progs+1, 6);
126         rp->wprogram=gtk_label_new("0");
127         gtk_table_attach_defaults(GTK_TABLE(table), rp->wprogram, 0,1,num_progs,num_progs+1);
128         gtk_widget_show(rp->wprogram);
129         rp->wversion=gtk_label_new("0");
130         gtk_table_attach_defaults(GTK_TABLE(table), rp->wversion, 1,2,num_progs,num_progs+1);
131         gtk_widget_show(rp->wversion);
132         rp->wnum=gtk_label_new("0");
133         gtk_table_attach_defaults(GTK_TABLE(table), rp->wnum, 2,3,num_progs,num_progs+1);
134         gtk_widget_show(rp->wnum);
135         rp->wmin=gtk_label_new("0");
136         gtk_table_attach_defaults(GTK_TABLE(table), rp->wmin, 3,4,num_progs,num_progs+1);
137         gtk_widget_show(rp->wmin);
138         rp->wmax=gtk_label_new("0");
139         gtk_table_attach_defaults(GTK_TABLE(table), rp->wmax, 4,5,num_progs,num_progs+1);
140         gtk_widget_show(rp->wmax);
141         rp->wavg=gtk_label_new("0");
142         gtk_table_attach_defaults(GTK_TABLE(table), rp->wavg, 5,6,num_progs,num_progs+1);
143         gtk_widget_show(rp->wavg);
144
145         rp->num=0;
146         rp->min.secs=0;
147         rp->min.nsecs=0;
148         rp->max.secs=0;
149         rp->max.nsecs=0;
150         rp->tot.secs=0;
151         rp->tot.nsecs=0;
152 }
153
154
155
156 static int
157 rpcprogs_packet(void *dummy _U_, packet_info *pinfo, epan_dissect_t *edt _U_, const void *arg)
158 {
159         const rpc_call_info_value *ri = arg;
160         nstime_t delta;
161         rpc_program_t *rp;
162
163         if(!prog_list){
164                 /* the list was empty */
165                 rp=g_malloc(sizeof(rpc_program_t));
166                 add_new_program(rp);
167                 rp->next=NULL;
168                 rp->program=ri->prog;
169                 rp->version=ri->vers;
170                 prog_list=rp;
171         } else if((ri->prog==prog_list->program)
172                 &&(ri->vers==prog_list->version)){
173                 rp=prog_list;
174         } else if( (ri->prog<prog_list->program)
175                 ||((ri->prog==prog_list->program)&&(ri->vers<prog_list->version))){
176                 /* we should be first entry in list */
177                 rp=g_malloc(sizeof(rpc_program_t));
178                 add_new_program(rp);
179                 rp->next=prog_list;
180                 rp->program=ri->prog;
181                 rp->version=ri->vers;
182                 prog_list=rp;
183         } else {
184                 /* we go somewhere else in the list */
185                 for(rp=prog_list;rp;rp=rp->next){
186                         if((rp->next)
187                         && (rp->next->program==ri->prog)
188                         && (rp->next->version==ri->vers)){
189                                 rp=rp->next;
190                                 break;
191                         }
192                         if((!rp->next)
193                         || (rp->next->program>ri->prog)
194                         || (  (rp->next->program==ri->prog)
195                             &&(rp->next->version>ri->vers))){
196                                 rpc_program_t *trp;
197                                 trp=g_malloc(sizeof(rpc_program_t));
198                                 add_new_program(trp);
199                                 trp->next=rp->next;
200                                 trp->program=ri->prog;
201                                 trp->version=ri->vers;
202                                 rp->next=trp;
203                                 rp=trp;
204                                 break;
205                         }
206                 }
207         }
208
209         
210         /* we are only interested in reply packets */
211         if(ri->request){
212                 return 0;
213         }
214
215         /* calculate time delta between request and reply */
216         delta.secs=pinfo->fd->abs_secs-ri->req_time.secs;
217         delta.nsecs=pinfo->fd->abs_usecs*1000-ri->req_time.nsecs;
218         if(delta.nsecs<0){
219                 delta.nsecs+=1000000000;
220                 delta.secs--;
221         }
222
223         if((rp->max.secs==0)
224         && (rp->max.nsecs==0) ){
225                 rp->max.secs=delta.secs;
226                 rp->max.nsecs=delta.nsecs;
227         }
228
229         if((rp->min.secs==0)
230         && (rp->min.nsecs==0) ){
231                 rp->min.secs=delta.secs;
232                 rp->min.nsecs=delta.nsecs;
233         }
234
235         if( (delta.secs<rp->min.secs)
236         ||( (delta.secs==rp->min.secs)
237           &&(delta.nsecs<rp->min.nsecs) ) ){
238                 rp->min.secs=delta.secs;
239                 rp->min.nsecs=delta.nsecs;
240         }
241
242         if( (delta.secs>rp->max.secs)
243         ||( (delta.secs==rp->max.secs)
244           &&(delta.nsecs>rp->max.nsecs) ) ){
245                 rp->max.secs=delta.secs;
246                 rp->max.nsecs=delta.nsecs;
247         }
248         
249         rp->tot.secs += delta.secs;
250         rp->tot.nsecs += delta.nsecs;
251         if(rp->tot.nsecs>1000000000){
252                 rp->tot.nsecs-=1000000000;
253                 rp->tot.secs++;
254         }
255         rp->num++;
256
257         return 1;
258 }
259
260
261 static void
262 rpcprogs_draw(void *dummy _U_)
263 {
264         rpc_program_t *rp;
265         int i;
266 #ifdef G_HAVE_UINT64
267         guint64 td;
268 #else
269         guint32 td;
270 #endif
271
272         for(rp=prog_list,i=1;rp;rp=rp->next,i++){
273                 /* scale it to units of 10us.*/
274                 /* for long captures with a large tot time, this can overflow on 32bit */
275                 td=(int)rp->tot.secs;
276                 td=td*100000+(int)rp->tot.nsecs/10000;
277                 if(rp->num){
278                         td/=rp->num;
279                 } else {
280                         td=0;
281                 }
282
283                 g_snprintf(rp->sprogram, sizeof(rp->sprogram), "%s",rpc_prog_name(rp->program));
284                 gtk_label_set_text(GTK_LABEL(rp->wprogram), rp->sprogram);
285
286                 g_snprintf(rp->sversion, sizeof(rp->sversion), "%d",rp->version);
287                 gtk_label_set_text(GTK_LABEL(rp->wversion), rp->sversion);
288
289                 g_snprintf(rp->snum, sizeof(rp->snum), "%d",rp->num);
290                 gtk_label_set_text(GTK_LABEL(rp->wnum), rp->snum);
291
292                 g_snprintf(rp->smin, sizeof(rp->smin), "%3d.%05d",(int)rp->min.secs,(int)rp->min.nsecs/10000);
293                 gtk_label_set_text(GTK_LABEL(rp->wmin), rp->smin);
294
295                 g_snprintf(rp->smax, sizeof(rp->smax), "%3d.%05d",(int)rp->max.secs,(int)rp->max.nsecs/10000);
296                 gtk_label_set_text(GTK_LABEL(rp->wmax), rp->smax);
297
298                 g_snprintf(rp->savg, sizeof(rp->savg), "%3d.%05d",(int)td/100000,(int)td%100000);
299                 gtk_label_set_text(GTK_LABEL(rp->wavg), rp->savg);
300
301         }
302 }
303
304 /* since the gtk2 implementation of tap is multithreaded we must protect
305  * remove_tap_listener() from modifying the list while draw_tap_listener()
306  * is running.  the other protected block is in main.c
307  *
308  * there should not be any other critical regions in gtk2
309  */
310 void protect_thread_critical_region(void);
311 void unprotect_thread_critical_region(void);
312 static void
313 win_destroy_cb(void *dummy _U_, gpointer data _U_)
314 {
315         rpc_program_t *rp, *rp2;
316
317         protect_thread_critical_region();
318         remove_tap_listener(win);
319         unprotect_thread_critical_region();
320
321         win=NULL;
322         for(rp=prog_list;rp;){
323                 rp2=rp->next;
324                 g_free(rp);
325                 rp=rp2;
326         }
327         prog_list=NULL;
328 }
329
330
331 /* When called, this function will start rpcprogs
332  */
333 void
334 gtk_rpcprogs_init(char *optarg _U_)
335 {
336         char *title_string;
337         GtkWidget *vbox;
338         GtkWidget *stat_label;
339         GtkWidget *tmp;
340         GString *error_string;
341         GtkWidget *bt_close;
342         GtkWidget *bbox;
343
344         if(win){
345                 gdk_window_raise(win->window);
346                 return;
347         }
348
349         title_string = rpcprogs_gen_title();
350         win=window_new(GTK_WINDOW_TOPLEVEL, title_string);
351
352         vbox=gtk_vbox_new(FALSE, 3);
353         gtk_container_add(GTK_CONTAINER(win), vbox);
354         gtk_container_set_border_width(GTK_CONTAINER(vbox), 12);
355
356         stat_label=gtk_label_new(title_string);
357         g_free(title_string);
358         gtk_box_pack_start(GTK_BOX(vbox), stat_label, FALSE, FALSE, 0);
359
360
361         table=gtk_table_new(1, 5, TRUE);
362         gtk_container_add(GTK_CONTAINER(vbox), table);
363
364         tmp=gtk_label_new("Program");
365         gtk_table_attach_defaults(GTK_TABLE(table), tmp, 0,1,0,1);
366         gtk_label_set_justify(GTK_LABEL(tmp), GTK_JUSTIFY_LEFT);
367
368         tmp=gtk_label_new("Version");
369         gtk_table_attach_defaults(GTK_TABLE(table), tmp, 1,2,0,1);
370         gtk_label_set_justify(GTK_LABEL(tmp), GTK_JUSTIFY_RIGHT);
371
372         tmp=gtk_label_new("Calls");
373         gtk_table_attach_defaults(GTK_TABLE(table), tmp, 2,3,0,1);
374         gtk_label_set_justify(GTK_LABEL(tmp), GTK_JUSTIFY_RIGHT);
375
376         tmp=gtk_label_new("Min SRT");
377         gtk_table_attach_defaults(GTK_TABLE(table), tmp, 3,4,0,1);
378         gtk_label_set_justify(GTK_LABEL(tmp), GTK_JUSTIFY_RIGHT);
379
380         tmp=gtk_label_new("Max SRT");
381         gtk_table_attach_defaults(GTK_TABLE(table), tmp, 4,5,0,1);
382         gtk_label_set_justify(GTK_LABEL(tmp), GTK_JUSTIFY_RIGHT);
383
384         tmp=gtk_label_new("Avg SRT");
385         gtk_table_attach_defaults(GTK_TABLE(table), tmp, 5,6,0,1);
386         gtk_label_set_justify(GTK_LABEL(tmp), GTK_JUSTIFY_RIGHT);
387
388         error_string=register_tap_listener("rpc", win, NULL, rpcprogs_reset, rpcprogs_packet, rpcprogs_draw);
389         if(error_string){
390                 fprintf(stderr, "ethereal: Couldn't register rpc,programs tap: %s\n",
391                     error_string->str);
392                 g_string_free(error_string, TRUE);
393                 exit(1);
394         }
395
396         /* Button row. */
397         bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
398         gtk_box_pack_start(GTK_BOX(vbox), bbox, FALSE, FALSE, 0);
399
400         bt_close = OBJECT_GET_DATA(bbox, GTK_STOCK_CLOSE);
401         window_set_cancel_button(win, bt_close, window_cancel_button_cb);
402
403         SIGNAL_CONNECT(win, "delete_event", window_delete_event_cb, NULL);
404         SIGNAL_CONNECT(win, "destroy", win_destroy_cb, win);
405
406         gtk_widget_show_all(win);
407         window_present(win);
408         
409         redissect_packets(&cfile);
410 }
411
412 static void
413 gtk_rpcprogs_cb(GtkWidget *w _U_, gpointer d _U_)
414 {
415         gtk_rpcprogs_init("");
416 }
417
418 void
419 register_tap_listener_gtkrpcprogs(void)
420 {
421         register_ethereal_tap("rpc,programs", gtk_rpcprogs_init);
422
423         register_tap_menu_item("ONC-RPC Programs", REGISTER_TAP_GROUP_NONE,
424         gtk_rpcprogs_cb, NULL, NULL, NULL);
425 }