- Changed behaviour of net_recv(), ssl_fetch_input() and ssl_read(). net_recv() now returns 0 on EOF instead of POLARSSL_ERR_NET_CONN_RESET. ssl_fetch_input() returns POLARSSL_ERR_SSL_CONN_EOF on an EOF from its f_recv() function. ssl_read() returns 0 if a POLARSSL_ERR_SSL_CONN_EOF is received after the handshake.

- Network functions now return POLARSSL_ERR_NET_WANT_READ or POLARSSL_ERR_NET_WANT_WRITE instead of the ambiguous POLARSSL_ERR_NET_TRY_AGAIN
This commit is contained in:
Paul Bakker 2011-05-18 13:32:51 +00:00
parent e471cd14bd
commit 831a755d9e
12 changed files with 59 additions and 35 deletions

View File

@ -20,6 +20,15 @@ Changes
* The error codes have been remapped and combining error codes * The error codes have been remapped and combining error codes
is now done with a PLUS instead of an OR as error codes is now done with a PLUS instead of an OR as error codes
used are negative. used are negative.
* Changed behaviour of net_read(), ssl_fetch_input() and ssl_recv().
net_recv() now returns 0 on EOF instead of
POLARSSL_ERR_NET_CONN_RESET. ssl_fetch_input() returns
POLARSSL_ERR_SSL_CONN_EOF on an EOF from its f_recv() function.
ssl_read() returns 0 if a POLARSSL_ERR_SSL_CONN_EOF is received
after the handshake.
* Network functions now return POLARSSL_ERR_NET_WANT_READ or
POLARSSL_ERR_NET_WANT_WRITE instead of the ambiguous
POLARSSL_ERR_NET_TRY_AGAIN
= Version 0.99-pre4 released on 2011-04-01 = Version 0.99-pre4 released on 2011-04-01
Features Features

View File

@ -56,7 +56,7 @@
* XTEA 1 0x0028-0x0028 * XTEA 1 0x0028-0x0028
* PADLOCK 1 0x0030-0x0030 * PADLOCK 1 0x0030-0x0030
* DES 1 0x0032-0x0032 * DES 1 0x0032-0x0032
* NET 10 0x0040-0x0052 * NET 11 0x0040-0x0054
* *
* High-level module nr (3 bits - 0x1...-0x8...) * High-level module nr (3 bits - 0x1...-0x8...)
* Name ID Nr of Errors * Name ID Nr of Errors

View File

@ -38,7 +38,8 @@
#define POLARSSL_ERR_NET_RECV_FAILED -0x004C /**< Reading information from the socket failed. */ #define POLARSSL_ERR_NET_RECV_FAILED -0x004C /**< Reading information from the socket failed. */
#define POLARSSL_ERR_NET_SEND_FAILED -0x004E /**< Sending information through the socket failed. */ #define POLARSSL_ERR_NET_SEND_FAILED -0x004E /**< Sending information through the socket failed. */
#define POLARSSL_ERR_NET_CONN_RESET -0x0050 /**< Connection was reset by peer. */ #define POLARSSL_ERR_NET_CONN_RESET -0x0050 /**< Connection was reset by peer. */
#define POLARSSL_ERR_NET_TRY_AGAIN -0x0052 /**< Connection was busy, try again. */ #define POLARSSL_ERR_NET_WANT_READ -0x0052 /**< Connection requires a read call. */
#define POLARSSL_ERR_NET_WANT_WRITE -0x0054 /**< Connection requires a write call. */
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@ -123,7 +124,7 @@ void net_usleep( unsigned long usec );
* \param len Maximum length of the buffer * \param len Maximum length of the buffer
* *
* \return This function returns the number of bytes received, * \return This function returns the number of bytes received,
* or a non-zero error code; POLARSSL_ERR_NET_TRY_AGAIN * or a non-zero error code; POLARSSL_ERR_NET_WANT_READ
* indicates read() is blocking. * indicates read() is blocking.
*/ */
int net_recv( void *ctx, unsigned char *buf, size_t len ); int net_recv( void *ctx, unsigned char *buf, size_t len );
@ -137,7 +138,7 @@ int net_recv( void *ctx, unsigned char *buf, size_t len );
* \param len The length of the buffer * \param len The length of the buffer
* *
* \return This function returns the number of bytes sent, * \return This function returns the number of bytes sent,
* or a non-zero error code; POLARSSL_ERR_NET_TRY_AGAIN * or a non-zero error code; POLARSSL_ERR_NET_WANT_WRITE
* indicates write() is blocking. * indicates write() is blocking.
*/ */
int net_send( void *ctx, unsigned char *buf, size_t len ); int net_send( void *ctx, unsigned char *buf, size_t len );

