Skip to content

Commit 99d5fb2

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

File tree

2 files changed

+13
-29
lines changed

2 files changed

+13
-29
lines changed

components/mdns/mdns.c

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5200,11 +5200,6 @@ static void _mdns_execute_action(mdns_action_t *action)
52005200
action->data.srv_instance.service->service->instance = action->data.srv_instance.instance;
52015201
_mdns_probe_all_pcbs(&action->data.srv_instance.service, 1, false, false);
52025202

5203-
break;
5204-
case ACTION_SERVICE_PORT_SET:
5205-
action->data.srv_port.service->service->port = action->data.srv_port.port;
5206-
_mdns_announce_all_pcbs(&action->data.srv_port.service, 1, true);
5207-
52085203
break;
52095204
case ACTION_SERVICE_TXT_REPLACE:
52105205
service = action->data.srv_txt_replace.service->service;
@@ -6233,40 +6228,30 @@ static mdns_result_t *_mdns_lookup_service(const char *instance, const char *ser
62336228
return NULL;
62346229
}
62356230

6236-
esp_err_t mdns_service_port_set_for_host(const char *instance, const char *service, const char *proto, const char *hostname, uint16_t port)
6231+
esp_err_t mdns_service_port_set_for_host(const char *instance, const char *service, const char *proto, const char *host, uint16_t port)
62376232
{
62386233
MDNS_SERVICE_LOCK();
6239-
if (!_mdns_server || !_mdns_server->services || _str_null_or_empty(service) || _str_null_or_empty(proto) || !port) {
6240-
MDNS_SERVICE_UNLOCK();
6241-
return ESP_ERR_INVALID_ARG;
6242-
}
6234+
esp_err_t ret = ESP_OK;
6235+
const char *hostname = host ? host : _mdns_server->hostname;
6236+
ESP_GOTO_ON_FALSE(_mdns_server && _mdns_server->services && !_str_null_or_empty(service) && !_str_null_or_empty(proto) && port,
6237+
ESP_ERR_INVALID_ARG, err, TAG, "Invalid state or arguments");
62436238
mdns_srv_item_t *s = _mdns_get_service_item_instance(instance, service, proto, hostname);
6244-
MDNS_SERVICE_UNLOCK();
6245-
if (!s) {
6246-
return ESP_ERR_NOT_FOUND;
6247-
}
6239+
ESP_GOTO_ON_FALSE(s, ESP_ERR_NOT_FOUND, err, TAG, "Service doesn't exist");
62486240

6249-
mdns_action_t *action = (mdns_action_t *)malloc(sizeof(mdns_action_t));
6250-
if (!action) {
6251-
HOOK_MALLOC_FAILED;
6252-
return ESP_ERR_NO_MEM;
6253-
}
6254-
action->type = ACTION_SERVICE_PORT_SET;
6255-
action->data.srv_port.service = s;
6256-
action->data.srv_port.port = port;
6257-
if (xQueueSend(_mdns_server->action_queue, &action, (TickType_t)0) != pdPASS) {
6258-
free(action);
6259-
return ESP_ERR_NO_MEM;
6260-
}
6261-
return ESP_OK;
6241+
s->service->port = port;
6242+
_mdns_announce_all_pcbs(&s, 1, true);
6243+
6244+
err:
6245+
MDNS_SERVICE_UNLOCK();
6246+
return ret;
62626247
}
62636248

62646249
esp_err_t mdns_service_port_set(const char *service, const char *proto, uint16_t port)
62656250
{
62666251
if (!_mdns_server) {
62676252
return ESP_ERR_INVALID_STATE;
62686253
}
6269-
return mdns_service_port_set_for_host(NULL, service, proto, _mdns_server->hostname, port);
6254+
return mdns_service_port_set_for_host(NULL, service, proto, NULL, port);
62706255
}
62716256

62726257
esp_err_t mdns_service_txt_set_for_host(const char *instance, const char *service, const char *proto, const char *hostname,

components/mdns/private_include/mdns_private.h

Lines changed: 0 additions & 1 deletion
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_PORT_SET,
192191
ACTION_SERVICE_TXT_REPLACE,
193192
ACTION_SERVICE_TXT_SET,
194193
ACTION_SERVICE_TXT_DEL,

0 commit comments

Comments
 (0)