2 * rpc_progs 2002 Ronnie Sahlberg
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
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.
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.
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.
25 /* This module provides rpc call/reply SRT statistics to Wireshark.
26 * It is only used by Wireshark and not TShark
28 * It serves as an example on how to use the tap api.
39 #include <epan/packet_info.h>
40 #include <epan/epan.h>
41 #include <epan/stat_cmd_args.h>
43 #include <epan/dissectors/packet-rpc.h>
45 #include "../stat_menu.h"
46 #include "../globals.h"
48 #include "gtk/gui_stat_menu.h"
49 #include "gtk/gui_utils.h"
50 #include "gtk/dlg_utils.h"
53 #define NANOSECS_PER_SEC 1000000000
55 static GtkWidget *win=NULL;
56 static GtkWidget *table=NULL;
57 static int num_progs=0;
59 /* used to keep track of statistics for a specific program/version */
60 typedef struct _rpc_program_t {
61 struct _rpc_program_t *next;
87 static rpc_program_t *prog_list=NULL;
91 rpcprogs_gen_title(void)
95 title = g_strdup_printf("ONC-RPC Program Statistics: %s",
96 cf_get_display_name(&cfile));
101 rpcprogs_reset(void *dummy _U_)
107 prog_list=prog_list->next;
109 gtk_widget_destroy(rp->wprogram);
111 gtk_widget_destroy(rp->wversion);
113 gtk_widget_destroy(rp->wnum);
115 gtk_widget_destroy(rp->wmin);
117 gtk_widget_destroy(rp->wmax);
119 gtk_widget_destroy(rp->wavg);
123 gtk_table_resize(GTK_TABLE(table), 1, 6);
128 add_new_program(rpc_program_t *rp)
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);
163 rpcprogs_packet(void *dummy _U_, packet_info *pinfo, epan_dissect_t *edt _U_, const void *arg)
165 const rpc_call_info_value *ri = arg;
170 /* the list was empty */
171 rp=g_malloc(sizeof(rpc_program_t));
174 rp->program=ri->prog;
175 rp->version=ri->vers;
177 } else if((ri->prog==prog_list->program)
178 &&(ri->vers==prog_list->version)){
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));
186 rp->program=ri->prog;
187 rp->version=ri->vers;
190 /* we go somewhere else in the list */
191 for(rp=prog_list;rp;rp=rp->next){
193 && (rp->next->program==ri->prog)
194 && (rp->next->version==ri->vers)){
199 || (rp->next->program>ri->prog)
200 || ( (rp->next->program==ri->prog)
201 &&(rp->next->version>ri->vers))){
203 trp=g_malloc(sizeof(rpc_program_t));
204 add_new_program(trp);
206 trp->program=ri->prog;
207 trp->version=ri->vers;
216 /* we are only interested in reply packets */
217 if(ri->request || !rp){
221 /* calculate time delta between request and reply */
222 nstime_delta(&delta, &pinfo->fd->abs_ts, &ri->req_time);
225 && (rp->max.nsecs==0) ){
226 rp->max.secs=delta.secs;
227 rp->max.nsecs=delta.nsecs;
231 && (rp->min.nsecs==0) ){
232 rp->min.secs=delta.secs;
233 rp->min.nsecs=delta.nsecs;
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;
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;
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;
263 rpcprogs_draw(void *dummy _U_)
269 for(rp=prog_list,i=1;rp;rp=rp->next,i++){
270 /* Ignore procedures with no calls */
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;
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);
286 g_snprintf(rp->sversion, sizeof(rp->sversion), "%d",rp->version);
287 gtk_label_set_text(GTK_LABEL(rp->wversion), rp->sversion);
289 g_snprintf(rp->snum, sizeof(rp->snum), "%d",rp->num);
290 gtk_label_set_text(GTK_LABEL(rp->wnum), rp->snum);
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);
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);
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);
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
308 * there should not be any other critical regions in gtk2
311 win_destroy_cb(void *dummy _U_, gpointer data _U_)
313 rpc_program_t *rp, *rp2;
315 protect_thread_critical_region();
316 remove_tap_listener(win);
317 unprotect_thread_critical_region();
320 for(rp=prog_list;rp;){
329 /* When called, this function will start rpcprogs
332 gtk_rpcprogs_init(const char *optarg _U_, void* userdata _U_)
336 GtkWidget *stat_label;
338 GString *error_string;
343 gdk_window_raise(win->window);
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);
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);
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);
360 table=gtk_table_new(1, 5, TRUE);
361 gtk_container_add(GTK_CONTAINER(vbox), table);
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);
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);
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);
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);
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);
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);
387 error_string=register_tap_listener("rpc", win, NULL, 0, rpcprogs_reset, rpcprogs_packet, rpcprogs_draw);
389 fprintf(stderr, "wireshark: Couldn't register rpc,programs tap: %s\n",
391 g_string_free(error_string, TRUE);
396 bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
397 gtk_box_pack_start(GTK_BOX(vbox), bbox, FALSE, FALSE, 0);
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);
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);
405 gtk_widget_show_all(win);
408 cf_retap_packets(&cfile);
409 gdk_window_raise(win->window);
413 gtk_rpcprogs_cb(GtkWidget *w _U_, gpointer d _U_)
415 gtk_rpcprogs_init("",NULL);
419 register_tap_listener_gtkrpcprogs(void)
421 register_stat_cmd_arg("rpc,programs", gtk_rpcprogs_init,NULL);
423 register_stat_menu_item("ONC-RPC Programs", REGISTER_STAT_GROUP_UNSORTED,
424 gtk_rpcprogs_cb, NULL, NULL, NULL);