View File

@ -52,7 +52,7 @@
#define POLARSSL_ERR_SSL_BAD_INPUT_DATA -0x7100 /**< Bad input parameters to function. */ #define POLARSSL_ERR_SSL_BAD_INPUT_DATA -0x7100 /**< Bad input parameters to function. */
#define POLARSSL_ERR_SSL_INVALID_MAC -0x7180 /**< Verification of the message MAC failed. */ #define POLARSSL_ERR_SSL_INVALID_MAC -0x7180 /**< Verification of the message MAC failed. */
#define POLARSSL_ERR_SSL_INVALID_RECORD -0x7200 /**< An invalid SSL record was received. */ #define POLARSSL_ERR_SSL_INVALID_RECORD -0x7200 /**< An invalid SSL record was received. */
#define POLARSSL_ERR_SSL_INVALID_MODULUS_SIZE -0x7280 /**< An invalid modulus size was received. */ #define POLARSSL_ERR_SSL_CONN_EOF -0x7280 /**< The connection indicated an EOF. */
#define POLARSSL_ERR_SSL_UNKNOWN_CIPHER -0x7300 /**< An unknown cipher was received. */ #define POLARSSL_ERR_SSL_UNKNOWN_CIPHER -0x7300 /**< An unknown cipher was received. */
#define POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN -0x7380 /**< The server has no ciphersuites in common with the client. */ #define POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN -0x7380 /**< The server has no ciphersuites in common with the client. */
#define POLARSSL_ERR_SSL_NO_SESSION_FOUND -0x7400 /**< No session to recover was found. */ #define POLARSSL_ERR_SSL_NO_SESSION_FOUND -0x7400 /**< No session to recover was found. */
@ -596,8 +596,8 @@ const char *ssl_get_version( const ssl_context *ssl );
* *
* \param ssl SSL context * \param ssl SSL context
* *
* \return 0 if successful, POLARSSL_ERR_NET_TRY_AGAIN, * \return 0 if successful, POLARSSL_ERR_NET_WANT_READ,
* or a specific SSL error code. * POLARSSL_ERR_NET_WANT_WRITE, or a specific SSL error code.
*/ */
int ssl_handshake( ssl_context *ssl ); int ssl_handshake( ssl_context *ssl );
@ -608,7 +608,7 @@ int ssl_handshake( ssl_context *ssl );
* \param buf buffer that will hold the data * \param buf buffer that will hold the data
* \param len how many bytes must be read * \param len how many bytes must be read
* *
* \return This function returns the number of bytes read, * \return This function returns the number of bytes read, 0 for EOF,
* or a negative error code. * or a negative error code.
*/ */
int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len ); int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len );
@ -623,7 +623,7 @@ int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len );
* \return This function returns the number of bytes written, * \return This function returns the number of bytes written,
* or a negative error code. * or a negative error code.
* *
* \note When this function returns POLARSSL_ERR_NET_TRY_AGAIN, * \note When this function returns POLARSSL_ERR_NET_WANT_WRITE,
* it must be called later with the *same* arguments, * it must be called later with the *same* arguments,
* until it returns a positive value. * until it returns a positive value.
*/ */
@ -653,6 +653,10 @@ int ssl_derive_keys( ssl_context *ssl );
void ssl_calc_verify( ssl_context *ssl, unsigned char hash[36] ); void ssl_calc_verify( ssl_context *ssl, unsigned char hash[36] );
int ssl_read_record( ssl_context *ssl ); int ssl_read_record( ssl_context *ssl );
/**
* \return 0 if successful, POLARSSL_ERR_SSL_CONN_EOF on EOF or
* another negative error code.
*/
int ssl_fetch_input( ssl_context *ssl, size_t nb_want ); int ssl_fetch_input( ssl_context *ssl, size_t nb_want );
int ssl_write_record( ssl_context *ssl ); int ssl_write_record( ssl_context *ssl );

View File

