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 "../register.h"
47 #include "../globals.h"
49 #include "gtk/gui_stat_menu.h"
50 #include "gtk/gui_utils.h"
51 #include "gtk/dlg_utils.h"
54 #define NANOSECS_PER_SEC 1000000000
56 static GtkWidget *win=NULL;
57 static GtkWidget *table=NULL;
58 static int num_progs=0;
60 /* used to keep track of statistics for a specific program/version */
61 typedef struct _rpc_program_t {
62 struct _rpc_program_t *next;
88 static rpc_program_t *prog_list=NULL;
92 rpcprogs_gen_title(void)
96 title = g_strdup_printf("ONC-RPC Program Statistics: %s",
97 cf_get_display_name(&cfile));
102 rpcprogs_reset(void *dummy _U_)
108 prog_list=prog_list->next;
110 gtk_widget_destroy(rp->wprogram);
112 gtk_widget_destroy(rp->wversion);
114 gtk_widget_destroy(rp->wnum);
116 gtk_widget_destroy(rp->wmin);
118 gtk_widget_destroy(rp->wmax);
120 gtk_widget_destroy(rp->wavg);
124 gtk_table_resize(GTK_TABLE(table), 1, 6);
129 add_new_program(rpc_program_t *rp)
132 gtk_table_resize(GTK_TABLE(table), num_progs+1, 6);
133 rp->wprogram=gtk_label_new("0");
134 gtk_table_attach_defaults(GTK_TABLE(table), rp->wprogram, 0,1,num_progs,num_progs+1);
135 gtk_widget_show(rp->wprogram);
136 rp->wversion=gtk_label_new("0");
137 gtk_table_attach_defaults(GTK_TABLE(table), rp->wversion, 1,2,num_progs,num_progs+1);
138 gtk_widget_show(rp->wversion);
139 rp->wnum=gtk_label_new("0");
140 gtk_table_attach_defaults(GTK_TABLE(table), rp->wnum, 2,3,num_progs,num_progs+1);
141 gtk_widget_show(rp->wnum);
142 rp->wmin=gtk_label_new("0");
143 gtk_table_attach_defaults(GTK_TABLE(table), rp->wmin, 3,4,num_progs,num_progs+1);
144 gtk_widget_show(rp->wmin);
145 rp->wmax=gtk_label_new("0");
146 gtk_table_attach_defaults(GTK_TABLE(table), rp->wmax, 4,5,num_progs,num_progs+1);
147 gtk_widget_show(rp->wmax);
148 rp->wavg=gtk_label_new("0");
149 gtk_table_attach_defaults(GTK_TABLE(table), rp->wavg, 5,6,num_progs,num_progs+1);
150 gtk_widget_show(rp->wavg);
164 rpcprogs_packet(void *dummy _U_, packet_info *pinfo, epan_dissect_t *edt _U_, const void *arg)
166 const rpc_call_info_value *ri = arg;
171 /* the list was empty */
172 rp=g_malloc(sizeof(rpc_program_t));
175 rp->program=ri->prog;
176 rp->version=ri->vers;
178 } else if((ri->prog==prog_list->program)
179 &&(ri->vers==prog_list->version)){
181 } else if( (ri->prog<prog_list->program)
182 ||((ri->prog==prog_list->program)&&(ri->vers<prog_list->version))){
183 /* we should be first entry in list */
184 rp=g_malloc(sizeof(rpc_program_t));
187 rp->program=ri->prog;
188 rp->version=ri->vers;
191 /* we go somewhere else in the list */
192 for(rp=prog_list;rp;rp=rp->next){
194 && (rp->next->program==ri->prog)
195 && (rp->next->version==ri->vers)){
200 || (rp->next->program>ri->prog)
201 || ( (rp->next->program==ri->prog)
202 &&(rp->next->version>ri->vers))){
204 trp=g_malloc(sizeof(rpc_program_t));
205 add_new_program(trp);
207 trp->program=ri->prog;
208 trp->version=ri->vers;
217 /* we are only interested in reply packets */
218 if(ri->request || !rp){
222 /* calculate time delta between request and reply */
223 nstime_delta(&delta, &pinfo->fd->abs_ts, &ri->req_time);
226 && (rp->max.nsecs==0) ){
227 rp->max.secs=delta.secs;
228 rp->max.nsecs=delta.nsecs;
232 && (rp->min.nsecs==0) ){
233 rp->min.secs=delta.secs;
234 rp->min.nsecs=delta.nsecs;
237 if( (delta.secs<rp->min.secs)
238 ||( (delta.secs==rp->min.secs)
239 &&(delta.nsecs<rp->min.nsecs) ) ){
240 rp->min.secs=delta.secs;
241 rp->min.nsecs=delta.nsecs;
244 if( (delta.secs>rp->max.secs)
245 ||( (delta.secs==rp->max.secs)
246 &&(delta.nsecs>rp->max.nsecs) ) ){
247 rp->max.secs=delta.secs;
248 rp->max.nsecs=delta.nsecs;
251 rp->tot.secs += delta.secs;
252 rp->tot.nsecs += delta.nsecs;
253 if(rp->tot.nsecs>NANOSECS_PER_SEC){
254 rp->tot.nsecs-=NANOSECS_PER_SEC;
264 rpcprogs_draw(void *dummy _U_)
270 for(rp=prog_list,i=1;rp;rp=rp->next,i++){
271 /* Ignore procedures with no calls */
276 /* Scale the average SRT in units of 1us and round to the nearest us.
277 tot.secs is a time_t which may be 32 or 64 bits (or even floating)
278 depending on the platform. After casting tot.secs to a 64 bits int, it
279 would take a capture with a duration of over 136 *years* to
280 overflow the secs portion of td. */
281 td = ((guint64)(rp->tot.secs))*NANOSECS_PER_SEC + rp->tot.nsecs;
282 td = ((td / rp->num) + 500) / 1000;
284 g_snprintf(rp->sprogram, sizeof(rp->sprogram), "%s",rpc_prog_name(rp->program));
285 gtk_label_set_text(GTK_LABEL(rp->wprogram), rp->sprogram);
287 g_snprintf(rp->sversion, sizeof(rp->sversion), "%d",rp->version);
288 gtk_label_set_text(GTK_LABEL(rp->wversion), rp->sversion);
290 g_snprintf(rp->snum, sizeof(rp->snum), "%d",rp->num);
291 gtk_label_set_text(GTK_LABEL(rp->wnum), rp->snum);
293 g_snprintf(rp->smin, sizeof(rp->smin), "%3d.%06d",(int)rp->min.secs, (rp->min.nsecs+500)/1000);
294 gtk_label_set_text(GTK_LABEL(rp->wmin), rp->smin);
296 g_snprintf(rp->smax, sizeof(rp->smax), "%3d.%06d",(int)rp->max.secs, (rp->max.nsecs+500)/1000);
297 gtk_label_set_text(GTK_LABEL(rp->wmax), rp->smax);
299 g_snprintf(rp->savg, sizeof(rp->savg), "%3d.%06d",(int)(td/1000000),(int)(td%1000000));
300 gtk_label_set_text(GTK_LABEL(rp->wavg), rp->savg);
305 /* since the gtk2 implementation of tap is multithreaded we must protect
306 * remove_tap_listener() from modifying the list while draw_tap_listener()
307 * is running. the other protected block is in main.c
309 * there should not be any other critical regions in gtk2
312 win_destroy_cb(void *dummy _U_, gpointer data _U_)
314 rpc_program_t *rp, *rp2;
316 protect_thread_critical_region();
317 remove_tap_listener(win);
318 unprotect_thread_critical_region();
321 for(rp=prog_list;rp;){
330 /* When called, this function will start rpcprogs
333 gtk_rpcprogs_init(const char *optarg _U_, void* userdata _U_)
337 GtkWidget *stat_label;
339 GString *error_string;
344 gdk_window_raise(win->window);
348 title_string = rpcprogs_gen_title();
349 win = dlg_window_new(title_string); /* transient_for top_level */
350 gtk_window_set_destroy_with_parent (GTK_WINDOW(win), TRUE);
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);
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);
361 table=gtk_table_new(1, 5, TRUE);
362 gtk_container_add(GTK_CONTAINER(vbox), table);
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);
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);
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);
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);
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);
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);
388 error_string=register_tap_listener("rpc", win, NULL, 0, rpcprogs_reset, rpcprogs_packet, rpcprogs_draw);
390 fprintf(stderr, "wireshark: Couldn't register rpc,programs tap: %s\n",
392 g_string_free(error_string, TRUE);
397 bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
398 gtk_box_pack_start(GTK_BOX(vbox), bbox, FALSE, FALSE, 0);
400 bt_close = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_CLOSE);
401 window_set_cancel_button(win, bt_close, window_cancel_button_cb);
403 g_signal_connect(win, "delete_event", G_CALLBACK(window_delete_event_cb), NULL);
404 g_signal_connect(win, "destroy", G_CALLBACK(win_destroy_cb), win);
406 gtk_widget_show_all(win);
409 cf_retap_packets(&cfile);
410 gdk_window_raise(win->window);
414 gtk_rpcprogs_cb(GtkWidget *w _U_, gpointer d _U_)
416 gtk_rpcprogs_init("",NULL);
420 register_tap_listener_gtkrpcprogs(void)
422 register_stat_cmd_arg("rpc,programs", gtk_rpcprogs_init,NULL);
424 register_stat_menu_item("ONC-RPC Programs", REGISTER_STAT_GROUP_UNSORTED,
425 gtk_rpcprogs_cb, NULL, NULL, NULL);