Move gtk to ui/gtk.
[metze/wireshark/wip.git] / ui / gtk / gtp_stat.c
1 /* gtp_stat.c
2  * gtp_stat   2008 Kari Tiirikainen
3  * Largely based on ldap_stat by Ronnie Sahlberg, all mistakes added by KTi
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #ifdef HAVE_SYS_TYPES_H
31 # include <sys/types.h>
32 #endif
33
34 #include <string.h>
35
36 #include <gtk/gtk.h>
37
38 #include <epan/packet_info.h>
39 #include <epan/epan.h>
40 #include <epan/value_string.h>
41 #include <epan/tap.h>
42 #include <epan/dissectors/packet-gtp.h>
43
44 #include "../timestats.h"
45 #include "../simple_dialog.h"
46 #include "../file.h"
47 #include "../stat_menu.h"
48
49 #include "ui/gtk/gui_utils.h"
50 #include "ui/gtk/dlg_utils.h"
51 #include "ui/gtk/service_response_time_table.h"
52 #include "ui/gtk/tap_param_dlg.h"
53 #include "ui/gtk/gtkglobals.h"
54 #include "ui/gtk/main.h"
55
56 #include "ui/gtk/old-gtk-compat.h"
57
58 /* used to keep track of the statistics for an entire program interface */
59 typedef struct _gtpstat_t {
60         GtkWidget *win;
61         srt_stat_table gtp_srt_table;
62 } gtpstat_t;
63
64 static void
65 gtpstat_set_title(gtpstat_t *gtp)
66 {
67         char            *title;
68
69         title = g_strdup_printf("GTP Control Plane  Response Time statistics: %s",
70             cf_get_display_name(&cfile));
71         gtk_window_set_title(GTK_WINDOW(gtp->win), title);
72         g_free(title);
73 }
74
75 static void
76 gtpstat_reset(void *pgtp)
77 {
78         gtpstat_t *gtp=(gtpstat_t *)pgtp;
79
80         reset_srt_table_data(&gtp->gtp_srt_table);
81         gtpstat_set_title(gtp);
82 }
83
84 static int
85 gtpstat_packet(void *pgtp, packet_info *pinfo, epan_dissect_t *edt _U_, const void *psi)
86 {
87         const gtp_msg_hash_t *gtp=psi;
88         gtpstat_t *fs=(gtpstat_t *)pgtp;
89         int idx=0;
90
91         /* we are only interested in reply packets */
92         if(gtp->is_request){
93                 return 0;
94         }
95         /* if we have not seen the request, just ignore it */
96         if(!gtp->req_frame){
97                 return 0;
98         }
99
100         /* Only use the commands we know how to handle, this is not a comprehensive list */
101         /* Redoing the message indexing is bit reduntant,                    */
102         /*  but using message type as such would yield a long gtp_srt_table. */
103         /*  Only a fraction of the messages are matchable req/resp pairs,    */
104         /*  it just doesn't feel feasible.                                   */
105
106         switch(gtp->msgtype){
107         case GTP_MSG_ECHO_REQ: idx=0;
108                 break;
109         case GTP_MSG_CREATE_PDP_REQ: idx=1;
110                 break;
111         case GTP_MSG_UPDATE_PDP_REQ: idx=2;
112                 break;
113         case GTP_MSG_DELETE_PDP_REQ: idx=3;
114                 break;
115         default:
116                 return 0;
117         }
118
119         add_srt_table_data(&fs->gtp_srt_table, idx, &gtp->req_time, pinfo);
120
121         return 1;
122 }
123
124
125
126 static void
127 gtpstat_draw(void *pgtp)
128 {
129         gtpstat_t *gtp=(gtpstat_t *)pgtp;
130
131         draw_srt_table_data(&gtp->gtp_srt_table);
132 }
133
134
135 static void
136 win_destroy_cb(GtkWindow *win _U_, gpointer data)
137 {
138         gtpstat_t *gtp=(gtpstat_t *)data;
139
140         protect_thread_critical_region();
141         remove_tap_listener(gtp);
142         unprotect_thread_critical_region();
143
144         free_srt_table_data(&gtp->gtp_srt_table);
145         g_free(gtp);
146 }
147
148
149 static void
150 gtk_gtpstat_init(const char *optarg, void *userdata _U_)
151 {
152         gtpstat_t *gtp;
153         const char *filter=NULL;
154         GtkWidget *label;
155         char *filter_string;
156         GString *error_string;
157         GtkWidget *vbox;
158         GtkWidget *bbox;
159         GtkWidget *close_bt;
160
161         if(!strncmp(optarg,"gtp,",4)){
162                 filter=optarg+4;
163         } else {
164                 filter="gtp"; /*NULL doesn't work here like in LDAP. Too little time/lazy to find out why ?*/
165         }
166
167         gtp=g_malloc(sizeof(gtpstat_t));
168
169         gtp->win = dlg_window_new("gtp-stat");  /* transient_for top_level */
170         gtk_window_set_destroy_with_parent (GTK_WINDOW(gtp->win), TRUE);
171
172         gtk_window_set_default_size(GTK_WINDOW(gtp->win), 550, 400);
173         gtpstat_set_title(gtp);
174
175         vbox=gtk_vbox_new(FALSE, 3);
176         gtk_container_add(GTK_CONTAINER(gtp->win), vbox);
177         gtk_container_set_border_width(GTK_CONTAINER(vbox), 12);
178
179         label=gtk_label_new("GTP 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("GTP Requests");
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(gtp->win);
193
194         init_srt_table(&gtp->gtp_srt_table, 4, vbox, NULL);
195         init_srt_table_row(&gtp->gtp_srt_table, 0, "Echo");
196         init_srt_table_row(&gtp->gtp_srt_table, 1, "Create PDP context");
197         init_srt_table_row(&gtp->gtp_srt_table, 2, "Update PDP context");
198         init_srt_table_row(&gtp->gtp_srt_table, 3, "Delete PDP context");
199
200         error_string=register_tap_listener("gtp", gtp, filter, 0, gtpstat_reset, gtpstat_packet, gtpstat_draw);
201         if(error_string){
202                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", error_string->str);
203                 g_string_free(error_string, TRUE);
204                 g_free(gtp);
205                 return;
206         }
207
208         /* Button row. */
209         bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
210         gtk_box_pack_end(GTK_BOX(vbox), bbox, FALSE, FALSE, 0);
211
212         close_bt = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_CLOSE);
213         window_set_cancel_button(gtp->win, close_bt, window_cancel_button_cb);
214
215         g_signal_connect(gtp->win, "delete_event", G_CALLBACK(window_delete_event_cb), NULL);
216         g_signal_connect(gtp->win, "destroy", G_CALLBACK(win_destroy_cb), gtp);
217
218         gtk_widget_show_all(gtp->win);
219         window_present(gtp->win);
220
221         cf_retap_packets(&cfile);
222         gdk_window_raise(gtk_widget_get_window(gtp->win));
223 }
224
225 static tap_param gtp_stat_params[] = {
226         { PARAM_FILTER, "Filter", NULL }
227 };
228
229 static tap_param_dlg gtp_stat_dlg = {
230         "GTP Control Plane Response Time Statistics",
231         "gtp",
232         gtk_gtpstat_init,
233         -1,
234         G_N_ELEMENTS(gtp_stat_params),
235         gtp_stat_params
236 };
237
238 void
239 register_tap_listener_gtkgtpstat(void)
240 {
241         register_dfilter_stat(&gtp_stat_dlg, "GTP",
242             REGISTER_STAT_GROUP_RESPONSE_TIME);
243 }
244
245 void gtp_srt_cb(GtkAction *action, gpointer user_data _U_)
246 {
247         tap_param_dlg_cb(action, &gtp_stat_dlg);
248 }
249