remove some warnings
[obnox/wireshark/wip.git] / gtk / camel_counter.c
1 /* camel_counter.c
2  * camel message counter for Wireshark
3  * Copyright 2006 Florent Drouin (based on h225_counter.c from Lars Roland)
4  *
5  * Wireshark - Network traffic analyzer
6  * By Gerald Combs <gerald@wireshark.org>
7  * Copyright 1998 Gerald Combs
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #ifdef HAVE_SYS_TYPES_H
29 # include <sys/types.h>
30 #endif
31
32 #include <string.h>
33 #include <gtk/gtk.h>
34
35 #include "epan/packet_info.h"
36 #include "epan/epan.h"
37 #include "epan/value_string.h"
38 #include "epan/tap.h"
39
40 #include "register.h"
41 #include "timestats.h"
42 #include "simple_dialog.h"
43 #include "file.h"
44 #include "globals.h"
45 #include "stat_menu.h"
46 #include "tap_dfilter_dlg.h"
47
48 #include "gtk/main.h"
49 #include "gtk/dlg_utils.h"
50 #include "gtk/gui_utils.h"
51 #include "gtk/gui_stat_util.h"
52 #include "gtk/compat_macros.h"
53 #include "epan/camel-persistentdata.h"
54
55 static void gtk_camelcounter_reset(void *phs);
56 static int gtk_camelcounter_packet(void *phs,
57                                    packet_info *pinfo _U_,
58                                    epan_dissect_t *edt _U_, 
59                                    const void *phi);
60 static void gtk_camelcounter_draw(void *phs);
61 static void win_destroy_cb(GtkWindow *win _U_, gpointer data);
62 static void gtk_camelcounter_init(const char *optarg, void *userdata _U_);
63 void register_tap_listener_gtk_camelcounter(void);
64
65 /* following values represent the size of their valuestring arrays */
66
67 struct camelcounter_t {
68   GtkWidget *win;
69   GtkWidget *vbox;
70   char *filter;
71   GtkWidget *scrolled_window;
72   GtkCList *table;
73   guint32 camel_msg[camel_MAX_NUM_OPR_CODES];
74 };
75
76 static void gtk_camelcounter_reset(void *phs)
77 {
78   struct camelcounter_t * p_counter= ( struct camelcounter_t *) phs;
79   int i;
80   
81   /* Erase Message Type count */
82   for(i=0;i<camel_MAX_NUM_OPR_CODES;i++) {
83     p_counter->camel_msg[i]=0; 
84   }
85 }
86
87
88 /*
89  * If there is a valid camel operation, increase the value in the array of counter
90  */
91 static int gtk_camelcounter_packet(void *phs,
92                                    packet_info *pinfo _U_,
93                                    epan_dissect_t *edt _U_, 
94                                    const void *phi)
95 {
96   struct camelcounter_t * p_counter =(struct camelcounter_t *)phs;
97   const struct camelsrt_info_t * pi=phi;
98   if (pi->opcode != 255)
99     p_counter->camel_msg[pi->opcode]++;
100   
101   return 1;
102 }
103
104 static void gtk_camelcounter_draw(void *phs)
105 {
106   struct camelcounter_t *p_counter=(struct camelcounter_t *)phs;
107   int i;
108   char *str[2];
109   
110   for(i=0;i<2;i++) {
111     str[i]=g_malloc(sizeof(char[256]));
112   }
113   /* Now print Message and Reason Counter Table */
114   /* clear list before printing */
115   gtk_clist_clear(p_counter->table);
116   
117   for(i=0;i<camel_MAX_NUM_OPR_CODES;i++) {
118     /* Message counter */
119     if(p_counter->camel_msg[i]!=0) {
120       g_snprintf(str[0], sizeof(char[256]), 
121                  "Request %s", val_to_str(i,camel_opr_code_strings,"Unknown message "));
122       g_snprintf(str[1], sizeof(char[256]),
123                  "%d", p_counter->camel_msg[i]);
124       gtk_clist_append(p_counter->table, str);
125     } 
126   } /* Message Type */
127   gtk_widget_show(GTK_WIDGET(p_counter->table));
128 }
129
130 void protect_thread_critical_region(void);
131 void unprotect_thread_critical_region(void);
132
133 static void win_destroy_cb(GtkWindow *win _U_, gpointer data)
134 {
135   struct camelcounter_t *hs=(struct camelcounter_t *)data;
136   
137   protect_thread_critical_region();
138   remove_tap_listener(hs);
139   unprotect_thread_critical_region();
140   
141   if(hs->filter){
142     g_free(hs->filter);
143     hs->filter=NULL;
144   }
145   g_free(hs);
146 }
147
148 static const gchar *titles[]={
149   "Message Type or Reason",
150   "Count" };
151
152 static void gtk_camelcounter_init(const char *optarg, void *userdata _U_)
153 {
154   struct camelcounter_t *p_camelcounter;
155   const char *filter=NULL; 
156   const char *emptyfilter=""; 
157   GString *error_string;
158   GtkWidget *bbox;
159   GtkWidget *close_bt;
160   
161   if(strncmp(optarg,"camel,counter,",14) == 0){
162     filter=optarg+14;
163   } else {
164     filter=NULL;
165   }
166   
167   p_camelcounter=g_malloc(sizeof(struct camelcounter_t));
168   p_camelcounter->filter=g_strdup(filter);
169   
170   gtk_camelcounter_reset(p_camelcounter);
171   
172   p_camelcounter->win=window_new(GTK_WINDOW_TOPLEVEL, "Ethereal: CAMEL counters");
173   gtk_window_set_default_size(GTK_WINDOW(p_camelcounter->win), 500, 300);
174   
175   p_camelcounter->vbox=gtk_vbox_new(FALSE, 3);
176   gtk_container_set_border_width(GTK_CONTAINER(p_camelcounter->vbox), 12);
177   
178   init_main_stat_window(p_camelcounter->win, p_camelcounter->vbox, "CAMEL Messages Counters", filter);
179   
180   /* init a scrolled window*/
181   p_camelcounter->scrolled_window = scrolled_window_new(NULL, NULL);
182
183   p_camelcounter->table = create_stat_table(p_camelcounter->scrolled_window, p_camelcounter->vbox, 2, titles);
184   
185   if (filter) {
186     error_string=register_tap_listener("CAMEL", p_camelcounter, filter,
187                                        gtk_camelcounter_reset,
188                                        gtk_camelcounter_packet,
189                                        gtk_camelcounter_draw);
190   } else {
191     error_string=register_tap_listener("CAMEL", p_camelcounter, emptyfilter,
192                                        gtk_camelcounter_reset,
193                                        gtk_camelcounter_packet,
194                                        gtk_camelcounter_draw);
195   }
196
197   if(error_string){
198     simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, error_string->str);
199     g_string_free(error_string, TRUE);
200     g_free(p_camelcounter);
201     return;
202   }
203
204   /* Button row. */
205   bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
206   gtk_box_pack_end(GTK_BOX(p_camelcounter->vbox), bbox, FALSE, FALSE, 0);
207
208   close_bt = OBJECT_GET_DATA(bbox, GTK_STOCK_CLOSE);
209   window_set_cancel_button(p_camelcounter->win, close_bt, window_cancel_button_cb);
210
211   SIGNAL_CONNECT(p_camelcounter->win, "delete_event", window_delete_event_cb, NULL);
212   SIGNAL_CONNECT(p_camelcounter->win, "destroy", win_destroy_cb, p_camelcounter);
213
214   gtk_widget_show_all(p_camelcounter->win);
215   window_present(p_camelcounter->win);
216
217   cf_retap_packets(&cfile, FALSE);
218 }
219
220 static tap_dfilter_dlg camel_counter_dlg = {
221   "CAMEL Messages and Response Status",
222   "camel,counter",
223   gtk_camelcounter_init,
224   -1
225 };
226
227 void  /* Next line mandatory */
228 register_tap_listener_gtk_camelcounter(void)
229 {
230   register_dfilter_stat(&camel_counter_dlg, "GSM/CAMEL",
231                         REGISTER_STAT_GROUP_TELEPHONY);
232
233 }