Merge branch 'bugfix/fix_mbedtls_net_sockets_error_handling_v4.0' into 'release/v4.0'

mbedtls: use `errno` instead of `SO_ERROR` for getting socket errors (v4.0)

See merge request espressif/esp-idf!5768
This commit is contained in:
Angus Gratton 2019-08-20 12:28:56 +08:00
commit 43efee5232

View file

@ -59,16 +59,6 @@ static int net_prepare( void )
return ( 0 ); return ( 0 );
} }
static int mbedtls_net_errno(int fd)
{
int sock_errno = 0;
u32_t optlen = sizeof(sock_errno);
getsockopt(fd, SOL_SOCKET, SO_ERROR, &sock_errno, &optlen);
return sock_errno;
}
/* /*
* Initialize a context * Initialize a context
*/ */
@ -204,22 +194,19 @@ int mbedtls_net_bind( mbedtls_net_context *ctx, const char *bind_ip, const char
* *
* Note: on a blocking socket this function always returns 0! * Note: on a blocking socket this function always returns 0!
*/ */
static int net_would_block( const mbedtls_net_context *ctx, int *errout ) static int net_would_block( const mbedtls_net_context *ctx )
{ {
int error = mbedtls_net_errno(ctx->fd); int error = errno;
if ( errout ) {
*errout = error;
}
/* /*
* Never return 'WOULD BLOCK' on a non-blocking socket * Never return 'WOULD BLOCK' on a non-blocking socket
*/ */
if ( ( fcntl( ctx->fd, F_GETFL, 0) & O_NONBLOCK ) != O_NONBLOCK ) { if ( ( fcntl( ctx->fd, F_GETFL, 0) & O_NONBLOCK ) != O_NONBLOCK ) {
errno = error;
return ( 0 ); return ( 0 );
} }
switch ( error ) { switch ( errno = error ) {
#if defined EAGAIN #if defined EAGAIN
case EAGAIN: case EAGAIN:
#endif #endif
@ -267,7 +254,7 @@ int mbedtls_net_accept( mbedtls_net_context *bind_ctx,
} }
if ( ret < 0 ) { if ( ret < 0 ) {
if ( net_would_block( bind_ctx, NULL ) != 0 ) { if ( net_would_block( bind_ctx ) != 0 ) {
return ( MBEDTLS_ERR_SSL_WANT_READ ); return ( MBEDTLS_ERR_SSL_WANT_READ );
} }
@ -347,7 +334,6 @@ int mbedtls_net_recv( void *ctx, unsigned char *buf, size_t len )
{ {
int ret; int ret;
int fd = ((mbedtls_net_context *) ctx)->fd; int fd = ((mbedtls_net_context *) ctx)->fd;
int error = 0;
if ( fd < 0 ) { if ( fd < 0 ) {
return ( MBEDTLS_ERR_NET_INVALID_CONTEXT ); return ( MBEDTLS_ERR_NET_INVALID_CONTEXT );
@ -356,15 +342,15 @@ int mbedtls_net_recv( void *ctx, unsigned char *buf, size_t len )
ret = (int) read( fd, buf, len ); ret = (int) read( fd, buf, len );
if ( ret < 0 ) { if ( ret < 0 ) {
if ( net_would_block( ctx, &error ) != 0 ) { if ( net_would_block( ctx ) != 0 ) {
return ( MBEDTLS_ERR_SSL_WANT_READ ); return ( MBEDTLS_ERR_SSL_WANT_READ );
} }
if ( error == EPIPE || error == ECONNRESET ) { if ( errno == EPIPE || errno == ECONNRESET ) {
return ( MBEDTLS_ERR_NET_CONN_RESET ); return ( MBEDTLS_ERR_NET_CONN_RESET );
} }
if ( error == EINTR ) { if ( errno == EINTR ) {
return ( MBEDTLS_ERR_SSL_WANT_READ ); return ( MBEDTLS_ERR_SSL_WANT_READ );
} }
@ -422,8 +408,6 @@ int mbedtls_net_send( void *ctx, const unsigned char *buf, size_t len )
int ret; int ret;
int fd = ((mbedtls_net_context *) ctx)->fd; int fd = ((mbedtls_net_context *) ctx)->fd;
int error = 0;
if ( fd < 0 ) { if ( fd < 0 ) {
return ( MBEDTLS_ERR_NET_INVALID_CONTEXT ); return ( MBEDTLS_ERR_NET_INVALID_CONTEXT );
} }
@ -431,15 +415,15 @@ int mbedtls_net_send( void *ctx, const unsigned char *buf, size_t len )
ret = (int) write( fd, buf, len ); ret = (int) write( fd, buf, len );
if ( ret < 0 ) { if ( ret < 0 ) {
if ( net_would_block( ctx, &error ) != 0 ) { if ( net_would_block( ctx ) != 0 ) {
return ( MBEDTLS_ERR_SSL_WANT_WRITE ); return ( MBEDTLS_ERR_SSL_WANT_WRITE );
} }
if ( error == EPIPE || error == ECONNRESET ) { if ( errno == EPIPE || errno == ECONNRESET ) {
return ( MBEDTLS_ERR_NET_CONN_RESET ); return ( MBEDTLS_ERR_NET_CONN_RESET );
} }
if ( error == EINTR ) { if ( errno == EINTR ) {
return ( MBEDTLS_ERR_SSL_WANT_WRITE ); return ( MBEDTLS_ERR_SSL_WANT_WRITE );
} }