Skip to content

Commit 643dc6d

Browse files
committed
fix(mdns): Fix API races setting instance name for services
Fixes **API race issue** (described in 8a69050) for API mdns_service_instance_name_set_for_host()
1 parent f9f234c commit 643dc6d

File tree

2 files changed

+27
-55
lines changed

2 files changed

+27
-55
lines changed

components/mdns/mdns.c

Lines changed: 27 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5108,9 +5108,6 @@ static void _mdns_free_action(mdns_action_t *action)
51085108
case ACTION_INSTANCE_SET:
51095109
free(action->data.instance);
51105110
break;
5111-
case ACTION_SERVICE_INSTANCE_SET:
5112-
free(action->data.srv_instance.instance);
5113-
break;
51145111
case ACTION_SEARCH_ADD:
51155112
//fallthrough
51165113
case ACTION_SEARCH_SEND:
@@ -5172,15 +5169,6 @@ static void _mdns_execute_action(mdns_action_t *action)
51725169
_mdns_server->instance = action->data.instance;
51735170
_mdns_restart_all_pcbs_no_instance();
51745171

5175-
break;
5176-
case ACTION_SERVICE_INSTANCE_SET:
5177-
if (action->data.srv_instance.service->service->instance) {
5178-
_mdns_send_bye(&action->data.srv_instance.service, 1, false);
5179-
free((char *)action->data.srv_instance.service->service->instance);
5180-
}
5181-
action->data.srv_instance.service->service->instance = action->data.srv_instance.instance;
5182-
_mdns_probe_all_pcbs(&action->data.srv_instance.service, 1, false, false);
5183-
51845172
break;
51855173
case ACTION_SERVICES_CLEAR:
51865174
_mdns_send_final_bye(false);
@@ -6344,65 +6332,54 @@ esp_err_t mdns_service_subtype_add_for_host(const char *instance_name, const cha
63446332

63456333
mdns_service_t *srv = s->service;
63466334
mdns_subtype_t *subtype_item = (mdns_subtype_t *)malloc(sizeof(mdns_subtype_t));
6347-
ESP_GOTO_ON_FALSE(subtype_item, ESP_ERR_NO_MEM, err, TAG, "Out of memory");
6335+
ESP_GOTO_ON_FALSE(subtype_item, ESP_ERR_NO_MEM, out_of_mem, TAG, "Out of memory");
63486336
subtype_item->subtype = strdup(subtype);
6349-
ESP_GOTO_ON_FALSE(subtype_item->subtype, ESP_ERR_NO_MEM, err, TAG, "Out of memory");
6337+
ESP_GOTO_ON_FALSE(subtype_item->subtype, ESP_ERR_NO_MEM, out_of_mem, TAG, "Out of memory");
63506338
subtype_item->next = srv->subtype;
63516339
srv->subtype = subtype_item;
63526340

63536341
err:
63546342
MDNS_SERVICE_UNLOCK();
6355-
if (ret == ESP_ERR_NO_MEM) {
6356-
HOOK_MALLOC_FAILED;
6357-
}
6343+
return ret;
6344+
out_of_mem:
6345+
MDNS_SERVICE_UNLOCK();
6346+
HOOK_MALLOC_FAILED;
6347+
free(subtype_item);
63586348
return ret;
63596349
}
63606350

