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