2 * wsp_stat 2003 Jean-Michel FAYARD
4 * $Id: wsp_stat.c,v 1.3 2003/09/19 07:24:39 guy Exp $
6 * Ethereal - Network traffic analyzer
7 * By Gerald Combs <gerald@ethereal.com>
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 /* #define DEBUG do{ printf("%s:%d ",__FILE__,__LINE__);} while(0); */
32 #include "epan/packet_info.h"
33 #include "simple_dialog.h"
35 #include "../register.h"
36 #include "../globals.h"
37 #include "compat_macros.h"
38 #include "../packet-wsp.h"
41 /* used to keep track of the stats for a specific PDU type*/
42 typedef struct _wsp_pdu_t {
47 /* used to keep track of the statictics for an entire program interface */
48 typedef struct _wsp_stats_t {
54 GtkWidget *table_pdu_types;
55 GtkWidget *table_status_code;
56 guint index; /* Number of status code to display */
58 /* used to keep track of a single type of status code */
59 typedef struct _wsp_status_code_t {
62 GtkWidget *widget;/* label in which we print the number of packets */
63 wspstat_t *sp; /* entire program interface */
66 static GtkWidget *dlg=NULL, *dlg_box;
67 static GtkWidget *filter_box;
68 static GtkWidget *filter_label, *filter_entry;
69 static GtkWidget *start_button;
72 wsp_free_hash( gpointer key, gpointer value, gpointer user_data _U_ )
78 wsp_reset_hash(gchar *key _U_ , wsp_status_code_t *data, gpointer ptr _U_ )
83 /* Update the entry corresponding to the number of packets of a special status code
84 * or create it if it don't exist.
87 wsp_draw_statuscode(gchar *key _U_, wsp_status_code_t *data, gchar * string_buff )
89 if ((data==NULL) || (data->packets==0))
91 if (data->widget==NULL){ /* create an entry in the table */
93 int x = 2*((data->sp->index) % 2);
94 int y = (data->sp->index) /2;
97 /* Maybe we should display the hexadecimal value ? */
98 /* sprintf(string_buff, "%s (0X%x)", data->name, *key); */
99 tmp = gtk_label_new( data->name /* string_buff */ );
100 gtk_table_attach_defaults(GTK_TABLE(data->sp->table_status_code), tmp, x, x+1, y, y+1);
101 gtk_label_set_justify(GTK_LABEL(tmp), GTK_JUSTIFY_LEFT);
102 gtk_widget_show(tmp);
104 sprintf( string_buff, "%9d", data->packets );
105 data->widget = gtk_label_new( string_buff );
106 gtk_table_attach_defaults(GTK_TABLE(data->sp->table_status_code), data->widget, x+1, x+2, y, y+1);
107 gtk_label_set_justify(GTK_LABEL(data->widget), GTK_JUSTIFY_LEFT);
108 gtk_widget_show( data->widget );
112 /* Just update the label string */
113 sprintf( string_buff, "%9d", data->packets );
114 gtk_label_set( GTK_LABEL(data->widget), string_buff);
118 wspstat_reset(void *psp)
123 for(i=1;i<=sp->num_pdus;i++)
125 sp->pdu_stats[i].packets=0;
127 g_hash_table_foreach( sp->hash, (GHFunc)wsp_reset_hash, NULL);
130 pdut2index(gint pdut)
132 if (pdut<=0x09) return pdut;
136 } else if (pdut==0x60||pdut==0x61){
143 index2pdut(gint pdut)
155 wspstat_packet(void *psp, packet_info *pinfo _U_, epan_dissect_t *edt _U_, void *pri)
158 wsp_info_value_t *value=pri;
159 gint index = pdut2index(value->pdut);
162 if (value->status_code != 0) {
163 gint *key=g_malloc( sizeof(gint) );
164 wsp_status_code_t *sc;
165 *key=value->status_code ;
166 sc = g_hash_table_lookup(
170 g_warning("%s:%d What's Wrong, doc ?\n", __FILE__, __LINE__);
171 sc = g_malloc( sizeof(wsp_status_code_t) );
189 sp->pdu_stats[ index ].packets++;
199 wspstat_draw(void *psp)
206 for(i=1;i<=sp->num_pdus ; i++)
208 sprintf(str, "%9d", sp->pdu_stats[i ].packets);
209 gtk_label_set( GTK_LABEL(sp->pdu_stats[i].widget), str);
213 g_hash_table_foreach( sp->hash, (GHFunc) wsp_draw_statuscode, str );
214 if (index != sp->index){
215 /* We have inserted a new entry corresponding to a status code ,
216 * let's resize the table */
217 gtk_table_resize ( GTK_TABLE(sp->table_status_code), sp->index % 2 , 4);
224 /* since the gtk2 implementation of tap is multithreaded we must protect
225 * remove_tap_listener() from modifying the list while draw_tap_listener()
226 * is running. the other protected block is in main.c
228 * there should not be any other critical regions in gtk2
230 void protect_thread_critical_region(void);
231 void unprotect_thread_critical_region(void);
233 win_destroy_cb(GtkWindow *win _U_, gpointer data)
235 wspstat_t *sp=(wspstat_t *)data;
237 protect_thread_critical_region();
238 remove_tap_listener(sp);
239 unprotect_thread_critical_region();
241 g_free(sp->pdu_stats);
243 g_hash_table_foreach( sp->hash, (GHFunc)wsp_free_hash, NULL);
244 g_hash_table_destroy( sp->hash);
249 add_table_entry(wspstat_t *sp, char *str, int x, int y, int index)
253 tmp=gtk_label_new( str );
254 gtk_table_attach_defaults(GTK_TABLE(sp->table_pdu_types), tmp, x, x+1, y, y+1);
255 gtk_label_set_justify(GTK_LABEL(tmp), GTK_JUSTIFY_LEFT);
256 gtk_widget_show(tmp);
258 sp->pdu_stats [index] .widget = GTK_LABEL( tmp ) ;
264 wsp_init_table(wspstat_t *sp)
268 /* gchar buffer[51]; */
270 add_table_entry( sp, "PDU Type " , 0, pos, 0);
271 add_table_entry( sp, "packets " , 1, pos, 0);
272 add_table_entry( sp, "PDU Type " , 2, pos, 0);
273 add_table_entry( sp, "packets " , 3, pos, 0);
275 for (i=1 ; i <= sp->num_pdus ; i++ )
278 if (i> (sp->num_pdus+1) /2 ){
281 /* Maybe we should display the hexadecimal value ? */
282 /* snprintf(buffer, 50, "%s (0X%x)", match_strval( index2pdut( i ), vals_pdu_type), index2pdut(i) );*/
284 match_strval(index2pdut(i), vals_pdu_type), /* or buffer, */
289 add_table_entry( sp, "0", x+1, pos
290 , i /* keep a pointer to this widget to update it in _draw() */
293 if (i== (sp->num_pdus+1) /2) {
299 /* When called, this function will create a new instance of gtk2-wspstat.
302 gtk_wspstat_init(char *optarg)
307 GString *error_string;
308 GtkWidget *main_vb, *pdutypes_fr, *statuscode_fr ;
310 wsp_status_code_t *sc;
313 if (!strncmp (optarg, "wsp,stat,", 9)){
319 sp = g_malloc( sizeof(wspstat_t) );
320 sp->hash = g_hash_table_new( g_int_hash, g_int_equal);
321 for (i=0 ; vals_status[i].strptr ; i++ )
324 sc=g_malloc( sizeof(wsp_status_code_t) );
325 key=g_malloc( sizeof(gint) );
326 sc->name=vals_status[i].strptr;
330 *key=vals_status[i].value;
337 sp->pdu_stats=g_malloc( (sp->num_pdus+1) * sizeof( wsp_pdu_t) );
339 sp->filter=g_malloc(strlen(filter)+1);
340 strcpy(sp->filter,filter);
341 title=g_strdup_printf("WSP Stats with filter: %s", filter);
344 title=g_strdup("WSP Stats");
346 for (i=0;i<=sp->num_pdus; i++)
348 sp->pdu_stats[i].packets=0;
351 sp->win = gtk_window_new( GTK_WINDOW_TOPLEVEL);
352 gtk_window_set_title( GTK_WINDOW(sp->win), title );
354 SIGNAL_CONNECT( sp->win, "destroy", win_destroy_cb, sp);
357 /* container for the two frames */
358 main_vb = gtk_vbox_new(FALSE, 10);
359 gtk_container_border_width(GTK_CONTAINER(main_vb), 10);
360 gtk_container_add(GTK_CONTAINER(sp->win), main_vb);
361 gtk_widget_show(main_vb);
363 /* PDU Types frame */
364 pdutypes_fr = gtk_frame_new("Summary of PDU Types (wsp.pdu_type)");
365 gtk_container_add(GTK_CONTAINER(main_vb), pdutypes_fr);
366 gtk_widget_show(pdutypes_fr);
368 sp->table_pdu_types = gtk_table_new( (sp->num_pdus+1) / 2 + 1, 4, FALSE);
369 gtk_container_add( GTK_CONTAINER( pdutypes_fr), sp->table_pdu_types);
370 gtk_container_set_border_width( GTK_CONTAINER(sp->table_pdu_types) , 10);
373 gtk_widget_show( sp->table_pdu_types );
375 /* Status Codes frame */
376 statuscode_fr = gtk_frame_new("Summary of Status Code (wsp.reply.status)");
377 gtk_container_add(GTK_CONTAINER(main_vb), statuscode_fr);
378 gtk_widget_show(statuscode_fr);
380 sp->table_status_code = gtk_table_new( 0, 4, FALSE);
381 gtk_container_add( GTK_CONTAINER( statuscode_fr), sp->table_status_code);
382 gtk_container_set_border_width( GTK_CONTAINER(sp->table_status_code) , 10);
383 sp->index = 0; /* No answers to display yet */
386 error_string = register_tap_listener(
394 /* error, we failed to attach to the tap. clean up */
395 simple_dialog( ESD_TYPE_WARN, NULL, error_string->str );
396 g_free(sp->pdu_stats);
399 g_string_free(error_string, TRUE);
402 gtk_widget_show_all( sp->win );
403 redissect_packets(&cfile);
409 wspstat_start_button_clicked(GtkWidget *item _U_, gpointer data _U_)
414 filter=(char *)gtk_entry_get_text(GTK_ENTRY(filter_entry));
416 gtk_wspstat_init("wsp,stat,");
418 sprintf(str, "wsp,stat,%s", filter);
419 gtk_wspstat_init(str);
430 gtk_wspstat_cb(GtkWidget *w _U_, gpointer d _U_)
432 /* if the window is already open, bring it to front */
434 gdk_window_raise(dlg->window);
438 dlg=gtk_window_new(GTK_WINDOW_TOPLEVEL);
439 gtk_window_set_title(GTK_WINDOW(dlg), "WSP Statistics");
440 SIGNAL_CONNECT(dlg, "destroy", dlg_destroy_cb, NULL);
441 dlg_box=gtk_vbox_new(FALSE, 0);
442 gtk_container_add(GTK_CONTAINER(dlg), dlg_box);
443 gtk_widget_show(dlg_box);
447 filter_box=gtk_hbox_new(FALSE, 10);
449 gtk_container_set_border_width(GTK_CONTAINER(filter_box), 10);
450 filter_label=gtk_label_new("Filter:");
451 gtk_box_pack_start(GTK_BOX(filter_box), filter_label, FALSE, FALSE, 0);
452 gtk_widget_show(filter_label);
454 filter_entry=gtk_entry_new_with_max_length(250);
455 gtk_box_pack_start(GTK_BOX(filter_box), filter_entry, FALSE, FALSE, 0);
456 gtk_widget_show(filter_entry);
458 gtk_box_pack_start(GTK_BOX(dlg_box), filter_box, TRUE, TRUE, 0);
459 gtk_widget_show(filter_box);
462 /* the start button */
463 start_button=gtk_button_new_with_label("Create Stat");
464 SIGNAL_CONNECT_OBJECT(start_button, "clicked",
465 wspstat_start_button_clicked, NULL);
466 gtk_box_pack_start(GTK_BOX(dlg_box), start_button, TRUE, TRUE, 0);
467 gtk_widget_show(start_button);
469 gtk_widget_show_all(dlg);
474 register_tap_listener_gtkwspstat(void)
476 register_ethereal_tap("wsp,stat,", gtk_wspstat_init);
480 register_tap_menu_gtkwspstat(void)
482 register_tap_menu_item("Statistics/Watch protocol/WAP-WSP",
483 gtk_wspstat_cb, NULL, NULL);