We must always return an error code on an error; otherwise, our caller
[obnox/wireshark/wip.git] / tap-rtp.c
1 /* tap-rtp.c
2  * RTP TAP for tshark
3  *
4  * $Id$
5  *
6  * Copyright 2008, Ericsson AB
7  * By Balint Reczey <balint.reczey@ericsson.com>
8  *
9  * based on gtk/rtp_stream_dlg.c
10  * Copyright 2003, Alcatel Business Systems
11  * By Lars Ruoff <lars.ruoff@gmx.net>
12  *
13  * Wireshark - Network traffic analyzer
14  * By Gerald Combs <gerald@wireshark.org>
15  * Copyright 1998 Gerald Combs
16  *
17  * This program is free software; you can redistribute it and/or
18  * modify it under the terms of the GNU General Public License
19  * as published by the Free Software Foundation; either version 2
20  * of the License, or (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
30  */
31
32 /*
33  * This TAP provides statistics for RTP streams
34  */
35
36 #ifdef HAVE_CONFIG_H
37 # include "config.h"
38 #endif
39
40 #include <stdio.h>
41
42 #ifdef HAVE_SYS_TYPES_H
43 # include <sys/types.h>
44 #endif
45
46 #include <string.h>
47 #include <locale.h>
48 #include "epan/packet_info.h"
49 #include "epan/value_string.h"
50 #include <epan/tap.h>
51 #include <epan/rtp_pt.h>
52 #include <epan/stat_cmd_args.h>
53 #include <epan/addr_resolv.h>
54 #include "tap-rtp-common.h"
55
56 /* The one and only global rtpstream_tapinfo_t structure for tshark and wireshark.
57  */
58 static rtpstream_tapinfo_t the_tapinfo_struct =
59         {0, NULL, 0, TAP_ANALYSE, NULL, NULL, NULL, 0, FALSE};
60
61 static void
62 rtp_streams_stat_draw(void *arg _U_)
63 {
64
65
66     GList *list;
67     rtp_stream_info_t* strinfo;
68     gchar *payload_type;
69     guint32 expected;
70     gint32 lost;
71     double perc;
72     char *savelocale;
73
74     printf("========================= RTP Streams ========================\n");
75     printf("%15s %5s %15s %5s %10s %16s %5s %12s %15s %15s %15s %s\n","Src IP addr", "Port",  "Dest IP addr", "Port", "SSRC", "Payload", "Pkts", "Lost", "Max Delta(ms)", "Max Jitter(ms)", "Mean Jitter(ms)", "Problems?");
76
77     /* save the current locale */
78     savelocale = setlocale(LC_NUMERIC, NULL);
79     /* switch to "C" locale to avoid problems with localized decimal separators
80        in g_snprintf("%f") functions */
81     setlocale(LC_NUMERIC, "C");
82
83     list = the_tapinfo_struct.strinfo_list;
84
85     list = g_list_first(list);
86     while (list)
87     {
88         strinfo = (rtp_stream_info_t*)(list->data);
89
90         /* payload type */
91         if(strinfo->pt>95){
92         if(strinfo->info_payload_type_str != NULL){
93             payload_type = g_strdup(strinfo->info_payload_type_str);
94         }else{
95             payload_type = g_strdup_printf("Unknown(%u)",strinfo->pt);
96         }
97
98         }else{
99             payload_type = g_strdup(val_to_str_ext(strinfo->pt, &rtp_payload_type_vals_ext,
100                 "Unknown (%u)"));
101         }
102
103         /* packet count, lost packets */
104         expected = (strinfo->rtp_stats.stop_seq_nr + strinfo->rtp_stats.cycles*65536)
105             - strinfo->rtp_stats.start_seq_nr + 1;
106         lost = expected - strinfo->rtp_stats.total_nr;
107         if (expected){
108             perc = (double)(lost*100)/(double)expected;
109         } else {
110             perc = 0;
111         }
112
113         printf("%15s %5u %15s %5u 0x%08X %16s %5u %5d (%.1f%%) %15.2f %15.2f %15.2f %s\n",
114             get_addr_name(&(strinfo->src_addr)),
115             strinfo->src_port,
116             get_addr_name(&(strinfo->dest_addr)),
117             strinfo->dest_port,
118             strinfo->ssrc,
119             payload_type,
120             strinfo->npackets,
121             lost, perc,
122             strinfo->rtp_stats.max_delta,
123             strinfo->rtp_stats.max_jitter,
124             strinfo->rtp_stats.mean_jitter,
125             (strinfo->problem)?"X":"");
126
127         list = g_list_next(list);
128
129
130     }
131
132     printf("==============================================================\n");
133     /* restore previous locale setting */
134     setlocale(LC_NUMERIC, savelocale);
135 }
136
137
138 static void
139 rtp_streams_stat_init(const char *optarg _U_, void* userdata _U_)
140 {
141     GString             *err_p;
142
143     err_p =
144         register_tap_listener("rtp", &the_tapinfo_struct, NULL, 0,
145             rtpstream_reset_cb,
146             rtpstream_packet,
147             rtp_streams_stat_draw);
148
149     if (err_p != NULL)
150     {
151         g_string_free(err_p, TRUE);
152
153         exit(1);
154     }
155 }
156
157
158 void
159 register_tap_listener_rtp_streams(void)
160 {
161     register_stat_cmd_arg("rtp,streams", rtp_streams_stat_init,NULL);
162 }