diff options
author | mannol <eniz_vukovic@hotmail.com> | 2015-04-29 01:01:25 +0200 |
---|---|---|
committer | mannol <eniz_vukovic@hotmail.com> | 2015-04-29 01:01:25 +0200 |
commit | 9bba7a0434d0967d5dd76b8afc7783ea2edad0cf (patch) | |
tree | 6486c250acb38429a63a62e54cd1e4bcb0a029ee /toxav/rtp.c | |
parent | e4a020333d76bc30172f54f2545677f01bdd54b6 (diff) |
Done
Diffstat (limited to 'toxav/rtp.c')
-rw-r--r-- | toxav/rtp.c | 67 |
1 files changed, 26 insertions, 41 deletions
diff --git a/toxav/rtp.c b/toxav/rtp.c index 6c603f79..2219805b 100644 --- a/toxav/rtp.c +++ b/toxav/rtp.c | |||
@@ -78,11 +78,11 @@ int handle_rtcp_packet ( Messenger *m, uint32_t friendnumber, const uint8_t *dat | |||
78 | void send_rtcp_report ( RTCPSession* session, Messenger* m, uint32_t friendnumber ); | 78 | void send_rtcp_report ( RTCPSession* session, Messenger* m, uint32_t friendnumber ); |
79 | 79 | ||
80 | 80 | ||
81 | RTPSession *rtp_new ( int payload_type, Messenger *messenger, int friend_num, void* cs, int (*mcb) (void*, RTPMessage*) ) | 81 | RTPSession *rtp_new ( int payload_type, Messenger *m, int friend_num, void* cs, int (*mcb) (void*, RTPMessage*) ) |
82 | { | 82 | { |
83 | assert(mcb); | 83 | assert(mcb); |
84 | assert(cs); | 84 | assert(cs); |
85 | assert(messenger); | 85 | assert(m); |
86 | 86 | ||
87 | RTPSession *retu = calloc(1, sizeof(RTPSession)); | 87 | RTPSession *retu = calloc(1, sizeof(RTPSession)); |
88 | 88 | ||
@@ -95,8 +95,8 @@ RTPSession *rtp_new ( int payload_type, Messenger *messenger, int friend_num, vo | |||
95 | retu->ssrc = random_int(); | 95 | retu->ssrc = random_int(); |
96 | retu->payload_type = payload_type % 128; | 96 | retu->payload_type = payload_type % 128; |
97 | 97 | ||
98 | retu->m = messenger; | 98 | retu->m = m; |
99 | retu->friend_id = friend_num; | 99 | retu->friend_number = friend_num; |
100 | 100 | ||
101 | if ( !(retu->csrc = calloc(1, sizeof(uint32_t))) ) { | 101 | if ( !(retu->csrc = calloc(1, sizeof(uint32_t))) ) { |
102 | LOGGER_WARNING("Alloc failed! Program might misbehave!"); | 102 | LOGGER_WARNING("Alloc failed! Program might misbehave!"); |
@@ -161,7 +161,7 @@ int rtp_do(RTPSession *session) | |||
161 | return rtp_StateNormal; | 161 | return rtp_StateNormal; |
162 | 162 | ||
163 | if (current_time_monotonic() - session->rtcp_session->last_sent_report_ts >= RTCP_REPORT_INTERVAL_MS) { | 163 | if (current_time_monotonic() - session->rtcp_session->last_sent_report_ts >= RTCP_REPORT_INTERVAL_MS) { |
164 | send_rtcp_report(session->rtcp_session, session->m, session->friend_id); | 164 | send_rtcp_report(session->rtcp_session, session->m, session->friend_number); |
165 | } | 165 | } |
166 | 166 | ||
167 | if (rb_full(session->rtcp_session->pl_stats)) { | 167 | if (rb_full(session->rtcp_session->pl_stats)) { |
@@ -209,15 +209,15 @@ int rtp_start_receiving(RTPSession* session) | |||
209 | if (session == NULL) | 209 | if (session == NULL) |
210 | return -1; | 210 | return -1; |
211 | 211 | ||
212 | if (m_callback_rtp_packet(session->m, session->friend_id, session->prefix, | 212 | if (m_callback_rtp_packet(session->m, session->friend_number, session->prefix, |
213 | handle_rtp_packet, session) == -1) { | 213 | handle_rtp_packet, session) == -1) { |
214 | LOGGER_WARNING("Failed to register rtp receive handler"); | 214 | LOGGER_WARNING("Failed to register rtp receive handler"); |
215 | return -1; | 215 | return -1; |
216 | } | 216 | } |
217 | if (m_callback_rtp_packet(session->m, session->friend_id, session->rtcp_session->prefix, | 217 | if (m_callback_rtp_packet(session->m, session->friend_number, session->rtcp_session->prefix, |
218 | handle_rtcp_packet, session->rtcp_session) == -1) { | 218 | handle_rtcp_packet, session->rtcp_session) == -1) { |
219 | LOGGER_WARNING("Failed to register rtcp receive handler"); | 219 | LOGGER_WARNING("Failed to register rtcp receive handler"); |
220 | m_callback_rtp_packet(session->m, session->friend_id, session->prefix, NULL, NULL); | 220 | m_callback_rtp_packet(session->m, session->friend_number, session->prefix, NULL, NULL); |
221 | return -1; | 221 | return -1; |
222 | } | 222 | } |
223 | 223 | ||
@@ -228,8 +228,8 @@ int rtp_stop_receiving(RTPSession* session) | |||
228 | if (session == NULL) | 228 | if (session == NULL) |
229 | return -1; | 229 | return -1; |
230 | 230 | ||
231 | m_callback_rtp_packet(session->m, session->friend_id, session->prefix, NULL, NULL); | 231 | m_callback_rtp_packet(session->m, session->friend_number, session->prefix, NULL, NULL); |
232 | m_callback_rtp_packet(session->m, session->friend_id, session->rtcp_session->prefix, NULL, NULL); /* RTCP */ | 232 | m_callback_rtp_packet(session->m, session->friend_number, session->rtcp_session->prefix, NULL, NULL); /* RTCP */ |
233 | 233 | ||
234 | return 0; | 234 | return 0; |
235 | } | 235 | } |
@@ -243,7 +243,8 @@ int rtp_send_data ( RTPSession *session, const uint8_t *data, uint16_t length, b | |||
243 | uint8_t parsed[MAX_RTP_SIZE]; | 243 | uint8_t parsed[MAX_RTP_SIZE]; |
244 | uint8_t *it; | 244 | uint8_t *it; |
245 | 245 | ||
246 | RTPHeader header[1]; | 246 | RTPHeader header[1] = {0}; |
247 | |||
247 | ADD_FLAG_VERSION ( header, session->version ); | 248 | ADD_FLAG_VERSION ( header, session->version ); |
248 | ADD_FLAG_PADDING ( header, session->padding ); | 249 | ADD_FLAG_PADDING ( header, session->padding ); |
249 | ADD_FLAG_EXTENSION ( header, session->extension ); | 250 | ADD_FLAG_EXTENSION ( header, session->extension ); |
@@ -278,12 +279,11 @@ int rtp_send_data ( RTPSession *session, const uint8_t *data, uint16_t length, b | |||
278 | 279 | ||
279 | memcpy(it, data, length); | 280 | memcpy(it, data, length); |
280 | 281 | ||
281 | if ( -1 == send_custom_lossy_packet(session->m, session->friend_id, parsed, parsed_len) ) { | 282 | if ( -1 == send_custom_lossy_packet(session->m, session->friend_number, parsed, parsed_len) ) { |
282 | LOGGER_WARNING("Failed to send full packet (len: %d)! std error: %s", length, strerror(errno)); | 283 | LOGGER_WARNING("Failed to send full packet (len: %d)! std error: %s", length, strerror(errno)); |
283 | return -1; | 284 | return -1; |
284 | } | 285 | } |
285 | 286 | ||
286 | /* Set sequ number */ | ||
287 | session->sequnum = session->sequnum >= MAX_SEQU_NUM ? 0 : session->sequnum + 1; | 287 | session->sequnum = session->sequnum >= MAX_SEQU_NUM ? 0 : session->sequnum + 1; |
288 | return 0; | 288 | return 0; |
289 | } | 289 | } |
@@ -300,7 +300,6 @@ void rtp_free_msg ( RTPMessage *msg ) | |||
300 | 300 | ||
301 | 301 | ||
302 | 302 | ||
303 | |||
304 | RTPHeader *parse_header_in ( const uint8_t *payload, int length ) | 303 | RTPHeader *parse_header_in ( const uint8_t *payload, int length ) |
305 | { | 304 | { |
306 | if ( !payload || !length ) { | 305 | if ( !payload || !length ) { |
@@ -322,12 +321,7 @@ RTPHeader *parse_header_in ( const uint8_t *payload, int length ) | |||
322 | 321 | ||
323 | retu->flags = *it; | 322 | retu->flags = *it; |
324 | ++it; | 323 | ++it; |
325 | 324 | ||
326 | /* This indicates if the first 2 bits are valid. | ||
327 | * Now it may happen that this is out of order but | ||
328 | * it cuts down chances of parsing some invalid value | ||
329 | */ | ||
330 | |||
331 | if ( GET_FLAG_VERSION(retu) != RTP_VERSION ) { | 325 | if ( GET_FLAG_VERSION(retu) != RTP_VERSION ) { |
332 | /* Deallocate */ | 326 | /* Deallocate */ |
333 | LOGGER_WARNING("Invalid version!"); | 327 | LOGGER_WARNING("Invalid version!"); |
@@ -335,15 +329,10 @@ RTPHeader *parse_header_in ( const uint8_t *payload, int length ) | |||
335 | return NULL; | 329 | return NULL; |
336 | } | 330 | } |
337 | 331 | ||
338 | /* | ||
339 | * Added a check for the size of the header little sooner so | ||
340 | * I don't need to parse the other stuff if it's bad | ||
341 | */ | ||
342 | uint8_t cc = GET_FLAG_CSRCC ( retu ); | 332 | uint8_t cc = GET_FLAG_CSRCC ( retu ); |
343 | int total = 12 /* Minimum header len */ + ( cc * 4 ); | 333 | int total = 12 /* Minimum header len */ + ( cc * 4 ); |
344 | 334 | ||
345 | if ( length < total ) { | 335 | if ( length < total ) { |
346 | /* Deallocate */ | ||
347 | LOGGER_WARNING("Length invalid!"); | 336 | LOGGER_WARNING("Length invalid!"); |
348 | free(retu); | 337 | free(retu); |
349 | return NULL; | 338 | return NULL; |
@@ -355,9 +344,10 @@ RTPHeader *parse_header_in ( const uint8_t *payload, int length ) | |||
355 | 344 | ||
356 | 345 | ||
357 | memcpy(&retu->timestamp, it, sizeof(retu->timestamp)); | 346 | memcpy(&retu->timestamp, it, sizeof(retu->timestamp)); |
358 | retu->timestamp = ntohl(retu->timestamp); | ||
359 | it += 4; | 347 | it += 4; |
360 | memcpy(&retu->ssrc, it, sizeof(retu->ssrc)); | 348 | memcpy(&retu->ssrc, it, sizeof(retu->ssrc)); |
349 | |||
350 | retu->timestamp = ntohl(retu->timestamp); | ||
361 | retu->ssrc = ntohl(retu->ssrc); | 351 | retu->ssrc = ntohl(retu->ssrc); |
362 | 352 | ||
363 | uint8_t x; | 353 | uint8_t x; |
@@ -380,34 +370,31 @@ RTPExtHeader *parse_ext_header_in ( const uint8_t *payload, uint16_t length ) | |||
380 | return NULL; | 370 | return NULL; |
381 | } | 371 | } |
382 | 372 | ||
383 | uint16_t ext_length; | 373 | memcpy(&retu->length, it, sizeof(retu->length)); |
384 | memcpy(&ext_length, it, sizeof(ext_length)); | 374 | retu->length = ntohs(retu->length); |
385 | ext_length = ntohs(ext_length); | ||
386 | it += 2; | 375 | it += 2; |
387 | 376 | ||
388 | 377 | if ( length < ( retu->length * sizeof(uint32_t) ) ) { | |
389 | if ( length < ( ext_length * sizeof(uint32_t) ) ) { | ||
390 | LOGGER_WARNING("Length invalid!"); | 378 | LOGGER_WARNING("Length invalid!"); |
391 | free(retu); | 379 | free(retu); |
392 | return NULL; | 380 | return NULL; |
393 | } | 381 | } |
394 | 382 | ||
395 | retu->length = ext_length; | ||
396 | memcpy(&retu->type, it, sizeof(retu->type)); | 383 | memcpy(&retu->type, it, sizeof(retu->type)); |
397 | retu->type = ntohs(retu->type); | 384 | retu->type = ntohs(retu->type); |
385 | |||
398 | it += 2; | 386 | it += 2; |
399 | 387 | ||
400 | if ( !(retu->table = calloc(ext_length, sizeof (uint32_t))) ) { | 388 | if ( !(retu->table = calloc(retu->length, sizeof (uint32_t))) ) { |
401 | LOGGER_WARNING("Alloc failed! Program might misbehave!"); | 389 | LOGGER_WARNING("Alloc failed! Program might misbehave!"); |
402 | free(retu); | 390 | free(retu); |
403 | return NULL; | 391 | return NULL; |
404 | } | 392 | } |
405 | 393 | ||
406 | uint16_t x; | 394 | uint16_t x; |
407 | 395 | for ( x = 0; x < retu->length; x++ ) { | |
408 | for ( x = 0; x < ext_length; x++ ) { | ||
409 | it += 4; | 396 | it += 4; |
410 | memcpy(&(retu->table[x]), it, sizeof(retu->table[x])); | 397 | memcpy(retu->table + x, it, sizeof(*retu->table)); |
411 | retu->table[x] = ntohl(retu->table[x]); | 398 | retu->table[x] = ntohl(retu->table[x]); |
412 | } | 399 | } |
413 | 400 | ||
@@ -433,7 +420,6 @@ uint8_t *parse_header_out ( const RTPHeader *header, uint8_t *payload ) | |||
433 | *it = header->marker_payloadt; | 420 | *it = header->marker_payloadt; |
434 | ++it; | 421 | ++it; |
435 | 422 | ||
436 | |||
437 | timestamp = htonl(header->timestamp); | 423 | timestamp = htonl(header->timestamp); |
438 | memcpy(it, ×tamp, sizeof(timestamp)); | 424 | memcpy(it, ×tamp, sizeof(timestamp)); |
439 | it += 4; | 425 | it += 4; |
@@ -579,7 +565,6 @@ int handle_rtcp_packet ( Messenger* m, uint32_t friendnumber, const uint8_t* dat | |||
579 | report->received_packets = ntohl(report->received_packets); | 565 | report->received_packets = ntohl(report->received_packets); |
580 | report->expected_packets = ntohl(report->expected_packets); | 566 | report->expected_packets = ntohl(report->expected_packets); |
581 | 567 | ||
582 | /* Invalid values */ | ||
583 | if (report->expected_packets == 0 || report->received_packets > report->expected_packets) { | 568 | if (report->expected_packets == 0 || report->received_packets > report->expected_packets) { |
584 | LOGGER_WARNING("Malformed rtcp report! %d %d", report->expected_packets, report->received_packets); | 569 | LOGGER_WARNING("Malformed rtcp report! %d %d", report->expected_packets, report->received_packets); |
585 | free(report); | 570 | free(report); |