2014-01-25 08:32:33 +08:00
|
|
|
/** toxrtp.c
|
2014-02-17 09:01:30 +08:00
|
|
|
*
|
2014-01-25 08:32:33 +08:00
|
|
|
* Copyright (C) 2013 Tox project All Rights Reserved.
|
|
|
|
*
|
|
|
|
* This file is part of Tox.
|
|
|
|
*
|
|
|
|
* Tox 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.
|
|
|
|
*
|
|
|
|
* Tox 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 Tox. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
2014-02-17 09:01:30 +08:00
|
|
|
*
|
2014-02-16 05:36:15 +08:00
|
|
|
* Report bugs/suggestions at #tox-dev @ freenode.net:6667
|
2014-01-25 08:32:33 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif /* HAVE_CONFIG_H */
|
|
|
|
|
2014-05-03 07:46:03 +08:00
|
|
|
#include "../toxcore/logger.h"
|
|
|
|
|
2014-02-10 06:06:44 +08:00
|
|
|
#include "rtp.h"
|
2014-01-25 08:32:33 +08:00
|
|
|
#include <assert.h>
|
2014-02-01 19:52:48 +08:00
|
|
|
#include <stdlib.h>
|
2014-01-25 08:32:33 +08:00
|
|
|
|
|
|
|
|
|
|
|
#define PAYLOAD_ID_VALUE_OPUS 1
|
|
|
|
#define PAYLOAD_ID_VALUE_VP8 2
|
|
|
|
|
|
|
|
#define size_32 4
|
|
|
|
|
|
|
|
#define inline__ inline __attribute__((always_inline))
|
|
|
|
|
|
|
|
|
|
|
|
#define ADD_FLAG_VERSION(_h, _v) do { ( _h->flags ) &= 0x3F; ( _h->flags ) |= ( ( ( _v ) << 6 ) & 0xC0 ); } while(0)
|
|
|
|
#define ADD_FLAG_PADDING(_h, _v) do { if ( _v > 0 ) _v = 1; ( _h->flags ) &= 0xDF; ( _h->flags ) |= ( ( ( _v ) << 5 ) & 0x20 ); } while(0)
|
|
|
|
#define ADD_FLAG_EXTENSION(_h, _v) do { if ( _v > 0 ) _v = 1; ( _h->flags ) &= 0xEF;( _h->flags ) |= ( ( ( _v ) << 4 ) & 0x10 ); } while(0)
|
|
|
|
#define ADD_FLAG_CSRCC(_h, _v) do { ( _h->flags ) &= 0xF0; ( _h->flags ) |= ( ( _v ) & 0x0F ); } while(0)
|
|
|
|
#define ADD_SETTING_MARKER(_h, _v) do { if ( _v > 1 ) _v = 1; ( _h->marker_payloadt ) &= 0x7F; ( _h->marker_payloadt ) |= ( ( ( _v ) << 7 ) /*& 0x80 */ ); } while(0)
|
|
|
|
#define ADD_SETTING_PAYLOAD(_h, _v) do { if ( _v > 127 ) _v = 127; ( _h->marker_payloadt ) &= 0x80; ( _h->marker_payloadt ) |= ( ( _v ) /* & 0x7F */ ); } while(0)
|
|
|
|
|
|
|
|
#define GET_FLAG_VERSION(_h) (( _h->flags & 0xd0 ) >> 6)
|
|
|
|
#define GET_FLAG_PADDING(_h) (( _h->flags & 0x20 ) >> 5)
|
|
|
|
#define GET_FLAG_EXTENSION(_h) (( _h->flags & 0x10 ) >> 4)
|
|
|
|
#define GET_FLAG_CSRCC(_h) ( _h->flags & 0x0f )
|
|
|
|
#define GET_SETTING_MARKER(_h) (( _h->marker_payloadt ) >> 7)
|
|
|
|
#define GET_SETTING_PAYLOAD(_h) ((_h->marker_payloadt) & 0x7f)
|
|
|
|
|
|
|
|
|
2014-02-01 19:52:48 +08:00
|
|
|
/**
|
|
|
|
* @brief Converts 4 bytes to uint32_t
|
2014-02-17 09:01:30 +08:00
|
|
|
*
|
2014-02-01 19:52:48 +08:00
|
|
|
* @param dest Where to convert
|
|
|
|
* @param bytes What bytes
|
|
|
|
* @return void
|
|
|
|
*/
|
2014-02-17 09:01:30 +08:00
|
|
|
inline__ void bytes_to_U32(uint32_t *dest, const uint8_t *bytes)
|
2014-02-01 19:52:48 +08:00
|
|
|
{
|
2014-02-17 09:01:30 +08:00
|
|
|
*dest =
|
|
|
|
#ifdef WORDS_BIGENDIAN
|
|
|
|
( ( uint32_t ) * bytes ) |
|
|
|
|
( ( uint32_t ) * ( bytes + 1 ) << 8 ) |
|
|
|
|
( ( uint32_t ) * ( bytes + 2 ) << 16 ) |
|
|
|
|
( ( uint32_t ) * ( bytes + 3 ) << 24 ) ;
|
2014-02-01 19:52:48 +08:00
|
|
|
#else
|
2014-02-17 09:01:30 +08:00
|
|
|
( ( uint32_t ) * bytes << 24 ) |
|
|
|
|
( ( uint32_t ) * ( bytes + 1 ) << 16 ) |
|
|
|
|
( ( uint32_t ) * ( bytes + 2 ) << 8 ) |
|
|
|
|
( ( uint32_t ) * ( bytes + 3 ) ) ;
|
|
|
|
#endif
|
2014-02-01 19:52:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Converts 2 bytes to uint16_t
|
2014-02-17 09:01:30 +08:00
|
|
|
*
|
2014-02-01 19:52:48 +08:00
|
|
|
* @param dest Where to convert
|
|
|
|
* @param bytes What bytes
|
|
|
|
* @return void
|
|
|
|
*/
|
2014-02-17 09:01:30 +08:00
|
|
|
inline__ void bytes_to_U16(uint16_t *dest, const uint8_t *bytes)
|
|
|
|
{
|
|
|
|
*dest =
|
|
|
|
#ifdef WORDS_BIGENDIAN
|
|
|
|
( ( uint16_t ) * bytes ) |
|
|
|
|
( ( uint16_t ) * ( bytes + 1 ) << 8 );
|
2014-02-01 19:52:48 +08:00
|
|
|
#else
|
2014-02-17 09:01:30 +08:00
|
|
|
( ( uint16_t ) * bytes << 8 ) |
|
|
|
|
( ( uint16_t ) * ( bytes + 1 ) );
|
|
|
|
#endif
|
2014-02-01 19:52:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Convert uint32_t to byte string of size 4
|
2014-02-17 09:01:30 +08:00
|
|
|
*
|
2014-02-01 19:52:48 +08:00
|
|
|
* @param dest Where to convert
|
|
|
|
* @param value The value
|
|
|
|
* @return void
|
|
|
|
*/
|
2014-02-17 09:01:30 +08:00
|
|
|
inline__ void U32_to_bytes(uint8_t *dest, uint32_t value)
|
|
|
|
{
|
|
|
|
#ifdef WORDS_BIGENDIAN
|
2014-02-01 19:52:48 +08:00
|
|
|
*(dest) = ( value );
|
|
|
|
*(dest + 1) = ( value >> 8 );
|
|
|
|
*(dest + 2) = ( value >> 16 );
|
|
|
|
*(dest + 3) = ( value >> 24 );
|
|
|
|
#else
|
|
|
|
*(dest) = ( value >> 24 );
|
|
|
|
*(dest + 1) = ( value >> 16 );
|
|
|
|
*(dest + 2) = ( value >> 8 );
|
|
|
|
*(dest + 3) = ( value );
|
2014-02-17 09:01:30 +08:00
|
|
|
#endif
|
2014-02-01 19:52:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Convert uint16_t to byte string of size 2
|
2014-02-17 09:01:30 +08:00
|
|
|
*
|
2014-02-01 19:52:48 +08:00
|
|
|
* @param dest Where to convert
|
|
|
|
* @param value The value
|
|
|
|
* @return void
|
|
|
|
*/
|
2014-02-17 09:01:30 +08:00
|
|
|
inline__ void U16_to_bytes(uint8_t *dest, uint16_t value)
|
2014-02-01 19:52:48 +08:00
|
|
|
{
|
2014-02-17 09:01:30 +08:00
|
|
|
#ifdef WORDS_BIGENDIAN
|
2014-02-01 19:52:48 +08:00
|
|
|
*(dest) = ( value );
|
|
|
|
*(dest + 1) = ( value >> 8 );
|
|
|
|
#else
|
|
|
|
*(dest) = ( value >> 8 );
|
|
|
|
*(dest + 1) = ( value );
|
2014-02-17 09:01:30 +08:00
|
|
|
#endif
|
2014-02-01 19:52:48 +08:00
|
|
|
}
|
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Checks if message came in late.
|
2014-02-17 09:01:30 +08:00
|
|
|
*
|
2014-01-25 08:32:33 +08:00
|
|
|
* @param session Control session.
|
|
|
|
* @param msg The message.
|
|
|
|
* @return int
|
|
|
|
* @retval -1 The message came in order.
|
|
|
|
* @retval 0 The message came late.
|
|
|
|
*/
|
2014-02-17 09:01:30 +08:00
|
|
|
inline__ int check_late_message (RTPSession *session, RTPMessage *msg)
|
2014-01-25 08:32:33 +08:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Check Sequence number. If this new msg has lesser number then the session->rsequnum
|
|
|
|
* it shows that the message came in late. Also check timestamp to be 100% certain.
|
2014-02-17 09:01:30 +08:00
|
|
|
*
|
2014-01-25 08:32:33 +08:00
|
|
|
*/
|
|
|
|
return ( msg->header->sequnum < session->rsequnum && msg->header->timestamp < session->timestamp ) ? 0 : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Increases nonce value by 'target'
|
2014-02-17 09:01:30 +08:00
|
|
|
*
|
2014-01-25 08:32:33 +08:00
|
|
|
* @param nonce The nonce
|
|
|
|
* @param target The target
|
|
|
|
* @return void
|
|
|
|
*/
|
2014-02-17 09:01:30 +08:00
|
|
|
inline__ void increase_nonce(uint8_t *nonce, uint16_t target)
|
2014-01-25 08:32:33 +08:00
|
|
|
{
|
2014-02-01 19:52:48 +08:00
|
|
|
uint16_t _nonce_counter;
|
|
|
|
|
|
|
|
uint8_t _reverse_bytes[2];
|
2014-04-22 04:51:36 +08:00
|
|
|
_reverse_bytes[0] = nonce[crypto_box_NONCEBYTES - 1];
|
|
|
|
_reverse_bytes[1] = nonce[crypto_box_NONCEBYTES - 2];
|
2014-02-01 19:52:48 +08:00
|
|
|
|
|
|
|
bytes_to_U16(&_nonce_counter, _reverse_bytes );
|
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
/* Check overflow */
|
2014-02-01 19:52:48 +08:00
|
|
|
if (_nonce_counter > UINT16_MAX - target ) { /* 2 bytes are not long enough */
|
|
|
|
uint8_t _it = 3;
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-04-22 04:51:36 +08:00
|
|
|
while ( _it <= crypto_box_NONCEBYTES ) _it += ++nonce[crypto_box_NONCEBYTES - _it] ?
|
|
|
|
crypto_box_NONCEBYTES : 1;
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-02-01 19:52:48 +08:00
|
|
|
_nonce_counter = _nonce_counter - (UINT16_MAX - target ); /* Assign the rest of it */
|
2014-01-25 08:32:33 +08:00
|
|
|
} else { /* Increase nonce */
|
2014-02-17 09:01:30 +08:00
|
|
|
|
|
|
|
_nonce_counter += target;
|
2014-01-25 08:32:33 +08:00
|
|
|
}
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-02-01 19:52:48 +08:00
|
|
|
/* Assign the last bytes */
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-02-01 19:52:48 +08:00
|
|
|
U16_to_bytes( _reverse_bytes, _nonce_counter);
|
2014-04-22 04:51:36 +08:00
|
|
|
nonce [crypto_box_NONCEBYTES - 1] = _reverse_bytes[0];
|
|
|
|
nonce [crypto_box_NONCEBYTES - 2] = _reverse_bytes[1];
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Speaks for it self.
|
2014-02-17 09:01:30 +08:00
|
|
|
*
|
2014-01-25 08:32:33 +08:00
|
|
|
*/
|
2014-02-17 09:01:30 +08:00
|
|
|
static const uint32_t payload_table[] = {
|
2014-01-25 08:32:33 +08:00
|
|
|
8000, 8000, 8000, 8000, 8000, 8000, 16000, 8000, 8000, 8000, /* 0-9 */
|
|
|
|
44100, 44100, 0, 0, 90000, 8000, 11025, 22050, 0, 0, /* 10-19 */
|
|
|
|
0, 0, 0, 0, 0, 90000, 90000, 0, 90000, 0, /* 20-29 */
|
|
|
|
0, 90000, 90000, 90000, 90000, 0, 0, 0, 0, 0, /* 30-39 */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 40-49 */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 50-59 */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 60-69 */
|
|
|
|
PAYLOAD_ID_VALUE_OPUS, PAYLOAD_ID_VALUE_VP8, 0, 0, 0, 0, 0, 0, 0, 0,/* 70-79 */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 80-89 */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 90-99 */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 100-109 */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 110-119 */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0 /* 120-127 */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Extracts header from payload.
|
2014-02-17 09:01:30 +08:00
|
|
|
*
|
2014-01-25 08:32:33 +08:00
|
|
|
* @param payload The payload.
|
|
|
|
* @param length The size of payload.
|
|
|
|
* @return RTPHeader* Extracted header.
|
|
|
|
* @retval NULL Error occurred while extracting header.
|
|
|
|
*/
|
2014-02-17 09:01:30 +08:00
|
|
|
RTPHeader *extract_header ( const uint8_t *payload, int length )
|
2014-01-25 08:32:33 +08:00
|
|
|
{
|
2014-02-01 19:52:48 +08:00
|
|
|
if ( !payload || !length ) {
|
2014-05-03 07:46:03 +08:00
|
|
|
LOGGER_WARNING("No payload to extract!");
|
2014-01-25 08:32:33 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2014-02-17 09:01:30 +08:00
|
|
|
|
|
|
|
const uint8_t *_it = payload;
|
|
|
|
|
|
|
|
RTPHeader *_retu = calloc(1, sizeof (RTPHeader));
|
2014-01-25 08:32:33 +08:00
|
|
|
assert(_retu);
|
2014-02-17 09:01:30 +08:00
|
|
|
|
|
|
|
_retu->flags = *_it;
|
|
|
|
++_it;
|
|
|
|
|
2014-02-16 03:44:33 +08:00
|
|
|
/* This indicates if the first 2 bits are valid.
|
|
|
|
* Now it may happen that this is out of order but
|
2014-01-25 08:32:33 +08:00
|
|
|
* it cuts down chances of parsing some invalid value
|
|
|
|
*/
|
2014-02-17 09:01:30 +08:00
|
|
|
|
|
|
|
if ( GET_FLAG_VERSION(_retu) != RTP_VERSION ) {
|
2014-01-25 08:32:33 +08:00
|
|
|
/* Deallocate */
|
2014-05-03 07:46:03 +08:00
|
|
|
LOGGER_WARNING("Invalid version!");
|
2014-01-25 08:32:33 +08:00
|
|
|
free(_retu);
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
/*
|
|
|
|
* Added a check for the size of the header little sooner so
|
|
|
|
* I don't need to parse the other stuff if it's bad
|
|
|
|
*/
|
2014-02-17 09:01:30 +08:00
|
|
|
uint8_t _cc = GET_FLAG_CSRCC ( _retu );
|
2014-01-25 08:32:33 +08:00
|
|
|
uint32_t _length = 12 /* Minimum header len */ + ( _cc * 4 );
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
if ( length < _length ) {
|
|
|
|
/* Deallocate */
|
2014-05-03 07:46:03 +08:00
|
|
|
LOGGER_WARNING("Length invalid!");
|
2014-01-25 08:32:33 +08:00
|
|
|
free(_retu);
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-03-28 06:29:18 +08:00
|
|
|
memset(_retu->csrc, 0, 16 * sizeof (uint32_t));
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-02-17 09:01:30 +08:00
|
|
|
_retu->marker_payloadt = *_it;
|
|
|
|
++_it;
|
2014-01-25 08:32:33 +08:00
|
|
|
_retu->length = _length;
|
2014-02-17 09:01:30 +08:00
|
|
|
|
|
|
|
|
|
|
|
bytes_to_U32(&_retu->timestamp, _it);
|
|
|
|
_it += 4;
|
2014-02-01 19:52:48 +08:00
|
|
|
bytes_to_U32(&_retu->ssrc, _it);
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-02-01 19:52:48 +08:00
|
|
|
uint8_t _x;
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
for ( _x = 0; _x < _cc; _x++ ) {
|
2014-02-17 09:01:30 +08:00
|
|
|
_it += 4;
|
|
|
|
bytes_to_U32(&(_retu->csrc[_x]), _it);
|
2014-01-25 08:32:33 +08:00
|
|
|
}
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
return _retu;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Extracts external header from payload. Must be called AFTER extract_header()!
|
2014-02-17 09:01:30 +08:00
|
|
|
*
|
2014-01-25 08:32:33 +08:00
|
|
|
* @param payload The ITERATED payload.
|
|
|
|
* @param length The size of payload.
|
|
|
|
* @return RTPExtHeader* Extracted extension header.
|
|
|
|
* @retval NULL Error occurred while extracting extension header.
|
|
|
|
*/
|
2014-02-17 09:01:30 +08:00
|
|
|
RTPExtHeader *extract_ext_header ( const uint8_t *payload, uint16_t length )
|
2014-01-25 08:32:33 +08:00
|
|
|
{
|
2014-02-17 09:01:30 +08:00
|
|
|
const uint8_t *_it = payload;
|
|
|
|
|
|
|
|
RTPExtHeader *_retu = calloc(1, sizeof (RTPExtHeader));
|
2014-01-25 08:32:33 +08:00
|
|
|
assert(_retu);
|
2014-02-17 09:01:30 +08:00
|
|
|
|
|
|
|
uint16_t _ext_length;
|
|
|
|
bytes_to_U16(&_ext_length, _it);
|
|
|
|
_it += 2;
|
|
|
|
|
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
if ( length < ( _ext_length * sizeof(uint32_t) ) ) {
|
2014-05-03 07:46:03 +08:00
|
|
|
LOGGER_WARNING("Length invalid!");
|
2014-02-07 07:10:55 +08:00
|
|
|
free(_retu);
|
2014-01-25 08:32:33 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
_retu->length = _ext_length;
|
2014-02-17 09:01:30 +08:00
|
|
|
bytes_to_U16(&_retu->type, _it);
|
|
|
|
_it += 2;
|
|
|
|
|
2014-02-01 19:52:48 +08:00
|
|
|
_retu->table = calloc(_ext_length, sizeof (uint32_t));
|
2014-01-25 08:32:33 +08:00
|
|
|
assert(_retu->table);
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-02-01 19:52:48 +08:00
|
|
|
uint16_t _x;
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-02-01 19:52:48 +08:00
|
|
|
for ( _x = 0; _x < _ext_length; _x++ ) {
|
2014-02-17 09:01:30 +08:00
|
|
|
_it += 4;
|
|
|
|
bytes_to_U32(&(_retu->table[_x]), _it);
|
2014-01-25 08:32:33 +08:00
|
|
|
}
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
return _retu;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Adds header to payload. Make sure _payload_ has enough space.
|
2014-02-17 09:01:30 +08:00
|
|
|
*
|
2014-01-25 08:32:33 +08:00
|
|
|
* @param header The header.
|
|
|
|
* @param payload The payload.
|
|
|
|
* @return uint8_t* Iterated position.
|
|
|
|
*/
|
2014-02-17 09:01:30 +08:00
|
|
|
uint8_t *add_header ( RTPHeader *header, uint8_t *payload )
|
2014-01-25 08:32:33 +08:00
|
|
|
{
|
|
|
|
uint8_t _cc = GET_FLAG_CSRCC ( header );
|
2014-02-17 09:01:30 +08:00
|
|
|
|
|
|
|
uint8_t *_it = payload;
|
|
|
|
|
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
/* Add sequence number first */
|
2014-02-17 09:01:30 +08:00
|
|
|
U16_to_bytes(_it, header->sequnum);
|
|
|
|
_it += 2;
|
|
|
|
|
|
|
|
*_it = header->flags;
|
|
|
|
++_it;
|
|
|
|
*_it = header->marker_payloadt;
|
|
|
|
++_it;
|
|
|
|
|
|
|
|
|
|
|
|
U32_to_bytes( _it, header->timestamp);
|
|
|
|
_it += 4;
|
2014-02-01 19:52:48 +08:00
|
|
|
U32_to_bytes( _it, header->ssrc);
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-03-07 10:13:04 +08:00
|
|
|
uint8_t _x;
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-03-07 10:13:04 +08:00
|
|
|
for ( _x = 0; _x < _cc; _x++ ) {
|
|
|
|
_it += 4;
|
|
|
|
U32_to_bytes( _it, header->csrc[_x]);
|
2014-01-25 08:32:33 +08:00
|
|
|
}
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-02-03 03:43:16 +08:00
|
|
|
return _it + 4;
|
2014-01-25 08:32:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Adds extension header to payload. Make sure _payload_ has enough space.
|
2014-02-17 09:01:30 +08:00
|
|
|
*
|
2014-01-25 08:32:33 +08:00
|
|
|
* @param header The header.
|
|
|
|
* @param payload The payload.
|
|
|
|
* @return uint8_t* Iterated position.
|
|
|
|
*/
|
2014-02-17 09:01:30 +08:00
|
|
|
uint8_t *add_ext_header ( RTPExtHeader *header, uint8_t *payload )
|
2014-01-25 08:32:33 +08:00
|
|
|
{
|
2014-02-17 09:01:30 +08:00
|
|
|
uint8_t *_it = payload;
|
|
|
|
|
|
|
|
U16_to_bytes(_it, header->length);
|
|
|
|
_it += 2;
|
|
|
|
U16_to_bytes(_it, header->type);
|
|
|
|
_it -= 2; /* Return to 0 position */
|
|
|
|
|
2014-02-07 07:10:55 +08:00
|
|
|
if ( header->table ) {
|
|
|
|
uint16_t _x;
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-02-07 07:10:55 +08:00
|
|
|
for ( _x = 0; _x < header->length; _x++ ) {
|
2014-02-17 09:01:30 +08:00
|
|
|
_it += 4;
|
|
|
|
U32_to_bytes(_it, header->table[_x]);
|
2014-02-07 07:10:55 +08:00
|
|
|
}
|
2014-01-25 08:32:33 +08:00
|
|
|
}
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-02-03 03:43:16 +08:00
|
|
|
return _it + 4;
|
2014-01-25 08:32:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Builds header from control session values.
|
2014-02-17 09:01:30 +08:00
|
|
|
*
|
2014-01-25 08:32:33 +08:00
|
|
|
* @param session Control session.
|
|
|
|
* @return RTPHeader* Created header.
|
|
|
|
*/
|
2014-02-17 09:01:30 +08:00
|
|
|
RTPHeader *build_header ( RTPSession *session )
|
2014-01-25 08:32:33 +08:00
|
|
|
{
|
2014-02-17 09:01:30 +08:00
|
|
|
RTPHeader *_retu = calloc ( 1, sizeof (RTPHeader) );
|
2014-01-25 08:32:33 +08:00
|
|
|
assert(_retu);
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
ADD_FLAG_VERSION ( _retu, session->version );
|
|
|
|
ADD_FLAG_PADDING ( _retu, session->padding );
|
|
|
|
ADD_FLAG_EXTENSION ( _retu, session->extension );
|
|
|
|
ADD_FLAG_CSRCC ( _retu, session->cc );
|
|
|
|
ADD_SETTING_MARKER ( _retu, session->marker );
|
|
|
|
ADD_SETTING_PAYLOAD ( _retu, session->payload_type );
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
_retu->sequnum = session->sequnum;
|
2014-05-05 08:33:08 +08:00
|
|
|
_retu->timestamp = current_time_monotonic(); /* milliseconds */
|
2014-01-25 08:32:33 +08:00
|
|
|
_retu->ssrc = session->ssrc;
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-03-07 10:13:04 +08:00
|
|
|
int i;
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-05-26 00:27:48 +08:00
|
|
|
for ( i = 0; i < session->cc; i++ )
|
2014-03-07 10:13:04 +08:00
|
|
|
_retu->csrc[i] = session->csrc[i];
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
_retu->length = 12 /* Minimum header len */ + ( session->cc * size_32 );
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
return _retu;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Parses data into RTPMessage struct. Stores headers separately from the payload data
|
|
|
|
* and so the length variable is set accordingly. _sequnum_ argument is
|
|
|
|
* passed by the handle_packet() since it's parsed already.
|
2014-02-17 09:01:30 +08:00
|
|
|
*
|
2014-01-25 08:32:33 +08:00
|
|
|
* @param session Control session.
|
|
|
|
* @param sequnum Sequence number that's parsed from payload in handle_packet()
|
|
|
|
* @param data Payload data.
|
|
|
|
* @param length Payload size.
|
|
|
|
* @return RTPMessage*
|
|
|
|
* @retval NULL Error occurred.
|
|
|
|
*/
|
2014-02-17 09:01:30 +08:00
|
|
|
RTPMessage *msg_parse ( uint16_t sequnum, const uint8_t *data, int length )
|
|
|
|
{
|
|
|
|
RTPMessage *_retu = calloc(1, sizeof (RTPMessage));
|
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
_retu->header = extract_header ( data, length ); /* It allocates memory and all */
|
2014-02-17 09:01:30 +08:00
|
|
|
|
|
|
|
if ( !_retu->header ) {
|
2014-05-03 07:46:03 +08:00
|
|
|
LOGGER_WARNING("Header failed to extract!");
|
2014-01-25 08:32:33 +08:00
|
|
|
free(_retu);
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
_retu->header->sequnum = sequnum;
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
_retu->length = length - _retu->header->length;
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
uint16_t _from_pos = _retu->header->length - 2 /* Since sequ num is excluded */ ;
|
2014-02-17 09:01:30 +08:00
|
|
|
|
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
if ( GET_FLAG_EXTENSION ( _retu->header ) ) {
|
|
|
|
_retu->ext_header = extract_ext_header ( data + _from_pos, length );
|
2014-02-17 09:01:30 +08:00
|
|
|
|
|
|
|
if ( _retu->ext_header ) {
|
2014-01-25 08:32:33 +08:00
|
|
|
_retu->length -= ( 4 /* Minimum ext header len */ + _retu->ext_header->length * size_32 );
|
|
|
|
_from_pos += ( 4 /* Minimum ext header len */ + _retu->ext_header->length * size_32 );
|
2014-02-01 19:52:48 +08:00
|
|
|
} else { /* Error */
|
2014-05-03 07:46:03 +08:00
|
|
|
LOGGER_WARNING("Ext Header failed to extract!");
|
2014-03-07 10:13:04 +08:00
|
|
|
rtp_free_msg(NULL, _retu);
|
2014-01-25 08:32:33 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_retu->ext_header = NULL;
|
|
|
|
}
|
2014-02-07 07:10:55 +08:00
|
|
|
|
|
|
|
if ( length - _from_pos <= MAX_RTP_SIZE )
|
|
|
|
memcpy ( _retu->data, data + _from_pos, length - _from_pos );
|
|
|
|
else {
|
2014-05-03 07:46:03 +08:00
|
|
|
LOGGER_WARNING("Invalid length!");
|
2014-02-07 07:10:55 +08:00
|
|
|
rtp_free_msg(NULL, _retu);
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
_retu->next = NULL;
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
return _retu;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Callback for networking core.
|
2014-02-17 09:01:30 +08:00
|
|
|
*
|
2014-01-25 08:32:33 +08:00
|
|
|
* @param object RTPSession object.
|
|
|
|
* @param ip_port Where the message comes from.
|
|
|
|
* @param data Message data.
|
|
|
|
* @param length Message length.
|
|
|
|
* @return int
|
|
|
|
* @retval -1 Error occurred.
|
|
|
|
* @retval 0 Success.
|
|
|
|
*/
|
2014-05-22 03:28:14 +08:00
|
|
|
int rtp_handle_packet ( void *object, uint8_t *data, uint32_t length )
|
2014-01-25 08:32:33 +08:00
|
|
|
{
|
2014-02-17 09:01:30 +08:00
|
|
|
RTPSession *_session = object;
|
|
|
|
RTPMessage *_msg;
|
|
|
|
|
2014-05-20 06:10:40 +08:00
|
|
|
if ( !_session || length < 13 + crypto_box_MACBYTES) { /* 12 is the minimum length for rtp + desc. byte */
|
2014-05-03 07:46:03 +08:00
|
|
|
LOGGER_WARNING("No session or invalid length of received buffer!");
|
2014-01-25 08:32:33 +08:00
|
|
|
return -1;
|
2014-05-03 07:46:03 +08:00
|
|
|
}
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-05-03 07:46:03 +08:00
|
|
|
if ( _session->queue_limit <= _session->queue_size ) {
|
|
|
|
LOGGER_WARNING("Queue limit reached!");
|
2014-01-25 08:32:33 +08:00
|
|
|
return -1;
|
2014-05-03 07:46:03 +08:00
|
|
|
}
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
uint8_t _plain[MAX_UDP_PACKET_SIZE];
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-02-01 19:52:48 +08:00
|
|
|
uint16_t _sequnum;
|
|
|
|
bytes_to_U16(&_sequnum, data + 1);
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
/* Clculate the right nonce */
|
2014-04-22 04:51:36 +08:00
|
|
|
uint8_t _calculated[crypto_box_NONCEBYTES];
|
|
|
|
memcpy(_calculated, _session->decrypt_nonce, crypto_box_NONCEBYTES);
|
2014-01-25 08:32:33 +08:00
|
|
|
increase_nonce ( _calculated, _sequnum );
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
/* Decrypt message */
|
|
|
|
int _decrypted_length = decrypt_data_symmetric(
|
2014-02-17 09:01:30 +08:00
|
|
|
(uint8_t *)_session->decrypt_key, _calculated, data + 3, length - 3, _plain );
|
|
|
|
|
|
|
|
/* This packet is either not encrypted properly or late
|
2014-01-25 08:32:33 +08:00
|
|
|
*/
|
2014-02-17 09:01:30 +08:00
|
|
|
if ( -1 == _decrypted_length ) {
|
|
|
|
|
|
|
|
/* If this is the case, then the packet is most likely late.
|
2014-01-25 08:32:33 +08:00
|
|
|
* Try with old nonce cycle.
|
|
|
|
*/
|
|
|
|
if ( _session->rsequnum < _sequnum ) {
|
|
|
|
_decrypted_length = decrypt_data_symmetric(
|
2014-02-17 09:01:30 +08:00
|
|
|
(uint8_t *)_session->decrypt_key, _session->nonce_cycle, data + 3, length - 3, _plain );
|
|
|
|
|
2014-05-26 00:27:48 +08:00
|
|
|
if ( _decrypted_length == -1 ) {
|
2014-05-03 07:46:03 +08:00
|
|
|
LOGGER_WARNING("Packet not ecrypted properly!");
|
|
|
|
return -1; /* This packet is not encrypted properly */
|
|
|
|
}
|
2014-02-17 09:01:30 +08:00
|
|
|
|
|
|
|
/* Otherwise, if decryption is ok with new cycle, set new cycle
|
|
|
|
*/
|
|
|
|
} else {
|
2014-01-25 08:32:33 +08:00
|
|
|
increase_nonce ( _calculated, MAX_SEQU_NUM );
|
|
|
|
_decrypted_length = decrypt_data_symmetric(
|
2014-02-17 09:01:30 +08:00
|
|
|
(uint8_t *)_session->decrypt_key, _calculated, data + 3, length - 3, _plain );
|
|
|
|
|
2014-05-26 00:27:48 +08:00
|
|
|
if ( _decrypted_length == -1 ) {
|
2014-05-03 07:46:03 +08:00
|
|
|
LOGGER_WARNING("Error decrypting!");
|
|
|
|
return -1; /* This is just an error */
|
|
|
|
}
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
/* A new cycle setting. */
|
2014-04-22 04:51:36 +08:00
|
|
|
memcpy(_session->nonce_cycle, _session->decrypt_nonce, crypto_box_NONCEBYTES);
|
|
|
|
memcpy(_session->decrypt_nonce, _calculated, crypto_box_NONCEBYTES);
|
2014-01-25 08:32:33 +08:00
|
|
|
}
|
|
|
|
}
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-02-01 19:52:48 +08:00
|
|
|
_msg = msg_parse ( _sequnum, _plain, _decrypted_length );
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-05-26 00:27:48 +08:00
|
|
|
if ( !_msg ) {
|
2014-05-03 07:46:03 +08:00
|
|
|
LOGGER_WARNING("Could not parse message!");
|
|
|
|
return -1;
|
|
|
|
}
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
/* Check if message came in late */
|
|
|
|
if ( check_late_message(_session, _msg) < 0 ) { /* Not late */
|
|
|
|
_session->rsequnum = _msg->header->sequnum;
|
|
|
|
_session->timestamp = _msg->header->timestamp;
|
|
|
|
}
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
pthread_mutex_lock(&_session->mutex);
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
if ( _session->last_msg ) {
|
|
|
|
_session->last_msg->next = _msg;
|
|
|
|
_session->last_msg = _msg;
|
|
|
|
} else {
|
|
|
|
_session->last_msg = _session->oldest_msg = _msg;
|
|
|
|
}
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-05-03 07:46:03 +08:00
|
|
|
_session->queue_size++;
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
pthread_mutex_unlock(&_session->mutex);
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Stores headers and payload data in one container ( data )
|
|
|
|
* and the length is set accordingly. Returned message is used for sending _only_.
|
2014-02-17 09:01:30 +08:00
|
|
|
*
|
2014-01-25 08:32:33 +08:00
|
|
|
* @param session The control session.
|
|
|
|
* @param data Payload data to send ( This is what you pass ).
|
|
|
|
* @param length Size of the payload data.
|
|
|
|
* @return RTPMessage* Created message.
|
|
|
|
* @retval NULL Error occurred.
|
|
|
|
*/
|
2014-02-17 09:01:30 +08:00
|
|
|
RTPMessage *rtp_new_message ( RTPSession *session, const uint8_t *data, uint32_t length )
|
2014-01-25 08:32:33 +08:00
|
|
|
{
|
2014-05-03 07:46:03 +08:00
|
|
|
if ( !session ) {
|
|
|
|
LOGGER_WARNING("No session!");
|
2014-01-25 08:32:33 +08:00
|
|
|
return NULL;
|
2014-05-03 07:46:03 +08:00
|
|
|
}
|
2014-02-17 09:01:30 +08:00
|
|
|
|
|
|
|
uint8_t *_from_pos;
|
|
|
|
RTPMessage *_retu = calloc(1, sizeof (RTPMessage));
|
2014-01-25 08:32:33 +08:00
|
|
|
assert(_retu);
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
/* Sets header values and copies the extension header in _retu */
|
|
|
|
_retu->header = build_header ( session ); /* It allocates memory and all */
|
|
|
|
_retu->ext_header = session->ext_header;
|
2014-02-17 09:01:30 +08:00
|
|
|
|
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
uint32_t _total_length = length + _retu->header->length;
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
if ( _retu->ext_header ) {
|
|
|
|
_total_length += ( 4 /* Minimum ext header len */ + _retu->ext_header->length * size_32 );
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
_from_pos = add_header ( _retu->header, _retu->data );
|
|
|
|
_from_pos = add_ext_header ( _retu->ext_header, _from_pos + 1 );
|
|
|
|
} else {
|
|
|
|
_from_pos = add_header ( _retu->header, _retu->data );
|
|
|
|
}
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
/*
|
|
|
|
* Parses the extension header into the message
|
|
|
|
* Of course if any
|
|
|
|
*/
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
/* Appends _data on to _retu->_data */
|
2014-02-03 03:43:16 +08:00
|
|
|
memcpy ( _from_pos, data, length );
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
_retu->length = _total_length;
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
_retu->next = NULL;
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
return _retu;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-01 19:52:48 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
/**
|
|
|
|
* @brief Release all messages held by session.
|
2014-02-17 09:01:30 +08:00
|
|
|
*
|
2014-01-25 08:32:33 +08:00
|
|
|
* @param session The session.
|
|
|
|
* @return int
|
|
|
|
* @retval -1 Error occurred.
|
|
|
|
* @retval 0 Success.
|
|
|
|
*/
|
2014-02-17 09:01:30 +08:00
|
|
|
int rtp_release_session_recv ( RTPSession *session )
|
2014-01-25 08:32:33 +08:00
|
|
|
{
|
2014-02-17 09:01:30 +08:00
|
|
|
if ( !session ) {
|
2014-05-03 07:46:03 +08:00
|
|
|
LOGGER_WARNING("No session!");
|
2014-01-25 08:32:33 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2014-02-17 09:01:30 +08:00
|
|
|
|
|
|
|
RTPMessage *_tmp, * _it;
|
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
pthread_mutex_lock(&session->mutex);
|
2014-02-17 09:01:30 +08:00
|
|
|
|
|
|
|
for ( _it = session->oldest_msg; _it; _it = _tmp ) {
|
2014-01-25 08:32:33 +08:00
|
|
|
_tmp = _it->next;
|
|
|
|
rtp_free_msg( session, _it);
|
|
|
|
}
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
session->last_msg = session->oldest_msg = NULL;
|
2014-05-03 07:46:03 +08:00
|
|
|
session->queue_size = 0;
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
pthread_mutex_unlock(&session->mutex);
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-03 07:46:03 +08:00
|
|
|
/**
|
|
|
|
* @brief Call this to change queue limit
|
2014-05-26 00:27:48 +08:00
|
|
|
*
|
2014-05-03 07:46:03 +08:00
|
|
|
* @param session The session
|
|
|
|
* @param limit new limit
|
|
|
|
* @return void
|
|
|
|
*/
|
2014-05-26 00:27:48 +08:00
|
|
|
void rtp_queue_adjust_limit(RTPSession *session, uint64_t limit)
|
2014-05-03 07:46:03 +08:00
|
|
|
{
|
|
|
|
RTPMessage *_tmp, * _it;
|
|
|
|
pthread_mutex_lock(&session->mutex);
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-05-03 07:46:03 +08:00
|
|
|
for ( _it = session->oldest_msg; session->queue_size > limit; _it = _tmp ) {
|
|
|
|
_tmp = _it->next;
|
|
|
|
rtp_free_msg( session, _it);
|
|
|
|
session->queue_size --;
|
|
|
|
}
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-05-03 07:46:03 +08:00
|
|
|
session->oldest_msg = _it;
|
|
|
|
session->queue_limit = limit;
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-05-03 07:46:03 +08:00
|
|
|
pthread_mutex_unlock(&session->mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
/**
|
2014-02-16 03:44:33 +08:00
|
|
|
* @brief Gets oldest message in the list.
|
2014-02-17 09:01:30 +08:00
|
|
|
*
|
2014-01-25 08:32:33 +08:00
|
|
|
* @param session Where the list is.
|
|
|
|
* @return RTPMessage* The message. You _must_ call rtp_msg_free() to free it.
|
|
|
|
* @retval NULL No messages in the list, or no list.
|
|
|
|
*/
|
2014-02-17 09:01:30 +08:00
|
|
|
RTPMessage *rtp_recv_msg ( RTPSession *session )
|
2014-01-25 08:32:33 +08:00
|
|
|
{
|
2014-05-03 07:46:03 +08:00
|
|
|
if ( !session ) {
|
|
|
|
LOGGER_WARNING("No session!");
|
2014-01-25 08:32:33 +08:00
|
|
|
return NULL;
|
2014-05-03 07:46:03 +08:00
|
|
|
}
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
pthread_mutex_lock(&session->mutex);
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-05-03 07:46:03 +08:00
|
|
|
if ( session->queue_size == 0 ) {
|
2014-05-17 01:56:40 +08:00
|
|
|
pthread_mutex_unlock(&session->mutex);
|
2014-05-03 07:46:03 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2014-05-26 00:27:48 +08:00
|
|
|
|
|
|
|
|
2014-05-03 07:46:03 +08:00
|
|
|
RTPMessage *_retu = session->oldest_msg;
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-05-03 07:46:03 +08:00
|
|
|
/*if (_retu)*/
|
|
|
|
session->oldest_msg = _retu->next;
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
if ( !session->oldest_msg )
|
|
|
|
session->last_msg = NULL;
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-05-11 00:00:49 +08:00
|
|
|
session->queue_size --;
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
pthread_mutex_unlock(&session->mutex);
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
return _retu;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Sends data to _RTPSession::dest
|
2014-02-17 09:01:30 +08:00
|
|
|
*
|
2014-01-25 08:32:33 +08:00
|
|
|
* @param session The session.
|
|
|
|
* @param messenger Tox* object.
|
|
|
|
* @param data The payload.
|
|
|
|
* @param length Size of the payload.
|
|
|
|
* @return int
|
|
|
|
* @retval -1 On error.
|
|
|
|
* @retval 0 On success.
|
|
|
|
*/
|
2014-02-17 09:01:30 +08:00
|
|
|
int rtp_send_msg ( RTPSession *session, Messenger *messenger, const uint8_t *data, uint16_t length )
|
2014-01-25 08:32:33 +08:00
|
|
|
{
|
2014-02-17 09:01:30 +08:00
|
|
|
RTPMessage *msg = rtp_new_message (session, data, length);
|
|
|
|
|
2014-05-26 00:27:48 +08:00
|
|
|
if ( !msg ) {
|
2014-05-03 07:46:03 +08:00
|
|
|
LOGGER_WARNING("No session!");
|
|
|
|
return -1;
|
|
|
|
}
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
uint8_t _send_data [ MAX_UDP_PACKET_SIZE ];
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
_send_data[0] = session->prefix;
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
/* Generate the right nonce */
|
2014-04-22 04:51:36 +08:00
|
|
|
uint8_t _calculated[crypto_box_NONCEBYTES];
|
|
|
|
memcpy(_calculated, session->encrypt_nonce, crypto_box_NONCEBYTES);
|
2014-01-25 08:32:33 +08:00
|
|
|
increase_nonce ( _calculated, msg->header->sequnum );
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
/* Need to skip 2 bytes that are for sequnum */
|
2014-02-16 03:44:33 +08:00
|
|
|
int encrypted_length = encrypt_data_symmetric( /* TODO: msg->length - 2 (fix this properly)*/
|
2014-02-17 09:01:30 +08:00
|
|
|
(uint8_t *) session->encrypt_key, _calculated, msg->data + 2, msg->length, _send_data + 3 );
|
|
|
|
|
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
_send_data[1] = msg->data[0];
|
|
|
|
_send_data[2] = msg->data[1];
|
2014-02-17 09:01:30 +08:00
|
|
|
|
|
|
|
|
2014-05-26 00:27:48 +08:00
|
|
|
if ( -1 == send_custom_lossy_packet(messenger, session->dest, _send_data, encrypted_length + 3) ) {
|
2014-05-03 07:46:03 +08:00
|
|
|
LOGGER_WARNING("Failed to send full packet! std error: %s", strerror(errno));
|
2014-04-10 06:15:40 +08:00
|
|
|
rtp_free_msg ( session, msg );
|
2014-01-25 08:32:33 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2014-02-17 09:01:30 +08:00
|
|
|
|
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
/* Set sequ number */
|
|
|
|
if ( session->sequnum >= MAX_SEQU_NUM ) {
|
|
|
|
session->sequnum = 0;
|
2014-04-22 04:51:36 +08:00
|
|
|
memcpy(session->encrypt_nonce, _calculated, crypto_box_NONCEBYTES);
|
2014-01-25 08:32:33 +08:00
|
|
|
} else {
|
|
|
|
session->sequnum++;
|
|
|
|
}
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
rtp_free_msg ( session, msg );
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Speaks for it self.
|
2014-02-17 09:01:30 +08:00
|
|
|
*
|
2014-02-01 19:52:48 +08:00
|
|
|
* @param session The control session msg belongs to. You set it as NULL when freeing recved messages.
|
|
|
|
* Otherwise set it to session the message was created from.
|
2014-01-25 08:32:33 +08:00
|
|
|
* @param msg The message.
|
|
|
|
* @return void
|
|
|
|
*/
|
2014-02-17 09:01:30 +08:00
|
|
|
void rtp_free_msg ( RTPSession *session, RTPMessage *msg )
|
2014-01-25 08:32:33 +08:00
|
|
|
{
|
2014-02-17 09:01:30 +08:00
|
|
|
if ( !session ) {
|
|
|
|
if ( msg->ext_header ) {
|
2014-01-25 08:32:33 +08:00
|
|
|
free ( msg->ext_header->table );
|
|
|
|
free ( msg->ext_header );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ( msg->ext_header && session->ext_header != msg->ext_header ) {
|
|
|
|
free ( msg->ext_header->table );
|
|
|
|
free ( msg->ext_header );
|
|
|
|
}
|
|
|
|
}
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
free ( msg->header );
|
|
|
|
free ( msg );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Must be called before calling any other rtp function. It's used
|
|
|
|
* to initialize RTP control session.
|
2014-02-17 09:01:30 +08:00
|
|
|
*
|
2014-01-25 08:32:33 +08:00
|
|
|
* @param payload_type Type of payload used to send. You can use values in toxmsi.h::MSICallType
|
|
|
|
* @param messenger Tox* object.
|
|
|
|
* @param friend_num Friend id.
|
|
|
|
* @param encrypt_key Speaks for it self.
|
|
|
|
* @param decrypt_key Speaks for it self.
|
|
|
|
* @param encrypt_nonce Speaks for it self.
|
|
|
|
* @param decrypt_nonce Speaks for it self.
|
|
|
|
* @return RTPSession* Created control session.
|
|
|
|
* @retval NULL Error occurred.
|
|
|
|
*/
|
2014-02-17 09:01:30 +08:00
|
|
|
RTPSession *rtp_init_session ( int payload_type,
|
|
|
|
Messenger *messenger,
|
|
|
|
int friend_num,
|
|
|
|
const uint8_t *encrypt_key,
|
|
|
|
const uint8_t *decrypt_key,
|
|
|
|
const uint8_t *encrypt_nonce,
|
|
|
|
const uint8_t *decrypt_nonce )
|
|
|
|
{
|
|
|
|
RTPSession *_retu = calloc(1, sizeof(RTPSession));
|
2014-01-25 08:32:33 +08:00
|
|
|
assert(_retu);
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-05-26 00:27:48 +08:00
|
|
|
if ( -1 == custom_lossy_packet_registerhandler(messenger, friend_num, payload_type, rtp_handle_packet, _retu) ||
|
|
|
|
!encrypt_key || !decrypt_key || !encrypt_nonce || !decrypt_nonce) {
|
2014-05-03 07:46:03 +08:00
|
|
|
LOGGER_ERROR("Error setting custom register handler for rtp session");
|
2014-02-16 03:44:33 +08:00
|
|
|
free(_retu);
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-05-17 01:56:40 +08:00
|
|
|
LOGGER_DEBUG("Registered packet handler: pt: %d; fid: %d", payload_type, friend_num);
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-02-16 03:44:33 +08:00
|
|
|
_retu->version = RTP_VERSION; /* It's always 2 */
|
|
|
|
_retu->padding = 0; /* If some additional data is needed about the packet */
|
2014-01-25 08:32:33 +08:00
|
|
|
_retu->extension = 0; /* If extension to header is needed */
|
|
|
|
_retu->cc = 1; /* Amount of contributors */
|
|
|
|
_retu->csrc = NULL; /* Container */
|
2014-02-03 03:43:16 +08:00
|
|
|
_retu->ssrc = random_int();
|
2014-01-25 08:32:33 +08:00
|
|
|
_retu->marker = 0;
|
2014-02-17 09:01:30 +08:00
|
|
|
_retu->payload_type = payload_table[payload_type];
|
|
|
|
|
2014-02-16 03:44:33 +08:00
|
|
|
_retu->dest = friend_num;
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
_retu->rsequnum = _retu->sequnum = 1;
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
_retu->ext_header = NULL; /* When needed allocate */
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
_retu->encrypt_key = encrypt_key;
|
|
|
|
_retu->decrypt_key = decrypt_key;
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
/* Need to allocate new memory */
|
2014-04-22 04:51:36 +08:00
|
|
|
_retu->encrypt_nonce = calloc ( crypto_box_NONCEBYTES, sizeof (uint8_t) );
|
2014-02-17 09:01:30 +08:00
|
|
|
assert(_retu->encrypt_nonce);
|
2014-04-22 04:51:36 +08:00
|
|
|
_retu->decrypt_nonce = calloc ( crypto_box_NONCEBYTES, sizeof (uint8_t) );
|
2014-02-17 09:01:30 +08:00
|
|
|
assert(_retu->decrypt_nonce);
|
2014-04-22 04:51:36 +08:00
|
|
|
_retu->nonce_cycle = calloc ( crypto_box_NONCEBYTES, sizeof (uint8_t) );
|
2014-02-17 09:01:30 +08:00
|
|
|
assert(_retu->nonce_cycle);
|
|
|
|
|
2014-04-22 04:51:36 +08:00
|
|
|
memcpy(_retu->encrypt_nonce, encrypt_nonce, crypto_box_NONCEBYTES);
|
|
|
|
memcpy(_retu->decrypt_nonce, decrypt_nonce, crypto_box_NONCEBYTES);
|
|
|
|
memcpy(_retu->nonce_cycle , decrypt_nonce, crypto_box_NONCEBYTES);
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-02-01 19:52:48 +08:00
|
|
|
_retu->csrc = calloc(1, sizeof (uint32_t));
|
2014-01-25 08:32:33 +08:00
|
|
|
assert(_retu->csrc);
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
_retu->csrc[0] = _retu->ssrc; /* Set my ssrc to the list receive */
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
/* Also set payload type as prefix */
|
|
|
|
_retu->prefix = payload_type;
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
_retu->oldest_msg = _retu->last_msg = NULL;
|
2014-05-03 07:46:03 +08:00
|
|
|
_retu->queue_limit = 100; /* Default */
|
|
|
|
_retu->queue_size = 0;
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
pthread_mutex_init(&_retu->mutex, NULL);
|
|
|
|
/*
|
2014-02-17 09:01:30 +08:00
|
|
|
*
|
2014-01-25 08:32:33 +08:00
|
|
|
*/
|
|
|
|
return _retu;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Terminate the session.
|
2014-02-17 09:01:30 +08:00
|
|
|
*
|
2014-01-25 08:32:33 +08:00
|
|
|
* @param session The session.
|
|
|
|
* @param messenger The messenger who owns the session
|
|
|
|
* @return int
|
|
|
|
* @retval -1 Error occurred.
|
|
|
|
* @retval 0 Success.
|
|
|
|
*/
|
2014-02-17 09:01:30 +08:00
|
|
|
int rtp_terminate_session ( RTPSession *session, Messenger *messenger )
|
2014-01-25 08:32:33 +08:00
|
|
|
{
|
2014-05-03 07:46:03 +08:00
|
|
|
if ( !session ) {
|
|
|
|
LOGGER_WARNING("No session!");
|
2014-01-25 08:32:33 +08:00
|
|
|
return -1;
|
2014-05-03 07:46:03 +08:00
|
|
|
}
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-05-22 22:18:22 +08:00
|
|
|
custom_lossy_packet_registerhandler(messenger, session->dest, session->prefix, NULL, NULL);
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-03-07 10:13:04 +08:00
|
|
|
rtp_release_session_recv(session);
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-03-07 10:13:04 +08:00
|
|
|
pthread_mutex_lock(&session->mutex);
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
free ( session->ext_header );
|
|
|
|
free ( session->csrc );
|
|
|
|
free ( session->decrypt_nonce );
|
|
|
|
free ( session->encrypt_nonce );
|
|
|
|
free ( session->nonce_cycle );
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-03-07 10:13:04 +08:00
|
|
|
pthread_mutex_unlock(&session->mutex);
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
pthread_mutex_destroy(&session->mutex);
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
/* And finally free session */
|
|
|
|
free ( session );
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
return 0;
|
2014-03-28 06:29:18 +08:00
|
|
|
}
|