From ddc3eb62d0ffd4f690f466b2798628d9bbf2e906 Mon Sep 17 00:00:00 2001 From: zhangwenxu Date: Tue, 27 Jun 2023 16:14:52 +0800 Subject: [PATCH] feat(mdns): add an API for setting address to a delegated host --- components/mdns/include/mdns.h | 15 +++++ components/mdns/mdns.c | 59 +++++++++++++++++++ .../mdns/private_include/mdns_private.h | 1 + 3 files changed, 75 insertions(+) diff --git a/components/mdns/include/mdns.h b/components/mdns/include/mdns.h index 26761c887..7abcc565a 100644 --- a/components/mdns/include/mdns.h +++ b/components/mdns/include/mdns.h @@ -158,6 +158,21 @@ esp_err_t mdns_hostname_get(char *hostname); */ esp_err_t mdns_delegate_hostname_add(const char *hostname, const mdns_ip_addr_t *address_list); +/** + * @brief Set the address to a delegated hostname + * + * @param hostname Hostname to set + * @param address_list The IP address list of the host + * + * @return + * - ESP_OK success + * - ESP_ERR_INVALID_STATE mDNS is not running + * - ESP_ERR_INVALID_ARG Parameter error + * - ESP_ERR_NO_MEM memory error + * + */ +esp_err_t mdns_delegate_hostname_set_address(const char *hostname, const mdns_ip_addr_t *address_list); + /** * @brief Remove a delegated hostname * All the services added to this host will also be removed. diff --git a/components/mdns/mdns.c b/components/mdns/mdns.c index 47a7e9bbd..33b76cf18 100644 --- a/components/mdns/mdns.c +++ b/components/mdns/mdns.c @@ -2993,6 +2993,27 @@ static void free_address_list(mdns_ip_addr_t *address_list) } } + +static bool _mdns_delegate_hostname_set_address(const char *hostname, mdns_ip_addr_t *address_list) +{ + if (!_str_null_or_empty(_mdns_server->hostname) && + strcasecmp(hostname, _mdns_server->hostname) == 0) { + return false; + } + mdns_host_item_t *host = _mdns_host_list; + while (host != NULL) { + if (strcasecmp(hostname, host->hostname) == 0) { + // free previous address list + free_address_list(host->address_list); + // set current address list to the host + host->address_list = address_list; + return true; + } + host = host->next; + } + return false; +} + static mdns_ip_addr_t *copy_address_list(const mdns_ip_addr_t *address_list) { mdns_ip_addr_t *head = NULL; @@ -4845,6 +4866,7 @@ static void _mdns_free_action(mdns_action_t *action) case ACTION_RX_HANDLE: _mdns_packet_free(action->data.rx_handle.packet); break; + case ACTION_DELEGATE_HOSTNAME_SET_ADDR: case ACTION_DELEGATE_HOSTNAME_ADD: free((char *)action->data.delegate_hostname.hostname); free_address_list(action->data.delegate_hostname.address_list); @@ -5065,6 +5087,13 @@ static void _mdns_execute_action(mdns_action_t *action) free_address_list(action->data.delegate_hostname.address_list); } break; + case ACTION_DELEGATE_HOSTNAME_SET_ADDR: + if (!_mdns_delegate_hostname_set_address(action->data.delegate_hostname.hostname, + action->data.delegate_hostname.address_list)) { + free_address_list(action->data.delegate_hostname.address_list); + } + free((char *)action->data.delegate_hostname.hostname); + break; case ACTION_DELEGATE_HOSTNAME_REMOVE: _mdns_delegate_hostname_remove(action->data.delegate_hostname.hostname); free((char *)action->data.delegate_hostname.hostname); @@ -5634,6 +5663,36 @@ esp_err_t mdns_delegate_hostname_remove(const char *hostname) return ESP_OK; } +esp_err_t mdns_delegate_hostname_set_address(const char *hostname, const mdns_ip_addr_t *address_list) +{ + if (!_mdns_server) { + return ESP_ERR_INVALID_STATE; + } + if (_str_null_or_empty(hostname) || strlen(hostname) > (MDNS_NAME_BUF_LEN - 1)) { + return ESP_ERR_INVALID_ARG; + } + char *new_hostname = strndup(hostname, MDNS_NAME_BUF_LEN - 1); + if (!new_hostname) { + return ESP_ERR_NO_MEM; + } + + mdns_action_t *action = (mdns_action_t *)malloc(sizeof(mdns_action_t)); + if (!action) { + HOOK_MALLOC_FAILED; + free(new_hostname); + return ESP_ERR_NO_MEM; + } + action->type = ACTION_DELEGATE_HOSTNAME_SET_ADDR; + action->data.delegate_hostname.hostname = new_hostname; + action->data.delegate_hostname.address_list = copy_address_list(address_list); + if (xQueueSend(_mdns_server->action_queue, &action, (TickType_t)0) != pdPASS) { + free(new_hostname); + free(action); + return ESP_ERR_NO_MEM; + } + return ESP_OK; +} + bool mdns_hostname_exists(const char *hostname) { return _hostname_is_ours(hostname); diff --git a/components/mdns/private_include/mdns_private.h b/components/mdns/private_include/mdns_private.h index d613e006b..9289e4449 100644 --- a/components/mdns/private_include/mdns_private.h +++ b/components/mdns/private_include/mdns_private.h @@ -196,6 +196,7 @@ typedef enum { ACTION_TASK_STOP, ACTION_DELEGATE_HOSTNAME_ADD, ACTION_DELEGATE_HOSTNAME_REMOVE, + ACTION_DELEGATE_HOSTNAME_SET_ADDR, ACTION_MAX } mdns_action_type_t;