efca6b83fc145e6de15f04e49ab613296ddd2a60
[obnox/wireshark/wip.git] / gtk / rtp_stream.c
1 /* rtp_stream.c
2  * RTP streams summary addition for Wireshark
3  *
4  * $Id$
5  *
6  * Copyright 2003, Alcatel Business Systems
7  * By Lars Ruoff <lars.ruoff@gmx.net>
8  *
9  * Wireshark - Network traffic analyzer
10  * By Gerald Combs <gerald@wireshark.org>
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 #include <string.h>
32
33 #ifdef HAVE_FCNTL_H
34 #include <fcntl.h>
35 #endif
36
37 #ifdef HAVE_SYS_TYPES_H
38 # include <sys/types.h>
39 #endif
40
41
42 #include <epan/epan.h>
43 #include <epan/packet.h>
44 #include <epan/tap.h>
45 #include <epan/dissectors/packet-rtp.h>
46 #include <epan/addr_resolv.h>
47
48 #include "../globals.h"
49 #include "../register.h"
50 #include "../alert_box.h"
51 #include "../simple_dialog.h"
52 #include "../tap-rtp-common.h"
53 #include <wsutil/file_util.h>
54
55 #include "gtk/rtp_stream.h"
56 #include "gtk/rtp_stream_dlg.h"
57 #include "gtk/main.h"
58
59
60 /****************************************************************************/
61 /* redraw the output */
62 static void rtpstream_draw(void *arg _U_)
63 {
64 /* XXX: see rtpstream_on_update in rtp_streams_dlg.c for comments
65         g_signal_emit_by_name(top_level, "signal_rtpstream_update");
66 */
67         rtpstream_dlg_update(the_tapinfo_struct.strinfo_list);
68         return;
69 }
70
71
72 /****************************************************************************/
73 /* scan for RTP streams */
74 void rtpstream_scan(void)
75 {
76         gboolean was_registered = the_tapinfo_struct.is_registered;
77         if (!the_tapinfo_struct.is_registered)
78                 register_tap_listener_rtp_stream();
79
80         the_tapinfo_struct.mode = TAP_ANALYSE;
81         cf_retap_packets(&cfile, FALSE);
82
83         if (!was_registered)
84                 remove_tap_listener_rtp_stream();
85 }
86
87
88 /****************************************************************************/
89 /* save rtp dump of stream_fwd */
90 gboolean rtpstream_save(rtp_stream_info_t* stream, const gchar *filename)
91 {
92         gboolean was_registered = the_tapinfo_struct.is_registered;
93         /* open file for saving */
94         the_tapinfo_struct.save_file = ws_fopen(filename, "wb");
95         if (the_tapinfo_struct.save_file==NULL) {
96                 open_failure_alert_box(filename, errno, TRUE);
97                 return FALSE;
98         }
99
100         rtp_write_header(stream, the_tapinfo_struct.save_file);
101         if (ferror(the_tapinfo_struct.save_file)) {
102                 write_failure_alert_box(filename, errno);
103                 fclose(the_tapinfo_struct.save_file);
104                 return FALSE;
105         }
106
107         if (!the_tapinfo_struct.is_registered)
108                 register_tap_listener_rtp_stream();
109
110         the_tapinfo_struct.mode = TAP_SAVE;
111         the_tapinfo_struct.filter_stream_fwd = stream;
112         cf_retap_packets(&cfile, FALSE);
113         the_tapinfo_struct.mode = TAP_ANALYSE;
114
115         if (!was_registered)
116                 remove_tap_listener_rtp_stream();
117
118         if (ferror(the_tapinfo_struct.save_file)) {
119                 write_failure_alert_box(filename, errno);
120                 fclose(the_tapinfo_struct.save_file);
121                 return FALSE;
122         }
123
124         if (fclose(the_tapinfo_struct.save_file) == EOF) {
125                 write_failure_alert_box(filename, errno);
126                 return FALSE;
127         }
128         return TRUE;
129 }
130
131
132 /****************************************************************************/
133 /* mark packets in stream_fwd or stream_rev */
134 void rtpstream_mark(rtp_stream_info_t* stream_fwd, rtp_stream_info_t* stream_rev)
135 {
136         gboolean was_registered = the_tapinfo_struct.is_registered;
137         if (!the_tapinfo_struct.is_registered)
138                 register_tap_listener_rtp_stream();
139
140         the_tapinfo_struct.mode = TAP_MARK;
141         the_tapinfo_struct.filter_stream_fwd = stream_fwd;
142         the_tapinfo_struct.filter_stream_rev = stream_rev;
143         cf_retap_packets(&cfile, FALSE);
144         the_tapinfo_struct.mode = TAP_ANALYSE;
145
146         if (!was_registered)
147                 remove_tap_listener_rtp_stream();
148 }
149
150
151 /****************************************************************************/
152 const rtpstream_tapinfo_t* rtpstream_get_info(void)
153 {
154         return &the_tapinfo_struct;
155 }
156
157
158 /****************************************************************************/
159 /* TAP INTERFACE */
160 /****************************************************************************/
161
162 /****************************************************************************/
163 void
164 remove_tap_listener_rtp_stream(void)
165 {
166         if (the_tapinfo_struct.is_registered) {
167                 protect_thread_critical_region();
168                 remove_tap_listener(&the_tapinfo_struct);
169                 unprotect_thread_critical_region();
170
171                 the_tapinfo_struct.is_registered = FALSE;
172         }
173 }
174
175
176 /****************************************************************************/
177 void
178 register_tap_listener_rtp_stream(void)
179 {
180         GString *error_string;
181
182         if (!the_tapinfo_struct.is_registered) {
183                 error_string = register_tap_listener("rtp", &the_tapinfo_struct,
184                         NULL, rtpstream_reset_cb, rtpstream_packet,
185                         rtpstream_draw);
186
187                 if (error_string != NULL) {
188                         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
189                                       error_string->str);
190                         g_string_free(error_string, TRUE);
191                         exit(1);
192                 }
193
194                 the_tapinfo_struct.is_registered = TRUE;
195         }
196 }