Skip to content

Commit a927bf3

Browse files
committed
fix(mdns): Fix API races while setting txt for services
Fixes **API race issue** (described in 8a69050) for API mdns_service_txt_set_for_host()
1 parent 99d5fb2 commit a927bf3

File tree

2 files changed

+18
-49
lines changed

2 files changed

+18
-49
lines changed

components/mdns/mdns.c

Lines changed: 18 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5111,9 +5111,6 @@ static void _mdns_free_action(mdns_action_t *action)
51115111
case ACTION_SERVICE_INSTANCE_SET:
51125112
free(action->data.srv_instance.instance);
51135113
break;
5114-
case ACTION_SERVICE_TXT_REPLACE:
5115-
_mdns_free_linked_txt(action->data.srv_txt_replace.txt);
5116-
break;
51175114
case ACTION_SERVICE_TXT_SET:
51185115
free(action->data.srv_txt_set.key);
51195116
free(action->data.srv_txt_set.value);
@@ -5200,15 +5197,6 @@ static void _mdns_execute_action(mdns_action_t *action)
52005197
action->data.srv_instance.service->service->instance = action->data.srv_instance.instance;
52015198
_mdns_probe_all_pcbs(&action->data.srv_instance.service, 1, false, false);
52025199

5203-
break;
5204-
case ACTION_SERVICE_TXT_REPLACE:
5205-
service = action->data.srv_txt_replace.service->service;
5206-
txt = service->txt;
5207-
service->txt = NULL;
5208-
_mdns_free_linked_txt(txt);
5209-
service->txt = action->data.srv_txt_replace.txt;
5210-
_mdns_announce_all_pcbs(&action->data.srv_txt_replace.service, 1, false);
5211-
52125200
break;
52135201
case ACTION_SERVICE_TXT_SET:
52145202
service = action->data.srv_txt_set.service->service;
@@ -6254,52 +6242,42 @@ esp_err_t mdns_service_port_set(const char *service, const char *proto, uint16_t
62546242
return mdns_service_port_set_for_host(NULL, service, proto, NULL, port);
62556243
}
62566244

6257-
esp_err_t mdns_service_txt_set_for_host(const char *instance, const char *service, const char *proto, const char *hostname,
6258-
mdns_txt_item_t txt[], uint8_t num_items)
6245+
esp_err_t mdns_service_txt_set_for_host(const char *instance, const char *service, const char *proto, const char *host,
6246+
mdns_txt_item_t txt_items[], uint8_t num_items)
62596247
{
62606248
MDNS_SERVICE_LOCK();
6261-
if (!_mdns_server || !_mdns_server->services || _str_null_or_empty(service) || _str_null_or_empty(proto) || (num_items && txt == NULL)) {
6262-
MDNS_SERVICE_UNLOCK();
6263-
return ESP_ERR_INVALID_ARG;
6264-
}
6249+
esp_err_t ret = ESP_OK;
6250+
const char *hostname = host ? host : _mdns_server->hostname;
6251+
ESP_GOTO_ON_FALSE(_mdns_server && _mdns_server->services && !_str_null_or_empty(service) && !_str_null_or_empty(proto) && !(num_items && txt_items == NULL),
6252+
ESP_ERR_INVALID_ARG, err, TAG, "Invalid state or arguments");
62656253
mdns_srv_item_t *s = _mdns_get_service_item_instance(instance, service, proto, hostname);
6266-
MDNS_SERVICE_UNLOCK();
6267-
if (!s) {
6268-
return ESP_ERR_NOT_FOUND;
6269-
}
6254+
ESP_GOTO_ON_FALSE(s, ESP_ERR_NOT_FOUND, err, TAG, "Service doesn't exist");
62706255

62716256
mdns_txt_linked_item_t *new_txt = NULL;
62726257
if (num_items) {
6273-
new_txt = _mdns_allocate_txt(num_items, txt);
6258+
new_txt = _mdns_allocate_txt(num_items, txt_items);
62746259
if (!new_txt) {
62756260
return ESP_ERR_NO_MEM;
62766261
}
62776262
}
6263+
mdns_service_t *srv = s->service;
6264+
mdns_txt_linked_item_t *txt = srv->txt;
6265+
srv->txt = NULL;
6266+
_mdns_free_linked_txt(txt);
6267+
srv->txt = new_txt;
6268+
_mdns_announce_all_pcbs(&s, 1, false);
62786269

6279-
mdns_action_t *action = (mdns_action_t *)malloc(sizeof(mdns_action_t));
6280-
if (!action) {
6281-
HOOK_MALLOC_FAILED;
6282-
_mdns_free_linked_txt(new_txt);
6283-
return ESP_ERR_NO_MEM;
6284-
}
6285-
action->type = ACTION_SERVICE_TXT_REPLACE;
6286-
action->data.srv_txt_replace.service = s;
6287-
action->data.srv_txt_replace.txt = new_txt;
6288-
6289-
if (xQueueSend(_mdns_server->action_queue, &action, (TickType_t)0) != pdPASS) {
6290-
_mdns_free_linked_txt(new_txt);
6291-
free(action);
6292-
return ESP_ERR_NO_MEM;
6293-
}
6294-
return ESP_OK;
6270+
err:
6271+
MDNS_SERVICE_UNLOCK();
6272+
return ret;
62956273
}
62966274

62976275
esp_err_t mdns_service_txt_set(const char *service, const char *proto, mdns_txt_item_t txt[], uint8_t num_items)
62986276
{
62996277
if (!_mdns_server) {
63006278
return ESP_ERR_INVALID_STATE;
63016279
}
6302-
return mdns_service_txt_set_for_host(NULL, service, proto, _mdns_server->hostname, txt, num_items);
6280+
return mdns_service_txt_set_for_host(NULL, service, proto, NULL, txt, num_items);
63036281
}
63046282

63056283
esp_err_t mdns_service_txt_item_set_for_host_with_explicit_value_len(const char *instance, const char *service, const char *proto,

components/mdns/private_include/mdns_private.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,6 @@ typedef enum {
188188
ACTION_HOSTNAME_SET,
189189
ACTION_INSTANCE_SET,
190190
ACTION_SERVICE_INSTANCE_SET,
191-
ACTION_SERVICE_TXT_REPLACE,
192191
ACTION_SERVICE_TXT_SET,
193192
ACTION_SERVICE_TXT_DEL,
194193
ACTION_SERVICE_SUBTYPE_ADD,
@@ -447,14 +446,6 @@ typedef struct {
447446
mdns_srv_item_t *service;
448447
char *instance;
449448
} srv_instance;
450-
struct {
451-
mdns_srv_item_t *service;
452-
uint16_t port;
453-
} srv_port;
454-
struct {
455-
mdns_srv_item_t *service;
456-
mdns_txt_linked_item_t *txt;
457-
} srv_txt_replace;
458449
struct {
459450
mdns_srv_item_t *service;
460451
char *key;

0 commit comments

Comments
 (0)