Fix a "equality comparison with extraneous parentheses" warning found
[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_dfilter_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_dfilter_dlg sctp_stat_dlg = {
61         "SCTP Statistics",
62         "sctp,stat",
63         sctpstat_init,
64         -1
65 };
66
67 typedef struct sctp_ep {
68         struct sctp_ep* next;
69         address src;
70         address dst;
71         guint16 sport;
72         guint16 dport;
73         guint32 chunk_count[256];
74 } sctp_ep_t;
75
76 /* used to keep track of the statistics for an entire program interface */
77 typedef struct _sctp_stat_t {
78         GtkWidget  *win;
79         GtkWidget  *vbox;
80         char       *filter;
81         GtkWidget  *scrolled_window;
82         GtkTreeView *table;
83         guint32    number_of_packets;
84         sctp_ep_t* ep_list;
85 } sctpstat_t;
86
87 typedef struct _sctp_info sctp_into_t;
88
89 #define CHUNK_TYPE(x)(tvb_get_guint8((x), CHUNK_TYPE_OFFSET))
90
91 static void
92 sctpstat_reset(void *phs)
93 {
94         sctpstat_t* sctp_stat = (sctpstat_t *)phs;
95         sctp_ep_t* list = (sctp_ep_t*)sctp_stat->ep_list;
96         sctp_ep_t* tmp = NULL;
97         guint16 chunk_type;
98
99         if(!list)
100                 return;
101
102         for(tmp = list; tmp ; tmp=tmp->next)
103                 for(chunk_type = 0; chunk_type < 256; chunk_type++)
104                         tmp->chunk_count[chunk_type] = 0;
105
106         sctp_stat->number_of_packets = 0;
107 }
108
109 static sctp_ep_t* alloc_sctp_ep(struct _sctp_info *si)
110 {
111         sctp_ep_t* ep;
112         guint16 chunk_type;
113
114         if(!si)
115                 return NULL;
116
117         if (!(ep = g_malloc(sizeof(sctp_ep_t))))
118                 return NULL;
119
120         COPY_ADDRESS(&ep->src,&si->ip_src);
121         COPY_ADDRESS(&ep->dst,&si->ip_dst);
122         ep->sport = si->sport;
123         ep->dport = si->dport;
124         ep->next = NULL;
125         for(chunk_type = 0; chunk_type < 256; chunk_type++)
126                 ep->chunk_count[chunk_type] = 0;
127         return ep;
128 }
129
130 static int
131 sctpstat_packet(void *phs, packet_info *pinfo _U_, epan_dissect_t *edt _U_, const void *phi)
132 {
133
134         sctpstat_t *hs=(sctpstat_t *)phs;
135         sctp_ep_t *tmp = NULL, *te = NULL;
136         struct _sctp_info *si = (struct _sctp_info *) phi;
137         guint32 tvb_number;
138
139         if (!hs)
140                 return (0);
141
142         hs->number_of_packets++;
143         if(!hs->ep_list) {
144                 hs->ep_list = alloc_sctp_ep(si);
145                 te = hs->ep_list;
146         } else {
147                 for(tmp=hs->ep_list ; tmp ; tmp=tmp->next) {
148                         if((!CMP_ADDRESS(&tmp->src,&si->ip_src)) &&
149                            (!CMP_ADDRESS(&tmp->dst,&si->ip_dst)) &&
150                            (tmp->sport == si->sport) &&
151                            (tmp->dport == si->dport)) {
152                                 te = tmp;
153                                 break;
154                         }
155                 }
156                 if(!te) {
157                         if ((te = alloc_sctp_ep(si))) {
158                                 te->next = hs->ep_list;
159                                 hs->ep_list = te;
160                         }
161                 }
162         }
163
164         if(!te)
165                 return (0);
166
167
168         if (si->number_of_tvbs > 0) {
169                 for(tvb_number = 0; tvb_number < si->number_of_tvbs; tvb_number++) {
170                         if (IS_SCTP_CHUNK_TYPE(CHUNK_TYPE(si->tvb[tvb_number])))
171                                 (te->chunk_count[CHUNK_TYPE(si->tvb[tvb_number])])++;
172                         else
173                                 (te->chunk_count[OTHER_CHUNKS_INDEX])++;
174                 }
175         }
176         return (1);
177 }
178
179
180 static void
181 sctpstat_draw(void *phs)
182 {
183         sctpstat_t *hs=(sctpstat_t *)phs;
184         sctp_ep_t* list = hs->ep_list, *tmp;
185         GtkListStore *store;
186         GtkTreeIter iter;
187
188         /* Now print Message and Reason Counter Table */
189         /* clear list before printing */
190         /* XXX use an iter for new/modified ? */
191         store = GTK_LIST_STORE(gtk_tree_view_get_model(hs->table));
192         gtk_list_store_clear(store);
193
194         for(tmp = list ; tmp ; tmp=tmp->next) {
195                 gtk_list_store_append(store, &iter);
196                 gtk_list_store_set(store, &iter,
197                 0,  ep_address_to_str(&tmp->src),
198                 1,  tmp->sport,
199                 2,  ep_address_to_str(&tmp->dst),
200                 3,  tmp->dport,
201                 4,  tmp->chunk_count[SCTP_DATA_CHUNK_ID],
202                 5,  tmp->chunk_count[SCTP_SACK_CHUNK_ID],
203                 6,  tmp->chunk_count[SCTP_HEARTBEAT_CHUNK_ID],
204                 7,  tmp->chunk_count[SCTP_HEARTBEAT_ACK_CHUNK_ID],
205                 8,  tmp->chunk_count[SCTP_INIT_CHUNK_ID],
206                 9,  tmp->chunk_count[SCTP_INIT_ACK_CHUNK_ID],
207                 10, tmp->chunk_count[SCTP_COOKIE_ECHO_CHUNK_ID],
208                 11, tmp->chunk_count[SCTP_COOKIE_ACK_CHUNK_ID],
209                 12, tmp->chunk_count[SCTP_ABORT_CHUNK_ID],
210                 13, tmp->chunk_count[SCTP_ERROR_CHUNK_ID],
211                 14, tmp->chunk_count[SCTP_NR_SACK_CHUNK_ID],
212                 15, tmp->chunk_count[SCTP_ASCONF_ACK_CHUNK_ID],
213                 16, tmp->chunk_count[SCTP_PKTDROP_CHUNK_ID],
214                 17, tmp->chunk_count[SCTP_FORWARD_TSN_CHUNK_ID],
215                 18, tmp->chunk_count[SCTP_ASCONF_CHUNK_ID],
216                 19, tmp->chunk_count[OTHER_CHUNKS_INDEX],
217                 -1
218                 );
219         }
220 }
221
222 static void
223 win_destroy_cb(GtkWindow *win _U_, gpointer data)
224 {
225         sctpstat_t *hs=(sctpstat_t *)data;
226
227         protect_thread_critical_region();
228         remove_tap_listener(hs);
229         unprotect_thread_critical_region();
230
231         if(hs->filter){
232                 g_free(hs->filter);
233                 hs->filter=NULL;
234         }
235         g_free(hs);
236 }
237
238
239 static const stat_column titles[]={
240         {G_TYPE_STRING, LEFT, "Source IP" },
241         {G_TYPE_UINT, RIGHT,  "Source Port" },
242         {G_TYPE_STRING, LEFT, "Dest IP" },
243         {G_TYPE_UINT, RIGHT,  "Dest Port" },
244         {G_TYPE_UINT, RIGHT,  "DATA" },
245         {G_TYPE_UINT, RIGHT,  "SACK" },
246         {G_TYPE_UINT, RIGHT,  "HBEAT" },
247         {G_TYPE_UINT, RIGHT,  "HBEAT-ACK" },
248         {G_TYPE_UINT, RIGHT,  "INIT" },
249         {G_TYPE_UINT, RIGHT,  "INIT-ACK" },
250         {G_TYPE_UINT, RIGHT,  "COOKIE" },
251         {G_TYPE_UINT, RIGHT,  "COOKIE-ACK" },
252         {G_TYPE_UINT, RIGHT,  "ABORT" },
253         {G_TYPE_UINT, RIGHT,  "ERROR" },
254         {G_TYPE_UINT, RIGHT,  "NR-SACK" },
255         {G_TYPE_UINT, RIGHT,  "ASCONF-ACK" },
256         {G_TYPE_UINT, RIGHT,  "PKTDROP" },
257         {G_TYPE_UINT, RIGHT,  "FORWARD-TSN" },
258         {G_TYPE_UINT, RIGHT,  "ASCONF" },
259         {G_TYPE_UINT, RIGHT,  "Others" }
260 };
261
262 static void
263 sctpstat_init(const char *optarg, void *userdata _U_)
264 {
265         sctpstat_t *hs;
266         GString *error_string;
267         GtkWidget *bbox;
268         GtkWidget *close_bt;
269
270         hs=g_malloc(sizeof(sctpstat_t));
271         if(strncmp(optarg,"sctp,stat,",10) == 0){
272                 hs->filter=g_strdup(optarg+10);
273         } else {
274                 hs->filter=NULL;
275         }
276         hs->ep_list = NULL;
277         hs->number_of_packets = 0;
278         sctpstat_reset(hs);
279
280         hs->win = dlg_window_new("Wireshark: SCTP Chunk Statistics");  /* transient_for top_level */
281         gtk_window_set_destroy_with_parent (GTK_WINDOW(hs->win), TRUE);
282         gtk_window_set_default_size(GTK_WINDOW(hs->win), 700, 250);
283
284         hs->vbox=gtk_vbox_new(FALSE, 3);
285         gtk_container_set_border_width(GTK_CONTAINER(hs->vbox), 12);
286
287         init_main_stat_window(hs->win, hs->vbox, "SCTP Chunk Counter", hs->filter);
288
289         /* init a scrolled window*/
290         hs->scrolled_window = scrolled_window_new(NULL, NULL);
291
292         hs->table = create_stat_table(hs->scrolled_window, hs->vbox, 20, titles);
293
294         error_string=register_tap_listener("sctp", hs, hs->filter, 0,
295                                            sctpstat_reset,
296                                            sctpstat_packet,
297                                            sctpstat_draw);
298         if(error_string){
299                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", error_string->str);
300                 g_string_free(error_string, TRUE);
301                 g_free(hs->filter);
302                 g_free(hs);
303                 return;
304         }
305
306         /* Button row. */
307         bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
308         gtk_box_pack_end(GTK_BOX(hs->vbox), bbox, FALSE, FALSE, 0);
309
310         close_bt = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_CLOSE);
311         window_set_cancel_button(hs->win, close_bt, window_cancel_button_cb);
312
313         g_signal_connect(hs->win, "delete_event", G_CALLBACK(window_delete_event_cb), NULL);
314         g_signal_connect(hs->win, "destroy", G_CALLBACK(win_destroy_cb), hs);
315
316         gtk_widget_show_all(hs->win);
317         window_present(hs->win);
318
319         cf_retap_packets(&cfile);
320 }
321
322 void
323 register_tap_listener_sctpstat(void)
324 {
325         register_dfilter_stat(&sctp_stat_dlg, "S_CTP/Chunk Counter",
326             REGISTER_STAT_GROUP_TELEPHONY);
327 }