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