Provide a mechanism by which a tap can arrange to have its menu enabled
[obnox/wireshark/wip.git] / gtk / wsp_stat.c
1 /* wsp_stat.c
2  * wsp_stat   2003 Jean-Michel FAYARD
3  *
4  * $Id: wsp_stat.c,v 1.3 2003/09/19 07:24:39 guy Exp $
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 <gtk/gtk.h>
31 #include "menu.h"
32 #include "epan/packet_info.h"
33 #include "simple_dialog.h"
34 #include "tap.h"
35 #include "../register.h"
36 #include "../globals.h"
37 #include "compat_macros.h"
38 #include "../packet-wsp.h"
39 #include <string.h>
40
41 /* used to keep track of the stats for a specific PDU type*/
42 typedef struct _wsp_pdu_t {
43         GtkLabel        *widget;
44         guint32          packets;
45 } wsp_pdu_t;
46
47 /* used to keep track of the statictics for an entire program interface */
48 typedef struct _wsp_stats_t {
49         char            *filter;
50         wsp_pdu_t       *pdu_stats;
51         guint32          num_pdus;
52         GtkWidget       *win;
53         GHashTable      *hash;
54         GtkWidget       *table_pdu_types;       
55         GtkWidget       *table_status_code;
56         guint            index; /* Number of status code to display */
57 } wspstat_t;
58 /* used to keep track of a single type of status code */
59 typedef struct _wsp_status_code_t {
60         gchar           *name;
61         guint32          packets;
62         GtkWidget       *widget;/* label in which we print the number of packets */
63         wspstat_t       *sp;    /* entire program interface */
64 } wsp_status_code_t;
65
66 static GtkWidget *dlg=NULL, *dlg_box;
67 static GtkWidget *filter_box;
68 static GtkWidget *filter_label, *filter_entry;
69 static GtkWidget *start_button;
70
71 static void
72 wsp_free_hash( gpointer key, gpointer value, gpointer user_data _U_ )
73 {
74         g_free(key);
75         g_free(value);
76 }
77 static void
78 wsp_reset_hash(gchar *key _U_ , wsp_status_code_t *data, gpointer ptr _U_ ) 
79 {       
80         data->packets = 0;
81 }
82
83 /* Update the entry corresponding to the number of packets of a special status code
84  * or create it if it don't exist.
85  */
86 static void
87 wsp_draw_statuscode(gchar *key _U_, wsp_status_code_t *data, gchar * string_buff )
88 {
89         if ((data==NULL) || (data->packets==0))
90                 return;
91         if (data->widget==NULL){        /* create an entry in the table */
92                 GtkWidget       *tmp;
93                 int x = 2*((data->sp->index) % 2);
94                 int y = (data->sp->index) /2;
95
96
97                 /* Maybe we should display the hexadecimal value ? */
98                 /* sprintf(string_buff, "%s  (0X%x)", data->name, *key); */
99                 tmp = gtk_label_new( data->name  /* string_buff */ );
100                 gtk_table_attach_defaults(GTK_TABLE(data->sp->table_status_code), tmp, x, x+1, y, y+1);
101                 gtk_label_set_justify(GTK_LABEL(tmp), GTK_JUSTIFY_LEFT);
102                 gtk_widget_show(tmp);
103
104                 sprintf( string_buff, "%9d", data->packets );
105                 data->widget = gtk_label_new( string_buff );
106                 gtk_table_attach_defaults(GTK_TABLE(data->sp->table_status_code), data->widget, x+1, x+2, y, y+1);
107                 gtk_label_set_justify(GTK_LABEL(data->widget), GTK_JUSTIFY_LEFT);
108                 gtk_widget_show( data->widget );
109
110                 data->sp->index++;
111         } else {
112                 /* Just update the label string */
113                 sprintf( string_buff, "%9d", data->packets );
114                 gtk_label_set( GTK_LABEL(data->widget), string_buff);
115         }
116 }
117 static void
118 wspstat_reset(void *psp)
119 {
120         wspstat_t *sp=psp;
121         guint32 i;
122
123         for(i=1;i<=sp->num_pdus;i++)
124         {
125                 sp->pdu_stats[i].packets=0;
126         }
127         g_hash_table_foreach( sp->hash, (GHFunc)wsp_reset_hash, NULL);  
128 }
129 static gint 
130 pdut2index(gint pdut)
131 {
132         if (pdut<=0x09)         return pdut;
133         if (pdut>=0x40){
134                 if (pdut <= 0x44){
135                         return pdut-54;
136                 } else if (pdut==0x60||pdut==0x61){
137                         return pdut-81;
138                 }
139         }
140         return 0;
141 }
142 static gint
143 index2pdut(gint pdut)
144 {
145         if (pdut<=0x09)
146                 return pdut;
147         if (pdut<=14)
148                 return pdut+54;
149         if (pdut<=16)
150                 return pdut+81;
151         return 0;
152 }
153
154 static int
155 wspstat_packet(void *psp, packet_info *pinfo _U_, epan_dissect_t *edt _U_, void *pri)
156 {
157         wspstat_t *sp=psp;
158         wsp_info_value_t *value=pri;
159         gint index = pdut2index(value->pdut);
160         int retour=0;
161
162         if (value->status_code != 0) {
163                 gint *key=g_malloc( sizeof(gint) );
164                 wsp_status_code_t *sc;
165                 *key=value->status_code ;
166                 sc = g_hash_table_lookup( 
167                                 sp->hash, 
168                                 key);
169                 if (!sc) {
170                         g_warning("%s:%d What's Wrong, doc ?\n", __FILE__, __LINE__);
171                         sc = g_malloc( sizeof(wsp_status_code_t) );
172                         sc -> packets = 1;
173                         sc -> name = NULL;
174                         sc -> widget=NULL;
175                         sc -> sp = sp;
176                         g_hash_table_insert(
177                                 sp->hash,
178                                 key,
179                                 sc);
180                 } else {
181                         sc->packets++;
182                 }
183                 retour=1;
184         }
185
186                 
187
188         if (index!=0) {
189                 sp->pdu_stats[ index ].packets++;
190                 retour = 1;
191         }
192         return retour;
193
194 }
195
196
197
198 static void
199 wspstat_draw(void *psp)
200 {
201         wspstat_t *sp=psp;
202         guint32 i;
203         char str[256];
204         guint index;
205
206         for(i=1;i<=sp->num_pdus ; i++)
207         {
208                 sprintf(str, "%9d",  sp->pdu_stats[i ].packets);
209                 gtk_label_set( GTK_LABEL(sp->pdu_stats[i].widget), str);
210         }
211
212         index=sp->index;
213         g_hash_table_foreach( sp->hash, (GHFunc) wsp_draw_statuscode, str );
214         if (index != sp->index){
215                 /* We have inserted a new entry corresponding to a status code ,
216                  * let's resize the table */
217                 gtk_table_resize ( GTK_TABLE(sp->table_status_code), sp->index  % 2 , 4);
218         }
219         
220 }
221
222
223
224 /* since the gtk2 implementation of tap is multithreaded we must protect
225  * remove_tap_listener() from modifying the list while draw_tap_listener()
226  * is running.  the other protected block is in main.c
227  *
228  * there should not be any other critical regions in gtk2
229  */
230 void protect_thread_critical_region(void);
231 void unprotect_thread_critical_region(void);
232 static void
233 win_destroy_cb(GtkWindow *win _U_, gpointer data)
234 {
235         wspstat_t *sp=(wspstat_t *)data;
236
237         protect_thread_critical_region();
238         remove_tap_listener(sp);
239         unprotect_thread_critical_region();
240
241         g_free(sp->pdu_stats);
242         g_free(sp->filter);
243         g_hash_table_foreach( sp->hash, (GHFunc)wsp_free_hash, NULL);
244         g_hash_table_destroy( sp->hash);        
245         g_free(sp);
246 }
247
248 static void
249 add_table_entry(wspstat_t *sp, char *str, int x, int y, int index)
250 {
251         GtkWidget *tmp;
252
253         tmp=gtk_label_new( str );
254         gtk_table_attach_defaults(GTK_TABLE(sp->table_pdu_types), tmp, x, x+1, y, y+1);
255         gtk_label_set_justify(GTK_LABEL(tmp), GTK_JUSTIFY_LEFT);
256         gtk_widget_show(tmp);
257         if (index != 0) {
258                 sp->pdu_stats [index] .widget = GTK_LABEL( tmp ) ;
259         }
260 }
261
262
263 static void
264 wsp_init_table(wspstat_t *sp)
265 {
266         int pos=0;
267         guint32 i;
268         /* gchar        buffer[51];     */
269         
270         add_table_entry( sp, "PDU Type               "  , 0, pos, 0);
271         add_table_entry( sp, "packets  "        , 1, pos, 0);
272         add_table_entry( sp, "PDU Type               "  , 2, pos, 0);
273         add_table_entry( sp, "packets  "        , 3, pos, 0);
274         pos++;
275         for (i=1 ; i <= sp->num_pdus ; i++ )
276         {
277                 int x = 0;
278                 if (i> (sp->num_pdus+1) /2 ){
279                         x=2;
280                 }
281                 /* Maybe we should display the hexadecimal value ? */
282                 /* snprintf(buffer, 50, "%s  (0X%x)", match_strval( index2pdut( i ), vals_pdu_type), index2pdut(i) );*/
283                 add_table_entry( sp, 
284                                 match_strval(index2pdut(i), vals_pdu_type), /* or buffer, */
285                                 x,
286                                 pos,
287                                 0
288                                 );
289                 add_table_entry( sp, "0", x+1, pos
290                                 , i /* keep a pointer to this widget to update it in _draw() */
291                                 );      
292                 pos++;
293                 if (i== (sp->num_pdus+1) /2) {
294                         pos=1;
295                 }
296         }
297 }
298
299 /* When called, this function will create a new instance of gtk2-wspstat.
300  */
301 static void
302 gtk_wspstat_init(char *optarg)
303 {
304         wspstat_t *sp;
305         char            *filter=NULL;
306         char            *title=NULL;
307         GString         *error_string;
308         GtkWidget       *main_vb, *pdutypes_fr, *statuscode_fr ;
309         guint32          i;
310         wsp_status_code_t *sc;
311         
312         
313         if (!strncmp (optarg, "wsp,stat,", 9)){
314                 filter=optarg+9;
315         } else {
316                 filter=NULL;
317         }
318         
319         sp = g_malloc( sizeof(wspstat_t) );
320         sp->hash = g_hash_table_new( g_int_hash, g_int_equal);
321         for (i=0 ; vals_status[i].strptr ; i++ )
322         {
323                 gint *key;
324                 sc=g_malloc( sizeof(wsp_status_code_t) );
325                 key=g_malloc( sizeof(gint) );
326                 sc->name=vals_status[i].strptr;
327                 sc->packets=0;
328                 sc->widget=NULL;
329                 sc->sp = sp;
330                 *key=vals_status[i].value;
331                 g_hash_table_insert(
332                                 sp->hash,
333                                 key,
334                                 sc);
335         }
336         sp->num_pdus = 16;
337         sp->pdu_stats=g_malloc( (sp->num_pdus+1) * sizeof( wsp_pdu_t) );
338         if(filter){
339                 sp->filter=g_malloc(strlen(filter)+1);
340                 strcpy(sp->filter,filter);
341                 title=g_strdup_printf("WSP Stats with filter: %s", filter);
342         } else {
343                 sp->filter=NULL;
344                 title=g_strdup("WSP Stats");
345         }
346         for (i=0;i<=sp->num_pdus; i++)
347         {
348                 sp->pdu_stats[i].packets=0;
349         }
350
351         sp->win = gtk_window_new( GTK_WINDOW_TOPLEVEL);
352         gtk_window_set_title( GTK_WINDOW(sp->win), title );
353         g_free(title);
354         SIGNAL_CONNECT( sp->win, "destroy", win_destroy_cb, sp);
355
356
357         /* container for the two frames */
358         main_vb = gtk_vbox_new(FALSE, 10);
359         gtk_container_border_width(GTK_CONTAINER(main_vb), 10);
360         gtk_container_add(GTK_CONTAINER(sp->win), main_vb);
361         gtk_widget_show(main_vb);
362
363         /* PDU Types frame */
364         pdutypes_fr = gtk_frame_new("Summary of PDU Types (wsp.pdu_type)");
365         gtk_container_add(GTK_CONTAINER(main_vb), pdutypes_fr);
366         gtk_widget_show(pdutypes_fr);
367         
368         sp->table_pdu_types = gtk_table_new( (sp->num_pdus+1) / 2 + 1, 4, FALSE);
369         gtk_container_add( GTK_CONTAINER( pdutypes_fr), sp->table_pdu_types);
370         gtk_container_set_border_width( GTK_CONTAINER(sp->table_pdu_types) , 10);
371
372         wsp_init_table(sp);
373         gtk_widget_show( sp->table_pdu_types );
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         gtk_widget_show(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
386         error_string = register_tap_listener( 
387                         "wsp",
388                         sp,
389                         filter,
390                         wspstat_reset,
391                         wspstat_packet,
392                         wspstat_draw);
393         if (error_string){
394                 /* error, we failed to attach to the tap. clean up */
395                 simple_dialog( ESD_TYPE_WARN, NULL, error_string->str );
396                 g_free(sp->pdu_stats);
397                 g_free(sp->filter);
398                 g_free(sp);
399                 g_string_free(error_string, TRUE);
400                 return ;
401         }
402         gtk_widget_show_all( sp->win );
403         redissect_packets(&cfile);
404 }
405
406
407
408 static void
409 wspstat_start_button_clicked(GtkWidget *item _U_, gpointer data _U_)
410 {
411         char *filter;
412         char str[256];
413
414         filter=(char *)gtk_entry_get_text(GTK_ENTRY(filter_entry));
415         if(filter[0]==0){
416                 gtk_wspstat_init("wsp,stat,");
417         } else {
418                 sprintf(str, "wsp,stat,%s", filter);
419                 gtk_wspstat_init(str);
420         }
421 }
422
423 static void
424 dlg_destroy_cb(void)
425 {
426         dlg=NULL;
427 }
428
429 static void
430 gtk_wspstat_cb(GtkWidget *w _U_, gpointer d _U_)
431 {
432         /* if the window is already open, bring it to front */
433         if(dlg){
434                 gdk_window_raise(dlg->window);
435                 return;
436         }
437
438         dlg=gtk_window_new(GTK_WINDOW_TOPLEVEL);
439         gtk_window_set_title(GTK_WINDOW(dlg), "WSP Statistics");
440         SIGNAL_CONNECT(dlg, "destroy", dlg_destroy_cb, NULL);
441         dlg_box=gtk_vbox_new(FALSE, 0);
442         gtk_container_add(GTK_CONTAINER(dlg), dlg_box);
443         gtk_widget_show(dlg_box);
444
445
446         /* filter box */
447         filter_box=gtk_hbox_new(FALSE, 10);
448         /* Filter label */
449         gtk_container_set_border_width(GTK_CONTAINER(filter_box), 10);
450         filter_label=gtk_label_new("Filter:");
451         gtk_box_pack_start(GTK_BOX(filter_box), filter_label, FALSE, FALSE, 0);
452         gtk_widget_show(filter_label);
453
454         filter_entry=gtk_entry_new_with_max_length(250);
455         gtk_box_pack_start(GTK_BOX(filter_box), filter_entry, FALSE, FALSE, 0);
456         gtk_widget_show(filter_entry);
457         
458         gtk_box_pack_start(GTK_BOX(dlg_box), filter_box, TRUE, TRUE, 0);
459         gtk_widget_show(filter_box);
460
461
462         /* the start button */
463         start_button=gtk_button_new_with_label("Create Stat");
464         SIGNAL_CONNECT_OBJECT(start_button, "clicked",
465                               wspstat_start_button_clicked, NULL);
466         gtk_box_pack_start(GTK_BOX(dlg_box), start_button, TRUE, TRUE, 0);
467         gtk_widget_show(start_button);
468
469         gtk_widget_show_all(dlg);
470 }
471
472
473 void
474 register_tap_listener_gtkwspstat(void)
475 {
476         register_ethereal_tap("wsp,stat,", gtk_wspstat_init);
477 }
478
479 void
480 register_tap_menu_gtkwspstat(void)
481 {
482         register_tap_menu_item("Statistics/Watch protocol/WAP-WSP",
483             gtk_wspstat_cb, NULL, NULL);
484 }