06810d7e477c3254de766ad2ad628209f88c9f36
[obnox/wireshark/wip.git] / gtk / wsp_stat.c
1 /* wsp_stat.c
2  * wsp_stat   2003 Jean-Michel FAYARD
3  *
4  * $Id$
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
8  * Copyright 1998 Gerald Combs
9  * 
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.
14  * 
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.
19  * 
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.
23  */
24
25 /* #define DEBUG        do{ printf("%s:%d  ",__FILE__,__LINE__);} while(0); */
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <string.h>
31
32 #include <gtk/gtk.h>
33
34 #include <epan/packet_info.h>
35 #include <epan/epan.h>
36
37 #include "tap_menu.h"
38 #include "simple_dialog.h"
39 #include "ui_util.h"
40 #include "dlg_utils.h"
41 #include <epan/tap.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"
48
49 /* used to keep track of the stats for a specific PDU type*/
50 typedef struct _wsp_pdu_t {
51         GtkLabel        *widget;
52         guint32          packets;
53 } wsp_pdu_t;
54
55 /* used to keep track of the statictics for an entire program interface */
56 typedef struct _wsp_stats_t {
57         char            *filter;
58         wsp_pdu_t       *pdu_stats;
59         guint32          num_pdus;
60         GtkWidget       *win;
61         GHashTable      *hash;
62         GtkWidget       *table_pdu_types;       
63         GtkWidget       *table_status_code;
64         guint            index; /* Number of status code to display */
65 } wspstat_t;
66 /* used to keep track of a single type of status code */
67 typedef struct _wsp_status_code_t {
68         const gchar     *name;
69         guint32          packets;
70         GtkWidget       *widget;/* label in which we print the number of packets */
71         wspstat_t       *sp;    /* entire program interface */
72 } wsp_status_code_t;
73
74 static void
75 wsp_free_hash( gpointer key, gpointer value, gpointer user_data _U_ )
76 {
77         g_free(key);
78         g_free(value);
79 }
80 static void
81 wsp_reset_hash(gchar *key _U_ , wsp_status_code_t *data, gpointer ptr _U_ ) 
82 {       
83         data->packets = 0;
84 }
85
86 /* Update the entry corresponding to the number of packets of a special status code
87  * or create it if it don't exist.
88  */
89 static void
90 wsp_draw_statuscode(gchar *key _U_, wsp_status_code_t *data, gchar * unused _U_ )
91 {
92         char string_buff[256];
93
94         if ((data==NULL) || (data->packets==0))
95                 return;
96         if (data->widget==NULL){        /* create an entry in the table */
97                 GtkWidget       *tmp;
98                 int x = 2*((data->sp->index) % 2);
99                 int y = (data->sp->index) /2;
100
101
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);
108
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 );
114
115                 data->sp->index++;
116         } else {
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);
120         }
121 }
122 static void
123 wspstat_reset(void *psp)
124 {
125         wspstat_t *sp=psp;
126         guint32 i;
127
128         for(i=1;i<=sp->num_pdus;i++)
129         {
130                 sp->pdu_stats[i].packets=0;
131         }
132         g_hash_table_foreach( sp->hash, (GHFunc)wsp_reset_hash, NULL);  
133 }
134 static gint 
135 pdut2index(gint pdut)
136 {
137         if (pdut<=0x09)         return pdut;
138         if (pdut>=0x40){
139                 if (pdut <= 0x44){
140                         return pdut-54;
141                 } else if (pdut==0x60||pdut==0x61){
142                         return pdut-81;
143                 }
144         }
145         return 0;
146 }
147 static gint
148 index2pdut(gint pdut)
149 {
150         if (pdut<=0x09)
151                 return pdut;
152         if (pdut<=14)
153                 return pdut+54;
154         if (pdut<=16)
155                 return pdut+81;
156         return 0;
157 }
158
159 static int
160 wspstat_packet(void *psp, packet_info *pinfo _U_, epan_dissect_t *edt _U_, const void *pri)
161 {
162         wspstat_t *sp=psp;
163         const wsp_info_value_t *value=pri;
164         gint index = pdut2index(value->pdut);
165         int retour=0;
166
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( 
172                                 sp->hash, 
173                                 key);
174                 if (!sc) {
175                         g_warning("%s:%d What's Wrong, doc ?\n", __FILE__, __LINE__);
176                         sc = g_malloc( sizeof(wsp_status_code_t) );
177                         sc -> packets = 1;
178                         sc -> name = NULL;
179                         sc -> widget=NULL;
180                         sc -> sp = sp;
181                         g_hash_table_insert(
182                                 sp->hash,
183                                 key,
184                                 sc);
185                 } else {
186                         sc->packets++;
187                 }
188                 retour=1;
189         }
190
191                 
192
193         if (index!=0) {
194                 sp->pdu_stats[ index ].packets++;
195                 retour = 1;
196         }
197         return retour;
198
199 }
200
201
202
203 static void
204 wspstat_draw(void *psp)
205 {
206         wspstat_t *sp=psp;
207         guint32 i;
208         char str[256];
209         guint index;
210
211         for(i=1;i<=sp->num_pdus ; i++)
212         {
213                 g_snprintf(str, 256, "%9d",  sp->pdu_stats[i ].packets);
214                 gtk_label_set( GTK_LABEL(sp->pdu_stats[i].widget), str);
215         }
216
217         index=sp->index;
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);
223         }
224         
225 }
226
227
228
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
232  *
233  * there should not be any other critical regions in gtk2
234  */
235 void protect_thread_critical_region(void);
236 void unprotect_thread_critical_region(void);
237 static void
238 win_destroy_cb(GtkWindow *win _U_, gpointer data)
239 {
240         wspstat_t *sp=(wspstat_t *)data;
241
242         protect_thread_critical_region();
243         remove_tap_listener(sp);
244         unprotect_thread_critical_region();
245
246         g_free(sp->pdu_stats);
247         g_free(sp->filter);
248         g_hash_table_foreach( sp->hash, (GHFunc)wsp_free_hash, NULL);
249         g_hash_table_destroy( sp->hash);        
250         g_free(sp);
251 }
252
253 static void
254 add_table_entry(wspstat_t *sp, const char *str, int x, int y, int index)
255 {
256         GtkWidget *tmp;
257
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);
262         if (index != 0) {
263                 sp->pdu_stats [index] .widget = GTK_LABEL( tmp ) ;
264         }
265 }
266
267
268 static void
269 wsp_init_table(wspstat_t *sp)
270 {
271         int pos=0;
272         guint32 i;
273         /* gchar        buffer[51];     */
274         
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);
279         pos++;
280         for (i=1 ; i <= sp->num_pdus ; i++ )
281         {
282                 int x = 0;
283                 if (i> (sp->num_pdus+1) /2 ){
284                         x=2;
285                 }
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) );*/
288                 add_table_entry( sp, 
289                                 match_strval(index2pdut(i), vals_pdu_type), /* or buffer, */
290                                 x,
291                                 pos,
292                                 0
293                                 );
294                 add_table_entry( sp, "0", x+1, pos
295                                 , i /* keep a pointer to this widget to update it in _draw() */
296                                 );      
297                 pos++;
298                 if (i== (sp->num_pdus+1) /2) {
299                         pos=1;
300                 }
301         }
302 }
303
304 /* When called, this function will create a new instance of gtk2-wspstat.
305  */
306 static void
307 gtk_wspstat_init(char *optarg)
308 {
309         wspstat_t *sp;
310         char            *filter=NULL;
311         char            *title=NULL;
312         GString         *error_string;
313         GtkWidget       *main_vb, *pdutypes_fr, *statuscode_fr ;
314         GtkWidget       *bt_close;
315         GtkWidget       *bbox;
316         guint32          i;
317         wsp_status_code_t *sc;
318         
319         
320         if (strncmp (optarg, "wsp,stat,", 9) == 0){
321                 filter=optarg+9;
322         } else {
323                 filter=NULL;
324         }
325         
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++ )
330         {
331                 gint *key;
332                 sc=g_malloc( sizeof(wsp_status_code_t) );
333                 key=g_malloc( sizeof(gint) );
334                 sc->name=vals_status[i].strptr;
335                 sc->packets=0;
336                 sc->widget=NULL;
337                 sc->sp = sp;
338                 *key=vals_status[i].value;
339                 g_hash_table_insert(
340                                 sp->hash,
341                                 key,
342                                 sc);
343         }
344         sp->num_pdus = 16;
345         sp->pdu_stats=g_malloc( (sp->num_pdus+1) * sizeof( wsp_pdu_t) );
346         if(filter){
347                 sp->filter=g_strdup(filter);
348                 title=g_strdup_printf("Ethereal: WAP-WSP statistics with filter: %s", filter);
349         } else {
350                 sp->filter=NULL;
351                 title=g_strdup("Ethereal: WAP-WSP statistics");
352         }
353         for (i=0;i<=sp->num_pdus; i++)
354         {
355                 sp->pdu_stats[i].packets=0;
356         }
357
358         gtk_window_set_title(GTK_WINDOW(sp->win), title);
359         g_free(title);
360
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);
365
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);
369         
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);
373
374         wsp_init_table(sp);
375
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);
379         
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 */
384
385         error_string = register_tap_listener( 
386                         "wsp",
387                         sp,
388                         filter,
389                         wspstat_reset,
390                         wspstat_packet,
391                         wspstat_draw);
392         if (error_string){
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);
396                 g_free(sp->filter);
397                 g_free(sp);
398                 g_string_free(error_string, TRUE);
399                 return ;
400         }
401
402         /* Button row. */
403     bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
404     gtk_box_pack_start(GTK_BOX(main_vb), bbox, FALSE, FALSE, 0);
405
406     bt_close = OBJECT_GET_DATA(bbox, GTK_STOCK_CLOSE);
407     window_set_cancel_button(sp->win, bt_close, window_cancel_button_cb);
408
409     SIGNAL_CONNECT(sp->win, "delete_event", window_delete_event_cb, NULL);
410         SIGNAL_CONNECT(sp->win, "destroy", win_destroy_cb, sp);
411
412     gtk_widget_show_all(sp->win);
413     window_present(sp->win);
414         
415     cf_retap_packets(&cfile);
416 }
417
418 static tap_dfilter_dlg wsp_stat_dlg = {
419         "WAP-WSP Packet Counter",
420         "wsp,stat",
421         gtk_wspstat_init,
422         -1
423 };
424
425 void
426 register_tap_listener_gtkwspstat(void)
427 {
428         register_tap_listener_cmd_arg("wsp,stat", gtk_wspstat_init);
429
430         register_tap_menu_item("WAP-WSP...", REGISTER_TAP_GROUP_TELEPHONY,
431             gtk_tap_dfilter_dlg_cb, NULL, NULL, &(wsp_stat_dlg));
432 }