Skip to content

Commit

Permalink
combine prvTCPSendSpecialPacketHelper and make static
Browse files Browse the repository at this point in the history
  • Loading branch information
HTRamsey committed Feb 3, 2024
1 parent 5098912 commit cc54eae
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 212 deletions.
54 changes: 36 additions & 18 deletions source/FreeRTOS_TCP_Transmission.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,21 @@
/* Just make sure the contents doesn't get compiled if TCP is not enabled. */
#if ipconfigUSE_TCP == 1

static BaseType_t prvTCPMakeSurePrepared( FreeRTOS_Socket_t * pxSocket );

/*
* Let ARP look-up the MAC-address of the peer and initialise the first SYN
* packet.
*/
static BaseType_t prvTCPPrepareConnect( FreeRTOS_Socket_t * pxSocket );

#if ipconfigIS_ENABLED( ipconfigUSE_TCP_WIN )
static uint8_t prvWinScaleFactor( const FreeRTOS_Socket_t * pxSocket );
#endif

static BaseType_t prvTCPSendSpecialPacketHelper( NetworkBufferDescriptor_t * const pxNetworkBuffer,
uint8_t ucTCPFlags );

/*------------------------------------------------------------------------*/

/**
Expand Down Expand Up @@ -1305,35 +1314,44 @@
*
* @return pdFAIL always indicating that the packet was not consumed.
*/
BaseType_t prvTCPSendSpecialPacketHelper( NetworkBufferDescriptor_t * pxNetworkBuffer,
uint8_t ucTCPFlags )
static BaseType_t prvTCPSendSpecialPacketHelper( NetworkBufferDescriptor_t * const pxNetworkBuffer,
uint8_t ucTCPFlags )
{
BaseType_t xReturn = pdTRUE;
configASSERT( pxNetworkBuffer != NULL );
configASSERT( pxNetworkBuffer->pucEthernetBuffer != NULL );

#if ( ipconfigIGNORE_UNKNOWN_PACKETS == 1 )
#if ipconfigIS_ENABLED( ipconfigIGNORE_UNKNOWN_PACKETS )
/* Configured to ignore unknown packets just suppress a compiler warning. */
( void ) pxNetworkBuffer;
( void ) ucTCPFlags;
#else
{
switch( uxIPHeaderSizePacket( pxNetworkBuffer ) )
{
#if ( ipconfigUSE_IPv4 != 0 )
case ipSIZE_OF_IPv4_HEADER:
xReturn = prvTCPSendSpecialPktHelper_IPV4( pxNetworkBuffer, ucTCPFlags );
break;
#endif /* ( ipconfigUSE_IPv4 != 0 ) */
/* Map the ethernet buffer onto the TCPPacket_t struct for easy access to the fields. */

#if ( ipconfigUSE_IPv6 != 0 )
case ipSIZE_OF_IPv6_HEADER:
xReturn = prvTCPSendSpecialPktHelper_IPV6( pxNetworkBuffer, ucTCPFlags );
break;
#endif /* ( ipconfigUSE_IPv6 != 0 ) */
/* MISRA Ref 11.3.1 [Misaligned access] */
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA.md#rule-113 */
/* coverity[misra_c_2012_rule_11_3_violation] */
TCPHeader_t * const pxTCPHeader = ( ( TCPHeader_t * ) &( pxNetworkBuffer->pucEthernetBuffer[ ipSIZE_OF_ETH_HEADER + uxIPHeaderSizePacket( pxNetworkBuffer ) ] ) );
const uint32_t ulSendLength = uxIPHeaderSizePacket( pxNetworkBuffer ) + ipSIZE_OF_TCP_HEADER; /* Plus 0 options. */
const uint8_t ucFlagsReceived = pxTCPHeader->ucTCPFlags;

default:
xReturn = pdFAIL;
break;
configASSERT( pxNetworkBuffer->xDataLength >= ( ipSIZE_OF_ETH_HEADER + ulSendLength ) );

pxTCPHeader->ucTCPFlags = ucTCPFlags;
pxTCPHeader->ucTCPOffset = ( ipSIZE_OF_TCP_HEADER ) << 2;

if( ( ucFlagsReceived & tcpTCP_FLAG_SYN ) != 0U )
{
/* A synchronize packet is received. It counts as 1 pseudo byte of data,
* so increase the variable with 1. Before sending a reply, the values of
* 'ulSequenceNumber' and 'ulAckNr' will be swapped. */
uint32_t ulSequenceNumber = FreeRTOS_ntohl( pxTCPHeader->ulSequenceNumber );
ulSequenceNumber++;
pxTCPHeader->ulSequenceNumber = FreeRTOS_htonl( ulSequenceNumber );
}

prvTCPReturnPacket( NULL, pxNetworkBuffer, ulSendLength, pdFALSE );
}
#endif /* !ipconfigIGNORE_UNKNOWN_PACKETS */

Expand Down
51 changes: 0 additions & 51 deletions source/FreeRTOS_TCP_Transmission_IPv4.c
Original file line number Diff line number Diff line change
Expand Up @@ -463,57 +463,6 @@ BaseType_t prvTCPPrepareConnect_IPV4( FreeRTOS_Socket_t * pxSocket )
}
/*-----------------------------------------------------------*/


/**
* @brief Common code for sending a TCP protocol control packet (i.e. no options, no
* payload, just flags).
*
* @param[in] pxNetworkBuffer The network buffer received from the peer.
* @param[in] ucTCPFlags The flags to determine what kind of packet this is.
*
* @return pdFAIL always indicating that the packet was not consumed.
*/
BaseType_t prvTCPSendSpecialPktHelper_IPV4( NetworkBufferDescriptor_t * pxNetworkBuffer,
uint8_t ucTCPFlags )
{
#if ( ipconfigIGNORE_UNKNOWN_PACKETS == 1 )
/* Configured to ignore unknown packets just suppress a compiler warning. */
( void ) pxNetworkBuffer;
( void ) ucTCPFlags;
#else
{
/* Map the ethernet buffer onto the TCPPacket_t struct for easy access to the fields. */

/* MISRA Ref 11.3.1 [Misaligned access] */
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA.md#rule-113 */
/* coverity[misra_c_2012_rule_11_3_violation] */
TCPPacket_t * pxTCPPacket = ( ( TCPPacket_t * ) pxNetworkBuffer->pucEthernetBuffer );
const uint32_t ulSendLength =
ipSIZE_OF_IPv4_HEADER + ipSIZE_OF_TCP_HEADER; /* Plus 0 options. */

uint8_t ucFlagsReceived = pxTCPPacket->xTCPHeader.ucTCPFlags;
pxTCPPacket->xTCPHeader.ucTCPFlags = ucTCPFlags;
pxTCPPacket->xTCPHeader.ucTCPOffset = ( ipSIZE_OF_TCP_HEADER ) << 2;

if( ( ucFlagsReceived & tcpTCP_FLAG_SYN ) != 0U )
{
/* A synchronize packet is received. It counts as 1 pseudo byte of data,
* so increase the variable with 1. Before sending a reply, the values of
* 'ulSequenceNumber' and 'ulAckNr' will be swapped. */
uint32_t ulSequenceNumber = FreeRTOS_ntohl( pxTCPPacket->xTCPHeader.ulSequenceNumber );
ulSequenceNumber++;
pxTCPPacket->xTCPHeader.ulSequenceNumber = FreeRTOS_htonl( ulSequenceNumber );
}

prvTCPReturnPacket( NULL, pxNetworkBuffer, ulSendLength, pdFALSE );
}
#endif /* !ipconfigIGNORE_UNKNOWN_PACKETS */

/* The packet was not consumed. */
return pdFAIL;
}
/*-----------------------------------------------------------*/

/* *INDENT-OFF* */
#endif /* ( ipconfigUSE_IPv4 != 0 ) && ( ipconfigUSE_TCP == 1 ) */
/* *INDENT-ON* */
52 changes: 0 additions & 52 deletions source/FreeRTOS_TCP_Transmission_IPv6.c
Original file line number Diff line number Diff line change
Expand Up @@ -479,58 +479,6 @@ BaseType_t prvTCPPrepareConnect_IPV6( FreeRTOS_Socket_t * pxSocket )
}
/*-----------------------------------------------------------*/


