Merge remote-tracking branch 'origin/main' into main

This commit is contained in:
Hosted Weblate 2021-02-02 19:35:21 +01:00
commit 32c4610957
15 changed files with 896 additions and 386 deletions

View File

@ -629,6 +629,10 @@ msgstr ""
msgid "Cannot reset into bootloader because no bootloader is present." msgid "Cannot reset into bootloader because no bootloader is present."
msgstr "" msgstr ""
#: ports/esp32s2/common-hal/socketpool/Socket.c
msgid "Cannot set socket options"
msgstr ""
#: shared-bindings/digitalio/DigitalInOut.c #: shared-bindings/digitalio/DigitalInOut.c
msgid "Cannot set value when direction is input." msgid "Cannot set value when direction is input."
msgstr "" msgstr ""
@ -865,7 +869,7 @@ msgstr ""
msgid "Error in regex" msgid "Error in regex"
msgstr "" msgstr ""
#: shared-bindings/socketpool/Socket.c #: shared-bindings/socketpool/Socket.c shared-bindings/ssl/SSLSocket.c
msgid "Error: Failure to bind" msgid "Error: Failure to bind"
msgstr "" msgstr ""
@ -924,7 +928,7 @@ msgstr ""
msgid "FFT is implemented for linear arrays only" msgid "FFT is implemented for linear arrays only"
msgstr "" msgstr ""
#: ports/esp32s2/common-hal/socketpool/Socket.c #: ports/esp32s2/common-hal/ssl/SSLSocket.c
msgid "Failed SSL handshake" msgid "Failed SSL handshake"
msgstr "" msgstr ""
@ -1295,7 +1299,7 @@ msgstr ""
msgid "Invalid size" msgid "Invalid size"
msgstr "" msgstr ""
#: ports/esp32s2/common-hal/socketpool/Socket.c #: ports/esp32s2/common-hal/ssl/SSLContext.c
msgid "Invalid socket for TLS" msgid "Invalid socket for TLS"
msgstr "" msgstr ""
@ -1303,10 +1307,6 @@ msgstr ""
msgid "Invalid state" msgid "Invalid state"
msgstr "" msgstr ""
#: ports/esp32s2/common-hal/socketpool/Socket.c
msgid "Invalid use of TLS Socket"
msgstr ""
#: shared-bindings/audiomixer/Mixer.c #: shared-bindings/audiomixer/Mixer.c
msgid "Invalid voice" msgid "Invalid voice"
msgstr "" msgstr ""
@ -1323,10 +1323,6 @@ msgstr ""
msgid "Invalid word/bit length" msgid "Invalid word/bit length"
msgstr "" msgstr ""
#: ports/esp32s2/common-hal/socketpool/Socket.c
msgid "Issue setting SO_REUSEADDR"
msgstr ""
#: shared-bindings/aesio/aes.c #: shared-bindings/aesio/aes.c
msgid "Key must be 16, 24, or 32 bytes long" msgid "Key must be 16, 24, or 32 bytes long"
msgstr "" msgstr ""
@ -1647,7 +1643,7 @@ msgstr ""
msgid "Out of memory" msgid "Out of memory"
msgstr "" msgstr ""
#: ports/esp32s2/common-hal/socketpool/Socket.c #: ports/esp32s2/common-hal/socketpool/SocketPool.c
msgid "Out of sockets" msgid "Out of sockets"
msgstr "" msgstr ""
@ -2167,7 +2163,7 @@ msgstr ""
msgid "Unexpected nrfx uuid type" msgid "Unexpected nrfx uuid type"
msgstr "" msgstr ""
#: ports/esp32s2/common-hal/socketpool/Socket.c #: ports/esp32s2/common-hal/ssl/SSLSocket.c
#, c-format #, c-format
msgid "Unhandled ESP TLS error %d %d %x %d" msgid "Unhandled ESP TLS error %d %d %x %d"
msgstr "" msgstr ""
@ -2466,7 +2462,7 @@ msgstr ""
msgid "buffer too small" msgid "buffer too small"
msgstr "" msgstr ""
#: shared-bindings/socketpool/Socket.c #: shared-bindings/socketpool/Socket.c shared-bindings/ssl/SSLSocket.c
msgid "buffer too small for requested bytes" msgid "buffer too small for requested bytes"
msgstr "" msgstr ""

View File

