From Carlos Mendioroz:
[metze/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_text(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_text(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         remove_tap_listener_mgcp_calls();
224         remove_tap_listener_actrace_calls();
225         remove_tap_listener_t38();
226         remove_tap_listener_skinny_calls();
227         remove_tap_listener_iax2_calls();
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_SKINNY:
381                         case VOIP_IAX2:
382                         case VOIP_COMMON:
383                                 /* XXX - not supported */
384                                 break;
385                 }
386                 
387         }
388         
389         g_string_free(filter_string_fwd, TRUE);
390 }
391
392
393 /****************************************************************************/
394 static void
395 voip_calls_on_select_all                    (GtkButton       *button _U_,
396                                         gpointer         user_data _U_)
397 {
398         gtk_clist_select_all(GTK_CLIST(clist));
399 }
400
401 /****************************************************************************/
402 static void
403 on_graph_bt_clicked                    (GtkButton       *button _U_,
404                                         gpointer         user_data _U_)
405 {
406         graph_analysis_item_t *gai;
407         GList* list;
408         GList* list2;
409         voip_calls_info_t *tmp_listinfo;
410
411         /* reset the "display" parameter in graph analysis */
412         list2 = g_list_first(voip_calls_get_info()->graph_analysis->list);
413         while (list2){
414                 gai = list2->data;
415                 gai->display = FALSE;
416                 list2 = g_list_next(list2);
417         }
418
419
420         /* set the display for selected calls */
421         list = g_list_first(voip_calls_get_info()->callsinfo_list);
422         while (list){
423                 tmp_listinfo=list->data;
424                 if (tmp_listinfo->selected){
425                         list2 = g_list_first(voip_calls_get_info()->graph_analysis->list);
426                         while (list2){
427                                 gai = list2->data;
428                                 if (gai->conv_num == tmp_listinfo->call_num){
429                                         gai->display = TRUE;
430                                 }
431                                 list2 = g_list_next(list2);
432                         }
433                 }
434                 list = g_list_next(list);
435         }
436
437         /* create or refresh the graph windows */
438         if (graph_analysis_data->dlg.window == NULL)    /* create the window */
439                 graph_analysis_create(graph_analysis_data);
440         else
441                 graph_analysis_update(graph_analysis_data);             /* refresh it */
442 }
443
444 #ifdef HAVE_LIBPORTAUDIO
445 /****************************************************************************/
446 static void
447 on_player_bt_clicked                    (GtkButton       *button _U_,
448                                         gpointer         user_data _U_)
449 {
450         rtp_player_init(voip_calls_get_info());
451 }
452 #endif /* HAVE_LIBPORTAUDIO */
453
454 /****************************************************************************/
455 /* when the user selects a row in the calls list */
456 static void
457 voip_calls_on_select_row(GtkCList *clist,
458                                             gint row _U_,
459                                             gint column _U_,
460                                             GdkEventButton *event _U_,
461                                             gpointer user_data _U_)
462 {
463 /*      GdkColor color = COLOR_DEFAULT;*/
464         gchar label_text[80];
465         GList* list;
466         voip_calls_info_t *listinfo;
467         
468         selected_call_fwd = gtk_clist_get_row_data(GTK_CLIST(clist), row);
469
470         if (selected_call_fwd==NULL)
471                 return;
472
473         selected_call_fwd->selected=TRUE;
474
475         /* count the selected calls */
476         calls_ns = 0;
477         list = g_list_first(voip_calls_get_info()->callsinfo_list);
478         while (list){
479                 listinfo=list->data;
480                 if (listinfo->selected){
481                         calls_ns++;     
482                 }
483                 list = g_list_next(list);
484         }
485
486         g_snprintf(label_text, 80,
487                 "Detected %d VoIP %s. Selected %d %s.",
488                 calls_nb, 
489             plurality(calls_nb, "Call", "Calls"),
490                         calls_ns,
491                         plurality(calls_ns, "Call", "Calls"));
492          gtk_label_set_text(GTK_LABEL(top_label), label_text);
493
494
495         if      (calls_ns > 0) {
496                 gtk_widget_set_sensitive(bt_filter, TRUE);
497                 gtk_widget_set_sensitive(bt_graph, TRUE);
498 #ifdef HAVE_LIBPORTAUDIO
499                 gtk_widget_set_sensitive(bt_player, TRUE);
500 #endif /* HAVE_LIBPORTAUDIO */
501         } else {
502                 gtk_widget_set_sensitive(bt_filter, FALSE);
503                 gtk_widget_set_sensitive(bt_graph, FALSE);
504 #ifdef HAVE_LIBPORTAUDIO
505                 gtk_widget_set_sensitive(bt_player, FALSE);
506 #endif /* HAVE_LIBPORTAUDIO */
507         }
508
509         /* TODO: activate other buttons when implemented */
510 }
511
512
513 /****************************************************************************/
514 /* when the user selects a row in the calls list */
515 static void
516 voip_calls_on_unselect_row(GtkCList *clist,
517                                             gint row _U_,
518                                             gint column _U_,
519                                             GdkEventButton *event _U_,
520                                             gpointer user_data _U_)
521 {
522         gchar label_text[80];
523         GList* list;
524         voip_calls_info_t *listinfo;
525         
526         selected_call_fwd = gtk_clist_get_row_data(GTK_CLIST(clist), row);
527
528         if (selected_call_fwd==NULL)
529                 return;
530
531         selected_call_fwd->selected=FALSE;
532
533         /* count the selected calls */
534         calls_ns = 0;
535         list = g_list_first(voip_calls_get_info()->callsinfo_list);
536         while (list){
537                 listinfo=list->data;
538                 if (listinfo->selected){
539                         calls_ns++;     
540                 }
541                 list = g_list_next(list);
542         }
543
544         g_snprintf(label_text, 80,
545                 "Detected %d VoIP %s. Selected %d %s.",
546                 calls_nb, 
547             plurality(calls_nb, "Call", "Calls"),
548                         calls_ns,
549                         plurality(calls_ns, "Call", "Calls"));
550          gtk_label_set_text(GTK_LABEL(top_label), label_text);
551
552         if      (calls_ns > 0) {
553                 gtk_widget_set_sensitive(bt_filter, TRUE);
554                 gtk_widget_set_sensitive(bt_graph, TRUE);
555 #ifdef HAVE_LIBPORTAUDIO
556                 gtk_widget_set_sensitive(bt_player, TRUE);
557 #endif /* HAVE_LIBPORTAUDIO */
558         } else {
559                 gtk_widget_set_sensitive(bt_filter, FALSE);
560                 gtk_widget_set_sensitive(bt_graph, FALSE);
561 #ifdef HAVE_LIBPORTAUDIO
562                 gtk_widget_set_sensitive(bt_player, FALSE);
563 #endif /* HAVE_LIBPORTAUDIO */
564         }
565
566         /* TODO: activate other buttons when implemented */
567 }
568
569
570
571
572
573
574
575
576 /****************************************************************************/
577
578 typedef struct column_arrows {
579         GtkWidget *table;
580         GtkWidget *ascend_pm;
581         GtkWidget *descend_pm;
582 } column_arrows;
583
584
585 /****************************************************************************/
586 static void
587 voip_calls_click_column_cb(GtkCList *clist, gint column, gpointer data)
588 {
589         column_arrows *col_arrows = (column_arrows *) data;
590         int i;
591
592         gtk_clist_freeze(clist);
593
594         for (i=0; i<NUM_COLS; i++) {
595                 gtk_widget_hide(col_arrows[i].ascend_pm);
596                 gtk_widget_hide(col_arrows[i].descend_pm);
597         }
598
599         if (column == clist->sort_column) {
600                 if (clist->sort_type == GTK_SORT_ASCENDING) {
601                         clist->sort_type = GTK_SORT_DESCENDING;
602                         gtk_widget_show(col_arrows[column].descend_pm);
603                 } else {
604                         clist->sort_type = GTK_SORT_ASCENDING;
605                         gtk_widget_show(col_arrows[column].ascend_pm);
606                 }
607         } else {
608                 clist->sort_type = GTK_SORT_ASCENDING;
609                 gtk_widget_show(col_arrows[column].ascend_pm);
610                 gtk_clist_set_sort_column(clist, column);
611         }
612         gtk_clist_thaw(clist);
613
614         gtk_clist_sort(clist);
615 }
616
617
618 /****************************************************************************/
619 static gint
620 voip_calls_sort_column(GtkCList *clist, gconstpointer ptr1, gconstpointer ptr2)
621 {
622         char *text1 = NULL;
623         char *text2 = NULL;
624         guint i1, i2, i3, i4;
625
626         const GtkCListRow *row1 = (const GtkCListRow *) ptr1;
627         const GtkCListRow *row2 = (const GtkCListRow *) ptr2;
628
629         text1 = GTK_CELL_TEXT (row1->cell[clist->sort_column])->text;
630         text2 = GTK_CELL_TEXT (row2->cell[clist->sort_column])->text;
631
632         switch(clist->sort_column){
633         case CALL_COL_START_TIME:
634         case CALL_COL_STOP_TIME:
635                 if ((sscanf(text1, "%u.%u", &i1, &i2) != 2) ||
636                         (sscanf(text2, "%u.%u", &i3, &i4) != 2) ){
637                                 return 0;
638                         }
639                 if (i1>i3)
640                         return 1;
641                 if (i1<i3)
642                         return -1;
643                 return (i3-i4);
644         case CALL_COL_INITIAL_SPEAKER:
645         case CALL_COL_FROM:
646         case CALL_COL_TO:
647         case CALL_COL_PROTOCOL:
648         case CALL_COL_STATE:
649         case CALL_COL_COMMENTS:
650                 return strcmp (text1, text2);
651         case CALL_COL_PACKETS:
652                 i1=atoi(text1);
653                 i2=atoi(text2);
654                 return i1-i2;
655         }
656         g_assert_not_reached();
657         return 0;
658 }
659
660
661 /****************************************************************************/
662 /* INTERFACE                                                                */
663 /****************************************************************************/
664
665 static void voip_calls_dlg_create (void)
666 {
667         GtkWidget *voip_calls_dlg_w;
668         GtkWidget *main_vb;
669         GtkWidget *scrolledwindow;
670         GtkWidget *hbuttonbox;
671         GtkWidget *bt_close;
672         GtkWidget *bt_select_all;
673         GtkTooltips *tooltips = gtk_tooltips_new();
674         const gchar *title_name_ptr;
675         gchar   *win_name;
676
677         const gchar *titles[NUM_COLS] =  {"Start Time", "Stop Time", "Initial Speaker", "From",  "To", "Protocol", "Packets", "State", "Comments"};
678         column_arrows *col_arrows;
679         GtkWidget *column_lb;
680         int i;
681
682         title_name_ptr = cf_get_display_name(&cfile);
683         win_name = g_strdup_printf("%s - VoIP Calls", title_name_ptr);
684         voip_calls_dlg_w=window_new(GTK_WINDOW_TOPLEVEL, win_name);
685
686         gtk_window_set_default_size(GTK_WINDOW(voip_calls_dlg_w), 840, 350);
687
688         main_vb = gtk_vbox_new (FALSE, 0);
689         gtk_container_add(GTK_CONTAINER(voip_calls_dlg_w), main_vb);
690         gtk_container_set_border_width (GTK_CONTAINER (main_vb), 12);
691
692         top_label = gtk_label_new ("Detected 0 VoIP Calls. Selected 0 Calls.");
693         gtk_box_pack_start (GTK_BOX (main_vb), top_label, FALSE, FALSE, 8);
694
695         scrolledwindow = scrolled_window_new (NULL, NULL);
696         gtk_box_pack_start (GTK_BOX (main_vb), scrolledwindow, TRUE, TRUE, 0);
697
698         clist = gtk_clist_new (NUM_COLS);
699         gtk_clist_set_selection_mode(GTK_CLIST (clist), GTK_SELECTION_MULTIPLE);
700
701         gtk_container_add (GTK_CONTAINER (scrolledwindow), clist);
702
703         gtk_clist_set_column_width (GTK_CLIST (clist), CALL_COL_START_TIME, 60);
704         gtk_clist_set_column_width (GTK_CLIST (clist), CALL_COL_STOP_TIME, 60);
705         gtk_clist_set_column_width (GTK_CLIST (clist), CALL_COL_INITIAL_SPEAKER, 80);
706         gtk_clist_set_column_width (GTK_CLIST (clist), CALL_COL_FROM, 130);
707         gtk_clist_set_column_width (GTK_CLIST (clist), CALL_COL_TO, 130);
708         gtk_clist_set_column_width (GTK_CLIST (clist), CALL_COL_PROTOCOL, 50);
709         gtk_clist_set_column_width (GTK_CLIST (clist), CALL_COL_PACKETS, 45);
710         gtk_clist_set_column_width (GTK_CLIST (clist), CALL_COL_STATE, 60);
711         gtk_clist_set_column_width (GTK_CLIST (clist), CALL_COL_COMMENTS, 100);
712
713         gtk_clist_set_column_justification(GTK_CLIST(clist), CALL_COL_START_TIME, GTK_JUSTIFY_LEFT);
714         gtk_clist_set_column_justification(GTK_CLIST(clist), CALL_COL_STOP_TIME, GTK_JUSTIFY_LEFT);
715         gtk_clist_set_column_justification(GTK_CLIST(clist), CALL_COL_INITIAL_SPEAKER, GTK_JUSTIFY_LEFT);
716         gtk_clist_set_column_justification(GTK_CLIST(clist), CALL_COL_FROM, GTK_JUSTIFY_LEFT);
717         gtk_clist_set_column_justification(GTK_CLIST(clist), CALL_COL_TO, GTK_JUSTIFY_LEFT);
718         gtk_clist_set_column_justification(GTK_CLIST(clist), CALL_COL_PROTOCOL, GTK_JUSTIFY_CENTER);
719         gtk_clist_set_column_justification(GTK_CLIST(clist), CALL_COL_PACKETS, GTK_JUSTIFY_CENTER);
720         gtk_clist_set_column_justification(GTK_CLIST(clist), CALL_COL_STATE, GTK_JUSTIFY_LEFT);
721         gtk_clist_set_column_justification(GTK_CLIST(clist), CALL_COL_COMMENTS, GTK_JUSTIFY_LEFT);
722
723         gtk_clist_column_titles_show (GTK_CLIST (clist));
724
725         gtk_clist_set_compare_func(GTK_CLIST(clist), voip_calls_sort_column);
726         gtk_clist_set_sort_column(GTK_CLIST(clist), 0);
727         gtk_clist_set_sort_type(GTK_CLIST(clist), GTK_SORT_ASCENDING);
728
729         gtk_widget_show(voip_calls_dlg_w);
730
731         /* sort by column feature */
732         col_arrows = (column_arrows *) g_malloc(sizeof(column_arrows) * NUM_COLS);
733
734         for (i=0; i<NUM_COLS; i++) {
735                 col_arrows[i].table = gtk_table_new(2, 2, FALSE);
736                 gtk_table_set_col_spacings(GTK_TABLE(col_arrows[i].table), 5);
737                 column_lb = gtk_label_new(titles[i]);
738                 gtk_table_attach(GTK_TABLE(col_arrows[i].table), column_lb, 0, 1, 0, 2, GTK_SHRINK, GTK_SHRINK, 0, 0);
739                 gtk_widget_show(column_lb);
740
741                 col_arrows[i].ascend_pm = xpm_to_widget(clist_ascend_xpm);
742                 gtk_table_attach(GTK_TABLE(col_arrows[i].table), col_arrows[i].ascend_pm, 1, 2, 1, 2, GTK_SHRINK, GTK_SHRINK, 0, 0);
743                 col_arrows[i].descend_pm = xpm_to_widget(clist_descend_xpm);
744                 gtk_table_attach(GTK_TABLE(col_arrows[i].table), col_arrows[i].descend_pm, 1, 2, 0, 1, GTK_SHRINK, GTK_SHRINK, 0, 0);
745                 /* make start time be the default sort order */
746                 if (i == 0) {
747                         gtk_widget_show(col_arrows[i].ascend_pm);
748                 }
749                 gtk_clist_set_column_widget(GTK_CLIST(clist), i, col_arrows[i].table);
750                 gtk_widget_show(col_arrows[i].table);
751         }
752
753         g_signal_connect(clist, "click-column", G_CALLBACK(voip_calls_click_column_cb), col_arrows);
754
755 /*      label_fwd = gtk_label_new (FWD_LABEL_TEXT);
756         gtk_box_pack_start (GTK_BOX (main_vb), label_fwd, FALSE, FALSE, 0);
757 */
758         status_label = gtk_label_new ("Total: Calls: 0   Start packets: 0   Completed calls: 0   Rejected calls: 0");
759         gtk_box_pack_start (GTK_BOX (main_vb), status_label, FALSE, FALSE, 8);
760
761         /* button row */
762         hbuttonbox = gtk_hbutton_box_new ();
763         gtk_box_pack_start (GTK_BOX (main_vb), hbuttonbox, FALSE, FALSE, 0);
764         gtk_button_box_set_layout (GTK_BUTTON_BOX (hbuttonbox), GTK_BUTTONBOX_SPREAD);
765         gtk_box_set_spacing (GTK_BOX (hbuttonbox), 30);
766
767         /*bt_unselect = gtk_button_new_with_label ("Unselect");
768         gtk_container_add (GTK_CONTAINER (hbuttonbox), bt_unselect);
769         gtk_tooltips_set_tip (tooltips, bt_unselect, "Unselect this conversation", NULL);*/
770
771         bt_filter = gtk_button_new_with_label ("Prepare Filter");
772         gtk_container_add (GTK_CONTAINER (hbuttonbox), bt_filter);
773         gtk_tooltips_set_tip (tooltips, bt_filter, "Prepare a display filter of the selected conversation", NULL);
774
775         bt_graph = gtk_button_new_with_label("Graph");
776         gtk_container_add(GTK_CONTAINER(hbuttonbox), bt_graph);
777         gtk_widget_show(bt_graph);
778         g_signal_connect(bt_graph, "clicked", G_CALLBACK(on_graph_bt_clicked), NULL);
779         gtk_tooltips_set_tip (tooltips, bt_graph, "Show a flow graph of the selected calls.", NULL);
780
781 #ifdef HAVE_LIBPORTAUDIO
782         bt_player = gtk_button_new_with_label("Player");
783         gtk_container_add(GTK_CONTAINER(hbuttonbox), bt_player);
784         gtk_widget_show(bt_player);
785         g_signal_connect(bt_player, "clicked", G_CALLBACK(on_player_bt_clicked), NULL);
786         gtk_tooltips_set_tip (tooltips, bt_player, "Launch the RTP player to listen the selected calls.", NULL);
787 #endif /* HAVE_LIBPORTAUDIO */
788
789         bt_select_all = gtk_button_new_with_label("Select All");
790         gtk_container_add (GTK_CONTAINER (hbuttonbox), bt_select_all);
791         GTK_WIDGET_SET_FLAGS(bt_select_all, GTK_CAN_DEFAULT);
792         gtk_tooltips_set_tip (tooltips, bt_select_all, "Select all the calls", NULL);
793
794         bt_close = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
795         gtk_container_add (GTK_CONTAINER (hbuttonbox), bt_close);
796         GTK_WIDGET_SET_FLAGS(bt_close, GTK_CAN_DEFAULT);
797         gtk_tooltips_set_tip (tooltips, bt_close, "Close this dialog", NULL);
798
799         g_signal_connect(clist, "select_row", G_CALLBACK(voip_calls_on_select_row), NULL);
800         g_signal_connect(clist, "unselect_row", G_CALLBACK(voip_calls_on_unselect_row), NULL);
801         /*g_signal_connect(bt_unselect, "clicked", G_CALLBACK(voip_calls_on_unselect), NULL);*/
802         g_signal_connect(bt_filter, "clicked", G_CALLBACK(voip_calls_on_filter), NULL);
803
804         window_set_cancel_button(voip_calls_dlg_w, bt_close, window_cancel_button_cb);
805
806         g_signal_connect(voip_calls_dlg_w, "delete_event", G_CALLBACK(window_delete_event_cb), NULL);
807         g_signal_connect(voip_calls_dlg_w, "destroy", G_CALLBACK(voip_calls_on_destroy), NULL);
808         g_signal_connect(bt_select_all, "clicked", G_CALLBACK(voip_calls_on_select_all), NULL);
809
810         gtk_widget_show_all(voip_calls_dlg_w);
811         window_present(voip_calls_dlg_w);
812
813         voip_calls_on_unselect(NULL, NULL);
814
815         voip_calls_dlg = voip_calls_dlg_w;
816
817         g_free(win_name); 
818 }
819
820
821 /****************************************************************************/
822 /* PUBLIC                                                                   */
823 /****************************************************************************/
824
825 /****************************************************************************/
826 /* update the contents of the dialog box clist */
827 /* list: pointer to list of voip_calls_info_t* */
828
829 void voip_calls_dlg_update(GList *list)
830 {
831         gchar label_text[256];
832         if (voip_calls_dlg != NULL) {
833                 calls_nb = 0;
834                 calls_ns = 0;
835
836                 g_snprintf(label_text, 256,
837                         "Total: Calls: %d   Start packets: %d   Completed calls: %d   Rejected calls: %d",
838                         g_list_length(voip_calls_get_info()->callsinfo_list),
839                         voip_calls_get_info()->start_packets, 
840                         voip_calls_get_info()->completed_calls,
841                         voip_calls_get_info()->rejected_calls);
842                  gtk_label_set_text(GTK_LABEL(status_label), label_text);
843
844                 gtk_clist_freeze(GTK_CLIST(clist));
845                 gtk_clist_clear(GTK_CLIST(clist));
846                 list = g_list_first(list);
847                 while (list)
848                 {
849                         add_to_clist((voip_calls_info_t*)(list->data));
850                         list = g_list_next(list);
851                 }
852                 gtk_clist_thaw(GTK_CLIST(clist));
853
854                 g_snprintf(label_text, 256,
855                         "Detected %d VoIP %s. Selected %d %s.",
856                         calls_nb, 
857                         plurality(calls_nb, "Call", "Calls"),
858                         calls_ns,
859                         plurality(calls_ns, "Call", "Calls"));
860                  gtk_label_set_text(GTK_LABEL(top_label), label_text);
861         }
862
863         last_list = list;
864 }
865
866
867 /****************************************************************************/
868 /* draw function for tap listeners to keep the window up to date */
869 void voip_calls_dlg_draw(void *ptr _U_)
870 {
871         if (voip_calls_get_info()->redraw) {
872                 voip_calls_dlg_update(voip_calls_get_info()->callsinfo_list);
873                 voip_calls_get_info()->redraw = FALSE;
874         }
875 }
876
877 /* reset function for tap listeners to clear window, if necessary */
878 void voip_calls_dlg_reset(void *ptr _U_)
879 {
880         /* Clean up memory used by calls tap */
881         voip_calls_reset((voip_calls_tapinfo_t*) voip_calls_get_info());
882
883         /* close the graph window if open */
884         if (graph_analysis_data && graph_analysis_data->dlg.window != NULL) {
885                 window_cancel_button_cb(NULL, graph_analysis_data->dlg.window);
886                 graph_analysis_data->dlg.window = NULL;
887         }
888 }
889
890 /* init function for tap */
891 /* Made extern only for "Fax T38 Analysis..." */
892 void
893 voip_calls_init_tap(const char *dummy _U_, void* userdata _U_)
894 {
895         gint c;
896         gchar *data[NUM_COLS];
897         gchar field[NUM_COLS][50];
898
899         if (graph_analysis_data == NULL) {
900                 graph_analysis_data_init();
901                 /* init the Graph Analysys */
902                 graph_analysis_data = graph_analysis_init();
903                 graph_analysis_data->graph_info = voip_calls_get_info()->graph_analysis;
904         }
905
906         /* Clean up memory used by calls tap */
907         voip_calls_reset((voip_calls_tapinfo_t*) voip_calls_get_info());
908
909         /* Register the tap listener */
910         sip_calls_init_tap();
911         mtp3_calls_init_tap();
912         isup_calls_init_tap();
913         h225_calls_init_tap();
914         h245dg_calls_init_tap();
915         q931_calls_init_tap();
916         h248_calls_init_tap();
917         sccp_calls_init_tap();
918         sdp_calls_init_tap();
919         /* We don't register this tap, if we don't have the unistim plugin loaded.*/
920         if (find_tap_id("unistim")) {
921                 unistim_calls_init_tap();
922         }
923         if (find_tap_id("voip")) {
924                 VoIPcalls_init_tap();
925         }
926         rtp_init_tap();
927         rtp_event_init_tap();
928         mgcp_calls_init_tap();
929         actrace_calls_init_tap();
930         t38_init_tap();
931         skinny_calls_init_tap();
932         iax2_calls_init_tap();
933
934         /* create dialog box if necessary */
935         if (voip_calls_dlg == NULL) {
936                 voip_calls_dlg_create();
937         } else {
938                 /* There's already a dialog box; reactivate it. */
939                 reactivate_window(voip_calls_dlg);
940         }
941
942         voip_calls_get_info()->redraw = TRUE;
943         voip_calls_dlg_draw(NULL);
944         voip_calls_get_info()->redraw = TRUE;
945         for (c=0;c<NUM_COLS;c++){
946                 data[c]=&field[c][0];
947                 field[c][0] = '\0';
948         }
949         g_snprintf(field[3], 50, "Please wait...");
950         gtk_clist_append(GTK_CLIST(clist), data);
951         
952         /* Scan for VoIP calls calls (redissect all packets) */
953         cf_retap_packets(&cfile, FALSE);
954         gdk_window_raise(voip_calls_dlg->window);
955         /* Tap listener will be removed and cleaned up in voip_calls_on_destroy */
956 }
957
958
959 /****************************************************************************/
960 /* entry point when called via the GTK menu */
961 static void voip_calls_launch(GtkWidget *w _U_, gpointer data _U_)
962 {
963         voip_calls_init_tap("",NULL);
964 }
965
966 /****************************************************************************/
967 void
968 register_tap_listener_voip_calls_dlg(void)
969 {
970         register_stat_cmd_arg("voip,calls",voip_calls_init_tap,NULL);
971         register_stat_menu_item("VoIP Calls", REGISTER_STAT_GROUP_TELEPHONY,
972             voip_calls_launch, NULL, NULL, NULL);    
973 }