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