Add userdata arguments to a bunch of stat initialization routines to
[obnox/wireshark/wip.git] / gtk / smb2_stat.c
1 /* smb2_stat.c
2  * smb2_stat   2005 Ronnie Sahlberg
3  *
4  * $Id$
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
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 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #ifdef HAVE_SYS_TYPES_H
30 # include <sys/types.h>
31 #endif
32
33 #include <string.h>
34
35 #include <gtk/gtk.h>
36
37 #include <epan/packet_info.h>
38 #include <epan/epan.h>
39 #include <epan/value_string.h>
40
41 #include <epan/tap.h>
42 #include <epan/dissectors/packet-smb2.h>
43 #include "../register.h"
44 #include "../timestats.h"
45 #include "compat_macros.h"
46 #include "../simple_dialog.h"
47 #include "gui_utils.h"
48 #include "dlg_utils.h"
49 #include "../file.h"
50 #include "../globals.h"
51 #include "filter_dlg.h"
52 #include "service_response_time_table.h"
53 #include "../stat_menu.h"
54 #include "../tap_dfilter_dlg.h"
55 #include "gtkglobals.h"
56
57
58 /* used to keep track of the statistics for an entire program interface */
59 typedef struct _smb2stat_t {
60         GtkWidget *win;
61         srt_stat_table smb2_srt_table;
62 } smb2stat_t;
63
64 static void
65 smb2stat_set_title(smb2stat_t *ss)
66 {
67         char *title;
68
69         title = g_strdup_printf("SMB2 Service Response Time statistics: %s",
70             cf_get_display_name(&cfile));
71         gtk_window_set_title(GTK_WINDOW(ss->win), title);
72         g_free(title);
73 }
74
75 static void
76 smb2stat_reset(void *pss)
77 {
78         smb2stat_t *ss=(smb2stat_t *)pss;
79
80         reset_srt_table_data(&ss->smb2_srt_table);
81         smb2stat_set_title(ss);
82 }
83
84 static int
85 smb2stat_packet(void *pss, packet_info *pinfo, epan_dissect_t *edt _U_, const void *psi)
86 {
87         smb2stat_t *ss=(smb2stat_t *)pss;
88         const smb2_info_t *si=psi;
89
90         /* we are only interested in response packets */
91         if(!(si->flags&SMB2_FLAGS_RESPONSE)){
92                 return 0;
93         }
94         /* if we havnt seen the request, just ignore it */
95         if(!si->saved){
96                 return 0;
97         }
98
99         add_srt_table_data(&ss->smb2_srt_table, si->opcode, &si->saved->req_time, pinfo);
100
101         return 1;
102 }
103
104
105
106 static void
107 smb2stat_draw(void *pss)
108 {
109         smb2stat_t *ss=(smb2stat_t *)pss;
110
111         draw_srt_table_data(&ss->smb2_srt_table);
112 }
113
114
115 void protect_thread_critical_region(void);
116 void unprotect_thread_critical_region(void);
117 static void
118 win_destroy_cb(GtkWindow *win _U_, gpointer data)
119 {
120         smb2stat_t *ss=(smb2stat_t *)data;
121
122         protect_thread_critical_region();
123         remove_tap_listener(ss);
124         unprotect_thread_critical_region();
125
126         free_srt_table_data(&ss->smb2_srt_table);
127         g_free(ss);
128 }
129
130
131 static void
132 gtk_smb2stat_init(const char *optarg, void *userdata _U_)
133 {
134         smb2stat_t *ss;
135         const char *filter=NULL;
136         GtkWidget *label;
137         char filter_string[256];
138         GString *error_string;
139         int i;
140         GtkWidget *vbox;
141         GtkWidget *bbox;
142         GtkWidget *close_bt;
143
144         if(!strncmp(optarg,"smb2,srt,",9)){
145                 filter=optarg+8;
146         } else {
147                 filter=NULL;
148         }
149
150         ss=g_malloc(sizeof(smb2stat_t));
151
152         ss->win=window_new(GTK_WINDOW_TOPLEVEL, "smb2-stat");
153         gtk_window_set_default_size(GTK_WINDOW(ss->win), 550, 400);
154         smb2stat_set_title(ss);
155
156         vbox=gtk_vbox_new(FALSE, 3);
157         gtk_container_add(GTK_CONTAINER(ss->win), vbox);
158         gtk_container_set_border_width(GTK_CONTAINER(vbox), 12);
159
160         label=gtk_label_new("SMB2 Service Response Time statistics");
161         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
162
163         g_snprintf(filter_string,255,"Filter:%s",filter?filter:"");
164         label=gtk_label_new(filter_string);
165         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
166
167         label=gtk_label_new("SMB2 Commands");
168         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
169
170         /* We must display TOP LEVEL Widget before calling init_srt_table() */
171         gtk_widget_show_all(ss->win);
172
173         init_srt_table(&ss->smb2_srt_table, 256, vbox, "smb2.cmd");
174         for(i=0;i<256;i++){
175                 init_srt_table_row(&ss->smb2_srt_table, i, val_to_str(i, smb2_cmd_vals, "Unknown(0x%02x)"));
176         }
177
178
179         error_string=register_tap_listener("smb2", ss, filter, smb2stat_reset, smb2stat_packet, smb2stat_draw);
180         if(error_string){
181                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, error_string->str);
182                 g_string_free(error_string, TRUE);
183                 g_free(ss);
184                 return;
185         }
186
187         /* Button row. */
188         bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
189         gtk_box_pack_end(GTK_BOX(vbox), bbox, FALSE, FALSE, 0);
190
191         close_bt = OBJECT_GET_DATA(bbox, GTK_STOCK_CLOSE);
192         window_set_cancel_button(ss->win, close_bt, window_cancel_button_cb);
193
194         SIGNAL_CONNECT(ss->win, "delete_event", window_delete_event_cb, NULL);
195         SIGNAL_CONNECT(ss->win, "destroy", win_destroy_cb, ss);
196
197         gtk_widget_show_all(ss->win);
198         window_present(ss->win);
199         
200         cf_retap_packets(&cfile, FALSE);
201 }
202
203 static tap_dfilter_dlg smb2_stat_dlg = {
204         "SMB2 SRT Statistics",
205         "smb2,srt",
206         gtk_smb2stat_init,
207         -1
208 };
209
210 void
211 register_tap_listener_gtksmb2stat(void)
212 {
213         register_dfilter_stat(&smb2_stat_dlg, "SMB2",
214             REGISTER_STAT_GROUP_RESPONSE_TIME);
215 }