@ -3,7 +3,6 @@
* *
* The MIT License (MIT) * The MIT License (MIT)
* *
* Copyright (c) 2020 Scott Shawcroft for Adafruit Industries
* Copyright (c) 2020 Lucian Copeland for Adafruit Industries * Copyright (c) 2020 Lucian Copeland for Adafruit Industries
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
@ -38,16 +37,15 @@
#include "components/lwip/lwip/src/include/lwip/sys.h" #include "components/lwip/lwip/src/include/lwip/sys.h"
#include "components/lwip/lwip/src/include/lwip/netdb.h" #include "components/lwip/lwip/src/include/lwip/netdb.h"
STATIC socketpool_socket_obj_t * open_socket_handles[CONFIG_LWIP_MAX_SOCKETS]; // 4 on the wrover/wroom STATIC socketpool_socket_obj_t * open_socket_handles[CONFIG_LWIP_MAX_SOCKETS];
void socket_reset(void) { void socket_reset(void) {
for (size_t i = 0; i < MP_ARRAY_SIZE(open_socket_handles); i++) { for (size_t i = 0; i < MP_ARRAY_SIZE(open_socket_handles); i++) {
if (open_socket_handles[i]) { if (open_socket_handles[i]) {
if (open_socket_handles[i]->num > 0) { if (open_socket_handles[i]->num > 0) {
// Close automatically clears socket handle
common_hal_socketpool_socket_close(open_socket_handles[i]); common_hal_socketpool_socket_close(open_socket_handles[i]);
open_socket_handles[i] = NULL;
} else { } else {
// accidentally got a TCP socket in here, or something.
open_socket_handles[i] = NULL; open_socket_handles[i] = NULL;
} }
} }
@ -64,59 +62,6 @@ bool register_open_socket(socketpool_socket_obj_t* self) {
return false; return false;
} }
STATIC void _lazy_init_LWIP(socketpool_socket_obj_t* self) {
if (self->num != -1) {
return; //safe to call on existing socket
}
if (self->tls != NULL) {
mp_raise_RuntimeError(translate("Invalid use of TLS Socket"));
}
int socknum = -1;
socknum = lwip_socket(self->family, self->type, self->ipproto);
if (socknum < 0 || !register_open_socket(self)) {
mp_raise_RuntimeError(translate("Out of sockets"));
}
self->num = socknum;
lwip_fcntl(socknum, F_SETFL, O_NONBLOCK);
}
STATIC void _lazy_init_TLS(socketpool_socket_obj_t* self) {
if (self->type != SOCK_STREAM || self->num != -1) {
mp_raise_RuntimeError(translate("Invalid socket for TLS"));
}
esp_tls_t* tls_handle = esp_tls_init();
if (tls_handle == NULL) {
mp_raise_espidf_MemoryError();
}
self->tls = tls_handle;
}
void common_hal_socketpool_socket_settimeout(socketpool_socket_obj_t* self, mp_uint_t timeout_ms) {
self->timeout_ms = timeout_ms;
}
bool common_hal_socketpool_socket_bind(socketpool_socket_obj_t* self,
const char* host, size_t hostlen, uint8_t port) {
_lazy_init_LWIP(self);
struct sockaddr_in bind_addr;
bind_addr.sin_addr.s_addr = inet_addr(host);
bind_addr.sin_family = AF_INET;
bind_addr.sin_port = htons(port);
int opt = 1;
int err = lwip_setsockopt(self->num, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
if (err != 0) {
mp_raise_RuntimeError(translate("Issue setting SO_REUSEADDR"));
}
int result = lwip_bind(self->num, (struct sockaddr *)&bind_addr, sizeof(bind_addr)) == 0;
return result;
}
bool common_hal_socketpool_socket_listen(socketpool_socket_obj_t* self, int backlog) {
return lwip_listen(self->num, backlog) == 0;
}
socketpool_socket_obj_t* common_hal_socketpool_socket_accept(socketpool_socket_obj_t* self, socketpool_socket_obj_t* common_hal_socketpool_socket_accept(socketpool_socket_obj_t* self,
uint8_t* ip, uint *port) { uint8_t* ip, uint *port) {
struct sockaddr_in accept_addr; struct sockaddr_in accept_addr;
@ -125,12 +70,6 @@ socketpool_socket_obj_t* common_hal_socketpool_socket_accept(socketpool_socket_o
bool timed_out = false; bool timed_out = false;
uint64_t start_ticks = supervisor_ticks_ms64(); uint64_t start_ticks = supervisor_ticks_ms64();
if (self->timeout_ms != (uint)-1) {
mp_printf(&mp_plat_print, "will timeout");
} else {
mp_printf(&mp_plat_print, "won't timeout");
}
// Allow timeouts and interrupts // Allow timeouts and interrupts
while (newsoc == -1 && while (newsoc == -1 &&
!timed_out && !timed_out &&
@ -140,7 +79,7 @@ socketpool_socket_obj_t* common_hal_socketpool_socket_accept(socketpool_socket_o
} }
RUN_BACKGROUND_TASKS; RUN_BACKGROUND_TASKS;
newsoc = lwip_accept(self->num, (struct sockaddr *)&accept_addr, &socklen); newsoc = lwip_accept(self->num, (struct sockaddr *)&accept_addr, &socklen);
// In non-blocking mode, fail instead of looping // In non-blocking mode, fail instead of timing out
if (newsoc == -1 && self->timeout_ms == 0) { if (newsoc == -1 && self->timeout_ms == 0) {
mp_raise_OSError(MP_EAGAIN); mp_raise_OSError(MP_EAGAIN);
} }
@ -159,8 +98,6 @@ socketpool_socket_obj_t* common_hal_socketpool_socket_accept(socketpool_socket_o
socketpool_socket_obj_t *sock = m_new_obj_with_finaliser(socketpool_socket_obj_t); socketpool_socket_obj_t *sock = m_new_obj_with_finaliser(socketpool_socket_obj_t);
sock->base.type = &socketpool_socket_type; sock->base.type = &socketpool_socket_type;
sock->num = newsoc; sock->num = newsoc;
sock->tls = NULL;
sock->ssl_context = NULL;
sock->pool = self->pool; sock->pool = self->pool;
if (!register_open_socket(sock)) { if (!register_open_socket(sock)) {
@ -175,183 +112,98 @@ socketpool_socket_obj_t* common_hal_socketpool_socket_accept(socketpool_socket_o
} }
} }
bool common_hal_socketpool_socket_bind(socketpool_socket_obj_t* self,
const char* host, size_t hostlen, uint8_t port) {
struct sockaddr_in bind_addr;
bind_addr.sin_addr.s_addr = inet_addr(host);
bind_addr.sin_family = AF_INET;
bind_addr.sin_port = htons(port);
int opt = 1;
int err = lwip_setsockopt(self->num, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
if (err != 0) {
mp_raise_RuntimeError(translate("Cannot set socket options"));
}
int result = lwip_bind(self->num, (struct sockaddr *)&bind_addr, sizeof(bind_addr)) == 0;
return result;
}
void common_hal_socketpool_socket_close(socketpool_socket_obj_t* self) {
self->connected = false;
if (self->num >= 0) {
lwip_shutdown(self->num, 0);
lwip_close(self->num);
self->num = -1;
}
// Remove socket record
for (size_t i = 0; i < MP_ARRAY_SIZE(open_socket_handles); i++) {
if (open_socket_handles[i] == self) {
open_socket_handles[i] = NULL;
}
}
}
bool common_hal_socketpool_socket_connect(socketpool_socket_obj_t* self, bool common_hal_socketpool_socket_connect(socketpool_socket_obj_t* self,
const char* host, mp_uint_t hostlen, mp_int_t port) { const char* host, mp_uint_t hostlen, mp_int_t port) {
// For simplicity we use esp_tls for all TCP connections. If it's not SSL, ssl_context will be
// NULL and should still work. This makes regular TCP connections more memory expensive but TLS
// should become more and more common. Therefore, we optimize for the TLS case.
// Todo: move to SSL Wrapper and add lwip_connect()
_lazy_init_TLS(self);
esp_tls_cfg_t* tls_config = NULL;
if (self->ssl_context != NULL) {
tls_config = &self->ssl_context->ssl_config;
}
int result = esp_tls_conn_new_sync(host, hostlen, port, tls_config, self->tls);
self->connected = result >= 0;
if (result < 0) {
int esp_tls_code;
int flags;
esp_err_t err = esp_tls_get_and_clear_last_error(self->tls->error_handle, &esp_tls_code, &flags);
if (err == ESP_ERR_MBEDTLS_SSL_SETUP_FAILED) {
mp_raise_espidf_MemoryError();
} else if (ESP_ERR_MBEDTLS_SSL_HANDSHAKE_FAILED) {
mp_raise_OSError_msg_varg(translate("Failed SSL handshake"));
} else {
mp_raise_OSError_msg_varg(translate("Unhandled ESP TLS error %d %d %x %d"), esp_tls_code, flags, err, result);
}
} else {
// Connection successful, set the timeout on the underlying socket. We can't rely on the IDF
// to do it because the config structure is only used for TLS connections. Generally, we
// shouldn't hit this timeout because we try to only read available data. However, there is
// always a chance that we try to read something that is used internally.
int fd;
esp_tls_get_conn_sockfd(self->tls, &fd);
struct timeval tv;
tv.tv_sec = 2 * 60; // Two minutes
tv.tv_usec = 0;
setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
}
return self->connected;
}
bool common_hal_socketpool_socket_get_connected(socketpool_socket_obj_t* self) {
return self->connected;
}
mp_uint_t common_hal_socketpool_socket_send(socketpool_socket_obj_t* self, const uint8_t* buf, mp_uint_t len) {
int sent = -1;
if (self->num != -1) {
// LWIP Socket
// TODO: deal with potential failure/add timeout?
sent = lwip_send(self->num, buf, len, 0);
} else if (self->tls != NULL) {
// TLS Socket
sent = esp_tls_conn_write(self->tls, buf, len);
}
if (sent < 0) {
mp_raise_OSError(MP_ENOTCONN);
}
return sent;
}
mp_uint_t common_hal_socketpool_socket_recv_into(socketpool_socket_obj_t* self, const uint8_t* buf, mp_uint_t len) {
int received = 0;
bool timed_out = false;
if (self->num != -1) {
// LWIP Socket
uint64_t start_ticks = supervisor_ticks_ms64();
received = -1;
while (received == -1 &&
!timed_out &&
!mp_hal_is_interrupted()) {
if (self->timeout_ms != (uint)-1 && self->timeout_ms != 0) {
timed_out = supervisor_ticks_ms64() - start_ticks >= self->timeout_ms;
}
RUN_BACKGROUND_TASKS;
received = lwip_recv(self->num, (void*) buf, len - 1, 0);
// In non-blocking mode, fail instead of looping
if (received == -1 && self->timeout_ms == 0) {
mp_raise_OSError(MP_EAGAIN);
}
}
} else if (self->tls != NULL) {
// TLS Socket
int status = 0;
uint64_t start_ticks = supervisor_ticks_ms64();
int sockfd;
esp_err_t err = esp_tls_get_conn_sockfd(self->tls, &sockfd);
if (err != ESP_OK) {
mp_raise_OSError(MP_EBADF);
}
while (received == 0 &&
status >= 0 &&
!timed_out &&
!mp_hal_is_interrupted()) {
if (self->timeout_ms != (uint)-1) {
timed_out = self->timeout_ms == 0 || supervisor_ticks_ms64() - start_ticks >= self->timeout_ms;
}
RUN_BACKGROUND_TASKS;
size_t available = esp_tls_get_bytes_avail(self->tls);
if (available == 0) {
// This reads the raw socket buffer and is used for non-TLS connections
// and between encrypted TLS blocks.
status = lwip_ioctl(sockfd, FIONREAD, &available);
}
size_t remaining = len - received;
if (available > remaining) {
available = remaining;
}
if (available > 0) {
status = esp_tls_conn_read(self->tls, (void*) buf + received, available);
if (status == 0) {
// Reading zero when something is available indicates a closed
// connection. (The available bytes could have been TLS internal.)
break;
}
if (status > 0) {
received += status;
}
}
}
} else {
// Socket does not have a valid descriptor of either type
mp_raise_OSError(MP_EBADF);
}
if (timed_out) {
mp_raise_OSError(ETIMEDOUT);
}
return received;
}
mp_uint_t common_hal_socketpool_socket_sendto(socketpool_socket_obj_t* self,
const char* host, size_t hostlen, uint8_t port, const uint8_t* buf, mp_uint_t len) {
_lazy_init_LWIP(self);
// Get the IP address string
const struct addrinfo hints = { const struct addrinfo hints = {
.ai_family = AF_INET, .ai_family = AF_INET,
.ai_socktype = SOCK_STREAM, .ai_socktype = SOCK_STREAM,
}; };
struct addrinfo *result; struct addrinfo *result_i;
int error = lwip_getaddrinfo(host, NULL, &hints, &result); int error = lwip_getaddrinfo(host, NULL, &hints, &result_i);
if (error != 0 || result == NULL) { if (error != 0 || result_i == NULL) {
return 0; mp_raise_OSError(EHOSTUNREACH);
} }
// Set parameters // Set parameters
struct sockaddr_in dest_addr; struct sockaddr_in dest_addr;
#pragma GCC diagnostic push #pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-align" #pragma GCC diagnostic ignored "-Wcast-align"
dest_addr.sin_addr.s_addr = ((struct sockaddr_in *)result->ai_addr)->sin_addr.s_addr; dest_addr.sin_addr.s_addr = ((struct sockaddr_in *)result_i->ai_addr)->sin_addr.s_addr;
#pragma GCC diagnostic pop #pragma GCC diagnostic pop
freeaddrinfo(result); freeaddrinfo(result_i);
dest_addr.sin_family = AF_INET; dest_addr.sin_family = AF_INET;
dest_addr.sin_port = htons(port); dest_addr.sin_port = htons(port);
int bytes_sent = lwip_sendto(self->num, buf, len, 0, (struct sockaddr *)&dest_addr, sizeof(dest_addr)); // Replace above with function call -----
if (bytes_sent < 0) {
mp_raise_BrokenPipeError(); // Switch to blocking mode for this one call
return 0; int opts;
opts = lwip_fcntl(self->num,F_GETFL,0);
opts = opts & (~O_NONBLOCK);
lwip_fcntl(self->num, F_SETFL, opts);
int result = -1;
result = lwip_connect(self->num, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr_in));
// Switch back once complete
opts = opts | O_NONBLOCK;
lwip_fcntl(self->num, F_SETFL, opts);
if (result) {
self->connected = true;
return true;
} else {
mp_raise_OSError(errno);
} }
return bytes_sent; }
bool common_hal_socketpool_socket_get_closed(socketpool_socket_obj_t* self) {
return self->num < 0;
}
bool common_hal_socketpool_socket_get_connected(socketpool_socket_obj_t* self) {
return self->connected;
}
bool common_hal_socketpool_socket_listen(socketpool_socket_obj_t* self, int backlog) {
return lwip_listen(self->num, backlog) == 0;
} }
mp_uint_t common_hal_socketpool_socket_recvfrom_into(socketpool_socket_obj_t* self, mp_uint_t common_hal_socketpool_socket_recvfrom_into(socketpool_socket_obj_t* self,
uint8_t* buf, mp_uint_t len, uint8_t* ip, uint *port) { uint8_t* buf, mp_uint_t len, uint8_t* ip, uint *port) {
_lazy_init_LWIP(self);
struct sockaddr_in source_addr; struct sockaddr_in source_addr;
socklen_t socklen = sizeof(source_addr); socklen_t socklen = sizeof(source_addr);
@ -362,7 +214,7 @@ mp_uint_t common_hal_socketpool_socket_recvfrom_into(socketpool_socket_obj_t* se
while (received == -1 && while (received == -1 &&
!timed_out && !timed_out &&
!mp_hal_is_interrupted()) { !mp_hal_is_interrupted()) {
if (self->timeout_ms != (uint)-1 && self->timeout_ms != 0) { if (self->timeout_ms != (uint)-1 && self->timeout_ms != 0) {
timed_out = supervisor_ticks_ms64() - start_ticks >= self->timeout_ms; timed_out = supervisor_ticks_ms64() - start_ticks >= self->timeout_ms;
} }
RUN_BACKGROUND_TASKS; RUN_BACKGROUND_TASKS;
@ -389,24 +241,87 @@ mp_uint_t common_hal_socketpool_socket_recvfrom_into(socketpool_socket_obj_t* se
return received; return received;
} }
void common_hal_socketpool_socket_close(socketpool_socket_obj_t* self) { mp_uint_t common_hal_socketpool_socket_recv_into(socketpool_socket_obj_t* self, const uint8_t* buf, mp_uint_t len) {
self->connected = false; int received = 0;
if (self->tls != NULL) { bool timed_out = false;
esp_tls_conn_destroy(self->tls);
self->tls = NULL; if (self->num != -1) {
// LWIP Socket
uint64_t start_ticks = supervisor_ticks_ms64();
received = -1;
while (received == -1 &&
!timed_out &&
!mp_hal_is_interrupted()) {
if (self->timeout_ms != (uint)-1 && self->timeout_ms != 0) {
timed_out = supervisor_ticks_ms64() - start_ticks >= self->timeout_ms;
}
RUN_BACKGROUND_TASKS;
received = lwip_recv(self->num, (void*) buf, len - 1, 0);
// In non-blocking mode, fail instead of looping
if (received == -1 && self->timeout_ms == 0) {
mp_raise_OSError(MP_EAGAIN);
}
}
} else {
mp_raise_OSError(MP_EBADF);
} }
if (self->num >= 0) {
lwip_shutdown(self->num, 0); if (timed_out) {
lwip_close(self->num); mp_raise_OSError(ETIMEDOUT);
self->num = -1;
} }
return received;
} }
bool common_hal_socketpool_socket_get_closed(socketpool_socket_obj_t* self) { mp_uint_t common_hal_socketpool_socket_send(socketpool_socket_obj_t* self, const uint8_t* buf, mp_uint_t len) {
return self->tls == NULL && self->num < 0; int sent = -1;
if (self->num != -1) {
// LWIP Socket
// TODO: deal with potential failure/add timeout?
sent = lwip_send(self->num, buf, len, 0);
} else {
mp_raise_OSError(MP_EBADF);
}
if (sent < 0) {
mp_raise_OSError(errno);
}
return sent;
} }
mp_uint_t common_hal_socketpool_socket_sendto(socketpool_socket_obj_t* self,
const char* host, size_t hostlen, uint8_t port, const uint8_t* buf, mp_uint_t len) {
mp_uint_t common_hal_socketpool_socket_get_hash(socketpool_socket_obj_t* self) { // Set parameters
return self->num; const struct addrinfo hints = {
.ai_family = AF_INET,
.ai_socktype = SOCK_STREAM,
};
struct addrinfo *result_i;
int error = lwip_getaddrinfo(host, NULL, &hints, &result_i);
if (error != 0 || result_i == NULL) {
mp_raise_OSError(EHOSTUNREACH);
}
// Set parameters
struct sockaddr_in dest_addr;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-align"
dest_addr.sin_addr.s_addr = ((struct sockaddr_in *)result_i->ai_addr)->sin_addr.s_addr;
#pragma GCC diagnostic pop
freeaddrinfo(result_i);
dest_addr.sin_family = AF_INET;
dest_addr.sin_port = htons(port);
int bytes_sent = lwip_sendto(self->num, buf, len, 0, (struct sockaddr *)&dest_addr, sizeof(dest_addr));
if (bytes_sent < 0) {
mp_raise_BrokenPipeError();
return 0;
}
return bytes_sent;
}
void common_hal_socketpool_socket_settimeout(socketpool_socket_obj_t* self, mp_uint_t timeout_ms) {
self->timeout_ms = timeout_ms;
} }

View File

@ -41,8 +41,6 @@ typedef struct {
int family; int family;
int ipproto; int ipproto;
bool connected; bool connected;
esp_tls_t* tls;
ssl_sslcontext_obj_t* ssl_context;
socketpool_socketpool_obj_t* pool; socketpool_socketpool_obj_t* pool;
mp_uint_t timeout_ms; mp_uint_t timeout_ms;
} socketpool_socket_obj_t; } socketpool_socket_obj_t;

View File

@ -25,6 +25,7 @@
*/ */
#include "shared-bindings/socketpool/SocketPool.h" #include "shared-bindings/socketpool/SocketPool.h"
#include "common-hal/socketpool/Socket.h"
#include "py/runtime.h" #include "py/runtime.h"
#include "shared-bindings/wifi/__init__.h" #include "shared-bindings/wifi/__init__.h"
@ -65,23 +66,23 @@ socketpool_socket_obj_t* common_hal_socketpool_socket(socketpool_socketpool_obj_
mp_raise_NotImplementedError(translate("Only IPv4 sockets supported")); mp_raise_NotImplementedError(translate("Only IPv4 sockets supported"));
} }
// Consider LWIP and MbedTLS "variant" sockets to be incompatible (for now)
// The variant of the socket is determined by whether the socket is wrapped
// by SSL. If no TLS handle is set in sslcontext_wrap_socket, the first call
// of bind() or connect() will create a LWIP socket with a corresponding
// socketnum.
// TODO: move MbedTLS to its own duplicate Socket or Server API, maybe?
socketpool_socket_obj_t *sock = m_new_obj_with_finaliser(socketpool_socket_obj_t); socketpool_socket_obj_t *sock = m_new_obj_with_finaliser(socketpool_socket_obj_t);
sock->base.type = &socketpool_socket_type; sock->base.type = &socketpool_socket_type;
sock->num = -1;
sock->type = socket_type; sock->type = socket_type;
sock->family = addr_family; sock->family = addr_family;
sock->ipproto = ipproto; sock->ipproto = ipproto;
sock->pool = self;
sock->timeout_ms = (uint)-1; sock->timeout_ms = (uint)-1;
sock->tls = NULL; // Create LWIP socket
sock->ssl_context = NULL; int socknum = -1;
sock->pool = self; socknum = lwip_socket(sock->family, sock->type, sock->ipproto);
if (socknum < 0 || !register_open_socket(sock)) {
mp_raise_RuntimeError(translate("Out of sockets"));
}
sock->num = socknum;
// Sockets should be nonblocking in most cases
lwip_fcntl(socknum, F_SETFL, O_NONBLOCK);
return sock; return sock;
} }

View File

@ -25,6 +25,9 @@
*/ */
#include "shared-bindings/ssl/SSLContext.h" #include "shared-bindings/ssl/SSLContext.h"
#include "shared-bindings/ssl/SSLSocket.h"
#include "bindings/espidf/__init__.h"
#include "py/runtime.h" #include "py/runtime.h"
@ -32,10 +35,26 @@ void common_hal_ssl_sslcontext_construct(ssl_sslcontext_obj_t* self) {
} }
socketpool_socket_obj_t* common_hal_ssl_sslcontext_wrap_socket(ssl_sslcontext_obj_t* self, ssl_sslsocket_obj_t* common_hal_ssl_sslcontext_wrap_socket(ssl_sslcontext_obj_t* self,
socketpool_socket_obj_t* socket, bool server_side, const char* server_hostname) { socketpool_socket_obj_t* socket, bool server_side, const char* server_hostname) {
socket->ssl_context = self; if (socket->type != SOCK_STREAM) {
mp_raise_RuntimeError(translate("Invalid socket for TLS"));
}
ssl_sslsocket_obj_t *sock = m_new_obj_with_finaliser(ssl_sslsocket_obj_t);
sock->base.type = &ssl_sslsocket_type;
sock->ssl_context = self;
sock->sock = socket;
esp_tls_t* tls_handle = esp_tls_init();
if (tls_handle == NULL) {
mp_raise_espidf_MemoryError();
}
sock->tls = tls_handle;
// TODO: do something with the original socket? Don't call a close on the internal LWIP.
// Should we store server hostname on the socket in case connect is called with an ip? // Should we store server hostname on the socket in case connect is called with an ip?
return socket; return sock;
} }

View File

@ -0,0 +1,176 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2020 Scott Shawcroft for Adafruit Industries
* Copyright (c) 2021 Lucian Copeland for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "shared-bindings/ssl/SSLSocket.h"
#include "shared-bindings/socketpool/Socket.h"
#include "shared-bindings/ssl/SSLContext.h"
#include "bindings/espidf/__init__.h"
#include "lib/utils/interrupt_char.h"
#include "py/mperrno.h"
#include "py/runtime.h"
#include "supervisor/shared/tick.h"
void common_hal_ssl_sslsocket_settimeout(ssl_sslsocket_obj_t* self, mp_uint_t timeout_ms) {
self->sock->timeout_ms = timeout_ms;
}
ssl_sslsocket_obj_t* common_hal_ssl_sslsocket_accept(ssl_sslsocket_obj_t* self,
uint8_t* ip, uint *port) {
socketpool_socket_obj_t * sock = common_hal_socketpool_socket_accept(self->sock, ip, port);
ssl_sslsocket_obj_t * sslsock = common_hal_ssl_sslcontext_wrap_socket(self->ssl_context, sock, false, NULL);
return sslsock;
}
bool common_hal_ssl_sslsocket_bind(ssl_sslsocket_obj_t* self,
const char* host, size_t hostlen, uint8_t port) {
return common_hal_socketpool_socket_bind(self->sock, host, hostlen, port);
}
void common_hal_ssl_sslsocket_close(ssl_sslsocket_obj_t* self) {
common_hal_socketpool_socket_close(self->sock);
esp_tls_conn_destroy(self->tls);
self->tls = NULL;
}
bool common_hal_ssl_sslsocket_connect(ssl_sslsocket_obj_t* self,
const char* host, mp_uint_t hostlen, mp_int_t port) {
esp_tls_cfg_t* tls_config = NULL;
tls_config = &self->ssl_context->ssl_config;
int result = esp_tls_conn_new_sync(host, hostlen, port, tls_config, self->tls);
self->sock->connected = result >= 0;
if (result < 0) {
int esp_tls_code;
int flags;
esp_err_t err = esp_tls_get_and_clear_last_error(self->tls->error_handle, &esp_tls_code, &flags);
if (err == ESP_ERR_MBEDTLS_SSL_SETUP_FAILED) {
mp_raise_espidf_MemoryError();
} else if (ESP_ERR_MBEDTLS_SSL_HANDSHAKE_FAILED) {
mp_raise_OSError_msg_varg(translate("Failed SSL handshake"));
} else {
mp_raise_OSError_msg_varg(translate("Unhandled ESP TLS error %d %d %x %d"), esp_tls_code, flags, err, result);
}
} else {
// Connection successful, set the timeout on the underlying socket. We can't rely on the IDF
// to do it because the config structure is only used for TLS connections. Generally, we
// shouldn't hit this timeout because we try to only read available data. However, there is
// always a chance that we try to read something that is used internally.
int fd;
esp_tls_get_conn_sockfd(self->tls, &fd);
struct timeval tv;
tv.tv_sec = 2 * 60; // Two minutes
tv.tv_usec = 0;
setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
}
return self->sock->connected;
}
bool common_hal_ssl_sslsocket_get_closed(ssl_sslsocket_obj_t* self) {
return self->tls == NULL && self->sock->num < 0;
}
bool common_hal_ssl_sslsocket_get_connected(ssl_sslsocket_obj_t* self) {
return self->sock->connected;
}
bool common_hal_ssl_sslsocket_listen(ssl_sslsocket_obj_t* self, int backlog) {
return common_hal_socketpool_socket_listen(self->sock, backlog);
}
mp_uint_t common_hal_ssl_sslsocket_recv_into(ssl_sslsocket_obj_t* self, const uint8_t* buf, mp_uint_t len) {
int received = 0;
bool timed_out = false;
int status = 0;
uint64_t start_ticks = supervisor_ticks_ms64();
int sockfd;
esp_err_t err = esp_tls_get_conn_sockfd(self->tls, &sockfd);
if (err != ESP_OK) {
mp_raise_OSError(MP_EBADF);
}
while (received == 0 &&
status >= 0 &&
!timed_out &&
!mp_hal_is_interrupted()) {
if (self->sock->timeout_ms != (uint)-1 && self->sock->timeout_ms != 0) {
timed_out = self->sock->timeout_ms == 0 || supervisor_ticks_ms64() - start_ticks >= self->sock->timeout_ms;
}
RUN_BACKGROUND_TASKS;
size_t available = esp_tls_get_bytes_avail(self->tls);
if (available == 0) {
// This reads the raw socket buffer and is used for non-TLS connections
// and between encrypted TLS blocks.
status = lwip_ioctl(sockfd, FIONREAD, &available);
}
size_t remaining = len - received;
if (available > remaining) {
available = remaining;
}
if (available > 0) {
status = esp_tls_conn_read(self->tls, (void*) buf + received, available);
if (status == 0) {
// Reading zero when something is available indicates a closed
// connection. (The available bytes could have been TLS internal.)
break;
}
if (status > 0) {
received += status;
}
}
// In non-blocking mode, fail instead of timing out
if (received==0 && self->sock->timeout_ms == 0) {
mp_raise_OSError(MP_EAGAIN);
}
}
if (timed_out) {
mp_raise_OSError(ETIMEDOUT);
}
return received;
}
mp_uint_t common_hal_ssl_sslsocket_send(ssl_sslsocket_obj_t* self, const uint8_t* buf, mp_uint_t len) {
int sent = -1;
sent = esp_tls_conn_write(self->tls, buf, len);
if (sent < 0) {
int esp_tls_code;
int flags;
esp_err_t err = esp_tls_get_and_clear_last_error(self->tls->error_handle, &esp_tls_code, &flags);
if (err == ESP_ERR_MBEDTLS_SSL_SETUP_FAILED) {
mp_raise_espidf_MemoryError();
} else if (ESP_ERR_MBEDTLS_SSL_HANDSHAKE_FAILED) {
mp_raise_OSError_msg_varg(translate("Failed SSL handshake"));
} else {
mp_raise_OSError_msg_varg(translate("Unhandled ESP TLS error %d %d %x %d"), esp_tls_code, flags, err, sent);
}
}
return sent;
}

View File

@ -0,0 +1,44 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2021 Lucian Copeland for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef MICROPY_INCLUDED_ESP32S2_COMMON_HAL_SSL_SSLSOCKET_H
#define MICROPY_INCLUDED_ESP32S2_COMMON_HAL_SSL_SSLSOCKET_H
#include "py/obj.h"
#include "common-hal/ssl/SSLContext.h"
#include "common-hal/socketpool/Socket.h"
#include "components/esp-tls/esp_tls.h"
typedef struct {
mp_obj_base_t base;
socketpool_socket_obj_t * sock;
esp_tls_t* tls;
ssl_sslcontext_obj_t* ssl_context;
} ssl_sslsocket_obj_t;
#endif // MICROPY_INCLUDED_ESP32S2_COMMON_HAL_SSL_SSLSOCKET_H

View File

@ -388,6 +388,7 @@ SRC_COMMON_HAL_ALL = \
socketpool/Socket.c \ socketpool/Socket.c \
ssl/__init__.c \ ssl/__init__.c \
ssl/SSLContext.c \ ssl/SSLContext.c \
ssl/SSLSocket.c \
supervisor/Runtime.c \ supervisor/Runtime.c \
supervisor/__init__.c \ supervisor/__init__.c \
watchdog/WatchDogMode.c \ watchdog/WatchDogMode.c \

View File

@ -3,8 +3,8 @@
* *
* The MIT License (MIT) * The MIT License (MIT)
* *
* SPDX-FileCopyrightText: Copyright (c) 2014 Damien P. George * Copyright (c) 2020 Scott Shawcroft for Adafruit Industries
* 2018 Nick Moore for Adafruit Industries * Copyright (c) 2021 Lucian Copeland for Adafruit Industries
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
@ -64,6 +64,25 @@ STATIC mp_obj_t socketpool_socket___exit__(size_t n_args, const mp_obj_t *args)
} }
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(socketpool_socket___exit___obj, 4, 4, socketpool_socket___exit__); STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(socketpool_socket___exit___obj, 4, 4, socketpool_socket___exit__);
//| def accept(self) -> Tuple[Socket, Tuple[str, int]]:
//| """Accept a connection on a listening socket of type SOCK_STREAM,
//| creating a new socket of type SOCK_STREAM.
//| Returns a tuple of (new_socket, remote_address)"""
//|
STATIC mp_obj_t socketpool_socket_accept(mp_obj_t self_in) {
socketpool_socket_obj_t *self = MP_OBJ_TO_PTR(self_in);
uint8_t ip[4];
uint port;
socketpool_socket_obj_t * sock = common_hal_socketpool_socket_accept(self, ip, &port);
mp_obj_t tuple_contents[2];
tuple_contents[0] = MP_OBJ_FROM_PTR(sock);
tuple_contents[1] = netutils_format_inet_addr(ip, port, NETUTILS_BIG);
return mp_obj_new_tuple(2, tuple_contents);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(socketpool_socket_accept_obj, socketpool_socket_accept);
//| def bind(self, address: Tuple[str, int]) -> None: //| def bind(self, address: Tuple[str, int]) -> None:
//| """Bind a socket to an address //| """Bind a socket to an address
//| //|
@ -89,41 +108,6 @@ STATIC mp_obj_t socketpool_socket_bind(mp_obj_t self_in, mp_obj_t addr_in) {
} }
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socketpool_socket_bind_obj, socketpool_socket_bind); STATIC MP_DEFINE_CONST_FUN_OBJ_2(socketpool_socket_bind_obj, socketpool_socket_bind);
//| def listen(self, backlog: int) -> None:
//| """Set socket to listen for incoming connections
//|
//| :param ~int backlog: length of backlog queue for waiting connetions"""
//| ...
//|
STATIC mp_obj_t socketpool_socket_listen(mp_obj_t self_in, mp_obj_t backlog_in) {
socketpool_socket_obj_t *self = MP_OBJ_TO_PTR(self_in);
int backlog = mp_obj_get_int(backlog_in);
common_hal_socketpool_socket_listen(self, backlog);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socketpool_socket_listen_obj, socketpool_socket_listen);
//| def accept(self) -> Tuple[Socket, Tuple[str, int]]:
//| """Accept a connection on a listening socket of type SOCK_STREAM,
//| creating a new socket of type SOCK_STREAM.
//| Returns a tuple of (new_socket, remote_address)"""
//|
STATIC mp_obj_t socketpool_socket_accept(mp_obj_t self_in) {
socketpool_socket_obj_t *self = MP_OBJ_TO_PTR(self_in);
uint8_t ip[4];
uint port;
socketpool_socket_obj_t * sock = common_hal_socketpool_socket_accept(self, ip, &port);
mp_obj_t tuple_contents[2];
tuple_contents[0] = MP_OBJ_FROM_PTR(sock);
tuple_contents[1] = netutils_format_inet_addr(ip, port, NETUTILS_BIG);
return mp_obj_new_tuple(2, tuple_contents);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(socketpool_socket_accept_obj, socketpool_socket_accept);
//| def close(self) -> None: //| def close(self) -> None:
//| """Closes this Socket and makes its resources available to its SocketPool.""" //| """Closes this Socket and makes its resources available to its SocketPool."""
//| //|
@ -159,31 +143,47 @@ STATIC mp_obj_t socketpool_socket_connect(mp_obj_t self_in, mp_obj_t addr_in) {
} }
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socketpool_socket_connect_obj, socketpool_socket_connect); STATIC MP_DEFINE_CONST_FUN_OBJ_2(socketpool_socket_connect_obj, socketpool_socket_connect);
//| def send(self, bytes: ReadableBuffer) -> int: //| def listen(self, backlog: int) -> None:
//| """Send some bytes to the connected remote address. //| """Set socket to listen for incoming connections
//| Suits sockets of type SOCK_STREAM
//| //|
//| :param ~bytes bytes: some bytes to send""" //| :param ~int backlog: length of backlog queue for waiting connetions"""
//| ... //| ...
//| //|
STATIC mp_obj_t socketpool_socket_send(mp_obj_t self_in, mp_obj_t buf_in) { STATIC mp_obj_t socketpool_socket_listen(mp_obj_t self_in, mp_obj_t backlog_in) {
socketpool_socket_obj_t *self = MP_OBJ_TO_PTR(self_in); socketpool_socket_obj_t *self = MP_OBJ_TO_PTR(self_in);
if (common_hal_socketpool_socket_get_closed(self)) {
// Bad file number. int backlog = mp_obj_get_int(backlog_in);
mp_raise_OSError(MP_EBADF);
} common_hal_socketpool_socket_listen(self, backlog);
if (!common_hal_socketpool_socket_get_connected(self)) { return mp_const_none;
mp_raise_BrokenPipeError();
}
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_READ);
mp_int_t ret = common_hal_socketpool_socket_send(self, bufinfo.buf, bufinfo.len);
if (ret == -1) {
mp_raise_BrokenPipeError();
}
return mp_obj_new_int_from_uint(ret);
} }
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socketpool_socket_send_obj, socketpool_socket_send); STATIC MP_DEFINE_CONST_FUN_OBJ_2(socketpool_socket_listen_obj, socketpool_socket_listen);
//| def recvfrom_into(self, buffer: WriteableBuffer) -> Tuple[int, Tuple[str, int]]:
//| """Reads some bytes from a remote address.
//|
//| Returns a tuple containing
//| * the number of bytes received into the given buffer
//| * a remote_address, which is a tuple of ip address and port number
//|
//| :param object buffer: buffer to read into"""
//| ...
//|
STATIC mp_obj_t socketpool_socket_recvfrom_into(mp_obj_t self_in, mp_obj_t data_in) {
socketpool_socket_obj_t *self = MP_OBJ_TO_PTR(self_in);
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(data_in, &bufinfo, MP_BUFFER_WRITE);
byte ip[4];
mp_uint_t port;
mp_int_t ret = common_hal_socketpool_socket_recvfrom_into(self,
(byte*)bufinfo.buf, bufinfo.len, ip, &port);
mp_obj_t tuple_contents[2];
tuple_contents[0] = mp_obj_new_int_from_uint(ret);
tuple_contents[1] = netutils_format_inet_addr(ip, port, NETUTILS_BIG);
return mp_obj_new_tuple(2, tuple_contents);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socketpool_socket_recvfrom_into_obj, socketpool_socket_recvfrom_into);
//| def recv_into(self, buffer: WriteableBuffer, bufsize: int) -> int: //| def recv_into(self, buffer: WriteableBuffer, bufsize: int) -> int:
//| """Reads some bytes from the connected remote address, writing //| """Reads some bytes from the connected remote address, writing
@ -199,7 +199,6 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(socketpool_socket_send_obj, socketpool_socket_s
//| :param int bufsize: optionally, a maximum number of bytes to read.""" //| :param int bufsize: optionally, a maximum number of bytes to read."""
//| ... //| ...
//| //|
STATIC mp_obj_t socketpool_socket_recv_into(size_t n_args, const mp_obj_t *args) { STATIC mp_obj_t socketpool_socket_recv_into(size_t n_args, const mp_obj_t *args) {
socketpool_socket_obj_t *self = MP_OBJ_TO_PTR(args[0]); socketpool_socket_obj_t *self = MP_OBJ_TO_PTR(args[0]);
if (common_hal_socketpool_socket_get_closed(self)) { if (common_hal_socketpool_socket_get_closed(self)) {
@ -232,6 +231,32 @@ STATIC mp_obj_t socketpool_socket_recv_into(size_t n_args, const mp_obj_t *args)
} }
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(socketpool_socket_recv_into_obj, 2, 3, socketpool_socket_recv_into); STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(socketpool_socket_recv_into_obj, 2, 3, socketpool_socket_recv_into);
//| def send(self, bytes: ReadableBuffer) -> int:
//| """Send some bytes to the connected remote address.
//| Suits sockets of type SOCK_STREAM
//|
//| :param ~bytes bytes: some bytes to send"""
//| ...
//|
STATIC mp_obj_t socketpool_socket_send(mp_obj_t self_in, mp_obj_t buf_in) {
socketpool_socket_obj_t *self = MP_OBJ_TO_PTR(self_in);
if (common_hal_socketpool_socket_get_closed(self)) {
// Bad file number.
mp_raise_OSError(MP_EBADF);
}
if (!common_hal_socketpool_socket_get_connected(self)) {
mp_raise_BrokenPipeError();
}
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_READ);
mp_int_t ret = common_hal_socketpool_socket_send(self, bufinfo.buf, bufinfo.len);
if (ret == -1) {
mp_raise_BrokenPipeError();
}
return mp_obj_new_int_from_uint(ret);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socketpool_socket_send_obj, socketpool_socket_send);
//| def sendto(self, bytes: ReadableBuffer, address: Tuple[str, int]) -> int: //| def sendto(self, bytes: ReadableBuffer, address: Tuple[str, int]) -> int:
//| """Send some bytes to a specific address. //| """Send some bytes to a specific address.
//| Suits sockets of type SOCK_DGRAM //| Suits sockets of type SOCK_DGRAM
@ -240,7 +265,6 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(socketpool_socket_recv_into_obj, 2, 3
//| :param ~tuple address: tuple of (remote_address, remote_port)""" //| :param ~tuple address: tuple of (remote_address, remote_port)"""
//| ... //| ...
//| //|
STATIC mp_obj_t socketpool_socket_sendto(mp_obj_t self_in, mp_obj_t data_in, mp_obj_t addr_in) { STATIC mp_obj_t socketpool_socket_sendto(mp_obj_t self_in, mp_obj_t data_in, mp_obj_t addr_in) {
socketpool_socket_obj_t *self = MP_OBJ_TO_PTR(self_in); socketpool_socket_obj_t *self = MP_OBJ_TO_PTR(self_in);
@ -264,37 +288,28 @@ STATIC mp_obj_t socketpool_socket_sendto(mp_obj_t self_in, mp_obj_t data_in, mp_
} }
STATIC MP_DEFINE_CONST_FUN_OBJ_3(socketpool_socket_sendto_obj, socketpool_socket_sendto); STATIC MP_DEFINE_CONST_FUN_OBJ_3(socketpool_socket_sendto_obj, socketpool_socket_sendto);
//| def recvfrom_into(self, buffer: WriteableBuffer) -> Tuple[int, Tuple[str, int]]: //| def setblocking(self, flag: bool) -> Optional[int]:
//| """Reads some bytes from a remote address. //| """Set the blocking behaviour of this socket.
//| //|
//| Returns a tuple containing //| :param ~bool flag: False means non-blocking, True means block indefinitely."""
//| * the number of bytes received into the given buffer
//| * a remote_address, which is a tuple of ip address and port number
//|
//| :param object buffer: buffer to read into"""
//| ... //| ...
//| //|
STATIC mp_obj_t socketpool_socket_recvfrom_into(mp_obj_t self_in, mp_obj_t data_in) { // method socket.setblocking(flag)
STATIC mp_obj_t socketpool_socket_setblocking(mp_obj_t self_in, mp_obj_t blocking) {
socketpool_socket_obj_t *self = MP_OBJ_TO_PTR(self_in); socketpool_socket_obj_t *self = MP_OBJ_TO_PTR(self_in);
mp_buffer_info_t bufinfo; if (mp_obj_is_true(blocking)) {
mp_get_buffer_raise(data_in, &bufinfo, MP_BUFFER_WRITE); common_hal_socketpool_socket_settimeout(self, -1);
} else {
byte ip[4]; common_hal_socketpool_socket_settimeout(self, 0);
mp_uint_t port; }
mp_int_t ret = common_hal_socketpool_socket_recvfrom_into(self, return mp_const_none;
(byte*)bufinfo.buf, bufinfo.len, ip, &port);
mp_obj_t tuple_contents[2];
tuple_contents[0] = mp_obj_new_int_from_uint(ret);
tuple_contents[1] = netutils_format_inet_addr(ip, port, NETUTILS_BIG);
return mp_obj_new_tuple(2, tuple_contents);
} }
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socketpool_socket_recvfrom_into_obj, socketpool_socket_recvfrom_into); STATIC MP_DEFINE_CONST_FUN_OBJ_2(socketpool_socket_setblocking_obj, socketpool_socket_setblocking);
// //| def setsockopt(self, level: int, optname: int, value: int) -> None: // //| def setsockopt(self, level: int, optname: int, value: int) -> None:
// //| """Sets socket options""" // //| """Sets socket options"""
// //| ... // //| ...
// //| // //|
// STATIC mp_obj_t socketpool_socket_setsockopt(size_t n_args, const mp_obj_t *args) { // STATIC mp_obj_t socketpool_socket_setsockopt(size_t n_args, const mp_obj_t *args) {
// // mod_network_socket_obj_t *self = MP_OBJ_TO_PTR(args[0]); // // mod_network_socket_obj_t *self = MP_OBJ_TO_PTR(args[0]);
@ -324,13 +339,13 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(socketpool_socket_recvfrom_into_obj, socketpool
// } // }
// STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(socketpool_socket_setsockopt_obj, 4, 4, socketpool_socket_setsockopt); // STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(socketpool_socket_setsockopt_obj, 4, 4, socketpool_socket_setsockopt);
//| def settimeout(self, value: int) -> None: //| def settimeout(self, value: int) -> None:
//| """Set the timeout value for this socket. //| """Set the timeout value for this socket.
//| //|
//| :param ~int value: timeout in seconds. 0 means non-blocking. None means block indefinitely.""" //| :param ~int value: timeout in seconds. 0 means non-blocking. None means block indefinitely."""
//| ... //| ...
//| //|
STATIC mp_obj_t socketpool_socket_settimeout(mp_obj_t self_in, mp_obj_t timeout_in) { STATIC mp_obj_t socketpool_socket_settimeout(mp_obj_t self_in, mp_obj_t timeout_in) {
socketpool_socket_obj_t *self = MP_OBJ_TO_PTR(self_in); socketpool_socket_obj_t *self = MP_OBJ_TO_PTR(self_in);
mp_uint_t timeout_ms; mp_uint_t timeout_ms;
@ -348,24 +363,6 @@ STATIC mp_obj_t socketpool_socket_settimeout(mp_obj_t self_in, mp_obj_t timeout_
} }
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socketpool_socket_settimeout_obj, socketpool_socket_settimeout); STATIC MP_DEFINE_CONST_FUN_OBJ_2(socketpool_socket_settimeout_obj, socketpool_socket_settimeout);
// //| def setblocking(self, flag: bool) -> Optional[int]:
// //| """Set the blocking behaviour of this socket.
// //|
// //| :param ~bool flag: False means non-blocking, True means block indefinitely."""
// //| ...
// //|
// // method socket.setblocking(flag)
// STATIC mp_obj_t socketpool_socket_setblocking(mp_obj_t self_in, mp_obj_t blocking) {
// // if (mp_obj_is_true(blocking)) {
// // return socket_settimeout(self_in, mp_const_none);
// // } else {
// // return socket_settimeout(self_in, MP_OBJ_NEW_SMALL_INT(0));
// // }
// return mp_const_none;
// }
// STATIC MP_DEFINE_CONST_FUN_OBJ_2(socketpool_socket_setblocking_obj, socketpool_socket_setblocking);
//| def __hash__(self) -> int: //| def __hash__(self) -> int:
//| """Returns a hash for the Socket.""" //| """Returns a hash for the Socket."""
//| ... //| ...
@ -373,7 +370,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(socketpool_socket_settimeout_obj, socketpool_so
STATIC mp_obj_t socketpool_socket_unary_op(mp_unary_op_t op, mp_obj_t self_in) { STATIC mp_obj_t socketpool_socket_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
switch (op) { switch (op) {
case MP_UNARY_OP_HASH: { case MP_UNARY_OP_HASH: {
return MP_OBJ_NEW_SMALL_INT(common_hal_socketpool_socket_get_hash(MP_OBJ_TO_PTR(self_in))); return mp_obj_id(self_in);
} }
default: default:
return MP_OBJ_NULL; // op not supported return MP_OBJ_NULL; // op not supported
@ -384,19 +381,19 @@ STATIC const mp_rom_map_elem_t socketpool_socket_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&socketpool_socket___exit___obj) }, { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&socketpool_socket___exit___obj) },
{ MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&socketpool_socket_close_obj) }, { MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&socketpool_socket_close_obj) },
{ MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&socketpool_socket_close_obj) },
{ MP_ROM_QSTR(MP_QSTR_bind), MP_ROM_PTR(&socketpool_socket_bind_obj) },
{ MP_ROM_QSTR(MP_QSTR_listen), MP_ROM_PTR(&socketpool_socket_listen_obj) },
{ MP_ROM_QSTR(MP_QSTR_accept), MP_ROM_PTR(&socketpool_socket_accept_obj) }, { MP_ROM_QSTR(MP_QSTR_accept), MP_ROM_PTR(&socketpool_socket_accept_obj) },
{ MP_ROM_QSTR(MP_QSTR_bind), MP_ROM_PTR(&socketpool_socket_bind_obj) },
{ MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&socketpool_socket_close_obj) },
{ MP_ROM_QSTR(MP_QSTR_connect), MP_ROM_PTR(&socketpool_socket_connect_obj) }, { MP_ROM_QSTR(MP_QSTR_connect), MP_ROM_PTR(&socketpool_socket_connect_obj) },
{ MP_ROM_QSTR(MP_QSTR_send), MP_ROM_PTR(&socketpool_socket_send_obj) }, { MP_ROM_QSTR(MP_QSTR_listen), MP_ROM_PTR(&socketpool_socket_listen_obj) },
{ MP_ROM_QSTR(MP_QSTR_sendto), MP_ROM_PTR(&socketpool_socket_sendto_obj) },
{ MP_ROM_QSTR(MP_QSTR_recvfrom_into), MP_ROM_PTR(&socketpool_socket_recvfrom_into_obj) }, { MP_ROM_QSTR(MP_QSTR_recvfrom_into), MP_ROM_PTR(&socketpool_socket_recvfrom_into_obj) },
{ MP_ROM_QSTR(MP_QSTR_recv_into), MP_ROM_PTR(&socketpool_socket_recv_into_obj) }, { MP_ROM_QSTR(MP_QSTR_recv_into), MP_ROM_PTR(&socketpool_socket_recv_into_obj) },
{ MP_ROM_QSTR(MP_QSTR_send), MP_ROM_PTR(&socketpool_socket_send_obj) },
{ MP_ROM_QSTR(MP_QSTR_sendto), MP_ROM_PTR(&socketpool_socket_sendto_obj) },
{ MP_ROM_QSTR(MP_QSTR_setblocking), MP_ROM_PTR(&socketpool_socket_setblocking_obj) },
// { MP_ROM_QSTR(MP_QSTR_setsockopt), MP_ROM_PTR(&socketpool_socket_setsockopt_obj) }, // { MP_ROM_QSTR(MP_QSTR_setsockopt), MP_ROM_PTR(&socketpool_socket_setsockopt_obj) },
{ MP_ROM_QSTR(MP_QSTR_settimeout), MP_ROM_PTR(&socketpool_socket_settimeout_obj) }, { MP_ROM_QSTR(MP_QSTR_settimeout), MP_ROM_PTR(&socketpool_socket_settimeout_obj) },
// { MP_ROM_QSTR(MP_QSTR_setblocking), MP_ROM_PTR(&socketpool_socket_setblocking_obj) },
}; };
STATIC MP_DEFINE_CONST_DICT(socketpool_socket_locals_dict, socketpool_socket_locals_dict_table); STATIC MP_DEFINE_CONST_DICT(socketpool_socket_locals_dict, socketpool_socket_locals_dict_table);

