Skip to content

Commit

Permalink
Add error handling on Socket_t in transport layer. (FreeRTOS#887)
Browse files Browse the repository at this point in the history
* Set tcpSocket to SOCKETS_INVALID_SOCKET when any error occurred in TLS_FreeRTOS_Connect.

* Initialize Socket_t to NULL.
  • Loading branch information
ActoryOu authored Dec 5, 2022
1 parent e9bfcb8 commit e85b49a
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
*/

/**
* @file sockets_wrapper.h
* @file tcp_sockets_wrapper.h
* @brief TCP transport functions wrapper.
*/

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ BaseType_t TCP_Sockets_Connect( Socket_t * pTcpSocket,
if( tcpSocket != FREERTOS_INVALID_SOCKET )
{
( void ) FreeRTOS_closesocket( tcpSocket );
tcpSocket = FREERTOS_INVALID_SOCKET;
}
}
else
Expand All @@ -192,7 +193,7 @@ void TCP_Sockets_Disconnect( Socket_t tcpSocket )
BaseType_t waitForShutdownLoopCount = 0;
uint8_t pDummyBuffer[ 2 ];

if( tcpSocket != FREERTOS_INVALID_SOCKET )
if( ( tcpSocket != NULL ) && ( tcpSocket != FREERTOS_INVALID_SOCKET ) )
{
/* Initiate graceful shutdown. */
( void ) FreeRTOS_shutdown( tcpSocket, FREERTOS_SHUT_RDWR );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,7 @@ TlsTransportStatus_t TLS_FreeRTOS_Connect( NetworkContext_t * pNetworkContext,
TlsTransportParams_t * pTlsTransportParams = NULL;
TlsTransportStatus_t returnStatus = TLS_TRANSPORT_SUCCESS;
BaseType_t socketStatus = 0;
BaseType_t isSocketConnected = pdFALSE, isTlsSetup = pdFALSE;

if( ( pNetworkContext == NULL ) ||
( pNetworkContext->pParams == NULL ) ||
Expand Down Expand Up @@ -660,6 +661,10 @@ TlsTransportStatus_t TLS_FreeRTOS_Connect( NetworkContext_t * pNetworkContext,
if( returnStatus == TLS_TRANSPORT_SUCCESS )
{
pTlsTransportParams = pNetworkContext->pParams;

/* Initialize tcpSocket. */
pTlsTransportParams->tcpSocket = NULL;

socketStatus = TCP_Sockets_Connect( &( pTlsTransportParams->tcpSocket ),
pHostName,
port,
Expand All @@ -678,6 +683,8 @@ TlsTransportStatus_t TLS_FreeRTOS_Connect( NetworkContext_t * pNetworkContext,
/* Initialize mbedtls. */
if( returnStatus == TLS_TRANSPORT_SUCCESS )
{
isSocketConnected = pdTRUE;

returnStatus = initMbedtls( &( pTlsTransportParams->sslContext.entropyContext ),
&( pTlsTransportParams->sslContext.ctrDrgbContext ) );
}
Expand All @@ -691,17 +698,25 @@ TlsTransportStatus_t TLS_FreeRTOS_Connect( NetworkContext_t * pNetworkContext,
/* Perform TLS handshake. */
if( returnStatus == TLS_TRANSPORT_SUCCESS )
{
isTlsSetup = pdTRUE;

returnStatus = tlsHandshake( pNetworkContext, pNetworkCredentials );
}

/* Clean up on failure. */
if( returnStatus != TLS_TRANSPORT_SUCCESS )
{
if( ( pNetworkContext != NULL ) && ( pNetworkContext->pParams != NULL ) )
/* Free SSL context if it's setup. */
if( isTlsSetup == pdTRUE )
{
sslContextFree( &( pTlsTransportParams->sslContext ) );
}

TCP_Sockets_Disconnect(pTlsTransportParams->tcpSocket);
/* Call Sockets_Disconnect if socket was connected. */
if( isSocketConnected == pdTRUE )
{
TCP_Sockets_Disconnect( pTlsTransportParams->tcpSocket );
pTlsTransportParams->tcpSocket = NULL;
}
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,7 @@ TlsTransportStatus_t TLS_FreeRTOS_Connect( NetworkContext_t * pNetworkContext,
TlsTransportParams_t * pTlsTransportParams = NULL;
TlsTransportStatus_t returnStatus = TLS_TRANSPORT_SUCCESS;
BaseType_t socketStatus = 0;
BaseType_t isSocketConnected = pdFALSE;

if( ( pNetworkContext == NULL ) ||
( pNetworkContext->pParams == NULL ) ||
Expand Down Expand Up @@ -696,6 +697,10 @@ TlsTransportStatus_t TLS_FreeRTOS_Connect( NetworkContext_t * pNetworkContext,
if( returnStatus == TLS_TRANSPORT_SUCCESS )
{
pTlsTransportParams = pNetworkContext->pParams;

/* Initialize tcpSocket. */
pTlsTransportParams->tcpSocket = NULL;

socketStatus = TCP_Sockets_Connect( &( pTlsTransportParams->tcpSocket ),
pHostName,
port,
Expand All @@ -714,13 +719,19 @@ TlsTransportStatus_t TLS_FreeRTOS_Connect( NetworkContext_t * pNetworkContext,
/* Perform TLS handshake. */
if( returnStatus == TLS_TRANSPORT_SUCCESS )
{
isSocketConnected = pdTRUE;

returnStatus = tlsSetup( pNetworkContext, pHostName, pNetworkCredentials );
}

/* Clean up on failure. */
if( returnStatus != TLS_TRANSPORT_SUCCESS )
{
TCP_Sockets_Disconnect( pTlsTransportParams->tcpSocket );
if( isSocketConnected == pdTRUE )
{
TCP_Sockets_Disconnect( pTlsTransportParams->tcpSocket );
pTlsTransportParams->tcpSocket = NULL;
}
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ PlaintextTransportStatus_t Plaintext_FreeRTOS_Connect( NetworkContext_t * pNetwo
else
{
pPlaintextTransportParams = pNetworkContext->pParams;

/* Initialize tcpSocket. */
pPlaintextTransportParams->tcpSocket = NULL;

/* Establish a TCP connection with the server. */
socketStatus = TCP_Sockets_Connect( &( pPlaintextTransportParams->tcpSocket ),
pHostName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ static int wolfSSL_IORecvGlue( WOLFSSL * ssl,
read = TCP_Sockets_Recv( xSocket, ( void * ) buf, ( size_t ) sz );

if( ( read == 0 ) ||
( read == -TCP_SOCKETS_ERRNO_EWOULDBLOCK) )
( read == -TCP_SOCKETS_ERRNO_EWOULDBLOCK ) )
{
read = WOLFSSL_CBIO_ERR_WANT_READ;
}
Expand All @@ -169,7 +169,7 @@ static int wolfSSL_IOSendGlue( WOLFSSL * ssl,
Socket_t xSocket = ( Socket_t ) context;
BaseType_t sent = TCP_Sockets_Send( xSocket, ( void * ) buf, ( size_t ) sz );

if( sent == -TCP_SOCKETS_ERRNO_EWOULDBLOCK)
if( sent == -TCP_SOCKETS_ERRNO_EWOULDBLOCK )
{
sent = WOLFSSL_CBIO_ERR_WANT_WRITE;
}
Expand Down Expand Up @@ -380,7 +380,7 @@ TlsTransportStatus_t TLS_FreeRTOS_Connect( NetworkContext_t * pNetworkContext,
{
TlsTransportStatus_t returnStatus = TLS_TRANSPORT_SUCCESS;
BaseType_t socketStatus = 0;

BaseType_t isSocketConnected = pdFALSE;

if( ( pNetworkContext == NULL ) ||
( pHostName == NULL ) ||
Expand All @@ -402,11 +402,13 @@ TlsTransportStatus_t TLS_FreeRTOS_Connect( NetworkContext_t * pNetworkContext,
/* Establish a TCP connection with the server. */
if( returnStatus == TLS_TRANSPORT_SUCCESS )
{
pNetworkContext->tcpSocket = NULL;

socketStatus = TCP_Sockets_Connect( &( pNetworkContext->tcpSocket ),
pHostName,
port,
receiveTimeoutMs,
sendTimeoutMs );
pHostName,
port,
receiveTimeoutMs,
sendTimeoutMs );

if( socketStatus != 0 )
{
Expand All @@ -420,6 +422,8 @@ TlsTransportStatus_t TLS_FreeRTOS_Connect( NetworkContext_t * pNetworkContext,
/* Initialize tls. */
if( returnStatus == TLS_TRANSPORT_SUCCESS )
{
isSocketConnected = pdTRUE;

returnStatus = initTLS();
}

Expand All @@ -432,7 +436,11 @@ TlsTransportStatus_t TLS_FreeRTOS_Connect( NetworkContext_t * pNetworkContext,
/* Clean up on failure. */
if( returnStatus != TLS_TRANSPORT_SUCCESS )
{
TCP_Sockets_Disconnect( pNetworkContext->tcpSocket );
if( isSocketConnected == pdTRUE )
{
TCP_Sockets_Disconnect( pNetworkContext->tcpSocket );
pNetworkContext->tcpSocket = NULL;
}
}
else
{
Expand Down

0 comments on commit e85b49a

Please sign in to comment.