Add support for PARAM_ENUM tap parameters, and use the tap_param_dlg
[obnox/wireshark/wip.git] / gtk / smb2_stat.c
1 /* smb2_stat.c
2  * smb2_stat   2005 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-smb2.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 _smb2stat_t {
58         GtkWidget *win;
59         srt_stat_table smb2_srt_table;
60 } smb2stat_t;
61
62 static void
63 smb2stat_set_title(smb2stat_t *ss)
64 {
65         char *title;
66
67         title = g_strdup_printf("SMB2 Service Response Time statistics: %s",
68             cf_get_display_name(&cfile));
69         gtk_window_set_title(GTK_WINDOW(ss->win), title);
70         g_free(title);
71 }
72
73 static void
74 smb2stat_reset(void *pss)
75 {
76         smb2stat_t *ss=(smb2stat_t *)pss;
77
78         reset_srt_table_data(&ss->smb2_srt_table);
79         smb2stat_set_title(ss);
80 }
81
82 static int
83 smb2stat_packet(void *pss, packet_info *pinfo, epan_dissect_t *edt _U_, const void *psi)
84 {
85         smb2stat_t *ss=(smb2stat_t *)pss;
86         const smb2_info_t *si=psi;
87
88         /* we are only interested in response packets */
89         if(!(si->flags&SMB2_FLAGS_RESPONSE)){
90                 return 0;
91         }
92         /* if we havnt seen the request, just ignore it */
93         if(!si->saved){
94                 return 0;
95         }
96
97         add_srt_table_data(&ss->smb2_srt_table, si->opcode, &si->saved->req_time, pinfo);
98
99         return 1;
100 }
101
102
103
104 static void
105 smb2stat_draw(void *pss)
106 {
107         smb2stat_t *ss=(smb2stat_t *)pss;
108
109         draw_srt_table_data(&ss->smb2_srt_table);
110 }
111
112
113 static void
114 win_destroy_cb(GtkWindow *win _U_, gpointer data)
115 {
116         smb2stat_t *ss=(smb2stat_t *)data;
117
118         protect_thread_critical_region();
119         remove_tap_listener(ss);
120         unprotect_thread_critical_region();
121
122         free_srt_table_data(&ss->smb2_srt_table);
123         g_free(ss);
124 }
125
126
127 static void
128 gtk_smb2stat_init(const char *optarg, void *userdata _U_)
129 {
130         smb2stat_t *ss;
131         const char *filter=NULL;
132         GtkWidget *label;
133         char *filter_string;
134         GString *error_string;
135         int i;
136         GtkWidget *vbox;
137         GtkWidget *bbox;
138         GtkWidget *close_bt;
139
140         if(!strncmp(optarg,"smb2,srt,",9)){
141                 filter=optarg+9;
142         } else {
143                 filter=NULL;
144         }
145
146         ss=g_malloc(sizeof(smb2stat_t));
147
148         ss->win = dlg_window_new("smb2-stat");  /* transient_for top_level */
149         gtk_window_set_destroy_with_parent (GTK_WINDOW(ss->win), TRUE);
150         gtk_window_set_default_size(GTK_WINDOW(ss->win), 550, 400);
151         smb2stat_set_title(ss);
152
153         vbox=gtk_vbox_new(FALSE, 3);
154         gtk_container_add(GTK_CONTAINER(ss->win), vbox);
155         gtk_container_set_border_width(GTK_CONTAINER(vbox), 12);
156
157         label=gtk_label_new("SMB2 Service Response Time statistics");
158         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
159
160         filter_string = g_strdup_printf("Filter: %s", filter ? filter : "");
161         label=gtk_label_new(filter_string);
162         g_free(filter_string);
163         gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
164         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
165
166         label=gtk_label_new("SMB2 Commands");
167         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
168
169         /* We must display TOP LEVEL Widget before calling init_srt_table() */
170         gtk_widget_show_all(ss->win);
171
172         init_srt_table(&ss->smb2_srt_table, 256, vbox, "smb2.cmd");
173         for(i=0;i<256;i++){
174                 init_srt_table_row(&ss->smb2_srt_table, i, val_to_str_ext(i, &smb2_cmd_vals_ext, "Unknown(0x%02x)"));
175         }
176
177
178         error_string=register_tap_listener("smb2", ss, filter, 0, smb2stat_reset, smb2stat_packet, smb2stat_draw);
179         if(error_string){
180                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", error_string->str);
181                 g_string_free(error_string, TRUE);
182                 g_free(ss);
183                 return;
184         }
185
186         /* Button row. */
187         bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
188         gtk_box_pack_end(GTK_BOX(vbox), bbox, FALSE, FALSE, 0);
189
190         close_bt = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_CLOSE);
191         window_set_cancel_button(ss->win, close_bt, window_cancel_button_cb);
192
193         g_signal_connect(ss->win, "delete_event", G_CALLBACK(window_delete_event_cb), NULL);
194         g_signal_connect(ss->win, "destroy", G_CALLBACK(win_destroy_cb), ss);
195
196         gtk_widget_show_all(ss->win);
197         window_present(ss->win);
198
199         cf_retap_packets(&cfile);
200         gdk_window_raise(ss->win->window);
201 }
202
203 static tap_param smb2_stat_params[] = {
204         { PARAM_FILTER, "Filter", NULL }
205 };
206
207 static tap_param_dlg smb2_stat_dlg = {
208         "SMB2 SRT Statistics",
209         "smb2,srt",
210         gtk_smb2stat_init,
211         -1,
212         G_N_ELEMENTS(smb2_stat_params),
213         smb2_stat_params
214 };
215
216 void
217 register_tap_listener_gtksmb2stat(void)
218 {
219         register_dfilter_stat(&smb2_stat_dlg, "SMB2",
220             REGISTER_STAT_GROUP_RESPONSE_TIME);
221 }