/**
* @brief Common code for sending a TCP protocol control packet (i.e. no options, no
* payload, just flags).
*
* @param[in] pxNetworkBuffer The network buffer received from the peer.
* @param[in] ucTCPFlags The flags to determine what kind of packet this is.
*
* @return pdFAIL always indicating that the packet was not consumed.
*/
BaseType_t prvTCPSendSpecialPktHelper_IPV6( NetworkBufferDescriptor_t * pxNetworkBuffer,
uint8_t ucTCPFlags )
{
#if ( ipconfigIGNORE_UNKNOWN_PACKETS == 1 )
/* Configured to ignore unknown packets just suppress a compiler warning. */
( void ) pxNetworkBuffer;
( void ) ucTCPFlags;
#else
{
/* Map the ethernet buffer onto the TCPPacket_t struct for easy access to the fields. */

/* MISRA Ref 11.3.1 [Misaligned access] */
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA.md#rule-113 */
/* coverity[misra_c_2012_rule_11_3_violation] */
TCPPacket_IPv6_t * pxTCPPacket = ( ( TCPPacket_IPv6_t * ) pxNetworkBuffer->pucEthernetBuffer );
const uint32_t ulSendLength =
ipSIZE_OF_IPv6_HEADER + ipSIZE_OF_TCP_HEADER; /* Plus 0 options. */

uint8_t ucFlagsReceived = pxTCPPacket->xTCPHeader.ucTCPFlags;
pxTCPPacket->xTCPHeader.ucTCPFlags = ucTCPFlags;
pxTCPPacket->xTCPHeader.ucTCPOffset = ( ipSIZE_OF_TCP_HEADER ) << 2;

if( ( ucFlagsReceived & tcpTCP_FLAG_SYN ) != 0U )
{
/* A synchronize packet is received. It counts as 1 pseudo byte of data,
* so increase the variable with 1. Before sending a reply, the values of
* 'ulSequenceNumber' and 'ulAckNr' will be swapped. */
uint32_t ulSequenceNumber = FreeRTOS_ntohl( pxTCPPacket->xTCPHeader.ulSequenceNumber );
ulSequenceNumber++;
pxTCPPacket->xTCPHeader.ulSequenceNumber = FreeRTOS_htonl( ulSequenceNumber );
}

prvTCPReturnPacket( NULL, pxNetworkBuffer, ulSendLength, pdFALSE );
}
#endif /* !ipconfigIGNORE_UNKNOWN_PACKETS */

/* The packet was not consumed. */
return pdFAIL;
}
/*-----------------------------------------------------------*/


