From f52584e7310ad9af414cdd22cbfad81d53417c22 Mon Sep 17 00:00:00 2001 From: Nikias Bassen Date: Fri, 19 Jul 2019 00:06:38 +0700 Subject: OpenSSL: Use SSL_pending() to determine if we want a select() before SSL_read() In order to obey the timeout in idevice_connection_receive_timeout(), we are using select() via socket_check_fd(). However, the SSL bio might have buffered more bytes than actually requested upon a call to SSL_read(), so in the next call to idevice_connection_receive_timeout() a select() would not find the fd being ready to read, and make it fail with an error, after the specified timeout is reached. With the help of SSL_pending() we can now skip calling select() so that SSL_read() will directly be called again. --- src/idevice.c | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/idevice.c b/src/idevice.c index 02d34cc..794af8b 100644 --- a/src/idevice.c +++ b/src/idevice.c @@ -453,19 +453,24 @@ LIBIMOBILEDEVICE_API idevice_error_t idevice_connection_receive_timeout(idevice_ if (connection->ssl_data) { uint32_t received = 0; + int do_select = 1; while (received < len) { - - int conn_error = socket_check_fd((int)(long)connection->data, FDM_READ, timeout); - idevice_error_t error = socket_recv_to_idevice_error(conn_error, len, received); - - switch (error) { - case IDEVICE_E_SUCCESS: - break; - case IDEVICE_E_UNKNOWN_ERROR: - debug_info("ERROR: socket_check_fd returned %d (%s)", conn_error, strerror(-conn_error)); - default: - return error; +#ifdef HAVE_OPENSSL + do_select = (SSL_pending(connection->ssl_data->session) == 0); +#endif + if (do_select) { + int conn_error = socket_check_fd((int)(long)connection->data, FDM_READ, timeout); + idevice_error_t error = socket_recv_to_idevice_error(conn_error, len, received); + + switch (error) { + case IDEVICE_E_SUCCESS: + break; + case IDEVICE_E_UNKNOWN_ERROR: + debug_info("ERROR: socket_check_fd returned %d (%s)", conn_error, strerror(-conn_error)); + default: + return error; + } } #ifdef HAVE_OPENSSL -- cgit v1.1-32-gdbae