Skip to content

Commit 40e3875

Browse files
authored
Merge pull request #315 from zwx1995esp/feature/add_delegate_host_addr_set
feat(mdns): add an API for setting address to a delegated host
2 parents 3625889 + ddc3eb6 commit 40e3875

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

components/mdns/include/mdns.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,21 @@ esp_err_t mdns_hostname_get(char *hostname);
158158
*/
159159
esp_err_t mdns_delegate_hostname_add(const char *hostname, const mdns_ip_addr_t *address_list);
160160

161+
/**
162+
* @brief Set the address to a delegated hostname
163+
*
164+
* @param hostname Hostname to set
165+
* @param address_list The IP address list of the host
166+
*
167+
* @return
168+
* - ESP_OK success
169+
* - ESP_ERR_INVALID_STATE mDNS is not running
170+
* - ESP_ERR_INVALID_ARG Parameter error
171+
* - ESP_ERR_NO_MEM memory error
172+
*
173+
*/
174+
esp_err_t mdns_delegate_hostname_set_address(const char *hostname, const mdns_ip_addr_t *address_list);
175+
161176
/**
162177
* @brief Remove a delegated hostname
163178
* All the services added to this host will also be removed.

components/mdns/mdns.c

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2993,6 +2993,27 @@ static void free_address_list(mdns_ip_addr_t *address_list)
29932993
}
29942994
}
29952995

2996+
2997+
static bool _mdns_delegate_hostname_set_address(const char *hostname, mdns_ip_addr_t *address_list)
2998+
{
2999+
if (!_str_null_or_empty(_mdns_server->hostname) &&
3000+
strcasecmp(hostname, _mdns_server->hostname) == 0) {
3001+
return false;
3002+
}
3003+
mdns_host_item_t *host = _mdns_host_list;
3004+
while (host != NULL) {
3005+
if (strcasecmp(hostname, host->hostname) == 0) {
3006+
// free previous address list
3007+
free_address_list(host->address_list);
3008+
// set current address list to the host
3009+
host->address_list = address_list;
3010+
return true;
3011+
}
3012+
host = host->next;
3013+
}
3014+
return false;
3015+
}
3016+
29963017
static mdns_ip_addr_t *copy_address_list(const mdns_ip_addr_t *address_list)
29973018
{
29983019
mdns_ip_addr_t *head = NULL;
@@ -4845,6 +4866,7 @@ static void _mdns_free_action(mdns_action_t *action)
48454866
case ACTION_RX_HANDLE:
48464867
_mdns_packet_free(action->data.rx_handle.packet);
48474868
break;
4869+
case ACTION_DELEGATE_HOSTNAME_SET_ADDR:
48484870
case ACTION_DELEGATE_HOSTNAME_ADD:
48494871
free((char *)action->data.delegate_hostname.hostname);
48504872
free_address_list(action->data.delegate_hostname.address_list);
@@ -5065,6 +5087,13 @@ static void _mdns_execute_action(mdns_action_t *action)
50655087
free_address_list(action->data.delegate_hostname.address_list);
50665088
}
50675089
break;
5090+
case ACTION_DELEGATE_HOSTNAME_SET_ADDR:
5091+
if (!_mdns_delegate_hostname_set_address(action->data.delegate_hostname.hostname,
5092+
action->data.delegate_hostname.address_list)) {
5093+
free_address_list(action->data.delegate_hostname.address_list);
5094+
}
5095+
free((char *)action->data.delegate_hostname.hostname);
5096+
break;
50685097
case ACTION_DELEGATE_HOSTNAME_REMOVE:
50695098
_mdns_delegate_hostname_remove(action->data.delegate_hostname.hostname);
50705099
free((char *)action->data.delegate_hostname.hostname);
@@ -5634,6 +5663,36 @@ esp_err_t mdns_delegate_hostname_remove(const char *hostname)
56345663
return ESP_OK;
56355664
}
56365665

5666+
esp_err_t mdns_delegate_hostname_set_address(const char *hostname, const mdns_ip_addr_t *address_list)
5667+
{
5668+
if (!_mdns_server) {
5669+
return ESP_ERR_INVALID_STATE;
5670+
}
5671+
if (_str_null_or_empty(hostname) || strlen(hostname) > (MDNS_NAME_BUF_LEN - 1)) {
5672+
return ESP_ERR_INVALID_ARG;
5673+
}
5674+
char *new_hostname = strndup(hostname, MDNS_NAME_BUF_LEN - 1);
5675+
if (!new_hostname) {
5676+
return ESP_ERR_NO_MEM;
5677+
}
5678+
5679+
mdns_action_t *action = (mdns_action_t *)malloc(sizeof(mdns_action_t));
5680+
if (!action) {
5681+
HOOK_MALLOC_FAILED;
5682+
free(new_hostname);
5683+
return ESP_ERR_NO_MEM;
5684+
}
5685+
action->type = ACTION_DELEGATE_HOSTNAME_SET_ADDR;
5686+
action->data.delegate_hostname.hostname = new_hostname;
5687+
action->data.delegate_hostname.address_list = copy_address_list(address_list);
5688+
if (xQueueSend(_mdns_server->action_queue, &action, (TickType_t)0) != pdPASS) {
5689+
free(new_hostname);
5690+
free(action);
5691+
return ESP_ERR_NO_MEM;
5692+
}
5693+
return ESP_OK;
5694+
}
5695+
56375696
bool mdns_hostname_exists(const char *hostname)
56385697
{
56395698
return _hostname_is_ours(hostname);

components/mdns/private_include/mdns_private.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ typedef enum {
196196
ACTION_TASK_STOP,
197197
ACTION_DELEGATE_HOSTNAME_ADD,
198198
ACTION_DELEGATE_HOSTNAME_REMOVE,
199+
ACTION_DELEGATE_HOSTNAME_SET_ADDR,
199200
ACTION_MAX
200201
} mdns_action_type_t;
201202

0 commit comments

Comments
 (0)