Remove unneeded includes from ui folder
[metze/wireshark/wip.git] / ui / gtk / bootp_stat.c
1 /* bootp_stat.c
2  * boop_stat   2003 Jean-Michel FAYARD
3  *
4  * Wireshark - Network traffic analyzer
5  * By Gerald Combs <gerald@wireshark.org>
6  * Copyright 1998 Gerald Combs
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 /* #define DEBUG        do{ printf("%s:%d  ",__FILE__,__LINE__);} while(0); */
24 #include "config.h"
25
26 #include <gtk/gtk.h>
27 #include <string.h>
28
29 #include <epan/packet_info.h>
30 #include <epan/tap.h>
31
32
33 #include "ui/simple_dialog.h"
34
35 #include "ui/gtk/gui_utils.h"
36 #include "ui/gtk/dlg_utils.h"
37 #include "ui/gtk/tap_param_dlg.h"
38 #include "ui/gtk/main.h"
39
40
41 void register_tap_listener_gtkdhcpstat(void);
42
43 typedef const char *bootp_info_value_t;
44
45 /* used to keep track of the statistics for an entire program interface */
46 typedef struct _dhcp_stats_t {
47         char       *filter;
48         GtkWidget  *win;
49         GHashTable *hash;
50         GtkWidget  *grid_message_type;
51         guint       index;      /* Number of  to display */
52 } dhcpstat_t;
53
54 /* used to keep track of a single DHCP message type */
55 typedef struct _dhcp_message_type_t {
56         const char *name;
57         guint32     packets;
58         GtkWidget  *widget;     /* label in which we print the number of packets */
59         dhcpstat_t *sp;         /* entire program interface */
60 } dhcp_message_type_t;
61
62 static void
63 dhcp_reset_hash(gchar *key _U_ , dhcp_message_type_t *data, gpointer ptr _U_)
64 {
65         data->packets = 0;
66 }
67
68 /* Update the entry corresponding to the number of packets of a special DHCP Message Type
69  * or create it if it don't exist.
70  */
71 static void
72 dhcp_draw_message_type(gchar *key _U_, dhcp_message_type_t *data, gchar *unused _U_)
73 {
74         char string_buff[256];
75
76         if ((data == NULL) || (data->packets == 0))
77                 return;
78         if (data->widget == NULL) {     /* create an entry in the table */
79                 GtkWidget *tmp;
80                 int x = 2*((data->sp->index) % 2);
81                 int y = (data->sp->index) / 2;
82
83                 /* Maybe we should display the hexadecimal value ? */
84                 /* g_snprintf(string_buff, sizeof(string_buff), "%s  (0X%x)", data->name, *key); */
85                 tmp = gtk_label_new(data->name  /* string_buff */);
86                 ws_gtk_grid_attach_extended(GTK_GRID(data->sp->grid_message_type), tmp, x, y, 1, 1,
87                                             (GtkAttachOptions)(GTK_EXPAND|GTK_FILL), (GtkAttachOptions)0, 0, 0);
88                 gtk_label_set_justify(GTK_LABEL(tmp), GTK_JUSTIFY_LEFT);
89                 gtk_widget_show(tmp);
90
91                 g_snprintf(string_buff, sizeof(string_buff), "%9d", data->packets);
92                 data->widget = gtk_label_new(string_buff);
93                 ws_gtk_grid_attach_extended(GTK_GRID(data->sp->grid_message_type), data->widget, x+1, y, 1, 1,
94                                             (GtkAttachOptions)(GTK_EXPAND|GTK_FILL), (GtkAttachOptions)0, 0, 0);
95                 gtk_label_set_justify(GTK_LABEL(data->widget), GTK_JUSTIFY_LEFT);
96                 gtk_widget_show(data->widget);
97
98                 data->sp->index++;
99         } else {
100                 /* Just update the label string */
101                 g_snprintf(string_buff, sizeof(string_buff), "%9d", data->packets);
102                 gtk_label_set_text(GTK_LABEL(data->widget), string_buff);
103         }
104 }
105 static void
106 dhcpstat_reset(void *psp)
107 {
108         dhcpstat_t *sp = (dhcpstat_t *)psp;
109         g_hash_table_foreach(sp->hash, (GHFunc)dhcp_reset_hash, NULL);
110 }
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    = (dhcpstat_t *)psp;
116         const bootp_info_value_t  value = (const bootp_info_value_t)pri;
117         dhcp_message_type_t      *sc;
118
119         if (sp == NULL)
120                 return FALSE;
121
122         sc = (dhcp_message_type_t *)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 = (dhcp_message_type_t *)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 = (dhcpstat_t *)psp;
149
150         g_hash_table_foreach(sp->hash, (GHFunc)dhcp_draw_message_type, NULL);
151 }
152
153 static void
154 win_destroy_cb(GtkWindow *win _U_, gpointer data)
155 {
156         dhcpstat_t *sp = (dhcpstat_t *)data;
157
158         remove_tap_listener(sp);
159
160         g_free(sp->filter);
161         g_hash_table_destroy(sp->hash);
162         g_free(sp);
163 }
164
165
166 /* When called, this function will create a new instance of gtk2-dhcpstat.
167  */
168 static void
169 dhcpstat_init(const char *opt_arg, void *userdata _U_)
170 {
171         dhcpstat_t *sp;
172         const char *filter;
173         char       *title;
174         GString    *error_string;
175         GtkWidget  *message_type_fr;
176         GtkWidget  *vbox;
177         GtkWidget  *bt_close;
178         GtkWidget  *bbox;
179
180         if (strncmp(opt_arg, "bootp,stat,", 11) == 0) {
181                 filter = opt_arg+11;
182         } else {
183                 filter = NULL;
184         }
185
186         sp = (dhcpstat_t *)g_malloc(sizeof(dhcpstat_t));
187         sp->hash = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
188         if(filter) {
189                 sp->filter = g_strdup(filter);
190                 title = g_strdup_printf("DHCP statistics with filter: %s", filter);
191         } else {
192                 sp->filter = NULL;
193                 title = g_strdup("DHCP statistics");
194         }
195
196         /* transient_for top_level */
197         sp->win = dlg_window_new(title);
198         gtk_window_set_destroy_with_parent(GTK_WINDOW(sp->win), TRUE);
199         g_free(title);
200
201         vbox = ws_gtk_box_new(GTK_ORIENTATION_VERTICAL, 3, FALSE);
202         gtk_container_add(GTK_CONTAINER(sp->win), vbox);
203         gtk_container_set_border_width(GTK_CONTAINER(vbox), 12);
204
205         /* Status Codes frame */
206         message_type_fr = gtk_frame_new("DHCP Message Type");
207         gtk_box_pack_start(GTK_BOX(vbox), message_type_fr, TRUE, TRUE, 0);
208         gtk_widget_show(message_type_fr);
209
210         sp->grid_message_type = ws_gtk_grid_new();
211         ws_gtk_grid_set_column_spacing(GTK_GRID(sp->grid_message_type), 10);
212         gtk_container_add(GTK_CONTAINER(message_type_fr), sp->grid_message_type);
213         gtk_container_set_border_width(GTK_CONTAINER(sp->grid_message_type) , 10);
214         sp->index = 0;          /* Nothing to display yet */
215
216
217         error_string = register_tap_listener(
218                         "bootp",
219                         sp,
220                         filter,
221                         0,
222                         dhcpstat_reset,
223                         dhcpstat_packet,
224                         dhcpstat_draw);
225         if (error_string) {
226                 /* error, we failed to attach to the tap. clean up */
227                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", error_string->str);
228                 g_free(sp->filter);
229                 g_free(sp);
230                 g_string_free(error_string, TRUE);
231                 return ;
232         }
233
234         /* Button row. */
235         bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
236         gtk_box_pack_start(GTK_BOX(vbox), bbox, FALSE, FALSE, 0);
237
238         bt_close = (GtkWidget *)g_object_get_data(G_OBJECT(bbox), GTK_STOCK_CLOSE);
239         window_set_cancel_button(sp->win, bt_close, window_cancel_button_cb);
240
241         g_signal_connect(sp->win, "delete_event", G_CALLBACK(window_delete_event_cb), NULL);
242         g_signal_connect(sp->win, "destroy", G_CALLBACK(win_destroy_cb), sp);
243
244         gtk_widget_show_all(sp->win);
245
246         window_present(sp->win);
247
248         cf_retap_packets(&cfile);
249         gdk_window_raise(gtk_widget_get_window(sp->win));
250 }
251
252 static tap_param bootp_stat_params[] = {
253         { PARAM_FILTER, "filter", "Filter", NULL, TRUE }
254 };
255
256 static tap_param_dlg dhcp_stat_dlg = {
257         "BOOTP-DHCP Packet Counter",
258         "bootp,stat",
259         dhcpstat_init,
260         -1,
261         G_N_ELEMENTS(bootp_stat_params),
262         bootp_stat_params
263 };
264
265 void
266 register_tap_listener_gtkdhcpstat(void)
267 {
268         register_param_stat(&dhcp_stat_dlg, "BOOTP-DHCP",
269             REGISTER_STAT_GROUP_UNSORTED);
270 }