Rewrite of dissector: get rid of static structures and use helper functions available...
[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     { 480, "Temporarily Unavailable"},
109     { 481, "Call/Transaction Does Not Exist"},
110     { 482, "Loop Detected"},
111     { 483, "Too Many Hops"},
112     { 484, "Address Incomplete"},
113     { 485, "Ambiguous"},
114     { 486, "Busy Here"},
115     { 487, "Request Terminated"},
116     { 488, "Not Acceptable Here"},
117     { 489, "Bad Event"},
118     { 491, "Request Pending"},
119     { 493, "Undecipherable"},
120     { 499, "Client Error - Others"},
121
122     { 500, "Server Internal Error"},
123     { 501, "Not Implemented"},
124     { 502, "Bad Gateway"},
125     { 503, "Service Unavailable"},
126     { 504, "Server Time-out"},
127     { 505, "Version Not Supported"},
128     { 513, "Message Too Large"},
129     { 599, "Server Error - Others"},
130
131     { 600, "Busy Everywhere"},
132     { 603, "Decline"},
133     { 604, "Does Not Exist Anywhere"},
134     { 606, "Not Acceptable"},
135     { 699, "Global Failure - Others"},
136
137     { 0,        NULL}
138 };
139
140 /* Create tables for responses and requests */
141 static void
142 sip_init_hash(sipstat_t *sp)
143 {
144     int i;
145
146     /* Create responses table */
147     sp->hash_responses = g_hash_table_new(g_int_hash, g_int_equal);
148
149     /* Add all response codes */
150     for (i=0 ; vals_status_code[i].strptr ; i++)
151     {
152         gint *key = g_malloc (sizeof(gint));
153         sip_response_code_t *sc = g_malloc (sizeof(sip_response_code_t));
154         *key = vals_status_code[i].value;
155         sc->packets=0;
156         sc->response_code =  *key;
157         sc->name=vals_status_code[i].strptr;
158         sc->sp = sp;
159         g_hash_table_insert(sc->sp->hash_responses, key, sc);
160     }
161
162     /* Create empty requests table */
163     sp->hash_requests = g_hash_table_new(g_str_hash, g_str_equal);
164 }
165
166 static void
167 sip_draw_hash_requests( gchar *key _U_ , sip_request_method_t *data, gchar * format)
168 {
169         if (data->packets==0)
170                 return;
171         printf( format, data->response, data->packets);
172 }
173
174 static void
175 sip_draw_hash_responses( gint * key _U_ , sip_response_code_t *data, char * format)
176 {
177         if (data==NULL) {
178                 g_warning("C'est quoi ce borderl key=%d\n", *key);
179                 exit(EXIT_FAILURE);
180         }
181         if (data->packets==0)
182                 return;
183         printf(format,  data->response_code, data->name, data->packets );
184 }
185
186 /* NOT USED at this moment */
187 /*
188 static void
189 sip_free_hash( gpointer key, gpointer value, gpointer user_data _U_ )
190 {
191         g_free(key);
192         g_free(value);
193 }
194 */
195
196 static void
197 sip_reset_hash_responses(gchar *key _U_ , sip_response_code_t *data, gpointer ptr _U_ )
198 {
199         data->packets = 0;
200 }
201 static void
202 sip_reset_hash_requests(gchar *key _U_ , sip_request_method_t *data, gpointer ptr _U_ )
203 {
204         data->packets = 0;
205 }
206
207 static void
208 sipstat_reset(void *psp  )
209 {
210         sipstat_t *sp=psp;
211         if (sp) {
212                 sp->packets = 0;
213                 sp->resent_packets = 0;
214                 g_hash_table_foreach( sp->hash_responses, (GHFunc)sip_reset_hash_responses, NULL);
215                 g_hash_table_foreach( sp->hash_requests, (GHFunc)sip_reset_hash_requests, NULL);
216         }
217 }
218
219
220 /* Main entry point to SIP tap */
221 static int
222 sipstat_packet(void *psp, packet_info *pinfo _U_, epan_dissect_t *edt _U_, const void *pri)
223 {
224     const sip_info_value_t *value=pri;
225     sipstat_t *sp = (sipstat_t *)psp;
226     
227     /* Total number of packets, including continuation packets */
228     sp->packets++;
229     
230     /* Update resent count if flag set */
231     if (value->resend)
232     {
233         sp->resent_packets++;
234     }
235
236     
237     /* Looking at both requests and responses */
238     if (value->response_code != 0)
239     {
240         /* Responses */
241         guint *key = g_malloc(sizeof(guint));
242         sip_response_code_t *sc;
243
244         /* Look up response code in hash table */
245         *key = value->response_code;
246         sc = g_hash_table_lookup(sp->hash_responses, key);
247         if (sc==NULL)
248         {
249             /* Non-standard status code ; we classify it as others
250              * in the relevant category
251              * (Informational,Success,Redirection,Client Error,Server Error,Global Failure)
252              */
253             int i = value->response_code;
254             if ((i<100) || (i>=700))
255             {
256                 /* Forget about crazy values */
257                 return 0;
258             }
259             else if (i<200)
260             {
261                 *key=199;       /* Hopefully, this status code will never be used */
262             }
263             else if (i<300)
264             {
265                 *key=299;
266             }
267             else if (i<400)
268             {
269                 *key=399;
270             }
271             else if (i<500)
272             {
273                 *key=499;
274             }
275             else if (i<600)
276             {
277                 *key=599;
278             }
279             else
280             {
281                 *key = 699;
282             }
283
284             /* Now look up this fallback code to get its text description */
285             sc = g_hash_table_lookup(sp->hash_responses, key);
286             if (sc==NULL)
287             {
288                 return 0;
289             }
290         }
291         sc->packets++;
292     }
293     else if (value->request_method)
294     {
295         /* Requests */
296         sip_request_method_t *sc;
297
298         /* Look up the request method in the table */
299         sc = g_hash_table_lookup(sp->hash_requests, value->request_method);
300         if (sc == NULL)
301         {
302             /* First of this type. Create structure and initialise */
303             sc=g_malloc(sizeof(sip_request_method_t));
304             sc->response = g_strdup(value->request_method);
305             sc->packets = 1;
306             sc->sp = sp;
307             /* Insert it into request table */
308             g_hash_table_insert(sp->hash_requests, sc->response, sc);
309         }
310         else
311         {
312             /* Already existed, just update count for that method */
313             sc->packets++;
314         }
315         /* g_free(value->request_method); */
316     }
317     else
318     {
319         /* No request method set. Just ignore */
320         return 0;
321     }
322
323     return 1;
324 }
325
326 static void
327 sipstat_draw(void *psp  )
328 {
329         sipstat_t *sp=psp;
330         printf("\n");
331         printf("===================================================================\n");
332         if (sp->filter == NULL)
333                 printf("SIP Statistics\n");
334         else
335                 printf("SIP Statistics with filter %s\n", sp->filter);
336
337         printf("\nNumber of SIP messages: %d", sp->packets);
338         printf("\nNumber of resent SIP messages: %d\n", sp->resent_packets);
339         printf( "\n* SIP Status Codes in reply packets\n");
340         g_hash_table_foreach( sp->hash_responses, (GHFunc)sip_draw_hash_responses,
341                 "  SIP %3d %-15s : %5d Packets\n");
342         printf("\n* List of SIP Request methods\n");
343         g_hash_table_foreach( sp->hash_requests,  (GHFunc)sip_draw_hash_requests,
344                 "  %-15s : %5d Packets\n");
345         printf("===================================================================\n");
346 }
347
348 static void
349 sipstat_init(const char *optarg, void* userdata _U_)
350 {
351         sipstat_t *sp;
352         const char *filter=NULL;
353         GString *error_string;
354
355         if (strncmp (optarg, "sip,stat,", 9) == 0){
356                 filter=optarg+9;
357         } else {
358                 filter=NULL;
359         }
360
361         sp = g_malloc( sizeof(sipstat_t) );
362         if(filter){
363                 sp->filter=g_strdup(filter);
364         } else {
365                 sp->filter=NULL;
366         }
367         /*g_hash_table_foreach( sip_status, (GHFunc)sip_reset_hash_responses, NULL);*/
368
369
370         error_string = register_tap_listener(
371                         "sip",
372                         sp,
373                         filter,
374                         sipstat_reset,
375                         sipstat_packet,
376                         sipstat_draw);
377         if (error_string){
378                 /* error, we failed to attach to the tap. clean up */
379                 g_free(sp->filter);
380                 g_free(sp);
381                 fprintf (stderr, "tshark: Couldn't register sip,stat tap: %s\n",
382                                 error_string->str);
383                 g_string_free(error_string, TRUE);
384                 exit(1);
385         }
386
387         sp->packets = 0;
388         sp->resent_packets = 0;
389         sip_init_hash(sp);
390 }
391
392 void
393 register_tap_listener_sipstat(void)
394 {
395         register_stat_cmd_arg("sip,stat", sipstat_init,NULL);
396 }