@ -71,7 +71,7 @@
#include "polarssl/rsa.h" #include "polarssl/rsa.h"
#endif #endif
#if defined(POLARSSL_SSL_C) #if defined(POLARSSL_SSL_TLS_C)
#include "polarssl/ssl.h" #include "polarssl/ssl.h"
#endif #endif
@ -162,7 +162,7 @@ void error_strerror( int ret, char *buf, size_t buflen )
snprintf( buf, buflen, "RSA - The random generator failed to generate non-zeros" ); snprintf( buf, buflen, "RSA - The random generator failed to generate non-zeros" );
#endif /* POLARSSL_RSA_C */ #endif /* POLARSSL_RSA_C */
#if defined(POLARSSL_SSL_C) #if defined(POLARSSL_SSL_TLS_C)
if( use_ret == -(POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE) ) if( use_ret == -(POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE) )
snprintf( buf, buflen, "SSL - The requested feature is not available" ); snprintf( buf, buflen, "SSL - The requested feature is not available" );
if( use_ret == -(POLARSSL_ERR_SSL_BAD_INPUT_DATA) ) if( use_ret == -(POLARSSL_ERR_SSL_BAD_INPUT_DATA) )
@ -171,8 +171,8 @@ void error_strerror( int ret, char *buf, size_t buflen )
snprintf( buf, buflen, "SSL - Verification of the message MAC failed" ); snprintf( buf, buflen, "SSL - Verification of the message MAC failed" );
if( use_ret == -(POLARSSL_ERR_SSL_INVALID_RECORD) ) if( use_ret == -(POLARSSL_ERR_SSL_INVALID_RECORD) )
snprintf( buf, buflen, "SSL - An invalid SSL record was received" ); snprintf( buf, buflen, "SSL - An invalid SSL record was received" );
if( use_ret == -(POLARSSL_ERR_SSL_INVALID_MODULUS_SIZE) ) if( use_ret == -(POLARSSL_ERR_SSL_CONN_EOF) )
snprintf( buf, buflen, "SSL - An invalid modulus size was received" ); snprintf( buf, buflen, "SSL - The connection indicated an EOF" );
if( use_ret == -(POLARSSL_ERR_SSL_UNKNOWN_CIPHER) ) if( use_ret == -(POLARSSL_ERR_SSL_UNKNOWN_CIPHER) )
snprintf( buf, buflen, "SSL - An unknown cipher was received" ); snprintf( buf, buflen, "SSL - An unknown cipher was received" );
if( use_ret == -(POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN) ) if( use_ret == -(POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN) )
@ -221,7 +221,7 @@ void error_strerror( int ret, char *buf, size_t buflen )
snprintf( buf, buflen, "SSL - Processing of the ChangeCipherSpec handshake message failed" ); snprintf( buf, buflen, "SSL - Processing of the ChangeCipherSpec handshake message failed" );
if( use_ret == -(POLARSSL_ERR_SSL_BAD_HS_FINISHED) ) if( use_ret == -(POLARSSL_ERR_SSL_BAD_HS_FINISHED) )
snprintf( buf, buflen, "SSL - Processing of the Finished handshake message failed" ); snprintf( buf, buflen, "SSL - Processing of the Finished handshake message failed" );
#endif /* POLARSSL_SSL_C */ #endif /* POLARSSL_SSL_TLS_C */
#if defined(POLARSSL_X509_PARSE_C) #if defined(POLARSSL_X509_PARSE_C)
if( use_ret == -(POLARSSL_ERR_X509_FEATURE_UNAVAILABLE) ) if( use_ret == -(POLARSSL_ERR_X509_FEATURE_UNAVAILABLE) )
@ -355,8 +355,10 @@ void error_strerror( int ret, char *buf, size_t buflen )
snprintf( buf, buflen, "NET - Sending information through the socket failed" ); snprintf( buf, buflen, "NET - Sending information through the socket failed" );
if( use_ret == -(POLARSSL_ERR_NET_CONN_RESET) ) if( use_ret == -(POLARSSL_ERR_NET_CONN_RESET) )
snprintf( buf, buflen, "NET - Connection was reset by peer" ); snprintf( buf, buflen, "NET - Connection was reset by peer" );
if( use_ret == -(POLARSSL_ERR_NET_TRY_AGAIN) ) if( use_ret == -(POLARSSL_ERR_NET_WANT_READ) )
snprintf( buf, buflen, "NET - Connection was busy, try again" ); snprintf( buf, buflen, "NET - Connection requires a read call" );
if( use_ret == -(POLARSSL_ERR_NET_WANT_WRITE) )
snprintf( buf, buflen, "NET - Connection requires a write call" );
#endif /* POLARSSL_NET_C */ #endif /* POLARSSL_NET_C */
#if defined(POLARSSL_PADLOCK_C) #if defined(POLARSSL_PADLOCK_C)

View File

