2 * sip message counter for ethereal
4 * $Id: tap-sipstat.c,v 1.1 2004/03/30 18:55:46 guy Exp $
5 * Copied from gtk/sip_stat.c and tap-httpstat.c
7 * Ethereal - Network traffic analyzer
8 * By Gerald Combs <gerald@ethereal.com>
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/value_string.h"
41 #include "packet-sip.h"
43 /* used to keep track of the statictics for an entire program interface */
44 typedef struct _sip_stats_t {
46 GHashTable *hash_responses;
47 GHashTable *hash_requests;
50 /* used to keep track of the stats for a specific response code
51 * for example it can be { 3, 404, "Not Found" ,...}
52 * which means we captured 3 reply sip/1.1 404 Not Found */
53 typedef struct _sip_response_code_t {
54 guint32 packets; /* 3 */
55 guint response_code; /* 404 */
56 gchar *name; /* Not Found */
58 } sip_response_code_t;
60 /* used to keep track of the stats for a specific request string */
61 typedef struct _sip_request_method_t {
62 gchar *response; /* eg. : INVITE */
65 } sip_request_method_t;
67 /* TODO: extra codes to be added from SIP extensions? */
68 static const value_string vals_status_code[] = {
71 { 181, "Call Is Being Forwarded"},
73 { 183, "Session Progress"},
74 { 199, "Informational - Others" },
78 { 299, "Success - Others"}, /* used to keep track of other Success packets */
80 { 300, "Multiple Choices"},
81 { 301, "Moved Permanently"},
82 { 302, "Moved Temporarily"},
84 { 380, "Alternative Service"},
85 { 399, "Redirection - Others"},
87 { 400, "Bad Request"},
88 { 401, "Unauthorized"},
89 { 402, "Payment Required"},
92 { 405, "Method Not Allowed"},
93 { 406, "Not Acceptable"},
94 { 407, "Proxy Authentication Required"},
95 { 408, "Request Timeout"},
97 { 413, "Request Entity Too Large"},
98 { 414, "Request-URI Too Long"},
99 { 415, "Unsupported Media Type"},
100 { 416, "Unsupported URI Scheme"},
101 { 420, "Bad Extension"},
102 { 421, "Extension Required"},
103 { 423, "Interval Too Brief"},
104 { 480, "Temporarily Unavailable"},
105 { 481, "Call/Transaction Does Not Exist"},
106 { 482, "Loop Detected"},
107 { 483, "Too Many Hops"},
108 { 484, "Address Incomplete"},
111 { 487, "Request Terminated"},
112 { 488, "Not Acceptable Here"},
114 { 491, "Request Pending"},
115 { 493, "Undecipherable"},
116 { 499, "Client Error - Others"},
118 { 500, "Server Internal Error"},
119 { 501, "Not Implemented"},
120 { 502, "Bad Gateway"},
121 { 503, "Service Unavailable"},
122 { 504, "Server Time-out"},
123 { 505, "Version Not Supported"},
124 { 513, "Message Too Large"},
125 { 599, "Server Error - Others"},
127 { 600, "Busy Everywhere"},
129 { 604, "Does Not Exist Anywhere"},
130 { 606, "Not Acceptable"},
131 { 699, "Global Failure - Others"},
136 /* Create tables for responses and requests */
138 sip_init_hash(sipstat_t *sp)
142 /* Create responses table */
143 sp->hash_responses = g_hash_table_new(g_int_hash, g_int_equal);
145 /* Add all response codes */
146 for (i=0 ; vals_status_code[i].strptr ; i++)
148 gint *key = g_malloc (sizeof(gint));
149 sip_response_code_t *sc = g_malloc (sizeof(sip_response_code_t));
150 *key = vals_status_code[i].value;
152 sc->response_code = *key;
153 sc->name=vals_status_code[i].strptr;
155 g_hash_table_insert(sc->sp->hash_responses, key, sc);
158 /* Create empty requests table */
159 sp->hash_requests = g_hash_table_new(g_str_hash, g_str_equal);
163 sip_draw_hash_requests( gchar *key _U_ , sip_request_method_t *data, gchar * format)
165 if (data->packets==0)
167 printf( format, data->response, data->packets);
171 sip_draw_hash_responses( gint * key _U_ , sip_response_code_t *data, char * format)
174 g_warning("C'est quoi ce borderl key=%d\n", *key);
177 if (data->packets==0)
179 printf(format, data->response_code, data->name, data->packets );
182 /* NOT USED at this moment */
185 sip_free_hash( gpointer key, gpointer value, gpointer user_data _U_ )
193 sip_reset_hash_responses(gchar *key _U_ , sip_response_code_t *data, gpointer ptr _U_ )
198 sip_reset_hash_requests(gchar *key _U_ , sip_request_method_t *data, gpointer ptr _U_ )
204 sipstat_reset(void *psp )
208 g_hash_table_foreach( sp->hash_responses, (GHFunc)sip_reset_hash_responses, NULL);
209 g_hash_table_foreach( sp->hash_requests, (GHFunc)sip_reset_hash_requests, NULL);
214 /* Main entry point to SIP tap */
216 sipstat_packet(void *psp, packet_info *pinfo _U_, epan_dissect_t *edt _U_, void *pri)
218 sip_info_value_t *value=pri;
219 sipstat_t *sp = (sipstat_t *)psp;
221 /* Looking at both requests and responses */
222 if (value->response_code != 0)
225 guint *key = g_malloc(sizeof(guint));
226 sip_response_code_t *sc;
228 /* Look up response code in hash table */
229 *key = value->response_code;
230 sc = g_hash_table_lookup(sp->hash_responses, key);
233 /* Non-standard status code ; we classify it as others
234 * in the relevant category
235 * (Informational,Success,Redirection,Client Error,Server Error,Global Failure)
237 int i = value->response_code;
238 if ((i<100) || (i>=700))
240 /* Forget about crazy values */
245 *key=199; /* Hopefully, this status code will never be used */
268 /* Now look up this fallback code to get its text description */
269 sc = g_hash_table_lookup(sp->hash_responses, key);
277 else if (value->request_method)
280 sip_request_method_t *sc;
282 /* Look up the request method in the table */
283 sc = g_hash_table_lookup(sp->hash_requests, value->request_method);
286 /* First of this type. Create structure and initialise */
287 sc=g_malloc(sizeof(sip_request_method_t));
288 sc->response = g_strdup(value->request_method);
291 /* Insert it into request table */
292 g_hash_table_insert(sp->hash_requests, sc->response, sc);
296 /* Already existed, just update count for that method */
299 /* g_free(value->request_method); */
303 /* No request method set. Just ignore */
311 sipstat_draw(void *psp )
315 printf("===================================================================\n");
316 if (sp->filter == NULL)
317 printf("SIP Statistics\n");
319 printf("SIP Statistics with filter %s\n", sp->filter);
321 printf( "\n* SIP Status Codes in reply packets\n");
322 g_hash_table_foreach( sp->hash_responses, (GHFunc)sip_draw_hash_responses,
323 " SIP %3d %-15s : %5d Packets\n");
324 printf("\n* List of SIP Request methods\n");
325 g_hash_table_foreach( sp->hash_requests, (GHFunc)sip_draw_hash_requests,
326 " %-15s : %5d Packets\n");
327 printf("===================================================================\n");
331 sipstat_init(char *optarg)
335 GString *error_string;
337 if (strncmp (optarg, "sip,stat,", 9) == 0){
343 sp = g_malloc( sizeof(sipstat_t) );
345 sp->filter=g_strdup(filter);
349 /*g_hash_table_foreach( sip_status, (GHFunc)sip_reset_hash_responses, NULL);*/
352 error_string = register_tap_listener(
360 /* error, we failed to attach to the tap. clean up */
363 fprintf (stderr, "tethereal: Coulnd't register sip,stat tap: %s\n",
365 g_string_free(error_string, TRUE);
373 register_tap_listener_sipstat(void)
375 register_ethereal_tap("sip,stat", sipstat_init);