2 * sip_stat 2004 Martin Mathieson
5 * Copied from http_stat.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.
34 #include <epan/packet_info.h>
35 #include <epan/epan.h>
37 #include "simple_dialog.h"
38 #include "gui_utils.h"
39 #include "dlg_utils.h"
41 #include "../register.h"
42 #include <epan/dissectors/packet-sip.h>
43 #include "../globals.h"
44 #include "compat_macros.h"
45 #include "../stat_menu.h"
46 #include "../tap_dfilter_dlg.h"
48 #define SUM_STR_MAX 1024
50 /* Used to keep track of the statistics for an entire program interface */
51 typedef struct _sip_stats_t {
54 GHashTable *hash_responses;
55 GHashTable *hash_requests;
56 guint32 packets; /* number of sip packets, including continuations */
57 guint32 resent_packets;
58 GtkWidget *packets_label;
59 GtkWidget *resent_label;
61 GtkWidget *request_box; /* container for INVITE, ... */
63 GtkWidget *informational_table; /* Status code between 100 and 199 */
64 GtkWidget *success_table; /* 200 and 299 */
65 GtkWidget *redirection_table; /* 300 and 399 */
66 GtkWidget *client_error_table; /* 400 and 499 */
67 GtkWidget *server_errors_table; /* 500 and 599 */
68 GtkWidget *global_failures_table; /* 600 and 699 */
71 /* Used to keep track of the stats for a specific response code
72 * for example it can be { 3, 404, "Not Found" ,...}
73 * which means we captured 3 reply sip/1.1 404 Not Found */
74 typedef struct _sip_response_code_t {
75 guint32 packets; /* 3 */
76 guint response_code; /* 404 */
77 const gchar *name; /* "Not Found" */
78 GtkWidget *widget; /* Label where we display it */
79 GtkWidget *table; /* Table in which we put it,
80 e.g. client_error_table */
81 sipstat_t *sp; /* Pointer back to main struct */
82 } sip_response_code_t;
84 /* Used to keep track of the stats for a specific request string */
85 typedef struct _sip_request_method_t {
86 gchar *response; /* eg. : INVITE */
89 sipstat_t *sp; /* Pointer back to main struct */
90 } sip_request_method_t;
92 /* TODO: extra codes to be added from SIP extensions? */
93 static const value_string vals_status_code[] = {
96 { 181, "Call Is Being Forwarded"},
98 { 183, "Session Progress"},
99 { 199, "Informational - Others" },
103 { 299, "Success - Others"}, /* used to keep track of other Success packets */
105 { 300, "Multiple Choices"},
106 { 301, "Moved Permanently"},
107 { 302, "Moved Temporarily"},
109 { 380, "Alternative Service"},
110 { 399, "Redirection - Others"},
112 { 400, "Bad Request"},
113 { 401, "Unauthorized"},
114 { 402, "Payment Required"},
117 { 405, "Method Not Allowed"},
118 { 406, "Not Acceptable"},
119 { 407, "Proxy Authentication Required"},
120 { 408, "Request Timeout"},
122 { 412, "Conditional Request Failed"},
123 { 413, "Request Entity Too Large"},
124 { 414, "Request-URI Too Long"},
125 { 415, "Unsupported Media Type"},
126 { 416, "Unsupported URI Scheme"},
127 { 420, "Bad Extension"},
128 { 421, "Extension Required"},
129 { 423, "Interval Too Brief"},
130 { 429, "Provide Referrer Identity"},
131 { 480, "Temporarily Unavailable"},
132 { 481, "Call/Transaction Does Not Exist"},
133 { 482, "Loop Detected"},
134 { 483, "Too Many Hops"},
135 { 484, "Address Incomplete"},
138 { 487, "Request Terminated"},
139 { 488, "Not Acceptable Here"},
141 { 491, "Request Pending"},
142 { 493, "Undecipherable"},
143 { 499, "Client Error - Others"},
145 { 500, "Server Internal Error"},
146 { 501, "Not Implemented"},
147 { 502, "Bad Gateway"},
148 { 503, "Service Unavailable"},
149 { 504, "Server Time-out"},
150 { 505, "Version Not Supported"},
151 { 513, "Message Too Large"},
152 { 599, "Server Error - Others"},
154 { 600, "Busy Everywhere"},
156 { 604, "Does Not Exist Anywhere"},
157 { 606, "Not Acceptable"},
158 { 699, "Global Failure - Others"},
163 /* Create tables for responses and requests */
165 sip_init_hash(sipstat_t *sp)
169 /* Create responses table */
170 sp->hash_responses = g_hash_table_new(g_int_hash, g_int_equal);
172 /* Add all response codes */
173 for (i=0 ; vals_status_code[i].strptr ; i++)
175 gint *key = g_malloc (sizeof(gint));
176 sip_response_code_t *sc = g_malloc (sizeof(sip_response_code_t));
177 *key = vals_status_code[i].value;
179 sc->response_code = *key;
180 sc->name=vals_status_code[i].strptr;
184 g_hash_table_insert(sc->sp->hash_responses, key, sc);
187 /* Create empty requests table */
188 sp->hash_requests = g_hash_table_new(g_str_hash, g_str_equal);
191 /* Draw the entry for an individual request message */
193 sip_draw_hash_requests(gchar *key _U_ , sip_request_method_t *data, gchar * unused _U_)
195 gchar string_buff[SUM_STR_MAX];
197 g_assert(data!=NULL);
199 if (data->packets==0)
204 /* Build string showing method and count */
205 g_snprintf(string_buff, sizeof(string_buff),
206 " %-11s : %3d packets", data->response, data->packets);
207 if (data->widget==NULL)
209 /* Create new label */
210 data->widget=gtk_label_new(string_buff);
211 gtk_misc_set_alignment(GTK_MISC(data->widget), 0.0, 0.5);
212 gtk_box_pack_start(GTK_BOX(data->sp->request_box), data->widget,FALSE,FALSE, 0);
213 gtk_widget_show(data->widget);
217 /* Update existing label */
218 gtk_label_set(GTK_LABEL(data->widget), string_buff);
222 /* Draw an individual response entry */
224 sip_draw_hash_responses(gint * key _U_ , sip_response_code_t *data, gchar * unused _U_)
226 gchar string_buff[SUM_STR_MAX];
228 g_assert(data!=NULL);
230 if (data->packets==0)
235 /* Create an entry in the relevant box of the window */
236 if (data->widget==NULL)
240 guint i = data->response_code;
242 /* Out of valid range - ignore */
243 if ((i<100)||(i>=700))
248 /* Find the table matching the code */
251 data->table = data->sp->informational_table;
255 data->table = data->sp->success_table;
259 data->table = data->sp->redirection_table;
263 data->table = data->sp->client_error_table;
267 data->table = data->sp->server_errors_table;
271 data->table = data->sp->global_failures_table;
274 /* Get number of rows in table */
275 x = GTK_TABLE(data->table)->nrows;
277 /* Create a new label with this response, e.g. "SIP 180 Ringing" */
278 g_snprintf(string_buff, sizeof(string_buff),
279 "SIP %3d %s ", data->response_code, data->name);
280 tmp = gtk_label_new(string_buff);
282 /* Insert the label in the correct place in the table */
283 gtk_table_attach_defaults(GTK_TABLE(data->table), tmp, 0, 1, x, x+1);
284 gtk_label_set_justify(GTK_LABEL(tmp), GTK_JUSTIFY_LEFT);
285 gtk_widget_show(tmp);
287 /* Show number of packets */
288 g_snprintf(string_buff, sizeof(string_buff), "%9d", data->packets);
289 data->widget=gtk_label_new(string_buff);
291 /* Show this widget in the right place */
292 gtk_table_attach_defaults(GTK_TABLE(data->table), data->widget, 1, 2,x,x+1);
293 gtk_label_set_justify(GTK_LABEL(data->widget), GTK_JUSTIFY_RIGHT);
294 gtk_widget_show(data->widget);
296 gtk_table_resize(GTK_TABLE(data->table), x+1, 4);
300 /* Just update the existing label string */
301 g_snprintf(string_buff, sizeof(string_buff), "%9d", data->packets);
302 gtk_label_set(GTK_LABEL(data->widget), string_buff);
309 sip_free_hash(gpointer key, gpointer value, gpointer user_data _U_)
316 sip_reset_hash_responses(gchar *key _U_ , sip_response_code_t *data, gpointer ptr _U_)
322 sip_reset_hash_requests(gchar *key _U_ , sip_request_method_t *data, gpointer ptr _U_)
328 sipstat_reset(void *psp)
334 sp->resent_packets = 0;
335 g_hash_table_foreach(sp->hash_responses, (GHFunc)sip_reset_hash_responses, NULL);
336 g_hash_table_foreach(sp->hash_requests, (GHFunc)sip_reset_hash_requests, NULL);
340 /* Main entry point to SIP tap */
342 sipstat_packet(void *psp, packet_info *pinfo _U_, epan_dissect_t *edt _U_, const void *pri)
344 const sip_info_value_t *value=pri;
345 sipstat_t *sp = (sipstat_t *)psp;
347 /* Total number of packets, including continuation packets */
350 /* Update resent count if flag set */
353 sp->resent_packets++;
357 /* Looking at both requests and responses */
358 if (value->response_code != 0)
361 guint *key = g_malloc(sizeof(guint));
362 sip_response_code_t *sc;
364 /* Look up response code in hash table */
365 *key = value->response_code;
366 sc = g_hash_table_lookup(sp->hash_responses, key);
369 /* Non-standard status code ; we classify it as others
370 * in the relevant category
371 * (Informational,Success,Redirection,Client Error,Server Error,Global Failure)
373 int i = value->response_code;
374 if ((i<100) || (i>=700))
376 /* Forget about crazy values */
381 *key=199; /* Hopefully, this status code will never be used */
404 /* Now look up this fallback code to get its text description */
405 sc = g_hash_table_lookup(sp->hash_responses, key);
413 else if (value->request_method)
416 sip_request_method_t *sc;
418 /* Look up the request method in the table */
419 sc = g_hash_table_lookup(sp->hash_requests, value->request_method);
422 /* First of this type. Create structure and initialise */
423 sc=g_malloc(sizeof(sip_request_method_t));
424 sc->response = g_strdup(value->request_method);
428 /* Insert it into request table */
429 g_hash_table_insert(sp->hash_requests, sc->response, sc);
433 /* Already existed, just update count for that method */
436 /* g_free(value->request_method); */
440 /* No request method set. Just ignore */
447 /* Redraw the whole stats window */
449 sipstat_draw(void *psp)
451 gchar string_buff[SUM_STR_MAX];
454 /* Set summary label */
455 g_snprintf(string_buff, sizeof(string_buff),
456 "SIP stats (%d packets)", sp->packets);
457 gtk_label_set(GTK_LABEL(sp->packets_label), string_buff);
459 /* Set resend count label */
460 g_snprintf(string_buff, sizeof(string_buff),
461 "(%d resent packets)", sp->resent_packets);
462 gtk_label_set(GTK_LABEL(sp->resent_label), string_buff);
464 /* Draw responses and requests from their tables */
465 g_hash_table_foreach(sp->hash_responses, (GHFunc)sip_draw_hash_responses, NULL);
466 g_hash_table_foreach(sp->hash_requests, (GHFunc)sip_draw_hash_requests, NULL);
468 gtk_widget_show_all(sp->win);
472 /* since the gtk2 implementation of tap is multithreaded we must protect
473 * remove_tap_listener() from modifying the list while draw_tap_listener()
474 * is running. the other protected block is in main.c
476 * there should not be any other critical regions in gtk2
478 void protect_thread_critical_region(void);
479 void unprotect_thread_critical_region(void);
481 /* When window is destroyed, clean up */
483 win_destroy_cb(GtkWindow *win _U_, gpointer data)
485 sipstat_t *sp=(sipstat_t *)data;
487 protect_thread_critical_region();
488 remove_tap_listener(sp);
489 unprotect_thread_critical_region();
491 g_hash_table_foreach(sp->hash_responses, (GHFunc)sip_free_hash, NULL);
492 g_hash_table_destroy(sp->hash_responses);
493 g_hash_table_foreach(sp->hash_requests, (GHFunc)sip_free_hash, NULL);
494 g_hash_table_destroy(sp->hash_requests);
500 /* Create a new instance of gtk_sipstat. */
502 gtk_sipstat_init(const char *optarg, void *userdata _U_)
505 const char *filter = NULL;
506 GString *error_string;
508 GtkWidget *main_vb, *separator,
509 *informational_fr, *success_fr, *redirection_fr,
510 *client_errors_fr, *server_errors_fr, *global_failures_fr,
516 if (strncmp (optarg, "sip,stat,", 9) == 0)
518 /* Skip those characters from filter to display */
527 /* Create sip stats window structure */
528 sp = g_malloc(sizeof(sipstat_t));
529 sp->win = window_new(GTK_WINDOW_TOPLEVEL, "sip-stat");
531 /* Set title to include any filter given */
534 sp->filter = g_strdup(filter);
535 title = g_strdup_printf("SIP statistics with filter: %s", filter);
540 title = g_strdup("SIP statistics");
543 gtk_window_set_title(GTK_WINDOW(sp->win), title);
547 /* Create container for all widgets */
548 main_vb = gtk_vbox_new(FALSE, 12);
549 gtk_container_border_width(GTK_CONTAINER(main_vb), 12);
550 gtk_container_add(GTK_CONTAINER(sp->win), main_vb);
552 /* Initialise & show number of packets */
554 sp->packets_label = gtk_label_new("SIP stats (0 SIP packets)");
555 gtk_container_add(GTK_CONTAINER(main_vb), sp->packets_label);
557 sp->resent_packets = 0;
558 sp->resent_label = gtk_label_new("(0 resent packets)");
559 gtk_container_add(GTK_CONTAINER(main_vb), sp->resent_label);
560 gtk_widget_show(sp->resent_label);
563 /* Informational response frame */
564 informational_fr = gtk_frame_new("Informational SIP 1xx");
565 gtk_container_add(GTK_CONTAINER(main_vb), informational_fr);
567 /* Information table (within that frame) */
568 sp->informational_table = gtk_table_new(0, 2, FALSE);
569 gtk_container_add(GTK_CONTAINER(informational_fr), sp->informational_table);
571 /* Success table and frame */
572 success_fr = gtk_frame_new ("Success SIP 2xx");
573 gtk_container_add(GTK_CONTAINER(main_vb), success_fr);
575 sp->success_table = gtk_table_new(0, 2, FALSE);
576 gtk_container_add(GTK_CONTAINER(success_fr), sp->success_table);
578 /* Redirection table and frame */
579 redirection_fr = gtk_frame_new ("Redirection SIP 3xx");
580 gtk_container_add(GTK_CONTAINER(main_vb), redirection_fr);
582 sp->redirection_table = gtk_table_new(0, 2, FALSE);
583 gtk_container_add(GTK_CONTAINER(redirection_fr), sp->redirection_table);
585 /* Client Errors table and frame */
586 client_errors_fr = gtk_frame_new("Client errors SIP 4xx");
587 gtk_container_add(GTK_CONTAINER(main_vb), client_errors_fr);
589 sp->client_error_table = gtk_table_new(0, 2, FALSE);
590 gtk_container_add(GTK_CONTAINER(client_errors_fr), sp->client_error_table);
592 /* Server Errors table and frame */
593 server_errors_fr = gtk_frame_new("Server errors SIP 5xx");
594 gtk_container_add(GTK_CONTAINER(main_vb), server_errors_fr);
596 sp->server_errors_table = gtk_table_new(0, 2, FALSE);
597 gtk_container_add(GTK_CONTAINER(server_errors_fr), sp->server_errors_table);
599 /* Global Failures table and frame */
600 global_failures_fr = gtk_frame_new("Global failures SIP 6xx");
601 gtk_container_add(GTK_CONTAINER(main_vb), global_failures_fr);
603 sp->global_failures_table = gtk_table_new(0, 2, FALSE);
604 gtk_container_add(GTK_CONTAINER(global_failures_fr), sp->global_failures_table);
607 /* Separator between requests and responses */
608 separator = gtk_hseparator_new();
609 gtk_container_add(GTK_CONTAINER(main_vb), separator);
611 /* Request table and frame */
612 request_fr = gtk_frame_new("List of request methods");
613 gtk_container_add(GTK_CONTAINER(main_vb), request_fr);
614 gtk_container_border_width(GTK_CONTAINER(request_fr), 0);
616 sp->request_box = gtk_vbox_new(FALSE, 10);
617 gtk_container_add(GTK_CONTAINER(request_fr), sp->request_box);
620 /* Register this tap listener now */
621 error_string = register_tap_listener("sip",
629 /* Error. We failed to attach to the tap. Clean up */
630 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, error_string->str);
633 g_string_free(error_string, TRUE);
638 bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
639 gtk_box_pack_start(GTK_BOX(main_vb), bbox, FALSE, FALSE, 0);
641 bt_close = OBJECT_GET_DATA(bbox, GTK_STOCK_CLOSE);
642 window_set_cancel_button(sp->win, bt_close, window_cancel_button_cb);
644 SIGNAL_CONNECT(sp->win, "delete_event", window_delete_event_cb, NULL);
645 SIGNAL_CONNECT(sp->win, "destroy", win_destroy_cb, sp);
647 /* Display up-to-date contents */
648 gtk_widget_show_all(sp->win);
649 window_present(sp->win);
652 cf_retap_packets(&cfile, FALSE);
655 static tap_dfilter_dlg sip_stat_dlg = {
656 "SIP Packet Counter",
662 /* Register this tap listener and add menu item. */
664 register_tap_listener_gtksipstat(void)
666 register_dfilter_stat(&sip_stat_dlg, "SIP", REGISTER_STAT_GROUP_TELEPHONY);