From 2174693096b73ce93261611c44ecba647cd01859 Mon Sep 17 00:00:00 2001 From: Jiacheng Guo Date: Fri, 2 Apr 2021 18:06:10 +0800 Subject: [PATCH] mdns: add remove delegate host api --- components/mdns/include/mdns.h | 2 + components/mdns/mdns.c | 76 +++++++++++++++++++ .../mdns/private_include/mdns_private.h | 1 + .../protocols/mdns/main/mdns_example_main.c | 6 ++ 4 files changed, 85 insertions(+) diff --git a/components/mdns/include/mdns.h b/components/mdns/include/mdns.h index dcf8972bdd..54cd8a3ee1 100644 --- a/components/mdns/include/mdns.h +++ b/components/mdns/include/mdns.h @@ -116,6 +116,8 @@ esp_err_t mdns_hostname_set(const char * hostname); esp_err_t mdns_delegate_hostname_add(const char * hostname, const esp_ip_addr_t *address); +esp_err_t mdns_delegate_hostname_remove(const char * hostname); + bool mdns_hostname_exists(const char *hostname); /** diff --git a/components/mdns/mdns.c b/components/mdns/mdns.c index 2a715fe304..35aa4e09c6 100644 --- a/components/mdns/mdns.c +++ b/components/mdns/mdns.c @@ -2472,6 +2472,48 @@ static bool _mdns_delegate_hostname_add(const char *hostname, const esp_ip_addr_ return true; } +static bool _mdns_delegate_hostname_remove(const char *hostname) +{ + mdns_srv_item_t *srv = _mdns_server->services; + mdns_srv_item_t *prev_srv = NULL; + while (srv) { + if (strcasecmp(srv->service->hostname, hostname) == 0) { + mdns_srv_item_t *to_free = srv; + _mdns_send_bye(&srv, 1, false); + _mdns_remove_scheduled_service_packets(srv->service); + if (prev_srv == NULL) { + _mdns_server->services = srv->next; + srv = srv->next; + } else { + prev_srv->next = srv->next; + } + _mdns_free_service(to_free->service); + free(to_free); + } else { + prev_srv = srv; + srv = srv->next; + } + } + mdns_host_item_t *host = _mdns_host_list; + mdns_host_item_t *prev_host = NULL; + while (host != NULL) { + if (strcasecmp(hostname, host->hostname) == 0) { + if (prev_host == NULL) { + _mdns_host_list = host->next; + } else { + prev_host->next = host->next; + } + free((char *)host->hostname); + free(host); + break; + } else { + prev_host = host; + host = host->next; + } + } + return true; +} + /** * @brief Check if parsed name is discovery */ @@ -4191,6 +4233,11 @@ static void _mdns_execute_action(mdns_action_t * action) break; case ACTION_DELEGATE_HOSTNAME_ADD: _mdns_delegate_hostname_add(action->data.delegate_hostname.hostname, &action->data.delegate_hostname.address); + break; + case ACTION_DELEGATE_HOSTNAME_REMOVE: + _mdns_delegate_hostname_remove(action->data.delegate_hostname.hostname); + free((char *)action->data.delegate_hostname.hostname); + break; default: break; } @@ -4636,6 +4683,35 @@ esp_err_t mdns_delegate_hostname_add(const char * hostname, const esp_ip_addr_t return ERR_OK; } +esp_err_t mdns_delegate_hostname_remove(const char * hostname) +{ + if (!_mdns_server) { + return ESP_ERR_INVALID_ARG; + } + 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_REMOVE; + action->data.delegate_hostname.hostname = new_hostname; + if (xQueueSend(_mdns_server->action_queue, &action, (portTickType)0) != pdPASS) { + free(new_hostname); + free(action); + return ESP_ERR_NO_MEM; + } + return ERR_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 b813c5b439..6acdf92833 100644 --- a/components/mdns/private_include/mdns_private.h +++ b/components/mdns/private_include/mdns_private.h @@ -185,6 +185,7 @@ typedef enum { ACTION_RX_HANDLE, ACTION_TASK_STOP, ACTION_DELEGATE_HOSTNAME_ADD, + ACTION_DELEGATE_HOSTNAME_REMOVE, ACTION_MAX } mdns_action_type_t; diff --git a/examples/protocols/mdns/main/mdns_example_main.c b/examples/protocols/mdns/main/mdns_example_main.c index fcad5eaa7d..525193d20d 100644 --- a/examples/protocols/mdns/main/mdns_example_main.c +++ b/examples/protocols/mdns/main/mdns_example_main.c @@ -187,10 +187,16 @@ static void mdns_example_task(void *pvParameters) query_mdns_host_with_gethostbyname("tinytester-lwip.local"); query_mdns_host_with_getaddrinfo("tinytester-lwip.local"); #endif + bool removed = false; while (1) { check_button(); vTaskDelay(50 / portTICK_PERIOD_MS); + if (pdTICKS_TO_MS(xTaskGetTickCount()) >= 15 * 1000 && ! removed) { + printf("Remove device\n"); + ESP_ERROR_CHECK(mdns_delegate_hostname_remove("test-device")); + removed = true; + } } }