Skip to content

Commit 4e11cc8

Browse files
mdns: Allow for unicast PTR queries
Adresses espressif/esp-idf#7932 * Original commit: espressif/esp-idf@7eeeb01
1 parent 7af91ec commit 4e11cc8

File tree

4 files changed

+42
-12
lines changed

4 files changed

+42
-12
lines changed

components/mdns/include/mdns.h

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -573,13 +573,36 @@ mdns_search_once_t *mdns_query_async_new(const char *name, const char *service_t
573573
uint32_t timeout, size_t max_results, mdns_query_notify_t notifier);
574574

575575
/**
576-
* @brief Query mDNS for host or service
576+
* @brief Generic mDNS query
577577
* All following query methods are derived from this one
578578
*
579579
* @param name service instance or host name (NULL for PTR queries)
580580
* @param service_type service type (_http, _arduino, _ftp etc.) (NULL for host queries)
581581
* @param proto service protocol (_tcp, _udp, etc.) (NULL for host queries)
582582
* @param type type of query (MDNS_TYPE_*)
583+
* @param unicast true for Unicast query, false for Multicast query
584+
* @param timeout time in milliseconds to wait for answers.
585+
* @param max_results maximum results to be collected
586+
* @param results pointer to the results of the query
587+
* results must be freed using mdns_query_results_free below
588+
*
589+
* @return
590+
* - ESP_OK success
591+
* - ESP_ERR_INVALID_STATE mDNS is not running
592+
* - ESP_ERR_NO_MEM memory error
593+
* - ESP_ERR_INVALID_ARG timeout was not given
594+
*/
595+
esp_err_t mdns_query_generic(const char * name, const char * service_type, const char * proto, uint16_t type, bool unicast, uint32_t timeout, size_t max_results, mdns_result_t ** results);
596+
597+
/**
598+
* @brief Query mDNS for host or service
599+
*
600+
* Note that querying PTR types sends Multicast query, all other types send Unicast queries
601+
*
602+
* @param name service instance or host name (NULL for PTR queries)
603+
* @param service_type service type (_http, _arduino, _ftp etc.) (NULL for host queries)
604+
* @param proto service protocol (_tcp, _udp, etc.) (NULL for host queries)
605+
* @param type type of query (MDNS_TYPE_*)
583606
* @param timeout time in milliseconds to wait for answers.
584607
* @param max_results maximum results to be collected
585608
* @param results pointer to the results of the query

components/mdns/mdns.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3786,7 +3786,7 @@ static void _mdns_search_free(mdns_search_once_t * search)
37863786
/**
37873787
* @brief Allocate new search structure
37883788
*/
3789-
static mdns_search_once_t *_mdns_search_init(const char *name, const char *service, const char *proto, uint16_t type,
3789+
static mdns_search_once_t *_mdns_search_init(const char *name, const char *service, const char *proto, uint16_t type, bool unicast,
37903790
uint32_t timeout, uint8_t max_results, mdns_query_notify_t notifier)
37913791
{
37923792
mdns_search_once_t * search = (mdns_search_once_t *)malloc(sizeof(mdns_search_once_t));
@@ -3827,6 +3827,7 @@ static mdns_search_once_t *_mdns_search_init(const char *name, const char *servi
38273827
}
38283828

38293829
search->type = type;
3830+
search->unicast = unicast;
38303831
search->timeout = timeout;
38313832
search->num_results = 0;
38323833
search->max_results = max_results;
@@ -4207,7 +4208,7 @@ static mdns_tx_packet_t * _mdns_create_search_packet(mdns_search_once_t * search
42074208
return NULL;
42084209
}
42094210
q->next = NULL;
4210-
q->unicast = search->type != MDNS_TYPE_PTR;
4211+
q->unicast = search->unicast;
42114212
q->type = search->type;
42124213
q->host = search->instance;
42134214
q->service = search->service;
@@ -5610,7 +5611,7 @@ mdns_search_once_t *mdns_query_async_new(const char *name, const char *service,
56105611
return NULL;
56115612
}
56125613

5613-
search = _mdns_search_init(name, service, proto, type, timeout, max_results, notifier);
5614+
search = _mdns_search_init(name, service, proto, type, type != MDNS_TYPE_PTR, timeout, max_results, notifier);
56145615
if (!search) {
56155616
return NULL;
56165617
}
@@ -5623,7 +5624,7 @@ mdns_search_once_t *mdns_query_async_new(const char *name, const char *service,
56235624
return search;
56245625
}
56255626

5626-
esp_err_t mdns_query(const char * name, const char * service, const char * proto, uint16_t type, uint32_t timeout, size_t max_results, mdns_result_t ** results)
5627+
esp_err_t mdns_query_generic(const char * name, const char * service, const char * proto, uint16_t type, bool unicast, uint32_t timeout, size_t max_results, mdns_result_t ** results)
56275628
{
56285629
mdns_search_once_t * search = NULL;
56295630

@@ -5637,7 +5638,7 @@ esp_err_t mdns_query(const char * name, const char * service, const char * proto
56375638
return ESP_ERR_INVALID_ARG;
56385639
}
56395640

5640-
search = _mdns_search_init(name, service, proto, type, timeout, max_results, NULL);
5641+
search = _mdns_search_init(name, service, proto, type, unicast, timeout, max_results, NULL);
56415642
if (!search) {
56425643
return ESP_ERR_NO_MEM;
56435644
}
@@ -5654,6 +5655,11 @@ esp_err_t mdns_query(const char * name, const char * service, const char * proto
56545655
return ESP_OK;
56555656
}
56565657

5658+
esp_err_t mdns_query(const char * name, const char * service_type, const char * proto, uint16_t type, uint32_t timeout, size_t max_results, mdns_result_t ** results)
5659+
{
5660+
return mdns_query_generic(name, service_type, proto, type, type != MDNS_TYPE_PTR, timeout, max_results, results);
5661+
}
5662+
56575663
esp_err_t mdns_query_ptr(const char * service, const char * proto, uint32_t timeout, size_t max_results, mdns_result_t ** results)
56585664
{
56595665
if (_str_null_or_empty(service) || _str_null_or_empty(proto)) {

components/mdns/private_include/mdns_private.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -368,6 +368,7 @@ typedef struct mdns_search_once_s {
368368
mdns_query_notify_t notifier;
369369
SemaphoreHandle_t done_semaphore;
370370
uint16_t type;
371+
bool unicast;
371372
uint8_t max_results;
372373
uint8_t num_results;
373374
char * instance;

components/mdns/test_afl_fuzz_host/mdns_di.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88

99
void (*mdns_test_static_execute_action)(mdns_action_t *) = NULL;
1010
mdns_srv_item_t * (*mdns_test_static_mdns_get_service_item)(const char * service, const char * proto, const char *hostname) = NULL;
11-
mdns_search_once_t *(*mdns_test_static_search_init)(const char *name, const char *service, const char *proto,
12-
uint16_t type, uint32_t timeout, uint8_t max_results,
11+
mdns_search_once_t *(*mdns_test_static_search_init)(const char *name, const char *service, const char *proto, uint16_t type, bool unicast,
12+
uint32_t timeout, uint8_t max_results,
1313
mdns_query_notify_t notifier) = NULL;
1414
esp_err_t (*mdns_test_static_send_search_action)(mdns_action_type_t type, mdns_search_once_t * search) = NULL;
1515
void (*mdns_test_static_search_free)(mdns_search_once_t * search) = NULL;
1616

1717
static void _mdns_execute_action(mdns_action_t * action);
1818
static mdns_srv_item_t * _mdns_get_service_item(const char * service, const char * proto, const char *hostname);
19-
static mdns_search_once_t *_mdns_search_init(const char *name, const char *service, const char *proto, uint16_t type,
19+
static mdns_search_once_t *_mdns_search_init(const char *name, const char *service, const char *proto, uint16_t type, bool unicast,
2020
uint32_t timeout, uint8_t max_results, mdns_query_notify_t notifier);
2121
static esp_err_t _mdns_send_search_action(mdns_action_type_t type, mdns_search_once_t * search);
2222
static void _mdns_search_free(mdns_search_once_t * search);
@@ -47,7 +47,7 @@ esp_err_t mdns_test_send_search_action(mdns_action_type_t type, mdns_search_once
4747

4848
mdns_search_once_t * mdns_test_search_init(const char * name, const char * service, const char * proto, uint16_t type, uint32_t timeout, uint8_t max_results)
4949
{
50-
return mdns_test_static_search_init(name, service, proto, type, timeout, max_results, NULL);
50+
return mdns_test_static_search_init(name, service, proto, type, timeout, type != MDNS_TYPE_PTR, max_results, NULL);
5151
}
5252

5353
mdns_srv_item_t * mdns_test_mdns_get_service_item(const char * service, const char * proto)

0 commit comments

Comments
 (0)