Skip to content

Commit 7437d31

Browse files
authored
Merge pull request #574 from DejinChen/fix/remove_same_protocol_service
fix(mdns): remove same protocol services with different instances
2 parents d20255a + 042533a commit 7437d31

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
@@ -5300,28 +5300,57 @@ static void _mdns_execute_action(mdns_action_t *action)
53005300
break;
53015301
case ACTION_SERVICE_DEL:
53025302
a = _mdns_server->services;
5303-
if (action->data.srv_del.service) {
5304-
if (_mdns_server->services == action->data.srv_del.service) {
5305-
_mdns_server->services = a->next;
5306-
_mdns_send_bye(&a, 1, false);
5307-
_mdns_remove_scheduled_service_packets(a->service);
5308-
_mdns_free_service(a->service);
5309-
free(a);
5310-
} else {
5311-
while (a->next && a->next != action->data.srv_del.service) {
5312-
a = a->next;
5303+
mdns_srv_item_t *b = a;
5304+
if (action->data.srv_del.instance) {
5305+
while (a) {
5306+
if (_mdns_service_match_instance(a->service, action->data.srv_del.instance,
5307+
action->data.srv_del.service, action->data.srv_del.proto,
5308+
action->data.srv_del.hostname)) {
5309+
if (_mdns_server->services != a) {
5310+
b->next = a->next;
5311+
} else {
5312+
_mdns_server->services = a->next;
5313+
}
5314+
_mdns_send_bye(&a, 1, false);
5315+
_mdns_remove_scheduled_service_packets(a->service);
5316+
_mdns_free_service(a->service);
5317+
free(a);
5318+
break;
53135319
}
5314-
if (a->next == action->data.srv_del.service) {
5315-
mdns_srv_item_t *b = a->next;
5316-
a->next = a->next->next;
5317-
_mdns_send_bye(&b, 1, false);
5318-
_mdns_remove_scheduled_service_packets(b->service);
5319-
_mdns_free_service(b->service);
5320-
free(b);
5320+
b = a;
5321+
a = a->next;
5322+
}
5323+
} else {
5324+
while (a) {
5325+
if (_mdns_service_match(a->service, action->data.srv_del.service, action->data.srv_del.proto,
5326+
action->data.srv_del.hostname)) {
5327+
if (_mdns_server->services != a) {
5328+
b->next = a->next;
5329+
_mdns_send_bye(&a, 1, false);
5330+
_mdns_remove_scheduled_service_packets(a->service);
5331+
_mdns_free_service(a->service);
5332+
free(a);
5333+
a = b->next;
5334+
continue;
5335+
} else {
5336+
_mdns_server->services = a->next;
5337+
_mdns_send_bye(&a, 1, false);
5338+
_mdns_remove_scheduled_service_packets(a->service);
5339+
_mdns_free_service(a->service);
5340+
free(a);
5341+
a = _mdns_server->services;
5342+
b = a;
5343+
continue;
5344+
}
53215345
}
5346+
b = a;
5347+
a = a->next;
53225348
}
53235349
}
5324-
5350+
free((char *)action->data.srv_del.instance);
5351+
free((char *)action->data.srv_del.service);
5352+
free((char *)action->data.srv_del.proto);
5353+
free((char *)action->data.srv_del.hostname);
53255354
break;
53265355
case ACTION_SERVICES_CLEAR:
53275356
_mdns_send_final_bye(false);
@@ -6501,6 +6530,16 @@ esp_err_t mdns_service_subtype_add_for_host(const char *instance_name, const cha
65016530
if (!s) {
65026531
return ESP_ERR_NOT_FOUND;
65036532
}
6533+
6534+
mdns_subtype_t *srv_subtype = s->service->subtype;
6535+
while (srv_subtype) {
6536+
if (strcmp(srv_subtype->subtype, subtype) == 0) {
6537+
// The same subtype has already been added
6538+
return ESP_ERR_INVALID_ARG;
6539+
}
6540+
srv_subtype = srv_subtype->next;
6541+
}
6542+
65046543
mdns_action_t *action = (mdns_action_t *)malloc(sizeof(mdns_action_t));
65056544
if (!action) {
65066545
HOOK_MALLOC_FAILED;
@@ -6589,12 +6628,40 @@ esp_err_t mdns_service_remove_for_host(const char *instance, const char *service
65896628
return ESP_ERR_NO_MEM;
65906629
}
65916630
action->type = ACTION_SERVICE_DEL;
6592-
action->data.srv_del.service = s;
6631+
action->data.srv_del.instance = NULL;
6632+
action->data.srv_del.hostname = NULL;
6633+
if (!_str_null_or_empty(instance)) {
6634+
action->data.srv_del.instance = strndup(instance, MDNS_NAME_BUF_LEN - 1);
6635+
if (!action->data.srv_del.instance) {
6636+
goto fail;
6637+
}
6638+
}
6639+
6640+
if (!_str_null_or_empty(hostname)) {
6641+
action->data.srv_del.hostname = strndup(hostname, MDNS_NAME_BUF_LEN - 1);
6642+
if (!action->data.srv_del.hostname) {
6643+
goto fail;
6644+
}
6645+
}
6646+
6647+
action->data.srv_del.service = strndup(service, MDNS_NAME_BUF_LEN - 1);
6648+
action->data.srv_del.proto = strndup(proto, MDNS_NAME_BUF_LEN - 1);
6649+
if (!action->data.srv_del.service || !action->data.srv_del.proto) {
6650+
goto fail;
6651+
}
6652+
65936653
if (xQueueSend(_mdns_server->action_queue, &action, (TickType_t)0) != pdPASS) {
6594-
free(action);
6595-
return ESP_ERR_NO_MEM;
6654+
goto fail;
65966655
}
65976656
return ESP_OK;
6657+
6658+
fail:
6659+
free((char *)action->data.srv_del.instance);
6660+
free((char *)action->data.srv_del.service);
6661+
free((char *)action->data.srv_del.proto);
6662+
free((char *)action->data.srv_del.hostname);
6663+
free(action);
6664+
return ESP_ERR_NO_MEM;
65986665
}
65996666

66006667
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)