Skip to content

Commit

Permalink
Merge pull request #730 from david-cermak/fix/mdns_minor
Browse files Browse the repository at this point in the history
[mdns]: Fixed some minor bugs
  • Loading branch information
david-cermak authored Jan 15, 2025
2 parents 4394f84 + 3d8835c commit 9b74256
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 22 deletions.
10 changes: 9 additions & 1 deletion components/mdns/include/mdns.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand All @@ -10,6 +10,7 @@
extern "C" {
#endif

#include "sdkconfig.h"
#include <esp_netif.h>

#define MDNS_TYPE_A 0x0001
Expand All @@ -21,6 +22,13 @@ extern "C" {
#define MDNS_TYPE_NSEC 0x002F
#define MDNS_TYPE_ANY 0x00FF

#if defined(CONFIG_LWIP_IPV6) && defined(CONFIG_MDNS_RESPOND_REVERSE_QUERIES)
#define MDNS_NAME_MAX_LEN (64+4) // Need to account for IPv6 reverse queries (64 char address + ".ip6" )
#else
#define MDNS_NAME_MAX_LEN 64 // Maximum string length of hostname, instance, service and proto
#endif
#define MDNS_NAME_BUF_LEN (MDNS_NAME_MAX_LEN+1) // Maximum char buffer size to hold hostname, instance, service or proto

/**
* @brief Asynchronous query handle
*/
Expand Down
30 changes: 17 additions & 13 deletions components/mdns/mdns.c
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
/*
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <string.h>
#include <sys/param.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "freertos/semphr.h"
#include "esp_log.h"
#include "esp_event.h"
#include "esp_random.h"
#include "esp_check.h"
#include "mdns.h"
#include "mdns_private.h"
#include "mdns_networking.h"
#include "esp_log.h"
#include "esp_random.h"
#include "esp_check.h"

static void _mdns_browse_item_free(mdns_browse_t *browse);
static esp_err_t _mdns_send_browse_action(mdns_action_type_t type, mdns_browse_t *browse);
Expand Down Expand Up @@ -334,6 +332,9 @@ static mdns_host_item_t *mdns_get_host_item(const char *hostname)

static bool _mdns_can_add_more_services(void)
{
#if MDNS_MAX_SERVICES == 0
return false;
#else
mdns_srv_item_t *s = _mdns_server->services;
uint16_t service_num = 0;
while (s) {
Expand All @@ -343,8 +344,8 @@ static bool _mdns_can_add_more_services(void)
return false;
}
}

return true;
#endif
}

esp_err_t _mdns_send_rx_action(mdns_rx_packet_t *packet)
Expand Down Expand Up @@ -3485,8 +3486,9 @@ static void _mdns_result_txt_create(const uint8_t *data, size_t len, mdns_txt_it
uint16_t i = 0, y;
size_t partLen = 0;
int num_items = _mdns_txt_items_count_get(data, len);
if (num_items < 0) {
return;//error
if (num_items < 0 || num_items > SIZE_MAX / sizeof(mdns_txt_item_t)) {
// Error: num_items is incorrect (or too large to allocate)
return;
}

if (!num_items) {
Expand Down Expand Up @@ -4477,10 +4479,11 @@ void mdns_preset_if_handle_system_event(void *arg, esp_event_base_t event_base,
case IP_EVENT_GOT_IP6: {
ip_event_got_ip6_t *event = (ip_event_got_ip6_t *) event_data;
mdns_if_t mdns_if = _mdns_get_if_from_esp_netif(event->esp_netif);
if (mdns_if < MDNS_MAX_INTERFACES) {
post_mdns_enable_pcb(mdns_if, MDNS_IP_PROTOCOL_V6);
post_mdns_announce_pcb(mdns_if, MDNS_IP_PROTOCOL_V4);
if (mdns_if >= MDNS_MAX_INTERFACES) {
return;
}
post_mdns_enable_pcb(mdns_if, MDNS_IP_PROTOCOL_V6);
post_mdns_announce_pcb(mdns_if, MDNS_IP_PROTOCOL_V4);
mdns_browse_t *browse = _mdns_server->browse;
while (browse) {
_mdns_browse_send(browse, mdns_if);
Expand Down Expand Up @@ -5892,7 +5895,7 @@ esp_err_t mdns_instance_name_set(const char *instance)
esp_err_t mdns_service_add_for_host(const char *instance, const char *service, const char *proto, const char *host,
uint16_t port, mdns_txt_item_t txt[], size_t num_items)
{
if (!_mdns_server || _str_null_or_empty(service) || _str_null_or_empty(proto) || !port || !_mdns_server->hostname) {
if (!_mdns_server || _str_null_or_empty(service) || _str_null_or_empty(proto) || !_mdns_server->hostname) {
return ESP_ERR_INVALID_ARG;
}

Expand All @@ -5901,7 +5904,8 @@ esp_err_t mdns_service_add_for_host(const char *instance, const char *service, c
const char *hostname = host ? host : _mdns_server->hostname;
mdns_service_t *s = NULL;

ESP_GOTO_ON_FALSE(_mdns_can_add_more_services(), ESP_ERR_NO_MEM, err, TAG, "Cannot add more services");
ESP_GOTO_ON_FALSE(_mdns_can_add_more_services(), ESP_ERR_NO_MEM, err, TAG,
"Cannot add more services, please increase CONFIG_MDNS_MAX_SERVICES (%d)", CONFIG_MDNS_MAX_SERVICES);

mdns_srv_item_t *item = _mdns_get_service_item_instance(instance, service, proto, hostname);
ESP_GOTO_ON_FALSE(!item, ESP_ERR_INVALID_ARG, err, TAG, "Service already exists");
Expand Down
2 changes: 1 addition & 1 deletion components/mdns/mdns_networking_lwip.c
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ typedef struct {
static err_t _mdns_pcb_init_api(struct tcpip_api_call_data *api_call_msg)
{
mdns_api_call_t *msg = (mdns_api_call_t *)api_call_msg;
msg->err = _udp_pcb_init(msg->tcpip_if, msg->ip_protocol);
msg->err = _udp_pcb_init(msg->tcpip_if, msg->ip_protocol) == ESP_OK ? ERR_OK : ERR_IF;
return msg->err;
}

Expand Down
8 changes: 1 addition & 7 deletions components/mdns/private_include/mdns_private.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down Expand Up @@ -103,12 +103,6 @@
#define MDNS_PACKET_QUEUE_LEN 16 // Maximum packets that can be queued for parsing
#define MDNS_ACTION_QUEUE_LEN CONFIG_MDNS_ACTION_QUEUE_LEN // Maximum actions pending to the server
#define MDNS_TXT_MAX_LEN 1024 // Maximum string length of text data in TXT record
#if defined(CONFIG_LWIP_IPV6) && defined(CONFIG_MDNS_RESPOND_REVERSE_QUERIES)
#define MDNS_NAME_MAX_LEN (64+4) // Need to account for IPv6 reverse queries (64 char address + ".ip6" )
#else
#define MDNS_NAME_MAX_LEN 64 // Maximum string length of hostname, instance, service and proto
#endif
#define MDNS_NAME_BUF_LEN (MDNS_NAME_MAX_LEN+1) // Maximum char buffer size to hold hostname, instance, service or proto
#define MDNS_MAX_PACKET_SIZE 1460 // Maximum size of mDNS outgoing packet

#define MDNS_HEAD_LEN 12
Expand Down
4 changes: 4 additions & 0 deletions components/mdns/tests/test_afl_fuzz_host/esp32_mock.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ void esp_log_write(esp_log_level_t level, const char *tag, const char *format, .
{
}

void esp_log(esp_log_config_t config, const char *tag, const char *format, ...)
{
}

uint32_t esp_log_timestamp(void)
{
return 0;
Expand Down

0 comments on commit 9b74256

Please sign in to comment.