Last set of char -> const char trivial warning fixes.
[obnox/wireshark/wip.git] / gtk / voip_calls_dlg.c
1 /* voip_calls_dlg.c
2  * VoIP calls summary addition for ethereal
3  *
4  * $Id$
5  *
6  * Copyright 2004, Ericsson , Spain
7  * By Francisco Alcoba <francisco.alcoba@ericsson.com>
8  *
9  * based on h323_calls_dlg.c
10  * Copyright 2004, Iskratel, Ltd, Kranj
11  * By Miha Jemec <m.jemec@iskratel.si>
12  *
13  * H323, RTP and Graph Support
14  * By Alejandro Vaquero, alejandro.vaquero@verso.com
15  * Copyright 2005, Verso Technologies Inc.
16  *
17  * Ethereal - Network traffic analyzer
18  * By Gerald Combs <gerald@ethereal.com>
19  * Copyright 1998 Gerald Combs
20  *
21  * This program is free software; you can redistribute it and/or
22  * modify it under the terms of the GNU General Public License
23  * as published by the Free Software Foundation; either version 2
24  * of the License, or (at your option) any later version.
25  *
26  * This program is distributed in the hope that it will be useful,
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29  * GNU General Public License for more details.
30  *
31  * You should have received a copy of the GNU General Public License
32  * along with this program; if not, write to the Free Software
33  * Foundation,  Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
34  */
35
36 #ifdef HAVE_CONFIG_H
37 #  include <config.h>
38 #endif
39
40 #include <string.h>
41 #include <ctype.h>
42
43 #include "register.h"
44
45 #include "graph_analysis.h"
46 #include "voip_calls_dlg.h"
47 #include "voip_calls.h"
48
49 #include "globals.h"
50 #include "epan/filesystem.h"
51
52 #include <epan/tap.h>
53 #include "tap_menu.h"
54 #include "dlg_utils.h"
55 #include "ui_util.h"
56 #include "compat_macros.h"
57 #include "gtkglobals.h"
58
59 #include "image/clist_ascend.xpm"
60 #include "image/clist_descend.xpm"
61 #include "simple_dialog.h"
62
63 #include <epan/to_str.h>
64
65 #include <string.h>
66 #include <ctype.h>
67 #include <epan/address.h>
68 #include <epan/addr_resolv.h>
69  
70 static const gchar FWD_LABEL_TEXT[] = "Select one call.";
71
72 /****************************************************************************/
73 /* pointer to the one and only dialog window */
74 static GtkWidget *voip_calls_dlg = NULL;
75
76 static GtkWidget *clist = NULL;
77 static GtkWidget *top_label = NULL;
78 static GtkWidget *status_label = NULL;
79 static GtkWidget *label_fwd = NULL;
80
81 /*static GtkWidet *bt_unselect = NULL;*/
82 static GtkWidget *bt_filter = NULL;
83 static GtkWidget *bt_graph = NULL;
84
85
86 static voip_calls_info_t* selected_call_fwd = NULL;  /* current selection */
87 static GList *last_list = NULL;
88
89 static guint32 calls_nb = 0;     /* number of displayed calls */
90 static guint32 calls_ns = 0;     /* number of selected calls */
91
92 static graph_analysis_data_t *graph_analysis_data = NULL;
93
94 #define NUM_COLS 9
95 static const GdkColor COLOR_SELECT = {0, 0x00ff, 0x80ff, 0x80ff};
96 static const GdkColor COLOR_DEFAULT = {0, 0xffff, 0xffff, 0xffff};
97
98 /****************************************************************************/
99 /* append a line to clist */
100 static void add_to_clist(voip_calls_info_t* strinfo)
101 {
102         gchar label_text[256];
103         gint added_row;
104         gchar *data[NUM_COLS];
105         gchar field[NUM_COLS][50];
106         gint c;
107         isup_calls_info_t *tmp_isupinfo;
108         h323_calls_info_t *tmp_h323info;
109         gboolean tmp_bool = FALSE;
110         GdkColor color = COLOR_SELECT;
111         for (c=0;c<NUM_COLS;c++){
112                 data[c]=&field[c][0];
113         }
114
115 /*      strinfo->selected = FALSE;*/
116
117         g_snprintf(field[0], 15, "%i.%2i", strinfo->start_sec, strinfo->start_usec/10000);
118         g_snprintf(field[1], 15, "%i.%2i", strinfo->stop_sec, strinfo->stop_usec/10000);
119 /*      xxx display_signed_time(data[0], sizeof(field[0]), strinfo->start_sec, strinfo->start_usec, USECS); */
120 /*      display_signed_time(data[1], sizeof(field[0]), strinfo->stop_sec, strinfo->stop_usec, USECS); */
121         g_snprintf(field[2], 30, "%s", get_addr_name(&(strinfo->initial_speaker)));
122         g_snprintf(field[3], 50, "%s", strinfo->from_identity);
123         g_snprintf(field[4], 50, "%s", strinfo->to_identity);
124         g_snprintf(field[5], 15, "%s", voip_protocol_name[strinfo->protocol]);
125         g_snprintf(field[6], 15, "%u", strinfo->npackets);
126         g_snprintf(field[7], 15, "%s", voip_call_state_name[strinfo->call_state]);
127
128         /* Add comments based on the protocol */
129         switch(strinfo->protocol){
130                 case VOIP_ISUP:
131                         tmp_isupinfo = strinfo->prot_info;
132                         g_snprintf(field[8],30, "%i-%i -> %i-%i", tmp_isupinfo->ni, tmp_isupinfo->opc,
133                                 tmp_isupinfo->ni, tmp_isupinfo->dpc);
134                         break;
135                 case VOIP_H323:
136                         tmp_h323info = strinfo->prot_info;
137                         if (strinfo->call_state == VOIP_CALL_SETUP) 
138                                 tmp_bool = tmp_h323info->is_faststart_Setup;
139                         else
140                                 if ((tmp_h323info->is_faststart_Setup == TRUE) && (tmp_h323info->is_faststart_Proc == TRUE)) tmp_bool = TRUE; 
141                         g_snprintf(field[8],35, "Tunneling: %s  Fast Start: %s", (tmp_h323info->is_h245Tunneling==TRUE?"ON":"OFF"), 
142                                 (tmp_bool==TRUE?"ON":"OFF"));
143                         break;
144                 default:
145                         field[8][0]='\0';
146         }
147
148         
149         added_row = gtk_clist_append(GTK_CLIST(clist), data);
150
151         /* set the background color if selected */
152         if (strinfo->selected) { 
153                 calls_ns++;
154                 gtk_clist_set_background(GTK_CLIST(clist), added_row, &color);
155         }
156
157         /* set data pointer of last row to point to user data for that row */
158         gtk_clist_set_row_data(GTK_CLIST(clist), added_row, strinfo);
159
160         /* Update the top label with the number of detected calls */
161         calls_nb++;
162         g_snprintf(label_text, 256,
163                 "Detected %d VoIP %s. Selected %d %s.",
164                 calls_nb, 
165                 plurality(calls_nb, "Call", "Calls"),
166                 calls_ns,
167                 plurality(calls_ns, "Call", "Calls"));
168         gtk_label_set(GTK_LABEL(top_label), label_text);
169
170         /* Update the status label with the number of total messages */
171         g_snprintf(label_text, 256,
172                 "Total: Calls: %d   Start packets: %d   Completed calls: %d   Rejected calls: %d",
173                         g_list_length(voip_calls_get_info()->strinfo_list),
174                         voip_calls_get_info()->start_packets, 
175                         voip_calls_get_info()->completed_calls,
176                         voip_calls_get_info()->rejected_calls);
177         gtk_label_set(GTK_LABEL(status_label), label_text);
178 }
179
180
181 void voip_calls_remove_tap_listener(void)
182 {
183         /* Remove the calls tap listener */
184         remove_tap_listener_sip_calls();
185         remove_tap_listener_isup_calls();
186         remove_tap_listener_mtp3_calls();
187         remove_tap_listener_h225_calls();
188         remove_tap_listener_h245dg_calls();
189         remove_tap_listener_q931_calls();
190         remove_tap_listener_sdp_calls();
191         remove_tap_listener_rtp();
192         remove_tap_listener_rtp_event();
193         if (find_tap_id("mgcp")) {
194                 remove_tap_listener_mgcp_calls();
195         }
196         remove_tap_listener_actrace_calls();
197 }
198
199 /****************************************************************************/
200 /* CALLBACKS                                                                */
201 /****************************************************************************/
202 static void
203 voip_calls_on_destroy                      (GtkObject       *object _U_,
204                                         gpointer         user_data _U_)
205 {
206         /* remove_tap_listeners */
207         voip_calls_remove_tap_listener();
208
209         /* Clean up memory used by calls tap */
210         voip_calls_dlg_reset(NULL);
211
212         /* Note that we no longer have a "VoIP Calls" dialog box. */
213         voip_calls_dlg = NULL;
214
215         graph_analysis_data = NULL;
216 }
217
218
219 /****************************************************************************/
220 static void
221 voip_calls_on_unselect                  (GtkButton       *button _U_,
222                                         gpointer         user_data _U_)
223 {
224         selected_call_fwd = NULL;
225         gtk_clist_unselect_all(GTK_CLIST(clist));
226         gtk_label_set_text(GTK_LABEL(label_fwd), FWD_LABEL_TEXT);
227
228         /*gtk_widget_set_sensitive(bt_unselect, FALSE);*/
229         gtk_widget_set_sensitive(bt_filter, FALSE);
230         gtk_widget_set_sensitive(bt_graph, FALSE);
231 }
232
233
234 /****************************************************************************/
235 static void
236 voip_calls_on_filter                    (GtkButton       *button _U_,
237                                         gpointer         user_data _U_)
238 {
239         const gchar *filter_string;
240         gchar c;
241         GString *filter_string_fwd;
242         const gchar *filter_prepend;
243         gboolean isFirst = TRUE;
244         GList* list;
245         guint filter_length = 0;
246         guint max_filter_length = 2048;
247         sip_calls_info_t *tmp_sipinfo;
248         isup_calls_info_t *tmp_isupinfo;
249         h323_calls_info_t *tmp_h323info;
250         h245_address_t *h245_add = NULL;
251
252         graph_analysis_item_t *gai;
253
254         if (selected_call_fwd==NULL)
255                 return;
256
257         filter_string=gtk_entry_get_text(GTK_ENTRY(main_display_filter_widget));
258         filter_length = strlen(filter_string);
259         filter_prepend = "";
260         while ((c = *filter_string++) != '\0') {
261                 if (!isspace((guchar)c)) {
262                         /* The filter string isn't blank, so there's already
263                            an expression; we OR in the new expression */
264                         filter_prepend = " or ";
265                         break;
266                 }
267         }
268                 
269         filter_string_fwd = g_string_new(filter_prepend);
270         
271         /* look in the Graph and get all the frame_num for this call */
272         g_string_sprintfa(filter_string_fwd, " (");
273         list = g_list_first(voip_calls_get_info()->graph_analysis->list);
274         while (list)
275         {
276                 gai = list->data;
277                 if (gai->conv_num == selected_call_fwd->call_num){
278                         g_string_sprintfa(filter_string_fwd,"%sframe.number == %d", isFirst?"":" or ", gai->frame_num );
279                         isFirst = FALSE;
280                 }               
281                 list = g_list_next (list);
282         }
283         g_string_sprintfa(filter_string_fwd, ") ");
284         filter_length = filter_length + filter_string_fwd->len;
285
286         if (filter_length < max_filter_length){
287                 gtk_entry_append_text(GTK_ENTRY(main_display_filter_widget), filter_string_fwd->str);
288         }
289         else{
290
291                 g_string_free(filter_string_fwd, TRUE);
292                 filter_string_fwd = g_string_new(filter_prepend);
293
294                 switch(selected_call_fwd->protocol){
295                         case VOIP_SIP:
296                                 tmp_sipinfo = selected_call_fwd->prot_info;
297                                 g_string_sprintfa(filter_string_fwd,
298                                    "(sip.Call-ID == \"%s\") ",
299                                    tmp_sipinfo->call_identifier 
300                                    );
301                                 gtk_entry_append_text(GTK_ENTRY(main_display_filter_widget), filter_string_fwd->str);
302                                 break;
303                         case VOIP_ISUP:
304                                 tmp_isupinfo = selected_call_fwd->prot_info;
305                                 g_string_sprintfa(filter_string_fwd,
306                                    "(isup.cic == %i and frame.number >=%i and frame.number<=%i and mtp3.network_indicator == %i and ((mtp3.dpc == %i) and (mtp3.opc == %i)) or((mtp3.dpc == %i) and (mtp3.opc == %i))) ",
307                                    tmp_isupinfo->cic,selected_call_fwd->first_frame_num,
308                                    selected_call_fwd->last_frame_num, 
309                                    tmp_isupinfo->ni, tmp_isupinfo->dpc, tmp_isupinfo->opc, 
310                                    tmp_isupinfo->opc, tmp_isupinfo->dpc
311                                    );
312                                 gtk_entry_append_text(GTK_ENTRY(main_display_filter_widget), filter_string_fwd->str);
313                                 break;
314                         case VOIP_H323:
315                                 tmp_h323info = selected_call_fwd->prot_info;
316                                 g_string_sprintfa(filter_string_fwd,
317                                    "((h225.guid == %x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x || q931.call_ref == %x:%x || q931.call_ref == %x:%x) ",
318                                    (guint8)tmp_h323info->guid[0], (guint8)tmp_h323info->guid[1], (guint8)tmp_h323info->guid[2],
319                                    (guint8)tmp_h323info->guid[3], (guint8)tmp_h323info->guid[4], (guint8)tmp_h323info->guid[5], (guint8)tmp_h323info->guid[6],
320                            (guint8)tmp_h323info->guid[7], (guint8)tmp_h323info->guid[8], (guint8)tmp_h323info->guid[9], (guint8)tmp_h323info->guid[10],
321                                    (guint8)tmp_h323info->guid[11], (guint8)tmp_h323info->guid[12], (guint8)tmp_h323info->guid[13], (guint8)tmp_h323info->guid[14],
322                                    (guint8)tmp_h323info->guid[15], (guint8)(tmp_h323info->q931_crv & 0xff), (guint8)((tmp_h323info->q931_crv & 0xff00)>>8)
323                                    , (guint8)(tmp_h323info->q931_crv2 & 0xff), (guint8)((tmp_h323info->q931_crv2 & 0xff00)>>8));
324                                 list = g_list_first(tmp_h323info->h245_list);
325                                 while (list)
326                                 {
327                                         h245_add=list->data;
328                                         g_string_sprintfa(filter_string_fwd,
329                                                 " || (ip.addr == %s && tcp.port == %d && h245) ", 
330                                                 ip_to_str((guint8 *)&(h245_add->h245_address)), h245_add->h245_port);
331                                 list = g_list_next(list);
332                                 }
333                                 g_string_sprintfa(filter_string_fwd, ") ");
334                                 gtk_entry_append_text(GTK_ENTRY(main_display_filter_widget), filter_string_fwd->str);
335                                 break;
336                         case VOIP_MGCP:
337                         case VOIP_AC_ISDN:
338                         case VOIP_AC_CAS:
339                                 /* XXX - not supported */
340                                 break;
341                 }
342                 
343         }
344         
345         g_string_free(filter_string_fwd, TRUE);
346 }
347
348
349
350
351 /****************************************************************************/
352 static void
353 on_graph_bt_clicked                    (GtkButton       *button _U_,
354                                         gpointer         user_data _U_)
355 {
356         graph_analysis_item_t *gai;
357         GList* list;
358         GList* list2;
359         voip_calls_info_t *tmp_listinfo;
360
361         /* reset the "display" parameter in graph analysis */
362         list2 = g_list_first(voip_calls_get_info()->graph_analysis->list);
363         while (list2){
364                 gai = list2->data;
365                 gai->display = FALSE;
366                 list2 = g_list_next(list2);
367         }
368
369
370         /* set the display for selected calls */
371         list = g_list_first(voip_calls_get_info()->strinfo_list);
372         while (list){
373                 tmp_listinfo=list->data;
374                 if (tmp_listinfo->selected){
375                         list2 = g_list_first(voip_calls_get_info()->graph_analysis->list);
376                         while (list2){
377                                 gai = list2->data;
378                                 if (gai->conv_num == tmp_listinfo->call_num){
379                                         gai->display = TRUE;
380                                 }
381                                 list2 = g_list_next(list2);
382                         }
383                 }
384                 list = g_list_next(list);
385         }
386
387         /* create or refresh the graph windows */
388         if (graph_analysis_data->dlg.window == NULL)    /* create the window */
389                 graph_analysis_create(graph_analysis_data);
390         else
391                 graph_analysis_update(graph_analysis_data);             /* refresh it */
392 }
393
394 /****************************************************************************/
395 /* when the user selects a row in the calls list */
396 static void
397 voip_calls_on_select_row(GtkCList *clist,
398                                             gint row _U_,
399                                             gint column _U_,
400                                             GdkEventButton *event _U_,
401                                             gpointer user_data _U_)
402 {
403         GdkColor color = COLOR_DEFAULT;
404         gchar label_text[80];
405         
406         selected_call_fwd = gtk_clist_get_row_data(GTK_CLIST(clist), row);
407
408         if (selected_call_fwd==NULL)
409                 return;
410
411         if (!selected_call_fwd->selected)
412                 calls_ns++;
413         else
414                 calls_ns--;
415
416         g_snprintf(label_text, 256,
417                 "Detected %d VoIP %s. Selected %d %s.",
418                 calls_nb, 
419             plurality(calls_nb, "Call", "Calls"),
420                         calls_ns,
421                         plurality(calls_ns, "Call", "Calls"));
422         gtk_label_set(GTK_LABEL(top_label), label_text);
423
424         g_snprintf(label_text, 80, "Selected Call: From  %s  To %s, starting time %i.%i",
425                 selected_call_fwd->from_identity,
426                 selected_call_fwd->to_identity,
427                 selected_call_fwd->start_sec, 
428                 selected_call_fwd->start_usec
429         );
430         gtk_label_set_text(GTK_LABEL(label_fwd), label_text);
431
432         selected_call_fwd->selected=!selected_call_fwd->selected;
433         if (selected_call_fwd->selected)
434                 color = COLOR_SELECT;
435         else
436                 color = COLOR_DEFAULT;
437
438         gtk_clist_set_background(GTK_CLIST(clist), row, &color);
439
440         if      (calls_ns > 0) {
441                 gtk_widget_set_sensitive(bt_filter, TRUE);
442                 gtk_widget_set_sensitive(bt_graph, TRUE);
443         } else {
444                 gtk_widget_set_sensitive(bt_filter, FALSE);
445                 gtk_widget_set_sensitive(bt_graph, FALSE);
446         }
447
448         /* TODO: activate other buttons when implemented */
449 }
450
451
452 /****************************************************************************/
453
454 typedef struct column_arrows {
455         GtkWidget *table;
456         GtkWidget *ascend_pm;
457         GtkWidget *descend_pm;
458 } column_arrows;
459
460
461 /****************************************************************************/
462 static void
463 voip_calls_click_column_cb(GtkCList *clist, gint column, gpointer data)
464 {
465         column_arrows *col_arrows = (column_arrows *) data;
466         int i;
467
468         gtk_clist_freeze(clist);
469
470         for (i=0; i<NUM_COLS; i++) {
471                 gtk_widget_hide(col_arrows[i].ascend_pm);
472                 gtk_widget_hide(col_arrows[i].descend_pm);
473         }
474
475         if (column == clist->sort_column) {
476                 if (clist->sort_type == GTK_SORT_ASCENDING) {
477                         clist->sort_type = GTK_SORT_DESCENDING;
478                         gtk_widget_show(col_arrows[column].descend_pm);
479                 } else {
480                         clist->sort_type = GTK_SORT_ASCENDING;
481                         gtk_widget_show(col_arrows[column].ascend_pm);
482                 }
483         } else {
484                 clist->sort_type = GTK_SORT_ASCENDING;
485                 gtk_widget_show(col_arrows[column].ascend_pm);
486                 gtk_clist_set_sort_column(clist, column);
487         }
488         gtk_clist_thaw(clist);
489
490         gtk_clist_sort(clist);
491 }
492
493
494 /****************************************************************************/
495 static gint
496 voip_calls_sort_column(GtkCList *clist, gconstpointer ptr1, gconstpointer ptr2)
497 {
498         char *text1 = NULL;
499         char *text2 = NULL;
500         int i1, i2, i3, i4;
501
502         const GtkCListRow *row1 = (const GtkCListRow *) ptr1;
503         const GtkCListRow *row2 = (const GtkCListRow *) ptr2;
504
505         text1 = GTK_CELL_TEXT (row1->cell[clist->sort_column])->text;
506         text2 = GTK_CELL_TEXT (row2->cell[clist->sort_column])->text;
507
508         switch(clist->sort_column){
509         case 0:
510         case 1:
511                 if ((sscanf(text1, "%u.%u", &i1, &i2) != 2) ||
512                         (sscanf(text2, "%u.%u", &i3, &i4) != 2) ){
513                                 return 0;
514                         }
515                 if (i1>i3)
516                         return 1;
517                 if (i1<i3)
518                         return -1;
519                 return (i3-i4);
520         case 2:
521         case 3:
522         case 4:
523         case 5:
524         case 7:
525         case 8:
526                 return strcmp (text1, text2);
527         case 6:
528                 i1=atoi(text1);
529                 i2=atoi(text2);
530                 return i1-i2;
531         }
532         g_assert_not_reached();
533         return 0;
534 }
535
536
537 /****************************************************************************/
538 /* INTERFACE                                                                */
539 /****************************************************************************/
540
541 static void voip_calls_dlg_create (void)
542 {
543         GtkWidget *voip_calls_dlg_w;
544         GtkWidget *main_vb;
545         GtkWidget *scrolledwindow;
546         GtkWidget *hbuttonbox;
547         GtkWidget *bt_close;
548         GtkTooltips *tooltips = gtk_tooltips_new();
549
550         const gchar *titles[NUM_COLS] =  {"Start Time", "Stop Time", "Initial Speaker", "From",  "To", "Protocol", "Packets", "State", "Comments"};
551         column_arrows *col_arrows;
552         GtkWidget *column_lb;
553         int i;
554
555         voip_calls_dlg_w=window_new(GTK_WINDOW_TOPLEVEL, "Ethereal: VoIP Calls");
556
557         gtk_window_set_default_size(GTK_WINDOW(voip_calls_dlg_w), 840, 350);
558
559         main_vb = gtk_vbox_new (FALSE, 0);
560         gtk_container_add(GTK_CONTAINER(voip_calls_dlg_w), main_vb);
561         gtk_container_set_border_width (GTK_CONTAINER (main_vb), 12);
562
563         top_label = gtk_label_new ("Detected 0 VoIP Calls. Selected 0 Calls.");
564         gtk_box_pack_start (GTK_BOX (main_vb), top_label, FALSE, FALSE, 8);
565
566         scrolledwindow = scrolled_window_new (NULL, NULL);
567         gtk_box_pack_start (GTK_BOX (main_vb), scrolledwindow, TRUE, TRUE, 0);
568
569         clist = gtk_clist_new (NUM_COLS);
570         gtk_container_add (GTK_CONTAINER (scrolledwindow), clist);
571
572         gtk_clist_set_column_width (GTK_CLIST (clist), 0, 60);
573         gtk_clist_set_column_width (GTK_CLIST (clist), 1, 60);
574         gtk_clist_set_column_width (GTK_CLIST (clist), 2, 80);
575         gtk_clist_set_column_width (GTK_CLIST (clist), 3, 130);
576         gtk_clist_set_column_width (GTK_CLIST (clist), 4, 130);
577         gtk_clist_set_column_width (GTK_CLIST (clist), 5, 50);
578         gtk_clist_set_column_width (GTK_CLIST (clist), 6, 45);
579         gtk_clist_set_column_width (GTK_CLIST (clist), 7, 60);
580         gtk_clist_set_column_width (GTK_CLIST (clist), 8, 100);
581
582         gtk_clist_set_column_justification(GTK_CLIST(clist), 0, GTK_JUSTIFY_LEFT);
583         gtk_clist_set_column_justification(GTK_CLIST(clist), 1, GTK_JUSTIFY_LEFT);
584         gtk_clist_set_column_justification(GTK_CLIST(clist), 2, GTK_JUSTIFY_LEFT);
585         gtk_clist_set_column_justification(GTK_CLIST(clist), 3, GTK_JUSTIFY_LEFT);
586         gtk_clist_set_column_justification(GTK_CLIST(clist), 4, GTK_JUSTIFY_LEFT);
587         gtk_clist_set_column_justification(GTK_CLIST(clist), 5, GTK_JUSTIFY_CENTER);
588         gtk_clist_set_column_justification(GTK_CLIST(clist), 6, GTK_JUSTIFY_CENTER);
589         gtk_clist_set_column_justification(GTK_CLIST(clist), 7, GTK_JUSTIFY_LEFT);
590         gtk_clist_set_column_justification(GTK_CLIST(clist), 8, GTK_JUSTIFY_LEFT);
591
592         gtk_clist_column_titles_show (GTK_CLIST (clist));
593
594         gtk_clist_set_compare_func(GTK_CLIST(clist), voip_calls_sort_column);
595         gtk_clist_set_sort_column(GTK_CLIST(clist), 0);
596         gtk_clist_set_sort_type(GTK_CLIST(clist), GTK_SORT_ASCENDING);
597
598         gtk_widget_show(voip_calls_dlg_w);
599
600         /* sort by column feature */
601         col_arrows = (column_arrows *) g_malloc(sizeof(column_arrows) * NUM_COLS);
602
603         for (i=0; i<NUM_COLS; i++) {
604                 col_arrows[i].table = gtk_table_new(2, 2, FALSE);
605                 gtk_table_set_col_spacings(GTK_TABLE(col_arrows[i].table), 5);
606                 column_lb = gtk_label_new(titles[i]);
607                 gtk_table_attach(GTK_TABLE(col_arrows[i].table), column_lb, 0, 1, 0, 2, GTK_SHRINK, GTK_SHRINK, 0, 0);
608                 gtk_widget_show(column_lb);
609
610                 col_arrows[i].ascend_pm = xpm_to_widget(clist_ascend_xpm);
611                 gtk_table_attach(GTK_TABLE(col_arrows[i].table), col_arrows[i].ascend_pm, 1, 2, 1, 2, GTK_SHRINK, GTK_SHRINK, 0, 0);
612                 col_arrows[i].descend_pm = xpm_to_widget(clist_descend_xpm);
613                 gtk_table_attach(GTK_TABLE(col_arrows[i].table), col_arrows[i].descend_pm, 1, 2, 0, 1, GTK_SHRINK, GTK_SHRINK, 0, 0);
614                 /* make start time be the default sort order */
615                 if (i == 0) {
616                         gtk_widget_show(col_arrows[i].ascend_pm);
617                 }
618                 gtk_clist_set_column_widget(GTK_CLIST(clist), i, col_arrows[i].table);
619                 gtk_widget_show(col_arrows[i].table);
620         }
621
622         SIGNAL_CONNECT(clist, "click-column", voip_calls_click_column_cb, col_arrows);
623
624         label_fwd = gtk_label_new (FWD_LABEL_TEXT);
625         gtk_box_pack_start (GTK_BOX (main_vb), label_fwd, FALSE, FALSE, 0);
626
627         status_label = gtk_label_new ("Total: Calls: 0   Start packets: 0   Completed calls: 0   Rejected calls: 0");
628         gtk_box_pack_start (GTK_BOX (main_vb), status_label, FALSE, FALSE, 8);
629
630         /* button row */
631         hbuttonbox = gtk_hbutton_box_new ();
632         gtk_box_pack_start (GTK_BOX (main_vb), hbuttonbox, FALSE, FALSE, 0);
633         gtk_button_box_set_layout (GTK_BUTTON_BOX (hbuttonbox), GTK_BUTTONBOX_SPREAD);
634         gtk_button_box_set_spacing (GTK_BUTTON_BOX (hbuttonbox), 30);
635
636         /*bt_unselect = gtk_button_new_with_label ("Unselect");
637         gtk_container_add (GTK_CONTAINER (hbuttonbox), bt_unselect);
638         gtk_tooltips_set_tip (tooltips, bt_unselect, "Unselect this conversation", NULL);*/
639
640         bt_filter = gtk_button_new_with_label ("Prepare Filter");
641         gtk_container_add (GTK_CONTAINER (hbuttonbox), bt_filter);
642         gtk_tooltips_set_tip (tooltips, bt_filter, "Prepare a display filter of the selected conversation", NULL);
643
644         bt_graph = gtk_button_new_with_label("Graph");
645         gtk_container_add(GTK_CONTAINER(hbuttonbox), bt_graph);
646         gtk_widget_show(bt_graph);
647         SIGNAL_CONNECT(bt_graph, "clicked", on_graph_bt_clicked, NULL);
648         gtk_tooltips_set_tip (tooltips, bt_graph, "Show a flow graph of the selected calls.", NULL);
649
650         bt_close = BUTTON_NEW_FROM_STOCK(GTK_STOCK_CLOSE);
651         gtk_container_add (GTK_CONTAINER (hbuttonbox), bt_close);
652         GTK_WIDGET_SET_FLAGS(bt_close, GTK_CAN_DEFAULT);
653         gtk_tooltips_set_tip (tooltips, bt_close, "Close this dialog", NULL);
654
655         SIGNAL_CONNECT(clist, "select_row", voip_calls_on_select_row, NULL);
656         /*SIGNAL_CONNECT(bt_unselect, "clicked", voip_calls_on_unselect, NULL);*/
657         SIGNAL_CONNECT(bt_filter, "clicked", voip_calls_on_filter, NULL);
658
659         window_set_cancel_button(voip_calls_dlg_w, bt_close, window_cancel_button_cb);
660
661         SIGNAL_CONNECT(voip_calls_dlg_w, "delete_event", window_delete_event_cb, NULL);
662         SIGNAL_CONNECT(voip_calls_dlg_w, "destroy", voip_calls_on_destroy, NULL);
663
664         gtk_widget_show_all(voip_calls_dlg_w);
665         window_present(voip_calls_dlg_w);
666
667         voip_calls_on_unselect(NULL, NULL);
668
669         voip_calls_dlg = voip_calls_dlg_w;
670 }
671
672
673 /****************************************************************************/
674 /* PUBLIC                                                                   */
675 /****************************************************************************/
676
677 /****************************************************************************/
678 /* update the contents of the dialog box clist */
679 /* list: pointer to list of voip_calls_info_t* */
680
681 void voip_calls_dlg_update(GList *list)
682 {
683         gchar label_text[256];
684         if (voip_calls_dlg != NULL) {
685                 calls_nb = 0;
686                 calls_ns = 0;
687
688         g_snprintf(label_text, 256,
689                 "Total: Calls: %d   Start packets: %d   Completed calls: %d   Rejected calls: %d",
690                 g_list_length(voip_calls_get_info()->strinfo_list),
691         voip_calls_get_info()->start_packets, 
692                 voip_calls_get_info()->completed_calls,
693                 voip_calls_get_info()->rejected_calls);
694         gtk_label_set(GTK_LABEL(status_label), label_text);
695
696                 gtk_clist_freeze(GTK_CLIST(clist));
697                 gtk_clist_clear(GTK_CLIST(clist));
698                 list = g_list_first(list);
699                 while (list)
700                 {
701                         add_to_clist((voip_calls_info_t*)(list->data));
702                         list = g_list_next(list);
703                 }
704                 gtk_clist_thaw(GTK_CLIST(clist));
705
706                 g_snprintf(label_text, 256,
707                         "Detected %d VoIP %s. Selected %d %s.",
708                         calls_nb, 
709                         plurality(calls_nb, "Call", "Calls"),
710                         calls_ns,
711                         plurality(calls_ns, "Call", "Calls"));
712                 gtk_label_set(GTK_LABEL(top_label), label_text);
713         }
714
715         last_list = list;
716 }
717
718
719 /****************************************************************************/
720 /* draw function for tap listeners to keep the window up to date */
721 void voip_calls_dlg_draw(void *ptr _U_)
722 {
723         if (voip_calls_get_info()->redraw) {
724                 voip_calls_dlg_update(voip_calls_get_info()->strinfo_list);
725                 voip_calls_get_info()->redraw = FALSE;
726         }
727 }
728
729 /* reset function for tap listeners to clear window, if necessary */
730 void voip_calls_dlg_reset(void *ptr _U_)
731 {
732         /* Clean up memory used by calls tap */
733         voip_calls_reset((voip_calls_tapinfo_t*) voip_calls_get_info());
734
735         /* close the graph window if open */
736         if (graph_analysis_data->dlg.window != NULL) {
737                 window_cancel_button_cb(NULL, graph_analysis_data->dlg.window);
738                 graph_analysis_data->dlg.window = NULL;
739         }
740 }
741
742 /* init function for tap */
743 static void
744 voip_calls_init_tap(const char *dummy _U_)
745 {
746         gint c;
747         gchar *data[NUM_COLS];
748         gchar field[NUM_COLS][50];
749
750         if (graph_analysis_data == NULL) {
751                 graph_analysis_data_init();
752                 /* init the Graph Analysys */
753                 graph_analysis_data = graph_analysis_init();
754                 graph_analysis_data->graph_info = voip_calls_get_info()->graph_analysis;
755         }
756
757         /* Clean up memory used by calls tap */
758         voip_calls_reset((voip_calls_tapinfo_t*) voip_calls_get_info());
759
760         /* Register the tap listener */
761         sip_calls_init_tap();
762         mtp3_calls_init_tap();
763         isup_calls_init_tap();
764         h225_calls_init_tap();
765         h245dg_calls_init_tap();
766         q931_calls_init_tap();
767         sdp_calls_init_tap();
768         rtp_init_tap();
769         rtp_event_init_tap();
770         /* We don't register this tap, if we don't have the mgcp plugin loaded.*/
771         if (find_tap_id("mgcp")) {
772                 mgcp_calls_init_tap();
773         }
774         actrace_calls_init_tap();
775
776         /* create dialog box if necessary */
777         if (voip_calls_dlg == NULL) {
778                 voip_calls_dlg_create();
779         } else {
780                 /* There's already a dialog box; reactivate it. */
781                 reactivate_window(voip_calls_dlg);
782         }
783
784         voip_calls_get_info()->redraw = TRUE;
785         voip_calls_dlg_draw(NULL);
786         voip_calls_get_info()->redraw = TRUE;
787         for (c=0;c<NUM_COLS;c++){
788                 data[c]=&field[c][0];
789                 field[c][0] = '\0';
790         }
791         g_snprintf(field[3], 50, "Please wait...");
792         gtk_clist_append(GTK_CLIST(clist), data);
793         
794         /* Scan for VoIP calls calls (redissect all packets) */
795         cf_retap_packets(&cfile);
796
797         /* Tap listener will be removed and cleaned up in voip_calls_on_destroy */
798 }
799
800
801 /****************************************************************************/
802 /* entry point when called via the GTK menu */
803 void voip_calls_launch(GtkWidget *w _U_, gpointer data _U_)
804 {
805         voip_calls_init_tap("");
806 }
807
808 /****************************************************************************/
809 void
810 register_tap_listener_voip_calls_dlg(void)
811 {
812         register_tap_listener_cmd_arg("voip,calls",voip_calls_init_tap);
813         register_tap_menu_item("VoIP Calls...", REGISTER_TAP_GROUP_TELEPHONY,
814             voip_calls_launch, NULL, NULL, NULL);
815             
816 }