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