2017-01-14 23:46:31 +08:00
|
|
|
/*
|
2018-01-22 06:38:08 +08:00
|
|
|
* Copyright © 2016-2018 The TokTok team.
|
2017-01-14 23:46:31 +08:00
|
|
|
* Copyright © 2013-2015 Tox project.
|
2014-02-17 09:01:30 +08:00
|
|
|
*
|
2017-01-14 23:46:31 +08:00
|
|
|
* This file is part of Tox, the free peer to peer instant messenger.
|
2014-01-25 08:32:33 +08:00
|
|
|
*
|
2017-01-14 23:46:31 +08:00
|
|
|
* 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.
|
2014-01-25 08:32:33 +08:00
|
|
|
*
|
2017-01-14 23:46:31 +08:00
|
|
|
* 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.
|
2014-01-25 08:32:33 +08:00
|
|
|
*
|
2017-01-14 23:46:31 +08:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Tox. If not, see <http://www.gnu.org/licenses/>.
|
2014-01-25 08:32:33 +08:00
|
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif /* HAVE_CONFIG_H */
|
|
|
|
|
2016-09-11 22:47:51 +08:00
|
|
|
#include "rtp.h"
|
|
|
|
|
|
|
|
#include "bwcontroller.h"
|
|
|
|
|
2016-09-01 07:33:20 +08:00
|
|
|
#include "../toxcore/Messenger.h"
|
2014-05-03 07:46:03 +08:00
|
|
|
#include "../toxcore/logger.h"
|
2014-05-31 23:27:22 +08:00
|
|
|
#include "../toxcore/util.h"
|
2014-05-03 07:46:03 +08:00
|
|
|
|
2015-04-26 06:31:03 +08:00
|
|
|
#include <assert.h>
|
2017-01-04 00:26:15 +08:00
|
|
|
#include <errno.h>
|
2016-09-01 07:33:20 +08:00
|
|
|
#include <stdlib.h>
|
2014-01-25 08:32:33 +08:00
|
|
|
|
|
|
|
|
2018-01-22 06:38:08 +08:00
|
|
|
size_t rtp_header_pack(uint8_t *const rdata, const struct RTPHeader *header)
|
|
|
|
{
|
|
|
|
uint8_t *p = rdata;
|
|
|
|
*p++ = (header->protocol_version & 3) << 6
|
|
|
|
| (header->pe & 1) << 5
|
|
|
|
| (header->xe & 1) << 4
|
|
|
|
| (header->cc & 0xf);
|
|
|
|
*p++ = (header->ma & 1) << 7
|
|
|
|
| (header->pt & 0x7f);
|
|
|
|
|
|
|
|
p += net_pack_u16(p, header->sequnum);
|
|
|
|
p += net_pack_u32(p, header->timestamp);
|
|
|
|
p += net_pack_u32(p, header->ssrc);
|
|
|
|
p += net_pack_u64(p, header->flags);
|
|
|
|
p += net_pack_u32(p, header->offset_full);
|
|
|
|
p += net_pack_u32(p, header->data_length_full);
|
|
|
|
p += net_pack_u32(p, header->received_length_full);
|
|
|
|
|
2018-02-02 23:37:19 +08:00
|
|
|
for (size_t i = 0; i < RTP_PADDING_FIELDS; i++) {
|
|
|
|
p += net_pack_u32(p, 0);
|
2018-01-22 06:38:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
p += net_pack_u16(p, header->offset_lower);
|
|
|
|
p += net_pack_u16(p, header->data_length_lower);
|
|
|
|
assert(p == rdata + RTP_HEADER_SIZE);
|
|
|
|
return p - rdata;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_t rtp_header_unpack(const uint8_t *data, struct RTPHeader *header)
|
|
|
|
{
|
|
|
|
const uint8_t *p = data;
|
|
|
|
header->protocol_version = (*p >> 6) & 3;
|
|
|
|
header->pe = (*p >> 5) & 1;
|
|
|
|
header->xe = (*p >> 4) & 1;
|
|
|
|
header->cc = *p & 0xf;
|
|
|
|
++p;
|
|
|
|
|
|
|
|
header->ma = (*p >> 7) & 1;
|
|
|
|
header->pt = *p & 0x7f;
|
|
|
|
++p;
|
|
|
|
|
|
|
|
p += net_unpack_u16(p, &header->sequnum);
|
|
|
|
p += net_unpack_u32(p, &header->timestamp);
|
|
|
|
p += net_unpack_u32(p, &header->ssrc);
|
|
|
|
p += net_unpack_u64(p, &header->flags);
|
|
|
|
p += net_unpack_u32(p, &header->offset_full);
|
|
|
|
p += net_unpack_u32(p, &header->data_length_full);
|
|
|
|
p += net_unpack_u32(p, &header->received_length_full);
|
|
|
|
|
2018-02-02 23:37:19 +08:00
|
|
|
p += sizeof(uint32_t) * RTP_PADDING_FIELDS;
|
2018-01-22 06:38:08 +08:00
|
|
|
|
|
|
|
p += net_unpack_u16(p, &header->offset_lower);
|
|
|
|
p += net_unpack_u16(p, &header->data_length_lower);
|
|
|
|
assert(p == data + RTP_HEADER_SIZE);
|
|
|
|
return p - data;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-20 04:49:04 +08:00
|
|
|
int handle_rtp_packet(Messenger *m, uint32_t friendnumber, const uint8_t *data, uint16_t length, void *object);
|
2014-01-25 08:32:33 +08:00
|
|
|
|
|
|
|
|
2016-09-13 04:37:58 +08:00
|
|
|
RTPSession *rtp_new(int payload_type, Messenger *m, uint32_t friendnumber,
|
|
|
|
BWController *bwc, void *cs,
|
|
|
|
int (*mcb)(void *, struct RTPMessage *))
|
2014-01-25 08:32:33 +08:00
|
|
|
{
|
2015-04-26 06:31:03 +08:00
|
|
|
assert(mcb);
|
|
|
|
assert(cs);
|
2015-04-29 07:01:25 +08:00
|
|
|
assert(m);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2016-09-18 08:31:55 +08:00
|
|
|
RTPSession *retu = (RTPSession *)calloc(1, sizeof(RTPSession));
|
2015-04-13 07:45:53 +08:00
|
|
|
|
2015-10-11 05:54:23 +08:00
|
|
|
if (!retu) {
|
2016-08-19 20:07:45 +08:00
|
|
|
LOGGER_WARNING(m->log, "Alloc failed! Program might misbehave!");
|
2018-01-29 05:30:39 +08:00
|
|
|
return nullptr;
|
2015-04-13 07:45:53 +08:00
|
|
|
}
|
|
|
|
|
2018-01-15 05:08:02 +08:00
|
|
|
retu->ssrc = random_u32();
|
2015-10-11 05:54:23 +08:00
|
|
|
retu->payload_type = payload_type;
|
2015-04-13 07:45:53 +08:00
|
|
|
|
2015-10-11 05:54:23 +08:00
|
|
|
retu->m = m;
|
|
|
|
retu->friend_number = friendnumber;
|
2015-04-13 07:45:53 +08:00
|
|
|
|
|
|
|
/* Also set payload type as prefix */
|
2015-10-11 05:54:23 +08:00
|
|
|
|
|
|
|
retu->bwc = bwc;
|
2015-04-26 06:31:03 +08:00
|
|
|
retu->cs = cs;
|
|
|
|
retu->mcb = mcb;
|
2015-10-11 05:54:23 +08:00
|
|
|
|
|
|
|
if (-1 == rtp_allow_receiving(retu)) {
|
2016-08-19 20:07:45 +08:00
|
|
|
LOGGER_WARNING(m->log, "Failed to start rtp receiving mode");
|
2015-04-26 06:31:03 +08:00
|
|
|
free(retu);
|
2018-01-29 05:30:39 +08:00
|
|
|
return nullptr;
|
2015-04-26 06:31:03 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-04-13 07:45:53 +08:00
|
|
|
return retu;
|
2014-01-25 08:32:33 +08:00
|
|
|
}
|
2016-09-13 04:37:58 +08:00
|
|
|
void rtp_kill(RTPSession *session)
|
2015-04-13 07:45:53 +08:00
|
|
|
{
|
2016-09-01 02:12:19 +08:00
|
|
|
if (!session) {
|
2015-10-11 05:54:23 +08:00
|
|
|
return;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2016-08-19 20:07:45 +08:00
|
|
|
LOGGER_DEBUG(session->m->log, "Terminated RTP session: %p", session);
|
2015-04-13 07:45:53 +08:00
|
|
|
|
2016-09-13 04:37:58 +08:00
|
|
|
rtp_stop_receiving(session);
|
|
|
|
free(session);
|
2015-04-13 07:45:53 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
int rtp_allow_receiving(RTPSession *session)
|
2015-04-13 07:45:53 +08:00
|
|
|
{
|
2018-01-29 05:30:39 +08:00
|
|
|
if (session == nullptr) {
|
2015-04-13 07:45:53 +08:00
|
|
|
return -1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
|
|
|
if (m_callback_rtp_packet(session->m, session->friend_number, session->payload_type,
|
|
|
|
handle_rtp_packet, session) == -1) {
|
2016-08-19 20:07:45 +08:00
|
|
|
LOGGER_WARNING(session->m->log, "Failed to register rtp receive handler");
|
2015-04-13 07:45:53 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2016-08-19 20:07:45 +08:00
|
|
|
LOGGER_DEBUG(session->m->log, "Started receiving on session: %p", session);
|
2015-04-13 07:45:53 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
int rtp_stop_receiving(RTPSession *session)
|
2015-04-13 07:45:53 +08:00
|
|
|
{
|
2018-01-29 05:30:39 +08:00
|
|
|
if (session == nullptr) {
|
2015-04-13 07:45:53 +08:00
|
|
|
return -1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-01-29 05:30:39 +08:00
|
|
|
m_callback_rtp_packet(session->m, session->friend_number, session->payload_type, nullptr, nullptr);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2016-08-19 20:07:45 +08:00
|
|
|
LOGGER_DEBUG(session->m->log, "Stopped receiving on session: %p", session);
|
2015-04-13 07:45:53 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2016-11-15 15:53:47 +08:00
|
|
|
int rtp_send_data(RTPSession *session, const uint8_t *data, uint16_t length, Logger *log)
|
2015-04-13 07:45:53 +08:00
|
|
|
{
|
2015-10-11 05:54:23 +08:00
|
|
|
if (!session) {
|
2016-11-15 15:53:47 +08:00
|
|
|
LOGGER_ERROR(log, "No session!");
|
2015-04-13 07:45:53 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-01-22 06:38:08 +08:00
|
|
|
VLA(uint8_t, rdata, length + RTP_HEADER_SIZE + 1);
|
2017-01-21 05:16:55 +08:00
|
|
|
memset(rdata, 0, SIZEOF_VLA(rdata));
|
2015-04-13 07:45:53 +08:00
|
|
|
|
2015-10-11 05:54:23 +08:00
|
|
|
rdata[0] = session->payload_type;
|
2015-04-26 06:31:03 +08:00
|
|
|
|
2018-01-22 06:38:08 +08:00
|
|
|
struct RTPHeader header = {0};
|
2015-04-26 06:31:03 +08:00
|
|
|
|
2018-01-22 06:38:08 +08:00
|
|
|
header.protocol_version = 2;
|
|
|
|
header.pe = 0;
|
|
|
|
header.xe = 0;
|
|
|
|
header.cc = 0;
|
2015-04-13 07:45:53 +08:00
|
|
|
|
2018-01-22 06:38:08 +08:00
|
|
|
header.ma = 0;
|
|
|
|
header.pt = session->payload_type % 128;
|
2015-04-13 07:45:53 +08:00
|
|
|
|
2018-01-22 06:38:08 +08:00
|
|
|
header.sequnum = session->sequnum;
|
|
|
|
header.timestamp = current_time_monotonic();
|
|
|
|
header.ssrc = session->ssrc;
|
2015-04-13 07:45:53 +08:00
|
|
|
|
2018-01-22 06:38:08 +08:00
|
|
|
header.offset_lower = 0;
|
|
|
|
header.data_length_lower = length;
|
2015-04-13 07:45:53 +08:00
|
|
|
|
2018-01-22 06:38:08 +08:00
|
|
|
if (MAX_CRYPTO_DATA_SIZE > length + RTP_HEADER_SIZE + 1) {
|
2015-04-13 07:45:53 +08:00
|
|
|
|
2015-10-11 05:54:23 +08:00
|
|
|
/**
|
2017-11-07 23:56:18 +08:00
|
|
|
* The length is lesser than the maximum allowed length (including header)
|
2015-10-11 05:54:23 +08:00
|
|
|
* Send the packet in single piece.
|
|
|
|
*/
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2018-01-22 06:38:08 +08:00
|
|
|
rtp_header_pack(rdata + 1, &header);
|
|
|
|
memcpy(rdata + 1 + RTP_HEADER_SIZE, data, length);
|
2014-06-29 10:29:39 +08:00
|
|
|
|
2017-01-21 05:16:55 +08:00
|
|
|
if (-1 == m_send_custom_lossy_packet(session->m, session->friend_number, rdata, SIZEOF_VLA(rdata))) {
|
|
|
|
LOGGER_WARNING(session->m->log, "RTP send failed (len: %d)! std error: %s", SIZEOF_VLA(rdata), strerror(errno));
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
} else {
|
2014-06-29 10:29:39 +08:00
|
|
|
|
2015-10-11 05:54:23 +08:00
|
|
|
/**
|
2017-11-07 23:56:18 +08:00
|
|
|
* The length is greater than the maximum allowed length (including header)
|
2015-10-11 05:54:23 +08:00
|
|
|
* Send the packet in multiple pieces.
|
|
|
|
*/
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2015-10-11 05:54:23 +08:00
|
|
|
uint16_t sent = 0;
|
2018-01-22 06:38:08 +08:00
|
|
|
uint16_t piece = MAX_CRYPTO_DATA_SIZE - (RTP_HEADER_SIZE + 1);
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2018-01-22 06:38:08 +08:00
|
|
|
while ((length - sent) + RTP_HEADER_SIZE + 1 > MAX_CRYPTO_DATA_SIZE) {
|
|
|
|
rtp_header_pack(rdata + 1, &header);
|
|
|
|
memcpy(rdata + 1 + RTP_HEADER_SIZE, data + sent, piece);
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2016-12-04 03:46:15 +08:00
|
|
|
if (-1 == m_send_custom_lossy_packet(session->m, session->friend_number,
|
2018-01-22 06:38:08 +08:00
|
|
|
rdata, piece + RTP_HEADER_SIZE + 1)) {
|
2016-08-19 20:07:45 +08:00
|
|
|
LOGGER_WARNING(session->m->log, "RTP send failed (len: %d)! std error: %s",
|
2018-01-22 06:38:08 +08:00
|
|
|
piece + RTP_HEADER_SIZE + 1, strerror(errno));
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2015-10-11 05:54:23 +08:00
|
|
|
sent += piece;
|
2018-01-22 06:38:08 +08:00
|
|
|
header.offset_lower = sent;
|
2015-10-11 05:54:23 +08:00
|
|
|
}
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2015-10-11 05:54:23 +08:00
|
|
|
/* Send remaining */
|
|
|
|
piece = length - sent;
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2015-10-11 05:54:23 +08:00
|
|
|
if (piece) {
|
2018-01-22 06:38:08 +08:00
|
|
|
rtp_header_pack(rdata + 1, &header);
|
|
|
|
memcpy(rdata + 1 + RTP_HEADER_SIZE, data + sent, piece);
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2016-12-04 03:46:15 +08:00
|
|
|
if (-1 == m_send_custom_lossy_packet(session->m, session->friend_number, rdata,
|
2018-01-22 06:38:08 +08:00
|
|
|
piece + RTP_HEADER_SIZE + 1)) {
|
2016-08-19 20:07:45 +08:00
|
|
|
LOGGER_WARNING(session->m->log, "RTP send failed (len: %d)! std error: %s",
|
2018-01-22 06:38:08 +08:00
|
|
|
piece + RTP_HEADER_SIZE + 1, strerror(errno));
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
}
|
2014-01-25 08:32:33 +08:00
|
|
|
}
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2015-10-11 05:54:23 +08:00
|
|
|
session->sequnum ++;
|
|
|
|
return 0;
|
2014-01-25 08:32:33 +08:00
|
|
|
}
|
2014-02-17 09:01:30 +08:00
|
|
|
|
|
|
|
|
2016-09-13 04:37:58 +08:00
|
|
|
static bool chloss(const RTPSession *session, const struct RTPHeader *header)
|
2014-01-25 08:32:33 +08:00
|
|
|
{
|
2018-01-22 06:38:08 +08:00
|
|
|
if (header->timestamp < session->rtimestamp) {
|
2015-10-11 05:54:23 +08:00
|
|
|
uint16_t hosq, lost = 0;
|
2015-11-04 02:42:05 +08:00
|
|
|
|
2018-01-22 06:38:08 +08:00
|
|
|
hosq = header->sequnum;
|
2015-11-04 02:42:05 +08:00
|
|
|
|
|
|
|
lost = (hosq > session->rsequnum) ?
|
|
|
|
(session->rsequnum + 65535) - hosq :
|
|
|
|
session->rsequnum - hosq;
|
|
|
|
|
2016-09-13 04:37:58 +08:00
|
|
|
fprintf(stderr, "Lost packet\n");
|
2015-11-04 02:42:05 +08:00
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
while (lost --) {
|
2018-01-10 21:39:07 +08:00
|
|
|
bwc_add_lost(session->bwc, 0);
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-11-04 02:42:05 +08:00
|
|
|
|
2015-10-11 05:54:23 +08:00
|
|
|
return true;
|
2014-01-25 08:32:33 +08:00
|
|
|
}
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2015-10-11 05:54:23 +08:00
|
|
|
return false;
|
2014-01-25 08:32:33 +08:00
|
|
|
}
|
2016-09-13 04:37:58 +08:00
|
|
|
static struct RTPMessage *new_message(size_t allocate_len, const uint8_t *data, uint16_t data_length)
|
2014-01-25 08:32:33 +08:00
|
|
|
{
|
2015-10-11 05:54:23 +08:00
|
|
|
assert(allocate_len >= data_length);
|
|
|
|
|
2018-01-22 06:38:08 +08:00
|
|
|
struct RTPMessage *msg = (struct RTPMessage *)calloc(sizeof(struct RTPMessage) +
|
|
|
|
(allocate_len - RTP_HEADER_SIZE), 1);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-01-22 06:38:08 +08:00
|
|
|
if (msg == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-01-22 06:38:08 +08:00
|
|
|
msg->len = data_length - RTP_HEADER_SIZE;
|
|
|
|
rtp_header_unpack(data, &msg->header);
|
|
|
|
memcpy(msg->data, data + RTP_HEADER_SIZE, allocate_len - RTP_HEADER_SIZE);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
|
|
|
return msg;
|
2014-01-25 08:32:33 +08:00
|
|
|
}
|
2018-01-22 06:38:08 +08:00
|
|
|
|
2016-09-20 04:49:04 +08:00
|
|
|
int handle_rtp_packet(Messenger *m, uint32_t friendnumber, const uint8_t *data, uint16_t length, void *object)
|
2014-01-25 08:32:33 +08:00
|
|
|
{
|
2015-04-30 06:40:30 +08:00
|
|
|
(void) m;
|
|
|
|
(void) friendnumber;
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2016-09-18 08:31:55 +08:00
|
|
|
RTPSession *session = (RTPSession *)object;
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2015-10-11 05:54:23 +08:00
|
|
|
data ++;
|
|
|
|
length--;
|
|
|
|
|
2018-01-22 06:38:08 +08:00
|
|
|
if (!session || length < RTP_HEADER_SIZE) {
|
2016-08-19 20:07:45 +08:00
|
|
|
LOGGER_WARNING(m->log, "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
|
|
|
|
2018-01-22 06:38:08 +08:00
|
|
|
struct RTPHeader header;
|
|
|
|
|
|
|
|
rtp_header_unpack(data, &header);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-01-22 06:38:08 +08:00
|
|
|
if (header.pt != session->payload_type % 128) {
|
2016-08-19 20:07:45 +08:00
|
|
|
LOGGER_WARNING(m->log, "Invalid payload type with the session");
|
2014-05-03 07:46:03 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-01-22 06:38:08 +08:00
|
|
|
if (header.offset_lower >= header.data_length_lower) {
|
2015-10-11 05:54:23 +08:00
|
|
|
/* Never allow this case to happen */
|
2015-04-26 06:31:03 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
|
|
|
bwc_feed_avg(session->bwc, length);
|
2015-11-04 02:42:05 +08:00
|
|
|
|
2018-01-22 06:38:08 +08:00
|
|
|
if (header.data_length_lower == length - RTP_HEADER_SIZE) {
|
2015-10-11 05:54:23 +08:00
|
|
|
/* The message is sent in single part */
|
|
|
|
|
|
|
|
/* Only allow messages which have arrived in order;
|
|
|
|
* drop late messages
|
|
|
|
*/
|
2018-01-22 06:38:08 +08:00
|
|
|
if (chloss(session, &header)) {
|
2015-10-11 05:54:23 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-09-01 03:40:20 +08:00
|
|
|
/* Message is not late; pick up the latest parameters */
|
2018-01-22 06:38:08 +08:00
|
|
|
session->rsequnum = header.sequnum;
|
|
|
|
session->rtimestamp = header.timestamp;
|
2016-09-01 03:40:20 +08:00
|
|
|
|
2015-10-11 05:54:23 +08:00
|
|
|
bwc_add_recv(session->bwc, length);
|
2015-11-04 02:42:05 +08:00
|
|
|
|
2015-10-11 05:54:23 +08:00
|
|
|
/* Invoke processing of active multiparted message */
|
|
|
|
if (session->mp) {
|
2016-09-01 02:12:19 +08:00
|
|
|
if (session->mcb) {
|
2016-09-13 04:37:58 +08:00
|
|
|
session->mcb(session->cs, session->mp);
|
2016-09-01 02:12:19 +08:00
|
|
|
} else {
|
2015-10-11 05:54:23 +08:00
|
|
|
free(session->mp);
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-01-29 05:30:39 +08:00
|
|
|
session->mp = nullptr;
|
2015-10-11 05:54:23 +08:00
|
|
|
}
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2015-10-11 05:54:23 +08:00
|
|
|
/* The message came in the allowed time;
|
|
|
|
* process it only if handler for the session is present.
|
|
|
|
*/
|
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (!session->mcb) {
|
2015-10-11 05:54:23 +08:00
|
|
|
return 0;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2016-09-13 04:37:58 +08:00
|
|
|
return session->mcb(session->cs, new_message(length, data, length));
|
2016-09-01 03:40:20 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2016-09-01 03:40:20 +08:00
|
|
|
/* The message is sent in multiple parts */
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2016-09-01 03:40:20 +08:00
|
|
|
if (session->mp) {
|
|
|
|
/* There are 2 possible situations in this case:
|
|
|
|
* 1) being that we got the part of already processing message.
|
|
|
|
* 2) being that we got the part of a new/old message.
|
|
|
|
*
|
|
|
|
* We handle them differently as we only allow a single multiparted
|
|
|
|
* processing message
|
|
|
|
*/
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-01-22 06:38:08 +08:00
|
|
|
if (session->mp->header.sequnum == header.sequnum &&
|
|
|
|
session->mp->header.timestamp == header.timestamp) {
|
2016-09-01 03:40:20 +08:00
|
|
|
/* First case */
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2016-09-01 03:40:20 +08:00
|
|
|
/* Make sure we have enough allocated memory */
|
2018-01-22 06:38:08 +08:00
|
|
|
if (session->mp->header.data_length_lower - session->mp->len < length - RTP_HEADER_SIZE ||
|
|
|
|
session->mp->header.data_length_lower <= header.offset_lower) {
|
2016-09-01 03:40:20 +08:00
|
|
|
/* There happened to be some corruption on the stream;
|
|
|
|
* continue wihtout this part
|
|
|
|
*/
|
|
|
|
return 0;
|
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-01-22 06:38:08 +08:00
|
|
|
memcpy(session->mp->data + header.offset_lower, data + RTP_HEADER_SIZE,
|
|
|
|
length - RTP_HEADER_SIZE);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-01-22 06:38:08 +08:00
|
|
|
session->mp->len += length - RTP_HEADER_SIZE;
|
2015-11-04 02:42:05 +08:00
|
|
|
|
2016-09-01 03:40:20 +08:00
|
|
|
bwc_add_recv(session->bwc, length);
|
2015-11-04 02:42:05 +08:00
|
|
|
|
2018-01-25 10:14:18 +08:00
|
|
|
if (session->mp->len == session->mp->header.data_length_lower) {
|
2016-09-01 03:40:20 +08:00
|
|
|
/* Received a full message; now push it for the further
|
|
|
|
* processing.
|
|
|
|
*/
|
2016-09-01 02:12:19 +08:00
|
|
|
if (session->mcb) {
|
2016-09-13 04:37:58 +08:00
|
|
|
session->mcb(session->cs, session->mp);
|
2016-09-01 02:12:19 +08:00
|
|
|
} else {
|
2015-10-11 05:54:23 +08:00
|
|
|
free(session->mp);
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-01-29 05:30:39 +08:00
|
|
|
session->mp = nullptr;
|
2015-10-11 05:54:23 +08:00
|
|
|
}
|
|
|
|
} else {
|
2016-09-01 03:40:20 +08:00
|
|
|
/* Second case */
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-01-22 06:38:08 +08:00
|
|
|
if (session->mp->header.timestamp > header.timestamp) {
|
2016-09-01 03:40:20 +08:00
|
|
|
/* The received message part is from the old message;
|
|
|
|
* discard it.
|
|
|
|
*/
|
2015-10-11 05:54:23 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-09-01 03:40:20 +08:00
|
|
|
/* Measure missing parts of the old message */
|
|
|
|
bwc_add_lost(session->bwc,
|
2018-01-25 10:14:18 +08:00
|
|
|
(session->mp->header.data_length_lower - session->mp->len) +
|
2016-09-01 03:40:20 +08:00
|
|
|
|
|
|
|
/* Must account sizes of rtp headers too */
|
2018-01-25 10:14:18 +08:00
|
|
|
((session->mp->header.data_length_lower - session->mp->len) /
|
2018-01-22 06:38:08 +08:00
|
|
|
MAX_CRYPTO_DATA_SIZE) * RTP_HEADER_SIZE);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2016-09-01 03:40:20 +08:00
|
|
|
/* Push the previous message for processing */
|
2015-10-11 05:54:23 +08:00
|
|
|
if (session->mcb) {
|
2016-09-13 04:37:58 +08:00
|
|
|
session->mcb(session->cs, session->mp);
|
2016-09-01 03:40:20 +08:00
|
|
|
} else {
|
|
|
|
free(session->mp);
|
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-01-29 05:30:39 +08:00
|
|
|
session->mp = nullptr;
|
2016-09-01 03:40:20 +08:00
|
|
|
goto NEW_MULTIPARTED;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* In this case threat the message as if it was received in order
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* This is also a point for new multiparted messages */
|
|
|
|
NEW_MULTIPARTED:
|
|
|
|
|
|
|
|
/* Only allow messages which have arrived in order;
|
|
|
|
* drop late messages
|
|
|
|
*/
|
2018-01-22 06:38:08 +08:00
|
|
|
if (chloss(session, &header)) {
|
2016-09-01 03:40:20 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Message is not late; pick up the latest parameters */
|
2018-01-22 06:38:08 +08:00
|
|
|
session->rsequnum = header.sequnum;
|
|
|
|
session->rtimestamp = header.timestamp;
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2016-09-01 03:40:20 +08:00
|
|
|
bwc_add_recv(session->bwc, length);
|
|
|
|
|
|
|
|
/* Again, only store message if handler is present
|
|
|
|
*/
|
|
|
|
if (session->mcb) {
|
2018-01-22 06:38:08 +08:00
|
|
|
session->mp = new_message(header.data_length_lower + RTP_HEADER_SIZE, data, length);
|
|
|
|
memmove(session->mp->data + header.offset_lower, session->mp->data, session->mp->len);
|
2015-10-11 05:54:23 +08:00
|
|
|
}
|
2015-01-25 06:29:54 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-01-25 06:29:54 +08:00
|
|
|
return 0;
|
2015-04-26 06:31:03 +08:00
|
|
|
}
|