From ec9dbd922d639d1c086e620ec7ff0d67193fac7d Mon Sep 17 00:00:00 2001 From: David Cermak Date: Fri, 21 Jan 2022 11:55:08 +0100 Subject: [PATCH] mdns: Support for null-value TXT records Closes https://github.com/espressif/esp-idf/issues/8267 --- components/mdns/mdns.c | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/components/mdns/mdns.c b/components/mdns/mdns.c index 2d37fc5997..0cf95c7176 100644 --- a/components/mdns/mdns.c +++ b/components/mdns/mdns.c @@ -702,12 +702,8 @@ static uint16_t _mdns_append_txt_record(uint8_t * packet, uint16_t * index, mdns char * tmp; mdns_txt_linked_item_t * txt = service->txt; while (txt) { - uint8_t txt_data_len = strlen(txt->key) + txt->value_len + 1 /* + '=' char */; - tmp = (char *)malloc(txt_data_len + 1 /* + '\0' term-char in sprintf() */); - if (tmp) { - int offset = sprintf(tmp, "%s=", txt->key); - memcpy(tmp + offset, txt->value, txt->value_len); - uint8_t l = _mdns_append_string_with_len(packet, index, tmp, txt_data_len); + if (asprintf(&tmp, "%s%s%.*s", txt->key, txt->value ? "=" : "", txt->value_len, txt->value) > 0) { + uint8_t l = _mdns_append_string(packet, index, tmp); free(tmp); if (!l) { return 0; @@ -2420,7 +2416,7 @@ static int _mdns_check_txt_collision(mdns_service_t * service, const uint8_t * d mdns_txt_linked_item_t * txt = service->txt; while (txt) { - data_len += 2 + strlen(txt->key) + strlen(txt->value); + data_len += 1 /* record-len */ + strlen(txt->key) + txt->value_len + (txt->value ? 1 : 0 /* "=" */); txt = txt->next; } @@ -2436,9 +2432,7 @@ static int _mdns_check_txt_collision(mdns_service_t * service, const uint8_t * d txt = service->txt; while (txt) { - tmp = (char *)malloc(2 + strlen(txt->key) + strlen(txt->value)); - if (tmp) { - sprintf(tmp, "%s=%s", txt->key, txt->value); + if (asprintf(&tmp, "%s%s%.*s", txt->key, txt->value ? "=" : "", txt->value_len, txt->value) > 0) { _mdns_append_string(ours, &index, tmp); free(tmp); } else { @@ -5146,7 +5140,7 @@ esp_err_t mdns_service_txt_item_set_for_host_with_explicit_value_len(const char const char *value, uint8_t value_len) { if (!_mdns_server || !_mdns_server->services || _str_null_or_empty(service) || _str_null_or_empty(proto) || - _str_null_or_empty(key) || !value) { + _str_null_or_empty(key) || (!value && value_len)) { return ESP_ERR_INVALID_ARG; } mdns_srv_item_t *s = _mdns_get_service_item(service, proto, hostname); @@ -5166,14 +5160,19 @@ esp_err_t mdns_service_txt_item_set_for_host_with_explicit_value_len(const char free(action); return ESP_ERR_NO_MEM; } - action->data.srv_txt_set.value = (char *)malloc(value_len); - if (!action->data.srv_txt_set.value) { - free(action->data.srv_txt_set.key); - free(action); - return ESP_ERR_NO_MEM; + if (value_len > 0) { + action->data.srv_txt_set.value = (char *)malloc(value_len); + if (!action->data.srv_txt_set.value) { + free(action->data.srv_txt_set.key); + free(action); + return ESP_ERR_NO_MEM; + } + memcpy(action->data.srv_txt_set.value, value, value_len); + action->data.srv_txt_set.value_len = value_len; + } else { + action->data.srv_txt_set.value = NULL; + action->data.srv_txt_set.value_len = 0; } - memcpy(action->data.srv_txt_set.value, value, value_len); - action->data.srv_txt_set.value_len = value_len; if (xQueueSend(_mdns_server->action_queue, &action, (portTickType)0) != pdPASS) { free(action->data.srv_txt_set.key); free(action->data.srv_txt_set.value);