View File

@ -31,22 +31,20 @@
extern const mp_obj_type_t socketpool_socket_type; extern const mp_obj_type_t socketpool_socket_type;
void common_hal_socketpool_socket_settimeout(socketpool_socket_obj_t* self, mp_uint_t timeout_ms);
bool common_hal_socketpool_socket_bind(socketpool_socket_obj_t* self, const char* host, size_t hostlen, uint8_t port);
bool common_hal_socketpool_socket_listen(socketpool_socket_obj_t* self, int backlog);
socketpool_socket_obj_t * common_hal_socketpool_socket_accept(socketpool_socket_obj_t* self, uint8_t* ip, uint *port); socketpool_socket_obj_t * common_hal_socketpool_socket_accept(socketpool_socket_obj_t* self, uint8_t* ip, uint *port);
bool common_hal_socketpool_socket_bind(socketpool_socket_obj_t* self, const char* host, size_t hostlen, uint8_t port);
bool common_hal_socketpool_socket_connect(socketpool_socket_obj_t* self, const char* host, size_t hostlen, mp_int_t port);
mp_uint_t common_hal_socketpool_socket_send(socketpool_socket_obj_t* self, const uint8_t* buf, mp_uint_t len);
mp_uint_t common_hal_socketpool_socket_recv_into(socketpool_socket_obj_t* self, const uint8_t* buf, mp_uint_t len);
mp_uint_t common_hal_socketpool_socket_sendto(socketpool_socket_obj_t* self,
const char* host, size_t hostlen, uint8_t port, const uint8_t* buf, mp_uint_t len);
mp_uint_t common_hal_socketpool_socket_recvfrom_into(socketpool_socket_obj_t* self,
uint8_t* buf, mp_uint_t len, uint8_t* ip, uint *port);
void common_hal_socketpool_socket_close(socketpool_socket_obj_t* self); void common_hal_socketpool_socket_close(socketpool_socket_obj_t* self);
bool common_hal_socketpool_socket_connect(socketpool_socket_obj_t* self, const char* host, size_t hostlen, mp_int_t port);
bool common_hal_socketpool_socket_get_closed(socketpool_socket_obj_t* self); bool common_hal_socketpool_socket_get_closed(socketpool_socket_obj_t* self);
bool common_hal_socketpool_socket_get_connected(socketpool_socket_obj_t* self); bool common_hal_socketpool_socket_get_connected(socketpool_socket_obj_t* self);
mp_uint_t common_hal_socketpool_socket_get_hash(socketpool_socket_obj_t* self); mp_uint_t common_hal_socketpool_socket_get_timeout(socketpool_socket_obj_t* self);
bool common_hal_socketpool_socket_listen(socketpool_socket_obj_t* self, int backlog);
mp_uint_t common_hal_socketpool_socket_recvfrom_into(socketpool_socket_obj_t* self,
uint8_t* buf, mp_uint_t len, uint8_t* ip, uint *port);
mp_uint_t common_hal_socketpool_socket_recv_into(socketpool_socket_obj_t* self, const uint8_t* buf, mp_uint_t len);
mp_uint_t common_hal_socketpool_socket_send(socketpool_socket_obj_t* self, const uint8_t* buf, mp_uint_t len);
mp_uint_t common_hal_socketpool_socket_sendto(socketpool_socket_obj_t* self,
const char* host, size_t hostlen, uint8_t port, const uint8_t* buf, mp_uint_t len);
void common_hal_socketpool_socket_settimeout(socketpool_socket_obj_t* self, mp_uint_t timeout_ms);
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_SOCKETPOOL_SOCKET_H #endif // MICROPY_INCLUDED_SHARED_BINDINGS_SOCKETPOOL_SOCKET_H

