2 * sip message counter for wireshark
5 * Copied from gtk/sip_stat.c and tap-httpstat.c
7 * Wireshark - Network traffic analyzer
8 * By Gerald Combs <gerald@wireshark.org>
9 * Copyright 1998 Gerald Combs
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.
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.
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.
32 #ifdef HAVE_SYS_TYPES_H
33 # include <sys/types.h>
37 #include "epan/packet_info.h"
39 #include <epan/stat_cmd_args.h>
40 #include "epan/value_string.h"
42 #include <epan/dissectors/packet-sip.h>
44 /* used to keep track of the statictics for an entire program interface */
45 typedef struct _sip_stats_t {
47 guint32 packets; /* number of sip packets, including continuations */
48 guint32 resent_packets;
49 GHashTable *hash_responses;
50 GHashTable *hash_requests;
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 */
61 } sip_response_code_t;
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 */
68 } sip_request_method_t;
70 /* TODO: extra codes to be added from SIP extensions? */
71 static const value_string vals_status_code[] = {
74 { 181, "Call Is Being Forwarded"},
76 { 183, "Session Progress"},
77 { 199, "Informational - Others" },
81 { 299, "Success - Others"}, /* used to keep track of other Success packets */
83 { 300, "Multiple Choices"},
84 { 301, "Moved Permanently"},
85 { 302, "Moved Temporarily"},
87 { 380, "Alternative Service"},
88 { 399, "Redirection - Others"},
90 { 400, "Bad Request"},
91 { 401, "Unauthorized"},
92 { 402, "Payment Required"},
95 { 405, "Method Not Allowed"},
96 { 406, "Not Acceptable"},
97 { 407, "Proxy Authentication Required"},
98 { 408, "Request Timeout"},
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 { 422, "Session Timer Too Small"},
108 { 423, "Interval Too Brief"},
109 { 429, "Provide Referrer Identity"},
110 { 480, "Temporarily Unavailable"},
111 { 481, "Call/Transaction Does Not Exist"},
112 { 482, "Loop Detected"},
113 { 483, "Too Many Hops"},
114 { 484, "Address Incomplete"},
117 { 487, "Request Terminated"},
118 { 488, "Not Acceptable Here"},
120 { 491, "Request Pending"},
121 { 493, "Undecipherable"},
122 { 494, "Security Agreement Required"},
123 { 499, "Client Error - Others"},
125 { 500, "Server Internal Error"},
126 { 501, "Not Implemented"},
127 { 502, "Bad Gateway"},
128 { 503, "Service Unavailable"},
129 { 504, "Server Time-out"},
130 { 505, "Version Not Supported"},
131 { 513, "Message Too Large"},
132 { 599, "Server Error - Others"},
134 { 600, "Busy Everywhere"},
136 { 604, "Does Not Exist Anywhere"},
137 { 606, "Not Acceptable"},
138 { 699, "Global Failure - Others"},
143 /* Create tables for responses and requests */
145 sip_init_hash(sipstat_t *sp)
149 /* Create responses table */
150 sp->hash_responses = g_hash_table_new(g_int_hash, g_int_equal);
152 /* Add all response codes */
153 for (i=0 ; vals_status_code[i].strptr ; i++)
155 gint *key = g_malloc (sizeof(gint));
156 sip_response_code_t *sc = g_malloc (sizeof(sip_response_code_t));
157 *key = vals_status_code[i].value;
159 sc->response_code = *key;
160 sc->name=vals_status_code[i].strptr;
162 g_hash_table_insert(sc->sp->hash_responses, key, sc);
165 /* Create empty requests table */
166 sp->hash_requests = g_hash_table_new(g_str_hash, g_str_equal);
170 sip_draw_hash_requests( gchar *key _U_ , sip_request_method_t *data, gchar * format)
172 if (data->packets==0)
174 printf( format, data->response, data->packets);
178 sip_draw_hash_responses( gint * key _U_ , sip_response_code_t *data, char * format)
181 g_warning("C'est quoi ce borderl key=%d\n", *key);
184 if (data->packets==0)
186 printf(format, data->response_code, data->name, data->packets );
189 /* NOT USED at this moment */
192 sip_free_hash( gpointer key, gpointer value, gpointer user_data _U_ )
200 sip_reset_hash_responses(gchar *key _U_ , sip_response_code_t *data, gpointer ptr _U_ )
205 sip_reset_hash_requests(gchar *key _U_ , sip_request_method_t *data, gpointer ptr _U_ )
211 sipstat_reset(void *psp )
216 sp->resent_packets = 0;
217 g_hash_table_foreach( sp->hash_responses, (GHFunc)sip_reset_hash_responses, NULL);
218 g_hash_table_foreach( sp->hash_requests, (GHFunc)sip_reset_hash_requests, NULL);
223 /* Main entry point to SIP tap */
225 sipstat_packet(void *psp, packet_info *pinfo _U_, epan_dissect_t *edt _U_, const void *pri)
227 const sip_info_value_t *value=pri;
228 sipstat_t *sp = (sipstat_t *)psp;
230 /* Total number of packets, including continuation packets */
233 /* Update resent count if flag set */
236 sp->resent_packets++;
240 /* Looking at both requests and responses */
241 if (value->response_code != 0)
244 guint *key = g_malloc(sizeof(guint));
245 sip_response_code_t *sc;
247 /* Look up response code in hash table */
248 *key = value->response_code;
249 sc = g_hash_table_lookup(sp->hash_responses, key);
252 /* Non-standard status code ; we classify it as others
253 * in the relevant category
254 * (Informational,Success,Redirection,Client Error,Server Error,Global Failure)
256 int i = value->response_code;
257 if ((i<100) || (i>=700))
259 /* Forget about crazy values */
264 *key=199; /* Hopefully, this status code will never be used */
287 /* Now look up this fallback code to get its text description */
288 sc = g_hash_table_lookup(sp->hash_responses, key);
296 else if (value->request_method)
299 sip_request_method_t *sc;
301 /* Look up the request method in the table */
302 sc = g_hash_table_lookup(sp->hash_requests, value->request_method);
305 /* First of this type. Create structure and initialise */
306 sc=g_malloc(sizeof(sip_request_method_t));
307 sc->response = g_strdup(value->request_method);
310 /* Insert it into request table */
311 g_hash_table_insert(sp->hash_requests, sc->response, sc);
315 /* Already existed, just update count for that method */
318 /* g_free(value->request_method); */
322 /* No request method set. Just ignore */
330 sipstat_draw(void *psp )
334 printf("===================================================================\n");
335 if (sp->filter == NULL)
336 printf("SIP Statistics\n");
338 printf("SIP Statistics with filter %s\n", sp->filter);
340 printf("\nNumber of SIP messages: %d", sp->packets);
341 printf("\nNumber of resent SIP messages: %d\n", sp->resent_packets);
342 printf( "\n* SIP Status Codes in reply packets\n");
343 g_hash_table_foreach( sp->hash_responses, (GHFunc)sip_draw_hash_responses,
344 " SIP %3d %-15s : %5d Packets\n");
345 printf("\n* List of SIP Request methods\n");
346 g_hash_table_foreach( sp->hash_requests, (GHFunc)sip_draw_hash_requests,
347 " %-15s : %5d Packets\n");
348 printf("===================================================================\n");
352 sipstat_init(const char *optarg, void* userdata _U_)
355 const char *filter=NULL;
356 GString *error_string;
358 if (strncmp (optarg, "sip,stat,", 9) == 0){
364 sp = g_malloc( sizeof(sipstat_t) );
366 sp->filter=g_strdup(filter);
370 /*g_hash_table_foreach( sip_status, (GHFunc)sip_reset_hash_responses, NULL);*/
373 error_string = register_tap_listener(
381 /* error, we failed to attach to the tap. clean up */
384 fprintf (stderr, "tshark: Couldn't register sip,stat tap: %s\n",
386 g_string_free(error_string, TRUE);
391 sp->resent_packets = 0;
396 register_tap_listener_sipstat(void)
398 register_stat_cmd_arg("sip,stat", sipstat_init,NULL);