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