Skip to content

Commit 6b4d4a2

Browse files
MrSurlydpgeorge
authored andcommitted
extmod/modussl_mbedtls: Implement non-blocking SSL sockets.
1 parent f368710 commit 6b4d4a2

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

extmod/modussl_mbedtls.c

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
#include <stdio.h>
3131
#include <string.h>
32+
#include <errno.h> // needed because mp_is_nonblocking_error uses system error codes
3233

3334
#include "py/nlr.h"
3435
#include "py/runtime.h"
@@ -83,6 +84,9 @@ int _mbedtls_ssl_send(void *ctx, const byte *buf, size_t len) {
8384

8485
int out_sz = sock_stream->write(sock, buf, len, &err);
8586
if (out_sz == MP_STREAM_ERROR) {
87+
if (mp_is_nonblocking_error(err)) {
88+
return MBEDTLS_ERR_SSL_WANT_WRITE;
89+
}
8690
return -err;
8791
} else {
8892
return out_sz;
@@ -97,6 +101,9 @@ int _mbedtls_ssl_recv(void *ctx, byte *buf, size_t len) {
97101

98102
int out_sz = sock_stream->read(sock, buf, len, &err);
99103
if (out_sz == MP_STREAM_ERROR) {
104+
if (mp_is_nonblocking_error(err)) {
105+
return MBEDTLS_ERR_SSL_WANT_READ;
106+
}
100107
return -err;
101108
} else {
102109
return out_sz;
@@ -199,6 +206,9 @@ STATIC mp_uint_t socket_read(mp_obj_t o_in, void *buf, mp_uint_t size, int *errc
199206
if (ret >= 0) {
200207
return ret;
201208
}
209+
if (ret == MBEDTLS_ERR_SSL_WANT_READ) {
210+
ret = MP_EWOULDBLOCK;
211+
}
202212
*errcode = ret;
203213
return MP_STREAM_ERROR;
204214
}
@@ -210,17 +220,20 @@ STATIC mp_uint_t socket_write(mp_obj_t o_in, const void *buf, mp_uint_t size, in
210220
if (ret >= 0) {
211221
return ret;
212222
}
223+
if (ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
224+
ret = MP_EWOULDBLOCK;
225+
}
213226
*errcode = ret;
214227
return MP_STREAM_ERROR;
215228
}
216229

217230
STATIC mp_obj_t socket_setblocking(mp_obj_t self_in, mp_obj_t flag_in) {
218-
// Currently supports only blocking mode
219-
(void)self_in;
220-
if (!mp_obj_is_true(flag_in)) {
221-
mp_not_implemented("");
222-
}
223-
return mp_const_none;
231+
mp_obj_ssl_socket_t *o = MP_OBJ_TO_PTR(self_in);
232+
mp_obj_t sock = o->sock;
233+
mp_obj_t dest[3];
234+
mp_load_method(sock, MP_QSTR_setblocking, dest);
235+
dest[2] = flag_in;
236+
return mp_call_method_n_kw(1, 0, dest);
224237
}
225238
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_setblocking_obj, socket_setblocking);
226239

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy