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