More eradication of old-style function definitions.
[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_dfilter_dlg.h"
46 #include "gtk/main.h"
47
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, sizeof(string_buff), "%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, sizeof(string_buff), "%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, sizeof(string_buff), "%9d", data->packets );
119                  gtk_label_set_text( 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 idx = 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 (idx!=0) {
194                 sp->pdu_stats[ idx ].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 idx;
210
211         for(i=1;i<=sp->num_pdus ; i++)
212         {
213             g_snprintf(str, sizeof(str), "%9d",  sp->pdu_stats[i ].packets);
214                  gtk_label_set_text( GTK_LABEL(sp->pdu_stats[i].widget), str);
215         }
216
217         idx=sp->index;
218         g_hash_table_foreach( sp->hash, (GHFunc) wsp_draw_statuscode, NULL );
219         if (idx != 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 static void
236 win_destroy_cb(GtkWindow *win _U_, gpointer data)
237 {
238         wspstat_t *sp=(wspstat_t *)data;
239
240         protect_thread_critical_region();
241         remove_tap_listener(sp);
242         unprotect_thread_critical_region();
243
244         g_free(sp->pdu_stats);
245         g_free(sp->filter);
246         g_hash_table_foreach( sp->hash, (GHFunc)wsp_free_hash, NULL);
247         g_hash_table_destroy( sp->hash);
248         g_free(sp);
249 }
250
251 static void
252 add_table_entry(wspstat_t *sp, const char *str, int x, int y, int idx)
253 {
254         GtkWidget *tmp;
255
256         tmp=gtk_label_new( str );
257         gtk_table_attach_defaults(GTK_TABLE(sp->table_pdu_types), tmp, x, x+1, y, y+1);
258         gtk_label_set_justify(GTK_LABEL(tmp), GTK_JUSTIFY_LEFT);
259         gtk_widget_show(tmp);
260         if (idx != 0) {
261                 sp->pdu_stats [idx] .widget = GTK_LABEL( tmp ) ;
262         }
263 }
264
265
266 static void
267 wsp_init_table(wspstat_t *sp)
268 {
269         int pos=0;
270         guint32 i;
271         /* gchar        buffer[51];     */
272
273         add_table_entry( sp, "PDU Type               "  , 0, pos, 0);
274         add_table_entry( sp, "packets  "        , 1, pos, 0);
275         add_table_entry( sp, "PDU Type               "  , 2, pos, 0);
276         add_table_entry( sp, "packets  "        , 3, pos, 0);
277         pos++;
278         for (i=1 ; i <= sp->num_pdus ; i++ )
279         {
280                 int x = 0;
281                 if (i> (sp->num_pdus+1) /2 ){
282                         x=2;
283                 }
284                 /* Maybe we should display the hexadecimal value ? */
285                 /* g_snprintf(buffer, sizeof(buffer), "%s  (0X%x)", match_strval_ext( index2pdut( i ), &wsp_vals_pdu_type_ext), index2pdut(i) );*/
286                 add_table_entry( sp,
287                                 match_strval_ext(index2pdut(i), &wsp_vals_pdu_type_ext), /* or buffer, */
288                                 x,
289                                 pos,
290                                 0
291                                 );
292                 add_table_entry( sp, "0", x+1, pos
293                                 , i /* keep a pointer to this widget to update it in _draw() */
294                                 );
295                 pos++;
296                 if (i== (sp->num_pdus+1) /2) {
297                         pos=1;
298                 }
299         }
300 }
301
302 /* When called, this function will create a new instance of gtk2-wspstat.
303  */
304 static void
305 gtk_wspstat_init(const char *optarg, void *userdata _U_)
306 {
307         wspstat_t *sp;
308         const char      *filter=NULL;
309         char            *title=NULL;
310         GString         *error_string;
311         GtkWidget       *main_vb, *pdutypes_fr, *statuscode_fr ;
312         GtkWidget       *bt_close;
313         GtkWidget       *bbox;
314         guint32          i;
315         wsp_status_code_t  *sc;
316         const value_string *wsp_vals_status_p;
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 = dlg_window_new("wsp-stat");  /* transient_for top_level */
327         gtk_window_set_destroy_with_parent (GTK_WINDOW(sp->win), TRUE);
328
329         sp->hash = g_hash_table_new( g_int_hash, g_int_equal);
330         wsp_vals_status_p = VALUE_STRING_EXT_VS_P(&wsp_vals_status_ext);
331         for (i=0 ; wsp_vals_status_p[i].strptr ; i++ )
332         {
333                 gint *key;
334                 sc=g_malloc( sizeof(wsp_status_code_t) );
335                 key=g_malloc( sizeof(gint) );
336                 sc->name=wsp_vals_status_p[i].strptr;
337                 sc->packets=0;
338                 sc->widget=NULL;
339                 sc->sp = sp;
340                 *key=wsp_vals_status_p[i].value;
341                 g_hash_table_insert(
342                                 sp->hash,
343                                 key,
344                                 sc);
345         }
346         sp->num_pdus = 16;
347         sp->pdu_stats=g_malloc( (sp->num_pdus+1) * sizeof( wsp_pdu_t) );
348         if(filter){
349                 sp->filter=g_strdup(filter);
350                 title=g_strdup_printf("Wireshark: WAP-WSP statistics with filter: %s", filter);
351         } else {
352                 sp->filter=NULL;
353                 title=g_strdup("Wireshark: WAP-WSP statistics");
354         }
355         for (i=0;i<=sp->num_pdus; i++)
356         {
357                 sp->pdu_stats[i].packets=0;
358         }
359
360         gtk_window_set_title(GTK_WINDOW(sp->win), title);
361         g_free(title);
362
363         /* container for the two frames */
364         main_vb = gtk_vbox_new(FALSE, 3);
365         gtk_container_set_border_width(GTK_CONTAINER(main_vb), 12);
366         gtk_container_add(GTK_CONTAINER(sp->win), main_vb);
367
368         /* PDU Types frame */
369         pdutypes_fr = gtk_frame_new("Summary of PDU Types (wsp.pdu_type)");
370         gtk_container_add(GTK_CONTAINER(main_vb), pdutypes_fr);
371
372         sp->table_pdu_types = gtk_table_new( (sp->num_pdus+1) / 2 + 1, 4, FALSE);
373         gtk_container_add( GTK_CONTAINER( pdutypes_fr), sp->table_pdu_types);
374         gtk_container_set_border_width( GTK_CONTAINER(sp->table_pdu_types) , 10);
375
376         wsp_init_table(sp);
377
378         /* Status Codes frame */
379         statuscode_fr = gtk_frame_new("Summary of Status Code (wsp.reply.status)");
380         gtk_container_add(GTK_CONTAINER(main_vb), statuscode_fr);
381
382         sp->table_status_code = gtk_table_new( 0, 4, FALSE);
383         gtk_container_add( GTK_CONTAINER( statuscode_fr), sp->table_status_code);
384         gtk_container_set_border_width( GTK_CONTAINER(sp->table_status_code) , 10);
385         sp->index = 0;          /* No answers to display yet */
386
387         error_string = register_tap_listener(
388                         "wsp",
389                         sp,
390                         filter,
391                         0,
392                         wspstat_reset,
393                         wspstat_packet,
394                         wspstat_draw);
395         if (error_string){
396                 /* error, we failed to attach to the tap. clean up */
397                 simple_dialog( ESD_TYPE_ERROR, ESD_BTN_OK, "%s", error_string->str );
398                 g_free(sp->pdu_stats);
399                 g_free(sp->filter);
400                 g_free(sp);
401                 g_string_free(error_string, TRUE);
402                 return ;
403         }
404
405         /* Button row. */
406         bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
407         gtk_box_pack_start(GTK_BOX(main_vb), bbox, FALSE, FALSE, 0);
408
409         bt_close = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_CLOSE);
410         window_set_cancel_button(sp->win, bt_close, window_cancel_button_cb);
411
412         g_signal_connect(sp->win, "delete_event", G_CALLBACK(window_delete_event_cb), NULL);
413         g_signal_connect(sp->win, "destroy", G_CALLBACK(win_destroy_cb), sp);
414
415         gtk_widget_show_all(sp->win);
416         window_present(sp->win);
417
418         cf_retap_packets(&cfile);
419         gdk_window_raise(sp->win->window);
420 }
421
422 static tap_dfilter_dlg wsp_stat_dlg = {
423         "WAP-WSP Packet Counter",
424         "wsp,stat",
425         gtk_wspstat_init,
426         -1
427 };
428
429 void
430 register_tap_listener_gtkwspstat(void)
431 {
432         register_dfilter_stat(&wsp_stat_dlg, "_WAP-WSP",
433             REGISTER_STAT_GROUP_TELEPHONY);
434 }