@@ -2,6 +2,7 @@ package protos
2
2
3
3
import (
4
4
"encoding/binary"
5
+ "encoding/hex"
5
6
"encoding/json"
6
7
"fmt"
7
8
@@ -177,32 +178,37 @@ func (rp *RTCP_Packet) MarshalJSON() ([]byte, error) {
177
178
func ParseRTCP (data []byte ) ([]byte , error ) {
178
179
dataLen := len (data )
179
180
if dataLen < 28 {
180
- return nil , fmt .Errorf ("Useless data inside RTCP packet='%s' length=%d" , string (data ), dataLen )
181
+ return nil , fmt .Errorf ("Fishy RTCP packet length=%d in packet: \n %v \n " , dataLen , hex . Dump (data ))
181
182
}
183
+ var err error
182
184
pkt := & RTCP_Packet {}
185
+ rtcpPkt := []byte {}
183
186
offset := 0
184
187
185
188
for dataLen > 0 {
186
189
if dataLen < 4 || dataLen > 576 {
187
- return nil , fmt .Errorf ("Fishy RTCP packet=%v length=%d" , data , dataLen )
190
+ return nil , fmt .Errorf ("Fishy RTCP packet length=%d in packet: \n %v \n " , dataLen , hex . Dump ( data ) )
188
191
}
189
192
190
193
//version := (data[offset] & 0xc0) >> 6
191
194
//padding := (data[offset] & 0x20) >> 5
192
- receptionReportCount := data [offset ] & 0x1f
193
- RTCPType := data [offset + 1 ]
194
- RTCPLength := binary .BigEndian .Uint16 (data [offset + 2 :]) * 4
195
-
195
+ receptionReportCount := int (data [offset ] & 0x1f )
196
+ RTCPType := int (data [offset + 1 ])
197
+ RTCPLength := int (binary .BigEndian .Uint16 (data [offset + 2 :]) * 4 )
196
198
offset += 4
197
199
198
200
if receptionReportCount < 0 || receptionReportCount > 4 {
199
- return nil , fmt .Errorf ("Fishy RTCP receptionReportCount=%d" , receptionReportCount )
201
+ return rtcpPkt , fmt .Errorf ("Fishy RTCP receptionReportCount=%v type=%d length=%d offset=%d in packet:\n %v" , receptionReportCount , RTCPType , dataLen , offset , hex .Dump (data ))
202
+ } else if RTCPLength > dataLen {
203
+ return rtcpPkt , fmt .Errorf ("Fishy RTCP report length=%d in packet:\n %v" , RTCPLength , hex .Dump (data ))
204
+ } else if RTCPType < 200 || RTCPType > 207 {
205
+ return rtcpPkt , fmt .Errorf ("Fishy RTCP type=%d in packet:\n %v" , RTCPType , hex .Dump (data ))
200
206
}
201
207
202
208
switch RTCPType {
203
209
case TYPE_RTCP_SR :
204
- if RTCPLength < 24 {
205
- return nil , fmt .Errorf ("To small RTCP packet=%v length=%d type=%d" , data [ offset : RTCPLength ] , RTCPLength , RTCPType )
210
+ if RTCPLength < 24 || offset + 24 > len ( data ) {
211
+ return rtcpPkt , fmt .Errorf ("Fishy RTCP packet=%v length=%d type=%d offset=%d " , data , RTCPLength , RTCPType , offset )
206
212
}
207
213
208
214
pkt .Ssrc = binary .BigEndian .Uint32 (data [offset :])
@@ -213,9 +219,9 @@ func ParseRTCP(data []byte) ([]byte, error) {
213
219
pkt .SenderInformation .Octet_count = binary .BigEndian .Uint32 (data [offset + 20 :])
214
220
offset += 24
215
221
216
- if receptionReportCount > 0 && RTCPLength >= 24 {
222
+ if receptionReportCount > 0 && RTCPLength >= 24 && offset + 24 <= len ( data ) {
217
223
tmpReportBlocks := make ([]RTCP_report_block , receptionReportCount )
218
- for i := 0 ; i < int ( receptionReportCount ) ; i ++ {
224
+ for i := 0 ; i < receptionReportCount ; i ++ {
219
225
tmpReportBlocks [i ].SourceSsrc = binary .BigEndian .Uint32 (data [offset :])
220
226
tmpReportBlocks [i ].Fraction_lost = data [offset + 4 ]
221
227
var cumBuf [4 ]byte
@@ -225,25 +231,29 @@ func ParseRTCP(data []byte) ([]byte, error) {
225
231
tmpReportBlocks [i ].Jitter = binary .BigEndian .Uint32 (data [offset + 12 :])
226
232
tmpReportBlocks [i ].LastSR = binary .BigEndian .Uint32 (data [offset + 16 :])
227
233
tmpReportBlocks [i ].Delay_last_SR = binary .BigEndian .Uint32 (data [offset + 20 :])
228
- tmpReportBlocks [i ].ReportCount = receptionReportCount
229
- tmpReportBlocks [i ].RTCPType = RTCPType
234
+ tmpReportBlocks [i ].ReportCount = uint8 ( receptionReportCount )
235
+ tmpReportBlocks [i ].RTCPType = uint8 ( RTCPType )
230
236
offset += 24
231
237
RTCPLength -= 24
232
238
pkt .ReportBlocks = pkt .AddReportBlock (tmpReportBlocks [i ])
233
239
}
234
240
}
241
+ rtcpPkt , err = pkt .MarshalJSON ()
242
+ if err != nil {
243
+ return nil , err
244
+ }
235
245
236
246
case TYPE_RTCP_RR :
237
- if RTCPLength < 4 {
238
- return nil , fmt .Errorf ("To small RTCP packet=%v length=%d type=%d" , data [ offset : RTCPLength ] , RTCPLength , RTCPType )
247
+ if RTCPLength < 4 || offset + 4 > len ( data ) {
248
+ return rtcpPkt , fmt .Errorf ("Fishy RTCP packet=%v length=%d type=%d offset=%d " , data , RTCPLength , RTCPType , offset )
239
249
}
240
250
241
251
pkt .Ssrc = binary .BigEndian .Uint32 (data [offset :])
242
252
offset += 4
243
253
244
- if receptionReportCount > 0 && RTCPLength >= 24 {
254
+ if receptionReportCount > 0 && RTCPLength >= 24 && offset + 24 <= len ( data ) {
245
255
tmpReportBlocks := make ([]RTCP_report_block , receptionReportCount )
246
- for i := 0 ; i < int ( receptionReportCount ) ; i ++ {
256
+ for i := 0 ; i < receptionReportCount ; i ++ {
247
257
tmpReportBlocks [i ].SourceSsrc = binary .BigEndian .Uint32 (data [offset :])
248
258
tmpReportBlocks [i ].Fraction_lost = data [offset + 4 ]
249
259
var cumBuf [4 ]byte
@@ -253,39 +263,36 @@ func ParseRTCP(data []byte) ([]byte, error) {
253
263
tmpReportBlocks [i ].Jitter = binary .BigEndian .Uint32 (data [offset + 12 :])
254
264
tmpReportBlocks [i ].LastSR = binary .BigEndian .Uint32 (data [offset + 16 :])
255
265
tmpReportBlocks [i ].Delay_last_SR = binary .BigEndian .Uint32 (data [offset + 20 :])
256
- tmpReportBlocks [i ].ReportCount = receptionReportCount
257
- tmpReportBlocks [i ].RTCPType = RTCPType
266
+ tmpReportBlocks [i ].ReportCount = uint8 ( receptionReportCount )
267
+ tmpReportBlocks [i ].RTCPType = uint8 ( RTCPType )
258
268
offset += 24
259
269
RTCPLength -= 24
260
270
pkt .ReportBlocks = pkt .AddReportBlock (tmpReportBlocks [i ])
261
271
}
262
272
}
273
+ rtcpPkt , err = pkt .MarshalJSON ()
274
+ if err != nil {
275
+ return nil , err
276
+ }
263
277
264
278
case TYPE_RTCP_SDES :
265
- logp .Debug ("rtcp" , "Discard RTCP_SDES packet type: %d" , RTCPType )
266
- offset += int ( RTCPLength )
279
+ logp .Debug ("rtcp" , "Discard RTCP_SDES packet type= %d" , RTCPType )
280
+ offset += RTCPLength
267
281
case TYPE_RTCP_APP :
268
- logp .Debug ("rtcp" , "Discard RTCP_APP packet type: %d" , RTCPType )
269
- offset += int ( RTCPLength )
282
+ logp .Debug ("rtcp" , "Discard RTCP_APP packet type= %d" , RTCPType )
283
+ offset += RTCPLength
270
284
case TYPE_RTCP_BYE :
271
- logp .Debug ("rtcp" , "Discard RTCP_BYE packet type: %d" , RTCPType )
272
- offset += int ( RTCPLength )
285
+ logp .Debug ("rtcp" , "Discard RTCP_BYE packet type= %d" , RTCPType )
286
+ offset += RTCPLength
273
287
case TYPE_RTCP_XR :
274
- logp .Debug ("rtcp" , "Discard RTCP_XR packet type: %d" , RTCPType )
275
- offset += int ( RTCPLength )
288
+ logp .Debug ("rtcp" , "Discard RTCP_XR packet type= %d" , RTCPType )
289
+ offset += RTCPLength
276
290
default :
277
- logp .Debug ("rtcp" , "Discard unsupported packet type: %d " , RTCPType )
278
- offset += int ( RTCPLength )
291
+ logp .Warn ("rtcp" , "Discard unsupported packet type=%d length=%d offset=%d in packet: \n %v " , RTCPType , dataLen , offset , hex . Dump ( data ) )
292
+ return nil , fmt . Errorf ( "Discard unsupported packet type: %d" , RTCPType )
279
293
}
280
294
281
295
dataLen -= offset
282
-
283
296
}
284
-
285
- rtcpPkt , err := pkt .MarshalJSON ()
286
- if err != nil {
287
- return nil , err
288
- }
289
-
290
297
return rtcpPkt , nil
291
298
}
0 commit comments