/* *INDENT-OFF* */
#endif /* ( ipconfigUSE_IPv6 != 0 ) && ( ipconfigUSE_TCP == 1 ) */
/* *INDENT-ON* */
14 changes: 9 additions & 5 deletions source/include/FreeRTOS_IP_Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,13 @@ typedef struct xARP_PACKET ARPPacket_t;
*/
typedef union XPROT_PACKET
{
ARPPacket_t xARPPacket; /**< Union member: ARP packet struct */
TCPPacket_t xTCPPacket; /**< Union member: TCP packet struct */
UDPPacket_t xUDPPacket; /**< Union member: UDP packet struct */
ICMPPacket_t xICMPPacket; /**< Union member: ICMP packet struct */
ARPPacket_t xARPPacket; /**< Union member: ARP packet struct */
ICMPPacket_t xICMPPacket; /**< Union member: ICMP packet struct */
TCPPacket_t xTCPPacket; /**< Union member: TCP packet struct */
UDPPacket_t xUDPPacket; /**< Union member: UDP packet struct */
ICMPPacket_IPv6_t xICMPPacketIPv6; /**< Union member: ICMPv6 packet struct */
TCPPacket_IPv6_t xTCPPacketIPv6; /**< Union member: TCPv6 packet struct */
UDPPacket_IPv6_t xUDPPacketIPv6; /**< Union member: UDPv6 packet struct */
} ProtocolPacket_t;

