Make the signatures of functions passed to "register_tap_listener()"
[obnox/wireshark/wip.git] / gtk / h323_analysis.c
1 /* h323_analysis.c
2  * H323 analysis addition for ethereal
3  *
4  * $Id$
5  *
6  * Copyright 2004, Iskratel, Ltd, Kranj
7  * By Miha Jemec <m.jemec@iskratel.si>
8  *
9  * Ethereal - Network traffic analyzer
10  * By Gerald Combs <gerald@ethereal.com>
11  * Copyright 1998 Gerald Combs
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation,  Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26  */
27
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 /*do not define this symbol. will be added soon*/
33 /*#define USE_CONVERSATION_GRAPH 1*/
34
35 #include "h323_analysis.h"
36 #include "h323_conversations.h"
37 #include "h323_conversations_dlg.h"
38
39 #include <epan/epan_dissect.h>
40 #include <epan/filesystem.h>
41
42 #include "globals.h"
43
44 #include "util.h"
45 #include <epan/tap.h>
46
47 #include <epan/dissectors/packet-h225.h>
48 #include <epan/dissectors/packet-h245.h>
49
50 /* in /gtk ... */
51 #include "dlg_utils.h"
52 #include "ui_util.h"
53 #include "alert_box.h"
54 #include "simple_dialog.h"
55 #include "tap_menu.h"
56 #include "main.h"
57 #include "progress_dlg.h"
58 #include "compat_macros.h"
59
60 #include <string.h>
61
62 /****************************************************************************/
63 /* structure that holds general information about the connection */
64 typedef struct _user_data_t {
65         /* tap associated data*/
66         guint32 ip_src;
67         guint16 port_src;
68         guint32 ip_dst;
69         guint16 port_dst;
70         guint32 ip_src_h245;
71         guint16 port_src_h245;
72         guint16 transport;
73
74         GtkWidget *window;
75         GtkCList *clist1;
76         GtkWidget *label_stats;
77         GtkCList *selected_clist1;
78         gint selected_row;
79
80 } user_data_t;
81
82 /* Column titles. */
83 static gchar *titles[7] =  {
84         "Packet",
85         "Time",
86         "Delay",
87         "             Side A",
88         "Direction",
89         "             Side B",
90         "Comment"
91 };
92
93 typedef const guint8 * ip_addr_p;
94
95 static gint32 last_sec = 0, last_usec = 0;
96
97
98 /****************************************************************************/
99 /* TAP FUNCTIONS */
100
101 /****************************************************************************/
102 /* when there is a [re]reading of packet's */
103 static void
104 h225_reset(void *user_data_arg _U_)
105 {
106         last_sec = 0;
107         last_usec = 0;
108         return;
109 }
110
111 /****************************************************************************/
112 /* here we can redraw the output */
113 /* not used yet */
114 static void h225_draw(void *prs _U_)
115 {
116         return;
117 }
118
119
120 static const GdkColor COLOR_DEFAULT = {0, 0xffff, 0xffff, 0xffff};
121 static const GdkColor COLOR_ERROR = {0, 0xffff, 0xbfff, 0xbfff};
122 static const GdkColor COLOR_WARNING = {0, 0xffff, 0xdfff, 0xbfff};
123 static const GdkColor COLOR_CN = {0, 0xbfff, 0xbfff, 0xffff};
124
125 /****************************************************************************/
126 /* append a line to clist1 */
127 static void add_to_clist1(GtkCList *clist1, guint32 number, gchar *time,
128                          double delay, gchar *sideA, gboolean direction, gchar *sideB,
129                          gchar *comment, GdkColor *color)
130 {
131         guint added_row;
132         gchar *data[7];
133         gchar field[7][32];
134
135         data[0]=&field[0][0];
136         data[1]=&field[1][0];
137         data[2]=&field[2][0];
138         data[3]=&field[3][0];
139         data[4]=&field[4][0];
140         data[5]=&field[5][0];
141         data[6]=&field[6][0];
142
143         g_snprintf(field[0], 32, "%u", number);
144         g_snprintf(field[1], 32, "%s", time);
145         g_snprintf(field[2], 32, "%f", delay);
146         g_snprintf(field[3], 32, "%s", sideA);
147         g_snprintf(field[4], 20, "%s", direction? "---->" : "<----");
148         g_snprintf(field[5], 32, "%s", sideB);
149         g_snprintf(field[6], 32, "%s", comment);
150
151         added_row = gtk_clist_append(GTK_CLIST(clist1), data);
152         gtk_clist_set_row_data(GTK_CLIST(clist1), added_row, GUINT_TO_POINTER(number));
153         gtk_clist_set_background(GTK_CLIST(clist1), added_row, color);
154 }
155
156
157 /****************************************************************************/
158 /* whenever a h225 packet is seen by the tap listener */
159 static int h225_packet(void *user_data_arg, packet_info *pinfo, epan_dissect_t *edt _U_, const void *h225info_arg)
160 {
161         user_data_t *user_data = user_data_arg;
162         const h225_packet_info *h225ptr_info = h225info_arg;    
163         GdkColor color = COLOR_DEFAULT;
164
165         gchar timeStr[32];
166         gchar message[32];
167         guint32 src, dst;
168         double delay;
169         gint32 delay_sec, delay_usec;
170
171         /* time since beginning of capture */
172         g_snprintf(timeStr, sizeof(timeStr), "%d.%06d", pinfo->fd->rel_secs, pinfo->fd->rel_usecs);
173
174         /* time since previous packet seen in tap listener */
175         delay_sec = pinfo->fd->rel_secs - last_sec;
176         delay_usec = pinfo->fd->rel_usecs - last_usec;
177         
178         delay = (double)delay_sec + ((double)delay_usec)/1000000;
179
180         last_sec = pinfo->fd->rel_secs;
181         last_usec = pinfo->fd->rel_usecs;
182
183         switch (h225ptr_info->cs_type) {
184                 
185                 case H225_SETUP:
186                         g_snprintf(message, sizeof(message),"H225 Setup");
187                         break;
188                 case H225_CALL_PROCEDING:
189                         g_snprintf(message, sizeof(message),"H225 Call Proceding");
190                         break;
191                 case H225_ALERTING:
192                         g_snprintf(message, sizeof(message),"H225 Alerting");
193                         break;
194                 case H225_CONNECT:
195                         g_snprintf(message, sizeof(message),"H225 Connect");
196                         break;
197                 case H225_RELEASE_COMPLET:
198                         g_snprintf(message, sizeof(message),"H225 Release Complet");
199                         break;
200                 case H225_OTHER:
201                         g_snprintf(message, sizeof(message),"H225 Other");
202                 }
203
204         g_memmove(&src, pinfo->src.data, 4);
205         g_memmove(&dst, pinfo->dst.data, 4);
206
207         if (src == user_data->ip_src)
208                 add_to_clist1(user_data->clist1,pinfo->fd->num,timeStr,delay,message, 1, "", "", &color);
209         else
210                 add_to_clist1(user_data->clist1, pinfo->fd->num, timeStr,delay, "", 0, message, "", &color);
211
212         return 0;
213 }
214
215
216 /****************************************************************************/
217 /* whenever a h245 packet is seen by the tap listener */
218 static int h245_packet(void *user_data_arg, packet_info *pinfo, epan_dissect_t *edt _U_, const void *h245info_arg)
219 {
220
221         user_data_t *user_data = user_data_arg;
222         const h245_packet_info *h245ptr_info = h245info_arg;    
223         GdkColor color = COLOR_DEFAULT;
224
225         gchar timeStr[32];
226         gchar message[32];
227         guint32 src, dst;
228         double delay;
229         gint32 delay_sec, delay_usec;
230
231         /* time since beginning of capture */
232         g_snprintf(timeStr, sizeof(timeStr), "%d.%06d", pinfo->fd->rel_secs, pinfo->fd->rel_usecs);
233
234         /* time since previous packet seen in tap listener */
235         delay_sec = pinfo->fd->rel_secs - last_sec;
236         delay_usec = pinfo->fd->rel_usecs - last_usec;
237         
238         delay = (double)delay_sec + ((double)delay_usec)/1000000;
239
240         last_sec = pinfo->fd->rel_secs;
241         last_usec = pinfo->fd->rel_usecs;
242
243         switch (h245ptr_info->msg_type) {
244                 
245                 case H245_TermCapSet:
246                         g_snprintf(message, sizeof(message),"H245 TermCapSet");
247                         break;
248                 case H245_TermCapSetAck:
249                         g_snprintf(message, sizeof(message),"H245_TermCapSetAck");
250                         break;
251                 case H245_TermCapSetRjc:
252                         g_snprintf(message, sizeof(message),"H245_TermCapSetRjc");
253                         break;
254                 case H245_TermCapSetRls:
255                         g_snprintf(message, sizeof(message),"H245_TermCapSetRls");
256                         break;
257                 case H245_OpenLogChn:
258                         g_snprintf(message, sizeof(message),"H245_OpenLogChn");
259                         break;
260                 case H245_OpenLogChnCnf:
261                         g_snprintf(message, sizeof(message),"H245_OpenLogChnCnf");
262                         break;
263                 case H245_OpenLogChnAck:
264                         g_snprintf(message, sizeof(message),"H245_OpenLogChnAck");
265                         break;
266                 case H245_OpenLogChnRjc:
267                         g_snprintf(message, sizeof(message),"H245_OpenLogChnRjc");
268                         break;
269                 case H245_CloseLogChn:
270                         g_snprintf(message, sizeof(message),"H245_CloseLogChn");
271                         break;
272                 case H245_CloseLogChnAck:
273                         g_snprintf(message, sizeof(message),"H245_CloseLogChnAck");
274                         break;
275                 case H245_MastSlvDet:
276                         g_snprintf(message, sizeof(message),"H245_MastSlvDet");
277                         break;
278                 case H245_MastSlvDetAck:
279                         g_snprintf(message, sizeof(message),"H245_MastSlvDetAck");
280                         break;
281                 case H245_MastSlvDetRjc:
282                         g_snprintf(message, sizeof(message),"H245_MastSlvDetRjc");
283                         break;
284                 case H245_MastSlvDetRls:
285                         g_snprintf(message, sizeof(message),"H245_MastSlvDetRls");
286                         break;
287                 case H245_OTHER:
288                         g_snprintf(message, sizeof(message),"H225 Other");
289                 }
290
291         g_memmove(&src, pinfo->src.data, 4);
292         g_memmove(&dst, pinfo->dst.data, 4);
293
294         if (src == user_data->ip_src)
295                 add_to_clist1(user_data->clist1,pinfo->fd->num,timeStr,delay,message, 1, "", "", &color);
296         else
297                 add_to_clist1(user_data->clist1, pinfo->fd->num, timeStr,delay, "", 0, message, "", &color);
298
299         return 0;
300 }
301
302
303 /**************** Callbacks *************************************************/
304 /****************************************************************************/
305 /* XXX just copied from gtk/rpc_stat.c */
306 void protect_thread_critical_region(void);
307 void unprotect_thread_critical_region(void);
308
309
310 /****************************************************************************/
311 /* close the dialog window and remove the tap listener */
312 static void on_destroy(GtkWidget *win _U_, user_data_t *user_data _U_)
313 {
314         g_free(user_data);
315 }
316
317 /****************************************************************************/
318 static void on_clist_select_row(GtkCList        *clist1 _U_,
319                                 gint             row _U_,
320                                 gint             column _U_,
321                                 GdkEvent        *event _U_,
322                                 user_data_t     *user_data _U_)
323 {
324         user_data->selected_clist1 = clist1;
325         user_data->selected_row = row;
326 }
327
328 /****************************************************************************/
329 static void on_goto_bt_clicked(GtkWidget *bt _U_, user_data_t *user_data _U_)
330 {
331         guint fnumber;
332
333         if (user_data->selected_clist1!=NULL) {
334                 fnumber = GPOINTER_TO_UINT(gtk_clist_get_row_data(
335                         GTK_CLIST(user_data->selected_clist1), user_data->selected_row) );
336                goto_frame(&cfile, fnumber);
337         }
338 }
339
340
341 /****************************************************************************/
342 /* re-dissects all packets */
343 static void on_refresh_bt_clicked(GtkWidget *bt _U_, user_data_t *user_data _U_)
344 {
345         gchar filter_text_h225[256];
346         gchar filter_text_h245[256];
347         /*gchar filter_text_rtp[256];*/
348         dfilter_t *sfcode;
349         GString *error_string;
350
351         /* clear the dialog box clists */
352         gtk_clist_clear(GTK_CLIST(user_data->clist1));
353
354         /* try to compile the filter for h225 */
355         g_snprintf(filter_text_h225,sizeof(filter_text_h225),
356         "h225 && (( ip.src==%s && %s.srcport==%u && ip.dst==%s && %s.dstport==%u ) || ( ip.src==%s && %s.srcport==%u && ip.dst==%s && %s.dstport==%u ))",
357                 ip_to_str((ip_addr_p)&(user_data->ip_src)),
358                                 transport_prot_name[user_data->transport],
359                 user_data->port_src,
360                 ip_to_str((ip_addr_p)&(user_data->ip_dst)),
361                                 transport_prot_name[user_data->transport],
362                 user_data->port_dst,
363                 ip_to_str((ip_addr_p)&(user_data->ip_dst)),
364                                 transport_prot_name[user_data->transport],
365                 user_data->port_dst,
366                 ip_to_str((ip_addr_p)&(user_data->ip_src)),
367                                 transport_prot_name[user_data->transport],
368                 user_data->port_src
369                 );
370
371         if (!dfilter_compile(filter_text_h225, &sfcode)) {
372                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, dfilter_error_msg);
373                 return;
374         }
375
376         /* try to compile the filter for h245 */
377         g_snprintf(filter_text_h245,sizeof(filter_text_h245), 
378         "h245 && (( ip.src==%s && %s.srcport==%u ) || ( ip.dst==%s && %s.dstport==%u ))",
379                 ip_to_str((ip_addr_p)&(user_data->ip_src_h245)),
380                                 transport_prot_name[user_data->transport],
381                 user_data->port_src_h245,
382                 ip_to_str((ip_addr_p)&(user_data->ip_src_h245)),
383                                 transport_prot_name[user_data->transport],
384                 user_data->port_src_h245
385                 );
386
387         if (!dfilter_compile(filter_text_h245, &sfcode)) {
388                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, dfilter_error_msg);
389                 return;
390         }
391
392         /* register tap h225 listener */
393         error_string = register_tap_listener("h225", user_data, filter_text_h225,
394                 h225_reset, h225_packet, h225_draw);
395         if (error_string != NULL) {
396                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, error_string->str);
397                         g_string_free(error_string, TRUE);
398                 return;
399         }
400
401         /* register tap h245 listener */
402         error_string = register_tap_listener("h245", user_data, filter_text_h245,
403                 NULL, h245_packet, NULL);
404         if (error_string != NULL) {
405                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, error_string->str);
406                         g_string_free(error_string, TRUE);
407                 return;
408         }
409
410         /* retap all packets */
411         retap_packets(&cfile);
412
413         /* remove tap listener again */
414         protect_thread_critical_region();
415         remove_tap_listener(user_data);
416         remove_tap_listener(user_data);
417         unprotect_thread_critical_region();
418
419 }
420
421
422 /************************************************************************************/
423 /************** Create the dialog box with all widgets ******************************/
424 void create_h225_dialog(user_data_t* user_data)
425 {
426         GtkWidget *window = NULL;
427         GtkWidget *clist_h225;
428         /*GtkWidget *label_stats;*/
429
430         GtkWidget *main_vb;
431         GtkWidget *label;
432         GtkWidget *scrolled_window;
433         GtkWidget *box4, *goto_bt, *close_bt, *refresh_bt;
434         GtkTooltips *tooltips = gtk_tooltips_new();
435
436         gchar label_forward[150];
437
438         gchar str_ip_src[16];
439         gchar str_ip_dst[16];
440
441         /* as multiple analysis windows can be opened, 
442          * don't use window_new_with_geom(), as that will place them on top of each other! */
443         window = window_new(GTK_WINDOW_TOPLEVEL, "Ethereal: H.323 VoIP Analysis");
444         gtk_window_set_default_size(GTK_WINDOW(window), 700, 350);
445
446         /* Container for each row of widgets */
447         main_vb = gtk_vbox_new(FALSE, 2);
448         gtk_container_add(GTK_CONTAINER(window), main_vb);
449         gtk_container_set_border_width(GTK_CONTAINER(main_vb), 2);
450         gtk_widget_show(main_vb);
451
452         strcpy(str_ip_src, ip_to_str((ip_addr_p)&user_data->ip_src));
453         strcpy(str_ip_dst, ip_to_str((ip_addr_p)&user_data->ip_dst));
454
455         g_snprintf(label_forward, 149,
456                 "\nAnalysing H.323 Call between  %s port %u (Side A) and  %s port %u (Side B)\n",
457                 str_ip_src, user_data->port_src, str_ip_dst, user_data->port_dst);
458
459         /* label */
460         label = gtk_label_new(label_forward);
461         gtk_box_pack_start (GTK_BOX (main_vb), label, FALSE, FALSE, 0);
462
463         /* scrolled window */
464         scrolled_window = scrolled_window_new (NULL, NULL);
465         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
466                                         GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
467         gtk_box_pack_start (GTK_BOX (main_vb), scrolled_window, TRUE, TRUE, 0);
468
469         /* place for some statistics */
470         /*label_stats = gtk_label_new("\n");*/
471         /*gtk_box_pack_start(GTK_BOX(main_vb), label_stats, FALSE, FALSE, 0);*/
472
473         /* packet clist */
474         clist_h225 = gtk_clist_new_with_titles(7, titles);
475         gtk_container_add(GTK_CONTAINER(scrolled_window), clist_h225);
476         gtk_widget_show(clist_h225);
477         SIGNAL_CONNECT(clist_h225, "select_row", on_clist_select_row, user_data);
478
479         /* column widths and justification */
480         gtk_clist_set_column_width(GTK_CLIST(clist_h225), 0, 45);
481         gtk_clist_set_column_width(GTK_CLIST(clist_h225), 1, 90);
482         gtk_clist_set_column_width(GTK_CLIST(clist_h225), 2, 90);
483         gtk_clist_set_column_width(GTK_CLIST(clist_h225), 3, 140);
484         gtk_clist_set_column_width(GTK_CLIST(clist_h225), 4, 60);
485         gtk_clist_set_column_width(GTK_CLIST(clist_h225), 5, 140);
486         gtk_clist_set_column_justification(GTK_CLIST(clist_h225), 0, GTK_JUSTIFY_CENTER);
487         gtk_clist_set_column_justification(GTK_CLIST(clist_h225), 1, GTK_JUSTIFY_CENTER);
488         gtk_clist_set_column_justification(GTK_CLIST(clist_h225), 2, GTK_JUSTIFY_CENTER);
489         gtk_clist_set_column_justification(GTK_CLIST(clist_h225), 3, GTK_JUSTIFY_LEFT);
490         gtk_clist_set_column_justification(GTK_CLIST(clist_h225), 4, GTK_JUSTIFY_CENTER);
491         gtk_clist_set_column_justification(GTK_CLIST(clist_h225), 5, GTK_JUSTIFY_LEFT);
492         gtk_clist_set_column_justification(GTK_CLIST(clist_h225), 6, GTK_JUSTIFY_CENTER);
493
494         gtk_widget_show(scrolled_window);
495
496         /* buttons */
497         box4 = gtk_hbutton_box_new();
498         gtk_box_pack_start(GTK_BOX(main_vb), box4, FALSE, FALSE, 0);
499         gtk_container_set_border_width(GTK_CONTAINER(box4), 10);
500         gtk_button_box_set_layout(GTK_BUTTON_BOX (box4), GTK_BUTTONBOX_END);
501         gtk_button_box_set_spacing(GTK_BUTTON_BOX (box4), 10);
502         gtk_button_box_set_child_ipadding(GTK_BUTTON_BOX (box4), 4, 0);
503         gtk_widget_show(box4);
504
505         refresh_bt = BUTTON_NEW_FROM_STOCK(GTK_STOCK_REFRESH);
506         gtk_container_add(GTK_CONTAINER(box4), refresh_bt);
507         gtk_widget_show(refresh_bt);
508         SIGNAL_CONNECT(refresh_bt, "clicked", on_refresh_bt_clicked, user_data);
509         gtk_tooltips_set_tip (tooltips, refresh_bt, "Refresh data", NULL);
510
511         goto_bt = BUTTON_NEW_FROM_STOCK(GTK_STOCK_JUMP_TO);
512         gtk_container_add(GTK_CONTAINER(box4), goto_bt);
513         gtk_widget_show(goto_bt);
514         SIGNAL_CONNECT(goto_bt, "clicked", on_goto_bt_clicked, user_data);
515         gtk_tooltips_set_tip (tooltips, goto_bt, "Jump to the selected packet", NULL);
516
517         close_bt = BUTTON_NEW_FROM_STOCK(GTK_STOCK_CLOSE);
518         gtk_container_add(GTK_CONTAINER(box4), close_bt);
519         GTK_WIDGET_SET_FLAGS(close_bt, GTK_CAN_DEFAULT);
520         gtk_widget_show(close_bt);
521         gtk_tooltips_set_tip (tooltips, close_bt, "Close this dialog", NULL);
522         window_set_cancel_button(window, close_bt, window_cancel_button_cb);
523
524         SIGNAL_CONNECT(window, "delete_event", window_delete_event_cb, NULL);
525         SIGNAL_CONNECT(window, "destroy", on_destroy, user_data);
526
527         gtk_widget_show_all(window);
528         window_present(window);
529
530         /* some widget references need to be saved for outside use */
531         user_data->window = window;
532         user_data->clist1 = GTK_CLIST(clist_h225);
533         user_data->label_stats = label;
534         user_data->selected_clist1 = GTK_CLIST(clist_h225);
535         user_data->selected_row = 0;
536 }
537
538
539 /****************************************************************************/
540 void h323_analysis(
541                 guint32 ip_src,
542                 guint16 port_src,
543                 guint32 ip_dst,
544                 guint16 port_dst,
545                 guint32 ip_src_h245,
546                 guint16 port_src_h245,
547                 guint16 transport
548                 )
549 {
550         user_data_t *user_data;
551
552         /* init */
553         user_data = g_malloc(sizeof(user_data_t));
554
555         user_data->ip_src = ip_src;
556         user_data->port_src = port_src;
557         user_data->ip_dst = ip_dst;
558         user_data->port_dst = port_dst;
559         user_data->ip_src_h245 = ip_src_h245;
560         user_data->port_src_h245 = port_src_h245;
561         user_data->transport = transport;
562
563         /* create the dialog box */
564         create_h225_dialog(user_data);
565
566         /* proceed as if the Refresh button would have been pressed */
567         on_refresh_bt_clicked(NULL, user_data);
568 }
569
570