Skip to content

Commit

Permalink
Centralize location of various packet detection routines.
Browse files Browse the repository at this point in the history
Don't accept RTCP as it was a valid RTP packet for purposes
of stats and latching. This obviously produces bogus results
and latch is going to flap.
  • Loading branch information
sobomax committed Jul 8, 2024
1 parent b0f4b06 commit bd900ee
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/rtp.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ typedef enum {
RTP_PARSER_PTOOSHRTXH = -4,
RTP_PARSER_PTOOSHRTPS = -5,
RTP_PARSER_PTOOSHRTP = -6,
RTP_PARSER_IPS = -7
RTP_PARSER_IPS = -7,
RTP_PARSER_ISRTCP = -8,
} rtp_parser_err_t;

struct rtp_packet;
Expand Down
67 changes: 67 additions & 0 deletions src/rtp_packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
*
*/

#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
Expand All @@ -36,13 +37,16 @@
#include "rtp_info.h"
#include "rtpp_time.h"
#include "rtp_packet.h"
#include "rtpp_packetops.h"
#include "rtpp_types.h"
#include "rtpp_mallocs.h"
#include "rtpp_refcnt.h"

#include "rtpp_wi.h"
#include "rtpp_wi_private.h"

#include "advanced/pproc_manager.h"

struct rtp_packet_full;

struct rtp_packet_priv {
Expand Down Expand Up @@ -125,9 +129,72 @@ rtp_packet_parse(struct rtp_packet *pkt)
assert(pkt->parsed == NULL);
pkt_full = (void *)pkt;
rinfo = &(pkt_full->pvt.rinfo);
if (rtp_packet_is_rtcp(pkt)) {
pkt->parse_result = RTP_PARSER_ISRTCP;
return (pkt->parse_result);
}
pkt->parse_result = rtp_packet_parse_raw(pkt->data.buf, pkt->size, rinfo);
if (pkt->parse_result == RTP_PARSER_OK) {
pkt->parsed = rinfo;
}
return (pkt->parse_result);
}

int
rtp_packet_is_rtcp(const struct rtp_packet *pkt)
{
if (pkt->size < 2)
return false;

uint8_t version = (pkt->data.buf[0] >> 6) & 0b11;
uint8_t packet_type = pkt->data.buf[1];

// Version should be 2 and RTCP packet types are in the range 200-204
if (version == 2 && packet_type >= 200 && packet_type <= 204)
return true;
return false;
}

int
rtpp_is_rtcp_tst(struct pkt_proc_ctx *pktx)
{
return rtp_packet_is_rtcp(pktx->pktp);
}

#define STUN_MAGIC 0x2112A442

int
rtp_packet_is_stun(const struct rtp_packet *pkt)
{
if (pkt->size < 20)
return (false);

if (ntohl(*(uint32_t *)(pkt->data.buf + 4)) != STUN_MAGIC)
return (false);
return (true);
}

int
rtpp_is_stun_tst(struct pkt_proc_ctx *pktx)
{
return rtp_packet_is_stun(pktx->pktp);
}

int
rtp_packet_is_dtls(const struct rtp_packet *pkt)
{
uint8_t b;

if (pkt->size < 13)
return false;

b = pkt->data.buf[0];

return (19 < b && b < 64);
}

int
rtpp_is_dtls_tst(struct pkt_proc_ctx *pktx)
{
return rtp_packet_is_dtls(pktx->pktp);
}
38 changes: 38 additions & 0 deletions src/rtpp_packetops.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2007-2024 Sippy Software, Inc., http://www.sippysoft.com
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/

#pragma once

struct rtp_packet;
struct pkt_proc_ctx;

int rtp_packet_is_rtcp(const struct rtp_packet *pkt);
int rtpp_is_rtcp_tst(struct pkt_proc_ctx *pktx);
int rtp_packet_is_stun(const struct rtp_packet *pkt);
int rtpp_is_stun_tst(struct pkt_proc_ctx *pktx);
int rtp_packet_is_dtls(const struct rtp_packet *pkt);
int rtpp_is_dtls_tst(struct pkt_proc_ctx *pktx);

0 comments on commit bd900ee

Please sign in to comment.