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