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