Add a "register_dfilter_stat()", to register stats that take a display
[obnox/wireshark/wip.git] / gtk / sctp_chunk_stat.c
1 /* sctp_chunk_stat.c
2  * SCTP chunk counter for ethereal
3  * Copyright 2005 Oleg Terletsky oleg.terletsky@comverse.com
4  *
5  * $Id$
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@ethereal.com>
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 "gtk_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 "tap_dfilter_dlg.h"
55 #include "gui_utils.h"
56
57
58 static void sctpstat_init(const char *optarg);
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            0x16
105 #define SCTP_ASCONF_ACK_CHUNK_ID      0x80
106 #define SCTP_PKTDROP_CHUNK_ID         0x81
107 #define SCTP_FORWARD_TSN_CHUNK_ID     0xC0
108 #define SCTP_ASCONF_CHUNK_ID          0xC1
109 #define SCTP_IETF_EXT                 0xFF
110
111 #define CHUNK_TYPE_OFFSET 0
112 #define CHUNK_TYPE(x)(tvb_get_guint8((x), CHUNK_TYPE_OFFSET))
113
114 static void
115 sctpstat_reset(void *phs)
116 {
117         sctpstat_t* sctp_stat = (sctpstat_t *)phs;
118         sctp_ep_t* list = (sctp_ep_t*)sctp_stat->ep_list;
119         sctp_ep_t* tmp = NULL;
120         guint16 chunk_type;
121         
122         if(!list)
123                 return;
124
125         for(tmp = list; tmp ; tmp=tmp->next)
126                 for(chunk_type = 0; chunk_type < 256; chunk_type++)
127                         tmp->chunk_count[chunk_type] = 0;
128
129         sctp_stat->number_of_packets = 0;
130 }
131
132 static sctp_ep_t* alloc_sctp_ep(struct _sctp_info *si)
133 {
134         sctp_ep_t* ep;
135         guint16 chunk_type;
136
137         if(!si)
138                 return NULL;
139
140         if (!(ep = g_malloc(sizeof(sctp_ep_t))))
141                 return NULL;
142         
143         COPY_ADDRESS(&ep->src,&si->ip_src);
144         COPY_ADDRESS(&ep->dst,&si->ip_dst);
145         ep->sport = si->sport;
146         ep->dport = si->dport;
147         ep->next = NULL;
148         for(chunk_type = 0; chunk_type < 256; chunk_type++)
149                 ep->chunk_count[chunk_type] = 0;
150         return ep;
151 }
152
153 static int
154 sctpstat_packet(void *phs, packet_info *pinfo _U_, epan_dissect_t *edt _U_, const void *phi)
155 {
156
157         sctpstat_t *hs=(sctpstat_t *)phs;
158         sctp_ep_t *tmp = NULL, *te = NULL;
159         struct _sctp_info *si = (struct _sctp_info *) phi;
160         guint32 tvb_number;
161         guint8 chunk_type;
162         
163         if (!hs)
164                 return (0);
165                 
166         hs->number_of_packets++;
167         if(!hs->ep_list) {
168                 hs->ep_list = alloc_sctp_ep(si);
169                 te = hs->ep_list;
170         } else {
171                 for(tmp=hs->ep_list ; tmp ; tmp=tmp->next) {
172                         if((!CMP_ADDRESS(&tmp->src,&si->ip_src)) &&
173                            (!CMP_ADDRESS(&tmp->dst,&si->ip_dst)) &&
174                            (tmp->sport == si->sport) &&
175                            (tmp->dport == si->dport)) {
176                                 te = tmp;
177                                 break;
178                         }
179                 }
180                 if(!te) {
181                         if ((te = alloc_sctp_ep(si))) {
182                                 te->next = hs->ep_list;
183                                 hs->ep_list = te;
184                         }
185                 }
186         }
187
188         if(!te)
189                 return (0);
190
191         
192         if (si->number_of_tvbs > 0) {
193                 chunk_type = CHUNK_TYPE(si->tvb[0]);
194                 if ((chunk_type == SCTP_INIT_CHUNK_ID) ||
195                     (chunk_type == SCTP_INIT_ACK_CHUNK_ID)) {
196                         (te->chunk_count[chunk_type])++;
197                 } else {
198                         for(tvb_number = 0; tvb_number < si->number_of_tvbs; tvb_number++) {
199                                 (te->chunk_count[CHUNK_TYPE(si->tvb[tvb_number])])++;
200                         }
201                 }
202         }
203         return (1);
204 }
205
206
207 static void
208 sctpstat_draw(void *phs)
209 {
210         sctpstat_t *hs=(sctpstat_t *)phs;
211         sctp_ep_t* list = hs->ep_list, *tmp=0;
212         char *str[14];
213         int i=0;
214
215         for(i=0;i<14;i++) {
216                 str[i]=g_malloc(sizeof(char[256]));
217         }
218         /* Now print Message and Reason Counter Table */
219         /* clear list before printing */
220         gtk_clist_clear(hs->table);
221
222
223         for(tmp = list ; tmp ; tmp=tmp->next) {
224                 
225                 g_snprintf(str[0],  sizeof(char[256]),"%s", address_to_str(&tmp->src));
226                 g_snprintf(str[1],  sizeof(char[256]),"%u", tmp->sport);
227                 g_snprintf(str[2],  sizeof(char[256]),"%s", address_to_str(&tmp->dst));
228                 g_snprintf(str[3],  sizeof(char[256]),"%u", tmp->dport);
229                 g_snprintf(str[4],  sizeof(char[256]),"%u", tmp->chunk_count[SCTP_DATA_CHUNK_ID]);
230                 g_snprintf(str[5],  sizeof(char[256]),"%u", tmp->chunk_count[SCTP_SACK_CHUNK_ID]);
231                 g_snprintf(str[6],  sizeof(char[256]),"%u", tmp->chunk_count[SCTP_HEARTBEAT_CHUNK_ID]);
232                 g_snprintf(str[7],  sizeof(char[256]),"%u", tmp->chunk_count[SCTP_HEARTBEAT_ACK_CHUNK_ID]);
233                 g_snprintf(str[8],  sizeof(char[256]),"%u", tmp->chunk_count[SCTP_INIT_CHUNK_ID]);
234                 g_snprintf(str[9],  sizeof(char[256]),"%u", tmp->chunk_count[SCTP_INIT_ACK_CHUNK_ID]);
235                 g_snprintf(str[10], sizeof(char[256]),"%u", tmp->chunk_count[SCTP_COOKIE_ECHO_CHUNK_ID]);
236                 g_snprintf(str[11], sizeof(char[256]),"%u", tmp->chunk_count[SCTP_COOKIE_ACK_CHUNK_ID]);
237                 g_snprintf(str[12], sizeof(char[256]),"%u", tmp->chunk_count[SCTP_ABORT_CHUNK_ID]);
238                 g_snprintf(str[13], sizeof(char[256]),"%u", tmp->chunk_count[SCTP_ERROR_CHUNK_ID]);
239
240                 gtk_clist_append(hs->table, str);
241         }
242
243         gtk_widget_show(GTK_WIDGET(hs->table));
244
245 }
246
247 void protect_thread_critical_region(void);
248 void unprotect_thread_critical_region(void);
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
282 static void
283 sctpstat_init(const char *optarg)
284 {
285         sctpstat_t *hs;
286         const char *filter=NULL;
287         GString *error_string;
288         GtkWidget *bbox;
289         GtkWidget *close_bt;
290
291         if(strncmp(optarg,"sctp,stat,",11) == 0){
292                 filter=optarg+11;
293         } else {
294                 filter="";
295         }
296
297         hs=g_malloc(sizeof(sctpstat_t));
298         hs->filter=g_strdup(filter);
299         hs->ep_list = NULL;
300         hs->number_of_packets = 0;
301         sctpstat_reset(hs);
302
303         hs->win=window_new(GTK_WINDOW_TOPLEVEL, "Ethereal: SCTP Chunk Statistics");
304         gtk_window_set_default_size(GTK_WINDOW(hs->win), 600, 200);
305
306         hs->vbox=gtk_vbox_new(FALSE, 3);
307         gtk_container_set_border_width(GTK_CONTAINER(hs->vbox), 12);
308
309         init_main_stat_window(hs->win, hs->vbox, "SCTP Chunk Counter", filter);
310
311         /* init a scrolled window*/
312         hs->scrolled_window = scrolled_window_new(NULL, NULL);
313
314         hs->table = create_stat_table(hs->scrolled_window, hs->vbox, 14, titles);
315
316         error_string=register_tap_listener("sctp", hs, filter, 
317                                            sctpstat_reset, 
318                                            sctpstat_packet, 
319                                            sctpstat_draw);
320         if(error_string){
321                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, error_string->str);
322                 g_string_free(error_string, TRUE);
323                 g_free(hs->filter);
324                 g_free(hs);
325                 return;
326         }
327
328         /* Button row. */
329         bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
330         gtk_box_pack_end(GTK_BOX(hs->vbox), bbox, FALSE, FALSE, 0);
331
332         close_bt = OBJECT_GET_DATA(bbox, GTK_STOCK_CLOSE);
333         window_set_cancel_button(hs->win, close_bt, window_cancel_button_cb);
334
335         SIGNAL_CONNECT(hs->win, "delete_event", window_delete_event_cb, NULL);
336         SIGNAL_CONNECT(hs->win, "destroy", win_destroy_cb, hs);
337
338         gtk_widget_show_all(hs->win);
339         window_present(hs->win);
340
341         cf_retap_packets(&cfile);
342 }
343
344 void
345 register_tap_listener_sctpstat(void)
346 {
347         register_dfilter_stat(&sctp_stat_dlg, "SCTP/Chunk Counter",
348             REGISTER_STAT_GROUP_TELEPHONY);
349 }