extmod/modussl_axtls: Allow to close ssl stream multiple times.

Make sure that 2nd close has no effect and operations on closed streams
are handled properly.
This commit is contained in:
Paul Sokolovsky 2017-07-20 00:20:53 +03:00
parent 4662006119
commit 4368ae3142
3 changed files with 29 additions and 4 deletions

View File

@ -102,6 +102,11 @@ STATIC void socket_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kin
STATIC mp_uint_t socket_read(mp_obj_t o_in, void *buf, mp_uint_t size, int *errcode) { STATIC mp_uint_t socket_read(mp_obj_t o_in, void *buf, mp_uint_t size, int *errcode) {
mp_obj_ssl_socket_t *o = MP_OBJ_TO_PTR(o_in); mp_obj_ssl_socket_t *o = MP_OBJ_TO_PTR(o_in);
if (o->ssl_sock == NULL) {
*errcode = EBADF;
return MP_STREAM_ERROR;
}
while (o->bytes_left == 0) { while (o->bytes_left == 0) {
mp_int_t r = ssl_read(o->ssl_sock, &o->buf); mp_int_t r = ssl_read(o->ssl_sock, &o->buf);
if (r == SSL_OK) { if (r == SSL_OK) {
@ -131,6 +136,12 @@ STATIC mp_uint_t socket_read(mp_obj_t o_in, void *buf, mp_uint_t size, int *errc
STATIC mp_uint_t socket_write(mp_obj_t o_in, const void *buf, mp_uint_t size, int *errcode) { STATIC mp_uint_t socket_write(mp_obj_t o_in, const void *buf, mp_uint_t size, int *errcode) {
mp_obj_ssl_socket_t *o = MP_OBJ_TO_PTR(o_in); mp_obj_ssl_socket_t *o = MP_OBJ_TO_PTR(o_in);
if (o->ssl_sock == NULL) {
*errcode = EBADF;
return MP_STREAM_ERROR;
}
mp_int_t r = ssl_write(o->ssl_sock, buf, size); mp_int_t r = ssl_write(o->ssl_sock, buf, size);
if (r < 0) { if (r < 0) {
*errcode = r; *errcode = r;
@ -151,9 +162,14 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_setblocking_obj, socket_setblocking);
STATIC mp_obj_t socket_close(mp_obj_t self_in) { STATIC mp_obj_t socket_close(mp_obj_t self_in) {
mp_obj_ssl_socket_t *self = MP_OBJ_TO_PTR(self_in); mp_obj_ssl_socket_t *self = MP_OBJ_TO_PTR(self_in);
ssl_free(self->ssl_sock); if (self->ssl_sock != NULL) {
ssl_ctx_free(self->ssl_ctx); ssl_free(self->ssl_sock);
return mp_stream_close(self->sock); ssl_ctx_free(self->ssl_ctx);
self->ssl_sock = NULL;
return mp_stream_close(self->sock);
}
return mp_const_none;
} }
STATIC MP_DEFINE_CONST_FUN_OBJ_1(socket_close_obj, socket_close); STATIC MP_DEFINE_CONST_FUN_OBJ_1(socket_close_obj, socket_close);

View File

@ -43,6 +43,14 @@ except OSError as er:
# close # close
ss.close() ss.close()
# close 2nd time
ss.close()
# read on closed socket
try:
ss.read(10)
except OSError as er:
print('read:', repr(er))
# write on closed socket # write on closed socket
try: try:

View File

@ -5,4 +5,5 @@ setblocking: NotImplementedError
4 4
b'' b''
read: OSError(-261,) read: OSError(-261,)
write: OSError(-256,) read: OSError(9,)
write: OSError(9,)