The rest of the Follow UDP Stream check-in
[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/asn1.h"
54 #include "epan/camel-persistentdata.h"
55
56 static void gtk_camelcounter_reset(void *phs);
57 static int gtk_camelcounter_packet(void *phs,
58                                    packet_info *pinfo _U_,
59                                    epan_dissect_t *edt _U_, 
60                                    const void *phi);
61 static void gtk_camelcounter_draw(void *phs);
62 static void win_destroy_cb(GtkWindow *win _U_, gpointer data);
63 static void gtk_camelcounter_init(const char *optarg, void *userdata _U_);
64 void register_tap_listener_gtk_camelcounter(void);
65
66 /* following values represent the size of their valuestring arrays */
67
68 struct camelcounter_t {
69   GtkWidget *win;
70   GtkWidget *vbox;
71   char *filter;
72   GtkWidget *scrolled_window;
73   GtkCList *table;
74   guint32 camel_msg[camel_MAX_NUM_OPR_CODES];
75 };
76
77 static void gtk_camelcounter_reset(void *phs)
78 {
79   struct camelcounter_t * p_counter= ( struct camelcounter_t *) phs;
80   int i;
81   
82   /* Erase Message Type count */
83   for(i=0;i<camel_MAX_NUM_OPR_CODES;i++) {
84     p_counter->camel_msg[i]=0; 
85   }
86 }
87
88
89 /*
90  * If there is a valid camel operation, increase the value in the array of counter
91  */
92 static int gtk_camelcounter_packet(void *phs,
93                                    packet_info *pinfo _U_,
94                                    epan_dissect_t *edt _U_, 
95                                    const void *phi)
96 {
97   struct camelcounter_t * p_counter =(struct camelcounter_t *)phs;
98   const struct camelsrt_info_t * pi=phi;
99   if (pi->opcode != 255)
100     p_counter->camel_msg[pi->opcode]++;
101   
102   return 1;
103 }
104
105 static void gtk_camelcounter_draw(void *phs)
106 {
107   struct camelcounter_t *p_counter=(struct camelcounter_t *)phs;
108   int i;
109   char *str[2];
110   
111   for(i=0;i<2;i++) {
112     str[i]=g_malloc(sizeof(char[256]));
113   }
114   /* Now print Message and Reason Counter Table */
115   /* clear list before printing */
116   gtk_clist_clear(p_counter->table);
117   
118   for(i=0;i<camel_MAX_NUM_OPR_CODES;i++) {
119     /* Message counter */
120     if(p_counter->camel_msg[i]!=0) {
121       g_snprintf(str[0], sizeof(char[256]), 
122                  "Request %s", val_to_str(i,camel_opr_code_strings,"Unknown message "));
123       g_snprintf(str[1], sizeof(char[256]),
124                  "%d", p_counter->camel_msg[i]);
125       gtk_clist_append(p_counter->table, str);
126     } 
127   } /* Message Type */
128   gtk_widget_show(GTK_WIDGET(p_counter->table));
129 }
130
131 void protect_thread_critical_region(void);
132 void unprotect_thread_critical_region(void);
133
134 static void win_destroy_cb(GtkWindow *win _U_, gpointer data)
135 {
136   struct camelcounter_t *hs=(struct camelcounter_t *)data;
137   
138   protect_thread_critical_region();
139   remove_tap_listener(hs);
140   unprotect_thread_critical_region();
141   
142   if(hs->filter){
143     g_free(hs->filter);
144     hs->filter=NULL;
145   }
146   g_free(hs);
147 }
148
149 static const gchar *titles[]={
150   "Message Type or Reason",
151   "Count" };
152
153 static void gtk_camelcounter_init(const char *optarg, void *userdata _U_)
154 {
155   struct camelcounter_t *p_camelcounter;
156   const char *filter=NULL; 
157   const char *emptyfilter=""; 
158   GString *error_string;
159   GtkWidget *bbox;
160   GtkWidget *close_bt;
161   
162   if(strncmp(optarg,"camel,counter,",14) == 0){
163     filter=optarg+14;
164   } else {
165     filter=NULL;
166   }
167   
168   p_camelcounter=g_malloc(sizeof(struct camelcounter_t));
169   p_camelcounter->filter=g_strdup(filter);
170   
171   gtk_camelcounter_reset(p_camelcounter);
172   
173   p_camelcounter->win=window_new(GTK_WINDOW_TOPLEVEL, "Wireshark: CAMEL counters");
174   gtk_window_set_default_size(GTK_WINDOW(p_camelcounter->win), 500, 300);
175   
176   p_camelcounter->vbox=gtk_vbox_new(FALSE, 3);
177   gtk_container_set_border_width(GTK_CONTAINER(p_camelcounter->vbox), 12);
178   
179   init_main_stat_window(p_camelcounter->win, p_camelcounter->vbox, "CAMEL Messages Counters", filter);
180   
181   /* init a scrolled window*/
182   p_camelcounter->scrolled_window = scrolled_window_new(NULL, NULL);
183
184   p_camelcounter->table = create_stat_table(p_camelcounter->scrolled_window, p_camelcounter->vbox, 2, titles);
185   
186   if (filter) {
187     error_string=register_tap_listener("CAMEL", p_camelcounter, filter,
188                                        gtk_camelcounter_reset,
189                                        gtk_camelcounter_packet,
190                                        gtk_camelcounter_draw);
191   } else {
192     error_string=register_tap_listener("CAMEL", p_camelcounter, emptyfilter,
193                                        gtk_camelcounter_reset,
194                                        gtk_camelcounter_packet,
195                                        gtk_camelcounter_draw);
196   }
197
198   if(error_string){
199     simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, error_string->str);
200     g_string_free(error_string, TRUE);
201     g_free(p_camelcounter);
202     return;
203   }
204
205   /* Button row. */
206   bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
207   gtk_box_pack_end(GTK_BOX(p_camelcounter->vbox), bbox, FALSE, FALSE, 0);
208
209   close_bt = OBJECT_GET_DATA(bbox, GTK_STOCK_CLOSE);
210   window_set_cancel_button(p_camelcounter->win, close_bt, window_cancel_button_cb);
211
212   SIGNAL_CONNECT(p_camelcounter->win, "delete_event", window_delete_event_cb, NULL);
213   SIGNAL_CONNECT(p_camelcounter->win, "destroy", win_destroy_cb, p_camelcounter);
214
215   gtk_widget_show_all(p_camelcounter->win);
216   window_present(p_camelcounter->win);
217
218   cf_retap_packets(&cfile, FALSE);
219 }
220
221 static tap_dfilter_dlg camel_counter_dlg = {
222   "CAMEL Messages and Response Status",
223   "camel,counter",
224   gtk_camelcounter_init,
225   -1
226 };
227
228 void  /* Next line mandatory */
229 register_tap_listener_gtk_camelcounter(void)
230 {
231   register_dfilter_stat(&camel_counter_dlg, "GSM/CAMEL",
232                         REGISTER_STAT_GROUP_TELEPHONY);
233
234 }