From 92593aa7a123da6bc8fa43e6bb619886a64aba38 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Fri, 17 Jun 2022 09:30:12 -0700 Subject: [PATCH] Fix bind, remove hard coded ip --- ports/espressif/common-hal/socketpool/Socket.c | 13 +++++++++++-- supervisor/shared/web_workflow/web_workflow.c | 4 +--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/ports/espressif/common-hal/socketpool/Socket.c b/ports/espressif/common-hal/socketpool/Socket.c index d808314c20..d09800811d 100644 --- a/ports/espressif/common-hal/socketpool/Socket.c +++ b/ports/espressif/common-hal/socketpool/Socket.c @@ -131,7 +131,17 @@ 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, uint32_t port) { struct sockaddr_in bind_addr; - bind_addr.sin_addr.s_addr = inet_addr(host); + const char *broadcast = ""; + uint32_t ip; + if (hostlen == 0) { + ip = IPADDR_ANY; + } else if (hostlen == strlen(broadcast) && + memcmp(host, broadcast, strlen(broadcast)) == 0) { + ip = IPADDR_BROADCAST; + } else { + ip = inet_addr(host); + } + bind_addr.sin_addr.s_addr = ip; bind_addr.sin_family = AF_INET; bind_addr.sin_port = htons(port); @@ -141,7 +151,6 @@ bool common_hal_socketpool_socket_bind(socketpool_socket_obj_t *self, mp_raise_RuntimeError(translate("Cannot set socket options")); } int result = lwip_bind(self->num, (struct sockaddr *)&bind_addr, sizeof(bind_addr)); - ESP_LOGE(TAG, "bind result %d", result); return result == 0; } diff --git a/supervisor/shared/web_workflow/web_workflow.c b/supervisor/shared/web_workflow/web_workflow.c index 586127a348..fac3e949ac 100644 --- a/supervisor/shared/web_workflow/web_workflow.c +++ b/supervisor/shared/web_workflow/web_workflow.c @@ -220,11 +220,9 @@ void supervisor_start_web_workflow(void) { common_hal_socketpool_socket_settimeout(&listening, 0); // Bind to any ip. // TODO: Make this port .env configurable. - const char *ip = "192.168.1.94"; - common_hal_socketpool_socket_bind(&listening, ip, strlen(ip), 80); + common_hal_socketpool_socket_bind(&listening, "", 0, 80); common_hal_socketpool_socket_listen(&listening, 1); - mp_int_t api_password_len = dotenv_get_key("/.env", "CIRCUITPY_WEB_API_PASSWORD", _api_password + 1, sizeof(_api_password) - 2); if (api_password_len > 0) { _api_password[0] = ':';