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

Commit 2a9f771

Browse files
geza-pycomiwahdan88
authored and
iwahdan88
committed
PYFW-353: Socket timeout in recv operation is not working in case of ssl socket (#48)
1 parent 2e48c2f commit 2a9f771

File tree

3 files changed

+37
-25
lines changed

3 files changed

+37
-25
lines changed

esp32/mods/lwipsocket.c

+33-21
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

+4-2
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@
6363
#include "lwip/netdb.h"
6464
#include "lwipsocket.h"
6565

66+
#include "mbedtls/ssl.h"
67+
6668
#include "freertos/FreeRTOS.h"
6769
#include "freertos/task.h"
6870
#include "freertos/timers.h"
@@ -498,7 +500,7 @@ STATIC mp_obj_t socket_recv(mp_obj_t self_in, mp_obj_t len_in) {
498500
mp_int_t ret = self->sock_base.nic_type->n_recv(self, (byte*)vstr.buf, len, &_errno);
499501
MP_THREAD_GIL_ENTER();
500502
if (ret < 0) {
501-
if (_errno == MP_EAGAIN) {
503+
if (_errno == MP_EAGAIN || _errno == MBEDTLS_ERR_SSL_TIMEOUT ) {
502504
if (self->sock_base.timeout > 0) {
503505
nlr_raise(mp_obj_new_exception_msg(&mp_type_TimeoutError, "timed out"));
504506
} else {
@@ -574,7 +576,7 @@ STATIC mp_obj_t socket_recvfrom(mp_obj_t self_in, mp_obj_t len_in) {
574576
mp_int_t ret = self->sock_base.nic_type->n_recvfrom(self, (byte*)vstr.buf, vstr.len, ip, &port, &_errno);
575577
MP_THREAD_GIL_ENTER();
576578
if (ret < 0) {
577-
if (_errno == MP_EAGAIN && self->sock_base.timeout > 0) {
579+
if ((_errno == MP_EAGAIN || _errno == MBEDTLS_ERR_SSL_TIMEOUT ) && self->sock_base.timeout > 0) {
578580
nlr_raise(mp_obj_new_exception_msg(&mp_type_TimeoutError, "timed out"));
579581
}
580582
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(_errno)));

esp32/mods/modussl.c

-2
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)