From 622d6d550494f8c4a7a5e1a742d60c2a3c5bb4e4 Mon Sep 17 00:00:00 2001 From: Mahavir Jain Date: Thu, 8 Aug 2019 13:42:47 +0530 Subject: [PATCH] mbedtls: use `errno` instead of `SO_ERROR` for getting socket errors As per upgrade notes of lwIP v2.1.0: socket API: according to the standard, SO_ERROR now only returns asynchronous errors. All other/normal/synchronous errors are (and always were) available via 'errno'. LWIP_SOCKET_SET_ERRNO has been removed - 'errno' is always set - and required! Refer: https://www.nongnu.org/lwip/2_1_x/upgrading.html Fixes https://github.com/espressif/esp-azure/issues/51 --- components/mbedtls/port/net_sockets.c | 38 ++++++++------------------- 1 file changed, 11 insertions(+), 27 deletions(-) diff --git a/components/mbedtls/port/net_sockets.c b/components/mbedtls/port/net_sockets.c index 6d8a1cc55..e64510071 100644 --- a/components/mbedtls/port/net_sockets.c +++ b/components/mbedtls/port/net_sockets.c @@ -59,16 +59,6 @@ static int net_prepare( void ) 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 */ @@ -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! */ -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); - - if ( errout ) { - *errout = error; - } + int error = errno; /* * Never return 'WOULD BLOCK' on a non-blocking socket */ if ( ( fcntl( ctx->fd, F_GETFL, 0) & O_NONBLOCK ) != O_NONBLOCK ) { + errno = error; return ( 0 ); } - switch ( error ) { + switch ( errno = error ) { #if defined EAGAIN case EAGAIN: #endif @@ -267,7 +254,7 @@ int mbedtls_net_accept( mbedtls_net_context *bind_ctx, } if ( ret < 0 ) { - if ( net_would_block( bind_ctx, NULL ) != 0 ) { + if ( net_would_block( bind_ctx ) != 0 ) { 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 fd = ((mbedtls_net_context *) ctx)->fd; - int error = 0; if ( fd < 0 ) { 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 ); if ( ret < 0 ) { - if ( net_would_block( ctx, &error ) != 0 ) { + if ( net_would_block( ctx ) != 0 ) { return ( MBEDTLS_ERR_SSL_WANT_READ ); } - if ( error == EPIPE || error == ECONNRESET ) { + if ( errno == EPIPE || errno == ECONNRESET ) { return ( MBEDTLS_ERR_NET_CONN_RESET ); } - if ( error == EINTR ) { + if ( errno == EINTR ) { 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 fd = ((mbedtls_net_context *) ctx)->fd; - int error = 0; - if ( fd < 0 ) { 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 ); if ( ret < 0 ) { - if ( net_would_block( ctx, &error ) != 0 ) { + if ( net_would_block( ctx ) != 0 ) { return ( MBEDTLS_ERR_SSL_WANT_WRITE ); } - if ( error == EPIPE || error == ECONNRESET ) { + if ( errno == EPIPE || errno == ECONNRESET ) { return ( MBEDTLS_ERR_NET_CONN_RESET ); } - if ( error == EINTR ) { + if ( errno == EINTR ) { return ( MBEDTLS_ERR_SSL_WANT_WRITE ); }