Add net_recv_timeout()
diff --git a/library/error.c b/library/error.c
index 35ecb97..f3c18c2 100644
--- a/library/error.c
+++ b/library/error.c
@@ -658,8 +658,6 @@
#endif /* POLARSSL_MD5_C */
#if defined(POLARSSL_NET_C)
- if( use_ret == -(POLARSSL_ERR_NET_UNKNOWN_HOST) )
- snprintf( buf, buflen, "NET - Failed to get an IP address for the given hostname" );
if( use_ret == -(POLARSSL_ERR_NET_SOCKET_FAILED) )
snprintf( buf, buflen, "NET - Failed to open a socket" );
if( use_ret == -(POLARSSL_ERR_NET_CONNECT_FAILED) )
@@ -680,6 +678,10 @@
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" );
+ if( use_ret == -(POLARSSL_ERR_NET_UNKNOWN_HOST) )
+ snprintf( buf, buflen, "NET - Failed to get an IP address for the given hostname" );
+ if( use_ret == -(POLARSSL_ERR_NET_TIMEOUT) )
+ snprintf( buf, buflen, "NET - The operation timed out" );
#endif /* POLARSSL_NET_C */
#if defined(POLARSSL_OID_C)
diff --git a/library/net.c b/library/net.c
index a0af807..7336f21 100644
--- a/library/net.c
+++ b/library/net.c
@@ -583,6 +583,49 @@
return( ret );
}
+#if defined(POLARSSL_HAVE_TIME)
+/*
+ * Read at most 'len' characters, blocking for at most 'timeout' seconds
+ */
+int net_recv_timeout( void *ctx, unsigned char *buf, size_t len,
+ unsigned char timeout )
+{
+ int ret;
+ struct timeval tv;
+ fd_set read_fds;
+ int fd = *((int *) ctx);
+
+ FD_ZERO( &read_fds );
+ FD_SET( fd, &read_fds );
+
+ tv.tv_sec = timeout;
+ tv.tv_usec = 0;
+
+ ret = select( fd + 1, &read_fds, NULL, NULL, &tv );
+
+ /* Zero fds ready means we timed out */
+ if( ret == 0 )
+ return( POLARSSL_ERR_NET_TIMEOUT );
+
+ if( ret < 0 )
+ {
+#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
+ !defined(EFI32)
+ if( WSAGetLastError() == WSAEINTR )
+ return( POLARSSL_ERR_NET_WANT_READ );
+#else
+ if( errno == EINTR )
+ return( POLARSSL_ERR_NET_WANT_READ );
+#endif
+
+ return( POLARSSL_ERR_NET_RECV_FAILED );
+ }
+
+ /* This call will not block */
+ return( net_recv( ctx, buf, len ) );
+}
+#endif /* POLARSSL_HAVE_TIME */
+
/*
* Write at most 'len' characters
*/