SSLSocket: handle exceptions during protocol read/write operations
These protocol operations should not raise exceptions, but sometimes they do. Catch the exception and extract the errno value if available. At the same time, harmonize the argument types for the underlying C routines
This commit is contained in:
parent
49a612056e
commit
3215f6c4ff
3 changed files with 24 additions and 13 deletions
|
|
@ -248,9 +248,22 @@ static const mp_rom_map_elem_t ssl_sslsocket_locals_dict_table[] = {
|
|||
|
||||
static MP_DEFINE_CONST_DICT(ssl_sslsocket_locals_dict, ssl_sslsocket_locals_dict_table);
|
||||
|
||||
static mp_uint_t sslsocket_read(mp_obj_t self_in, void *buf, mp_uint_t size, int *errorcode) {
|
||||
typedef mp_uint_t (*readwrite_func)(ssl_sslsocket_obj_t *, const uint8_t *, mp_uint_t);
|
||||
|
||||
static mp_int_t readwrite_common(mp_obj_t self_in, readwrite_func fn, const uint8_t *buf, size_t size, int *errorcode) {
|
||||
ssl_sslsocket_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
mp_int_t ret = common_hal_ssl_sslsocket_recv_into(self, buf, size);
|
||||
mp_int_t ret;
|
||||
nlr_buf_t nlr;
|
||||
if (nlr_push(&nlr) == 0) {
|
||||
ret = fn(self, buf, size);
|
||||
nlr_pop();
|
||||
} else {
|
||||
mp_obj_t exc = MP_OBJ_FROM_PTR(nlr.ret_val);
|
||||
if (nlr_push(&nlr) == 0) {
|
||||
ret = -mp_obj_get_int(mp_load_attr(exc, MP_QSTR_errno));
|
||||
nlr_pop();
|
||||
}
|
||||
}
|
||||
if (ret < 0) {
|
||||
*errorcode = -ret;
|
||||
return MP_STREAM_ERROR;
|
||||
|
|
@ -258,14 +271,12 @@ static mp_uint_t sslsocket_read(mp_obj_t self_in, void *buf, mp_uint_t size, int
|
|||
return ret;
|
||||
}
|
||||
|
||||
static mp_uint_t sslsocket_read(mp_obj_t self_in, void *buf, mp_uint_t size, int *errorcode) {
|
||||
return readwrite_common(self_in, (readwrite_func)common_hal_ssl_sslsocket_recv_into, buf, size, errorcode);
|
||||
}
|
||||
|
||||
static mp_uint_t sslsocket_write(mp_obj_t self_in, const void *buf, mp_uint_t size, int *errorcode) {
|
||||
ssl_sslsocket_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
mp_int_t ret = common_hal_ssl_sslsocket_send(self, buf, size);
|
||||
if (ret < 0) {
|
||||
*errorcode = -ret;
|
||||
return MP_STREAM_ERROR;
|
||||
}
|
||||
return ret;
|
||||
return readwrite_common(self_in, common_hal_ssl_sslsocket_send, buf, size, errorcode);
|
||||
}
|
||||
|
||||
static mp_uint_t sslsocket_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint_t arg, int *errcode) {
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ bool common_hal_ssl_sslsocket_get_connected(ssl_sslsocket_obj_t *self);
|
|||
bool common_hal_ssl_sslsocket_readable(ssl_sslsocket_obj_t *self);
|
||||
bool common_hal_ssl_sslsocket_writable(ssl_sslsocket_obj_t *self);
|
||||
void common_hal_ssl_sslsocket_listen(ssl_sslsocket_obj_t *self, int backlog);
|
||||
mp_uint_t common_hal_ssl_sslsocket_recv_into(ssl_sslsocket_obj_t *self, uint8_t *buf, uint32_t len);
|
||||
mp_uint_t common_hal_ssl_sslsocket_send(ssl_sslsocket_obj_t *self, const uint8_t *buf, uint32_t len);
|
||||
mp_uint_t common_hal_ssl_sslsocket_recv_into(ssl_sslsocket_obj_t *self, uint8_t *buf, mp_uint_t len);
|
||||
mp_uint_t common_hal_ssl_sslsocket_send(ssl_sslsocket_obj_t *self, const uint8_t *buf, mp_uint_t len);
|
||||
void common_hal_ssl_sslsocket_settimeout(ssl_sslsocket_obj_t *self, mp_obj_t timeout_obj);
|
||||
void common_hal_ssl_sslsocket_setsockopt(ssl_sslsocket_obj_t *self, mp_obj_t level, mp_obj_t optname, mp_obj_t optval);
|
||||
|
|
|
|||
|
|
@ -333,7 +333,7 @@ cleanup:
|
|||
}
|
||||
}
|
||||
|
||||
mp_uint_t common_hal_ssl_sslsocket_recv_into(ssl_sslsocket_obj_t *self, uint8_t *buf, uint32_t len) {
|
||||
mp_uint_t common_hal_ssl_sslsocket_recv_into(ssl_sslsocket_obj_t *self, uint8_t *buf, mp_uint_t len) {
|
||||
self->poll_mask = 0;
|
||||
int ret = mbedtls_ssl_read(&self->ssl, buf, len);
|
||||
DEBUG_PRINT("recv_into mbedtls_ssl_read() -> %d\n", ret);
|
||||
|
|
@ -353,7 +353,7 @@ mp_uint_t common_hal_ssl_sslsocket_recv_into(ssl_sslsocket_obj_t *self, uint8_t
|
|||
mbedtls_raise_error(ret);
|
||||
}
|
||||
|
||||
mp_uint_t common_hal_ssl_sslsocket_send(ssl_sslsocket_obj_t *self, const uint8_t *buf, uint32_t len) {
|
||||
mp_uint_t common_hal_ssl_sslsocket_send(ssl_sslsocket_obj_t *self, const uint8_t *buf, mp_uint_t len) {
|
||||
self->poll_mask = 0;
|
||||
int ret = mbedtls_ssl_write(&self->ssl, buf, len);
|
||||
DEBUG_PRINT("send mbedtls_ssl_write() -> %d\n", ret);
|
||||
|
|
|
|||
Loading…
Reference in a new issue