/**
Expand All @@ -221,9 +224,10 @@ typedef union XPROT_PACKET
*/
typedef union xPROT_HEADERS
{
ARPHeader_t xARPHeader; /**< Union member: ARP header */
ICMPHeader_t xICMPHeader; /**< Union member: ICMP header */
UDPHeader_t xUDPHeader; /**< Union member: UDP header */
TCPHeader_t xTCPHeader; /**< Union member: TCP header */
UDPHeader_t xUDPHeader; /**< Union member: UDP header */
ICMPHeader_IPv6_t xICMPHeaderIPv6; /**< Union member: ICMPv6 header */
} ProtocolHeaders_t;

Expand Down
21 changes: 0 additions & 21 deletions source/include/FreeRTOS_TCP_State_Handling.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,27 +79,6 @@ FreeRTOS_Socket_t * prvHandleListen_IPV4( FreeRTOS_Socket_t * pxSocket,
FreeRTOS_Socket_t * prvHandleListen_IPV6( FreeRTOS_Socket_t * pxSocket,
NetworkBufferDescriptor_t * pxNetworkBuffer );

/*
* Common code for sending a TCP protocol control packet (i.e. no options, no
* payload, just flags).
*/
BaseType_t prvTCPSendSpecialPacketHelper( NetworkBufferDescriptor_t * pxNetworkBuffer,
uint8_t ucTCPFlags );

/*
* Common code for sending a TCP protocol control packet (i.e. no options, no
* payload, just flags).
*/
BaseType_t prvTCPSendSpecialPktHelper_IPV4( NetworkBufferDescriptor_t * pxNetworkBuffer,
uint8_t ucTCPFlags );

/*
* Common code for sending a TCP protocol control packet (i.e. no options, no
* payload, just flags).
*/
BaseType_t prvTCPSendSpecialPktHelper_IPV6( NetworkBufferDescriptor_t * pxNetworkBuffer,
uint8_t ucTCPFlags );

/*
* After a listening socket receives a new connection, it may duplicate itself.
* The copying takes place in prvTCPSocketCopy.
Expand Down
1 change: 1 addition & 0 deletions source/include/FreeRTOS_TCP_Transmission.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ NetworkBufferDescriptor_t * prvTCPBufferResize( const FreeRTOS_Socket_t * pxSock
NetworkBufferDescriptor_t * pxNetworkBuffer,
int32_t lDataLen,
UBaseType_t uxOptionsLength );

/* *INDENT-OFF* */
#ifdef __cplusplus
} /* extern "C" */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,3 @@ BaseType_t prvTCPPrepareConnect_IPV6( FreeRTOS_Socket_t * pxSocket )
{
return pdTRUE;
}