View File

@ -3,8 +3,7 @@
* *
* The MIT License (MIT) * The MIT License (MIT)
* *
* SPDX-FileCopyrightText: Copyright (c) 2014 Damien P. George * Copyright (c) 2020 Scott Shawcroft for Adafruit Industries
* 2018 Nick Moore for Adafruit Industries
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal

View File

@ -51,7 +51,7 @@ STATIC mp_obj_t ssl_sslcontext_make_new(const mp_obj_type_t *type, size_t n_args
return MP_OBJ_FROM_PTR(s); return MP_OBJ_FROM_PTR(s);
} }
//| def wrap_socket(sock: socketpool.Socket, *, server_side: bool = False, server_hostname: Optional[str] = None) -> socketpool.Socket: //| def wrap_socket(sock: socketpool.Socket, *, server_side: bool = False, server_hostname: Optional[str] = None) -> ssl.SSLSocket:
//| """Wraps the socket into a socket-compatible class that handles SSL negotiation. //| """Wraps the socket into a socket-compatible class that handles SSL negotiation.
//| The socket must be of type SOCK_STREAM.""" //| The socket must be of type SOCK_STREAM."""
//| ... //| ...

View File

@ -30,12 +30,13 @@
#include "common-hal/ssl/SSLContext.h" #include "common-hal/ssl/SSLContext.h"
#include "shared-bindings/socketpool/Socket.h" #include "shared-bindings/socketpool/Socket.h"
#include "shared-bindings/ssl/SSLSocket.h"
extern const mp_obj_type_t ssl_sslcontext_type; extern const mp_obj_type_t ssl_sslcontext_type;
void common_hal_ssl_sslcontext_construct(ssl_sslcontext_obj_t* self); void common_hal_ssl_sslcontext_construct(ssl_sslcontext_obj_t* self);
socketpool_socket_obj_t* common_hal_ssl_sslcontext_wrap_socket(ssl_sslcontext_obj_t* self, ssl_sslsocket_obj_t* common_hal_ssl_sslcontext_wrap_socket(ssl_sslcontext_obj_t* self,
socketpool_socket_obj_t* sock, bool server_side, const char* server_hostname); socketpool_socket_obj_t* sock, bool server_side, const char* server_hostname);
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_SSL_SSLCONTEXT_H #endif // MICROPY_INCLUDED_SHARED_BINDINGS_SSL_SSLCONTEXT_H

