carve out the (currently disabled) welcome page into it's own file to slightly reduce...
[obnox/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
36 #include "simple_dialog.h"
37 #include "gui_utils.h"
38 #include "dlg_utils.h"
39 #include <epan/tap.h>
40 #include "../register.h"
41 #include "../globals.h"
42 #include "compat_macros.h"
43 #include "../stat_menu.h"
44 #include "../tap_dfilter_dlg.h"
45
46 typedef const char* bootp_info_value_t;
47
48 /* used to keep track of the statictics 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 /* used to keep track of a single DHCP message type */
57 typedef struct _dhcp_message_type_t {
58         const char      *name;
59         guint32          packets;
60         GtkWidget       *widget;/* label in which we print the number of packets */
61         dhcpstat_t      *sp;    /* entire program interface */
62 } dhcp_message_type_t;
63
64 static void
65 dhcp_free_hash( gpointer key _U_ , gpointer value, gpointer user_data _U_ )
66 {
67         g_free(value);
68 }
69 static void
70 dhcp_reset_hash(gchar *key _U_ , dhcp_message_type_t *data, gpointer ptr _U_ ) 
71 {       
72         data->packets = 0;
73 }
74
75 /* Update the entry corresponding to the number of packets of a special DHCP Message Type
76  * or create it if it don't exist.
77  */
78 static void
79 dhcp_draw_message_type(gchar *key _U_, dhcp_message_type_t *data, gchar * unused _U_ )
80 {
81         char string_buff[256];
82
83         if ((data==NULL) || (data->packets==0))
84                 return;
85         if (data->widget==NULL){        /* create an entry in the table */
86                 GtkWidget       *tmp;
87                 int x = 2*((data->sp->index) % 2);
88                 int y = (data->sp->index) /2;
89
90
91                 /* Maybe we should display the hexadecimal value ? */
92                 /* g_snprintf(string_buff, 256, "%s  (0X%x)", data->name, *key); */
93                 tmp = gtk_label_new( data->name  /* string_buff */ );
94                 gtk_table_attach_defaults(GTK_TABLE(data->sp->table_message_type), tmp, x, x+1, y, y+1);
95                 gtk_label_set_justify(GTK_LABEL(tmp), GTK_JUSTIFY_LEFT);
96                 gtk_widget_show(tmp);
97
98                 g_snprintf( string_buff, 256, "%9d", data->packets );
99                 data->widget = gtk_label_new( string_buff );
100                 gtk_table_attach_defaults(GTK_TABLE(data->sp->table_message_type), data->widget, x+1, x+2, y, y+1);
101                 gtk_label_set_justify(GTK_LABEL(data->widget), GTK_JUSTIFY_LEFT);
102                 gtk_widget_show( data->widget );
103
104                 data->sp->index++;
105         } else {
106                 /* Just update the label string */
107                 g_snprintf( string_buff, 256, "%9d", data->packets );
108                 gtk_label_set( GTK_LABEL(data->widget), string_buff);
109         }
110 }
111 static void
112 dhcpstat_reset(void *psp)
113 {
114         dhcpstat_t *sp=psp;
115         g_hash_table_foreach( sp->hash, (GHFunc)dhcp_reset_hash, NULL); 
116 }
117 static int
118 dhcpstat_packet(void *psp, packet_info *pinfo _U_, epan_dissect_t *edt _U_, const void *pri)
119 {
120         dhcpstat_t *sp=psp;
121         const bootp_info_value_t value=pri;
122         dhcp_message_type_t *sc;
123
124         if (sp==NULL)
125                 return 0;
126         sc = g_hash_table_lookup( 
127                         sp->hash, 
128                         value);
129         if (!sc) {
130                 /*g_warning("%s:%d What's Wrong for %s, doc ?", __FILE__, __LINE__, value);*/
131                 sc = g_malloc( sizeof(dhcp_message_type_t) );
132                 sc -> packets = 1;
133                 sc -> name = value;
134                 sc -> widget=NULL;
135                 sc -> sp = sp;
136                 g_hash_table_insert(
137                                 sp->hash,
138                                 (gpointer) value,
139                                 sc);
140         } else {
141                 /*g_warning("sc(%s)->packets++", sc->name);*/
142                 sc->packets++;
143         }
144         return 1;
145 }
146
147
148 static void
149 dhcpstat_draw(void *psp)
150 {
151         dhcpstat_t *sp=psp;
152         guint index;
153
154         index=sp->index;
155         g_hash_table_foreach( sp->hash, (GHFunc) dhcp_draw_message_type, NULL );
156         if (index != sp->index){
157                 /* We have inserted a new entry corresponding to a status code ,
158                  * let's resize the table */
159                 gtk_table_resize ( GTK_TABLE(sp->table_message_type), sp->index  % 2 , 4);
160         }
161         
162 }
163
164
165
166 /* since the gtk2 implementation of tap is multithreaded we must protect
167  * remove_tap_listener() from modifying the list while draw_tap_listener()
168  * is running.  the other protected block is in main.c
169  *
170  * there should not be any other critical regions in gtk2
171  */
172 void protect_thread_critical_region(void);
173 void unprotect_thread_critical_region(void);
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         /* top level window */
221         sp->win = window_new(GTK_WINDOW_TOPLEVEL, title);
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                         dhcpstat_reset,
245                         dhcpstat_packet,
246                         dhcpstat_draw);
247         if (error_string){
248                 /* error, we failed to attach to the tap. clean up */
249                 simple_dialog( ESD_TYPE_ERROR, ESD_BTN_OK, error_string->str );
250                 g_free(sp->filter);
251                 g_free(sp);
252                 g_string_free(error_string, TRUE);
253                 return ;
254         }
255
256         /* Button row. */
257         bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
258         gtk_box_pack_start(GTK_BOX(vbox), bbox, FALSE, FALSE, 0);
259
260         bt_close = OBJECT_GET_DATA(bbox, GTK_STOCK_CLOSE);
261         window_set_cancel_button(sp->win, bt_close, window_cancel_button_cb);
262
263         SIGNAL_CONNECT(sp->win, "delete_event", window_delete_event_cb, NULL);
264         SIGNAL_CONNECT(sp->win, "destroy", win_destroy_cb, sp);
265
266         gtk_widget_show_all(sp->win);
267
268         window_present(sp->win);
269
270         cf_retap_packets(&cfile, FALSE);
271 }
272
273 static tap_dfilter_dlg dhcp_stat_dlg = {
274         "BOOTP-DHCP Packet Counter",
275         "bootp,stat",
276         dhcpstat_init,
277         -1
278 };
279
280 void
281 register_tap_listener_gtkdhcpstat(void)
282 {
283         register_dfilter_stat(&dhcp_stat_dlg, "BOOTP-DHCP",
284             REGISTER_STAT_GROUP_NONE);
285 }