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.
33 #include <epan/packet_info.h>
34 #include <epan/epan.h>
36 #include <epan/dissectors/packet-sip.h>
38 #include "../simple_dialog.h"
39 #include "../register.h"
40 #include "../globals.h"
41 #include "../stat_menu.h"
43 #include "gtk/gui_utils.h"
44 #include "gtk/dlg_utils.h"
45 #include "gtk/tap_dfilter_dlg.h"
49 #define SUM_STR_MAX 1024
51 /* Used to keep track of the statistics for an entire program interface */
52 typedef struct _sip_stats_t {
55 GHashTable *hash_responses;
56 GHashTable *hash_requests;
57 guint32 packets; /* number of sip packets, including continuations */
58 guint32 resent_packets;
59 guint32 average_setup_time;
60 guint32 max_setup_time;
61 guint32 min_setup_time;
62 guint32 no_of_completed_calls;
63 guint64 total_setup_time;
64 GtkWidget *packets_label;
65 GtkWidget *resent_label;
66 GtkWidget *average_setup_time_label;
68 GtkWidget *request_box; /* container for INVITE, ... */
70 GtkWidget *informational_table; /* Status code between 100 and 199 */
71 GtkWidget *success_table; /* 200 and 299 */
72 GtkWidget *redirection_table; /* 300 and 399 */
73 GtkWidget *client_error_table; /* 400 and 499 */
74 GtkWidget *server_errors_table; /* 500 and 599 */
75 GtkWidget *global_failures_table; /* 600 and 699 */
78 /* Used to keep track of the stats for a specific response code
79 * for example it can be { 3, 404, "Not Found" ,...}
80 * which means we captured 3 reply sip/1.1 404 Not Found */
81 typedef struct _sip_response_code_t {
82 guint32 packets; /* 3 */
83 guint response_code; /* 404 */
84 const gchar *name; /* "Not Found" */
85 GtkWidget *widget; /* Label where we display it */
86 GtkWidget *table; /* Table in which we put it,
87 e.g. client_error_table */
88 sipstat_t *sp; /* Pointer back to main struct */
89 } sip_response_code_t;
91 /* Used to keep track of the stats for a specific request string */
92 typedef struct _sip_request_method_t {
93 gchar *response; /* eg. : INVITE */
96 sipstat_t *sp; /* Pointer back to main struct */
97 } sip_request_method_t;
99 /* TODO: extra codes to be added from SIP extensions? */
100 static const value_string vals_status_code[] = {
103 { 181, "Call Is Being Forwarded"},
105 { 183, "Session Progress"},
106 { 199, "Informational - Others" },
110 { 299, "Success - Others"}, /* used to keep track of other Success packets */
112 { 300, "Multiple Choices"},
113 { 301, "Moved Permanently"},
114 { 302, "Moved Temporarily"},
116 { 380, "Alternative Service"},
117 { 399, "Redirection - Others"},
119 { 400, "Bad Request"},
120 { 401, "Unauthorized"},
121 { 402, "Payment Required"},
124 { 405, "Method Not Allowed"},
125 { 406, "Not Acceptable"},
126 { 407, "Proxy Authentication Required"},
127 { 408, "Request Timeout"},
129 { 412, "Conditional Request Failed"},
130 { 413, "Request Entity Too Large"},
131 { 414, "Request-URI Too Long"},
132 { 415, "Unsupported Media Type"},
133 { 416, "Unsupported URI Scheme"},
134 { 420, "Bad Extension"},
135 { 421, "Extension Required"},
136 { 422, "Session Timer Too Small"},
137 { 423, "Interval Too Brief"},
138 { 429, "Provide Referrer Identity"},
139 { 480, "Temporarily Unavailable"},
140 { 481, "Call/Transaction Does Not Exist"},
141 { 482, "Loop Detected"},
142 { 483, "Too Many Hops"},
143 { 484, "Address Incomplete"},
146 { 487, "Request Terminated"},
147 { 488, "Not Acceptable Here"},
149 { 491, "Request Pending"},
150 { 493, "Undecipherable"},
151 { 494, "Security Agreement Required"},
152 { 499, "Client Error - Others"},
154 { 500, "Server Internal Error"},
155 { 501, "Not Implemented"},
156 { 502, "Bad Gateway"},
157 { 503, "Service Unavailable"},
158 { 504, "Server Time-out"},
159 { 505, "Version Not Supported"},
160 { 513, "Message Too Large"},
161 { 599, "Server Error - Others"},
163 { 600, "Busy Everywhere"},
165 { 604, "Does Not Exist Anywhere"},
166 { 606, "Not Acceptable"},
167 { 699, "Global Failure - Others"},
172 void register_tap_listener_gtksipstat(void);
175 /* Create tables for responses and requests */
177 sip_init_hash(sipstat_t *sp)
181 /* Create responses table */
182 sp->hash_responses = g_hash_table_new(g_int_hash, g_int_equal);
184 /* Add all response codes */
185 for (i=0 ; vals_status_code[i].strptr ; i++)
187 gint *key = g_malloc (sizeof(gint));
188 sip_response_code_t *sc = g_malloc (sizeof(sip_response_code_t));
189 *key = vals_status_code[i].value;
191 sc->response_code = *key;
192 sc->name=vals_status_code[i].strptr;
196 g_hash_table_insert(sc->sp->hash_responses, key, sc);
199 /* Create empty requests table */
200 sp->hash_requests = g_hash_table_new(g_str_hash, g_str_equal);
203 /* Draw the entry for an individual request message */
205 sip_draw_hash_requests(gchar *key _U_ , sip_request_method_t *data, gchar * unused _U_)
207 gchar string_buff[SUM_STR_MAX];
209 g_assert(data!=NULL);
211 if (data->packets==0)
216 /* Build string showing method and count */
217 g_snprintf(string_buff, sizeof(string_buff),
218 " %-11s : %3d packets", data->response, data->packets);
219 if (data->widget==NULL)
221 /* Create new label */
222 data->widget=gtk_label_new(string_buff);
223 gtk_misc_set_alignment(GTK_MISC(data->widget), 0.0f, 0.5f);
224 gtk_box_pack_start(GTK_BOX(data->sp->request_box), data->widget,FALSE,FALSE, 0);
225 gtk_widget_show(data->widget);
229 /* Update existing label */
230 gtk_label_set_text(GTK_LABEL(data->widget), string_buff);
234 /* Draw an individual response entry */
236 sip_draw_hash_responses(gint * key _U_ , sip_response_code_t *data, gchar * unused _U_)
238 gchar string_buff[SUM_STR_MAX];
240 g_assert(data!=NULL);
242 if (data->packets==0)
247 /* Create an entry in the relevant box of the window */
248 if (data->widget==NULL)
252 guint i = data->response_code;
254 /* Out of valid range - ignore */
255 if ((i<100)||(i>=700))
260 /* Find the table matching the code */
263 data->table = data->sp->informational_table;
267 data->table = data->sp->success_table;
271 data->table = data->sp->redirection_table;
275 data->table = data->sp->client_error_table;
279 data->table = data->sp->server_errors_table;
283 data->table = data->sp->global_failures_table;
286 /* Get number of rows in table */
287 x = GTK_TABLE(data->table)->nrows;
289 /* Create a new label with this response, e.g. "SIP 180 Ringing" */
290 g_snprintf(string_buff, sizeof(string_buff),
291 "SIP %3d %s ", data->response_code, data->name);
292 tmp = gtk_label_new(string_buff);
294 /* Insert the label in the correct place in the table */
295 gtk_table_attach_defaults(GTK_TABLE(data->table), tmp, 0, 1, x, x+1);
296 gtk_label_set_justify(GTK_LABEL(tmp), GTK_JUSTIFY_LEFT);
297 gtk_widget_show(tmp);
299 /* Show number of packets */
300 g_snprintf(string_buff, sizeof(string_buff), "%9d", data->packets);
301 data->widget=gtk_label_new(string_buff);
303 /* Show this widget in the right place */
304 gtk_table_attach_defaults(GTK_TABLE(data->table), data->widget, 1, 2,x,x+1);
305 gtk_label_set_justify(GTK_LABEL(data->widget), GTK_JUSTIFY_RIGHT);
306 gtk_widget_show(data->widget);
308 gtk_table_resize(GTK_TABLE(data->table), x+1, 4);
312 /* Just update the existing label string */
313 g_snprintf(string_buff, sizeof(string_buff), "%9d", data->packets);
314 gtk_label_set_text(GTK_LABEL(data->widget), string_buff);
321 sip_free_hash(gpointer key, gpointer value, gpointer user_data _U_)
328 sip_reset_hash_responses(gchar *key _U_ , sip_response_code_t *data, gpointer ptr _U_)
334 sip_reset_hash_requests(gchar *key _U_ , sip_request_method_t *data, gpointer ptr _U_)
340 sipstat_reset(void *psp)
346 sp->resent_packets = 0;
347 sp->average_setup_time = 0;
348 sp->max_setup_time = 0;
349 sp->max_setup_time = 0;
350 sp->no_of_completed_calls = 0;
351 sp->total_setup_time = 0;
352 g_hash_table_foreach(sp->hash_responses, (GHFunc)sip_reset_hash_responses, NULL);
353 g_hash_table_foreach(sp->hash_requests, (GHFunc)sip_reset_hash_requests, NULL);
357 /* Main entry point to SIP tap */
359 sipstat_packet(void *psp, packet_info *pinfo _U_, epan_dissect_t *edt _U_, const void *pri)
361 const sip_info_value_t *value=pri;
362 sipstat_t *sp = (sipstat_t *)psp;
364 /* Total number of packets, including continuation packets */
367 /* Update resent count if flag set */
370 sp->resent_packets++;
373 /* Calculate average setup time */
374 if (value->setup_time){
375 sp->no_of_completed_calls++;
376 /* Check if it's the first value */
377 if ( sp->total_setup_time == 0 ){
378 sp->average_setup_time = value->setup_time;
379 sp->total_setup_time = value->setup_time;
380 sp->max_setup_time = value->setup_time;
381 sp->min_setup_time = value->setup_time;
383 sp->total_setup_time = sp->total_setup_time + value->setup_time;
384 if (sp->max_setup_time < value->setup_time){
385 sp->max_setup_time = value->setup_time;
387 if (sp->min_setup_time > value->setup_time){
388 sp->min_setup_time = value->setup_time;
390 /* Calculate average */
391 sp->average_setup_time = (guint32)(sp->total_setup_time / sp->no_of_completed_calls);
395 /* Looking at both requests and responses */
396 if (value->response_code != 0)
399 guint *key = g_malloc(sizeof(guint));
400 sip_response_code_t *sc;
402 /* Look up response code in hash table */
403 *key = value->response_code;
404 sc = g_hash_table_lookup(sp->hash_responses, key);
407 /* Non-standard status code ; we classify it as others
408 * in the relevant category
409 * (Informational,Success,Redirection,Client Error,Server Error,Global Failure)
411 int i = value->response_code;
412 if ((i<100) || (i>=700))
414 /* Forget about crazy values */
419 *key=199; /* Hopefully, this status code will never be used */
442 /* Now look up this fallback code to get its text description */
443 sc = g_hash_table_lookup(sp->hash_responses, key);
451 else if (value->request_method)
454 sip_request_method_t *sc;
456 /* Look up the request method in the table */
457 sc = g_hash_table_lookup(sp->hash_requests, value->request_method);
460 /* First of this type. Create structure and initialise */
461 sc=g_malloc(sizeof(sip_request_method_t));
462 sc->response = g_strdup(value->request_method);
466 /* Insert it into request table */
467 g_hash_table_insert(sp->hash_requests, sc->response, sc);
471 /* Already existed, just update count for that method */
474 /* g_free(value->request_method); */
478 /* No request method set. Just ignore */
485 /* Redraw the whole stats window */
487 sipstat_draw(void *psp)
489 gchar string_buff[SUM_STR_MAX];
492 /* Set summary label */
493 g_snprintf(string_buff, sizeof(string_buff),
494 "SIP stats (%d packets)", sp->packets);
495 gtk_label_set_text(GTK_LABEL(sp->packets_label), string_buff);
497 /* Set resend count label */
498 g_snprintf(string_buff, sizeof(string_buff),
499 "(%d resent packets)", sp->resent_packets);
500 gtk_label_set_text(GTK_LABEL(sp->resent_label), string_buff);
502 /* Draw responses and requests from their tables */
503 g_hash_table_foreach(sp->hash_responses, (GHFunc)sip_draw_hash_responses, NULL);
504 g_hash_table_foreach(sp->hash_requests, (GHFunc)sip_draw_hash_requests, NULL);
506 /* Set resend count label */
507 g_snprintf(string_buff, sizeof(string_buff),
508 "Average setup time %d ms\n Min %d ms\n Max %d ms", sp->average_setup_time, sp->min_setup_time, sp->max_setup_time);
509 gtk_label_set_text(GTK_LABEL(sp->average_setup_time_label), string_buff);
511 gtk_widget_show_all(sp->win);
515 /* since the gtk2 implementation of tap is multithreaded we must protect
516 * remove_tap_listener() from modifying the list while draw_tap_listener()
517 * is running. the other protected block is in main.c
519 * there should not be any other critical regions in gtk2
521 /* When window is destroyed, clean up */
523 win_destroy_cb(GtkWindow *win _U_, gpointer data)
525 sipstat_t *sp=(sipstat_t *)data;
527 protect_thread_critical_region();
528 remove_tap_listener(sp);
529 unprotect_thread_critical_region();
531 g_hash_table_foreach(sp->hash_responses, (GHFunc)sip_free_hash, NULL);
532 g_hash_table_destroy(sp->hash_responses);
533 g_hash_table_foreach(sp->hash_requests, (GHFunc)sip_free_hash, NULL);
534 g_hash_table_destroy(sp->hash_requests);
540 /* Create a new instance of gtk_sipstat. */
542 gtk_sipstat_init(const char *optarg, void *userdata _U_)
545 const char *filter = NULL;
546 GString *error_string;
548 GtkWidget *main_vb, *separator,
549 *informational_fr, *success_fr, *redirection_fr,
550 *client_errors_fr, *server_errors_fr, *global_failures_fr,
556 if (strncmp (optarg, "sip,stat,", 9) == 0)
558 /* Skip those characters from filter to display */
567 /* Create sip stats window structure */
568 sp = g_malloc(sizeof(sipstat_t));
569 sp->win = window_new(GTK_WINDOW_TOPLEVEL, "sip-stat");
571 /* Set title to include any filter given */
574 sp->filter = g_strdup(filter);
575 title = g_strdup_printf("SIP statistics with filter: %s", filter);
580 title = g_strdup("SIP statistics");
583 gtk_window_set_title(GTK_WINDOW(sp->win), title);
587 /* Create container for all widgets */
588 main_vb = gtk_vbox_new(FALSE, 12);
589 gtk_container_set_border_width(GTK_CONTAINER(main_vb), 12);
590 gtk_container_add(GTK_CONTAINER(sp->win), main_vb);
592 /* Initialise & show number of packets */
594 sp->packets_label = gtk_label_new("SIP stats (0 SIP packets)");
595 gtk_container_add(GTK_CONTAINER(main_vb), sp->packets_label);
597 sp->resent_packets = 0;
598 sp->resent_label = gtk_label_new("(0 resent packets)");
599 gtk_container_add(GTK_CONTAINER(main_vb), sp->resent_label);
600 gtk_widget_show(sp->resent_label);
603 /* Informational response frame */
604 informational_fr = gtk_frame_new("Informational SIP 1xx");
605 gtk_container_add(GTK_CONTAINER(main_vb), informational_fr);
607 /* Information table (within that frame) */
608 sp->informational_table = gtk_table_new(0, 2, FALSE);
609 gtk_container_add(GTK_CONTAINER(informational_fr), sp->informational_table);
611 /* Success table and frame */
612 success_fr = gtk_frame_new ("Success SIP 2xx");
613 gtk_container_add(GTK_CONTAINER(main_vb), success_fr);
615 sp->success_table = gtk_table_new(0, 2, FALSE);
616 gtk_container_add(GTK_CONTAINER(success_fr), sp->success_table);
618 /* Redirection table and frame */
619 redirection_fr = gtk_frame_new ("Redirection SIP 3xx");
620 gtk_container_add(GTK_CONTAINER(main_vb), redirection_fr);
622 sp->redirection_table = gtk_table_new(0, 2, FALSE);
623 gtk_container_add(GTK_CONTAINER(redirection_fr), sp->redirection_table);
625 /* Client Errors table and frame */
626 client_errors_fr = gtk_frame_new("Client errors SIP 4xx");
627 gtk_container_add(GTK_CONTAINER(main_vb), client_errors_fr);
629 sp->client_error_table = gtk_table_new(0, 2, FALSE);
630 gtk_container_add(GTK_CONTAINER(client_errors_fr), sp->client_error_table);
632 /* Server Errors table and frame */
633 server_errors_fr = gtk_frame_new("Server errors SIP 5xx");
634 gtk_container_add(GTK_CONTAINER(main_vb), server_errors_fr);
636 sp->server_errors_table = gtk_table_new(0, 2, FALSE);
637 gtk_container_add(GTK_CONTAINER(server_errors_fr), sp->server_errors_table);
639 /* Global Failures table and frame */
640 global_failures_fr = gtk_frame_new("Global failures SIP 6xx");
641 gtk_container_add(GTK_CONTAINER(main_vb), global_failures_fr);
643 sp->global_failures_table = gtk_table_new(0, 2, FALSE);
644 gtk_container_add(GTK_CONTAINER(global_failures_fr), sp->global_failures_table);
647 /* Separator between requests and responses */
648 separator = gtk_hseparator_new();
649 gtk_container_add(GTK_CONTAINER(main_vb), separator);
651 /* Request table and frame */
652 request_fr = gtk_frame_new("List of request methods");
653 gtk_container_add(GTK_CONTAINER(main_vb), request_fr);
654 gtk_container_set_border_width(GTK_CONTAINER(request_fr), 0);
656 sp->request_box = gtk_vbox_new(FALSE, 10);
657 gtk_container_add(GTK_CONTAINER(request_fr), sp->request_box);
659 sp->average_setup_time = 0;
660 sp->max_setup_time =0;
661 sp->min_setup_time =0;
662 sp->average_setup_time_label = gtk_label_new("(Not calculated)");
663 gtk_container_add(GTK_CONTAINER(main_vb), sp->average_setup_time_label);
664 gtk_widget_show(sp->average_setup_time_label);
667 /* Register this tap listener now */
668 error_string = register_tap_listener("sip",
676 /* Error. We failed to attach to the tap. Clean up */
677 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", error_string->str);
680 g_string_free(error_string, TRUE);
685 bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
686 gtk_box_pack_start(GTK_BOX(main_vb), bbox, FALSE, FALSE, 0);
688 bt_close = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_CLOSE);
689 window_set_cancel_button(sp->win, bt_close, window_cancel_button_cb);
691 g_signal_connect(sp->win, "delete_event", G_CALLBACK(window_delete_event_cb), NULL);
692 g_signal_connect(sp->win, "destroy", G_CALLBACK(win_destroy_cb), sp);
694 /* Display up-to-date contents */
695 gtk_widget_show_all(sp->win);
696 window_present(sp->win);
699 cf_retap_packets(&cfile, FALSE);
700 gdk_window_raise(sp->win->window);
703 static tap_dfilter_dlg sip_stat_dlg = {
704 "SIP Packet Counter",
710 /* Register this tap listener and add menu item. */
712 register_tap_listener_gtksipstat(void)
714 register_dfilter_stat(&sip_stat_dlg, "SIP", REGISTER_STAT_GROUP_TELEPHONY);