Skip to content

Commit

Permalink
Renames pxSocket->u.xUDP.ucMulticastTTL to xMulticastTTL so it can be…
Browse files Browse the repository at this point in the history
… used for both IPv4 and IPv6

Adds more socket option defines to cover IPv6 as well as source-specific multicast
  • Loading branch information
Emil Popov committed Oct 13, 2023
1 parent 0c84933 commit 4e210f9
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 10 deletions.
2 changes: 1 addition & 1 deletion source/FreeRTOS_DNS_Networking.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
#if ( ipconfigSUPPORT_IP_MULTICAST != 0 )
/* Since this socket may be used for LLMNR or mDNS, set the multicast TTL to 1. */
uint8_t ucMulticastTTL = 1;
( void ) FreeRTOS_setsockopt( xSocket, 0, FREERTOS_SO_IP_MULTICAST_TTL, &( ucMulticastTTL ), sizeof( uint8_t ) );
( void ) FreeRTOS_setsockopt( xSocket, 0, FREERTOS_SO_IP_MULTICAST_TTL, &( ucMulticastTTL ), sizeof( ucMulticastTTL ) );
#endif
}

Expand Down
40 changes: 36 additions & 4 deletions source/FreeRTOS_Sockets.c
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,7 @@ Socket_t FreeRTOS_socket( BaseType_t xDomain,
#endif /* ipconfigUDP_MAX_RX_PACKETS > 0 */

#if ( ipconfigSUPPORT_IP_MULTICAST != 0 )
pxSocket->u.xUDP.ucMulticastTTL = ipconfigMULTICAST_DEFAULT_TTL;
pxSocket->u.xUDP.xMulticastTTL = ipconfigMULTICAST_DEFAULT_TTL;
vListInitialise( &pxSocket->u.xUDP.xMulticastGroupsList );
#endif
}
Expand Down Expand Up @@ -1450,7 +1450,7 @@ static int32_t prvSendUDPPacket( const FreeRTOS_Socket_t * pxSocket,
#if ( ipconfigSUPPORT_IP_MULTICAST != 0 )
if( xIsIPv4Multicast( pxDestinationAddress->sin_address.ulIP_IPv4 ) )
{
pxNetworkBuffer->ucSendTTL = pxSocket->u.xUDP.ucMulticastTTL;
pxNetworkBuffer->ucSendTTL = ( uint8_t ) pxSocket->u.xUDP.xMulticastTTL;
}
else
{
Expand Down Expand Up @@ -2969,6 +2969,7 @@ BaseType_t FreeRTOS_setsockopt( Socket_t xSocket,

#if ( ipconfigSUPPORT_IP_MULTICAST != 0 )
case FREERTOS_SO_IP_MULTICAST_TTL:
case FREERTOS_SO_IPV6_MULTICAST_HOPS:
case FREERTOS_SO_IP_ADD_MEMBERSHIP:
case FREERTOS_SO_IP_DROP_MEMBERSHIP:
xReturn = prvSetMulticastSocketOption( xSocket, lLevel, lOptionName, pvOptionValue, uxOptionLength );
Expand Down Expand Up @@ -6420,15 +6421,46 @@ void * pvSocketGetSocketID( const ConstSocket_t xSocket )
{
case FREERTOS_SO_IP_MULTICAST_TTL:

if( ( pxSocket->ucProtocol != ( uint8_t ) FREERTOS_IPPROTO_UDP ) || ( uxOptionLength != sizeof( uint8_t ) ) )
{
uint8_t ucValue = *( ( uint8_t * ) pvOptionValue );

if( ( ( pxSocket->bits.bIsIPv6 == pdTRUE_UNSIGNED ) || pxSocket->ucProtocol != ( uint8_t ) FREERTOS_IPPROTO_UDP ) || ( uxOptionLength != sizeof( ucValue ) ) )
{
break; /* will return -pdFREERTOS_ERRNO_EINVAL */
}

/* Override the default TTL value with this one. */
pxSocket->u.xUDP.ucMulticastTTL = *( ( uint8_t * ) pvOptionValue );
pxSocket->u.xUDP.xMulticastTTL = ( BaseType_t ) ucValue;

xReturn = 0;
}
break;

case FREERTOS_SO_IPV6_MULTICAST_HOPS:
{
BaseType_t xValue = *( ( BaseType_t * ) pvOptionValue );

if( ( pxSocket->bits.bIsIPv6 == pdFALSE_UNSIGNED ) || ( pxSocket->ucProtocol != ( uint8_t ) FREERTOS_IPPROTO_UDP ) || ( uxOptionLength != sizeof( BaseType_t ) ) )
{
break; /* will return -pdFREERTOS_ERRNO_EINVAL */
}

if ( ( xValue < -1 ) || ( xValue > 255 ) )
{
break; /* will return -pdFREERTOS_ERRNO_EINVAL */
}else if ( xValue == -1 )
{
/* Set the default TTL value. */
pxSocket->u.xUDP.xMulticastTTL = ipconfigUDP_TIME_TO_LIVE;
}
else
{
/* Override the default TTL value with this one. */
pxSocket->u.xUDP.xMulticastTTL = xValue;
}

xReturn = 0;
}
break;

case FREERTOS_SO_IP_ADD_MEMBERSHIP:
Expand Down
3 changes: 2 additions & 1 deletion source/include/FreeRTOSIPConfigDefaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -1169,7 +1169,8 @@

/* Specifies the TTL value that will be used for multicast
* UDP packets by default. Can be overridden per socket by
* setting the FREERTOS_SO_IP_MULTICAST_TTL socket option. */
* setting the FREERTOS_SO_IP_MULTICAST_TTL socket option or by
* setting the FREERTOS_SO_IPV6_MULTICAST_HOPS in case of an IPv6 socket. */
#ifndef ipconfigMULTICAST_DEFAULT_TTL
#define ipconfigMULTICAST_DEFAULT_TTL ( 1 )
#endif
Expand Down
10 changes: 7 additions & 3 deletions source/include/FreeRTOS_IP_Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -649,10 +649,14 @@ typedef struct UDPSOCKET
#endif /* ipconfigUSE_CALLBACKS */
#if ( ipconfigSUPPORT_IP_MULTICAST != 0 )
List_t xMulticastGroupsList;
uint8_t ucMulticastTTL; /**< Allows for multicast sockets to use a different TTL value to limit the scope of the multicast packet. Usually set to 1.
BaseType_t xMulticastTTL; /**< Allows for multicast sockets to use a different TTL value to limit the scope of the multicast packet. Usually set to 1.
* Note that the options are of different sizes for IPv4 and IPv6.
* Example:
* uint8_t ucMulticastTTL = 1;
* FreeRTOS_setsockopt( MCastSendSock, 0, FREERTOS_SO_IP_MULTICAST_TTL, ( void * ) &ucMulticastTTL, sizeof( uint8_t ) );
* uint8_t ttl = 1;
* FreeRTOS_setsockopt( MCastSendSock, 0, FREERTOS_SO_IP_MULTICAST_TTL, ( void * ) &ttl, sizeof( ttl ) );
* IPv6 Example:
* BaseType_t hops = 1;
* FreeRTOS_setsockopt( MCastSendSock_v6, 0, FREERTOS_SO_IPV6_MULTICAST_HOPS, ( void * ) &hops, sizeof( hops ) );
*/
#endif
} IPUDPSocket_t;
Expand Down
7 changes: 6 additions & 1 deletion source/include/FreeRTOS_Sockets.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,12 @@
#if ( ipconfigSUPPORT_IP_MULTICAST != 0 )
#define FREERTOS_SO_IP_MULTICAST_TTL ( 19 ) /* TTL value to me used when sending multicast packets. Defaults to ipconfigMULTICAST_DEFAULT_TTL */
#define FREERTOS_SO_IP_ADD_MEMBERSHIP ( 20 ) /* Mark the socket as able to receive multicast messages from a multicast group address */
#define FREERTOS_SO_IP_DROP_MEMBERSHIP ( 21 ) /* Remove membership from a multicast group address */
#define FREERTOS_SO_IP_ADD_SOURCE_MEMBERSHIP ( 21 ) /* not implemented */
#define FREERTOS_SO_IP_DROP_MEMBERSHIP ( 22 ) /* Remove membership from a multicast group address */
#define FREERTOS_SO_IP_DROP_SOURCE_MEMBERSHIP ( 23 ) /* not implemented */
#define FREERTOS_SO_IPV6_ADD_MEMBERSHIP ( 24 ) /* Same as FREERTOS_SO_IP_ADD_MEMBERSHIP but for IPv6 */
#define FREERTOS_SO_IPV6_DROP_MEMBERSHIP ( 25 ) /* Same as FREERTOS_SO_IP_DROP_MEMBERSHIP but for IPv6 */
#define FREERTOS_SO_IPV6_MULTICAST_HOPS ( 26 ) /* Similar to FREERTOS_SO_IP_MULTICAST_TTL but for IPv6. Defaults to ipconfigMULTICAST_DEFAULT_TTL */
#endif /* (ipconfigSUPPORT_IP_MULTICAST != 0) */

#define FREERTOS_INADDR_ANY ( 0U ) /* The 0.0.0.0 IPv4 address. */
Expand Down

0 comments on commit 4e210f9

Please sign in to comment.