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