Skip to content

Commit

Permalink
feat(mdns): add an API for setting address to a delegated host
Browse files Browse the repository at this point in the history
  • Loading branch information
zwx1995esp committed Jun 27, 2023
1 parent 4f1769e commit 0156b22
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
15 changes: 15 additions & 0 deletions components/mdns/include/mdns.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
59 changes: 59 additions & 0 deletions components/mdns/mdns.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions components/mdns/private_include/mdns_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 0156b22

Please sign in to comment.