2 * RTP streams summary addition for ethereal
6 * Copyright 2003, Alcatel Business Systems
7 * By Lars Ruoff <lars.ruoff@gmx.net>
9 * Ethereal - Network traffic analyzer
10 * By Gerald Combs <gerald@ethereal.com>
11 * Copyright 1998 Gerald Combs
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.
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.
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.
32 #include "rtp_stream.h"
33 #include "rtp_stream_dlg.h"
39 #include <epan/dissectors/packet-rtp.h>
42 #include "alert_box.h"
43 #include "simple_dialog.h"
49 #ifdef HAVE_SYS_TYPES_H
50 # include <sys/types.h>
54 #include <epan/addr_resolv.h>
57 /****************************************************************************/
58 /* the one and only global rtpstream_tapinfo_t structure */
59 static rtpstream_tapinfo_t the_tapinfo_struct =
60 {0, NULL, 0, TAP_ANALYSE, NULL, NULL, NULL, 0, FALSE};
63 /****************************************************************************/
64 /* GCompareFunc style comparison function for _rtp_stream_info */
65 static gint rtp_stream_info_cmp(gconstpointer aa, gconstpointer bb)
67 const struct _rtp_stream_info* a = aa;
68 const struct _rtp_stream_info* b = bb;
72 if (a==NULL || b==NULL)
74 if (ADDRESSES_EQUAL(&(a->src_addr), &(b->src_addr))
75 && (a->src_port == b->src_port)
76 && ADDRESSES_EQUAL(&(a->dest_addr), &(b->dest_addr))
77 && (a->dest_port == b->dest_port)
78 && (a->ssrc == b->ssrc))
85 /****************************************************************************/
86 /* when there is a [re]reading of packet's */
87 void rtpstream_reset(rtpstream_tapinfo_t *tapinfo)
91 if (tapinfo->mode == TAP_ANALYSE) {
92 /* free the data items first */
93 list = g_list_first(tapinfo->strinfo_list);
97 list = g_list_next(list);
99 g_list_free(tapinfo->strinfo_list);
100 tapinfo->strinfo_list = NULL;
101 tapinfo->nstreams = 0;
102 tapinfo->npackets = 0;
105 ++(tapinfo->launch_count);
110 static void rtpstream_reset_cb(void *arg)
112 rtpstream_reset(arg);
115 /****************************************************************************/
116 /* redraw the output */
117 static void rtpstream_draw(void *arg _U_)
119 /* XXX: see rtpstream_on_update in rtp_streams_dlg.c for comments
120 gtk_signal_emit_by_name(top_level, "signal_rtpstream_update");
122 rtpstream_dlg_update(the_tapinfo_struct.strinfo_list);
128 * rtpdump file format
130 * The file starts with the tool to be used for playing this file,
131 * the multicast/unicast receive address and the port.
133 * #!rtpplay1.0 224.2.0.1/3456\n
135 * This is followed by one binary header (RD_hdr_t) and one RD_packet_t
136 * structure for each received packet. All fields are in network byte
137 * order. We don't need the source IP address since we can do mapping
138 * based on SSRC. This saves (a little) space, avoids non-IPv4
139 * problems and privacy/security concerns. The header is followed by
140 * the RTP/RTCP header and (optionally) the actual payload.
143 #define RTPFILE_VERSION "1.0"
146 * Write a header to the current output file.
147 * The header consists of an identifying string, followed
148 * by a binary structure.
150 static void rtp_write_header(rtp_stream_info_t *strinfo, FILE *file)
152 guint32 start_sec; /* start of recording (GMT) (seconds) */
153 guint32 start_usec; /* start of recording (GMT) (microseconds)*/
154 guint32 source; /* network source (multicast address) */
155 guint16 port; /* UDP port */
156 guint16 padding; /* 2 padding bytes */
158 fprintf(file, "#!rtpplay%s %s/%u\n", RTPFILE_VERSION,
159 get_addr_name(&(strinfo->dest_addr)),
162 start_sec = g_htonl(strinfo->start_sec);
163 start_usec = g_htonl(strinfo->start_usec);
164 source = *(strinfo->src_addr.data); /* rtpdump only accepts guint32 as source, will be fake for IPv6 */
165 port = g_htons(strinfo->src_port);
168 fwrite(&start_sec, 4, 1, file);
169 fwrite(&start_usec, 4, 1, file);
170 fwrite(&source, 4, 1, file);
171 fwrite(&port, 2, 1, file);
172 fwrite(&padding, 2, 1, file);
175 /* utility function for writing a sample to file in rtpdump -F dump format (.rtp)*/
176 static void rtp_write_sample(rtp_sample_t* sample, FILE* file)
178 guint16 length; /* length of packet, including this header (may
179 be smaller than plen if not whole packet recorded) */
180 guint16 plen; /* actual header+payload length for RTP, 0 for RTCP */
181 guint32 offset; /* milliseconds since the start of recording */
183 length = g_htons(sample->header.frame_length + 8);
184 plen = g_htons(sample->header.frame_length);
185 offset = g_htonl(sample->header.rec_time);
187 fwrite(&length, 2, 1, file);
188 fwrite(&plen, 2, 1, file);
189 fwrite(&offset, 4, 1, file);
190 fwrite(sample->frame, sample->header.frame_length, 1, file);
194 /****************************************************************************/
195 /* whenever a RTP packet is seen by the tap listener */
196 static int rtpstream_packet(void *arg, packet_info *pinfo, epan_dissect_t *edt _U_, const void *arg2)
198 rtpstream_tapinfo_t *tapinfo = arg;
199 const struct _rtp_info *rtpinfo = arg2;
200 rtp_stream_info_t tmp_strinfo;
201 rtp_stream_info_t *strinfo = NULL;
205 struct _rtp_conversation_info *p_conv_data = NULL;
207 /* gather infos on the stream this packet is part of */
208 COPY_ADDRESS(&(tmp_strinfo.src_addr), &(pinfo->src));
209 tmp_strinfo.src_port = pinfo->srcport;
210 COPY_ADDRESS(&(tmp_strinfo.dest_addr), &(pinfo->dst));
211 tmp_strinfo.dest_port = pinfo->destport;
212 tmp_strinfo.ssrc = rtpinfo->info_sync_src;
213 tmp_strinfo.pt = rtpinfo->info_payload_type;
215 if (tapinfo->mode == TAP_ANALYSE) {
216 /* check wether we already have a stream with these parameters in the list */
217 list = g_list_first(tapinfo->strinfo_list);
220 if (rtp_stream_info_cmp(&tmp_strinfo, (rtp_stream_info_t*)(list->data))==0)
222 strinfo = (rtp_stream_info_t*)(list->data); /*found!*/
225 list = g_list_next(list);
228 /* not in the list? then create a new entry */
230 tmp_strinfo.npackets = 0;
231 tmp_strinfo.first_frame_num = pinfo->fd->num;
232 tmp_strinfo.start_sec = pinfo->fd->abs_ts.secs;
233 tmp_strinfo.start_usec = pinfo->fd->abs_ts.nsecs/1000;
234 tmp_strinfo.start_rel_sec = pinfo->fd->rel_ts.secs;
235 tmp_strinfo.start_rel_usec = pinfo->fd->rel_ts.nsecs/1000;
236 tmp_strinfo.tag_vlan_error = 0;
237 tmp_strinfo.tag_diffserv_error = 0;
238 tmp_strinfo.vlan_id = 0;
239 tmp_strinfo.problem = FALSE;
241 /* reset RTP stats */
242 tmp_strinfo.rtp_stats.first_packet = TRUE;
243 tmp_strinfo.rtp_stats.max_delta = 0;
244 tmp_strinfo.rtp_stats.max_jitter = 0;
245 tmp_strinfo.rtp_stats.mean_jitter = 0;
246 tmp_strinfo.rtp_stats.delta = 0;
247 tmp_strinfo.rtp_stats.diff = 0;
248 tmp_strinfo.rtp_stats.jitter = 0;
249 tmp_strinfo.rtp_stats.bandwidth = 0;
250 tmp_strinfo.rtp_stats.total_bytes = 0;
251 tmp_strinfo.rtp_stats.bw_start_index = 0;
252 tmp_strinfo.rtp_stats.bw_index = 0;
253 tmp_strinfo.rtp_stats.timestamp = 0;
254 tmp_strinfo.rtp_stats.max_nr = 0;
255 tmp_strinfo.rtp_stats.total_nr = 0;
256 tmp_strinfo.rtp_stats.sequence = 0;
257 tmp_strinfo.rtp_stats.start_seq_nr = 0;
258 tmp_strinfo.rtp_stats.stop_seq_nr = 0;
259 tmp_strinfo.rtp_stats.cycles = 0;
260 tmp_strinfo.rtp_stats.under = FALSE;
261 tmp_strinfo.rtp_stats.start_time = 0;
262 tmp_strinfo.rtp_stats.time = 0;
263 tmp_strinfo.rtp_stats.reg_pt = PT_UNDEFINED;
265 /* Get the Setup frame number who set this RTP stream */
266 p_conv_data = p_get_proto_data(pinfo->fd, proto_get_id_by_filter_name("rtp"));
268 tmp_strinfo.setup_frame_number = p_conv_data->frame_number;
270 tmp_strinfo.setup_frame_number = 0xFFFFFFFF;
272 strinfo = g_malloc(sizeof(rtp_stream_info_t));
273 *strinfo = tmp_strinfo; /* memberwise copy of struct */
274 tapinfo->strinfo_list = g_list_append(tapinfo->strinfo_list, strinfo);
277 /* get RTP stats for the packet */
278 rtp_packet_analyse(&(strinfo->rtp_stats), pinfo, rtpinfo);
279 if (strinfo->rtp_stats.flags & STAT_FLAG_WRONG_TIMESTAMP
280 || strinfo->rtp_stats.flags & STAT_FLAG_WRONG_SEQ)
281 strinfo->problem = TRUE;
284 /* increment the packets counter for this stream */
285 ++(strinfo->npackets);
286 strinfo->stop_rel_sec = pinfo->fd->rel_ts.secs;
287 strinfo->stop_rel_usec = pinfo->fd->rel_ts.nsecs/1000;
289 /* increment the packets counter of all streams */
290 ++(tapinfo->npackets);
292 return 1; /* refresh output */
294 else if (tapinfo->mode == TAP_SAVE) {
295 if (rtp_stream_info_cmp(&tmp_strinfo, tapinfo->filter_stream_fwd)==0) {
296 /* XXX - what if rtpinfo->info_all_data_present is
297 FALSE, so that we don't *have* all the data? */
298 sample.header.rec_time =
299 (pinfo->fd->abs_ts.nsecs/1000 + 1000000 - tapinfo->filter_stream_fwd->start_usec)/1000
300 + (pinfo->fd->abs_ts.secs - tapinfo->filter_stream_fwd->start_sec - 1)*1000;
301 sample.header.frame_length = rtpinfo->info_data_len;
302 sample.frame = rtpinfo->info_data;
303 rtp_write_sample(&sample, tapinfo->save_file);
306 else if (tapinfo->mode == TAP_MARK) {
308 if (rtp_stream_info_cmp(&tmp_strinfo, tapinfo->filter_stream_fwd)==0
309 || rtp_stream_info_cmp(&tmp_strinfo, tapinfo->filter_stream_rev)==0)
311 cf_mark_frame(&cfile, pinfo->fd);
318 /****************************************************************************/
319 /* scan for RTP streams */
320 void rtpstream_scan(void)
322 gboolean was_registered = the_tapinfo_struct.is_registered;
323 if (!the_tapinfo_struct.is_registered)
324 register_tap_listener_rtp_stream();
326 the_tapinfo_struct.mode = TAP_ANALYSE;
327 cf_retap_packets(&cfile);
330 remove_tap_listener_rtp_stream();
334 /****************************************************************************/
335 /* save rtp dump of stream_fwd */
336 gboolean rtpstream_save(rtp_stream_info_t* stream, const gchar *filename)
338 gboolean was_registered = the_tapinfo_struct.is_registered;
339 /* open file for saving */
340 the_tapinfo_struct.save_file = fopen(filename, "wb");
341 if (the_tapinfo_struct.save_file==NULL) {
342 open_failure_alert_box(filename, errno, TRUE);
346 rtp_write_header(stream, the_tapinfo_struct.save_file);
347 if (ferror(the_tapinfo_struct.save_file)) {
348 write_failure_alert_box(filename, errno);
349 fclose(the_tapinfo_struct.save_file);
353 if (!the_tapinfo_struct.is_registered)
354 register_tap_listener_rtp_stream();
356 the_tapinfo_struct.mode = TAP_SAVE;
357 the_tapinfo_struct.filter_stream_fwd = stream;
358 cf_retap_packets(&cfile);
359 the_tapinfo_struct.mode = TAP_ANALYSE;
362 remove_tap_listener_rtp_stream();
364 if (ferror(the_tapinfo_struct.save_file)) {
365 write_failure_alert_box(filename, errno);
366 fclose(the_tapinfo_struct.save_file);
370 if (fclose(the_tapinfo_struct.save_file) == EOF) {
371 write_failure_alert_box(filename, errno);
378 /****************************************************************************/
379 /* mark packets in stream_fwd or stream_rev */
380 void rtpstream_mark(rtp_stream_info_t* stream_fwd, rtp_stream_info_t* stream_rev)
382 gboolean was_registered = the_tapinfo_struct.is_registered;
383 if (!the_tapinfo_struct.is_registered)
384 register_tap_listener_rtp_stream();
386 the_tapinfo_struct.mode = TAP_MARK;
387 the_tapinfo_struct.filter_stream_fwd = stream_fwd;
388 the_tapinfo_struct.filter_stream_rev = stream_rev;
389 cf_retap_packets(&cfile);
390 the_tapinfo_struct.mode = TAP_ANALYSE;
393 remove_tap_listener_rtp_stream();
397 /****************************************************************************/
398 const rtpstream_tapinfo_t* rtpstream_get_info(void)
400 return &the_tapinfo_struct;
404 /****************************************************************************/
406 /****************************************************************************/
408 /* XXX just copied from gtk/rpc_stat.c */
409 void protect_thread_critical_region(void);
410 void unprotect_thread_critical_region(void);
412 /****************************************************************************/
414 remove_tap_listener_rtp_stream(void)
416 if (the_tapinfo_struct.is_registered) {
417 protect_thread_critical_region();
418 remove_tap_listener(&the_tapinfo_struct);
419 unprotect_thread_critical_region();
421 the_tapinfo_struct.is_registered = FALSE;
426 /****************************************************************************/
428 register_tap_listener_rtp_stream(void)
430 GString *error_string;
432 if (!the_tapinfo_struct.is_registered) {
433 error_string = register_tap_listener("rtp", &the_tapinfo_struct,
434 NULL, rtpstream_reset_cb, rtpstream_packet,
437 if (error_string != NULL) {
438 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
440 g_string_free(error_string, TRUE);
444 the_tapinfo_struct.is_registered = TRUE;