fc89dcdae2a641fb926d71689e3e80b1959b5304
[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 "../register.h"
46 #include "../simple_dialog.h"
47 #include "../file.h"
48 #include "../globals.h"
49 #include "../stat_menu.h"
50
51 #include "gtk/gui_stat_util.h"
52 #include "gtk/dlg_utils.h"
53 #include "gtk/tap_dfilter_dlg.h"
54 #include "gtk/gui_utils.h"
55 #include "gtk/main.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         GtkCList   *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 SCTP_DATA_CHUNK_ID               0
90 #define SCTP_INIT_CHUNK_ID               1
91 #define SCTP_INIT_ACK_CHUNK_ID           2
92 #define SCTP_SACK_CHUNK_ID               3
93 #define SCTP_HEARTBEAT_CHUNK_ID          4
94 #define SCTP_HEARTBEAT_ACK_CHUNK_ID      5
95 #define SCTP_ABORT_CHUNK_ID              6
96 #define SCTP_SHUTDOWN_CHUNK_ID           7
97 #define SCTP_SHUTDOWN_ACK_CHUNK_ID       8
98 #define SCTP_ERROR_CHUNK_ID              9
99 #define SCTP_COOKIE_ECHO_CHUNK_ID       10
100 #define SCTP_COOKIE_ACK_CHUNK_ID        11
101 #define SCTP_ECNE_CHUNK_ID              12
102 #define SCTP_CWR_CHUNK_ID               13
103 #define SCTP_SHUTDOWN_COMPLETE_CHUNK_ID 14
104 #define SCTP_AUTH_CHUNK_ID              15
105 #define SCTP_NR_SACK_CHUNK_ID           16
106 #define SCTP_ASCONF_ACK_CHUNK_ID      0x80
107 #define SCTP_PKTDROP_CHUNK_ID         0x81
108 #define SCTP_FORWARD_TSN_CHUNK_ID     0xC0
109 #define SCTP_ASCONF_CHUNK_ID          0xC1
110 #define SCTP_IETF_EXT                 0xFF
111
112 #define CHUNK_TYPE_OFFSET 0
113 #define CHUNK_TYPE(x)(tvb_get_guint8((x), CHUNK_TYPE_OFFSET))
114
115 static void
116 sctpstat_reset(void *phs)
117 {
118         sctpstat_t* sctp_stat = (sctpstat_t *)phs;
119         sctp_ep_t* list = (sctp_ep_t*)sctp_stat->ep_list;
120         sctp_ep_t* tmp = NULL;
121         guint16 chunk_type;
122
123         if(!list)
124                 return;
125
126         for(tmp = list; tmp ; tmp=tmp->next)
127                 for(chunk_type = 0; chunk_type < 256; chunk_type++)
128                         tmp->chunk_count[chunk_type] = 0;
129
130         sctp_stat->number_of_packets = 0;
131 }
132
133 static sctp_ep_t* alloc_sctp_ep(struct _sctp_info *si)
134 {
135         sctp_ep_t* ep;
136         guint16 chunk_type;
137
138         if(!si)
139                 return NULL;
140
141         if (!(ep = g_malloc(sizeof(sctp_ep_t))))
142                 return NULL;
143
144         COPY_ADDRESS(&ep->src,&si->ip_src);
145         COPY_ADDRESS(&ep->dst,&si->ip_dst);
146         ep->sport = si->sport;
147         ep->dport = si->dport;
148         ep->next = NULL;
149         for(chunk_type = 0; chunk_type < 256; chunk_type++)
150                 ep->chunk_count[chunk_type] = 0;
151         return ep;
152 }
153
154 static int
155 sctpstat_packet(void *phs, packet_info *pinfo _U_, epan_dissect_t *edt _U_, const void *phi)
156 {
157
158         sctpstat_t *hs=(sctpstat_t *)phs;
159         sctp_ep_t *tmp = NULL, *te = NULL;
160         struct _sctp_info *si = (struct _sctp_info *) phi;
161         guint32 tvb_number;
162         guint8 chunk_type;
163
164         if (!hs)
165                 return (0);
166
167         hs->number_of_packets++;
168         if(!hs->ep_list) {
169                 hs->ep_list = alloc_sctp_ep(si);
170                 te = hs->ep_list;
171         } else {
172                 for(tmp=hs->ep_list ; tmp ; tmp=tmp->next) {
173                         if((!CMP_ADDRESS(&tmp->src,&si->ip_src)) &&
174                            (!CMP_ADDRESS(&tmp->dst,&si->ip_dst)) &&
175                            (tmp->sport == si->sport) &&
176                            (tmp->dport == si->dport)) {
177                                 te = tmp;
178                                 break;
179                         }
180                 }
181                 if(!te) {
182                         if ((te = alloc_sctp_ep(si))) {
183                                 te->next = hs->ep_list;
184                                 hs->ep_list = te;
185                         }
186                 }
187         }
188
189         if(!te)
190                 return (0);
191
192
193         if (si->number_of_tvbs > 0) {
194                 chunk_type = CHUNK_TYPE(si->tvb[0]);
195                 if ((chunk_type == SCTP_INIT_CHUNK_ID) ||
196                     (chunk_type == SCTP_INIT_ACK_CHUNK_ID)) {
197                         (te->chunk_count[chunk_type])++;
198                 } else {
199                         for(tvb_number = 0; tvb_number < si->number_of_tvbs; tvb_number++) {
200                                 (te->chunk_count[CHUNK_TYPE(si->tvb[tvb_number])])++;
201                         }
202                 }
203         }
204         return (1);
205 }
206
207
208 static void
209 sctpstat_draw(void *phs)
210 {
211         sctpstat_t *hs=(sctpstat_t *)phs;
212         sctp_ep_t* list = hs->ep_list, *tmp=0;
213         char *str[15];
214         int i=0;
215
216         for(i=0;i<15;i++) {
217                 str[i]=g_malloc(sizeof(char[256]));
218         }
219         /* Now print Message and Reason Counter Table */
220         /* clear list before printing */
221         gtk_clist_clear(hs->table);
222
223
224         for(tmp = list ; tmp ; tmp=tmp->next) {
225
226                 g_snprintf(str[0],  sizeof(char[256]),"%s", address_to_str(&tmp->src));
227                 g_snprintf(str[1],  sizeof(char[256]),"%u", tmp->sport);
228                 g_snprintf(str[2],  sizeof(char[256]),"%s", address_to_str(&tmp->dst));
229                 g_snprintf(str[3],  sizeof(char[256]),"%u", tmp->dport);
230                 g_snprintf(str[4],  sizeof(char[256]),"%u", tmp->chunk_count[SCTP_DATA_CHUNK_ID]);
231                 g_snprintf(str[5],  sizeof(char[256]),"%u", tmp->chunk_count[SCTP_SACK_CHUNK_ID]);
232                 g_snprintf(str[6],  sizeof(char[256]),"%u", tmp->chunk_count[SCTP_HEARTBEAT_CHUNK_ID]);
233                 g_snprintf(str[7],  sizeof(char[256]),"%u", tmp->chunk_count[SCTP_HEARTBEAT_ACK_CHUNK_ID]);
234                 g_snprintf(str[8],  sizeof(char[256]),"%u", tmp->chunk_count[SCTP_INIT_CHUNK_ID]);
235                 g_snprintf(str[9],  sizeof(char[256]),"%u", tmp->chunk_count[SCTP_INIT_ACK_CHUNK_ID]);
236                 g_snprintf(str[10], sizeof(char[256]),"%u", tmp->chunk_count[SCTP_COOKIE_ECHO_CHUNK_ID]);
237                 g_snprintf(str[11], sizeof(char[256]),"%u", tmp->chunk_count[SCTP_COOKIE_ACK_CHUNK_ID]);
238                 g_snprintf(str[12], sizeof(char[256]),"%u", tmp->chunk_count[SCTP_ABORT_CHUNK_ID]);
239                 g_snprintf(str[13], sizeof(char[256]),"%u", tmp->chunk_count[SCTP_ERROR_CHUNK_ID]);
240                 g_snprintf(str[14], sizeof(char[256]),"%u", tmp->chunk_count[SCTP_NR_SACK_CHUNK_ID]);
241
242                 gtk_clist_append(hs->table, str);
243         }
244
245         gtk_widget_show(GTK_WIDGET(hs->table));
246
247 }
248
249 static void
250 win_destroy_cb(GtkWindow *win _U_, gpointer data)
251 {
252         sctpstat_t *hs=(sctpstat_t *)data;
253
254         protect_thread_critical_region();
255         remove_tap_listener(hs);
256         unprotect_thread_critical_region();
257
258         if(hs->filter){
259                 g_free(hs->filter);
260                 hs->filter=NULL;
261         }
262         g_free(hs);
263 }
264
265
266 static const gchar *titles[]={
267                         "Source IP",
268                         "Source Port",
269                         "Dest IP",
270                         "Dest Port",
271                         "DATA",
272                         "SACK",
273                         "HBEAT",
274                         "HBEAT_ACK",
275                         "INIT",
276                         "INIT_ACK",
277                         "COOKIE",
278                         "COOKIE_ACK",
279                         "ABORT",
280                         "ERROR",
281                         "NR_SACK" };
282
283 static void
284 sctpstat_init(const char *optarg, void *userdata _U_)
285 {
286         sctpstat_t *hs;
287         const char *filter=NULL;
288         GString *error_string;
289         GtkWidget *bbox;
290         GtkWidget *close_bt;
291
292         if(strncmp(optarg,"sctp,stat,",10) == 0){
293                 filter=optarg+10;
294         } else {
295                 filter="";
296         }
297
298         hs=g_malloc(sizeof(sctpstat_t));
299         hs->filter=g_strdup(filter);
300         hs->ep_list = NULL;
301         hs->number_of_packets = 0;
302         sctpstat_reset(hs);
303
304         hs->win=window_new(GTK_WINDOW_TOPLEVEL, "Wireshark: SCTP Chunk Statistics");
305         gtk_window_set_default_size(GTK_WINDOW(hs->win), 600, 200);
306
307         hs->vbox=gtk_vbox_new(FALSE, 3);
308         gtk_container_set_border_width(GTK_CONTAINER(hs->vbox), 12);
309
310         init_main_stat_window(hs->win, hs->vbox, "SCTP Chunk Counter", filter);
311
312         /* init a scrolled window*/
313         hs->scrolled_window = scrolled_window_new(NULL, NULL);
314
315         hs->table = create_stat_table(hs->scrolled_window, hs->vbox, 15, titles);
316
317         error_string=register_tap_listener("sctp", hs, filter,
318                                            sctpstat_reset,
319                                            sctpstat_packet,
320                                            sctpstat_draw);
321         if(error_string){
322                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", error_string->str);
323                 g_string_free(error_string, TRUE);
324                 g_free(hs->filter);
325                 g_free(hs);
326                 return;
327         }
328
329         /* Button row. */
330         bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
331         gtk_box_pack_end(GTK_BOX(hs->vbox), bbox, FALSE, FALSE, 0);
332
333         close_bt = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_CLOSE);
334         window_set_cancel_button(hs->win, close_bt, window_cancel_button_cb);
335
336         g_signal_connect(hs->win, "delete_event", G_CALLBACK(window_delete_event_cb), NULL);
337         g_signal_connect(hs->win, "destroy", G_CALLBACK(win_destroy_cb), hs);
338
339         gtk_widget_show_all(hs->win);
340         window_present(hs->win);
341
342         cf_retap_packets(&cfile, FALSE);
343 }
344
345 void
346 register_tap_listener_sctpstat(void)
347 {
348         register_dfilter_stat(&sctp_stat_dlg, "SCTP/Chunk Counter",
349             REGISTER_STAT_GROUP_TELEPHONY);
350 }