Fix bind, remove hard coded ip

This commit is contained in:
Scott Shawcroft 2022-06-17 09:30:12 -07:00
parent 35045f0eab
commit 92593aa7a1
No known key found for this signature in database
GPG Key ID: 0DFD512649C052DA
2 changed files with 12 additions and 5 deletions

View File

@ -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 = "<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;
}

View File

@ -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] = ':';