mbedtls: Remove WIN32 #ifdef sections, use socket's SO_ERROR flag over errno when possible

This commit is contained in:
Angus Gratton 2016-05-27 11:50:06 +10:00
parent 367c17d1cf
commit d5221e7efa

View file

@ -217,18 +217,14 @@ int mbedtls_net_bind( mbedtls_net_context *ctx, const char *bind_ip, const char
} }
#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \ static int socket_errno( const mbedtls_net_context *ctx )
!defined(EFI32)
/*
* Check if the requested operation would be blocking on a non-blocking socket
* and thus 'failed' with a negative return value.
*/
static int net_would_block( const mbedtls_net_context *ctx )
{ {
((void) ctx); int sock_errno = 0;
return( WSAGetLastError() == WSAEWOULDBLOCK ); u32_t optlen = sizeof(sock_errno);
lwip_getsockopt(ctx->fd, SOL_SOCKET, SO_ERROR, &sock_errno, &optlen);
return sock_errno;
} }
#else
/* /*
* Check if the requested operation would be blocking on a non-blocking socket * Check if the requested operation would be blocking on a non-blocking socket
* and thus 'failed' with a negative return value. * and thus 'failed' with a negative return value.
@ -243,7 +239,8 @@ static int net_would_block( const mbedtls_net_context *ctx )
if( ( fcntl( ctx->fd, F_GETFL, 0) & O_NONBLOCK ) != O_NONBLOCK ) if( ( fcntl( ctx->fd, F_GETFL, 0) & O_NONBLOCK ) != O_NONBLOCK )
return( 0 ); return( 0 );
switch( errno )
switch( socket_errno(ctx) )
{ {
#if defined EAGAIN #if defined EAGAIN
case EAGAIN: case EAGAIN:
@ -255,7 +252,6 @@ static int net_would_block( const mbedtls_net_context *ctx )
} }
return( 0 ); return( 0 );
} }
#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
/* /*
* Accept a connection from a remote client * Accept a connection from a remote client
@ -401,9 +397,7 @@ void mbedtls_net_usleep( unsigned long usec )
#endif #endif
} }
/* /* Read at most 'len' characters */
* Read at most 'len' characters
*/
int mbedtls_net_recv( void *ctx, unsigned char *buf, size_t len ) int mbedtls_net_recv( void *ctx, unsigned char *buf, size_t len )
{ {
int ret; int ret;
@ -419,17 +413,13 @@ int mbedtls_net_recv( void *ctx, unsigned char *buf, size_t len )
if( net_would_block( ctx ) != 0 ) if( net_would_block( ctx ) != 0 )
return( MBEDTLS_ERR_SSL_WANT_READ ); return( MBEDTLS_ERR_SSL_WANT_READ );
#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \ int sock_errno = socket_errno(ctx);
!defined(EFI32)
if( WSAGetLastError() == WSAECONNRESET ) if( sock_errno == EPIPE || sock_errno == ECONNRESET )
return( MBEDTLS_ERR_NET_CONN_RESET );
#else
if( errno == EPIPE || errno == ECONNRESET )
return( MBEDTLS_ERR_NET_CONN_RESET ); return( MBEDTLS_ERR_NET_CONN_RESET );
if( errno == EINTR ) if( sock_errno == EINTR )
return( MBEDTLS_ERR_SSL_WANT_READ ); return( MBEDTLS_ERR_SSL_WANT_READ );
#endif
return( MBEDTLS_ERR_NET_RECV_FAILED ); return( MBEDTLS_ERR_NET_RECV_FAILED );
} }
@ -465,14 +455,8 @@ int mbedtls_net_recv_timeout( void *ctx, unsigned char *buf, size_t len,
if( ret < 0 ) if( ret < 0 )
{ {
#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
!defined(EFI32)
if( WSAGetLastError() == WSAEINTR )
return( MBEDTLS_ERR_SSL_WANT_READ );
#else
if( errno == EINTR ) if( errno == EINTR )
return( MBEDTLS_ERR_SSL_WANT_READ ); return( MBEDTLS_ERR_SSL_WANT_READ );
#endif
return( MBEDTLS_ERR_NET_RECV_FAILED ); return( MBEDTLS_ERR_NET_RECV_FAILED );
} }
@ -499,17 +483,13 @@ int mbedtls_net_send( void *ctx, const unsigned char *buf, size_t len )
if( net_would_block( ctx ) != 0 ) if( net_would_block( ctx ) != 0 )
return( MBEDTLS_ERR_SSL_WANT_WRITE ); return( MBEDTLS_ERR_SSL_WANT_WRITE );
#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \ int sock_errno = socket_errno(ctx);
!defined(EFI32)
if( WSAGetLastError() == WSAECONNRESET ) if( sock_errno == EPIPE || sock_errno == ECONNRESET )
return( MBEDTLS_ERR_NET_CONN_RESET );
#else
if( errno == EPIPE || errno == ECONNRESET )
return( MBEDTLS_ERR_NET_CONN_RESET ); return( MBEDTLS_ERR_NET_CONN_RESET );
if( errno == EINTR ) if( sock_errno == EINTR )
return( MBEDTLS_ERR_SSL_WANT_WRITE ); return( MBEDTLS_ERR_SSL_WANT_WRITE );
#endif
return( MBEDTLS_ERR_NET_SEND_FAILED ); return( MBEDTLS_ERR_NET_SEND_FAILED );
} }