e0d8808f4a41b8da9452146f815b2ce8b0918254
[obnox/wireshark/wip.git] / gtk / smb_stat.c
1 /* smb_stat.c
2  * smb_stat   2003 Ronnie Sahlberg
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 #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 #include <string.h>
33
34 #include <gtk/gtk.h>
35
36 #include <epan/packet_info.h>
37 #include <epan/epan.h>
38 #include <epan/value_string.h>
39 #include <epan/tap.h>
40 #include <epan/dissectors/packet-smb.h>
41
42 #include "../timestats.h"
43 #include "../simple_dialog.h"
44 #include "../file.h"
45 #include "../globals.h"
46 #include "../stat_menu.h"
47
48 #include "gtk/gui_utils.h"
49 #include "gtk/dlg_utils.h"
50 #include "gtk/filter_dlg.h"
51 #include "gtk/service_response_time_table.h"
52 #include "gtk/tap_param_dlg.h"
53 #include "gtk/gtkglobals.h"
54 #include "gtk/main.h"
55
56
57 /* used to keep track of the statistics for an entire program interface */
58 typedef struct _smbstat_t {
59         GtkWidget *win;
60         srt_stat_table smb_srt_table;
61         srt_stat_table trans2_srt_table;
62         srt_stat_table nt_trans_srt_table;
63 } smbstat_t;
64
65 static void
66 smbstat_set_title(smbstat_t *ss)
67 {
68         char *title;
69
70         title = g_strdup_printf("SMB Service Response Time statistics: %s",
71             cf_get_display_name(&cfile));
72         gtk_window_set_title(GTK_WINDOW(ss->win), title);
73         g_free(title);
74 }
75
76 static void
77 smbstat_reset(void *pss)
78 {
79         smbstat_t *ss=(smbstat_t *)pss;
80
81         reset_srt_table_data(&ss->smb_srt_table);
82         reset_srt_table_data(&ss->trans2_srt_table);
83         reset_srt_table_data(&ss->nt_trans_srt_table);
84         smbstat_set_title(ss);
85 }
86
87 static int
88 smbstat_packet(void *pss, packet_info *pinfo, epan_dissect_t *edt _U_, const void *psi)
89 {
90         smbstat_t *ss=(smbstat_t *)pss;
91         const smb_info_t *si=psi;
92
93         /* we are only interested in reply packets */
94         if(si->request){
95                 return 0;
96         }
97         /* if we havnt seen the request, just ignore it */
98         if(!si->sip){
99                 return 0;
100         }
101
102         add_srt_table_data(&ss->smb_srt_table, si->cmd, &si->sip->req_time, pinfo);
103
104         if(si->cmd==0xA0 && si->sip->extra_info_type == SMB_EI_NTI){
105                 smb_nt_transact_info_t *sti=(smb_nt_transact_info_t *)si->sip->extra_info;
106
107                 if(sti){
108                         add_srt_table_data(&ss->nt_trans_srt_table, sti->subcmd, &si->sip->req_time, pinfo);
109                 }
110         } else if(si->cmd==0x32 && si->sip->extra_info_type == SMB_EI_T2I){
111                 smb_transact2_info_t *st2i=(smb_transact2_info_t *)si->sip->extra_info;
112
113                 if(st2i){
114                         add_srt_table_data(&ss->trans2_srt_table, st2i->subcmd, &si->sip->req_time, pinfo);
115                 }
116         }
117
118         return 1;
119 }
120
121
122
123 static void
124 smbstat_draw(void *pss)
125 {
126         smbstat_t *ss=(smbstat_t *)pss;
127
128         draw_srt_table_data(&ss->smb_srt_table);
129         draw_srt_table_data(&ss->trans2_srt_table);
130         draw_srt_table_data(&ss->nt_trans_srt_table);
131 }
132
133
134 static void
135 win_destroy_cb(GtkWindow *win _U_, gpointer data)
136 {
137         smbstat_t *ss=(smbstat_t *)data;
138
139         protect_thread_critical_region();
140         remove_tap_listener(ss);
141         unprotect_thread_critical_region();
142
143         free_srt_table_data(&ss->smb_srt_table);
144         free_srt_table_data(&ss->trans2_srt_table);
145         free_srt_table_data(&ss->nt_trans_srt_table);
146         g_free(ss);
147 }
148
149
150 static void
151 gtk_smbstat_init(const char *optarg, void *userdata _U_)
152 {
153         smbstat_t *ss;
154         const char *filter=NULL;
155         GtkWidget *label;
156         char *filter_string;
157         GString *error_string;
158         int i;
159         GtkWidget *vbox;
160         GtkWidget *bbox;
161         GtkWidget *close_bt;
162
163         if(!strncmp(optarg,"smb,srt,",8)){
164                 filter=optarg+8;
165         } else {
166                 filter=NULL;
167         }
168
169         ss=g_malloc(sizeof(smbstat_t));
170
171         ss->win = dlg_window_new("smb-stat");  /* transient_for top_level */
172         gtk_window_set_destroy_with_parent (GTK_WINDOW(ss->win), TRUE);
173         gtk_window_set_default_size(GTK_WINDOW(ss->win), 550, 600);
174         smbstat_set_title(ss);
175
176         vbox=gtk_vbox_new(FALSE, 3);
177         gtk_container_add(GTK_CONTAINER(ss->win), vbox);
178         gtk_container_set_border_width(GTK_CONTAINER(vbox), 12);
179
180         label=gtk_label_new("SMB Service Response Time statistics");
181         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
182
183         filter_string = g_strdup_printf("Filter: %s", filter ? filter : "");
184         label=gtk_label_new(filter_string);
185         g_free(filter_string);
186         gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
187         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
188
189         label=gtk_label_new("SMB Commands");
190         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
191
192         /* We must display TOP LEVEL Widget before calling init_srt_table() */
193         gtk_widget_show_all(ss->win);
194
195         init_srt_table(&ss->smb_srt_table, 256, vbox, "smb.cmd");
196         for(i=0;i<256;i++){
197                 init_srt_table_row(&ss->smb_srt_table, i, val_to_str_ext(i, &smb_cmd_vals_ext, "Unknown(0x%02x)"));
198         }
199
200
201         label=gtk_label_new("Transaction2 Sub-Commands");
202         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
203         init_srt_table(&ss->trans2_srt_table, 256, vbox, "smb.trans2.cmd");
204         for(i=0;i<256;i++){
205                 init_srt_table_row(&ss->trans2_srt_table, i, val_to_str_ext(i, &trans2_cmd_vals_ext, "Unknown(0x%02x)"));
206         }
207
208
209         label=gtk_label_new("NT Transaction Sub-Commands");
210         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
211         init_srt_table(&ss->nt_trans_srt_table, 256, vbox, "smb.nt.function");
212         for(i=0;i<256;i++){
213                 init_srt_table_row(&ss->nt_trans_srt_table, i, val_to_str_ext(i, &nt_cmd_vals_ext, "Unknown(0x%02x)"));
214         }
215
216
217         error_string=register_tap_listener("smb", ss, filter, 0, smbstat_reset, smbstat_packet, smbstat_draw);
218         if(error_string){
219                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", error_string->str);
220                 g_string_free(error_string, TRUE);
221                 g_free(ss);
222                 return;
223         }
224
225         /* Button row. */
226         bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
227         gtk_box_pack_end(GTK_BOX(vbox), bbox, FALSE, FALSE, 0);
228
229         close_bt = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_CLOSE);
230         window_set_cancel_button(ss->win, close_bt, window_cancel_button_cb);
231
232         g_signal_connect(ss->win, "delete_event", G_CALLBACK(window_delete_event_cb), NULL);
233         g_signal_connect(ss->win, "destroy", G_CALLBACK(win_destroy_cb), ss);
234
235         gtk_widget_show_all(ss->win);
236         window_present(ss->win);
237
238         cf_retap_packets(&cfile);
239         gdk_window_raise(ss->win->window);
240 }
241
242 static tap_param smb_stat_params[] = {
243         { PARAM_FILTER, "Filter", NULL }
244 };
245
246 static tap_param_dlg smb_stat_dlg = {
247         "SMB SRT Statistics",
248         "smb,srt",
249         gtk_smbstat_init,
250         -1,
251         G_N_ELEMENTS(smb_stat_params),
252         smb_stat_params
253 };
254
255 void
256 register_tap_listener_gtksmbstat(void)
257 {
258         register_dfilter_stat(&smb_stat_dlg, "SMB",
259             REGISTER_STAT_GROUP_RESPONSE_TIME);
260 }