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