@ -239,7 +239,7 @@ int net_accept( int bind_fd, int *client_fd, void *client_ip )
if( *client_fd < 0 ) if( *client_fd < 0 )
{ {
if( net_is_blocking() != 0 ) if( net_is_blocking() != 0 )
return( POLARSSL_ERR_NET_TRY_AGAIN ); return( POLARSSL_ERR_NET_WANT_READ );
return( POLARSSL_ERR_NET_ACCEPT_FAILED ); return( POLARSSL_ERR_NET_ACCEPT_FAILED );
} }
@ -292,13 +292,10 @@ int net_recv( void *ctx, unsigned char *buf, size_t len )
{ {
int ret = read( *((int *) ctx), buf, len ); int ret = read( *((int *) ctx), buf, len );
if( len > 0 && ret == 0 )
return( POLARSSL_ERR_NET_CONN_RESET );
if( ret < 0 ) if( ret < 0 )
{ {
if( net_is_blocking() != 0 ) if( net_is_blocking() != 0 )
return( POLARSSL_ERR_NET_TRY_AGAIN ); return( POLARSSL_ERR_NET_WANT_READ );
#if defined(_WIN32) || defined(_WIN32_WCE) #if defined(_WIN32) || defined(_WIN32_WCE)
if( WSAGetLastError() == WSAECONNRESET ) if( WSAGetLastError() == WSAECONNRESET )
@ -308,7 +305,7 @@ int net_recv( void *ctx, unsigned char *buf, size_t len )
return( POLARSSL_ERR_NET_CONN_RESET ); return( POLARSSL_ERR_NET_CONN_RESET );
if( errno == EINTR ) if( errno == EINTR )
return( POLARSSL_ERR_NET_TRY_AGAIN ); return( POLARSSL_ERR_NET_WANT_READ );
#endif #endif
return( POLARSSL_ERR_NET_RECV_FAILED ); return( POLARSSL_ERR_NET_RECV_FAILED );
@ -327,7 +324,7 @@ int net_send( void *ctx, unsigned char *buf, size_t len )
if( ret < 0 ) if( ret < 0 )
{ {
if( net_is_blocking() != 0 ) if( net_is_blocking() != 0 )
return( POLARSSL_ERR_NET_TRY_AGAIN ); return( POLARSSL_ERR_NET_WANT_WRITE );
#if defined(_WIN32) || defined(_WIN32_WCE) #if defined(_WIN32) || defined(_WIN32_WCE)
if( WSAGetLastError() == WSAECONNRESET ) if( WSAGetLastError() == WSAECONNRESET )
@ -337,7 +334,7 @@ int net_send( void *ctx, unsigned char *buf, size_t len )
return( POLARSSL_ERR_NET_CONN_RESET ); return( POLARSSL_ERR_NET_CONN_RESET );
if( errno == EINTR ) if( errno == EINTR )
return( POLARSSL_ERR_NET_TRY_AGAIN ); return( POLARSSL_ERR_NET_WANT_WRITE );
#endif #endif
return( POLARSSL_ERR_NET_SEND_FAILED ); return( POLARSSL_ERR_NET_SEND_FAILED );

View File

@ -878,6 +878,9 @@ int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
ssl->in_left, nb_want ) ); ssl->in_left, nb_want ) );
SSL_DEBUG_RET( 2, "ssl->f_recv", ret ); SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
if( ret == 0 )
return( POLARSSL_ERR_SSL_CONN_EOF );
if( ret < 0 ) if( ret < 0 )
return( ret ); return( ret );
@ -2092,6 +2095,9 @@ int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
{ {
if( ( ret = ssl_read_record( ssl ) ) != 0 ) if( ( ret = ssl_read_record( ssl ) ) != 0 )
{ {
if( ret == POLARSSL_ERR_SSL_CONN_EOF )
return( 0 );
SSL_DEBUG_RET( 1, "ssl_read_record", ret ); SSL_DEBUG_RET( 1, "ssl_read_record", ret );
return( ret ); return( ret );
} }
@ -2104,6 +2110,9 @@ int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
*/ */
if( ( ret = ssl_read_record( ssl ) ) != 0 ) if( ( ret = ssl_read_record( ssl ) ) != 0 )
{ {
if( ret == POLARSSL_ERR_SSL_CONN_EOF )
return( 0 );
SSL_DEBUG_RET( 1, "ssl_read_record", ret ); SSL_DEBUG_RET( 1, "ssl_read_record", ret );
return( ret ); return( ret );
} }

View File

@ -115,7 +115,7 @@ int main( void )
while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 ) while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
{ {
if( ret != POLARSSL_ERR_NET_TRY_AGAIN ) if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
{ {
printf( " failed\n ! ssl_write returned %d\n\n", ret ); printf( " failed\n ! ssl_write returned %d\n\n", ret );
goto exit; goto exit;
@ -137,7 +137,7 @@ int main( void )
memset( buf, 0, sizeof( buf ) ); memset( buf, 0, sizeof( buf ) );
ret = ssl_read( &ssl, buf, len ); ret = ssl_read( &ssl, buf, len );
if( ret == POLARSSL_ERR_NET_TRY_AGAIN ) if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
continue; continue;
if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY ) if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY )

View File

@ -298,7 +298,7 @@ int main( int argc, char *argv[] )
while( ( ret = ssl_handshake( &ssl ) ) != 0 ) while( ( ret = ssl_handshake( &ssl ) ) != 0 )
{ {
if( ret != POLARSSL_ERR_NET_TRY_AGAIN ) if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
{ {
printf( " failed\n ! ssl_handshake returned %d\n\n", ret ); printf( " failed\n ! ssl_handshake returned %d\n\n", ret );
goto exit; goto exit;
@ -348,7 +348,7 @@ int main( int argc, char *argv[] )
while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 ) while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
{ {
if( ret != POLARSSL_ERR_NET_TRY_AGAIN ) if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
{ {
printf( " failed\n ! ssl_write returned %d\n\n", ret ); printf( " failed\n ! ssl_write returned %d\n\n", ret );
goto exit; goto exit;
@ -370,7 +370,7 @@ int main( int argc, char *argv[] )
memset( buf, 0, sizeof( buf ) ); memset( buf, 0, sizeof( buf ) );
ret = ssl_read( &ssl, buf, len ); ret = ssl_read( &ssl, buf, len );
if( ret == POLARSSL_ERR_NET_TRY_AGAIN ) if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
continue; continue;
if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY ) if( ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY )

View File

@ -305,7 +305,7 @@ accept:
while( ( ret = ssl_handshake( &ssl ) ) != 0 ) while( ( ret = ssl_handshake( &ssl ) ) != 0 )
{ {
if( ret != POLARSSL_ERR_NET_TRY_AGAIN ) if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
{ {
printf( " failed\n ! ssl_handshake returned %d\n\n", ret ); printf( " failed\n ! ssl_handshake returned %d\n\n", ret );
goto accept; goto accept;
@ -326,7 +326,7 @@ accept:
memset( buf, 0, sizeof( buf ) ); memset( buf, 0, sizeof( buf ) );
ret = ssl_read( &ssl, buf, len ); ret = ssl_read( &ssl, buf, len );
if( ret == POLARSSL_ERR_NET_TRY_AGAIN ) if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
continue; continue;
if( ret <= 0 ) if( ret <= 0 )
@ -371,7 +371,7 @@ accept:
goto accept; goto accept;
} }
if( ret != POLARSSL_ERR_NET_TRY_AGAIN ) if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
{ {
printf( " failed\n ! ssl_write returned %d\n\n", ret ); printf( " failed\n ! ssl_write returned %d\n\n", ret );
goto exit; goto exit;

View File

@ -293,7 +293,8 @@ static int ssl_test( struct options *opt )
goto exit; goto exit;
} }
if( ret < 0 && ret != POLARSSL_ERR_NET_TRY_AGAIN ) if( ret < 0 && ret != POLARSSL_ERR_NET_WANT_READ &&
ret != POLARSSL_ERR_NET_WANT_WRITE )
{ {
printf( " ! ssl_write returned %d\n\n", ret ); printf( " ! ssl_write returned %d\n\n", ret );
break; break;
@ -336,7 +337,8 @@ static int ssl_test( struct options *opt )
goto exit; goto exit;
} }
if( ret < 0 && ret != POLARSSL_ERR_NET_TRY_AGAIN ) if( ret < 0 && ret != POLARSSL_ERR_NET_WANT_READ &&
ret != POLARSSL_ERR_NET_WANT_WRITE )
{ {
printf( " ! ssl_read returned %d\n\n", ret ); printf( " ! ssl_read returned %d\n\n", ret );
break; break;

View File

@ -243,7 +243,7 @@ int main( int argc, char *argv[] )
*/ */
while( ( ret = ssl_handshake( &ssl ) ) != 0 ) while( ( ret = ssl_handshake( &ssl ) ) != 0 )
{ {
if( ret != POLARSSL_ERR_NET_TRY_AGAIN ) if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
{ {
printf( " failed\n ! ssl_handshake returned %d\n\n", ret ); printf( " failed\n ! ssl_handshake returned %d\n\n", ret );
goto exit; goto exit;