2 * boop_stat 2003 Jean-Michel FAYARD
6 * Ethereal - Network traffic analyzer
7 * By Gerald Combs <gerald@ethereal.com>
8 * Copyright 1998 Gerald Combs
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.
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.
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.
25 /* #define DEBUG do{ printf("%s:%d ",__FILE__,__LINE__);} while(0); */
33 #include <epan/packet_info.h>
34 #include <epan/epan.h>
37 #include "simple_dialog.h"
39 #include "dlg_utils.h"
41 #include "../register.h"
42 #include "../globals.h"
43 #include "compat_macros.h"
44 #include "../tap_dfilter_dlg.h"
45 #include "tap_dfilter_dlg.h"
47 typedef const char* bootp_info_value_t;
49 /* used to keep track of the statictics for an entire program interface */
50 typedef struct _dhcp_stats_t {
54 GtkWidget *table_message_type;
55 guint index; /* Number of to display */
57 /* used to keep track of a single DHCP message type */
58 typedef struct _dhcp_message_type_t {
61 GtkWidget *widget;/* label in which we print the number of packets */
62 dhcpstat_t *sp; /* entire program interface */
63 } dhcp_message_type_t;
66 dhcp_free_hash( gpointer key _U_ , gpointer value, gpointer user_data _U_ )
71 dhcp_reset_hash(gchar *key _U_ , dhcp_message_type_t *data, gpointer ptr _U_ )
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.
80 dhcp_draw_message_type(gchar *key _U_, dhcp_message_type_t *data, gchar * unused _U_ )
82 char string_buff[256];
84 if ((data==NULL) || (data->packets==0))
86 if (data->widget==NULL){ /* create an entry in the table */
88 int x = 2*((data->sp->index) % 2);
89 int y = (data->sp->index) /2;
92 /* Maybe we should display the hexadecimal value ? */
93 /* g_snprintf(string_buff, 256, "%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);
99 g_snprintf( string_buff, 256, "%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 );
107 /* Just update the label string */
108 g_snprintf( string_buff, 256, "%9d", data->packets );
109 gtk_label_set( GTK_LABEL(data->widget), string_buff);
113 dhcpstat_reset(void *psp)
116 g_hash_table_foreach( sp->hash, (GHFunc)dhcp_reset_hash, NULL);
119 dhcpstat_packet(void *psp, packet_info *pinfo _U_, epan_dissect_t *edt _U_, const void *pri)
122 const bootp_info_value_t value=pri;
123 dhcp_message_type_t *sc;
127 sc = g_hash_table_lookup(
131 /*g_warning("%s:%d What's Wrong for %s, doc ?", __FILE__, __LINE__, value);*/
132 sc = g_malloc( sizeof(dhcp_message_type_t) );
142 /*g_warning("sc(%s)->packets++", sc->name);*/
150 dhcpstat_draw(void *psp)
156 g_hash_table_foreach( sp->hash, (GHFunc) dhcp_draw_message_type, NULL );
157 if (index != 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);
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
171 * there should not be any other critical regions in gtk2
173 void protect_thread_critical_region(void);
174 void unprotect_thread_critical_region(void);
176 win_destroy_cb(GtkWindow *win _U_, gpointer data)
178 dhcpstat_t *sp=(dhcpstat_t *)data;
180 protect_thread_critical_region();
181 remove_tap_listener(sp);
182 unprotect_thread_critical_region();
185 g_hash_table_foreach( sp->hash, (GHFunc)dhcp_free_hash, NULL);
186 g_hash_table_destroy( sp->hash);
191 /* When called, this function will create a new instance of gtk2-dhcpstat.
194 dhcpstat_init(char *optarg)
199 GString *error_string;
200 GtkWidget *message_type_fr;
205 if (strncmp (optarg, "bootp,stat,", 11) == 0){
211 sp = g_malloc( sizeof(dhcpstat_t) );
212 sp->hash = g_hash_table_new( g_str_hash, g_str_equal);
214 sp->filter=g_strdup(filter);
215 title=g_strdup_printf("DHCP statistics with filter: %s", filter);
218 title=g_strdup("DHCP statistics");
221 /* top level window */
222 sp->win = window_new(GTK_WINDOW_TOPLEVEL, title);
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);
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);
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 */
241 error_string = register_tap_listener(
249 /* error, we failed to attach to the tap. clean up */
250 simple_dialog( ESD_TYPE_ERROR, ESD_BTN_OK, error_string->str );
253 g_string_free(error_string, TRUE);
258 bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
259 gtk_box_pack_start(GTK_BOX(vbox), bbox, FALSE, FALSE, 0);
261 bt_close = OBJECT_GET_DATA(bbox, GTK_STOCK_CLOSE);
262 window_set_cancel_button(sp->win, bt_close, window_cancel_button_cb);
264 SIGNAL_CONNECT(sp->win, "delete_event", window_delete_event_cb, NULL);
265 SIGNAL_CONNECT(sp->win, "destroy", win_destroy_cb, sp);
267 gtk_widget_show_all(sp->win);
269 window_present(sp->win);
271 cf_retap_packets(&cfile);
274 static tap_dfilter_dlg dhcp_stat_dlg = {
275 "BOOTP-DHCP Packet Counter",
282 register_tap_listener_gtkdhcpstat(void)
284 register_tap_listener_cmd_arg("bootp,stat", dhcpstat_init);
286 register_tap_menu_item("BOOTP-DHCP", REGISTER_TAP_GROUP_NONE,
287 gtk_tap_dfilter_dlg_cb, NULL, NULL, &(dhcp_stat_dlg));