Skip to content

Commit 4e210f9

Browse files
author
Emil Popov
committed
Renames pxSocket->u.xUDP.ucMulticastTTL to xMulticastTTL so it can be used for both IPv4 and IPv6
Adds more socket option defines to cover IPv6 as well as source-specific multicast
1 parent 0c84933 commit 4e210f9

File tree

5 files changed

+52
-10
lines changed

5 files changed

+52
-10
lines changed

source/FreeRTOS_DNS_Networking.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
#if ( ipconfigSUPPORT_IP_MULTICAST != 0 )
8888
/* Since this socket may be used for LLMNR or mDNS, set the multicast TTL to 1. */
8989
uint8_t ucMulticastTTL = 1;
90-
( void ) FreeRTOS_setsockopt( xSocket, 0, FREERTOS_SO_IP_MULTICAST_TTL, &( ucMulticastTTL ), sizeof( uint8_t ) );
90+
( void ) FreeRTOS_setsockopt( xSocket, 0, FREERTOS_SO_IP_MULTICAST_TTL, &( ucMulticastTTL ), sizeof( ucMulticastTTL ) );
9191
#endif
9292
}
9393

source/FreeRTOS_Sockets.c

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,7 @@ Socket_t FreeRTOS_socket( BaseType_t xDomain,
750750
#endif /* ipconfigUDP_MAX_RX_PACKETS > 0 */
751751

752752
#if ( ipconfigSUPPORT_IP_MULTICAST != 0 )
753-
pxSocket->u.xUDP.ucMulticastTTL = ipconfigMULTICAST_DEFAULT_TTL;
753+
pxSocket->u.xUDP.xMulticastTTL = ipconfigMULTICAST_DEFAULT_TTL;
754754
vListInitialise( &pxSocket->u.xUDP.xMulticastGroupsList );
755755
#endif
756756
}
@@ -1450,7 +1450,7 @@ static int32_t prvSendUDPPacket( const FreeRTOS_Socket_t * pxSocket,
14501450
#if ( ipconfigSUPPORT_IP_MULTICAST != 0 )
14511451
if( xIsIPv4Multicast( pxDestinationAddress->sin_address.ulIP_IPv4 ) )
14521452
{
1453-
pxNetworkBuffer->ucSendTTL = pxSocket->u.xUDP.ucMulticastTTL;
1453+
pxNetworkBuffer->ucSendTTL = ( uint8_t ) pxSocket->u.xUDP.xMulticastTTL;
14541454
}
14551455
else
14561456
{
@@ -2969,6 +2969,7 @@ BaseType_t FreeRTOS_setsockopt( Socket_t xSocket,
29692969

29702970
#if ( ipconfigSUPPORT_IP_MULTICAST != 0 )
29712971
case FREERTOS_SO_IP_MULTICAST_TTL:
2972+
case FREERTOS_SO_IPV6_MULTICAST_HOPS:
29722973
case FREERTOS_SO_IP_ADD_MEMBERSHIP:
29732974
case FREERTOS_SO_IP_DROP_MEMBERSHIP:
29742975
xReturn = prvSetMulticastSocketOption( xSocket, lLevel, lOptionName, pvOptionValue, uxOptionLength );
@@ -6420,15 +6421,46 @@ void * pvSocketGetSocketID( const ConstSocket_t xSocket )
64206421
{
64216422
case FREERTOS_SO_IP_MULTICAST_TTL:
64226423

6423-
if( ( pxSocket->ucProtocol != ( uint8_t ) FREERTOS_IPPROTO_UDP ) || ( uxOptionLength != sizeof( uint8_t ) ) )
6424+
{
6425+
uint8_t ucValue = *( ( uint8_t * ) pvOptionValue );
6426+
6427+
if( ( ( pxSocket->bits.bIsIPv6 == pdTRUE_UNSIGNED ) || pxSocket->ucProtocol != ( uint8_t ) FREERTOS_IPPROTO_UDP ) || ( uxOptionLength != sizeof( ucValue ) ) )
64246428
{
64256429
break; /* will return -pdFREERTOS_ERRNO_EINVAL */
64266430
}
64276431

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

64316435
xReturn = 0;
6436+
}
6437+
break;
6438+
6439+
case FREERTOS_SO_IPV6_MULTICAST_HOPS:
6440+
{
6441+
BaseType_t xValue = *( ( BaseType_t * ) pvOptionValue );
6442+
6443+
if( ( pxSocket->bits.bIsIPv6 == pdFALSE_UNSIGNED ) || ( pxSocket->ucProtocol != ( uint8_t ) FREERTOS_IPPROTO_UDP ) || ( uxOptionLength != sizeof( BaseType_t ) ) )
6444+
{
6445+
break; /* will return -pdFREERTOS_ERRNO_EINVAL */
6446+
}
6447+
6448+
if ( ( xValue < -1 ) || ( xValue > 255 ) )
6449+
{
6450+
break; /* will return -pdFREERTOS_ERRNO_EINVAL */
6451+
}else if ( xValue == -1 )
6452+
{
6453+
/* Set the default TTL value. */
6454+
pxSocket->u.xUDP.xMulticastTTL = ipconfigUDP_TIME_TO_LIVE;
6455+
}
6456+
else
6457+
{
6458+
/* Override the default TTL value with this one. */
6459+
pxSocket->u.xUDP.xMulticastTTL = xValue;
6460+
}
6461+
6462+
xReturn = 0;
6463+
}
64326464
break;
64336465

64346466
case FREERTOS_SO_IP_ADD_MEMBERSHIP:

source/include/FreeRTOSIPConfigDefaults.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1169,7 +1169,8 @@
11691169

11701170
/* Specifies the TTL value that will be used for multicast
11711171
* UDP packets by default. Can be overridden per socket by
1172-
* setting the FREERTOS_SO_IP_MULTICAST_TTL socket option. */
1172+
* setting the FREERTOS_SO_IP_MULTICAST_TTL socket option or by
1173+
* setting the FREERTOS_SO_IPV6_MULTICAST_HOPS in case of an IPv6 socket. */
11731174
#ifndef ipconfigMULTICAST_DEFAULT_TTL
11741175
#define ipconfigMULTICAST_DEFAULT_TTL ( 1 )
11751176
#endif

source/include/FreeRTOS_IP_Private.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -649,10 +649,14 @@ typedef struct UDPSOCKET
649649
#endif /* ipconfigUSE_CALLBACKS */
650650
#if ( ipconfigSUPPORT_IP_MULTICAST != 0 )
651651
List_t xMulticastGroupsList;
652-
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.
652+
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.
653+
* Note that the options are of different sizes for IPv4 and IPv6.
653654
* Example:
654-
* uint8_t ucMulticastTTL = 1;
655-
* FreeRTOS_setsockopt( MCastSendSock, 0, FREERTOS_SO_IP_MULTICAST_TTL, ( void * ) &ucMulticastTTL, sizeof( uint8_t ) );
655+
* uint8_t ttl = 1;
656+
* FreeRTOS_setsockopt( MCastSendSock, 0, FREERTOS_SO_IP_MULTICAST_TTL, ( void * ) &ttl, sizeof( ttl ) );
657+
* IPv6 Example:
658+
* BaseType_t hops = 1;
659+
* FreeRTOS_setsockopt( MCastSendSock_v6, 0, FREERTOS_SO_IPV6_MULTICAST_HOPS, ( void * ) &hops, sizeof( hops ) );
656660
*/
657661
#endif
658662
} IPUDPSocket_t;

source/include/FreeRTOS_Sockets.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,12 @@
154154
#if ( ipconfigSUPPORT_IP_MULTICAST != 0 )
155155
#define FREERTOS_SO_IP_MULTICAST_TTL ( 19 ) /* TTL value to me used when sending multicast packets. Defaults to ipconfigMULTICAST_DEFAULT_TTL */
156156
#define FREERTOS_SO_IP_ADD_MEMBERSHIP ( 20 ) /* Mark the socket as able to receive multicast messages from a multicast group address */
157-
#define FREERTOS_SO_IP_DROP_MEMBERSHIP ( 21 ) /* Remove membership from a multicast group address */
157+
#define FREERTOS_SO_IP_ADD_SOURCE_MEMBERSHIP ( 21 ) /* not implemented */
158+
#define FREERTOS_SO_IP_DROP_MEMBERSHIP ( 22 ) /* Remove membership from a multicast group address */
159+
#define FREERTOS_SO_IP_DROP_SOURCE_MEMBERSHIP ( 23 ) /* not implemented */
160+
#define FREERTOS_SO_IPV6_ADD_MEMBERSHIP ( 24 ) /* Same as FREERTOS_SO_IP_ADD_MEMBERSHIP but for IPv6 */
161+
#define FREERTOS_SO_IPV6_DROP_MEMBERSHIP ( 25 ) /* Same as FREERTOS_SO_IP_DROP_MEMBERSHIP but for IPv6 */
162+
#define FREERTOS_SO_IPV6_MULTICAST_HOPS ( 26 ) /* Similar to FREERTOS_SO_IP_MULTICAST_TTL but for IPv6. Defaults to ipconfigMULTICAST_DEFAULT_TTL */
158163
#endif /* (ipconfigSUPPORT_IP_MULTICAST != 0) */
159164

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

0 commit comments

Comments
 (0)