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