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