/*
* Common code for sending a TCP protocol control packet (i.e. no options, no
* payload, just flags).
*/
BaseType_t prvTCPSendSpecialPktHelper_IPV6( NetworkBufferDescriptor_t * pxNetworkBuffer,
uint8_t ucTCPFlags )
{
return pdTRUE;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2448,17 +2448,15 @@ void test_prvSendData_AckMsg_Null_Syn_State_Data_To_Send_Rcv_Zero( void )
/* test prvTCPSendSpecialPacketHelper function with incorrect header size */
void test_prvTCPSendSpecialPacketHelper_Incorrect_HeaderSize( void )
{
BaseType_t Return = pdTRUE;

pxSocket = &xSocket;
pxNetworkBuffer = &xNetworkBuffer;
pxNetworkBuffer->pucEthernetBuffer = ucEthernetBuffer;
pxNetworkBuffer->xDataLength = 0;

uxIPHeaderSizePacket_ExpectAnyArgsAndReturn( 0 );

Return = prvTCPSendSpecialPacketHelper( pxNetworkBuffer, tcpTCP_FLAG_ACK );
uxIPHeaderSizePacket_IgnoreAndReturn( 0 );

TEST_ASSERT_EQUAL( pdFALSE, Return );
catch_assert( prvTCPSendSpecialPacketHelper( pxNetworkBuffer, tcpTCP_FLAG_ACK ) );
pxNetworkBuffer->xDataLength = ipconfigNETWORK_MTU;
}

/* test prvTCPSendSpecialPacketHelper function with incorrect header size */
Expand All @@ -2470,7 +2468,7 @@ void test_prvTCPSendSpecialPacketHelper_IPv6_HeaderSize( void )
pxNetworkBuffer = &xNetworkBuffer;
pxNetworkBuffer->pucEthernetBuffer = ucEthernetBuffer;

uxIPHeaderSizePacket_ExpectAnyArgsAndReturn( ipSIZE_OF_IPv6_HEADER );
uxIPHeaderSizePacket_IgnoreAndReturn( ipSIZE_OF_IPv6_HEADER );

Return = prvTCPSendSpecialPacketHelper( pxNetworkBuffer, tcpTCP_FLAG_ACK );

Expand Down Expand Up @@ -2506,7 +2504,7 @@ void test_prvTCPSendSpecialPacketHelper( void )
eARPGetCacheEntry_ReturnThruPtr_ppxEndPoint( &pxEndPoint );

Return = prvTCPSendSpecialPacketHelper( pxNetworkBuffer, tcpTCP_FLAG_ACK );
TEST_ASSERT_EQUAL( pdFALSE, Return );
TEST_ASSERT_EQUAL( pdTRUE, Return );
TEST_ASSERT_EQUAL( 1, NetworkInterfaceOutputFunction_Stub_Called );
TEST_ASSERT_EQUAL( tcpTCP_FLAG_ACK, pxTCPPacket->xTCPHeader.ucTCPFlags );
TEST_ASSERT_EQUAL( 0x50, pxTCPPacket->xTCPHeader.ucTCPOffset );
Expand Down Expand Up @@ -2543,7 +2541,7 @@ void test_prvTCPSendSpecialPacketHelper_flagSYN( void )
eARPGetCacheEntry_ReturnThruPtr_ppxEndPoint( &pxEndPoint );

Return = prvTCPSendSpecialPacketHelper( pxNetworkBuffer, tcpTCP_FLAG_ACK );
TEST_ASSERT_EQUAL( pdFALSE, Return );
TEST_ASSERT_EQUAL( pdTRUE, Return );
TEST_ASSERT_EQUAL( 1, NetworkInterfaceOutputFunction_Stub_Called );
TEST_ASSERT_EQUAL( tcpTCP_FLAG_ACK, pxTCPPacket->xTCPHeader.ucTCPFlags );
TEST_ASSERT_EQUAL( 0x50, pxTCPPacket->xTCPHeader.ucTCPOffset );
Expand Down Expand Up @@ -2579,7 +2577,7 @@ void test_prvTCPSendChallengeAck( void )
eARPGetCacheEntry_ReturnThruPtr_ppxEndPoint( &pxEndPoint );

Return = prvTCPSendChallengeAck( pxNetworkBuffer );
TEST_ASSERT_EQUAL( pdFALSE, Return );
TEST_ASSERT_EQUAL( pdTRUE, Return );
TEST_ASSERT_EQUAL( 1, NetworkInterfaceOutputFunction_Stub_Called );
TEST_ASSERT_EQUAL( tcpTCP_FLAG_ACK, pxTCPPacket->xTCPHeader.ucTCPFlags );
TEST_ASSERT_EQUAL( 0x50, pxTCPPacket->xTCPHeader.ucTCPOffset );
Expand Down Expand Up @@ -2615,7 +2613,7 @@ void test_prvTCPSendReset( void )

Return = prvTCPSendReset( pxNetworkBuffer );
TEST_ASSERT_EQUAL( 1, NetworkInterfaceOutputFunction_Stub_Called );
TEST_ASSERT_EQUAL( pdFALSE, Return );
TEST_ASSERT_EQUAL( pdTRUE, Return );
TEST_ASSERT_EQUAL( tcpTCP_FLAG_ACK | tcpTCP_FLAG_RST, pxTCPPacket->xTCPHeader.ucTCPFlags );
TEST_ASSERT_EQUAL( 0x50, pxTCPPacket->xTCPHeader.ucTCPOffset );
}
Original file line number Diff line number Diff line change
Expand Up @@ -552,47 +552,3 @@ void test_prvTCPPrepareConnect_IPV6_HappyPath_IPv6( void )
TEST_ASSERT_EQUAL( pdPASS, xReturn );
TEST_ASSERT_EQUAL_MEMORY( pxSocket->pxEndPoint, pxEndPoint, sizeof( NetworkEndPoint_t ) );
}

