Add userdata arguments to a bunch of stat initialization routines to
[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 "simple_dialog.h"
38 #include "gui_utils.h"
39 #include "dlg_utils.h"
40 #include <epan/tap.h>
41 #include "../register.h"
42 #include "../globals.h"
43 #include "compat_macros.h"
44 #include <epan/dissectors/packet-wsp.h>
45 #include "../stat_menu.h"
46 #include "../tap_dfilter_dlg.h"
47
48 /* used to keep track of the stats for a specific PDU type*/
49 typedef struct _wsp_pdu_t {
50         GtkLabel        *widget;
51         guint32          packets;
52 } wsp_pdu_t;
53
54 /* used to keep track of the statictics for an entire program interface */
55 typedef struct _wsp_stats_t {
56         char            *filter;
57         wsp_pdu_t       *pdu_stats;
58         guint32          num_pdus;
59         GtkWidget       *win;
60         GHashTable      *hash;
61         GtkWidget       *table_pdu_types;       
62         GtkWidget       *table_status_code;
63         guint            index; /* Number of status code to display */
64 } wspstat_t;
65 /* used to keep track of a single type of status code */
66 typedef struct _wsp_status_code_t {
67         const gchar     *name;
68         guint32          packets;
69         GtkWidget       *widget;/* label in which we print the number of packets */
70         wspstat_t       *sp;    /* entire program interface */
71 } wsp_status_code_t;
72
73 static void
74 wsp_free_hash( gpointer key, gpointer value, gpointer user_data _U_ )
75 {
76         g_free(key);
77         g_free(value);
78 }
79 static void
80 wsp_reset_hash(gchar *key _U_ , wsp_status_code_t *data, gpointer ptr _U_ ) 
81 {       
82         data->packets = 0;
83 }
84
85 /* Update the entry corresponding to the number of packets of a special status code
86  * or create it if it don't exist.
87  */
88 static void
89 wsp_draw_statuscode(gchar *key _U_, wsp_status_code_t *data, gchar * unused _U_ )
90 {
91         char string_buff[256];
92
93         if ((data==NULL) || (data->packets==0))
94                 return;
95         if (data->widget==NULL){        /* create an entry in the table */
96                 GtkWidget       *tmp;
97                 int x = 2*((data->sp->index) % 2);
98                 int y = (data->sp->index) /2;
99
100
101                 /* Maybe we should display the hexadecimal value ? */
102                 /* g_snprintf(string_buff, 256, "%s  (0X%x)", data->name, *key); */
103                 tmp = gtk_label_new( data->name  /* string_buff */ );
104                 gtk_table_attach_defaults(GTK_TABLE(data->sp->table_status_code), tmp, x, x+1, y, y+1);
105                 gtk_label_set_justify(GTK_LABEL(tmp), GTK_JUSTIFY_LEFT);
106                 gtk_widget_show(tmp);
107
108                 g_snprintf( string_buff, 256, "%9d", data->packets );
109                 data->widget = gtk_label_new( string_buff );
110                 gtk_table_attach_defaults(GTK_TABLE(data->sp->table_status_code), data->widget, x+1, x+2, y, y+1);
111                 gtk_label_set_justify(GTK_LABEL(data->widget), GTK_JUSTIFY_LEFT);
112                 gtk_widget_show( data->widget );
113
114                 data->sp->index++;
115         } else {
116                 /* Just update the label string */
117                 g_snprintf( string_buff, 256, "%9d", data->packets );
118                 gtk_label_set( GTK_LABEL(data->widget), string_buff);
119         }
120 }
121 static void
122 wspstat_reset(void *psp)
123 {
124         wspstat_t *sp=psp;
125         guint32 i;
126
127         for(i=1;i<=sp->num_pdus;i++)
128         {
129                 sp->pdu_stats[i].packets=0;
130         }
131         g_hash_table_foreach( sp->hash, (GHFunc)wsp_reset_hash, NULL);  
132 }
133 static gint 
134 pdut2index(gint pdut)
135 {
136         if (pdut<=0x09)         return pdut;
137         if (pdut>=0x40){
138                 if (pdut <= 0x44){
139                         return pdut-54;
140                 } else if (pdut==0x60||pdut==0x61){
141                         return pdut-81;
142                 }
143         }
144         return 0;
145 }
146 static gint
147 index2pdut(gint pdut)
148 {
149         if (pdut<=0x09)
150                 return pdut;
151         if (pdut<=14)
152                 return pdut+54;
153         if (pdut<=16)
154                 return pdut+81;
155         return 0;
156 }
157
158 static int
159 wspstat_packet(void *psp, packet_info *pinfo _U_, epan_dissect_t *edt _U_, const void *pri)
160 {
161         wspstat_t *sp=psp;
162         const wsp_info_value_t *value=pri;
163         gint index = pdut2index(value->pdut);
164         int retour=0;
165
166         if (value->status_code != 0) {
167                 gint *key=g_malloc( sizeof(gint) );
168                 wsp_status_code_t *sc;
169                 *key=value->status_code ;
170                 sc = g_hash_table_lookup( 
171                                 sp->hash, 
172                                 key);
173                 if (!sc) {
174                         g_warning("%s:%d What's Wrong, doc ?\n", __FILE__, __LINE__);
175                         sc = g_malloc( sizeof(wsp_status_code_t) );
176                         sc -> packets = 1;
177                         sc -> name = NULL;
178                         sc -> widget=NULL;
179                         sc -> sp = sp;
180                         g_hash_table_insert(
181                                 sp->hash,
182                                 key,
183                                 sc);
184                 } else {
185                         sc->packets++;
186                 }
187                 retour=1;
188         }
189
190                 
191
192         if (index!=0) {
193                 sp->pdu_stats[ index ].packets++;
194                 retour = 1;
195         }
196         return retour;
197
198 }
199
200
201
202 static void
203 wspstat_draw(void *psp)
204 {
205         wspstat_t *sp=psp;
206         guint32 i;
207         char str[256];
208         guint index;
209
210         for(i=1;i<=sp->num_pdus ; i++)
211         {
212                 g_snprintf(str, 256, "%9d",  sp->pdu_stats[i ].packets);
213                 gtk_label_set( GTK_LABEL(sp->pdu_stats[i].widget), str);
214         }
215
216         index=sp->index;
217         g_hash_table_foreach( sp->hash, (GHFunc) wsp_draw_statuscode, NULL );
218         if (index != sp->index){
219                 /* We have inserted a new entry corresponding to a status code ,
220                  * let's resize the table */
221                 gtk_table_resize ( GTK_TABLE(sp->table_status_code), sp->index  % 2 , 4);
222         }
223         
224 }
225
226
227
228 /* since the gtk2 implementation of tap is multithreaded we must protect
229  * remove_tap_listener() from modifying the list while draw_tap_listener()
230  * is running.  the other protected block is in main.c
231  *
232  * there should not be any other critical regions in gtk2
233  */
234 void protect_thread_critical_region(void);
235 void unprotect_thread_critical_region(void);
236 static void
237 win_destroy_cb(GtkWindow *win _U_, gpointer data)
238 {
239         wspstat_t *sp=(wspstat_t *)data;
240
241         protect_thread_critical_region();
242         remove_tap_listener(sp);
243         unprotect_thread_critical_region();
244
245         g_free(sp->pdu_stats);
246         g_free(sp->filter);
247         g_hash_table_foreach( sp->hash, (GHFunc)wsp_free_hash, NULL);
248         g_hash_table_destroy( sp->hash);        
249         g_free(sp);
250 }
251
252 static void
253 add_table_entry(wspstat_t *sp, const char *str, int x, int y, int index)
254 {
255         GtkWidget *tmp;
256
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);
261         if (index != 0) {
262                 sp->pdu_stats [index] .widget = GTK_LABEL( tmp ) ;
263         }
264 }
265
266
267 static void
268 wsp_init_table(wspstat_t *sp)
269 {
270         int pos=0;
271         guint32 i;
272         /* gchar        buffer[51];     */
273         
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);
278         pos++;
279         for (i=1 ; i <= sp->num_pdus ; i++ )
280         {
281                 int x = 0;
282                 if (i> (sp->num_pdus+1) /2 ){
283                         x=2;
284                 }
285                 /* Maybe we should display the hexadecimal value ? */
286                 /* g_snprintf(buffer, 50, "%s  (0X%x)", match_strval( index2pdut( i ), vals_pdu_type), index2pdut(i) );*/
287                 add_table_entry( sp, 
288                                 match_strval(index2pdut(i), vals_pdu_type), /* or buffer, */
289                                 x,
290                                 pos,
291                                 0
292                                 );
293                 add_table_entry( sp, "0", x+1, pos
294                                 , i /* keep a pointer to this widget to update it in _draw() */
295                                 );      
296                 pos++;
297                 if (i== (sp->num_pdus+1) /2) {
298                         pos=1;
299                 }
300         }
301 }
302
303 /* When called, this function will create a new instance of gtk2-wspstat.
304  */
305 static void
306 gtk_wspstat_init(const char *optarg, void *userdata _U_)
307 {
308         wspstat_t *sp;
309         const char      *filter=NULL;
310         char            *title=NULL;
311         GString         *error_string;
312         GtkWidget       *main_vb, *pdutypes_fr, *statuscode_fr ;
313         GtkWidget       *bt_close;
314         GtkWidget       *bbox;
315         guint32          i;
316         wsp_status_code_t *sc;
317         
318         
319         if (strncmp (optarg, "wsp,stat,", 9) == 0){
320                 filter=optarg+9;
321         } else {
322                 filter=NULL;
323         }
324         
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++ )
329         {
330                 gint *key;
331                 sc=g_malloc( sizeof(wsp_status_code_t) );
332                 key=g_malloc( sizeof(gint) );
333                 sc->name=vals_status[i].strptr;
334                 sc->packets=0;
335                 sc->widget=NULL;
336                 sc->sp = sp;
337                 *key=vals_status[i].value;
338                 g_hash_table_insert(
339                                 sp->hash,
340                                 key,
341                                 sc);
342         }
343         sp->num_pdus = 16;
344         sp->pdu_stats=g_malloc( (sp->num_pdus+1) * sizeof( wsp_pdu_t) );
345         if(filter){
346                 sp->filter=g_strdup(filter);
347                 title=g_strdup_printf("Ethereal: WAP-WSP statistics with filter: %s", filter);
348         } else {
349                 sp->filter=NULL;
350                 title=g_strdup("Ethereal: WAP-WSP statistics");
351         }
352         for (i=0;i<=sp->num_pdus; i++)
353         {
354                 sp->pdu_stats[i].packets=0;
355         }
356
357         gtk_window_set_title(GTK_WINDOW(sp->win), title);
358         g_free(title);
359
360         /* container for the two frames */
361         main_vb = gtk_vbox_new(FALSE, 3);
362         gtk_container_border_width(GTK_CONTAINER(main_vb), 12);
363         gtk_container_add(GTK_CONTAINER(sp->win), main_vb);
364
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);
368         
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);
372
373         wsp_init_table(sp);
374
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         
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 */
383
384         error_string = register_tap_listener( 
385                         "wsp",
386                         sp,
387                         filter,
388                         wspstat_reset,
389                         wspstat_packet,
390                         wspstat_draw);
391         if (error_string){
392                 /* error, we failed to attach to the tap. clean up */
393                 simple_dialog( ESD_TYPE_ERROR, ESD_BTN_OK, error_string->str );
394                 g_free(sp->pdu_stats);
395                 g_free(sp->filter);
396                 g_free(sp);
397                 g_string_free(error_string, TRUE);
398                 return ;
399         }
400
401         /* Button row. */
402     bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
403     gtk_box_pack_start(GTK_BOX(main_vb), bbox, FALSE, FALSE, 0);
404
405     bt_close = OBJECT_GET_DATA(bbox, GTK_STOCK_CLOSE);
406     window_set_cancel_button(sp->win, bt_close, window_cancel_button_cb);
407
408     SIGNAL_CONNECT(sp->win, "delete_event", window_delete_event_cb, NULL);
409         SIGNAL_CONNECT(sp->win, "destroy", win_destroy_cb, sp);
410
411     gtk_widget_show_all(sp->win);
412     window_present(sp->win);
413         
414     cf_retap_packets(&cfile, FALSE);
415 }
416
417 static tap_dfilter_dlg wsp_stat_dlg = {
418         "WAP-WSP Packet Counter",
419         "wsp,stat",
420         gtk_wspstat_init,
421         -1
422 };
423
424 void
425 register_tap_listener_gtkwspstat(void)
426 {
427         register_dfilter_stat(&wsp_stat_dlg, "WAP-WSP",
428             REGISTER_STAT_GROUP_TELEPHONY);
429 }