Skip to content

Commit 7a67f05

Browse files
committed
extmod/modussl: Support polling in ussl objects by passing through ioctl
The underlying socket can handling polling, and any other transparent ioctl requests. Note that CPython handles the case of polling an ssl object by polling the file descriptor of the underlying socket file, and that behaviour is emulated here.
1 parent 4a2051e commit 7a67f05

2 files changed

Lines changed: 16 additions & 30 deletions

File tree

‎extmod/modussl_axtls.c‎

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -177,21 +177,13 @@ STATIC mp_uint_t socket_write(mp_obj_t o_in, const void *buf, mp_uint_t size, in
177177

178178
STATIC mp_uint_t socket_ioctl(mp_obj_t o_in, mp_uint_t request, uintptr_t arg, int *errcode) {
179179
mp_obj_ssl_socket_t *self = MP_OBJ_TO_PTR(o_in);
180-
(void)arg;
181-
switch (request) {
182-
case MP_STREAM_CLOSE:
183-
if (self->ssl_sock != NULL) {
184-
ssl_free(self->ssl_sock);
185-
ssl_ctx_free(self->ssl_ctx);
186-
self->ssl_sock = NULL;
187-
mp_stream_close(self->sock);
188-
}
189-
return 0;
190-
191-
default:
192-
*errcode = MP_EINVAL;
193-
return MP_STREAM_ERROR;
180+
if (request == MP_STREAM_CLOSE && self->ssl_sock != NULL) {
181+
ssl_free(self->ssl_sock);
182+
ssl_ctx_free(self->ssl_ctx);
183+
self->ssl_sock = NULL;
194184
}
185+
// Pass all requests down to the underlying socket
186+
return mp_get_stream(self->sock)->ioctl(self->sock, request, arg, errcode);
195187
}
196188

197189
STATIC mp_obj_t socket_setblocking(mp_obj_t self_in, mp_obj_t flag_in) {

‎extmod/modussl_mbedtls.c‎

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -270,23 +270,17 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_setblocking_obj, socket_setblocking);
270270

271271
STATIC mp_uint_t socket_ioctl(mp_obj_t o_in, mp_uint_t request, uintptr_t arg, int *errcode) {
272272
mp_obj_ssl_socket_t *self = MP_OBJ_TO_PTR(o_in);
273-
(void)arg;
274-
switch (request) {
275-
case MP_STREAM_CLOSE:
276-
mbedtls_pk_free(&self->pkey);
277-
mbedtls_x509_crt_free(&self->cert);
278-
mbedtls_x509_crt_free(&self->cacert);
279-
mbedtls_ssl_free(&self->ssl);
280-
mbedtls_ssl_config_free(&self->conf);
281-
mbedtls_ctr_drbg_free(&self->ctr_drbg);
282-
mbedtls_entropy_free(&self->entropy);
283-
mp_stream_close(self->sock);
284-
return 0;
285-
286-
default:
287-
*errcode = MP_EINVAL;
288-
return MP_STREAM_ERROR;
273+
if (request == MP_STREAM_CLOSE) {
274+
mbedtls_pk_free(&self->pkey);
275+
mbedtls_x509_crt_free(&self->cert);
276+
mbedtls_x509_crt_free(&self->cacert);
277+
mbedtls_ssl_free(&self->ssl);
278+
mbedtls_ssl_config_free(&self->conf);
279+
mbedtls_ctr_drbg_free(&self->ctr_drbg);
280+
mbedtls_entropy_free(&self->entropy);
289281
}
282+
// Pass all requests down to the underlying socket
283+
return mp_get_stream(self->sock)->ioctl(self->sock, request, arg, errcode);
290284
}
291285

292286
STATIC const mp_rom_map_elem_t ussl_socket_locals_dict_table[] = {

0 commit comments

Comments
 (0)