Fix some source headers, reformat SPDX license lines in comment block.
[metze/wireshark/wip.git] / ui / tap-iax2-analysis.c
1 /* tap-iax2-analysis.c
2  * IAX2 analysis addition for Wireshark
3  *
4  * based on rtp_analysis.c
5  * Copyright 2003, Alcatel Business Systems
6  * By Lars Ruoff <lars.ruoff@gmx.net>
7  *
8  * based on tap_rtp.c
9  * Copyright 2003, Iskratel, Ltd, Kranj
10  * By Miha Jemec <m.jemec@iskratel.si>
11  *
12  * Wireshark - Network traffic analyzer
13  * By Gerald Combs <gerald@wireshark.org>
14  * Copyright 1998 Gerald Combs
15  *
16  * SPDX-License-Identifier: GPL-2.0-or-later
17  */
18
19 #include "config.h"
20
21 #include <math.h>
22
23 #include <glib.h>
24
25 #include <epan/dissectors/packet-iax2.h>
26
27 #include "tap-iax2-analysis.h"
28
29 /****************************************************************************/
30 /* This comes from tap-rtp-common.c */
31 /****************************************************************************/
32
33     void
34 iax2_packet_analyse(tap_iax2_stat_t *statinfo,
35         packet_info *pinfo,
36         const struct _iax2_info_t *iax2info)
37 {
38     double current_time;
39     double current_jitter;
40     double current_diff;
41
42     statinfo->flags = 0;
43     /* check payload type */
44     if (iax2info->ftype == AST_FRAME_VOICE) {
45         if (iax2info->csub != statinfo->pt)
46             statinfo->flags |= STAT_FLAG_PT_CHANGE;
47         statinfo->pt = iax2info->csub;
48     }
49
50     /* store the current time and calculate the current jitter */
51     current_time = nstime_to_sec(&pinfo->rel_ts);
52     current_diff = fabs (current_time - statinfo->time - (((double)iax2info->timestamp - (double)statinfo->timestamp)/1000));
53     current_jitter = statinfo->jitter + ( current_diff - statinfo->jitter)/16;
54     statinfo->delta = current_time - (statinfo->time);
55     statinfo->jitter = current_jitter;
56     statinfo->diff = current_diff;
57
58     /* calculate the BW in Kbps adding the IP+IAX2 header to the RTP -> 20bytes(IP)+ 4bytes(Mini) = 24bytes */
59     statinfo->bw_history[statinfo->bw_index].bytes = iax2info->payload_len + 24;
60     statinfo->bw_history[statinfo->bw_index].time = current_time;
61     /* check if there are more than 1sec in the history buffer to calculate BW in bps. If so, remove those for the calculation */
62     while ((statinfo->bw_history[statinfo->bw_start_index].time+1) < current_time) {
63         statinfo->total_bytes -= statinfo->bw_history[statinfo->bw_start_index].bytes;
64         statinfo->bw_start_index++;
65         if (statinfo->bw_start_index == BUFF_BW) statinfo->bw_start_index = 0;
66     };
67     statinfo->total_bytes += iax2info->payload_len + 24;
68     statinfo->bandwidth = (double)(statinfo->total_bytes*8)/1000;
69     statinfo->bw_index++;
70     if (statinfo->bw_index == BUFF_BW) statinfo->bw_index = 0;
71
72
73     /*  is this the first packet we got in this direction? */
74     if (statinfo->first_packet) {
75         statinfo->start_seq_nr = 0;
76         statinfo->start_time = current_time;
77         statinfo->delta = 0;
78         statinfo->jitter = 0;
79         statinfo->diff = 0;
80         statinfo->flags |= STAT_FLAG_FIRST;
81         statinfo->first_packet = FALSE;
82     }
83     /* is it a regular packet? */
84     if (!(statinfo->flags & STAT_FLAG_FIRST)
85             && !(statinfo->flags & STAT_FLAG_MARKER)
86             && !(statinfo->flags & STAT_FLAG_PT_CN)
87             && !(statinfo->flags & STAT_FLAG_WRONG_TIMESTAMP)
88             && !(statinfo->flags & STAT_FLAG_FOLLOW_PT_CN)) {
89         /* include it in maximum delta calculation */
90         if (statinfo->delta > statinfo->max_delta) {
91             statinfo->max_delta = statinfo->delta;
92             statinfo->max_nr = pinfo->num;
93         }
94         /* maximum and mean jitter calculation */
95         if (statinfo->jitter > statinfo->max_jitter) {
96             statinfo->max_jitter = statinfo->jitter;
97         }
98         statinfo->mean_jitter = (statinfo->mean_jitter*statinfo->total_nr + current_diff) / (statinfo->total_nr+1);
99     }
100     /* regular payload change? (CN ignored) */
101     if (!(statinfo->flags & STAT_FLAG_FIRST)
102             && !(statinfo->flags & STAT_FLAG_PT_CN)) {
103         if ((statinfo->pt != statinfo->reg_pt)
104                 && (statinfo->reg_pt != PT_UNDEFINED)) {
105             statinfo->flags |= STAT_FLAG_REG_PT_CHANGE;
106         }
107     }
108
109     /* set regular payload*/
110     if (!(statinfo->flags & STAT_FLAG_PT_CN)) {
111         statinfo->reg_pt = statinfo->pt;
112     }
113
114     /* TODO: lost packets / duplicated:  we should infer this from timestamp... */
115     statinfo->time = current_time;
116     statinfo->timestamp = iax2info->timestamp; /* millisecs */
117     statinfo->stop_seq_nr = 0;
118     statinfo->total_nr++;
119
120     return;
121 }
122
123 /*
124  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
125  *
126  * Local variables:
127  * c-basic-offset: 4
128  * tab-width: 8
129  * indent-tabs-mode: nil
130  * End:
131  *
132  * vi: set shiftwidth=4 tabstop=8 expandtab:
133  * :indentSize=4:tabSize=8:noTabs=true:
134  */