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