Update to IO-Stat to put the capture filename on the titlebar
[obnox/wireshark/wip.git] / gtk / smb_stat.c
1 /* smb_stat.c
2  * smb_stat   2003 Ronnie Sahlberg
3  *
4  * $Id: smb_stat.c,v 1.11 2003/08/25 11:06:32 sahlberg Exp $
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 #include <stdio.h>
30
31 #ifdef HAVE_SYS_TYPES_H
32 # include <sys/types.h>
33 #endif
34
35 #include <gtk/gtk.h>
36 #include <string.h>
37 #include "menu.h"
38 #include "../epan/packet_info.h"
39 #include "../tap.h"
40 #include "../epan/value_string.h"
41 #include "../smb.h"
42 #include "../register.h"
43 #include "../timestats.h"
44 #include "compat_macros.h"
45 #include "../simple_dialog.h"
46 #include "../file.h"
47 #include "../globals.h"
48 #include "service_response_time_table.h"
49
50 extern GtkWidget   *main_display_filter_widget;
51
52 /* used to keep track of the statistics for an entire program interface */
53 typedef struct _smbstat_t {
54         GtkWidget *win;
55         srt_stat_table smb_srt_table;
56         srt_stat_table trans2_srt_table;
57         srt_stat_table nt_trans_srt_table;
58 } smbstat_t;
59
60
61
62 static void
63 smbstat_reset(void *pss)
64 {
65         smbstat_t *ss=(smbstat_t *)pss;
66         char title[256];
67
68         reset_srt_table_data(&ss->smb_srt_table);
69         reset_srt_table_data(&ss->trans2_srt_table);
70         reset_srt_table_data(&ss->nt_trans_srt_table);
71         snprintf(title, 255, "SMB Service Response Time statistics: %s", cfile.filename);
72         gtk_window_set_title(GTK_WINDOW(ss->win), title);
73 }
74
75 static int
76 smbstat_packet(void *pss, packet_info *pinfo, epan_dissect_t *edt _U_, void *psi)
77 {
78         smbstat_t *ss=(smbstat_t *)pss;
79         smb_info_t *si=psi;
80
81         /* we are only interested in reply packets */
82         if(si->request){
83                 return 0;
84         }
85         /* if we havnt seen the request, just ignore it */
86         if(!si->sip){
87                 return 0;
88         }
89
90         add_srt_table_data(&ss->smb_srt_table, si->cmd, &si->sip->req_time, pinfo);
91
92         if(si->cmd==0xA0){
93                 smb_nt_transact_info_t *sti=(smb_nt_transact_info_t *)si->sip->extra_info;
94
95                 add_srt_table_data(&ss->nt_trans_srt_table, sti->subcmd, &si->sip->req_time, pinfo);
96         } else if(si->cmd==0x32){
97                 smb_transact2_info_t *st2i=(smb_transact2_info_t *)si->sip->extra_info;
98
99                 add_srt_table_data(&ss->trans2_srt_table, st2i->subcmd, &si->sip->req_time, pinfo);
100         }
101
102         return 1;
103 }
104
105
106
107 static void
108 smbstat_draw(void *pss)
109 {
110         smbstat_t *ss=(smbstat_t *)pss;
111
112         draw_srt_table_data(&ss->smb_srt_table);
113         draw_srt_table_data(&ss->trans2_srt_table);
114         draw_srt_table_data(&ss->nt_trans_srt_table);
115 }
116
117
118 void protect_thread_critical_region(void);
119 void unprotect_thread_critical_region(void);
120 static void
121 win_destroy_cb(GtkWindow *win _U_, gpointer data)
122 {
123         smbstat_t *ss=(smbstat_t *)data;
124
125         protect_thread_critical_region();
126         remove_tap_listener(ss);
127         unprotect_thread_critical_region();
128
129         free_srt_table_data(&ss->smb_srt_table);
130         free_srt_table_data(&ss->trans2_srt_table);
131         free_srt_table_data(&ss->nt_trans_srt_table);
132         g_free(ss);
133 }
134
135
136 static void
137 gtk_smbstat_init(char *optarg)
138 {
139         smbstat_t *ss;
140         char *filter=NULL;
141         GtkWidget *label;
142         char filter_string[256];
143         GString *error_string;
144         int i;
145         GtkWidget *vbox;
146         char title[256];
147
148         if(!strncmp(optarg,"smb,srt,",8)){
149                 filter=optarg+8;
150         } else {
151                 filter=NULL;
152         }
153
154         ss=g_malloc(sizeof(smbstat_t));
155
156         ss->win=gtk_window_new(GTK_WINDOW_TOPLEVEL);
157         gtk_window_set_default_size(GTK_WINDOW(ss->win), 550, 600);
158         snprintf(title, 255, "SMB Service Response Time statistics: %s", cfile.filename);
159         gtk_window_set_title(GTK_WINDOW(ss->win), title);
160         SIGNAL_CONNECT(ss->win, "destroy", win_destroy_cb, ss);
161
162         vbox=gtk_vbox_new(FALSE, 0);
163         gtk_container_add(GTK_CONTAINER(ss->win), vbox);
164         gtk_container_set_border_width(GTK_CONTAINER(vbox), 10);
165         gtk_widget_show(vbox);
166
167         label=gtk_label_new("SMB Service Response Time statistics");
168         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
169         gtk_widget_show(label);
170
171         snprintf(filter_string,255,"Filter:%s",filter?filter:"");
172         label=gtk_label_new(filter_string);
173         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
174         gtk_widget_show(label);
175
176
177         label=gtk_label_new("SMB Commands");
178         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
179         gtk_widget_show(label);
180
181         /* We must display TOP LEVEL Widget before calling init_srt_table() */
182         gtk_widget_show(ss->win);
183
184         init_srt_table(&ss->smb_srt_table, 256, vbox);
185         for(i=0;i<256;i++){
186                 init_srt_table_row(&ss->smb_srt_table, i, val_to_str(i, smb_cmd_vals, "Unknown(0x%02x)"));
187         }
188
189
190         label=gtk_label_new("Transaction2 Sub-Commands");
191         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
192         gtk_widget_show(label);
193         init_srt_table(&ss->trans2_srt_table, 256, vbox);
194         for(i=0;i<256;i++){
195                 init_srt_table_row(&ss->trans2_srt_table, i, val_to_str(i, trans2_cmd_vals, "Unknown(0x%02x)"));
196         }
197
198
199         label=gtk_label_new("NT Transaction Sub-Commands");
200         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
201         gtk_widget_show(label);
202         init_srt_table(&ss->nt_trans_srt_table, 256, vbox);
203         for(i=0;i<256;i++){
204                 init_srt_table_row(&ss->nt_trans_srt_table, i, val_to_str(i, nt_cmd_vals, "Unknown(0x%02x)"));
205         }
206
207
208         error_string=register_tap_listener("smb", ss, filter, smbstat_reset, smbstat_packet, smbstat_draw);
209         if(error_string){
210                 simple_dialog(ESD_TYPE_WARN, NULL, error_string->str);
211                 g_string_free(error_string, TRUE);
212                 g_free(ss);
213                 return;
214         }
215
216         gtk_widget_show_all(ss->win);
217         redissect_packets(&cfile);
218 }
219
220
221
222 static GtkWidget *dlg=NULL, *dlg_box;
223 static GtkWidget *filter_box;
224 static GtkWidget *filter_label, *filter_entry;
225 static GtkWidget *start_button;
226
227 static void
228 dlg_destroy_cb(void)
229 {
230         dlg=NULL;
231 }
232
233 static void
234 smbstat_start_button_clicked(GtkWidget *item _U_, gpointer data _U_)
235 {
236         char *filter;
237         char str[256];
238
239         filter=(char *)gtk_entry_get_text(GTK_ENTRY(filter_entry));
240         if(filter[0]==0){
241                 gtk_smbstat_init("smb,srt");
242         } else {
243                 sprintf(str,"smb,srt,%s", filter);
244                 gtk_smbstat_init(str);
245         }
246 }
247
248 static void
249 gtk_smbstat_cb(GtkWidget *w _U_, gpointer d _U_)
250 {
251         char *filter;
252
253         /* if the window is already open, bring it to front */
254         if(dlg){
255                 gdk_window_raise(dlg->window);
256                 return;
257         }
258
259         dlg=gtk_window_new(GTK_WINDOW_TOPLEVEL);
260         gtk_window_set_title(GTK_WINDOW(dlg), "SMB Service Response Time statistics");
261         SIGNAL_CONNECT(dlg, "destroy", dlg_destroy_cb, NULL);
262         dlg_box=gtk_vbox_new(FALSE, 0);
263         gtk_container_add(GTK_CONTAINER(dlg), dlg_box);
264         gtk_widget_show(dlg_box);
265
266
267         /* filter box */
268         filter_box=gtk_hbox_new(FALSE, 10);
269         /* Filter label */
270         gtk_container_set_border_width(GTK_CONTAINER(filter_box), 10);
271         filter_label=gtk_label_new("Filter:");
272         gtk_box_pack_start(GTK_BOX(filter_box), filter_label, FALSE, FALSE, 0);
273         gtk_widget_show(filter_label);
274
275         filter_entry=gtk_entry_new_with_max_length(250);
276         gtk_box_pack_start(GTK_BOX(filter_box), filter_entry, FALSE, FALSE, 0);
277         filter=gtk_entry_get_text(GTK_ENTRY(main_display_filter_widget));
278         if(filter){
279                 gtk_entry_set_text(GTK_ENTRY(filter_entry), filter);
280         }
281         gtk_widget_show(filter_entry);
282
283         gtk_box_pack_start(GTK_BOX(dlg_box), filter_box, TRUE, TRUE, 0);
284         gtk_widget_show(filter_box);
285
286
287         /* the start button */
288         start_button=gtk_button_new_with_label("Create Stat");
289         SIGNAL_CONNECT_OBJECT(start_button, "clicked",
290                               smbstat_start_button_clicked, NULL);
291         gtk_box_pack_start(GTK_BOX(dlg_box), start_button, TRUE, TRUE, 0);
292         gtk_widget_show(start_button);
293
294         gtk_widget_show_all(dlg);
295 }
296
297 void
298 register_tap_listener_gtksmbstat(void)
299 {
300         register_ethereal_tap("smb,srt", gtk_smbstat_init);
301 }
302
303 void
304 register_tap_menu_gtksmbstat(void)
305 {
306         register_tap_menu_item("Service Response Time/SMB", gtk_smbstat_cb);
307 }