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