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