Merge pull request #7043 from jepler/picow-socket-bugfixes

Pico W: a number of socketpool and ssl bugfixes
This commit is contained in:
Dan Halbert 2022-10-12 19:13:00 -04:00 committed by GitHub
commit 2ebb45d53f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 7 deletions

View File

@ -821,6 +821,16 @@ socketpool_socket_obj_t *common_hal_socketpool_socket_accept(socketpool_socket_o
MICROPY_PY_LWIP_EXIT
DEBUG_printf("registering socket in socketpool_socket_accept()\n");
if (!register_open_socket(socket2)) {
DEBUG_printf("collecting garbage to open socket\n");
gc_collect();
if (!register_open_socket(socket2)) {
mp_raise_RuntimeError(translate("Out of sockets"));
}
}
mark_user_socket(socket2);
// output values
memcpy(ip, &(socket2->pcb.tcp->remote_ip), NETUTILS_IPV4ADDR_BUFSIZE);
*port = (mp_uint_t)socket2->pcb.tcp->remote_port;
@ -1070,6 +1080,9 @@ int socketpool_socket_recv_into(socketpool_socket_obj_t *socket,
ret = lwip_raw_udp_receive(socket, (byte *)buf, len, NULL, NULL, &_errno);
break;
}
if (ret < 0) {
return -_errno;
}
return ret;
}

View File

@ -42,7 +42,7 @@ void common_hal_ssl_sslcontext_load_verify_locations(ssl_sslcontext_obj_t *self,
self->crt_bundle_attach = NULL;
self->use_global_ca_store = false;
self->cacert_buf = (const unsigned char *)cadata;
self->cacert_bytes = strlen(cadata) + 1;
self->cacert_bytes = *cadata ? strlen(cadata) + 1 : 0;
}
void common_hal_ssl_sslcontext_set_default_verify_paths(ssl_sslcontext_obj_t *self) {

View File

@ -100,7 +100,7 @@ STATIC int _mbedtls_ssl_send(void *ctx, const byte *buf, size_t len) {
mp_obj_t sock = *(mp_obj_t *)ctx;
// mp_uint_t out_sz = sock_stream->write(sock, buf, len, &err);
mp_int_t out_sz = common_hal_socketpool_socket_send(sock, buf, len);
mp_int_t out_sz = socketpool_socket_send(sock, buf, len);
DEBUG("socket_send() -> %d", out_sz);
if (out_sz < 0) {
int err = -out_sz;
@ -118,7 +118,7 @@ STATIC int _mbedtls_ssl_send(void *ctx, const byte *buf, size_t len) {
STATIC int _mbedtls_ssl_recv(void *ctx, byte *buf, size_t len) {
mp_obj_t sock = *(mp_obj_t *)ctx;
mp_int_t out_sz = common_hal_socketpool_socket_recv_into(sock, buf, len);
mp_int_t out_sz = socketpool_socket_recv_into(sock, buf, len);
DEBUG("socket_recv() -> %d", out_sz);
if (out_sz < 0) {
int err = -out_sz;
@ -230,7 +230,7 @@ cleanup:
mbedtls_entropy_free(&o->entropy);
if (ret == MBEDTLS_ERR_SSL_ALLOC_FAILED) {
mp_raise_OSError(MP_ENOMEM);
mp_raise_type(&mp_type_MemoryError);
} else if (ret == MBEDTLS_ERR_PK_BAD_INPUT_DATA) {
mp_raise_ValueError(MP_ERROR_TEXT("invalid key"));
} else if (ret == MBEDTLS_ERR_X509_BAD_INPUT_DATA) {
@ -299,8 +299,7 @@ void common_hal_ssl_sslsocket_close(ssl_sslsocket_obj_t *self) {
mbedtls_entropy_free(&self->entropy);
}
void common_hal_ssl_sslsocket_connect(ssl_sslsocket_obj_t *self, const char *host, size_t hostlen, uint32_t port) {
common_hal_socketpool_socket_connect(self->sock, host, hostlen, port);
STATIC void do_handshake(ssl_sslsocket_obj_t *self) {
int ret;
while ((ret = mbedtls_ssl_handshake(&self->ssl)) != 0) {
if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
@ -326,7 +325,7 @@ cleanup:
mbedtls_entropy_free(&self->entropy);
if (ret == MBEDTLS_ERR_SSL_ALLOC_FAILED) {
mp_raise_OSError(MP_ENOMEM);
mp_raise_type(&mp_type_MemoryError);
} else if (ret == MBEDTLS_ERR_PK_BAD_INPUT_DATA) {
mp_raise_ValueError(MP_ERROR_TEXT("invalid key"));
} else if (ret == MBEDTLS_ERR_X509_BAD_INPUT_DATA) {
@ -336,6 +335,11 @@ cleanup:
}
}
void common_hal_ssl_sslsocket_connect(ssl_sslsocket_obj_t *self, const char *host, size_t hostlen, uint32_t port) {
common_hal_socketpool_socket_connect(self->sock, host, hostlen, port);
do_handshake(self);
}
bool common_hal_ssl_sslsocket_get_closed(ssl_sslsocket_obj_t *self) {
return self->closed;
}