forked from sipcapture/hep-c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser_rtcp.c
217 lines (171 loc) · 5.53 KB
/
parser_rtcp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
* $Id$
*
* captagent - Homer capture agent. Modular
* Duplicate SIP messages in Homer Encapulate Protocol [HEP] [ipv6 version]
*
* Author: Alexandr Dubovikov <[email protected]>
* (C) Homer Project 2012-2014 (http://www.sipcapture.org)
*
* Homer capture agent is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version
*
* Homer capture agent is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <stdio.h>
#include "log.h"
#include "parser_rtcp.h"
int check_rtcp_version (char *packet, int len) {
if(packet == NULL || len == 0) return -1;
rtcp_header_t *rtcp = (rtcp_header_t *)packet;
if(rtcp->version != 2)
{
LERR("wrong version\n");
return -2;
}
if(rtcp->type >= RTCP_SR && rtcp->type <= RTCP_SDES) {
return 1;
}
return -3;
}
int check_rtp_version(char *packet, int len) {
if(packet == NULL || len == 0) return -1;
rtcp_header_t *rtcp = (rtcp_header_t *)packet;
if(rtcp->version != 2)
{
LERR("wrong version\n");
return -2;
}
return 1;
}
int capt_parse_rtcp(char *packet, int len, char *json_buffer, int buffer_len) {
// check parameters
if(packet == NULL || len == 0) return -1;
rtcp_header_t *rtcp = (rtcp_header_t *)packet;
int ret = 0, flag = 0;
ret += snprintf(json_buffer, buffer_len, "{ ");
int pno = 0, total = len;
LDEBUG("Parsing compound packet (total of %d bytes)\n", total);
while(rtcp) {
pno++;
switch(rtcp->type) {
/* SR, sender report */
case RTCP_SR: {
LDEBUG("#%d SR (200)\n", pno);
rtcp_sr_t *sr = (rtcp_sr_t*)rtcp;
ret += snprintf(json_buffer+ret, buffer_len - ret, SENDER_REPORT_JSON,
sender_info_get_ntp_timestamp_msw(&sr->si),
sender_info_get_ntp_timestamp_lsw(&sr->si),
sender_info_get_octet_count(&sr->si),
sender_info_get_rtp_timestamp(&sr->si),
sender_info_get_packet_count(&sr->si));
if(sr->header.rc > 0) {
ret += snprintf(json_buffer+ret, buffer_len - ret, REPORT_BLOCK_JSON,
ntohl(sr->ssrc), rtcp->type,
report_block_get_identifier(&sr->rb[0]),
report_block_get_high_ext_seq(&sr->rb[0]),
report_block_get_fraction_lost(&sr->rb[0]),
report_block_get_interarrival_jitter(&sr->rb[0]),
report_block_get_cum_packet_loss(&sr->rb[0]),
report_block_get_last_SR_time(&sr->rb[0]),
report_block_get_last_SR_delay(&sr->rb[0]));
}
break;
}
/* RR, receiver report */
case RTCP_RR: {
LDEBUG("#%d RR (201)\n", pno);
rtcp_rr_t *rr = (rtcp_rr_t*)rtcp;
if(rr->header.rc > 0) {
ret += snprintf(json_buffer+ret, buffer_len - ret, REPORT_BLOCK_JSON,
ntohl(rr->ssrc),
rtcp->type,
report_block_get_identifier(&rr->rb[0]),
report_block_get_high_ext_seq(&rr->rb[0]),
report_block_get_fraction_lost(&rr->rb[0]),
report_block_get_interarrival_jitter(&rr->rb[0]),
report_block_get_cum_packet_loss(&rr->rb[0]),
report_block_get_last_SR_time(&rr->rb[0]),
report_block_get_last_SR_delay(&rr->rb[0]));
}
break;
}
/* SDES, source description */
case RTCP_SDES: {
LDEBUG("#%d SDES (202)\n", pno);
/* if not needed send sdes */
if(!send_sdes) break;
int items;
rtcp_sdes_t *sdes = (rtcp_sdes_t*)rtcp;
rtcp_sdes_item_t *end = (rtcp_sdes_item_t *)
((uint32_t *)rtcp + ntohs(rtcp->length) + 1);
rtcp_sdes_item_t *rsp, *rspn;
ret += snprintf(json_buffer+ret, buffer_len - ret, SDES_REPORT_BEGIN_JSON,
ntohl(sdes->csrc), sdes->header.rc);
rsp = &sdes->item[0];
if (rsp >= end) break;
for (items = 0; rsp->type; rsp = rspn ) {
rspn = (rtcp_sdes_item_t *)((char*)rsp+rsp->len+2);
if (rspn >= end) {
rsp = rspn;
break;
}
ret += snprintf(json_buffer+ret, buffer_len - ret, SDES_REPORT_INFO_JSON,
rsp->type, rsp->len, rsp->content);
items++;
}
/* cut , off */
if (items) ret -= 1;
ret += snprintf(json_buffer+ret, buffer_len - ret, "],");
break;
}
/* BYE, Goodbye */
case RTCP_BYE: {
LDEBUG("#%d BYE (203)\n", pno);
flag = 1;
//rtcp_bye_t *bye = (rtcp_bye_t*)rtcp;
break;
}
/* APP, Application-defined */
case RTCP_APP: {
LDEBUG("#%d APP (204)\n", pno);
flag = 1;
//rtcp_app_t *app = (rtcp_app_t*)rtcp;
break;
}
default:
break;
}
int length = ntohs(rtcp->length);
if(length == 0) {
break;
}
total -= length*4+4;
if(total <= 0) {
LDEBUG("End of RTCP packet\n");
break;
}
rtcp = (rtcp_header_t *)((uint32_t*)rtcp + length + 1);
}
/* Bad parsed message or BYE/APP packet */
if(ret < 10) {
if(flag == 0) // BAD PARSING
return -2;
// else: BYE or APP packet
return 0;
}
/* replace last comma */
json_buffer[ret-1]='}';
//ret += snprintf(json_buffer + ret - 1, buffer_len - ret + 1, "}");
return ret;
}