Skip to content

Commit 042533a

Browse files
committed
fix(mdns): remove same protocol services with different instances
1 parent 3f12ef6 commit 042533a

File tree

2 files changed

+92
-22
lines changed

2 files changed

+92
-22
lines changed

components/mdns/mdns.c

Lines changed: 88 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5200,28 +5200,57 @@ static void _mdns_execute_action(mdns_action_t *action)
52005200
break;
52015201
case ACTION_SERVICE_DEL:
52025202
a = _mdns_server->services;
5203-
if (action->data.srv_del.service) {
5204-
if (_mdns_server->services == action->data.srv_del.service) {
5205-
_mdns_server->services = a->next;
5206-
_mdns_send_bye(&a, 1, false);
5207-
_mdns_remove_scheduled_service_packets(a->service);
5208-
_mdns_free_service(a->service);
5209-
free(a);
5210-
} else {
5211-
while (a->next && a->next != action->data.srv_del.service) {
5212-
a = a->next;
5203+
mdns_srv_item_t *b = a;
5204+
if (action->data.srv_del.instance) {
5205+
while (a) {
5206+
if (_mdns_service_match_instance(a->service, action->data.srv_del.instance,
5207+
action->data.srv_del.service, action->data.srv_del.proto,
5208+
action->data.srv_del.hostname)) {
5209+
if (_mdns_server->services != a) {
5210+
b->next = a->next;
5211+
} else {
5212+
_mdns_server->services = a->next;
5213+
}
5214+
_mdns_send_bye(&a, 1, false);
5215+
_mdns_remove_scheduled_service_packets(a->service);
5216+
_mdns_free_service(a->service);
5217+
free(a);
5218+
break;
52135219
}
5214-
if (a->next == action->data.srv_del.service) {
5215-
mdns_srv_item_t *b = a->next;
5216-
a->next = a->next->next;
5217-
_mdns_send_bye(&b, 1, false);
5218-
_mdns_remove_scheduled_service_packets(b->service);
5219-
_mdns_free_service(b->service);
5220-
free(b);
5220+
b = a;
5221+
a = a->next;
5222+
}
5223+
} else {
5224+
while (a) {
5225+
if (_mdns_service_match(a->service, action->data.srv_del.service, action->data.srv_del.proto,
5226+
action->data.srv_del.hostname)) {
5227+
if (_mdns_server->services != a) {
5228+
b->next = a->next;
5229+
_mdns_send_bye(&a, 1, false);
5230+
_mdns_remove_scheduled_service_packets(a->service);
5231+
_mdns_free_service(a->service);
5232+
free(a);
5233+
a = b->next;
5234+
continue;
5235+
} else {
5236+
_mdns_server->services = a->next;
5237+
_mdns_send_bye(&a, 1, false);
5238+
_mdns_remove_scheduled_service_packets(a->service);
5239+
_mdns_free_service(a->service);
5240+
free(a);
5241+
a = _mdns_server->services;
5242+
b = a;
5243+
continue;
5244+
}
52215245
}
5246+
b = a;
5247+
a = a->next;
52225248
}
52235249
}
5224-
5250+
free((char *)action->data.srv_del.instance);
5251+
free((char *)action->data.srv_del.service);
5252+
free((char *)action->data.srv_del.proto);
5253+
free((char *)action->data.srv_del.hostname);
52255254
break;
52265255
case ACTION_SERVICES_CLEAR:
52275256
_mdns_send_final_bye(false);
@@ -6401,6 +6430,16 @@ esp_err_t mdns_service_subtype_add_for_host(const char *instance_name, const cha
64016430
if (!s) {
64026431
return ESP_ERR_NOT_FOUND;
64036432
}
6433+
6434+
mdns_subtype_t *srv_subtype = s->service->subtype;
6435+
while (srv_subtype) {
6436+
if (strcmp(srv_subtype->subtype, subtype) == 0) {
6437+
// The same subtype has already been added
6438+
return ESP_ERR_INVALID_ARG;
6439+
}
6440+
srv_subtype = srv_subtype->next;
6441+
}
6442+
64046443
mdns_action_t *action = (mdns_action_t *)malloc(sizeof(mdns_action_t));
64056444
if (!action) {
64066445
HOOK_MALLOC_FAILED;
@@ -6489,12 +6528,40 @@ esp_err_t mdns_service_remove_for_host(const char *instance, const char *service
64896528
return ESP_ERR_NO_MEM;
64906529
}
64916530
action->type = ACTION_SERVICE_DEL;
6492-
action->data.srv_del.service = s;
6531+
action->data.srv_del.instance = NULL;
6532+
action->data.srv_del.hostname = NULL;
6533+
if (!_str_null_or_empty(instance)) {
6534+
action->data.srv_del.instance = strndup(instance, MDNS_NAME_BUF_LEN - 1);
6535+
if (!action->data.srv_del.instance) {
6536+
goto fail;
6537+
}
6538+
}
6539+
6540+
if (!_str_null_or_empty(hostname)) {
6541+
action->data.srv_del.hostname = strndup(hostname, MDNS_NAME_BUF_LEN - 1);
6542+
if (!action->data.srv_del.hostname) {
6543+
goto fail;
6544+
}
6545+
}
6546+
6547+
action->data.srv_del.service = strndup(service, MDNS_NAME_BUF_LEN - 1);
6548+
action->data.srv_del.proto = strndup(proto, MDNS_NAME_BUF_LEN - 1);
6549+
if (!action->data.srv_del.service || !action->data.srv_del.proto) {
6550+
goto fail;
6551+
}
6552+
64936553
if (xQueueSend(_mdns_server->action_queue, &action, (TickType_t)0) != pdPASS) {
6494-
free(action);
6495-
return ESP_ERR_NO_MEM;
6554+
goto fail;
64966555
}
64976556
return ESP_OK;
6557+
6558+
fail:
6559+
free((char *)action->data.srv_del.instance);
6560+
free((char *)action->data.srv_del.service);
6561+
free((char *)action->data.srv_del.proto);
6562+
free((char *)action->data.srv_del.hostname);
6563+
free(action);
6564+
return ESP_ERR_NO_MEM;
64986565
}
64996566

65006567
esp_err_t mdns_service_remove(const char *service_type, const char *proto)

components/mdns/private_include/mdns_private.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,10 @@ typedef struct {
450450
mdns_srv_item_t *service;
451451
} srv_add;
452452
struct {
453-
mdns_srv_item_t *service;
453+
char *instance;
454+
char *service;
455+
char *proto;
456+
char *hostname;
454457
} srv_del;
455458
struct {
456459
mdns_srv_item_t *service;

0 commit comments

Comments
 (0)