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