Warningfix:
[obnox/wireshark/wip.git] / tap-sipstat.c
1 /* tap_sipstat.c
2  * sip message counter for wireshark
3  *
4  * $Id$
5  * Copied from gtk/sip_stat.c and tap-httpstat.c
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/tap.h>
39 #include <epan/stat_cmd_args.h>
40 #include "epan/value_string.h"
41 #include "register.h"
42 #include <epan/dissectors/packet-sip.h>
43
44 /* used to keep track of the statictics for an entire program interface */
45 typedef struct _sip_stats_t {
46         char            *filter;
47         guint32         packets;        /* number of sip packets, including continuations */
48         guint32         resent_packets;
49         guint32         average_setup_time;
50         guint32         max_setup_time;
51         guint32         min_setup_time;
52         guint32         no_of_completed_calls;
53         guint64         total_setup_time;
54         GHashTable      *hash_responses;
55         GHashTable      *hash_requests;
56 } sipstat_t;
57
58 /* used to keep track of the stats for a specific response code
59  * for example it can be { 3, 404, "Not Found" ,...}
60  * which means we captured 3 reply sip/1.1 404 Not Found */
61 typedef struct _sip_response_code_t {
62         guint32          packets;               /* 3 */
63         guint            response_code;         /* 404 */
64         const gchar     *name;                  /* Not Found */
65         sipstat_t       *sp;
66 } sip_response_code_t;
67
68 /* used to keep track of the stats for a specific request string */
69 typedef struct _sip_request_method_t {
70         gchar           *response;      /* eg. : INVITE */
71         guint32          packets;
72         sipstat_t       *sp;
73 } sip_request_method_t;
74
75 /* TODO: extra codes to be added from SIP extensions? */
76 static const value_string vals_status_code[] = {
77     { 100, "Trying"},
78     { 180, "Ringing"},
79     { 181, "Call Is Being Forwarded"},
80     { 182, "Queued"},
81     { 183, "Session Progress"},
82     { 199, "Informational - Others" },
83
84     { 200, "OK"},
85     { 202, "Accepted"},
86     { 299, "Success - Others"}, /* used to keep track of other Success packets */
87
88     { 300, "Multiple Choices"},
89     { 301, "Moved Permanently"},
90     { 302, "Moved Temporarily"},
91     { 305, "Use Proxy"},
92     { 380, "Alternative Service"},
93     { 399, "Redirection - Others"},
94
95     { 400, "Bad Request"},
96     { 401, "Unauthorized"},
97     { 402, "Payment Required"},
98     { 403, "Forbidden"},
99     { 404, "Not Found"},
100     { 405, "Method Not Allowed"},
101     { 406, "Not Acceptable"},
102     { 407, "Proxy Authentication Required"},
103     { 408, "Request Timeout"},
104     { 410, "Gone"},
105     { 412, "Conditional Request Failed"},
106     { 413, "Request Entity Too Large"},
107     { 414, "Request-URI Too Long"},
108     { 415, "Unsupported Media Type"},
109     { 416, "Unsupported URI Scheme"},
110     { 420, "Bad Extension"},
111     { 421, "Extension Required"},
112     { 422, "Session Timer Too Small"},
113     { 423, "Interval Too Brief"},
114     { 429, "Provide Referrer Identity"},
115     { 480, "Temporarily Unavailable"},
116     { 481, "Call/Transaction Does Not Exist"},
117     { 482, "Loop Detected"},
118     { 483, "Too Many Hops"},
119     { 484, "Address Incomplete"},
120     { 485, "Ambiguous"},
121     { 486, "Busy Here"},
122     { 487, "Request Terminated"},
123     { 488, "Not Acceptable Here"},
124     { 489, "Bad Event"},
125     { 491, "Request Pending"},
126     { 493, "Undecipherable"},
127     { 494, "Security Agreement Required"},
128     { 499, "Client Error - Others"},
129
130     { 500, "Server Internal Error"},
131     { 501, "Not Implemented"},
132     { 502, "Bad Gateway"},
133     { 503, "Service Unavailable"},
134     { 504, "Server Time-out"},
135     { 505, "Version Not Supported"},
136     { 513, "Message Too Large"},
137     { 599, "Server Error - Others"},
138
139     { 600, "Busy Everywhere"},
140     { 603, "Decline"},
141     { 604, "Does Not Exist Anywhere"},
142     { 606, "Not Acceptable"},
143     { 699, "Global Failure - Others"},
144
145     { 0,        NULL}
146 };
147
148 /* Create tables for responses and requests */
149 static void
150 sip_init_hash(sipstat_t *sp)
151 {
152     int i;
153
154     /* Create responses table */
155     sp->hash_responses = g_hash_table_new(g_int_hash, g_int_equal);
156
157     /* Add all response codes */
158     for (i=0 ; vals_status_code[i].strptr ; i++)
159     {
160         gint *key = g_malloc (sizeof(gint));
161         sip_response_code_t *sc = g_malloc (sizeof(sip_response_code_t));
162         *key = vals_status_code[i].value;
163         sc->packets=0;
164         sc->response_code =  *key;
165         sc->name=vals_status_code[i].strptr;
166         sc->sp = sp;
167         g_hash_table_insert(sc->sp->hash_responses, key, sc);
168     }
169
170     /* Create empty requests table */
171     sp->hash_requests = g_hash_table_new(g_str_hash, g_str_equal);
172 }
173
174 static void
175 sip_draw_hash_requests( gchar *key _U_ , sip_request_method_t *data, gchar * format)
176 {
177         if (data->packets==0)
178                 return;
179         printf( format, data->response, data->packets);
180 }
181
182 static void
183 sip_draw_hash_responses( gint * key _U_ , sip_response_code_t *data, char * format)
184 {
185         if (data==NULL) {
186                 g_warning("C'est quoi ce borderl key=%d\n", *key);
187                 exit(EXIT_FAILURE);
188         }
189         if (data->packets==0)
190                 return;
191         printf(format,  data->response_code, data->name, data->packets );
192 }
193
194 /* NOT USED at this moment */
195 /*
196 static void
197 sip_free_hash( gpointer key, gpointer value, gpointer user_data _U_ )
198 {
199         g_free(key);
200         g_free(value);
201 }
202 */
203
204 static void
205 sip_reset_hash_responses(gchar *key _U_ , sip_response_code_t *data, gpointer ptr _U_ )
206 {
207         data->packets = 0;
208 }
209 static void
210 sip_reset_hash_requests(gchar *key _U_ , sip_request_method_t *data, gpointer ptr _U_ )
211 {
212         data->packets = 0;
213 }
214
215 static void
216 sipstat_reset(void *psp  )
217 {
218         sipstat_t *sp=psp;
219         if (sp) {
220                 sp->packets = 0;
221                 sp->resent_packets = 0;
222                 sp->average_setup_time = 0;
223                 sp->max_setup_time = 0;
224                 sp->min_setup_time = 0;
225                 sp->no_of_completed_calls = 0;
226                 sp->total_setup_time = 0;
227
228                 g_hash_table_foreach( sp->hash_responses, (GHFunc)sip_reset_hash_responses, NULL);
229                 g_hash_table_foreach( sp->hash_requests, (GHFunc)sip_reset_hash_requests, NULL);
230         }
231 }
232
233
234 /* Main entry point to SIP tap */
235 static int
236 sipstat_packet(void *psp, packet_info *pinfo _U_, epan_dissect_t *edt _U_, const void *pri)
237 {
238     const sip_info_value_t *value=pri;
239     sipstat_t *sp = (sipstat_t *)psp;
240     
241     /* Total number of packets, including continuation packets */
242     sp->packets++;
243
244         /* Calculate average setup time */
245         if (value->setup_time){
246                 sp->no_of_completed_calls++;
247                 /* Check if it's the first value */
248                 if ( sp->total_setup_time == 0 ){
249                         sp->average_setup_time = value->setup_time;
250                         sp->total_setup_time = value->setup_time;
251                         sp->max_setup_time = value->setup_time;
252                         sp->min_setup_time = value->setup_time;
253                 }else{
254                         sp->total_setup_time = sp->total_setup_time + value->setup_time;
255                         if (sp->max_setup_time < value->setup_time){
256                                 sp->max_setup_time = value->setup_time;
257                         }
258                         if (sp->min_setup_time > value->setup_time){
259                                 sp->min_setup_time = value->setup_time;
260                         }
261                         /* Calculate average */
262                         sp->average_setup_time = (guint32)(sp->total_setup_time / sp->no_of_completed_calls); 
263                 }
264         }
265     
266     /* Update resent count if flag set */
267     if (value->resend)
268     {
269         sp->resent_packets++;
270     }
271
272     
273     /* Looking at both requests and responses */
274     if (value->response_code != 0)
275     {
276         /* Responses */
277         guint *key = g_malloc(sizeof(guint));
278         sip_response_code_t *sc;
279
280         /* Look up response code in hash table */
281         *key = value->response_code;
282         sc = g_hash_table_lookup(sp->hash_responses, key);
283         if (sc==NULL)
284         {
285             /* Non-standard status code ; we classify it as others
286              * in the relevant category
287              * (Informational,Success,Redirection,Client Error,Server Error,Global Failure)
288              */
289             int i = value->response_code;
290             if ((i<100) || (i>=700))
291             {
292                 /* Forget about crazy values */
293                 return 0;
294             }
295             else if (i<200)
296             {
297                 *key=199;       /* Hopefully, this status code will never be used */
298             }
299             else if (i<300)
300             {
301                 *key=299;
302             }
303             else if (i<400)
304             {
305                 *key=399;
306             }
307             else if (i<500)
308             {
309                 *key=499;
310             }
311             else if (i<600)
312             {
313                 *key=599;
314             }
315             else
316             {
317                 *key = 699;
318             }
319
320             /* Now look up this fallback code to get its text description */
321             sc = g_hash_table_lookup(sp->hash_responses, key);
322             if (sc==NULL)
323             {
324                 return 0;
325             }
326         }
327         sc->packets++;
328     }
329     else if (value->request_method)
330     {
331         /* Requests */
332         sip_request_method_t *sc;
333
334         /* Look up the request method in the table */
335         sc = g_hash_table_lookup(sp->hash_requests, value->request_method);
336         if (sc == NULL)
337         {
338             /* First of this type. Create structure and initialise */
339             sc=g_malloc(sizeof(sip_request_method_t));
340             sc->response = g_strdup(value->request_method);
341             sc->packets = 1;
342             sc->sp = sp;
343             /* Insert it into request table */
344             g_hash_table_insert(sp->hash_requests, sc->response, sc);
345         }
346         else
347         {
348             /* Already existed, just update count for that method */
349             sc->packets++;
350         }
351         /* g_free(value->request_method); */
352     }
353     else
354     {
355         /* No request method set. Just ignore */
356         return 0;
357     }
358
359     return 1;
360 }
361
362 static void
363 sipstat_draw(void *psp  )
364 {
365         sipstat_t *sp=psp;
366         printf("\n");
367         printf("===================================================================\n");
368         if (sp->filter == NULL)
369                 printf("SIP Statistics\n");
370         else
371                 printf("SIP Statistics with filter %s\n", sp->filter);
372
373         printf("\nNumber of SIP messages: %d", sp->packets);
374         printf("\nNumber of resent SIP messages: %d\n", sp->resent_packets);
375         printf( "\n* SIP Status Codes in reply packets\n");
376         g_hash_table_foreach( sp->hash_responses, (GHFunc)sip_draw_hash_responses,
377                 "  SIP %3d %-15s : %5d Packets\n");
378         printf("\n* List of SIP Request methods\n");
379         g_hash_table_foreach( sp->hash_requests,  (GHFunc)sip_draw_hash_requests,
380                 "  %-15s : %5d Packets\n");
381         printf( "\n* Average setup time %d ms\n Min %d ms\n Max %d ms\n", sp->average_setup_time, sp->min_setup_time, sp->max_setup_time);
382         printf("===================================================================\n");
383 }
384
385 static void
386 sipstat_init(const char *optarg, void* userdata _U_)
387 {
388         sipstat_t *sp;
389         const char *filter=NULL;
390         GString *error_string;
391
392         if (strncmp (optarg, "sip,stat,", 9) == 0){
393                 filter=optarg+9;
394         } else {
395                 filter=NULL;
396         }
397
398         sp = g_malloc( sizeof(sipstat_t) );
399         if(filter){
400                 sp->filter=g_strdup(filter);
401         } else {
402                 sp->filter=NULL;
403         }
404         /*g_hash_table_foreach( sip_status, (GHFunc)sip_reset_hash_responses, NULL);*/
405
406
407         error_string = register_tap_listener(
408                         "sip",
409                         sp,
410                         filter,
411                         sipstat_reset,
412                         sipstat_packet,
413                         sipstat_draw);
414         if (error_string){
415                 /* error, we failed to attach to the tap. clean up */
416                 g_free(sp->filter);
417                 g_free(sp);
418                 fprintf (stderr, "tshark: Couldn't register sip,stat tap: %s\n",
419                                 error_string->str);
420                 g_string_free(error_string, TRUE);
421                 exit(1);
422         }
423
424         sp->packets = 0;
425         sp->resent_packets = 0;
426         sip_init_hash(sp);
427 }
428
429 void
430 register_tap_listener_sipstat(void)
431 {
432         register_stat_cmd_arg("sip,stat", sipstat_init,NULL);
433 }