Skip to content
This repository was archived by the owner on Sep 16, 2024. It is now read-only.

Commit 70c09a7

Browse files
geza-pycomIslam Wahdan
authored and
Islam Wahdan
committed
PYFW-353: Socket timeout in recv operation is not working in case of ssl socket (#48)
1 parent a12e988 commit 70c09a7

File tree

3 files changed

+37
-25
lines changed

3 files changed

+37
-25
lines changed

esp32/mods/lwipsocket.c

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,11 @@ int lwipsocket_socket_recv(mod_network_socket_obj_t *s, byte *buf, mp_uint_t len
211211
ret = 0;
212212
break;
213213
}
214-
// blocking do nothing
214+
// blocking and timed out, return with error
215+
// mbedtls_net_recv_timeout() returned with timeout
216+
else {
217+
break;
218+
}
215219
}
216220
else if (ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) {
217221
// printf("Close notify received\n");
@@ -278,33 +282,41 @@ int lwipsocket_socket_setsockopt(mod_network_socket_obj_t *s, mp_uint_t level, m
278282

279283
int lwipsocket_socket_settimeout(mod_network_socket_obj_t *s, mp_int_t timeout_ms, int *_errno) {
280284
int ret;
281-
uint32_t option = lwip_fcntl_r(s->sock_base.u.sd, F_GETFL, 0);
282285

283-
if (timeout_ms <= 0) {
284-
if (timeout_ms == 0) {
285-
// set non-blocking mode
286-
option |= O_NONBLOCK;
286+
if (s->sock_base.is_ssl) {
287+
mp_obj_ssl_socket_t *ss = (mp_obj_ssl_socket_t *)s;
288+
// mbedtls_net_recv_timeout() API is registered with mbedtls_ssl_set_bio() so setting timeout on receive works
289+
mbedtls_ssl_conf_read_timeout(&ss->conf, timeout_ms);
290+
}
291+
else {
292+
uint32_t option = lwip_fcntl_r(s->sock_base.u.sd, F_GETFL, 0);
293+
294+
if (timeout_ms <= 0) {
295+
if (timeout_ms == 0) {
296+
// set non-blocking mode
297+
option |= O_NONBLOCK;
298+
} else {
299+
// set blocking mode
300+
option &= ~O_NONBLOCK;
301+
timeout_ms = UINT32_MAX;
302+
}
287303
} else {
288304
// set blocking mode
289305
option &= ~O_NONBLOCK;
290-
timeout_ms = UINT32_MAX;
291306
}
292-
} else {
293-
// set blocking mode
294-
option &= ~O_NONBLOCK;
295-
}
296307

297-
// set the timeout
298-
struct timeval tv;
299-
tv.tv_sec = timeout_ms / 1000; // seconds
300-
tv.tv_usec = (timeout_ms % 1000) * 1000; // microseconds
301-
ret = lwip_setsockopt_r(s->sock_base.u.sd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
302-
ret |= lwip_setsockopt_r(s->sock_base.u.sd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
303-
ret |= lwip_fcntl_r(s->sock_base.u.sd, F_SETFL, option);
308+
// set the timeout
309+
struct timeval tv;
310+
tv.tv_sec = timeout_ms / 1000; // seconds
311+
tv.tv_usec = (timeout_ms % 1000) * 1000; // microseconds
312+
ret = lwip_setsockopt_r(s->sock_base.u.sd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
313+
ret |= lwip_setsockopt_r(s->sock_base.u.sd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
314+
ret |= lwip_fcntl_r(s->sock_base.u.sd, F_SETFL, option);
304315

305-
if (ret != 0) {
306-
*_errno = errno;
307-
return -1;
316+
if (ret != 0) {
317+
*_errno = errno;
318+
return -1;
319+
}
308320
}
309321

310322
s->sock_base.timeout = timeout_ms;

esp32/mods/modusocket.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@
6262
#include "lwip/netdb.h"
6363
#include "lwipsocket.h"
6464

65+
#include "mbedtls/ssl.h"
66+
6567
#include "freertos/FreeRTOS.h"
6668
#include "freertos/task.h"
6769
#include "freertos/timers.h"
@@ -497,7 +499,7 @@ STATIC mp_obj_t socket_recv(mp_obj_t self_in, mp_obj_t len_in) {
497499
mp_int_t ret = self->sock_base.nic_type->n_recv(self, (byte*)vstr.buf, len, &_errno);
498500
MP_THREAD_GIL_ENTER();
499501
if (ret < 0) {
500-
if (_errno == MP_EAGAIN) {
502+
if (_errno == MP_EAGAIN || _errno == MBEDTLS_ERR_SSL_TIMEOUT ) {
501503
if (self->sock_base.timeout > 0) {
502504
nlr_raise(mp_obj_new_exception_msg(&mp_type_TimeoutError, "timed out"));
503505
} else {
@@ -573,7 +575,7 @@ STATIC mp_obj_t socket_recvfrom(mp_obj_t self_in, mp_obj_t len_in) {
573575
mp_int_t ret = self->sock_base.nic_type->n_recvfrom(self, (byte*)vstr.buf, vstr.len, ip, &port, &_errno);
574576
MP_THREAD_GIL_ENTER();
575577
if (ret < 0) {
576-
if (_errno == MP_EAGAIN && self->sock_base.timeout > 0) {
578+
if ((_errno == MP_EAGAIN || _errno == MBEDTLS_ERR_SSL_TIMEOUT ) && self->sock_base.timeout > 0) {
577579
nlr_raise(mp_obj_new_exception_msg(&mp_type_TimeoutError, "timed out"));
578580
}
579581
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(_errno)));

esp32/mods/modussl.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,6 @@ static int32_t mod_ssl_setup_socket (mp_obj_ssl_socket_t *ssl_sock, const char *
149149
}
150150
}
151151

152-
mbedtls_ssl_conf_read_timeout(&ssl_sock->conf, 1000);
153-
154152
ssl_sock->context_fd.fd = ssl_sock->sock_base.u.sd;
155153
ssl_sock->sock_base.is_ssl = true;
156154

0 commit comments

Comments
 (0)