2 * wsp_stat 2003 Jean-Michel FAYARD
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); */
34 #include <epan/packet_info.h>
35 #include <epan/epan.h>
38 #include "simple_dialog.h"
40 #include "dlg_utils.h"
42 #include "../register.h"
43 #include "../globals.h"
44 #include "compat_macros.h"
45 #include <epan/dissectors/packet-wsp.h>
46 #include "../tap_dfilter_dlg.h"
47 #include "tap_dfilter_dlg.h"
49 /* used to keep track of the stats for a specific PDU type*/
50 typedef struct _wsp_pdu_t {
55 /* used to keep track of the statictics for an entire program interface */
56 typedef struct _wsp_stats_t {
62 GtkWidget *table_pdu_types;
63 GtkWidget *table_status_code;
64 guint index; /* Number of status code to display */
66 /* used to keep track of a single type of status code */
67 typedef struct _wsp_status_code_t {
70 GtkWidget *widget;/* label in which we print the number of packets */
71 wspstat_t *sp; /* entire program interface */
75 wsp_free_hash( gpointer key, gpointer value, gpointer user_data _U_ )
81 wsp_reset_hash(gchar *key _U_ , wsp_status_code_t *data, gpointer ptr _U_ )
86 /* Update the entry corresponding to the number of packets of a special status code
87 * or create it if it don't exist.
90 wsp_draw_statuscode(gchar *key _U_, wsp_status_code_t *data, gchar * unused _U_ )
92 char string_buff[256];
94 if ((data==NULL) || (data->packets==0))
96 if (data->widget==NULL){ /* create an entry in the table */
98 int x = 2*((data->sp->index) % 2);
99 int y = (data->sp->index) /2;
102 /* Maybe we should display the hexadecimal value ? */
103 /* g_snprintf(string_buff, 256, "%s (0X%x)", data->name, *key); */
104 tmp = gtk_label_new( data->name /* string_buff */ );
105 gtk_table_attach_defaults(GTK_TABLE(data->sp->table_status_code), tmp, x, x+1, y, y+1);
106 gtk_label_set_justify(GTK_LABEL(tmp), GTK_JUSTIFY_LEFT);
107 gtk_widget_show(tmp);
109 g_snprintf( string_buff, 256, "%9d", data->packets );
110 data->widget = gtk_label_new( string_buff );
111 gtk_table_attach_defaults(GTK_TABLE(data->sp->table_status_code), data->widget, x+1, x+2, y, y+1);
112 gtk_label_set_justify(GTK_LABEL(data->widget), GTK_JUSTIFY_LEFT);
113 gtk_widget_show( data->widget );
117 /* Just update the label string */
118 g_snprintf( string_buff, 256, "%9d", data->packets );
119 gtk_label_set( GTK_LABEL(data->widget), string_buff);
123 wspstat_reset(void *psp)
128 for(i=1;i<=sp->num_pdus;i++)
130 sp->pdu_stats[i].packets=0;
132 g_hash_table_foreach( sp->hash, (GHFunc)wsp_reset_hash, NULL);
135 pdut2index(gint pdut)
137 if (pdut<=0x09) return pdut;
141 } else if (pdut==0x60||pdut==0x61){
148 index2pdut(gint pdut)
160 wspstat_packet(void *psp, packet_info *pinfo _U_, epan_dissect_t *edt _U_, const void *pri)
163 const wsp_info_value_t *value=pri;
164 gint index = pdut2index(value->pdut);
167 if (value->status_code != 0) {
168 gint *key=g_malloc( sizeof(gint) );
169 wsp_status_code_t *sc;
170 *key=value->status_code ;
171 sc = g_hash_table_lookup(
175 g_warning("%s:%d What's Wrong, doc ?\n", __FILE__, __LINE__);
176 sc = g_malloc( sizeof(wsp_status_code_t) );
194 sp->pdu_stats[ index ].packets++;
204 wspstat_draw(void *psp)
211 for(i=1;i<=sp->num_pdus ; i++)
213 g_snprintf(str, 256, "%9d", sp->pdu_stats[i ].packets);
214 gtk_label_set( GTK_LABEL(sp->pdu_stats[i].widget), str);
218 g_hash_table_foreach( sp->hash, (GHFunc) wsp_draw_statuscode, NULL );
219 if (index != sp->index){
220 /* We have inserted a new entry corresponding to a status code ,
221 * let's resize the table */
222 gtk_table_resize ( GTK_TABLE(sp->table_status_code), sp->index % 2 , 4);
229 /* since the gtk2 implementation of tap is multithreaded we must protect
230 * remove_tap_listener() from modifying the list while draw_tap_listener()
231 * is running. the other protected block is in main.c
233 * there should not be any other critical regions in gtk2
235 void protect_thread_critical_region(void);
236 void unprotect_thread_critical_region(void);
238 win_destroy_cb(GtkWindow *win _U_, gpointer data)
240 wspstat_t *sp=(wspstat_t *)data;
242 protect_thread_critical_region();
243 remove_tap_listener(sp);
244 unprotect_thread_critical_region();
246 g_free(sp->pdu_stats);
248 g_hash_table_foreach( sp->hash, (GHFunc)wsp_free_hash, NULL);
249 g_hash_table_destroy( sp->hash);
254 add_table_entry(wspstat_t *sp, const char *str, int x, int y, int index)
258 tmp=gtk_label_new( str );
259 gtk_table_attach_defaults(GTK_TABLE(sp->table_pdu_types), tmp, x, x+1, y, y+1);
260 gtk_label_set_justify(GTK_LABEL(tmp), GTK_JUSTIFY_LEFT);
261 gtk_widget_show(tmp);
263 sp->pdu_stats [index] .widget = GTK_LABEL( tmp ) ;
269 wsp_init_table(wspstat_t *sp)
273 /* gchar buffer[51]; */
275 add_table_entry( sp, "PDU Type " , 0, pos, 0);
276 add_table_entry( sp, "packets " , 1, pos, 0);
277 add_table_entry( sp, "PDU Type " , 2, pos, 0);
278 add_table_entry( sp, "packets " , 3, pos, 0);
280 for (i=1 ; i <= sp->num_pdus ; i++ )
283 if (i> (sp->num_pdus+1) /2 ){
286 /* Maybe we should display the hexadecimal value ? */
287 /* g_snprintf(buffer, 50, "%s (0X%x)", match_strval( index2pdut( i ), vals_pdu_type), index2pdut(i) );*/
289 match_strval(index2pdut(i), vals_pdu_type), /* or buffer, */
294 add_table_entry( sp, "0", x+1, pos
295 , i /* keep a pointer to this widget to update it in _draw() */
298 if (i== (sp->num_pdus+1) /2) {
304 /* When called, this function will create a new instance of gtk2-wspstat.
307 gtk_wspstat_init(char *optarg)
312 GString *error_string;
313 GtkWidget *main_vb, *pdutypes_fr, *statuscode_fr ;
317 wsp_status_code_t *sc;
320 if (strncmp (optarg, "wsp,stat,", 9) == 0){
326 sp = g_malloc( sizeof(wspstat_t) );
327 sp->win = window_new(GTK_WINDOW_TOPLEVEL, "wsp-stat");
328 sp->hash = g_hash_table_new( g_int_hash, g_int_equal);
329 for (i=0 ; vals_status[i].strptr ; i++ )
332 sc=g_malloc( sizeof(wsp_status_code_t) );
333 key=g_malloc( sizeof(gint) );
334 sc->name=vals_status[i].strptr;
338 *key=vals_status[i].value;
345 sp->pdu_stats=g_malloc( (sp->num_pdus+1) * sizeof( wsp_pdu_t) );
347 sp->filter=g_strdup(filter);
348 title=g_strdup_printf("Ethereal: WAP-WSP statistics with filter: %s", filter);
351 title=g_strdup("Ethereal: WAP-WSP statistics");
353 for (i=0;i<=sp->num_pdus; i++)
355 sp->pdu_stats[i].packets=0;
358 gtk_window_set_title(GTK_WINDOW(sp->win), title);
361 /* container for the two frames */
362 main_vb = gtk_vbox_new(FALSE, 3);
363 gtk_container_border_width(GTK_CONTAINER(main_vb), 12);
364 gtk_container_add(GTK_CONTAINER(sp->win), main_vb);
366 /* PDU Types frame */
367 pdutypes_fr = gtk_frame_new("Summary of PDU Types (wsp.pdu_type)");
368 gtk_container_add(GTK_CONTAINER(main_vb), pdutypes_fr);
370 sp->table_pdu_types = gtk_table_new( (sp->num_pdus+1) / 2 + 1, 4, FALSE);
371 gtk_container_add( GTK_CONTAINER( pdutypes_fr), sp->table_pdu_types);
372 gtk_container_set_border_width( GTK_CONTAINER(sp->table_pdu_types) , 10);
376 /* Status Codes frame */
377 statuscode_fr = gtk_frame_new("Summary of Status Code (wsp.reply.status)");
378 gtk_container_add(GTK_CONTAINER(main_vb), 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 */
385 error_string = register_tap_listener(
393 /* error, we failed to attach to the tap. clean up */
394 simple_dialog( ESD_TYPE_ERROR, ESD_BTN_OK, error_string->str );
395 g_free(sp->pdu_stats);
398 g_string_free(error_string, TRUE);
403 bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
404 gtk_box_pack_start(GTK_BOX(main_vb), bbox, FALSE, FALSE, 0);
406 bt_close = OBJECT_GET_DATA(bbox, GTK_STOCK_CLOSE);
407 window_set_cancel_button(sp->win, bt_close, window_cancel_button_cb);
409 SIGNAL_CONNECT(sp->win, "delete_event", window_delete_event_cb, NULL);
410 SIGNAL_CONNECT(sp->win, "destroy", win_destroy_cb, sp);
412 gtk_widget_show_all(sp->win);
413 window_present(sp->win);
415 cf_retap_packets(&cfile);
418 static tap_dfilter_dlg wsp_stat_dlg = {
419 "WAP-WSP Packet Counter",
426 register_tap_listener_gtkwspstat(void)
428 register_tap_listener_cmd_arg("wsp,stat", gtk_wspstat_init);
430 register_tap_menu_item("WAP-WSP...", REGISTER_TAP_GROUP_TELEPHONY,
431 gtk_tap_dfilter_dlg_cb, NULL, NULL, &(wsp_stat_dlg));