merge_all_tap_menus() has been moved to menus.c.
[obnox/wireshark/wip.git] / gtk / wsp_stat.c
1 /* wsp_stat.c
2  * wsp_stat   2003 Jean-Michel FAYARD
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
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 #include <string.h>
30
31 #include <gtk/gtk.h>
32
33 #include <epan/packet.h>
34 #include <epan/packet_info.h>
35 #include <epan/epan.h>
36 #include <epan/tap.h>
37 #include <epan/dissectors/packet-wsp.h>
38
39 #include "../simple_dialog.h"
40 #include "../globals.h"
41 #include "../stat_menu.h"
42
43 #include "gtk/gui_utils.h"
44 #include "gtk/dlg_utils.h"
45 #include "gtk/tap_param_dlg.h"
46 #include "gtk/main.h"
47
48 #include "gtk/old-gtk-compat.h"
49
50 /* used to keep track of the stats for a specific PDU type*/
51 typedef struct _wsp_pdu_t {
52         GtkLabel        *widget;
53         guint32          packets;
54 } wsp_pdu_t;
55
56 /* used to keep track of the statictics for an entire program interface */
57 typedef struct _wsp_stats_t {
58         char            *filter;
59         wsp_pdu_t       *pdu_stats;
60         guint32          num_pdus;
61         GtkWidget       *win;
62         GHashTable      *hash;
63         GtkWidget       *table_pdu_types;
64         GtkWidget       *table_status_code;
65         guint            index; /* Number of status code to display */
66 } wspstat_t;
67 /* used to keep track of a single type of status code */
68 typedef struct _wsp_status_code_t {
69         const gchar     *name;
70         guint32          packets;
71         GtkWidget       *widget;/* label in which we print the number of packets */
72         wspstat_t       *sp;    /* entire program interface */
73 } wsp_status_code_t;
74
75 static void
76 wsp_free_hash( gpointer key, gpointer value, gpointer user_data _U_ )
77 {
78         g_free(key);
79         g_free(value);
80 }
81 static void
82 wsp_reset_hash(gchar *key _U_ , wsp_status_code_t *data, gpointer ptr _U_ )
83 {
84         data->packets = 0;
85 }
86
87 /* Update the entry corresponding to the number of packets of a special status code
88  * or create it if it don't exist.
89  */
90 static void
91 wsp_draw_statuscode(gchar *key _U_, wsp_status_code_t *data, gchar * unused _U_ )
92 {
93         char string_buff[256];
94
95         if ((data==NULL) || (data->packets==0))
96                 return;
97         if (data->widget==NULL){        /* create an entry in the table */
98                 GtkWidget       *tmp;
99                 int x = 2*((data->sp->index) % 2);
100                 int y = (data->sp->index) /2;
101
102
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);
109
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 );
115
116                 data->sp->index++;
117         } else {
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);
121         }
122 }
123 static void
124 wspstat_reset(void *psp)
125 {
126         wspstat_t *sp=psp;
127         guint32 i;
128
129         for(i=1;i<=sp->num_pdus;i++)
130         {
131                 sp->pdu_stats[i].packets=0;
132         }
133         g_hash_table_foreach( sp->hash, (GHFunc)wsp_reset_hash, NULL);
134 }
135 static gint
136 pdut2index(gint pdut)
137 {
138         if (pdut<=0x09)         return pdut;
139         if (pdut>=0x40){
140                 if (pdut <= 0x44){
141                         return pdut-54;
142                 } else if (pdut==0x60||pdut==0x61){
143                         return pdut-81;
144                 }
145         }
146         return 0;
147 }
148 static gint
149 index2pdut(gint pdut)
150 {
151         if (pdut<=0x09)
152                 return pdut;
153         if (pdut<=14)
154                 return pdut+54;
155         if (pdut<=16)
156                 return pdut+81;
157         return 0;
158 }
159
160 static int
161 wspstat_packet(void *psp, packet_info *pinfo _U_, epan_dissect_t *edt _U_, const void *pri)
162 {
163         wspstat_t *sp=psp;
164         const wsp_info_value_t *value=pri;
165         gint idx = pdut2index(value->pdut);
166         int retour=0;
167
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(
173                                 sp->hash,
174                                 key);
175                 if (!sc) {
176                         g_warning("%s:%d What's Wrong, doc ?\n", __FILE__, __LINE__);
177                         sc = g_malloc( sizeof(wsp_status_code_t) );
178                         sc -> packets = 1;
179                         sc -> name = NULL;
180                         sc -> widget=NULL;
181                         sc -> sp = sp;
182                         g_hash_table_insert(
183                                 sp->hash,
184                                 key,
185                                 sc);
186                 } else {
187                         sc->packets++;
188                 }
189                 retour=1;
190         }
191
192
193
194         if (idx!=0) {
195                 sp->pdu_stats[ idx ].packets++;
196                 retour = 1;
197         }
198         return retour;
199
200 }
201
202
203
204 static void
205 wspstat_draw(void *psp)
206 {
207         wspstat_t *sp=psp;
208         guint32 i;
209         char str[256];
210         guint idx;
211
212         for(i=1;i<=sp->num_pdus ; i++)
213         {
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);
216         }
217
218         idx=sp->index;
219         g_hash_table_foreach( sp->hash, (GHFunc) wsp_draw_statuscode, NULL );
220         if (idx != 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);
224         }
225
226 }
227
228
229
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
233  *
234  * there should not be any other critical regions in gtk2
235  */
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 idx)
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 (idx != 0) {
262                 sp->pdu_stats [idx] .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, sizeof(buffer), "%s  (0X%x)", match_strval_ext( index2pdut( i ), &wsp_vals_pdu_type_ext), index2pdut(i) );*/
287                 add_table_entry( sp,
288                                 match_strval_ext(index2pdut(i), &wsp_vals_pdu_type_ext), /* 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         const value_string *wsp_vals_status_p;
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 = dlg_window_new("wsp-stat");  /* transient_for top_level */
328         gtk_window_set_destroy_with_parent (GTK_WINDOW(sp->win), TRUE);
329
330         sp->hash = g_hash_table_new( g_int_hash, g_int_equal);
331         wsp_vals_status_p = VALUE_STRING_EXT_VS_P(&wsp_vals_status_ext);
332         for (i=0 ; wsp_vals_status_p[i].strptr ; i++ )
333         {
334                 gint *key;
335                 sc=g_malloc( sizeof(wsp_status_code_t) );
336                 key=g_malloc( sizeof(gint) );
337                 sc->name=wsp_vals_status_p[i].strptr;
338                 sc->packets=0;
339                 sc->widget=NULL;
340                 sc->sp = sp;
341                 *key=wsp_vals_status_p[i].value;
342                 g_hash_table_insert(
343                                 sp->hash,
344                                 key,
345                                 sc);
346         }
347         sp->num_pdus = 16;
348         sp->pdu_stats=g_malloc( (sp->num_pdus+1) * sizeof( wsp_pdu_t) );
349         if(filter){
350                 sp->filter=g_strdup(filter);
351                 title=g_strdup_printf("Wireshark: WAP-WSP statistics with filter: %s", filter);
352         } else {
353                 sp->filter=NULL;
354                 title=g_strdup("Wireshark: WAP-WSP statistics");
355         }
356         for (i=0;i<=sp->num_pdus; i++)
357         {
358                 sp->pdu_stats[i].packets=0;
359         }
360
361         gtk_window_set_title(GTK_WINDOW(sp->win), title);
362         g_free(title);
363
364         /* container for the two frames */
365         main_vb = gtk_vbox_new(FALSE, 3);
366         gtk_container_set_border_width(GTK_CONTAINER(main_vb), 12);
367         gtk_container_add(GTK_CONTAINER(sp->win), main_vb);
368
369         /* PDU Types frame */
370         pdutypes_fr = gtk_frame_new("Summary of PDU Types (wsp.pdu_type)");
371         gtk_container_add(GTK_CONTAINER(main_vb), pdutypes_fr);
372
373         sp->table_pdu_types = gtk_table_new( (sp->num_pdus+1) / 2 + 1, 4, FALSE);
374         gtk_container_add( GTK_CONTAINER( pdutypes_fr), sp->table_pdu_types);
375         gtk_container_set_border_width( GTK_CONTAINER(sp->table_pdu_types) , 10);
376
377         wsp_init_table(sp);
378
379         /* Status Codes frame */
380         statuscode_fr = gtk_frame_new("Summary of Status Code (wsp.reply.status)");
381         gtk_container_add(GTK_CONTAINER(main_vb), statuscode_fr);
382
383         sp->table_status_code = gtk_table_new( 0, 4, FALSE);
384         gtk_container_add( GTK_CONTAINER( statuscode_fr), sp->table_status_code);
385         gtk_container_set_border_width( GTK_CONTAINER(sp->table_status_code) , 10);
386         sp->index = 0;          /* No answers to display yet */
387
388         error_string = register_tap_listener(
389                         "wsp",
390                         sp,
391                         filter,
392                         0,
393                         wspstat_reset,
394                         wspstat_packet,
395                         wspstat_draw);
396         if (error_string){
397                 /* error, we failed to attach to the tap. clean up */
398                 simple_dialog( ESD_TYPE_ERROR, ESD_BTN_OK, "%s", error_string->str );
399                 g_free(sp->pdu_stats);
400                 g_free(sp->filter);
401                 g_free(sp);
402                 g_string_free(error_string, TRUE);
403                 return ;
404         }
405
406         /* Button row. */
407         bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
408         gtk_box_pack_start(GTK_BOX(main_vb), bbox, FALSE, FALSE, 0);
409
410         bt_close = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_CLOSE);
411         window_set_cancel_button(sp->win, bt_close, window_cancel_button_cb);
412
413         g_signal_connect(sp->win, "delete_event", G_CALLBACK(window_delete_event_cb), NULL);
414         g_signal_connect(sp->win, "destroy", G_CALLBACK(win_destroy_cb), sp);
415
416         gtk_widget_show_all(sp->win);
417         window_present(sp->win);
418
419         cf_retap_packets(&cfile);
420         gdk_window_raise(gtk_widget_get_window(sp->win));
421 }
422
423 static tap_param wsp_stat_params[] = {
424         { PARAM_FILTER, "Filter", NULL }
425 };
426
427 static tap_param_dlg wsp_stat_dlg = {
428         "WAP-WSP Packet Counter",
429         "wsp,stat",
430         gtk_wspstat_init,
431         -1,
432         G_N_ELEMENTS(wsp_stat_params),
433         wsp_stat_params
434 };
435
436 void
437 register_tap_listener_gtkwspstat(void)
438 {
439         register_dfilter_stat(&wsp_stat_dlg, "_WAP-WSP",
440             REGISTER_STAT_GROUP_TELEPHONY);
441 }
442
443 #ifdef MAIN_MENU_USE_UIMANAGER
444 void wsp_stat_cb(GtkAction *action, gpointer user_data _U_)
445 {
446         tap_param_dlg_cb(action, &wsp_stat_dlg);
447 }
448 #endif