Skip to content

Commit 0191d6f

Browse files
fix(mdns): add the maximum number of services
* Original commit: espressif/esp-idf@ba458c6
1 parent b26c866 commit 0191d6f

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

components/mdns/Kconfig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
menu "mDNS"
2+
3+
config MDNS_MAX_SERVICES
4+
int "Max number of services"
5+
range 1 64
6+
default 10
7+
help
8+
Services take up a certain amount of memory, and allowing fewer
9+
services to be open at the same time conserves memory. Specify
10+
the maximum amount of services here. The valid value is from 1
11+
to 64.
12+
13+
endmenu

components/mdns/mdns.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,21 @@ static mdns_srv_item_t * _mdns_get_service_item(const char * service, const char
9696
return NULL;
9797
}
9898

99+
static bool _mdns_can_add_more_services(void)
100+
{
101+
mdns_srv_item_t * s = _mdns_server->services;
102+
uint16_t service_num = 0;
103+
while (s) {
104+
service_num ++;
105+
s = s->next;
106+
if (service_num >= MDNS_MAX_SERVICES) {
107+
return false;
108+
}
109+
}
110+
111+
return true;
112+
}
113+
99114
esp_err_t _mdns_send_rx_action(mdns_rx_packet_t * packet)
100115
{
101116
mdns_action_t * action = NULL;
@@ -4124,6 +4139,11 @@ esp_err_t mdns_service_add(const char * instance, const char * service, const ch
41244139
if (!_mdns_server || _str_null_or_empty(service) || _str_null_or_empty(proto) || !port) {
41254140
return ESP_ERR_INVALID_ARG;
41264141
}
4142+
4143+
if (!_mdns_can_add_more_services()) {
4144+
return ESP_ERR_NO_MEM;
4145+
}
4146+
41274147
mdns_srv_item_t * item = _mdns_get_service_item(service, proto);
41284148
if (item) {
41294149
return ESP_ERR_INVALID_ARG;

components/mdns/private_include/mdns_private.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
#define _mdns_dbg_printf(...) printf(__VA_ARGS__)
2121
#endif
2222

23+
/** The maximum number of services */
24+
#define MDNS_MAX_SERVICES CONFIG_MDNS_MAX_SERVICES
25+
2326
#define MDNS_ANSWER_PTR_TTL 4500
2427
#define MDNS_ANSWER_TXT_TTL 4500
2528
#define MDNS_ANSWER_SRV_TTL 120

0 commit comments

Comments
 (0)