Rename the tap_dfilter_dlg.[ch] files to reflect that they can do more
[obnox/wireshark/wip.git] / gtk / sctp_chunk_stat.c
1 /* sctp_chunk_stat.c
2  * SCTP chunk counter for Wireshark
3  * Copyright 2005 Oleg Terletsky <oleg.terletsky [AT] comverse.com>
4  * Copyright 2009 Varun Notibala <nbvarun [AT] gmail.com>
5  *
6  * $Id$
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
10  * Copyright 1998 Gerald Combs
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  */
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 #include <string.h>
36
37 #include <gtk/gtk.h>
38
39 #include <epan/packet_info.h>
40 #include <epan/epan.h>
41 #include <epan/value_string.h>
42 #include <epan/tap.h>
43 #include <epan/dissectors/packet-sctp.h>
44
45 #include "../simple_dialog.h"
46 #include "../file.h"
47 #include "../globals.h"
48 #include "../stat_menu.h"
49
50 #include "gtk/gui_stat_util.h"
51 #include "gtk/dlg_utils.h"
52 #include "gtk/tap_param_dlg.h"
53 #include "gtk/gui_utils.h"
54 #include "gtk/main.h"
55 #include "gtk/sctp_stat.h"
56
57
58 static void sctpstat_init(const char *optarg, void *userdata);
59
60 static tap_param sctp_stat_params[] = {
61         { PARAM_FILTER, "Filter", NULL }
62 };
63
64 static tap_param_dlg sctp_stat_dlg = {
65         "SCTP Statistics",
66         "sctp,stat",
67         sctpstat_init,
68         -1,
69         G_N_ELEMENTS(sctp_stat_params),
70         sctp_stat_params
71 };
72
73 typedef struct sctp_ep {
74         struct sctp_ep* next;
75         address src;
76         address dst;
77         guint16 sport;
78         guint16 dport;
79         guint32 chunk_count[256];
80 } sctp_ep_t;
81
82 /* used to keep track of the statistics for an entire program interface */
83 typedef struct _sctp_stat_t {
84         GtkWidget  *win;
85         GtkWidget  *vbox;
86         char       *filter;
87         GtkWidget  *scrolled_window;
88         GtkTreeView *table;
89         guint32    number_of_packets;
90         sctp_ep_t* ep_list;
91 } sctpstat_t;
92
93 typedef struct _sctp_info sctp_into_t;
94
95 #define CHUNK_TYPE(x)(tvb_get_guint8((x), CHUNK_TYPE_OFFSET))
96
97 static void
98 sctpstat_reset(void *phs)
99 {
100         sctpstat_t* sctp_stat = (sctpstat_t *)phs;
101         sctp_ep_t* list = (sctp_ep_t*)sctp_stat->ep_list;
102         sctp_ep_t* tmp = NULL;
103         guint16 chunk_type;
104
105         if(!list)
106                 return;
107
108         for(tmp = list; tmp ; tmp=tmp->next)
109                 for(chunk_type = 0; chunk_type < 256; chunk_type++)
110                         tmp->chunk_count[chunk_type] = 0;
111
112         sctp_stat->number_of_packets = 0;
113 }
114
115 static sctp_ep_t* alloc_sctp_ep(struct _sctp_info *si)
116 {
117         sctp_ep_t* ep;
118         guint16 chunk_type;
119
120         if(!si)
121                 return NULL;
122
123         if (!(ep = g_malloc(sizeof(sctp_ep_t))))
124                 return NULL;
125
126         COPY_ADDRESS(&ep->src,&si->ip_src);
127         COPY_ADDRESS(&ep->dst,&si->ip_dst);
128         ep->sport = si->sport;
129         ep->dport = si->dport;
130         ep->next = NULL;
131         for(chunk_type = 0; chunk_type < 256; chunk_type++)
132                 ep->chunk_count[chunk_type] = 0;
133         return ep;
134 }
135
136 static int
137 sctpstat_packet(void *phs, packet_info *pinfo _U_, epan_dissect_t *edt _U_, const void *phi)
138 {
139
140         sctpstat_t *hs=(sctpstat_t *)phs;
141         sctp_ep_t *tmp = NULL, *te = NULL;
142         struct _sctp_info *si = (struct _sctp_info *) phi;
143         guint32 tvb_number;
144
145         if (!hs)
146                 return (0);
147
148         hs->number_of_packets++;
149         if(!hs->ep_list) {
150                 hs->ep_list = alloc_sctp_ep(si);
151                 te = hs->ep_list;
152         } else {
153                 for(tmp=hs->ep_list ; tmp ; tmp=tmp->next) {
154                         if((!CMP_ADDRESS(&tmp->src,&si->ip_src)) &&
155                            (!CMP_ADDRESS(&tmp->dst,&si->ip_dst)) &&
156                            (tmp->sport == si->sport) &&
157                            (tmp->dport == si->dport)) {
158                                 te = tmp;
159                                 break;
160                         }
161                 }
162                 if(!te) {
163                         if ((te = alloc_sctp_ep(si))) {
164                                 te->next = hs->ep_list;
165                                 hs->ep_list = te;
166                         }
167                 }
168         }
169
170         if(!te)
171                 return (0);
172
173
174         if (si->number_of_tvbs > 0) {
175                 for(tvb_number = 0; tvb_number < si->number_of_tvbs; tvb_number++) {
176                         if (IS_SCTP_CHUNK_TYPE(CHUNK_TYPE(si->tvb[tvb_number])))
177                                 (te->chunk_count[CHUNK_TYPE(si->tvb[tvb_number])])++;
178                         else
179                                 (te->chunk_count[OTHER_CHUNKS_INDEX])++;
180                 }
181         }
182         return (1);
183 }
184
185
186 static void
187 sctpstat_draw(void *phs)
188 {
189         sctpstat_t *hs=(sctpstat_t *)phs;
190         sctp_ep_t* list = hs->ep_list, *tmp;
191         GtkListStore *store;
192         GtkTreeIter iter;
193
194         /* Now print Message and Reason Counter Table */
195         /* clear list before printing */
196         /* XXX use an iter for new/modified ? */
197         store = GTK_LIST_STORE(gtk_tree_view_get_model(hs->table));
198         gtk_list_store_clear(store);
199
200         for(tmp = list ; tmp ; tmp=tmp->next) {
201                 gtk_list_store_append(store, &iter);
202                 gtk_list_store_set(store, &iter,
203                 0,  ep_address_to_str(&tmp->src),
204                 1,  tmp->sport,
205                 2,  ep_address_to_str(&tmp->dst),
206                 3,  tmp->dport,
207                 4,  tmp->chunk_count[SCTP_DATA_CHUNK_ID],
208                 5,  tmp->chunk_count[SCTP_SACK_CHUNK_ID],
209                 6,  tmp->chunk_count[SCTP_HEARTBEAT_CHUNK_ID],
210                 7,  tmp->chunk_count[SCTP_HEARTBEAT_ACK_CHUNK_ID],
211                 8,  tmp->chunk_count[SCTP_INIT_CHUNK_ID],
212                 9,  tmp->chunk_count[SCTP_INIT_ACK_CHUNK_ID],
213                 10, tmp->chunk_count[SCTP_COOKIE_ECHO_CHUNK_ID],
214                 11, tmp->chunk_count[SCTP_COOKIE_ACK_CHUNK_ID],
215                 12, tmp->chunk_count[SCTP_ABORT_CHUNK_ID],
216                 13, tmp->chunk_count[SCTP_ERROR_CHUNK_ID],
217                 14, tmp->chunk_count[SCTP_NR_SACK_CHUNK_ID],
218                 15, tmp->chunk_count[SCTP_ASCONF_ACK_CHUNK_ID],
219                 16, tmp->chunk_count[SCTP_PKTDROP_CHUNK_ID],
220                 17, tmp->chunk_count[SCTP_FORWARD_TSN_CHUNK_ID],
221                 18, tmp->chunk_count[SCTP_ASCONF_CHUNK_ID],
222                 19, tmp->chunk_count[OTHER_CHUNKS_INDEX],
223                 -1
224                 );
225         }
226 }
227
228 static void
229 win_destroy_cb(GtkWindow *win _U_, gpointer data)
230 {
231         sctpstat_t *hs=(sctpstat_t *)data;
232
233         protect_thread_critical_region();
234         remove_tap_listener(hs);
235         unprotect_thread_critical_region();
236
237         if(hs->filter){
238                 g_free(hs->filter);
239                 hs->filter=NULL;
240         }
241         g_free(hs);
242 }
243
244
245 static const stat_column titles[]={
246         {G_TYPE_STRING, LEFT, "Source IP" },
247         {G_TYPE_UINT, RIGHT,  "Source Port" },
248         {G_TYPE_STRING, LEFT, "Dest IP" },
249         {G_TYPE_UINT, RIGHT,  "Dest Port" },
250         {G_TYPE_UINT, RIGHT,  "DATA" },
251         {G_TYPE_UINT, RIGHT,  "SACK" },
252         {G_TYPE_UINT, RIGHT,  "HBEAT" },
253         {G_TYPE_UINT, RIGHT,  "HBEAT-ACK" },
254         {G_TYPE_UINT, RIGHT,  "INIT" },
255         {G_TYPE_UINT, RIGHT,  "INIT-ACK" },
256         {G_TYPE_UINT, RIGHT,  "COOKIE" },
257         {G_TYPE_UINT, RIGHT,  "COOKIE-ACK" },
258         {G_TYPE_UINT, RIGHT,  "ABORT" },
259         {G_TYPE_UINT, RIGHT,  "ERROR" },
260         {G_TYPE_UINT, RIGHT,  "NR-SACK" },
261         {G_TYPE_UINT, RIGHT,  "ASCONF-ACK" },
262         {G_TYPE_UINT, RIGHT,  "PKTDROP" },
263         {G_TYPE_UINT, RIGHT,  "FORWARD-TSN" },
264         {G_TYPE_UINT, RIGHT,  "ASCONF" },
265         {G_TYPE_UINT, RIGHT,  "Others" }
266 };
267
268 static void
269 sctpstat_init(const char *optarg, void *userdata _U_)
270 {
271         sctpstat_t *hs;
272         GString *error_string;
273         GtkWidget *bbox;
274         GtkWidget *close_bt;
275
276         hs=g_malloc(sizeof(sctpstat_t));
277         if(strncmp(optarg,"sctp,stat,",10) == 0){
278                 hs->filter=g_strdup(optarg+10);
279         } else {
280                 hs->filter=NULL;
281         }
282         hs->ep_list = NULL;
283         hs->number_of_packets = 0;
284         sctpstat_reset(hs);
285
286         hs->win = dlg_window_new("Wireshark: SCTP Chunk Statistics");  /* transient_for top_level */
287         gtk_window_set_destroy_with_parent (GTK_WINDOW(hs->win), TRUE);
288         gtk_window_set_default_size(GTK_WINDOW(hs->win), 700, 250);
289
290         hs->vbox=gtk_vbox_new(FALSE, 3);
291         gtk_container_set_border_width(GTK_CONTAINER(hs->vbox), 12);
292
293         init_main_stat_window(hs->win, hs->vbox, "SCTP Chunk Counter", hs->filter);
294
295         /* init a scrolled window*/
296         hs->scrolled_window = scrolled_window_new(NULL, NULL);
297
298         hs->table = create_stat_table(hs->scrolled_window, hs->vbox, 20, titles);
299
300         error_string=register_tap_listener("sctp", hs, hs->filter, 0,
301                                            sctpstat_reset,
302                                            sctpstat_packet,
303                                            sctpstat_draw);
304         if(error_string){
305                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", error_string->str);
306                 g_string_free(error_string, TRUE);
307                 g_free(hs->filter);
308                 g_free(hs);
309                 return;
310         }
311
312         /* Button row. */
313         bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
314         gtk_box_pack_end(GTK_BOX(hs->vbox), bbox, FALSE, FALSE, 0);
315
316         close_bt = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_CLOSE);
317         window_set_cancel_button(hs->win, close_bt, window_cancel_button_cb);
318
319         g_signal_connect(hs->win, "delete_event", G_CALLBACK(window_delete_event_cb), NULL);
320         g_signal_connect(hs->win, "destroy", G_CALLBACK(win_destroy_cb), hs);
321
322         gtk_widget_show_all(hs->win);
323         window_present(hs->win);
324
325         cf_retap_packets(&cfile);
326 }
327
328 void
329 register_tap_listener_sctpstat(void)
330 {
331         register_dfilter_stat(&sctp_stat_dlg, "S_CTP/Chunk Counter",
332             REGISTER_STAT_GROUP_TELEPHONY);
333 }