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