From Oleg Terletsky: Support for SCTP chunk counters. Modified to handle bundling...
[obnox/wireshark/wip.git] / tap-sctpchunkstat.c
1 /* tap_sctpchunkstat.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 /* With MSVC and a libethereal.dll this file needs to import some variables 
27    in a special way. Therefore _NEED_VAR_IMPORT_ is defined. */  
28 #define _NEED_VAR_IMPORT_
29
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #include <stdio.h>
35
36 #ifdef HAVE_SYS_TYPES_H
37 # include <sys/types.h>
38 #endif
39
40 #include <string.h>
41 #include "epan/packet_info.h"
42 #include "epan/addr_resolv.h"
43 #include <epan/tap.h>
44 #include "epan/value_string.h"
45 #include "register.h"
46 #include <epan/dissectors/packet-sctp.h>
47
48 typedef struct sctp_ep {
49         struct sctp_ep* next;
50         address src;
51         address dst;
52         guint16 sport;
53         guint16 dport;
54         guint32 chunk_count[256];
55 } sctp_ep_t;
56
57
58 /* used to keep track of the statistics for an entire program interface */
59 typedef struct _sctpstat_t {
60         char*      filter;
61         guint32    number_of_packets;
62         sctp_ep_t* ep_list;
63 } sctpstat_t;
64
65
66 #define SCTP_DATA_CHUNK_ID               0
67 #define SCTP_INIT_CHUNK_ID               1
68 #define SCTP_INIT_ACK_CHUNK_ID           2
69 #define SCTP_SACK_CHUNK_ID               3
70 #define SCTP_HEARTBEAT_CHUNK_ID          4
71 #define SCTP_HEARTBEAT_ACK_CHUNK_ID      5
72 #define SCTP_ABORT_CHUNK_ID              6
73 #define SCTP_SHUTDOWN_CHUNK_ID           7
74 #define SCTP_SHUTDOWN_ACK_CHUNK_ID       8
75 #define SCTP_ERROR_CHUNK_ID              9
76 #define SCTP_COOKIE_ECHO_CHUNK_ID       10
77 #define SCTP_COOKIE_ACK_CHUNK_ID        11
78 #define SCTP_ECNE_CHUNK_ID              12
79 #define SCTP_CWR_CHUNK_ID               13
80 #define SCTP_SHUTDOWN_COMPLETE_CHUNK_ID 14
81 #define SCTP_AUTH_CHUNK_ID            0x16
82 #define SCTP_ASCONF_ACK_CHUNK_ID      0x80
83 #define SCTP_PKTDROP_CHUNK_ID         0x81
84 #define SCTP_FORWARD_TSN_CHUNK_ID     0xC0
85 #define SCTP_ASCONF_CHUNK_ID          0xC1
86 #define SCTP_IETF_EXT                 0xFF
87
88 #define CHUNK_TYPE_OFFSET 0
89 #define CHUNK_TYPE(x)(tvb_get_guint8((x), CHUNK_TYPE_OFFSET))
90
91
92 extern gchar* address_to_str(const address *);
93
94
95 static void
96 sctpstat_reset(void *phs)
97 {
98         sctpstat_t* sctp_stat = (sctpstat_t *)phs;
99         sctp_ep_t* list = (sctp_ep_t*)sctp_stat->ep_list;
100         sctp_ep_t* tmp = NULL;
101         guint16 chunk_type;
102         
103         if(!list)
104                 return;
105
106         for(tmp = list; tmp ; tmp=tmp->next)
107                 for(chunk_type = 0; chunk_type < 256; chunk_type++)
108                         tmp->chunk_count[chunk_type] = 0;
109
110         sctp_stat->number_of_packets = 0;
111 }
112
113
114 sctp_ep_t* alloc_sctp_ep(struct _sctp_info *si)
115 {
116         sctp_ep_t* ep;
117         guint16 chunk_type;
118
119         if(!si)
120                 return NULL;
121
122         if (!(ep = g_malloc(sizeof(sctp_ep_t))))
123                 return NULL;
124         
125         COPY_ADDRESS(&ep->src,&si->ip_src);
126         COPY_ADDRESS(&ep->dst,&si->ip_dst);
127         ep->sport = si->sport;
128         ep->dport = si->dport;
129         ep->next = NULL;
130         for(chunk_type = 0; chunk_type < 256; chunk_type++)
131                 ep->chunk_count[chunk_type] = 0;
132         return ep;
133 }
134
135
136         
137
138 static int
139 sctpstat_packet(void *phs, packet_info *pinfo _U_, epan_dissect_t *edt _U_, const void *phi)
140 {
141
142         sctpstat_t *hs=(sctpstat_t *)phs;
143         sctp_ep_t *tmp = NULL, *te = NULL;
144         struct _sctp_info *si = (struct _sctp_info *) phi;
145         guint32 tvb_number;
146         guint8 chunk_type;
147         
148         if (!hs)
149                 return (0);
150                 
151         hs->number_of_packets++;
152         
153         if(!hs->ep_list) {
154                 hs->ep_list = alloc_sctp_ep(si);
155                 te = hs->ep_list;
156         } else {
157                 for(tmp=hs->ep_list ; tmp ; tmp=tmp->next)
158                 {
159                         if((!CMP_ADDRESS(&tmp->src,&si->ip_src)) &&
160                            (!CMP_ADDRESS(&tmp->dst,&si->ip_dst)) &&
161                            (tmp->sport == si->sport) &&
162                            (tmp->dport == si->dport))
163                         {
164                                 te = tmp;
165                                 break;
166                         }
167                 }
168                 if(!te) {
169                         if ((te = alloc_sctp_ep(si))) {
170                                 te->next = hs->ep_list;
171                                 hs->ep_list = te;
172                         }
173                 }
174         }
175
176         if(!te)
177                 return (0);
178
179         
180         if (si->number_of_tvbs > 0) {
181                 chunk_type = CHUNK_TYPE(si->tvb[0]);
182                 if ((chunk_type == SCTP_INIT_CHUNK_ID) ||
183                     (chunk_type == SCTP_INIT_ACK_CHUNK_ID)) {
184                         te->chunk_count[chunk_type]++;
185                 } else {
186                         for(tvb_number = 0; tvb_number < si->number_of_tvbs; tvb_number++)
187                                 te->chunk_count[CHUNK_TYPE(si->tvb[tvb_number])]++;
188                 }
189         }
190         return (1);
191 }
192
193
194 static void
195 sctpstat_draw(void *phs)
196 {
197         sctpstat_t *hs=(sctpstat_t *)phs;
198         sctp_ep_t* list = hs->ep_list, *tmp;
199
200         printf("-------------------------------------------- SCTP Statistics --------------------------------------------------------------------------\n");
201         printf("|  Total packets RX/TX %u\n", hs->number_of_packets);
202         printf("---------------------------------------------------------------------------------------------------------------------------------------\n");
203         printf("|   Source IP   |PortA|    Dest. IP   |PortB|  DATA  |  SACK  |  HBEAT |HBEATACK|  INIT  | INITACK| COOKIE |COOKIACK| ABORT  |  ERROR |\n");
204         printf("---------------------------------------------------------------------------------------------------------------------------------------\n");
205         
206         for(tmp = list ; tmp ; tmp=tmp->next) {
207                 printf("|%15s|%5u|%15s|%5u|%8u|%8u|%8u|%8u|%8u|%8u|%8u|%8u|%8u|%8u|\n",
208                        address_to_str(&tmp->src),tmp->sport,
209                        address_to_str(&tmp->dst),tmp->dport,
210                        tmp->chunk_count[SCTP_DATA_CHUNK_ID],
211                        tmp->chunk_count[SCTP_SACK_CHUNK_ID],
212                        tmp->chunk_count[SCTP_HEARTBEAT_CHUNK_ID],
213                        tmp->chunk_count[SCTP_HEARTBEAT_ACK_CHUNK_ID],
214                        tmp->chunk_count[SCTP_INIT_CHUNK_ID],
215                        tmp->chunk_count[SCTP_INIT_ACK_CHUNK_ID],
216                        tmp->chunk_count[SCTP_COOKIE_ECHO_CHUNK_ID],
217                        tmp->chunk_count[SCTP_COOKIE_ACK_CHUNK_ID],
218                        tmp->chunk_count[SCTP_ABORT_CHUNK_ID],
219                        tmp->chunk_count[SCTP_ERROR_CHUNK_ID]);
220         }
221         printf("---------------------------------------------------------------------------------------------------------------------------------------\n");
222 }
223
224
225 static void
226 sctpstat_init(char *optarg)
227 {
228         sctpstat_t *hs;
229         char *filter=NULL;
230         GString *error_string;
231
232         if(!strncmp(optarg,"sctp,stat,",11)){
233                 filter=optarg+11;
234         } else {
235                 filter=g_malloc(1);
236                 *filter='\0';
237         }
238
239         hs = g_malloc(sizeof(sctpstat_t));
240         hs->filter=g_malloc(strlen(filter)+1);
241         hs->ep_list = NULL;
242         hs->number_of_packets = 0;
243         strcpy(hs->filter, filter);
244
245         sctpstat_reset(hs);
246
247         error_string=register_tap_listener("sctp", hs, filter, NULL, sctpstat_packet, sctpstat_draw);
248         if(error_string){
249                 /* error, we failed to attach to the tap. clean up */
250                 g_free(hs->filter);
251                 g_free(hs);
252
253                 fprintf(stderr, "tethereal: Couldn't register sctp,stat tap: %s\n",
254                     error_string->str);
255                 g_string_free(error_string, TRUE);
256                 exit(1);
257         }
258 }
259
260
261 void
262 register_tap_listener_sctpstat(void)
263 {
264         register_ethereal_tap("sctp,stat", sctpstat_init);
265 }