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