2 * wsp_stat 2003 Jean-Michel FAYARD
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 /* #define DEBUG do{ printf("%s:%d ",__FILE__,__LINE__);} while(0); */
33 #include <epan/packet.h>
34 #include <epan/packet_info.h>
35 #include <epan/epan.h>
37 #include <epan/dissectors/packet-wsp.h>
39 #include "../simple_dialog.h"
40 #include "../register.h"
41 #include "../globals.h"
42 #include "../stat_menu.h"
44 #include "gtk/gui_utils.h"
45 #include "gtk/dlg_utils.h"
46 #include "gtk/tap_dfilter_dlg.h"
50 /* used to keep track of the stats for a specific PDU type*/
51 typedef struct _wsp_pdu_t {
56 /* used to keep track of the statictics for an entire program interface */
57 typedef struct _wsp_stats_t {
63 GtkWidget *table_pdu_types;
64 GtkWidget *table_status_code;
65 guint index; /* Number of status code to display */
67 /* used to keep track of a single type of status code */
68 typedef struct _wsp_status_code_t {
71 GtkWidget *widget;/* label in which we print the number of packets */
72 wspstat_t *sp; /* entire program interface */
76 wsp_free_hash( gpointer key, gpointer value, gpointer user_data _U_ )
82 wsp_reset_hash(gchar *key _U_ , wsp_status_code_t *data, gpointer ptr _U_ )
87 /* Update the entry corresponding to the number of packets of a special status code
88 * or create it if it don't exist.
91 wsp_draw_statuscode(gchar *key _U_, wsp_status_code_t *data, gchar * unused _U_ )
93 char string_buff[256];
95 if ((data==NULL) || (data->packets==0))
97 if (data->widget==NULL){ /* create an entry in the table */
99 int x = 2*((data->sp->index) % 2);
100 int y = (data->sp->index) /2;
103 /* Maybe we should display the hexadecimal value ? */
104 /* g_snprintf(string_buff, sizeof(string_buff), "%s (0X%x)", data->name, *key); */
105 tmp = gtk_label_new( data->name /* string_buff */ );
106 gtk_table_attach_defaults(GTK_TABLE(data->sp->table_status_code), tmp, x, x+1, y, y+1);
107 gtk_label_set_justify(GTK_LABEL(tmp), GTK_JUSTIFY_LEFT);
108 gtk_widget_show(tmp);
110 g_snprintf( string_buff, sizeof(string_buff), "%9d", data->packets );
111 data->widget = gtk_label_new( string_buff );
112 gtk_table_attach_defaults(GTK_TABLE(data->sp->table_status_code), data->widget, x+1, x+2, y, y+1);
113 gtk_label_set_justify(GTK_LABEL(data->widget), GTK_JUSTIFY_LEFT);
114 gtk_widget_show( data->widget );
118 /* Just update the label string */
119 g_snprintf( string_buff, sizeof(string_buff), "%9d", data->packets );
120 gtk_label_set_text( GTK_LABEL(data->widget), string_buff);
124 wspstat_reset(void *psp)
129 for(i=1;i<=sp->num_pdus;i++)
131 sp->pdu_stats[i].packets=0;
133 g_hash_table_foreach( sp->hash, (GHFunc)wsp_reset_hash, NULL);
136 pdut2index(gint pdut)
138 if (pdut<=0x09) return pdut;
142 } else if (pdut==0x60||pdut==0x61){
149 index2pdut(gint pdut)
161 wspstat_packet(void *psp, packet_info *pinfo _U_, epan_dissect_t *edt _U_, const void *pri)
164 const wsp_info_value_t *value=pri;
165 gint index = pdut2index(value->pdut);
168 if (value->status_code != 0) {
169 gint *key=g_malloc( sizeof(gint) );
170 wsp_status_code_t *sc;
171 *key=value->status_code ;
172 sc = g_hash_table_lookup(
176 g_warning("%s:%d What's Wrong, doc ?\n", __FILE__, __LINE__);
177 sc = g_malloc( sizeof(wsp_status_code_t) );
195 sp->pdu_stats[ index ].packets++;
205 wspstat_draw(void *psp)
212 for(i=1;i<=sp->num_pdus ; i++)
214 g_snprintf(str, sizeof(str), "%9d", sp->pdu_stats[i ].packets);
215 gtk_label_set_text( GTK_LABEL(sp->pdu_stats[i].widget), str);
219 g_hash_table_foreach( sp->hash, (GHFunc) wsp_draw_statuscode, NULL );
220 if (index != sp->index){
221 /* We have inserted a new entry corresponding to a status code ,
222 * let's resize the table */
223 gtk_table_resize ( GTK_TABLE(sp->table_status_code), sp->index % 2 , 4);
230 /* since the gtk2 implementation of tap is multithreaded we must protect
231 * remove_tap_listener() from modifying the list while draw_tap_listener()
232 * is running. the other protected block is in main.c
234 * there should not be any other critical regions in gtk2
237 win_destroy_cb(GtkWindow *win _U_, gpointer data)
239 wspstat_t *sp=(wspstat_t *)data;
241 protect_thread_critical_region();
242 remove_tap_listener(sp);
243 unprotect_thread_critical_region();
245 g_free(sp->pdu_stats);
247 g_hash_table_foreach( sp->hash, (GHFunc)wsp_free_hash, NULL);
248 g_hash_table_destroy( sp->hash);
253 add_table_entry(wspstat_t *sp, const char *str, int x, int y, int index)
257 tmp=gtk_label_new( str );
258 gtk_table_attach_defaults(GTK_TABLE(sp->table_pdu_types), tmp, x, x+1, y, y+1);
259 gtk_label_set_justify(GTK_LABEL(tmp), GTK_JUSTIFY_LEFT);
260 gtk_widget_show(tmp);
262 sp->pdu_stats [index] .widget = GTK_LABEL( tmp ) ;
268 wsp_init_table(wspstat_t *sp)
272 /* gchar buffer[51]; */
274 add_table_entry( sp, "PDU Type " , 0, pos, 0);
275 add_table_entry( sp, "packets " , 1, pos, 0);
276 add_table_entry( sp, "PDU Type " , 2, pos, 0);
277 add_table_entry( sp, "packets " , 3, pos, 0);
279 for (i=1 ; i <= sp->num_pdus ; i++ )
282 if (i> (sp->num_pdus+1) /2 ){
285 /* Maybe we should display the hexadecimal value ? */
286 /* g_snprintf(buffer, sizeof(buffer), "%s (0X%x)", match_strval( index2pdut( i ), vals_pdu_type), index2pdut(i) );*/
288 match_strval(index2pdut(i), vals_pdu_type), /* or buffer, */
293 add_table_entry( sp, "0", x+1, pos
294 , i /* keep a pointer to this widget to update it in _draw() */
297 if (i== (sp->num_pdus+1) /2) {
303 /* When called, this function will create a new instance of gtk2-wspstat.
306 gtk_wspstat_init(const char *optarg, void *userdata _U_)
309 const char *filter=NULL;
311 GString *error_string;
312 GtkWidget *main_vb, *pdutypes_fr, *statuscode_fr ;
316 wsp_status_code_t *sc;
319 if (strncmp (optarg, "wsp,stat,", 9) == 0){
325 sp = g_malloc( sizeof(wspstat_t) );
326 sp->win = window_new(GTK_WINDOW_TOPLEVEL, "wsp-stat");
327 sp->hash = g_hash_table_new( g_int_hash, g_int_equal);
328 for (i=0 ; vals_status[i].strptr ; i++ )
331 sc=g_malloc( sizeof(wsp_status_code_t) );
332 key=g_malloc( sizeof(gint) );
333 sc->name=vals_status[i].strptr;
337 *key=vals_status[i].value;
344 sp->pdu_stats=g_malloc( (sp->num_pdus+1) * sizeof( wsp_pdu_t) );
346 sp->filter=g_strdup(filter);
347 title=g_strdup_printf("Wireshark: WAP-WSP statistics with filter: %s", filter);
350 title=g_strdup("Wireshark: WAP-WSP statistics");
352 for (i=0;i<=sp->num_pdus; i++)
354 sp->pdu_stats[i].packets=0;
357 gtk_window_set_title(GTK_WINDOW(sp->win), title);
360 /* container for the two frames */
361 main_vb = gtk_vbox_new(FALSE, 3);
362 gtk_container_set_border_width(GTK_CONTAINER(main_vb), 12);
363 gtk_container_add(GTK_CONTAINER(sp->win), main_vb);
365 /* PDU Types frame */
366 pdutypes_fr = gtk_frame_new("Summary of PDU Types (wsp.pdu_type)");
367 gtk_container_add(GTK_CONTAINER(main_vb), pdutypes_fr);
369 sp->table_pdu_types = gtk_table_new( (sp->num_pdus+1) / 2 + 1, 4, FALSE);
370 gtk_container_add( GTK_CONTAINER( pdutypes_fr), sp->table_pdu_types);
371 gtk_container_set_border_width( GTK_CONTAINER(sp->table_pdu_types) , 10);
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);
379 sp->table_status_code = gtk_table_new( 0, 4, FALSE);
380 gtk_container_add( GTK_CONTAINER( statuscode_fr), sp->table_status_code);
381 gtk_container_set_border_width( GTK_CONTAINER(sp->table_status_code) , 10);
382 sp->index = 0; /* No answers to display yet */
384 error_string = register_tap_listener(
392 /* error, we failed to attach to the tap. clean up */
393 simple_dialog( ESD_TYPE_ERROR, ESD_BTN_OK, "%s", error_string->str );
394 g_free(sp->pdu_stats);
397 g_string_free(error_string, TRUE);
402 bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
403 gtk_box_pack_start(GTK_BOX(main_vb), bbox, FALSE, FALSE, 0);
405 bt_close = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_CLOSE);
406 window_set_cancel_button(sp->win, bt_close, window_cancel_button_cb);
408 g_signal_connect(sp->win, "delete_event", G_CALLBACK(window_delete_event_cb), NULL);
409 g_signal_connect(sp->win, "destroy", G_CALLBACK(win_destroy_cb), sp);
411 gtk_widget_show_all(sp->win);
412 window_present(sp->win);
414 cf_retap_packets(&cfile, FALSE);
415 gdk_window_raise(sp->win->window);
418 static tap_dfilter_dlg wsp_stat_dlg = {
419 "WAP-WSP Packet Counter",
426 register_tap_listener_gtkwspstat(void)
428 register_dfilter_stat(&wsp_stat_dlg, "WAP-WSP",
429 REGISTER_STAT_GROUP_TELEPHONY);