Change window_new() to dlg_window_new() and destroy the window with the top level...
[obnox/wireshark/wip.git] / gtk / ldap_stat.c
1 /* ldap_stat.c
2  * ldap_stat   2003 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
33 #include <string.h>
34
35 #include <gtk/gtk.h>
36
37 #include <epan/packet_info.h>
38 #include <epan/epan.h>
39 #include <epan/value_string.h>
40 #include <epan/tap.h>
41 #include <epan/dissectors/packet-ldap.h>
42
43 #include "../register.h"
44 #include "../timestats.h"
45 #include "../simple_dialog.h"
46 #include "../file.h"
47 #include "../globals.h"
48 #include "../stat_menu.h"
49
50 #include "gtk/gui_utils.h"
51 #include "gtk/dlg_utils.h"
52 #include "gtk/filter_dlg.h"
53 #include "gtk/service_response_time_table.h"
54 #include "gtk/tap_dfilter_dlg.h"
55 #include "gtk/gtkglobals.h"
56 #include "gtk/main.h"
57
58
59 /* used to keep track of the statistics for an entire program interface */
60 typedef struct _ldapstat_t {
61         GtkWidget *win;
62         srt_stat_table ldap_srt_table;
63 } ldapstat_t;
64
65 static void
66 ldapstat_set_title(ldapstat_t *ldap)
67 {
68         char            *title;
69
70         title = g_strdup_printf("LDAP Service Response Time statistics: %s",
71             cf_get_display_name(&cfile));
72         gtk_window_set_title(GTK_WINDOW(ldap->win), title);
73         g_free(title);
74 }
75
76 static void
77 ldapstat_reset(void *pldap)
78 {
79         ldapstat_t *ldap=(ldapstat_t *)pldap;
80
81         reset_srt_table_data(&ldap->ldap_srt_table);
82         ldapstat_set_title(ldap);
83 }
84
85 static int
86 ldapstat_packet(void *pldap, packet_info *pinfo, epan_dissect_t *edt _U_, const void *psi)
87 {
88         const ldap_call_response_t *ldap=psi;
89         ldapstat_t *fs=(ldapstat_t *)pldap;
90
91         /* we are only interested in reply packets */
92         if(ldap->is_request){
93                 return 0;
94         }
95         /* if we havnt seen the request, just ignore it */
96         if(!ldap->req_frame){
97                 return 0;
98         }
99
100         /* only use the commands we know how to handle */
101         switch(ldap->protocolOpTag){
102         case LDAP_REQ_BIND:
103         case LDAP_REQ_SEARCH:
104         case LDAP_REQ_MODIFY:
105         case LDAP_REQ_ADD:
106         case LDAP_REQ_DELETE:
107         case LDAP_REQ_MODRDN:
108         case LDAP_REQ_COMPARE:
109         case LDAP_REQ_EXTENDED:
110                 break;
111         default:
112                 return 0;
113         }
114
115         add_srt_table_data(&fs->ldap_srt_table, ldap->protocolOpTag, &ldap->req_time, pinfo);
116
117         return 1;
118 }
119
120
121
122 static void
123 ldapstat_draw(void *pldap)
124 {
125         ldapstat_t *ldap=(ldapstat_t *)pldap;
126
127         draw_srt_table_data(&ldap->ldap_srt_table);
128 }
129
130
131 static void
132 win_destroy_cb(GtkWindow *win _U_, gpointer data)
133 {
134         ldapstat_t *ldap=(ldapstat_t *)data;
135
136         protect_thread_critical_region();
137         remove_tap_listener(ldap);
138         unprotect_thread_critical_region();
139
140         free_srt_table_data(&ldap->ldap_srt_table);
141         g_free(ldap);
142 }
143
144
145 static void
146 gtk_ldapstat_init(const char *optarg, void *userdata _U_)
147 {
148         ldapstat_t *ldap;
149         const char *filter=NULL;
150         GtkWidget *label;
151         char *filter_string;
152         GString *error_string;
153         GtkWidget *vbox;
154         GtkWidget *bbox;
155         GtkWidget *close_bt;
156
157         if(!strncmp(optarg,"ldap,srt,",9)){
158                 filter=optarg+9;
159         } else {
160                 filter=NULL;
161         }
162
163         ldap=g_malloc(sizeof(ldapstat_t));
164
165         ldap->win = dlg_window_new("ldap-stat");  
166         gtk_window_set_destroy_with_parent (GTK_WINDOW(ldap->win), TRUE);
167         gtk_window_set_default_size(GTK_WINDOW(ldap->win), 550, 400);
168         ldapstat_set_title(ldap);
169
170         vbox=gtk_vbox_new(FALSE, 3);
171         gtk_container_add(GTK_CONTAINER(ldap->win), vbox);
172         gtk_container_set_border_width(GTK_CONTAINER(vbox), 12);
173
174         label=gtk_label_new("LDAP Service Response Time statistics");
175         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
176
177         filter_string = g_strdup_printf("Filter: %s", filter ? filter : "");
178         label=gtk_label_new(filter_string);
179         g_free(filter_string);
180         gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
181         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
182
183         label=gtk_label_new("LDAP Commands");
184         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
185
186         /* We must display TOP LEVEL Widget before calling init_srt_table() */
187         gtk_widget_show_all(ldap->win);
188
189         init_srt_table(&ldap->ldap_srt_table, 24, vbox, NULL);
190         init_srt_table_row(&ldap->ldap_srt_table, 0, "Bind");
191         init_srt_table_row(&ldap->ldap_srt_table, 1, "<unknown>");
192         init_srt_table_row(&ldap->ldap_srt_table, 2, "<unknown>");
193         init_srt_table_row(&ldap->ldap_srt_table, 3, "Search");
194         init_srt_table_row(&ldap->ldap_srt_table, 4, "<unknown>");
195         init_srt_table_row(&ldap->ldap_srt_table, 5, "<unknown>");
196         init_srt_table_row(&ldap->ldap_srt_table, 6, "Modify");
197         init_srt_table_row(&ldap->ldap_srt_table, 7, "<unknown>");
198         init_srt_table_row(&ldap->ldap_srt_table, 8, "Add");
199         init_srt_table_row(&ldap->ldap_srt_table, 9, "<unknown>");
200         init_srt_table_row(&ldap->ldap_srt_table, 10, "Delete");
201         init_srt_table_row(&ldap->ldap_srt_table, 11, "<unknown>");
202         init_srt_table_row(&ldap->ldap_srt_table, 12, "Modrdn");
203         init_srt_table_row(&ldap->ldap_srt_table, 13, "<unknown>");
204         init_srt_table_row(&ldap->ldap_srt_table, 14, "Compare");
205         init_srt_table_row(&ldap->ldap_srt_table, 15, "<unknown>");
206         init_srt_table_row(&ldap->ldap_srt_table, 16, "<unknown>");
207         init_srt_table_row(&ldap->ldap_srt_table, 17, "<unknown>");
208         init_srt_table_row(&ldap->ldap_srt_table, 18, "<unknown>");
209         init_srt_table_row(&ldap->ldap_srt_table, 19, "<unknown>");
210         init_srt_table_row(&ldap->ldap_srt_table, 20, "<unknown>");
211         init_srt_table_row(&ldap->ldap_srt_table, 21, "<unknown>");
212         init_srt_table_row(&ldap->ldap_srt_table, 22, "<unknown>");
213         init_srt_table_row(&ldap->ldap_srt_table, 23, "Extended");
214
215
216         error_string=register_tap_listener("ldap", ldap, filter, 0, ldapstat_reset, ldapstat_packet, ldapstat_draw);
217         if(error_string){
218                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", error_string->str);
219                 g_string_free(error_string, TRUE);
220                 g_free(ldap);
221                 return;
222         }
223
224         /* Button row. */
225         bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
226         gtk_box_pack_end(GTK_BOX(vbox), bbox, FALSE, FALSE, 0);
227
228         close_bt = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_CLOSE);
229         window_set_cancel_button(ldap->win, close_bt, window_cancel_button_cb);
230
231         g_signal_connect(ldap->win, "delete_event", G_CALLBACK(window_delete_event_cb), NULL);
232         g_signal_connect(ldap->win, "destroy", G_CALLBACK(win_destroy_cb), ldap);
233
234         gtk_widget_show_all(ldap->win);
235         window_present(ldap->win);
236
237         cf_retap_packets(&cfile);
238         gdk_window_raise(ldap->win->window);
239 }
240
241 static tap_dfilter_dlg ldap_stat_dlg = {
242         "LDAP Service Response Time Statistics",
243         "ldap,srt",
244         gtk_ldapstat_init,
245         -1
246 };
247
248 void
249 register_tap_listener_gtkldapstat(void)
250 {
251         register_dfilter_stat(&ldap_stat_dlg, "LDAP",
252             REGISTER_STAT_GROUP_RESPONSE_TIME);
253 }