6361-
esp_err_t mdns_service_instance_name_set_for_host(const char *instance_old, const char *service, const char *proto, const char *hostname,
6351+
esp_err_t mdns_service_instance_name_set_for_host(const char *instance_old, const char *service, const char *proto, const char *host,
63626352
const char *instance)
63636353
{
63646354
MDNS_SERVICE_LOCK();
6365-
if (!_mdns_server || !_mdns_server->services || _str_null_or_empty(service) || _str_null_or_empty(proto)) {
6366-
MDNS_SERVICE_UNLOCK();
6367-
return ESP_ERR_INVALID_ARG;
6368-
}
6369-
if (_str_null_or_empty(instance) || strlen(instance) > (MDNS_NAME_BUF_LEN - 1)) {
6370-
MDNS_SERVICE_UNLOCK();
6371-
return ESP_ERR_INVALID_ARG;
6372-
}
6355+
esp_err_t ret = ESP_OK;
6356+
const char *hostname = host ? host : _mdns_server->hostname;
6357+
6358+
ESP_GOTO_ON_FALSE(_mdns_server && _mdns_server->services && !_str_null_or_empty(service) && !_str_null_or_empty(proto) &&
6359+
!_str_null_or_empty(instance) && strlen(instance) <= (MDNS_NAME_BUF_LEN - 1), ESP_ERR_INVALID_ARG, err, TAG, "Invalid state or arguments");
6360+
63736361
mdns_srv_item_t *s = _mdns_get_service_item_instance(instance_old, service, proto, hostname);
6374-
MDNS_SERVICE_UNLOCK();
6375-
if (!s) {
6376-
return ESP_ERR_NOT_FOUND;
6377-
}
6378-
char *new_instance = strndup(instance, MDNS_NAME_BUF_LEN - 1);
6379-
if (!new_instance) {
6380-
return ESP_ERR_NO_MEM;
6381-
}
6362+
ESP_GOTO_ON_FALSE(s, ESP_ERR_NOT_FOUND, err, TAG, "Service doesn't exist");
63826363

6383-
mdns_action_t *action = (mdns_action_t *)malloc(sizeof(mdns_action_t));
6384-
if (!action) {
6385-
HOOK_MALLOC_FAILED;
6386-
free(new_instance);
6387-
return ESP_ERR_NO_MEM;
6364+
if (s->service->instance) {
6365+
_mdns_send_bye(&s, 1, false);
6366+
free((char *)s->service->instance);
63886367
}
6389-
action->type = ACTION_SERVICE_INSTANCE_SET;
6390-
action->data.srv_instance.service = s;
6391-
action->data.srv_instance.instance = new_instance;
6392-
if (xQueueSend(_mdns_server->action_queue, &action, (TickType_t)0) != pdPASS) {
6393-
free(new_instance);
6394-
free(action);
6395-
return ESP_ERR_NO_MEM;
6396-
}
6397-
return ESP_OK;
6368+
s->service->instance = strndup(instance, MDNS_NAME_BUF_LEN - 1);
6369+
ESP_GOTO_ON_FALSE(s->service->instance, ESP_ERR_NO_MEM, err, TAG, "Out of memory");
6370+
_mdns_probe_all_pcbs(&s, 1, false, false);
6371+
6372+
err:
6373+
MDNS_SERVICE_UNLOCK();
6374+
return ret;
63986375
}
63996376

64006377
esp_err_t mdns_service_instance_name_set(const char *service, const char *proto, const char *instance)
64016378
{
64026379
if (!_mdns_server) {
64036380
return ESP_ERR_INVALID_STATE;
64046381
}
6405-
return mdns_service_instance_name_set_for_host(NULL, service, proto, _mdns_server->hostname, instance);
6382+
return mdns_service_instance_name_set_for_host(NULL, service, proto, NULL, instance);
64066383
}
64076384

64086385
esp_err_t mdns_service_remove_for_host(const char *instance, const char *service, const char *proto, const char *host)
@@ -6413,7 +6390,7 @@ esp_err_t mdns_service_remove_for_host(const char *instance, const char *service
64136390
ESP_GOTO_ON_FALSE(_mdns_server && _mdns_server->services && !_str_null_or_empty(service) && !_str_null_or_empty(proto),
64146391
ESP_ERR_INVALID_ARG, err, TAG, "Invalid state or arguments");
64156392
mdns_srv_item_t *s = _mdns_get_service_item_instance(instance, service, proto, hostname);
6416-
ESP_GOTO_ON_FALSE(s, ESP_ERR_INVALID_ARG, err, TAG, "Service doesn't exist");
6393+
ESP_GOTO_ON_FALSE(s, ESP_ERR_NOT_FOUND, err, TAG, "Service doesn't exist");
64176394

64186395
mdns_srv_item_t *a = _mdns_server->services;
64196396
mdns_srv_item_t *b = a;

components/mdns/private_include/mdns_private.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,6 @@ typedef enum {
187187
ACTION_SYSTEM_EVENT,
188188
ACTION_HOSTNAME_SET,
189189
ACTION_INSTANCE_SET,
190-
ACTION_SERVICE_INSTANCE_SET,
191190
ACTION_SERVICES_CLEAR,
192191
ACTION_SEARCH_ADD,
193192
ACTION_SEARCH_SEND,
@@ -439,10 +438,6 @@ typedef struct {
439438
mdns_if_t interface;
440439
mdns_event_actions_t event_action;
441440
} sys_event;
442-
struct {
443-
mdns_srv_item_t *service;
444-
char *instance;
445-
} srv_instance;
446441
struct {
447442
mdns_search_once_t *search;
448443
} search_add;

0 commit comments

Comments
 (0)