2020-08-11 19:04:55 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2020 Scott Shawcroft 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.
|
|
|
|
*/
|
2020-08-12 19:57:39 -04:00
|
|
|
|
|
|
|
#include "shared-bindings/socketpool/Socket.h"
|
|
|
|
|
2020-08-21 16:32:59 -04:00
|
|
|
#include "bindings/espidf/__init__.h"
|
2020-08-19 20:51:33 -04:00
|
|
|
#include "lib/utils/interrupt_char.h"
|
2020-08-18 20:06:59 -04:00
|
|
|
#include "py/mperrno.h"
|
|
|
|
#include "py/runtime.h"
|
2020-08-19 20:51:33 -04:00
|
|
|
#include "supervisor/shared/tick.h"
|
|
|
|
|
2020-11-13 13:12:07 -05:00
|
|
|
#include "components/lwip/lwip/src/include/lwip/err.h"
|
|
|
|
#include "components/lwip/lwip/src/include/lwip/sockets.h"
|
|
|
|
#include "components/lwip/lwip/src/include/lwip/sys.h"
|
|
|
|
#include "components/lwip/lwip/src/include/lwip/netdb.h"
|
|
|
|
|
2020-08-12 19:57:39 -04:00
|
|
|
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_connect(socketpool_socket_obj_t* self, 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.
|
|
|
|
|
2020-08-13 19:55:49 -04:00
|
|
|
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->tcp);
|
2020-08-18 20:06:59 -04:00
|
|
|
self->connected = result >= 0;
|
|
|
|
if (result < 0) {
|
|
|
|
int esp_tls_code;
|
2020-08-19 20:51:33 -04:00
|
|
|
int flags;
|
|
|
|
esp_err_t err = esp_tls_get_and_clear_last_error(self->tcp->error_handle, &esp_tls_code, &flags);
|
2020-08-18 20:06:59 -04:00
|
|
|
|
2020-08-21 14:00:02 -04:00
|
|
|
if (err == ESP_ERR_MBEDTLS_SSL_SETUP_FAILED) {
|
|
|
|
mp_raise_espidf_MemoryError();
|
2020-08-25 19:15:45 -04:00
|
|
|
} else if (ESP_ERR_MBEDTLS_SSL_HANDSHAKE_FAILED) {
|
|
|
|
mp_raise_OSError_msg_varg(translate("Failed SSL handshake"));
|
2020-08-21 14:00:02 -04:00
|
|
|
} else {
|
2020-08-25 19:15:45 -04:00
|
|
|
mp_raise_OSError_msg_varg(translate("Unhandled ESP TLS error %d %d %x %d"), esp_tls_code, flags, err, result);
|
2020-08-21 14:00:02 -04:00
|
|
|
}
|
2020-08-18 20:06:59 -04:00
|
|
|
}
|
2020-08-21 14:00:02 -04:00
|
|
|
|
2020-08-18 20:06:59 -04:00
|
|
|
return self->connected;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool common_hal_socketpool_socket_get_connected(socketpool_socket_obj_t* self) {
|
|
|
|
return self->connected;
|
2020-08-12 19:57:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
mp_uint_t common_hal_socketpool_socket_send(socketpool_socket_obj_t* self, const uint8_t* buf, mp_uint_t len) {
|
|
|
|
size_t sent = esp_tls_conn_write(self->tcp, buf, len);
|
|
|
|
|
|
|
|
if (sent < 0) {
|
2020-08-18 20:06:59 -04:00
|
|
|
mp_raise_OSError(MP_ENOTCONN);
|
2020-08-12 19:57:39 -04:00
|
|
|
}
|
|
|
|
return sent;
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_uint_t common_hal_socketpool_socket_recv_into(socketpool_socket_obj_t* self, const uint8_t* buf, mp_uint_t len) {
|
2020-08-19 20:51:33 -04:00
|
|
|
size_t received = 0;
|
2020-10-29 22:38:25 -04:00
|
|
|
int status = 0;
|
2020-08-19 20:51:33 -04:00
|
|
|
uint64_t start_ticks = supervisor_ticks_ms64();
|
2020-08-21 14:00:02 -04:00
|
|
|
int sockfd;
|
|
|
|
esp_err_t err = esp_tls_get_conn_sockfd(self->tcp, &sockfd);
|
|
|
|
if (err != ESP_OK) {
|
|
|
|
mp_raise_OSError(MP_EBADF);
|
|
|
|
}
|
2020-10-29 22:38:25 -04:00
|
|
|
while (received == 0 &&
|
|
|
|
status >= 0 &&
|
2020-08-19 20:51:33 -04:00
|
|
|
(self->timeout_ms == 0 || supervisor_ticks_ms64() - start_ticks <= self->timeout_ms) &&
|
|
|
|
!mp_hal_is_interrupted()) {
|
|
|
|
RUN_BACKGROUND_TASKS;
|
|
|
|
size_t available = esp_tls_get_bytes_avail(self->tcp);
|
2020-08-21 14:00:02 -04:00
|
|
|
if (available == 0) {
|
|
|
|
// This reads the raw socket buffer and is used for non-TLS connections
|
|
|
|
// and between encrypted TLS blocks.
|
2020-10-29 22:38:25 -04:00
|
|
|
status = lwip_ioctl(sockfd, FIONREAD, &available);
|
2020-08-21 14:00:02 -04:00
|
|
|
}
|
2020-08-19 20:51:33 -04:00
|
|
|
size_t remaining = len - received;
|
|
|
|
if (available > remaining) {
|
|
|
|
available = remaining;
|
|
|
|
}
|
2020-08-21 14:00:02 -04:00
|
|
|
if (available > 0) {
|
2020-10-29 22:38:25 -04:00
|
|
|
status = esp_tls_conn_read(self->tcp, (void*) buf + received, available);
|
2020-11-24 21:14:22 -05:00
|
|
|
if (status == 0) {
|
|
|
|
// Reading zero when something is available indicates a closed
|
|
|
|
// connection. (The available bytes could have been TLS internal.)
|
|
|
|
break;
|
|
|
|
}
|
2020-10-29 22:38:25 -04:00
|
|
|
if (status > 0) {
|
|
|
|
received += status;
|
|
|
|
}
|
2020-08-19 20:51:33 -04:00
|
|
|
}
|
|
|
|
}
|
2020-08-12 19:57:39 -04:00
|
|
|
|
2020-10-29 22:38:25 -04:00
|
|
|
if (received == 0) {
|
2020-08-12 19:57:39 -04:00
|
|
|
// socket closed
|
2020-08-18 20:06:59 -04:00
|
|
|
common_hal_socketpool_socket_close(self);
|
2020-08-12 19:57:39 -04:00
|
|
|
}
|
2020-10-29 22:38:25 -04:00
|
|
|
if (status < 0) {
|
2020-08-18 20:06:59 -04:00
|
|
|
mp_raise_BrokenPipeError();
|
2020-08-12 19:57:39 -04:00
|
|
|
}
|
|
|
|
return received;
|
|
|
|
}
|
|
|
|
|
2020-10-28 21:08:31 -04:00
|
|
|
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) {
|
|
|
|
|
2020-11-13 13:12:07 -05:00
|
|
|
// Get the IP address string
|
2020-10-28 21:08:31 -04:00
|
|
|
const struct addrinfo hints = {
|
|
|
|
.ai_family = AF_INET,
|
|
|
|
.ai_socktype = SOCK_STREAM,
|
|
|
|
};
|
2020-11-13 13:12:07 -05:00
|
|
|
struct addrinfo *result;
|
|
|
|
int error = lwip_getaddrinfo(host, NULL, &hints, &result);
|
|
|
|
if (error != 0 || result == NULL) {
|
|
|
|
return 0;
|
2020-10-28 21:08:31 -04:00
|
|
|
}
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wcast-align"
|
2020-11-13 13:12:07 -05:00
|
|
|
struct in_addr *addr = &((struct sockaddr_in *)result->ai_addr)->sin_addr;
|
2020-10-28 21:08:31 -04:00
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
char ip_str[IP4ADDR_STRLEN_MAX];
|
|
|
|
inet_ntoa_r(*addr, ip_str, IP4ADDR_STRLEN_MAX);
|
2020-11-13 13:12:07 -05:00
|
|
|
freeaddrinfo(result);
|
2020-10-28 21:08:31 -04:00
|
|
|
|
2020-11-13 13:12:07 -05:00
|
|
|
// Set parameters
|
|
|
|
struct sockaddr_in dest_addr;
|
|
|
|
dest_addr.sin_addr.s_addr = inet_addr((const char *)ip_str);
|
|
|
|
dest_addr.sin_family = AF_INET;
|
|
|
|
dest_addr.sin_port = htons(port);
|
2020-10-28 21:08:31 -04:00
|
|
|
|
2020-11-13 13:12:07 -05:00
|
|
|
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;
|
2020-10-28 21:08:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
mp_uint_t common_hal_socketpool_socket_recvfrom_into(socketpool_socket_obj_t* self,
|
2020-11-13 13:12:07 -05:00
|
|
|
uint8_t* buf, mp_uint_t len, uint8_t* ip, uint *port) {
|
2020-10-28 21:08:31 -04:00
|
|
|
|
2020-11-13 13:12:07 -05:00
|
|
|
struct sockaddr_in source_addr;
|
|
|
|
socklen_t socklen = sizeof(source_addr);
|
|
|
|
int bytes_received = lwip_recvfrom(self->num, buf, len - 1, 0, (struct sockaddr *)&source_addr, &socklen);
|
2020-10-28 21:08:31 -04:00
|
|
|
|
2020-11-13 13:12:07 -05:00
|
|
|
memcpy((void *)ip, (void*)&source_addr.sin_addr.s_addr, sizeof source_addr.sin_addr.s_addr);
|
|
|
|
*port = source_addr.sin_port;
|
|
|
|
|
|
|
|
if (bytes_received < 0) {
|
|
|
|
mp_raise_BrokenPipeError();
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
buf[bytes_received] = 0; // Null-terminate whatever we received
|
|
|
|
return bytes_received;
|
|
|
|
}
|
2020-10-28 21:08:31 -04:00
|
|
|
}
|
|
|
|
|
2020-08-12 19:57:39 -04:00
|
|
|
void common_hal_socketpool_socket_close(socketpool_socket_obj_t* self) {
|
2020-09-03 19:32:12 -04:00
|
|
|
self->connected = false;
|
2020-08-12 19:57:39 -04:00
|
|
|
if (self->tcp != NULL) {
|
2020-09-03 19:32:12 -04:00
|
|
|
esp_tls_conn_destroy(self->tcp);
|
2020-08-12 19:57:39 -04:00
|
|
|
self->tcp = NULL;
|
|
|
|
}
|
2020-10-28 21:08:31 -04:00
|
|
|
if (self->num >= 0) {
|
|
|
|
lwip_shutdown(self->num, 0);
|
|
|
|
lwip_close(self->num);
|
2020-11-24 14:51:06 -05:00
|
|
|
self->num = -1;
|
2020-10-28 21:08:31 -04:00
|
|
|
}
|
2020-08-12 19:57:39 -04:00
|
|
|
}
|
2020-08-18 20:06:59 -04:00
|
|
|
|
|
|
|
bool common_hal_socketpool_socket_get_closed(socketpool_socket_obj_t* self) {
|
2020-10-28 21:08:31 -04:00
|
|
|
return self->tcp == NULL && self->num < 0;
|
2020-08-18 20:06:59 -04:00
|
|
|
}
|
2020-08-25 19:15:45 -04:00
|
|
|
|
|
|
|
|
|
|
|
mp_uint_t common_hal_socketpool_socket_get_hash(socketpool_socket_obj_t* self) {
|
|
|
|
return self->num;
|
|
|
|
}
|