[vlc-devel] [PATCH 1/2] amt: refactor to account for packet size
Alexandre Janniaux
ajanni at videolabs.io
Wed Mar 25 13:01:35 CET 2020
+ remove signed/unsigned implicit conversion from recv on Windows
+ ensure usage of size_t where needed
+ ensure packet size is no more than what was read
+ ensure zero-sized datagrams are removed too
---
modules/access/amt.c | 26 ++++++++++++++------------
1 file changed, 14 insertions(+), 12 deletions(-)
diff --git a/modules/access/amt.c b/modules/access/amt.c
index 7706bfd573..b5ed878ce8 100644
--- a/modules/access/amt.c
+++ b/modules/access/amt.c
@@ -553,7 +553,7 @@ static int Control( stream_t *p_access, int i_query, va_list args )
static block_t *BlockAMT(stream_t *p_access, bool *restrict eof)
{
access_sys_t *sys = p_access->p_sys;
- ssize_t len = 0, shift = 0, tunnel = IP_HDR_LEN + UDP_HDR_LEN + AMT_HDR_LEN;
+ size_t tunnel = IP_HDR_LEN + UDP_HDR_LEN + AMT_HDR_LEN;
/* Allocate anticipated MTU buffer for holding the UDP packet suitable for native or AMT tunneled multicast */
block_t *pkt = block_Alloc( sys->mtu + tunnel );
@@ -591,17 +591,17 @@ static block_t *BlockAMT(stream_t *p_access, bool *restrict eof)
if( sys->tryAMT )
{
/* AMT is a wrapper for UDP streams, so recv is used. */
- len = recv( sys->sAMT, pkt->p_buffer, sys->mtu + tunnel, 0 );
+ ssize_t len = vlc_recv_i11e( sys->sAMT, pkt->p_buffer, sys->mtu + tunnel, 0 );
/* Check for the integrity of the received AMT packet */
- if( len < 0 || *(pkt->p_buffer) != AMT_MULT_DATA )
+ if( len <= 0 || *(pkt->p_buffer) != AMT_MULT_DATA )
goto error;
- /* Set the offet to the first byte of the payload */
- shift += tunnel;
+ /* Ensure packet size is no more than what was read */
+ pkt->i_buffer = (size_t)len;
/* If the length received is less than the AMT tunnel header then it's truncated */
- if( len < tunnel )
+ if( pkt->i_buffer < tunnel )
{
msg_Err(p_access, "%zd bytes packet truncated (MTU was %zd)", len, sys->mtu);
pkt->i_flags |= BLOCK_FLAG_CORRUPTED;
@@ -610,7 +610,9 @@ static block_t *BlockAMT(stream_t *p_access, bool *restrict eof)
/* Otherwise subtract the length of the AMT encapsulation from the packet received */
else
{
- len -= tunnel;
+ /* Set the offset to payload start */
+ pkt->p_buffer += tunnel;
+ pkt->i_buffer -= tunnel;
}
}
/* Otherwise pull native multicast */
@@ -618,12 +620,12 @@ static block_t *BlockAMT(stream_t *p_access, bool *restrict eof)
{
struct sockaddr temp;
socklen_t temp_size = sizeof( struct sockaddr );
- len = recvfrom( sys->sAMT, (char *)pkt->p_buffer, sys->mtu + tunnel, 0, (struct sockaddr*)&temp, &temp_size );
- }
+ ssize_t len = vlc_recvfrom_i11e( sys->sAMT, pkt->p_buffer, sys->mtu + tunnel, 0, (struct sockaddr*)&temp, &temp_size );
+ if( len < 0 )
+ goto error;
- /* Set the offset to payload start */
- pkt->p_buffer += shift;
- pkt->i_buffer -= shift;
+ pkt->i_buffer = (size_t)len;
+ }
return pkt;
--
2.26.0
More information about the vlc-devel
mailing list