packet-gsm_sim.c was the wrong file to check in.
[obnox/wireshark/wip.git] / ui / gtk / bootp_stat.c
1 /* bootp_stat.c
2  * boop_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
30 #include <gtk/gtk.h>
31 #include <string.h>
32
33 #include <epan/packet_info.h>
34 #include <epan/epan.h>
35 #include <epan/tap.h>
36
37 #include "../stat_menu.h"
38
39 #include "ui/simple_dialog.h"
40
41 #include "ui/gtk/gui_utils.h"
42 #include "ui/gtk/dlg_utils.h"
43 #include "ui/gtk/tap_param_dlg.h"
44 #include "ui/gtk/main.h"
45
46 #include "ui/gtk/old-gtk-compat.h"
47
48 typedef const char* bootp_info_value_t;
49
50 /* used to keep track of the statictics for an entire program interface */
51 typedef struct _dhcp_stats_t {
52         char            *filter;
53         GtkWidget       *win;
54         GHashTable      *hash;
55         GtkWidget       *table_message_type;
56         guint            index; /* Number of  to display */
57 } dhcpstat_t;
58 /* used to keep track of a single DHCP message type */
59 typedef struct _dhcp_message_type_t {
60         const char      *name;
61         guint32          packets;
62         GtkWidget       *widget;/* label in which we print the number of packets */
63         dhcpstat_t      *sp;    /* entire program interface */
64 } dhcp_message_type_t;
65
66 static void
67 dhcp_free_hash( gpointer key _U_ , gpointer value, gpointer user_data _U_ )
68 {
69         g_free(value);
70 }
71 static void
72 dhcp_reset_hash(gchar *key _U_ , dhcp_message_type_t *data, gpointer ptr _U_ )
73 {
74         data->packets = 0;
75 }
76
77 /* Update the entry corresponding to the number of packets of a special DHCP Message Type
78  * or create it if it don't exist.
79  */
80 static void
81 dhcp_draw_message_type(gchar *key _U_, dhcp_message_type_t *data, gchar * unused _U_ )
82 {
83         char string_buff[256];
84
85         if ((data==NULL) || (data->packets==0))
86                 return;
87         if (data->widget==NULL){        /* create an entry in the table */
88                 GtkWidget       *tmp;
89                 int x = 2*((data->sp->index) % 2);
90                 int y = (data->sp->index) /2;
91
92
93                 /* Maybe we should display the hexadecimal value ? */
94                 /* g_snprintf(string_buff, sizeof(string_buff), "%s  (0X%x)", data->name, *key); */
95                 tmp = gtk_label_new( data->name  /* string_buff */ );
96                 gtk_table_attach_defaults(GTK_TABLE(data->sp->table_message_type), tmp, x, x+1, y, y+1);
97                 gtk_label_set_justify(GTK_LABEL(tmp), GTK_JUSTIFY_LEFT);
98                 gtk_widget_show(tmp);
99
100                 g_snprintf( string_buff, sizeof(string_buff), "%9d", data->packets );
101                 data->widget = gtk_label_new( string_buff );
102                 gtk_table_attach_defaults(GTK_TABLE(data->sp->table_message_type), data->widget, x+1, x+2, y, y+1);
103                 gtk_label_set_justify(GTK_LABEL(data->widget), GTK_JUSTIFY_LEFT);
104                 gtk_widget_show( data->widget );
105
106                 data->sp->index++;
107         } else {
108                 /* Just update the label string */
109                 g_snprintf( string_buff, sizeof(string_buff), "%9d", data->packets );
110                 gtk_label_set_text( GTK_LABEL(data->widget), string_buff);
111         }
112 }
113 static void
114 dhcpstat_reset(void *psp)
115 {
116         dhcpstat_t *sp=psp;
117         g_hash_table_foreach( sp->hash, (GHFunc)dhcp_reset_hash, NULL);
118 }
119 static int
120 dhcpstat_packet(void *psp, packet_info *pinfo _U_, epan_dissect_t *edt _U_, const void *pri)
121 {
122         dhcpstat_t *sp=psp;
123         const bootp_info_value_t value=pri;
124         dhcp_message_type_t *sc;
125
126         if (sp==NULL)
127                 return 0;
128         sc = g_hash_table_lookup(
129                         sp->hash,
130                         value);
131         if (!sc) {
132                 /*g_warning("%s:%d What's Wrong for %s, doc ?", __FILE__, __LINE__, value);*/
133                 sc = g_malloc( sizeof(dhcp_message_type_t) );
134                 sc -> packets = 1;
135                 sc -> name = value;
136                 sc -> widget=NULL;
137                 sc -> sp = sp;
138                 g_hash_table_insert(
139                                 sp->hash,
140                                 (gpointer) value,
141                                 sc);
142         } else {
143                 /*g_warning("sc(%s)->packets++", sc->name);*/
144                 sc->packets++;
145         }
146         return 1;
147 }
148
149
150 static void
151 dhcpstat_draw(void *psp)
152 {
153         dhcpstat_t *sp=psp;
154         guint idx;
155
156         idx=sp->index;
157         g_hash_table_foreach( sp->hash, (GHFunc) dhcp_draw_message_type, NULL );
158         if (idx != sp->index){
159                 /* We have inserted a new entry corresponding to a status code ,
160                  * let's resize the table */
161                 gtk_table_resize ( GTK_TABLE(sp->table_message_type), sp->index  % 2 , 4);
162         }
163
164 }
165
166
167
168 /* since the gtk2 implementation of tap is multithreaded we must protect
169  * remove_tap_listener() from modifying the list while draw_tap_listener()
170  * is running.  the other protected block is in main.c
171  *
172  * there should not be any other critical regions in gtk2
173  */
174 static void
175 win_destroy_cb(GtkWindow *win _U_, gpointer data)
176 {
177         dhcpstat_t *sp=(dhcpstat_t *)data;
178
179         protect_thread_critical_region();
180         remove_tap_listener(sp);
181         unprotect_thread_critical_region();
182
183         g_free(sp->filter);
184         g_hash_table_foreach( sp->hash, (GHFunc)dhcp_free_hash, NULL);
185         g_hash_table_destroy( sp->hash);
186         g_free(sp);
187 }
188
189
190 /* When called, this function will create a new instance of gtk2-dhcpstat.
191  */
192 static void
193 dhcpstat_init(const char *optarg, void *userdata _U_)
194 {
195         dhcpstat_t *sp;
196         const char      *filter=NULL;
197         char            *title=NULL;
198         GString         *error_string;
199         GtkWidget       *message_type_fr;
200         GtkWidget       *vbox;
201         GtkWidget       *bt_close;
202         GtkWidget       *bbox;
203
204         if (strncmp (optarg, "bootp,stat,", 11) == 0){
205                 filter=optarg+11;
206         } else {
207                 filter=NULL;
208         }
209
210         sp = g_malloc( sizeof(dhcpstat_t) );
211         sp->hash = g_hash_table_new( g_str_hash, g_str_equal);
212         if(filter){
213                 sp->filter=g_strdup(filter);
214                 title=g_strdup_printf("DHCP statistics with filter: %s", filter);
215         } else {
216                 sp->filter=NULL;
217                 title=g_strdup("DHCP statistics");
218         }
219
220         /* transient_for top_level */
221         sp->win= dlg_window_new(title);
222         gtk_window_set_destroy_with_parent (GTK_WINDOW(sp->win), TRUE);
223         g_free(title);
224
225         vbox = gtk_vbox_new(FALSE, 3);
226         gtk_container_add(GTK_CONTAINER(sp->win), vbox);
227         gtk_container_set_border_width(GTK_CONTAINER(vbox), 12);
228
229         /* Status Codes frame */
230         message_type_fr = gtk_frame_new("DHCP Message Type");
231         gtk_container_add(GTK_CONTAINER(vbox), message_type_fr);
232         gtk_widget_show(message_type_fr);
233
234         sp->table_message_type = gtk_table_new( 0, 4, FALSE);
235         gtk_table_set_col_spacings( GTK_TABLE(sp->table_message_type), 10);
236         gtk_container_add( GTK_CONTAINER( message_type_fr), sp->table_message_type);
237         gtk_container_set_border_width( GTK_CONTAINER(sp->table_message_type) , 10);
238         sp->index = 0;          /* Nothing to display yet */
239
240
241         error_string = register_tap_listener(
242                         "bootp",
243                         sp,
244                         filter,
245                         0,
246                         dhcpstat_reset,
247                         dhcpstat_packet,
248                         dhcpstat_draw);
249         if (error_string){
250                 /* error, we failed to attach to the tap. clean up */
251                 simple_dialog( ESD_TYPE_ERROR, ESD_BTN_OK, "%s", error_string->str );
252                 g_free(sp->filter);
253                 g_free(sp);
254                 g_string_free(error_string, TRUE);
255                 return ;
256         }
257
258         /* Button row. */
259         bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
260         gtk_box_pack_start(GTK_BOX(vbox), bbox, FALSE, FALSE, 0);
261
262         bt_close = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_CLOSE);
263         window_set_cancel_button(sp->win, bt_close, window_cancel_button_cb);
264
265         g_signal_connect(sp->win, "delete_event", G_CALLBACK(window_delete_event_cb), NULL);
266         g_signal_connect(sp->win, "destroy", G_CALLBACK(win_destroy_cb), sp);
267
268         gtk_widget_show_all(sp->win);
269
270         window_present(sp->win);
271
272         cf_retap_packets(&cfile);
273         gdk_window_raise(gtk_widget_get_window(sp->win));
274 }
275
276 static tap_param bootp_stat_params[] = {
277         { PARAM_FILTER, "Filter", NULL }
278 };
279
280 static tap_param_dlg dhcp_stat_dlg = {
281         "BOOTP-DHCP Packet Counter",
282         "bootp,stat",
283         dhcpstat_init,
284         -1,
285         G_N_ELEMENTS(bootp_stat_params),
286         bootp_stat_params
287 };
288
289 void
290 register_tap_listener_gtkdhcpstat(void)
291 {
292         register_dfilter_stat(&dhcp_stat_dlg, "BOOTP-DHCP",
293             REGISTER_STAT_GROUP_UNSORTED);
294 }
295
296
297 void bootp_dhcp_stat_cb(GtkAction *action, gpointer user_data _U_)
298 {
299         tap_param_dlg_cb(action, &dhcp_stat_dlg);
300 }
301