]> granicus.if.org Git - esp-idf/commitdiff
mbedtls: use `errno` instead of `SO_ERROR` for getting socket errors
authorMahavir Jain <mahavir@espressif.com>
Thu, 8 Aug 2019 08:12:47 +0000 (13:42 +0530)
committerbot <bot@espressif.com>
Thu, 8 Aug 2019 12:17:32 +0000 (12:17 +0000)
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

index 6d8a1cc550063917cbc7919a58c8904cbd54b451..e64510071e5a3ac8572a6607490cf4935ccd05ed 100644 (file)
@@ -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 );
         }