/**
* @brief This function validates sending a TCP protocol control packet,
* when an un synchronised packet is received.
*/
void test_prvTCPSendSpecialPktHelper_IPV6( void )
{
NetworkBufferDescriptor_t xNetworkBuffer, * pxNetworkBuffer = &xNetworkBuffer;
TCPPacket_IPv6_t * pxTCPPacket;
uint8_t ucTCPFlags = tcpTCP_FLAG_RST;
BaseType_t xReturn;

pxNetworkBuffer->pucEthernetBuffer = ucEthernetBuffer;
pxTCPPacket = ( ( TCPPacket_IPv6_t * ) pxNetworkBuffer->pucEthernetBuffer );
pxTCPPacket->xTCPHeader.ucTCPFlags = 0;

xReturn = prvTCPSendSpecialPktHelper_IPV6( pxNetworkBuffer, ucTCPFlags );

TEST_ASSERT_EQUAL( pdFAIL, xReturn );
TEST_ASSERT_EQUAL( ucTCPFlags, pxTCPPacket->xTCPHeader.ucTCPFlags );
TEST_ASSERT_EQUAL( ( ipSIZE_OF_TCP_HEADER ) << 2, pxTCPPacket->xTCPHeader.ucTCPOffset );
}

/**
* @brief This function validates sending a TCP protocol control packet,
* when an synchronize packet is received.
*/
void test_prvTCPSendSpecialPktHelper_IPV6_Syn( void )
{
NetworkBufferDescriptor_t xNetworkBuffer, * pxNetworkBuffer = &xNetworkBuffer;
TCPPacket_IPv6_t * pxTCPPacket;
uint8_t ucTCPFlags = tcpTCP_FLAG_RST;
BaseType_t xReturn;

xNetworkBuffer.pucEthernetBuffer = ucEthernetBuffer;
pxTCPPacket = ( ( TCPPacket_IPv6_t * ) xNetworkBuffer.pucEthernetBuffer );
pxTCPPacket->xTCPHeader.ucTCPFlags = tcpTCP_FLAG_SYN;

xReturn = prvTCPSendSpecialPktHelper_IPV6( pxNetworkBuffer, ucTCPFlags );

TEST_ASSERT_EQUAL( pdFAIL, xReturn );
TEST_ASSERT_EQUAL( ucTCPFlags, pxTCPPacket->xTCPHeader.ucTCPFlags );
TEST_ASSERT_EQUAL( ( ipSIZE_OF_TCP_HEADER ) << 2, pxTCPPacket->xTCPHeader.ucTCPOffset );
}

0 comments on commit cc54eae

Please sign in to comment.