View File

@ -0,0 +1,320 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2021 Lucian Copeland for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "shared-bindings/ssl/SSLSocket.h"
#include <stdio.h>
#include <string.h>
#include "lib/utils/context_manager_helpers.h"
#include "py/objtuple.h"
#include "py/objlist.h"
#include "py/runtime.h"
#include "py/mperrno.h"
#include "lib/netutils/netutils.h"
//| class SSLSocket:
//| """Implements TLS security on a subset of `socketpool.Socket` functions. Cannot be created
//| directly. Instead, call `wrap_socket` on an existing socket object.
//|
//| Provides a subset of CPython's `ssl.SSLSocket` API. It only implements the versions of
//| recv that do not allocate bytes objects."""
//|
//| def __enter__(self) -> SSLSocket:
//| """No-op used by Context Managers."""
//| ...
//|
// Provided by context manager helper.
//| def __exit__(self) -> None:
//| """Automatically closes the Socket when exiting a context. See
//| :ref:`lifetime-and-contextmanagers` for more info."""
//| ...
//|
STATIC mp_obj_t ssl_sslsocket___exit__(size_t n_args, const mp_obj_t *args) {
(void)n_args;
common_hal_ssl_sslsocket_close(args[0]);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ssl_sslsocket___exit___obj, 4, 4, ssl_sslsocket___exit__);
//| def accept(self) -> Tuple[SSLSocket, Tuple[str, int]]:
//| """Accept a connection on a listening socket of type SOCK_STREAM,
//| creating a new socket of type SOCK_STREAM.
//| Returns a tuple of (new_socket, remote_address)"""
//|
STATIC mp_obj_t ssl_sslsocket_accept(mp_obj_t self_in) {
ssl_sslsocket_obj_t *self = MP_OBJ_TO_PTR(self_in);
uint8_t ip[4];
uint port;
ssl_sslsocket_obj_t * sslsock = common_hal_ssl_sslsocket_accept(self, ip, &port);
mp_obj_t tuple_contents[2];
tuple_contents[0] = MP_OBJ_FROM_PTR(sslsock);
tuple_contents[1] = netutils_format_inet_addr(ip, port, NETUTILS_BIG);
return mp_obj_new_tuple(2, tuple_contents);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(ssl_sslsocket_accept_obj, ssl_sslsocket_accept);
//| def bind(self, address: Tuple[str, int]) -> None:
//| """Bind a socket to an address
//|
//| :param ~tuple address: tuple of (remote_address, remote_port)"""
//| ...
//|
STATIC mp_obj_t ssl_sslsocket_bind(mp_obj_t self_in, mp_obj_t addr_in) {
ssl_sslsocket_obj_t *self = MP_OBJ_TO_PTR(self_in);
mp_obj_t *addr_items;
mp_obj_get_array_fixed_n(addr_in, 2, &addr_items);
size_t hostlen;
const char* host = mp_obj_str_get_data(addr_items[0], &hostlen);
mp_int_t port = mp_obj_get_int(addr_items[1]);
bool ok = common_hal_ssl_sslsocket_bind(self, host, hostlen, port);
if (!ok) {
mp_raise_ValueError(translate("Error: Failure to bind"));
}
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(ssl_sslsocket_bind_obj, ssl_sslsocket_bind);
//| def close(self) -> None:
//| """Closes this Socket"""
//|
STATIC mp_obj_t ssl_sslsocket_close(mp_obj_t self_in) {
ssl_sslsocket_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_ssl_sslsocket_close(self);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(ssl_sslsocket_close_obj, ssl_sslsocket_close);
//| def connect(self, address: Tuple[str, int]) -> None:
//| """Connect a socket to a remote address
//|
//| :param ~tuple address: tuple of (remote_address, remote_port)"""
//| ...
//|
STATIC mp_obj_t ssl_sslsocket_connect(mp_obj_t self_in, mp_obj_t addr_in) {
ssl_sslsocket_obj_t *self = MP_OBJ_TO_PTR(self_in);
mp_obj_t *addr_items;
mp_obj_get_array_fixed_n(addr_in, 2, &addr_items);
size_t hostlen;
const char* host = mp_obj_str_get_data(addr_items[0], &hostlen);
mp_int_t port = mp_obj_get_int(addr_items[1]);
bool ok = common_hal_ssl_sslsocket_connect(self, host, hostlen, port);
if (!ok) {
mp_raise_OSError(0);
}
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(ssl_sslsocket_connect_obj, ssl_sslsocket_connect);
//| def listen(self, backlog: int) -> None:
//| """Set socket to listen for incoming connections
//|
//| :param ~int backlog: length of backlog queue for waiting connetions"""
//| ...
//|
STATIC mp_obj_t ssl_sslsocket_listen(mp_obj_t self_in, mp_obj_t backlog_in) {
ssl_sslsocket_obj_t *self = MP_OBJ_TO_PTR(self_in);
int backlog = mp_obj_get_int(backlog_in);
common_hal_ssl_sslsocket_listen(self, backlog);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(ssl_sslsocket_listen_obj, ssl_sslsocket_listen);
//| def recv_into(self, buffer: WriteableBuffer, bufsize: int) -> int:
//| """Reads some bytes from the connected remote address, writing
//| into the provided buffer. If bufsize <= len(buffer) is given,
//| a maximum of bufsize bytes will be read into the buffer. If no
//| valid value is given for bufsize, the default is the length of
//| the given buffer.
//|
//| Suits sockets of type SOCK_STREAM
//| Returns an int of number of bytes read.
//|
//| :param bytearray buffer: buffer to receive into
//| :param int bufsize: optionally, a maximum number of bytes to read."""
//| ...
//|
STATIC mp_obj_t ssl_sslsocket_recv_into(size_t n_args, const mp_obj_t *args) {
ssl_sslsocket_obj_t *self = MP_OBJ_TO_PTR(args[0]);
if (common_hal_ssl_sslsocket_get_closed(self)) {
// Bad file number.
mp_raise_OSError(MP_EBADF);
}
// if (!common_hal_ssl_sslsocket_get_connected(self)) {
// // not connected
// mp_raise_OSError(MP_ENOTCONN);
// }
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_WRITE);
mp_int_t len = bufinfo.len;
if (n_args == 3) {
mp_int_t given_len = mp_obj_get_int(args[2]);
if (given_len > len) {
mp_raise_ValueError(translate("buffer too small for requested bytes"));
}
if (given_len > 0 && given_len < len) {
len = given_len;
}
}
if (len == 0) {
return MP_OBJ_NEW_SMALL_INT(0);
}
mp_int_t ret = common_hal_ssl_sslsocket_recv_into(self, (byte*)bufinfo.buf, len);
return mp_obj_new_int_from_uint(ret);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ssl_sslsocket_recv_into_obj, 2, 3, ssl_sslsocket_recv_into);
//| def send(self, bytes: ReadableBuffer) -> int:
//| """Send some bytes to the connected remote address.
//| Suits sockets of type SOCK_STREAM
//|
//| :param ~bytes bytes: some bytes to send"""
//| ...
//|
STATIC mp_obj_t ssl_sslsocket_send(mp_obj_t self_in, mp_obj_t buf_in) {
ssl_sslsocket_obj_t *self = MP_OBJ_TO_PTR(self_in);
if (common_hal_ssl_sslsocket_get_closed(self)) {
// Bad file number.
mp_raise_OSError(MP_EBADF);
}
if (!common_hal_ssl_sslsocket_get_connected(self)) {
mp_raise_BrokenPipeError();
}
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_READ);
mp_int_t ret = common_hal_ssl_sslsocket_send(self, bufinfo.buf, bufinfo.len);
if (ret == -1) {
mp_raise_BrokenPipeError();
}
return mp_obj_new_int_from_uint(ret);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(ssl_sslsocket_send_obj, ssl_sslsocket_send);
// //| def setsockopt(self, level: int, optname: int, value: int) -> None:
// //| """Sets socket options"""
// //| ...
// //|
// STATIC mp_obj_t ssl_sslsocket_setsockopt(size_t n_args, const mp_obj_t *args) {
// }
// STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ssl_sslsocket_setsockopt_obj, 4, 4, ssl_sslsocket_setsockopt);
//| def settimeout(self, value: int) -> None:
//| """Set the timeout value for this socket.
//|
//| :param ~int value: timeout in seconds. 0 means non-blocking. None means block indefinitely."""
//| ...
//|
STATIC mp_obj_t ssl_sslsocket_settimeout(mp_obj_t self_in, mp_obj_t timeout_in) {
ssl_sslsocket_obj_t *self = MP_OBJ_TO_PTR(self_in);
mp_uint_t timeout_ms;
if (timeout_in == mp_const_none) {
timeout_ms = -1;
} else {
#if MICROPY_PY_BUILTINS_FLOAT
timeout_ms = 1000 * mp_obj_get_float(timeout_in);
#else
timeout_ms = 1000 * mp_obj_get_int(timeout_in);
#endif
}
common_hal_ssl_sslsocket_settimeout(self, timeout_ms);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(ssl_sslsocket_settimeout_obj, ssl_sslsocket_settimeout);
//| def setblocking(self, flag: bool) -> Optional[int]:
//| """Set the blocking behaviour of this socket.
//|
//| :param ~bool flag: False means non-blocking, True means block indefinitely."""
//| ...
//|
// method socket.setblocking(flag)
STATIC mp_obj_t ssl_sslsocket_setblocking(mp_obj_t self_in, mp_obj_t blocking) {
ssl_sslsocket_obj_t *self = MP_OBJ_TO_PTR(self_in);
if (mp_obj_is_true(blocking)) {
common_hal_ssl_sslsocket_settimeout(self, -1);
} else {
common_hal_ssl_sslsocket_settimeout(self, 0);
}
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(ssl_sslsocket_setblocking_obj, ssl_sslsocket_setblocking);
//| def __hash__(self) -> int:
//| """Returns a hash for the Socket."""
//| ...
//|
STATIC mp_obj_t ssl_sslsocket_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
switch (op) {
case MP_UNARY_OP_HASH: {
return mp_obj_id(self_in);
}
default:
return MP_OBJ_NULL; // op not supported
}
}
STATIC const mp_rom_map_elem_t ssl_sslsocket_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&ssl_sslsocket___exit___obj) },
{ MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&ssl_sslsocket_close_obj) },
{ MP_ROM_QSTR(MP_QSTR_accept), MP_ROM_PTR(&ssl_sslsocket_accept_obj) },
{ MP_ROM_QSTR(MP_QSTR_bind), MP_ROM_PTR(&ssl_sslsocket_bind_obj) },
{ MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&ssl_sslsocket_close_obj) },
{ MP_ROM_QSTR(MP_QSTR_connect), MP_ROM_PTR(&ssl_sslsocket_connect_obj) },
{ MP_ROM_QSTR(MP_QSTR_listen), MP_ROM_PTR(&ssl_sslsocket_listen_obj) },
{ MP_ROM_QSTR(MP_QSTR_recv_into), MP_ROM_PTR(&ssl_sslsocket_recv_into_obj) },
{ MP_ROM_QSTR(MP_QSTR_send), MP_ROM_PTR(&ssl_sslsocket_send_obj) },
{ MP_ROM_QSTR(MP_QSTR_setblocking), MP_ROM_PTR(&ssl_sslsocket_setblocking_obj) },
// { MP_ROM_QSTR(MP_QSTR_setsockopt), MP_ROM_PTR(&ssl_sslsocket_setsockopt_obj) },
{ MP_ROM_QSTR(MP_QSTR_settimeout), MP_ROM_PTR(&ssl_sslsocket_settimeout_obj) },
};
STATIC MP_DEFINE_CONST_DICT(ssl_sslsocket_locals_dict, ssl_sslsocket_locals_dict_table);
const mp_obj_type_t ssl_sslsocket_type = {
{ &mp_type_type },
.name = MP_QSTR_SSLSocket,
.locals_dict = (mp_obj_dict_t*)&ssl_sslsocket_locals_dict,
.unary_op = ssl_sslsocket_unary_op,
};

View File

@ -0,0 +1,45 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2020 Lucian Copeland for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_SSL_SSLSOCKET_H
#define MICROPY_INCLUDED_SHARED_BINDINGS_SSL_SSLSOCKET_H
#include "common-hal/ssl/SSLSocket.h"
extern const mp_obj_type_t ssl_sslsocket_type;
ssl_sslsocket_obj_t * common_hal_ssl_sslsocket_accept(ssl_sslsocket_obj_t* self, uint8_t* ip, uint *port);
bool common_hal_ssl_sslsocket_bind(ssl_sslsocket_obj_t* self, const char* host, size_t hostlen, uint8_t port);
void common_hal_ssl_sslsocket_close(ssl_sslsocket_obj_t* self);
bool common_hal_ssl_sslsocket_connect(ssl_sslsocket_obj_t* self, const char* host, size_t hostlen, mp_int_t port);
bool common_hal_ssl_sslsocket_get_closed(ssl_sslsocket_obj_t* self);
bool common_hal_ssl_sslsocket_get_connected(ssl_sslsocket_obj_t* self);
bool 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, const 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_uint_t timeout_ms);
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_SSL_SSLSOCKET_H