Add support for PARAM_ENUM tap parameters, and use the tap_param_dlg
[obnox/wireshark/wip.git] / gtk / diameter_stat.c
1 /* diameter_stat.c
2  *  Diameter Service Response Time Statistics
3  * (c) 2008 Abhik Sarkar
4  *
5  * Based almost completely on gtp_stat by Kari Tiirikainen
6  *
7  * $Id$
8  *
9  * Wireshark - Network traffic analyzer
10  * By Gerald Combs <gerald@wireshark.org>
11  * Copyright 1998 Gerald Combs
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26  */
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #ifdef HAVE_SYS_TYPES_H
33 # include <sys/types.h>
34 #endif
35
36 #include <string.h>
37
38 #include <gtk/gtk.h>
39
40 #include <epan/packet_info.h>
41 #include <epan/epan.h>
42 #include <epan/tap.h>
43 #include <epan/dissectors/packet-diameter.h>
44
45 #include "../timestats.h"
46 #include "../simple_dialog.h"
47 #include "../file.h"
48 #include "../stat_menu.h"
49
50 #include "gtk/gui_utils.h"
51 #include "gtk/dlg_utils.h"
52 #include "gtk/service_response_time_table.h"
53 #include "gtk/tap_param_dlg.h"
54 #include "gtk/gtkglobals.h"
55 #include "gtk/main.h"
56
57 /* used to keep track of the statistics for an entire program interface */
58 typedef struct _diameterstat_t {
59         GtkWidget *win;
60         srt_stat_table diameter_srt_table;
61 } diameterstat_t;
62
63 static GHashTable* cmd_str_hash;
64
65 static void
66 diameterstat_set_title(diameterstat_t *diameter)
67 {
68         char            *title;
69
70         title = g_strdup_printf("Diameter Service Response Time statistics: %s",
71             cf_get_display_name(&cfile));
72         gtk_window_set_title(GTK_WINDOW(diameter->win), title);
73         g_free(title);
74 }
75
76 static void
77 diameterstat_reset(void *pdiameter)
78 {
79         diameterstat_t *diameter=(diameterstat_t *)pdiameter;
80
81         reset_srt_table_data(&diameter->diameter_srt_table);
82         diameterstat_set_title(diameter);
83 }
84
85
86 static int
87 diameterstat_packet(void *pdiameter, packet_info *pinfo, epan_dissect_t *edt _U_, const void *pdi)
88 {
89         const diameter_req_ans_pair_t *diameter=pdi;
90         diameterstat_t *fs=(diameterstat_t *)pdiameter;
91         int* idx = NULL;
92
93         /* Process only answers where corresponding request is found.
94          * Unpaired daimeter messages are currently not supported by statistics.
95          * Return 0, since redraw is not needed. */
96         if(!diameter || diameter->processing_request || !diameter->req_frame)
97                 return 0;
98
99         idx = (int*) g_hash_table_lookup(cmd_str_hash, diameter->cmd_str);
100         if (idx == NULL) {
101                 idx = g_malloc(sizeof(int));
102                 *idx = (int) g_hash_table_size(cmd_str_hash);
103                 g_hash_table_insert(cmd_str_hash, (gchar*) diameter->cmd_str, idx);
104                 init_srt_table_row(&fs->diameter_srt_table, *idx,  (const char*) diameter->cmd_str);
105         }
106
107         add_srt_table_data(&fs->diameter_srt_table, *idx, &diameter->req_time, pinfo);
108
109         return 1;
110 }
111
112
113
114 static void
115 diameterstat_draw(void *pdiameter)
116 {
117         diameterstat_t *diameter=(diameterstat_t *)pdiameter;
118
119         draw_srt_table_data(&diameter->diameter_srt_table);
120 }
121
122
123 static void
124 win_destroy_cb(GtkWindow *win _U_, gpointer data)
125 {
126         diameterstat_t *diameter=(diameterstat_t *)data;
127
128         protect_thread_critical_region();
129         remove_tap_listener(diameter);
130         unprotect_thread_critical_region();
131
132         free_srt_table_data(&diameter->diameter_srt_table);
133         g_free(diameter);
134         g_hash_table_destroy(cmd_str_hash);
135 }
136
137
138 static void
139 gtk_diameterstat_init(const char *optarg, void *userdata _U_)
140 {
141         diameterstat_t *diameter;
142         const char *filter=NULL;
143         GtkWidget *label;
144         char *filter_string;
145         GString *error_string;
146         GtkWidget *vbox;
147         GtkWidget *bbox;
148         GtkWidget *close_bt;
149         int* idx;
150
151         if(!strncmp(optarg,"diameter,",9)){
152                 filter=optarg+9;
153         } else {
154                 filter="diameter"; /*NULL doesn't work here like in LDAP. Too little time/lazy to find out why ?*/
155         }
156
157         diameter=g_malloc(sizeof(diameterstat_t));
158         idx = g_malloc(sizeof(int));
159         *idx = 0;
160         cmd_str_hash = g_hash_table_new(g_str_hash,g_str_equal);
161         g_hash_table_insert(cmd_str_hash, (gchar *)"Unknown", idx);
162
163         diameter->win = dlg_window_new("diameter-stat");  /* transient_for top_level */
164         gtk_window_set_destroy_with_parent (GTK_WINDOW(diameter->win), TRUE);
165         gtk_window_set_default_size(GTK_WINDOW(diameter->win), 550, 400);
166         diameterstat_set_title(diameter);
167
168         vbox=gtk_vbox_new(FALSE, 3);
169         gtk_container_add(GTK_CONTAINER(diameter->win), vbox);
170         gtk_container_set_border_width(GTK_CONTAINER(vbox), 12);
171
172         label=gtk_label_new("Diameter Service Response Time statistics");
173         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
174
175         filter_string = g_strdup_printf("Filter: %s", filter ? filter : "");
176         label=gtk_label_new(filter_string);
177         g_free(filter_string);
178         gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
179         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
180
181         label=gtk_label_new("Diameter Requests");
182         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
183
184         /* We must display TOP LEVEL Widget before calling init_srt_table() */
185         gtk_widget_show_all(diameter->win);
186
187         /** @todo the filter to use in stead of NULL is "diameter.cmd.code"
188          * to enable the filter popup in the service response time dalouge
189          * Note to make it work the command code must be stored rather than the
190          * index.
191          */
192         init_srt_table(&diameter->diameter_srt_table, 1, vbox, NULL);
193         init_srt_table_row(&diameter->diameter_srt_table, 0, "Unknown");
194
195         error_string=register_tap_listener("diameter", diameter, filter, 0, diameterstat_reset, diameterstat_packet, diameterstat_draw);
196         if(error_string){
197                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", error_string->str);
198                 g_string_free(error_string, TRUE);
199                 g_free(diameter);
200                 return;
201         }
202
203         /* Button row. */
204         bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
205         gtk_box_pack_end(GTK_BOX(vbox), bbox, FALSE, FALSE, 0);
206
207         close_bt = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_CLOSE);
208         window_set_cancel_button(diameter->win, close_bt, window_cancel_button_cb);
209
210         g_signal_connect(diameter->win, "delete_event", G_CALLBACK(window_delete_event_cb), NULL);
211         g_signal_connect(diameter->win, "destroy", G_CALLBACK(win_destroy_cb), diameter);
212
213         gtk_widget_show_all(diameter->win);
214         window_present(diameter->win);
215
216         cf_retap_packets(&cfile);
217         gdk_window_raise(diameter->win->window);
218 }
219
220 static tap_param diameter_stat_params[] = {
221         { PARAM_FILTER, "Filter", NULL }
222 };
223
224 static tap_param_dlg diameter_stat_dlg = {
225         "Diameter Service Response Time Statistics",
226         "diameter",
227         gtk_diameterstat_init,
228         -1,
229         G_N_ELEMENTS(diameter_stat_params),
230         diameter_stat_params
231 };
232
233 void
234 register_tap_listener_gtkdiameterstat(void)
235 {
236         register_dfilter_stat(&diameter_stat_dlg, "Diameter",
237             REGISTER_STAT_GROUP_RESPONSE_TIME);
238 }