999691a7eab59f9e4b99fde2946080870dcce0af
[obnox/wireshark/wip.git] / gtk / voip_calls_dlg.c
1 /* voip_calls_dlg.c
2  * VoIP calls summary addition for Wireshark
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  * Wireshark - Network traffic analyzer
18  * By Gerald Combs <gerald@wireshark.org>
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 #include <string.h>
40 #include <ctype.h>
41
42 #include "gtk/gtk.h"
43
44 #include <epan/epan.h>
45 #include <epan/packet.h>
46 #include "epan/filesystem.h"
47 #include <epan/tap.h>
48 #include <epan/stat_cmd_args.h>
49 #include <epan/to_str.h>
50 #include <epan/address.h>
51 #include <epan/addr_resolv.h>
52 #include <epan/dissectors/packet-h248.h>
53
54 #include "../register.h"
55 #include "../globals.h"
56 #include "../stat_menu.h"
57
58 #include "gtk/graph_analysis.h"
59 #include "gtk/voip_calls_dlg.h"
60 #include "gtk/voip_calls.h"
61 #include "gtk/gui_stat_menu.h"
62 #include "gtk/dlg_utils.h"
63 #include "gtk/gui_utils.h"
64 #include "gtk/gtkglobals.h"
65
66 #include "image/clist_ascend.xpm"
67 #include "image/clist_descend.xpm"
68 #include "simple_dialog.h"
69
70 #ifdef HAVE_LIBPORTAUDIO
71 #include "gtk/rtp_analysis.h"
72 #include "gtk/rtp_player.h"
73 #endif /* HAVE_LIBPORTAUDIO */
74
75
76 /****************************************************************************/
77 /* pointer to the one and only dialog window */
78 static GtkWidget *voip_calls_dlg = NULL;
79
80 static GtkWidget *clist = NULL;
81 static GtkWidget *top_label = NULL;
82 static GtkWidget *status_label = NULL;
83
84 /*static GtkWidet *bt_unselect = NULL;*/
85 static GtkWidget *bt_filter = NULL;
86 static GtkWidget *bt_graph = NULL;
87 #ifdef HAVE_LIBPORTAUDIO
88 static GtkWidget *bt_player = NULL;
89 #endif /* HAVE_LIBPORTAUDIO */
90
91 static voip_calls_info_t* selected_call_fwd = NULL;  /* current selection */
92 static GList *last_list = NULL;
93
94 static guint32 calls_nb = 0;     /* number of displayed calls */
95 static guint32 calls_ns = 0;     /* number of selected calls */
96
97 static graph_analysis_data_t *graph_analysis_data = NULL;
98
99 #define NUM_COLS 9
100 #define CALL_COL_START_TIME             0
101 #define CALL_COL_STOP_TIME              1
102 #define CALL_COL_INITIAL_SPEAKER        2
103 #define CALL_COL_FROM                   3
104 #define CALL_COL_TO                     4
105 #define CALL_COL_PROTOCOL               5
106 #define CALL_COL_PACKETS                6
107 #define CALL_COL_STATE                  7
108 #define CALL_COL_COMMENTS               8
109 static const GdkColor COLOR_SELECT = {0, 0x00ff, 0x80ff, 0x80ff};
110 static const GdkColor COLOR_DEFAULT = {0, 0xffff, 0xffff, 0xffff};
111 static const GdkColor COLOR_FOREGROUND = {0, 0x0000, 0x0000, 0x0000};
112
113 /****************************************************************************/
114 /* append a line to clist */
115 static void add_to_clist(voip_calls_info_t* strinfo)
116 {
117         gchar label_text[256];
118         gint added_row;
119         gchar *data[NUM_COLS];
120         gchar field[NUM_COLS][50];
121         gint c;
122         isup_calls_info_t *tmp_isupinfo;
123         h323_calls_info_t *tmp_h323info;
124         gboolean tmp_bool = FALSE;
125         GdkColor bg_color = COLOR_SELECT;
126         GdkColor fg_color = COLOR_FOREGROUND;
127         for (c=0;c<NUM_COLS;c++){
128                 data[c]=&field[c][0];
129         }
130
131 /*      strinfo->selected = FALSE;*/
132
133         g_snprintf(field[CALL_COL_START_TIME], 15, "%i.%03i", strinfo->start_sec, strinfo->start_usec/1000);
134         g_snprintf(field[CALL_COL_STOP_TIME], 15, "%i.%03i", strinfo->stop_sec, strinfo->stop_usec/1000);
135 /*      xxx display_signed_time(data[0], sizeof(field[CALL_COL_START_TIME]), strinfo->start_sec, strinfo->start_usec, USECS); */
136 /*      display_signed_time(data[1], sizeof(field[CALL_COL_STOP_TIME]), strinfo->stop_sec, strinfo->stop_usec, USECS); */
137         g_snprintf(field[CALL_COL_INITIAL_SPEAKER], 30, "%s", get_addr_name(&(strinfo->initial_speaker)));
138         g_snprintf(field[CALL_COL_FROM], 50, "%s", strinfo->from_identity);
139         g_snprintf(field[CALL_COL_TO], 50, "%s", strinfo->to_identity);
140         g_snprintf(field[CALL_COL_PROTOCOL], 15, "%s", ((strinfo->protocol==VOIP_COMMON)&&strinfo->protocol_name)?strinfo->protocol_name:voip_protocol_name[strinfo->protocol]);
141         g_snprintf(field[CALL_COL_PACKETS], 15, "%u", strinfo->npackets);
142         g_snprintf(field[CALL_COL_STATE], 15, "%s", voip_call_state_name[strinfo->call_state]);
143
144         /* Add comments based on the protocol */
145         switch(strinfo->protocol){
146                 case VOIP_ISUP:
147                         tmp_isupinfo = strinfo->prot_info;
148                         g_snprintf(field[CALL_COL_COMMENTS],30, "%i-%i -> %i-%i", tmp_isupinfo->ni, tmp_isupinfo->opc,
149                                 tmp_isupinfo->ni, tmp_isupinfo->dpc);
150                         break;
151                 case VOIP_H323:
152                         tmp_h323info = strinfo->prot_info;
153                         if (strinfo->call_state == VOIP_CALL_SETUP) 
154                                 tmp_bool = tmp_h323info->is_faststart_Setup;
155                         else
156                                 if ((tmp_h323info->is_faststart_Setup == TRUE) && (tmp_h323info->is_faststart_Proc == TRUE)) tmp_bool = TRUE; 
157                         g_snprintf(field[CALL_COL_COMMENTS],35, "Tunneling: %s  Fast Start: %s", (tmp_h323info->is_h245Tunneling==TRUE?"ON":"OFF"), 
158                                 (tmp_bool==TRUE?"ON":"OFF"));
159                         break;
160                 case VOIP_COMMON:
161                         field[CALL_COL_COMMENTS][0]='\0';
162                         if (strinfo->call_comment)
163                                 g_snprintf(field[CALL_COL_COMMENTS],30, "%s", strinfo->call_comment);
164                         break;
165                 default:
166                         field[CALL_COL_COMMENTS][0]='\0';
167         }
168
169         
170         added_row = gtk_clist_append(GTK_CLIST(clist), data);
171
172         /* set the background color if selected */
173         if (strinfo->selected) { 
174                 calls_ns++;
175                 gtk_clist_set_background(GTK_CLIST(clist), added_row, &bg_color);
176                 gtk_clist_set_foreground(GTK_CLIST(clist), added_row, &fg_color);
177         }
178
179         /* set data pointer of last row to point to user data for that row */
180         gtk_clist_set_row_data(GTK_CLIST(clist), added_row, strinfo);
181
182         /* Update the top label with the number of detected calls */
183         calls_nb++;
184         g_snprintf(label_text, 256,
185                 "Detected %d VoIP %s. Selected %d %s.",
186                 calls_nb, 
187                 plurality(calls_nb, "Call", "Calls"),
188                 calls_ns,
189                 plurality(calls_ns, "Call", "Calls"));
190         gtk_label_set(GTK_LABEL(top_label), label_text);
191
192         /* Update the status label with the number of total messages */
193         g_snprintf(label_text, 256,
194                 "Total: Calls: %d   Start packets: %d   Completed calls: %d   Rejected calls: %d",
195                         g_list_length(voip_calls_get_info()->callsinfo_list),
196                         voip_calls_get_info()->start_packets, 
197                         voip_calls_get_info()->completed_calls,
198                         voip_calls_get_info()->rejected_calls);
199         gtk_label_set(GTK_LABEL(status_label), label_text);
200 }
201
202
203 static void voip_calls_remove_tap_listener(void)
204 {
205         /* Remove the calls tap listener */
206         remove_tap_listener_sip_calls();
207         remove_tap_listener_isup_calls();
208         remove_tap_listener_mtp3_calls();
209         remove_tap_listener_h225_calls();
210         remove_tap_listener_h245dg_calls();
211         remove_tap_listener_q931_calls();
212         remove_tap_listener_h248_calls();
213         remove_tap_listener_sccp_calls();
214         remove_tap_listener_sdp_calls();
215         remove_tap_listener_rtp();
216         if (find_tap_id("unistim")) {
217                 remove_tap_listener_unistim_calls();
218         }
219         if (find_tap_id("voip")) {
220                 remove_tap_listener_voip_calls();
221         }
222         remove_tap_listener_rtp_event();
223         if (find_tap_id("mgcp")) {
224                 remove_tap_listener_mgcp_calls();
225         }
226         remove_tap_listener_actrace_calls();
227         remove_tap_listener_t38();
228 }
229
230 /****************************************************************************/
231 /* CALLBACKS                                                                */
232 /****************************************************************************/
233 static void
234 voip_calls_on_destroy                      (GtkObject       *object _U_,
235                                         gpointer         user_data _U_)
236 {
237         /* remove_tap_listeners */
238         voip_calls_remove_tap_listener();
239
240         /* Clean up memory used by calls tap */
241         voip_calls_dlg_reset(NULL);
242
243         /* Note that we no longer have a "VoIP Calls" dialog box. */
244         voip_calls_dlg = NULL;
245
246         graph_analysis_data = NULL;
247 }
248
249
250 /****************************************************************************/
251 static void
252 voip_calls_on_unselect                  (GtkButton       *button _U_,
253                                         gpointer         user_data _U_)
254 {
255         selected_call_fwd = NULL;
256         gtk_clist_unselect_all(GTK_CLIST(clist));
257 /*      gtk_label_set_text(GTK_LABEL(label_fwd), FWD_LABEL_TEXT);
258 */
259         /*gtk_widget_set_sensitive(bt_unselect, FALSE);*/
260         gtk_widget_set_sensitive(bt_filter, FALSE);
261         gtk_widget_set_sensitive(bt_graph, FALSE);
262 #ifdef HAVE_LIBPORTAUDIO
263         gtk_widget_set_sensitive(bt_player, FALSE);
264 #endif /* HAVE_LIBPORTAUDIO */
265 }
266
267
268 /****************************************************************************/
269 static void
270 voip_calls_on_filter                    (GtkButton       *button _U_,
271                                         gpointer         user_data _U_)
272 {
273         const gchar *filter_string;
274         gchar c;
275         GString *filter_string_fwd;
276         const gchar *filter_prepend;
277         gboolean isFirst = TRUE;
278         GList* list;
279         guint filter_length = 0;
280         guint max_filter_length = 2048;
281         sip_calls_info_t *tmp_sipinfo;
282         isup_calls_info_t *tmp_isupinfo;
283         h323_calls_info_t *tmp_h323info;
284         h245_address_t *h245_add = NULL;
285
286         graph_analysis_item_t *gai;
287
288         if (selected_call_fwd==NULL)
289                 return;
290
291         filter_string=gtk_entry_get_text(GTK_ENTRY(main_display_filter_widget));
292         filter_length = strlen(filter_string);
293         filter_prepend = "";
294         while ((c = *filter_string++) != '\0') {
295                 if (!isspace((guchar)c)) {
296                         /* The filter string isn't blank, so there's already
297                            an expression; we OR in the new expression */
298                         filter_prepend = " or ";
299                         break;
300                 }
301         }
302                 
303         filter_string_fwd = g_string_new(filter_prepend);
304         
305         /* look in the Graph and get all the frame_num for this call */
306         g_string_append_printf(filter_string_fwd, " (");
307         list = g_list_first(voip_calls_get_info()->graph_analysis->list);
308         while (list)
309         {
310                 gai = list->data;
311                 if (gai->conv_num == selected_call_fwd->call_num){
312                         g_string_append_printf(filter_string_fwd,"%sframe.number == %d", isFirst?"":" or ", gai->frame_num );
313                         isFirst = FALSE;
314                 }               
315                 list = g_list_next (list);
316         }
317         g_string_append_printf(filter_string_fwd, ") ");
318         filter_length = filter_length + filter_string_fwd->len;
319
320         if (filter_length < max_filter_length){
321                 gtk_entry_append_text(GTK_ENTRY(main_display_filter_widget), filter_string_fwd->str);
322         } else {
323                 g_string_free(filter_string_fwd, TRUE);
324                 filter_string_fwd = g_string_new(filter_prepend);
325
326                 switch(selected_call_fwd->protocol){
327                         case VOIP_SIP:
328                                 tmp_sipinfo = selected_call_fwd->prot_info;
329                                 g_string_append_printf(filter_string_fwd,
330                                    "(sip.Call-ID == \"%s\") ",
331                                    tmp_sipinfo->call_identifier 
332                                    );
333                                 gtk_entry_append_text(GTK_ENTRY(main_display_filter_widget), filter_string_fwd->str);
334                                 break;
335                         case VOIP_ISUP:
336                                 tmp_isupinfo = selected_call_fwd->prot_info;
337                                 g_string_append_printf(filter_string_fwd,
338                                    "(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))) ",
339                                    tmp_isupinfo->cic,selected_call_fwd->first_frame_num,
340                                    selected_call_fwd->last_frame_num, 
341                                    tmp_isupinfo->ni, tmp_isupinfo->dpc, tmp_isupinfo->opc, 
342                                    tmp_isupinfo->opc, tmp_isupinfo->dpc
343                                    );
344                                 gtk_entry_append_text(GTK_ENTRY(main_display_filter_widget), filter_string_fwd->str);
345                                 break;
346                         case VOIP_H323:
347                                 tmp_h323info = selected_call_fwd->prot_info;
348                                 g_string_append_printf(filter_string_fwd,
349                                    "((h225.guid == %s || q931.call_ref == %x:%x || q931.call_ref == %x:%x) ",
350                                    guid_to_str(&tmp_h323info->guid[0]),
351                                    (guint8)(tmp_h323info->q931_crv & 0xff),
352                                    (guint8)((tmp_h323info->q931_crv & 0xff00)>>8),
353                                    (guint8)(tmp_h323info->q931_crv2 & 0xff),
354                                    (guint8)((tmp_h323info->q931_crv2 & 0xff00)>>8));
355                                 list = g_list_first(tmp_h323info->h245_list);
356                                 while (list)
357                                 {
358                                         h245_add=list->data;
359                                         g_string_append_printf(filter_string_fwd,
360                                                 " || (ip.addr == %s && tcp.port == %d && h245) ", 
361                                                 ip_to_str((guint8 *)&(h245_add->h245_address)), h245_add->h245_port);
362                                 list = g_list_next(list);
363                                 }
364                                 g_string_append_printf(filter_string_fwd, ") ");
365                                 gtk_entry_append_text(GTK_ENTRY(main_display_filter_widget), filter_string_fwd->str);
366                                 break;
367                         case TEL_H248: {
368                                 const gcp_ctx_t* ctx = selected_call_fwd->prot_info;
369                                 gtk_entry_append_text(GTK_ENTRY(main_display_filter_widget), ep_strdup_printf("h248.ctx == 0x%x", ctx->id ));
370                                 break;
371                         }
372                         case TEL_SCCP:
373                         case VOIP_MGCP:
374                         case VOIP_AC_ISDN:
375                         case VOIP_AC_CAS:
376                         case MEDIA_T38:
377                         case TEL_BSSMAP:
378                         case TEL_RANAP:
379                         case VOIP_UNISTIM:
380                         case VOIP_COMMON:
381                                 /* XXX - not supported */
382                                 break;
383                 }
384                 
385         }
386         
387         g_string_free(filter_string_fwd, TRUE);
388 }
389
390
391 /****************************************************************************/
392 static void
393 voip_calls_on_select_all                    (GtkButton       *button _U_,
394                                         gpointer         user_data _U_)
395 {
396         gtk_clist_select_all(GTK_CLIST(clist));
397 }
398
399 /****************************************************************************/
400 static void
401 on_graph_bt_clicked                    (GtkButton       *button _U_,
402                                         gpointer         user_data _U_)
403 {
404         graph_analysis_item_t *gai;
405         GList* list;
406         GList* list2;
407         voip_calls_info_t *tmp_listinfo;
408
409         /* reset the "display" parameter in graph analysis */
410         list2 = g_list_first(voip_calls_get_info()->graph_analysis->list);
411         while (list2){
412                 gai = list2->data;
413                 gai->display = FALSE;
414                 list2 = g_list_next(list2);
415         }
416
417
418         /* set the display for selected calls */
419         list = g_list_first(voip_calls_get_info()->callsinfo_list);
420         while (list){
421                 tmp_listinfo=list->data;
422                 if (tmp_listinfo->selected){
423                         list2 = g_list_first(voip_calls_get_info()->graph_analysis->list);
424                         while (list2){
425                                 gai = list2->data;
426                                 if (gai->conv_num == tmp_listinfo->call_num){
427                                         gai->display = TRUE;
428                                 }
429                                 list2 = g_list_next(list2);
430                         }
431                 }
432                 list = g_list_next(list);
433         }
434
435         /* create or refresh the graph windows */
436         if (graph_analysis_data->dlg.window == NULL)    /* create the window */
437                 graph_analysis_create(graph_analysis_data);
438         else
439                 graph_analysis_update(graph_analysis_data);             /* refresh it */
440 }
441
442 #ifdef HAVE_LIBPORTAUDIO
443 /****************************************************************************/
444 static void
445 on_player_bt_clicked                    (GtkButton       *button _U_,
446                                         gpointer         user_data _U_)
447 {
448         rtp_player_init(voip_calls_get_info());
449 }
450 #endif /* HAVE_LIBPORTAUDIO */
451
452 /****************************************************************************/
453 /* when the user selects a row in the calls list */
454 static void
455 voip_calls_on_select_row(GtkCList *clist,
456                                             gint row _U_,
457                                             gint column _U_,
458                                             GdkEventButton *event _U_,
459                                             gpointer user_data _U_)
460 {
461 /*      GdkColor color = COLOR_DEFAULT;*/
462         gchar label_text[80];
463         GList* list;
464         voip_calls_info_t *listinfo;
465         
466         selected_call_fwd = gtk_clist_get_row_data(GTK_CLIST(clist), row);
467
468         if (selected_call_fwd==NULL)
469                 return;
470
471         selected_call_fwd->selected=TRUE;
472
473         /* count the selected calls */
474         calls_ns = 0;
475         list = g_list_first(voip_calls_get_info()->callsinfo_list);
476         while (list){
477                 listinfo=list->data;
478                 if (listinfo->selected){
479                         calls_ns++;     
480                 }
481                 list = g_list_next(list);
482         }
483
484         g_snprintf(label_text, 80,
485                 "Detected %d VoIP %s. Selected %d %s.",
486                 calls_nb, 
487             plurality(calls_nb, "Call", "Calls"),
488                         calls_ns,
489                         plurality(calls_ns, "Call", "Calls"));
490         gtk_label_set(GTK_LABEL(top_label), label_text);
491
492
493         if      (calls_ns > 0) {
494                 gtk_widget_set_sensitive(bt_filter, TRUE);
495                 gtk_widget_set_sensitive(bt_graph, TRUE);
496 #ifdef HAVE_LIBPORTAUDIO
497                 gtk_widget_set_sensitive(bt_player, TRUE);
498 #endif /* HAVE_LIBPORTAUDIO */
499         } else {
500                 gtk_widget_set_sensitive(bt_filter, FALSE);
501                 gtk_widget_set_sensitive(bt_graph, FALSE);
502 #ifdef HAVE_LIBPORTAUDIO
503                 gtk_widget_set_sensitive(bt_player, FALSE);
504 #endif /* HAVE_LIBPORTAUDIO */
505         }
506
507         /* TODO: activate other buttons when implemented */
508 }
509
510
511 /****************************************************************************/
512 /* when the user selects a row in the calls list */
513 static void
514 voip_calls_on_unselect_row(GtkCList *clist,
515                                             gint row _U_,
516                                             gint column _U_,
517                                             GdkEventButton *event _U_,
518                                             gpointer user_data _U_)
519 {
520         gchar label_text[80];
521         GList* list;
522         voip_calls_info_t *listinfo;
523         
524         selected_call_fwd = gtk_clist_get_row_data(GTK_CLIST(clist), row);
525
526         if (selected_call_fwd==NULL)
527                 return;
528
529         selected_call_fwd->selected=FALSE;
530
531         /* count the selected calls */
532         calls_ns = 0;
533         list = g_list_first(voip_calls_get_info()->callsinfo_list);
534         while (list){
535                 listinfo=list->data;
536                 if (listinfo->selected){
537                         calls_ns++;     
538                 }
539                 list = g_list_next(list);
540         }
541
542         g_snprintf(label_text, 80,
543                 "Detected %d VoIP %s. Selected %d %s.",
544                 calls_nb, 
545             plurality(calls_nb, "Call", "Calls"),
546                         calls_ns,
547                         plurality(calls_ns, "Call", "Calls"));
548         gtk_label_set(GTK_LABEL(top_label), label_text);
549
550         if      (calls_ns > 0) {
551                 gtk_widget_set_sensitive(bt_filter, TRUE);
552                 gtk_widget_set_sensitive(bt_graph, TRUE);
553 #ifdef HAVE_LIBPORTAUDIO
554                 gtk_widget_set_sensitive(bt_player, TRUE);
555 #endif /* HAVE_LIBPORTAUDIO */
556         } else {
557                 gtk_widget_set_sensitive(bt_filter, FALSE);
558                 gtk_widget_set_sensitive(bt_graph, FALSE);
559 #ifdef HAVE_LIBPORTAUDIO
560                 gtk_widget_set_sensitive(bt_player, FALSE);
561 #endif /* HAVE_LIBPORTAUDIO */
562         }
563
564         /* TODO: activate other buttons when implemented */
565 }
566
567
568
569
570
571
572
573
574 /****************************************************************************/
575
576 typedef struct column_arrows {
577         GtkWidget *table;
578         GtkWidget *ascend_pm;
579         GtkWidget *descend_pm;
580 } column_arrows;
581
582
583 /****************************************************************************/
584 static void
585 voip_calls_click_column_cb(GtkCList *clist, gint column, gpointer data)
586 {
587         column_arrows *col_arrows = (column_arrows *) data;
588         int i;
589
590         gtk_clist_freeze(clist);
591
592         for (i=0; i<NUM_COLS; i++) {
593                 gtk_widget_hide(col_arrows[i].ascend_pm);
594                 gtk_widget_hide(col_arrows[i].descend_pm);
595         }
596
597         if (column == clist->sort_column) {
598                 if (clist->sort_type == GTK_SORT_ASCENDING) {
599                         clist->sort_type = GTK_SORT_DESCENDING;
600                         gtk_widget_show(col_arrows[column].descend_pm);
601                 } else {
602                         clist->sort_type = GTK_SORT_ASCENDING;
603                         gtk_widget_show(col_arrows[column].ascend_pm);
604                 }
605         } else {
606                 clist->sort_type = GTK_SORT_ASCENDING;
607                 gtk_widget_show(col_arrows[column].ascend_pm);
608                 gtk_clist_set_sort_column(clist, column);
609         }
610         gtk_clist_thaw(clist);
611
612         gtk_clist_sort(clist);
613 }
614
615
616 /****************************************************************************/
617 static gint
618 voip_calls_sort_column(GtkCList *clist, gconstpointer ptr1, gconstpointer ptr2)
619 {
620         char *text1 = NULL;
621         char *text2 = NULL;
622         guint i1, i2, i3, i4;
623
624         const GtkCListRow *row1 = (const GtkCListRow *) ptr1;
625         const GtkCListRow *row2 = (const GtkCListRow *) ptr2;
626
627         text1 = GTK_CELL_TEXT (row1->cell[clist->sort_column])->text;
628         text2 = GTK_CELL_TEXT (row2->cell[clist->sort_column])->text;
629
630         switch(clist->sort_column){
631         case CALL_COL_START_TIME:
632         case CALL_COL_STOP_TIME:
633                 if ((sscanf(text1, "%u.%u", &i1, &i2) != 2) ||
634                         (sscanf(text2, "%u.%u", &i3, &i4) != 2) ){
635                                 return 0;
636                         }
637                 if (i1>i3)
638                         return 1;
639                 if (i1<i3)
640                         return -1;
641                 return (i3-i4);
642         case CALL_COL_INITIAL_SPEAKER:
643         case CALL_COL_FROM:
644         case CALL_COL_TO:
645         case CALL_COL_PROTOCOL:
646         case CALL_COL_STATE:
647         case CALL_COL_COMMENTS:
648                 return strcmp (text1, text2);
649         case CALL_COL_PACKETS:
650                 i1=atoi(text1);
651                 i2=atoi(text2);
652                 return i1-i2;
653         }
654         g_assert_not_reached();
655         return 0;
656 }
657
658
659 /****************************************************************************/
660 /* INTERFACE                                                                */
661 /****************************************************************************/
662
663 static void voip_calls_dlg_create (void)
664 {
665         GtkWidget *voip_calls_dlg_w;
666         GtkWidget *main_vb;
667         GtkWidget *scrolledwindow;
668         GtkWidget *hbuttonbox;
669         GtkWidget *bt_close;
670         GtkWidget *bt_select_all;
671         GtkTooltips *tooltips = gtk_tooltips_new();
672         const gchar *title_name_ptr;
673         gchar   *win_name;
674
675         const gchar *titles[NUM_COLS] =  {"Start Time", "Stop Time", "Initial Speaker", "From",  "To", "Protocol", "Packets", "State", "Comments"};
676         column_arrows *col_arrows;
677         GtkWidget *column_lb;
678         int i;
679
680         title_name_ptr = cf_get_display_name(&cfile);
681         win_name = g_strdup_printf("%s - VoIP Calls", title_name_ptr);
682         voip_calls_dlg_w=window_new(GTK_WINDOW_TOPLEVEL, win_name);
683
684         gtk_window_set_default_size(GTK_WINDOW(voip_calls_dlg_w), 840, 350);
685
686         main_vb = gtk_vbox_new (FALSE, 0);
687         gtk_container_add(GTK_CONTAINER(voip_calls_dlg_w), main_vb);
688         gtk_container_set_border_width (GTK_CONTAINER (main_vb), 12);
689
690         top_label = gtk_label_new ("Detected 0 VoIP Calls. Selected 0 Calls.");
691         gtk_box_pack_start (GTK_BOX (main_vb), top_label, FALSE, FALSE, 8);
692
693         scrolledwindow = scrolled_window_new (NULL, NULL);
694         gtk_box_pack_start (GTK_BOX (main_vb), scrolledwindow, TRUE, TRUE, 0);
695
696         clist = gtk_clist_new (NUM_COLS);
697         gtk_clist_set_selection_mode(GTK_CLIST (clist), GTK_SELECTION_MULTIPLE);
698
699         gtk_container_add (GTK_CONTAINER (scrolledwindow), clist);
700
701         gtk_clist_set_column_width (GTK_CLIST (clist), CALL_COL_START_TIME, 60);
702         gtk_clist_set_column_width (GTK_CLIST (clist), CALL_COL_STOP_TIME, 60);
703         gtk_clist_set_column_width (GTK_CLIST (clist), CALL_COL_INITIAL_SPEAKER, 80);
704         gtk_clist_set_column_width (GTK_CLIST (clist), CALL_COL_FROM, 130);
705         gtk_clist_set_column_width (GTK_CLIST (clist), CALL_COL_TO, 130);
706         gtk_clist_set_column_width (GTK_CLIST (clist), CALL_COL_PROTOCOL, 50);
707         gtk_clist_set_column_width (GTK_CLIST (clist), CALL_COL_PACKETS, 45);
708         gtk_clist_set_column_width (GTK_CLIST (clist), CALL_COL_STATE, 60);
709         gtk_clist_set_column_width (GTK_CLIST (clist), CALL_COL_COMMENTS, 100);
710
711         gtk_clist_set_column_justification(GTK_CLIST(clist), CALL_COL_START_TIME, GTK_JUSTIFY_LEFT);
712         gtk_clist_set_column_justification(GTK_CLIST(clist), CALL_COL_STOP_TIME, GTK_JUSTIFY_LEFT);
713         gtk_clist_set_column_justification(GTK_CLIST(clist), CALL_COL_INITIAL_SPEAKER, GTK_JUSTIFY_LEFT);
714         gtk_clist_set_column_justification(GTK_CLIST(clist), CALL_COL_FROM, GTK_JUSTIFY_LEFT);
715         gtk_clist_set_column_justification(GTK_CLIST(clist), CALL_COL_TO, GTK_JUSTIFY_LEFT);
716         gtk_clist_set_column_justification(GTK_CLIST(clist), CALL_COL_PROTOCOL, GTK_JUSTIFY_CENTER);
717         gtk_clist_set_column_justification(GTK_CLIST(clist), CALL_COL_PACKETS, GTK_JUSTIFY_CENTER);
718         gtk_clist_set_column_justification(GTK_CLIST(clist), CALL_COL_STATE, GTK_JUSTIFY_LEFT);
719         gtk_clist_set_column_justification(GTK_CLIST(clist), CALL_COL_COMMENTS, GTK_JUSTIFY_LEFT);
720
721         gtk_clist_column_titles_show (GTK_CLIST (clist));
722
723         gtk_clist_set_compare_func(GTK_CLIST(clist), voip_calls_sort_column);
724         gtk_clist_set_sort_column(GTK_CLIST(clist), 0);
725         gtk_clist_set_sort_type(GTK_CLIST(clist), GTK_SORT_ASCENDING);
726
727         gtk_widget_show(voip_calls_dlg_w);
728
729         /* sort by column feature */
730         col_arrows = (column_arrows *) g_malloc(sizeof(column_arrows) * NUM_COLS);
731
732         for (i=0; i<NUM_COLS; i++) {
733                 col_arrows[i].table = gtk_table_new(2, 2, FALSE);
734                 gtk_table_set_col_spacings(GTK_TABLE(col_arrows[i].table), 5);
735                 column_lb = gtk_label_new(titles[i]);
736                 gtk_table_attach(GTK_TABLE(col_arrows[i].table), column_lb, 0, 1, 0, 2, GTK_SHRINK, GTK_SHRINK, 0, 0);
737                 gtk_widget_show(column_lb);
738
739                 col_arrows[i].ascend_pm = xpm_to_widget(clist_ascend_xpm);
740                 gtk_table_attach(GTK_TABLE(col_arrows[i].table), col_arrows[i].ascend_pm, 1, 2, 1, 2, GTK_SHRINK, GTK_SHRINK, 0, 0);
741                 col_arrows[i].descend_pm = xpm_to_widget(clist_descend_xpm);
742                 gtk_table_attach(GTK_TABLE(col_arrows[i].table), col_arrows[i].descend_pm, 1, 2, 0, 1, GTK_SHRINK, GTK_SHRINK, 0, 0);
743                 /* make start time be the default sort order */
744                 if (i == 0) {
745                         gtk_widget_show(col_arrows[i].ascend_pm);
746                 }
747                 gtk_clist_set_column_widget(GTK_CLIST(clist), i, col_arrows[i].table);
748                 gtk_widget_show(col_arrows[i].table);
749         }
750
751         g_signal_connect(clist, "click-column", G_CALLBACK(voip_calls_click_column_cb), col_arrows);
752
753 /*      label_fwd = gtk_label_new (FWD_LABEL_TEXT);
754         gtk_box_pack_start (GTK_BOX (main_vb), label_fwd, FALSE, FALSE, 0);
755 */
756         status_label = gtk_label_new ("Total: Calls: 0   Start packets: 0   Completed calls: 0   Rejected calls: 0");
757         gtk_box_pack_start (GTK_BOX (main_vb), status_label, FALSE, FALSE, 8);
758
759         /* button row */
760         hbuttonbox = gtk_hbutton_box_new ();
761         gtk_box_pack_start (GTK_BOX (main_vb), hbuttonbox, FALSE, FALSE, 0);
762         gtk_button_box_set_layout (GTK_BUTTON_BOX (hbuttonbox), GTK_BUTTONBOX_SPREAD);
763         gtk_box_set_spacing (GTK_BOX (hbuttonbox), 30);
764
765         /*bt_unselect = gtk_button_new_with_label ("Unselect");
766         gtk_container_add (GTK_CONTAINER (hbuttonbox), bt_unselect);
767         gtk_tooltips_set_tip (tooltips, bt_unselect, "Unselect this conversation", NULL);*/
768
769         bt_filter = gtk_button_new_with_label ("Prepare Filter");
770         gtk_container_add (GTK_CONTAINER (hbuttonbox), bt_filter);
771         gtk_tooltips_set_tip (tooltips, bt_filter, "Prepare a display filter of the selected conversation", NULL);
772
773         bt_graph = gtk_button_new_with_label("Graph");
774         gtk_container_add(GTK_CONTAINER(hbuttonbox), bt_graph);
775         gtk_widget_show(bt_graph);
776         g_signal_connect(bt_graph, "clicked", G_CALLBACK(on_graph_bt_clicked), NULL);
777         gtk_tooltips_set_tip (tooltips, bt_graph, "Show a flow graph of the selected calls.", NULL);
778
779 #ifdef HAVE_LIBPORTAUDIO
780         bt_player = gtk_button_new_with_label("Player");
781         gtk_container_add(GTK_CONTAINER(hbuttonbox), bt_player);
782         gtk_widget_show(bt_player);
783         g_signal_connect(bt_player, "clicked", G_CALLBACK(on_player_bt_clicked), NULL);
784         gtk_tooltips_set_tip (tooltips, bt_player, "Launch the RTP player to listen the selected calls.", NULL);
785 #endif /* HAVE_LIBPORTAUDIO */
786
787         bt_select_all = gtk_button_new_with_label("Select All");
788         gtk_container_add (GTK_CONTAINER (hbuttonbox), bt_select_all);
789         GTK_WIDGET_SET_FLAGS(bt_select_all, GTK_CAN_DEFAULT);
790         gtk_tooltips_set_tip (tooltips, bt_select_all, "Select all the calls", NULL);
791
792         bt_close = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
793         gtk_container_add (GTK_CONTAINER (hbuttonbox), bt_close);
794         GTK_WIDGET_SET_FLAGS(bt_close, GTK_CAN_DEFAULT);
795         gtk_tooltips_set_tip (tooltips, bt_close, "Close this dialog", NULL);
796
797         g_signal_connect(clist, "select_row", G_CALLBACK(voip_calls_on_select_row), NULL);
798         g_signal_connect(clist, "unselect_row", G_CALLBACK(voip_calls_on_unselect_row), NULL);
799         /*g_signal_connect(bt_unselect, "clicked", G_CALLBACK(voip_calls_on_unselect), NULL);*/
800         g_signal_connect(bt_filter, "clicked", G_CALLBACK(voip_calls_on_filter), NULL);
801
802         window_set_cancel_button(voip_calls_dlg_w, bt_close, window_cancel_button_cb);
803
804         g_signal_connect(voip_calls_dlg_w, "delete_event", G_CALLBACK(window_delete_event_cb), NULL);
805         g_signal_connect(voip_calls_dlg_w, "destroy", G_CALLBACK(voip_calls_on_destroy), NULL);
806         g_signal_connect(bt_select_all, "clicked", G_CALLBACK(voip_calls_on_select_all), NULL);
807
808         gtk_widget_show_all(voip_calls_dlg_w);
809         window_present(voip_calls_dlg_w);
810
811         voip_calls_on_unselect(NULL, NULL);
812
813         voip_calls_dlg = voip_calls_dlg_w;
814
815         g_free(win_name); 
816 }
817
818
819 /****************************************************************************/
820 /* PUBLIC                                                                   */
821 /****************************************************************************/
822
823 /****************************************************************************/
824 /* update the contents of the dialog box clist */
825 /* list: pointer to list of voip_calls_info_t* */
826
827 void voip_calls_dlg_update(GList *list)
828 {
829         gchar label_text[256];
830         if (voip_calls_dlg != NULL) {
831                 calls_nb = 0;
832                 calls_ns = 0;
833
834                 g_snprintf(label_text, 256,
835                         "Total: Calls: %d   Start packets: %d   Completed calls: %d   Rejected calls: %d",
836                         g_list_length(voip_calls_get_info()->callsinfo_list),
837                         voip_calls_get_info()->start_packets, 
838                         voip_calls_get_info()->completed_calls,
839                         voip_calls_get_info()->rejected_calls);
840                 gtk_label_set(GTK_LABEL(status_label), label_text);
841
842                 gtk_clist_freeze(GTK_CLIST(clist));
843                 gtk_clist_clear(GTK_CLIST(clist));
844                 list = g_list_first(list);
845                 while (list)
846                 {
847                         add_to_clist((voip_calls_info_t*)(list->data));
848                         list = g_list_next(list);
849                 }
850                 gtk_clist_thaw(GTK_CLIST(clist));
851
852                 g_snprintf(label_text, 256,
853                         "Detected %d VoIP %s. Selected %d %s.",
854                         calls_nb, 
855                         plurality(calls_nb, "Call", "Calls"),
856                         calls_ns,
857                         plurality(calls_ns, "Call", "Calls"));
858                 gtk_label_set(GTK_LABEL(top_label), label_text);
859         }
860
861         last_list = list;
862 }
863
864
865 /****************************************************************************/
866 /* draw function for tap listeners to keep the window up to date */
867 void voip_calls_dlg_draw(void *ptr _U_)
868 {
869         if (voip_calls_get_info()->redraw) {
870                 voip_calls_dlg_update(voip_calls_get_info()->callsinfo_list);
871                 voip_calls_get_info()->redraw = FALSE;
872         }
873 }
874
875 /* reset function for tap listeners to clear window, if necessary */
876 void voip_calls_dlg_reset(void *ptr _U_)
877 {
878         /* Clean up memory used by calls tap */
879         voip_calls_reset((voip_calls_tapinfo_t*) voip_calls_get_info());
880
881         /* close the graph window if open */
882         if (graph_analysis_data && graph_analysis_data->dlg.window != NULL) {
883                 window_cancel_button_cb(NULL, graph_analysis_data->dlg.window);
884                 graph_analysis_data->dlg.window = NULL;
885         }
886 }
887
888 /* init function for tap */
889 /* Made extern only for "Fax T38 Analysis..." */
890 void
891 voip_calls_init_tap(const char *dummy _U_, void* userdata _U_)
892 {
893         gint c;
894         gchar *data[NUM_COLS];
895         gchar field[NUM_COLS][50];
896
897         if (graph_analysis_data == NULL) {
898                 graph_analysis_data_init();
899                 /* init the Graph Analysys */
900                 graph_analysis_data = graph_analysis_init();
901                 graph_analysis_data->graph_info = voip_calls_get_info()->graph_analysis;
902         }
903
904         /* Clean up memory used by calls tap */
905         voip_calls_reset((voip_calls_tapinfo_t*) voip_calls_get_info());
906
907         /* Register the tap listener */
908         sip_calls_init_tap();
909         mtp3_calls_init_tap();
910         isup_calls_init_tap();
911         h225_calls_init_tap();
912         h245dg_calls_init_tap();
913         q931_calls_init_tap();
914         h248_calls_init_tap();
915         sccp_calls_init_tap();
916         sdp_calls_init_tap();
917         /* We don't register this tap, if we don't have the unistim plugin loaded.*/
918         if (find_tap_id("unistim")) {
919                 unistim_calls_init_tap();
920         }
921         if (find_tap_id("voip")) {
922                 VoIPcalls_init_tap();
923         }
924         rtp_init_tap();
925         rtp_event_init_tap();
926         /* We don't register this tap, if we don't have the mgcp plugin loaded.*/
927         if (find_tap_id("mgcp")) {
928                 mgcp_calls_init_tap();
929         }
930         actrace_calls_init_tap();
931         t38_init_tap();
932
933         /* create dialog box if necessary */
934         if (voip_calls_dlg == NULL) {
935                 voip_calls_dlg_create();
936         } else {
937                 /* There's already a dialog box; reactivate it. */
938                 reactivate_window(voip_calls_dlg);
939         }
940
941         voip_calls_get_info()->redraw = TRUE;
942         voip_calls_dlg_draw(NULL);
943         voip_calls_get_info()->redraw = TRUE;
944         for (c=0;c<NUM_COLS;c++){
945                 data[c]=&field[c][0];
946                 field[c][0] = '\0';
947         }
948         g_snprintf(field[3], 50, "Please wait...");
949         gtk_clist_append(GTK_CLIST(clist), data);
950         
951         /* Scan for VoIP calls calls (redissect all packets) */
952         cf_retap_packets(&cfile, FALSE);
953         gdk_window_raise(voip_calls_dlg->window);
954         /* Tap listener will be removed and cleaned up in voip_calls_on_destroy */
955 }
956
957
958 /****************************************************************************/
959 /* entry point when called via the GTK menu */
960 static void voip_calls_launch(GtkWidget *w _U_, gpointer data _U_)
961 {
962         voip_calls_init_tap("",NULL);
963 }
964
965 /****************************************************************************/
966 void
967 register_tap_listener_voip_calls_dlg(void)
968 {
969         register_stat_cmd_arg("voip,calls",voip_calls_init_tap,NULL);
970         register_stat_menu_item("VoIP Calls", REGISTER_STAT_GROUP_TELEPHONY,
971             voip_calls_launch, NULL, NULL, NULL);    
972 }