From ddc58e8220f8b4d8829cfe47cfecdf071e92c845 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Tue, 14 Dec 2021 16:14:21 +0100 Subject: [PATCH] mdns: Indicate interface using esp_netif in search results * Original commit: espressif/esp-idf@f8495f1e86de9a8e7d046bf13d0ca04775041b4c --- components/mdns/Kconfig | 33 ++++ components/mdns/include/mdns.h | 9 +- components/mdns/mdns.c | 147 ++++++++++-------- components/mdns/mdns_console.c | 26 ++-- components/mdns/mdns_networking_lwip.c | 6 +- components/mdns/mdns_networking_socket.c | 6 +- .../mdns/private_include/mdns_private.h | 25 ++- .../protocols/mdns/main/mdns_example_main.c | 5 +- 8 files changed, 156 insertions(+), 101 deletions(-) diff --git a/components/mdns/Kconfig b/components/mdns/Kconfig index 5607afac8..7ceefd88d 100644 --- a/components/mdns/Kconfig +++ b/components/mdns/Kconfig @@ -1,5 +1,13 @@ menu "mDNS" + config MDNS_MAX_INTERFACES + int "Max number of interfaces" + range 1 9 + default 3 + help + Number of network interfaces to be served by the mdns library. + Lowering this number helps to reduce some static RAM usage. + config MDNS_MAX_SERVICES int "Max number of services" range 1 64 @@ -90,4 +98,29 @@ menu "mDNS" help Enables adding multiple service instances under the same service type. + menu "MDNS Predefined interfaces" + + config MDNS_PREDEF_NETIF_STA + bool "Use predefined interface for WiFi Station" + default y + help + Set up mdns for the default WiFi station. + Disable this option if you do not need mDNS on default WiFi STA. + + config MDNS_PREDEF_NETIF_AP + bool "Use predefined interface for WiFi Access Point" + default y + help + Set up mdns for the default WiFi Access Point. + Disable this option if you do not need mDNS on default WiFi AP. + + config MDNS_PREDEF_NETIF_ETH + bool "Use predefined interface for Ethernet" + default y + help + Set up mdns for the default Ethernet interface. + Disable this option if you do not need mDNS on default Ethernet. + + endmenu # MDNS Predefined interfaces + endmenu diff --git a/components/mdns/include/mdns.h b/components/mdns/include/mdns.h index fb14077b4..ab4e97ac3 100644 --- a/components/mdns/include/mdns.h +++ b/components/mdns/include/mdns.h @@ -21,8 +21,6 @@ extern "C" { #define MDNS_TYPE_NSEC 0x002F #define MDNS_TYPE_ANY 0x00FF -#define CONFIG_MDNS_IF_MAX 4 - /** * @brief Asynchronous query handle */ @@ -64,11 +62,6 @@ typedef struct mdns_ip_addr_s { struct mdns_ip_addr_s * next; /*!< next IP, or NULL for the last IP in the list */ } mdns_ip_addr_t; -typedef enum mdns_if_internal { - MDNS_IF_INVALID = -1, - MDNS_IF_MAX = CONFIG_MDNS_IF_MAX -} mdns_if_t; - /** * @brief mDNS query type to be explicitly set to either Unicast or Multicast */ @@ -83,7 +76,7 @@ typedef enum { typedef struct mdns_result_s { struct mdns_result_s * next; /*!< next result, or NULL for the last result in the list */ - mdns_if_t tcpip_if; /*!< interface index */ + esp_netif_t* esp_netif; /*!< ptr to corresponding esp-netif */ uint32_t ttl; /*!< time to live */ mdns_ip_protocol_t ip_protocol; /*!< ip_protocol type of the interface (v4/v6) */ diff --git a/components/mdns/mdns.c b/components/mdns/mdns.c index 27c6797cd..50996cf7f 100644 --- a/components/mdns/mdns.c +++ b/components/mdns/mdns.c @@ -59,11 +59,6 @@ static bool _mdns_append_host_list(mdns_out_answer_t ** destination, bool flush, static void _mdns_remap_self_service_hostname(const char *old_hostname, const char *new_hostname); static esp_err_t mdns_post_custom_action_tcpip_if(mdns_if_t mdns_if, mdns_event_actions_t event_action); -typedef enum mdns_netif_predef { - MDNS_USES_PREDEFINED_IF = 0, - MDNS_USES_CUSTOM_IF = 1, -} mdns_netif_predef_t; - typedef enum { MDNS_IF_STA = 0, MDNS_IF_AP = 1, @@ -73,7 +68,7 @@ typedef enum { typedef struct mdns_interfaces mdns_interfaces_t; struct mdns_interfaces { - mdns_netif_predef_t predefined; + bool predefined; esp_netif_t * netif; mdns_predef_if_t predef_if; mdns_if_t duplicate; @@ -83,10 +78,16 @@ struct mdns_interfaces { * @brief Internal collection of mdns supported interfaces * */ -static mdns_interfaces_t s_esp_netifs[MDNS_IF_MAX] = { - { .predefined = MDNS_USES_PREDEFINED_IF, .netif = NULL, .predef_if = MDNS_IF_STA, .duplicate = MDNS_IF_MAX }, - { .predefined = MDNS_USES_PREDEFINED_IF, .netif = NULL, .predef_if = MDNS_IF_AP, .duplicate = MDNS_IF_MAX }, - { .predefined = MDNS_USES_PREDEFINED_IF, .netif = NULL, .predef_if = MDNS_IF_ETH, .duplicate = MDNS_IF_MAX }, +static mdns_interfaces_t s_esp_netifs[MDNS_MAX_INTERFACES] = { +#if CONFIG_MDNS_PREDEF_NETIF_STA + { .predefined = true, .netif = NULL, .predef_if = MDNS_IF_STA, .duplicate = MDNS_MAX_INTERFACES }, +#endif +#if CONFIG_MDNS_PREDEF_NETIF_AP + { .predefined = true, .netif = NULL, .predef_if = MDNS_IF_AP, .duplicate = MDNS_MAX_INTERFACES }, +#endif +#if CONFIG_MDNS_PREDEF_NETIF_ETH + { .predefined = true, .netif = NULL, .predef_if = MDNS_IF_ETH, .duplicate = MDNS_MAX_INTERFACES }, +#endif }; @@ -95,12 +96,12 @@ static mdns_interfaces_t s_esp_netifs[MDNS_IF_MAX] = { */ static mdns_if_t mdns_if_from_predef_if(mdns_predef_if_t predef_if) { - for (int i=0; i MDNS_IF_INVALID && tcpip_if < MDNS_IF_MAX) { - if (s_esp_netifs[tcpip_if].netif == NULL && s_esp_netifs[tcpip_if].predefined == MDNS_USES_PREDEFINED_IF) { + if (tcpip_if < MDNS_MAX_INTERFACES) { + if (s_esp_netifs[tcpip_if].netif == NULL && s_esp_netifs[tcpip_if].predefined) { // if a predefined interface face and used local copy is NULL, try to search for the default interface key s_esp_netifs[tcpip_if].netif = esp_netif_from_preset_if(s_esp_netifs[tcpip_if].predef_if); } @@ -136,7 +137,7 @@ esp_netif_t *_mdns_get_esp_netif(mdns_if_t tcpip_if) * @brief Clean internal mdns interface's pointer */ static inline void _mdns_clean_netif_ptr(mdns_if_t tcpip_if) { - if (tcpip_if < MDNS_IF_MAX) { + if (tcpip_if < MDNS_MAX_INTERFACES) { s_esp_netifs[tcpip_if].netif = NULL; } } @@ -147,11 +148,11 @@ static inline void _mdns_clean_netif_ptr(mdns_if_t tcpip_if) { */ static mdns_if_t _mdns_get_if_from_esp_netif(esp_netif_t *interface) { - for (int i=0; i MDNS_IF_INVALID && tcpip_if < MDNS_IF_MAX) { + if (tcpip_if < MDNS_MAX_INTERFACES) { return s_esp_netifs[tcpip_if].duplicate; } - return MDNS_IF_MAX; + return MDNS_MAX_INTERFACES; } /** @@ -1113,7 +1114,7 @@ static mdns_if_t _mdns_get_other_if (mdns_if_t tcpip_if) static bool _mdns_if_is_dup(mdns_if_t tcpip_if) { mdns_if_t other_if = _mdns_get_other_if (tcpip_if); - if (other_if == MDNS_IF_MAX) { + if (other_if == MDNS_MAX_INTERFACES) { return false; } if (_mdns_server->interfaces[tcpip_if].pcbs[MDNS_IP_PROTOCOL_V4].state == PCB_DUP @@ -2119,7 +2120,7 @@ static void _mdns_send_bye(mdns_srv_item_t ** services, size_t len, bool include return; } - for (i=0; iinterfaces[i].pcbs[j].pcb && _mdns_server->interfaces[i].pcbs[j].state == PCB_RUNNING) { _mdns_pcb_send_bye((mdns_if_t)i, (mdns_ip_protocol_t)j, services, len, include_ip); @@ -2177,7 +2178,7 @@ static void _mdns_announce_pcb(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protoco static void _mdns_probe_all_pcbs(mdns_srv_item_t ** services, size_t len, bool probe_ip, bool clear_old_probe) { uint8_t i, j; - for (i=0; iinterfaces[i].pcbs[j].pcb) { mdns_pcb_t * _pcb = &_mdns_server->interfaces[i].pcbs[j]; @@ -2199,7 +2200,7 @@ static void _mdns_probe_all_pcbs(mdns_srv_item_t ** services, size_t len, bool p static void _mdns_announce_all_pcbs(mdns_srv_item_t ** services, size_t len, bool include_ip) { uint8_t i, j; - for (i=0; itype == MDNS_TYPE_PTR) { result = search_result->result; while (result) { - if (packet->tcpip_if == result->tcpip_if + if (_mdns_get_esp_netif(packet->tcpip_if) == result->esp_netif && packet->ip_protocol == result->ip_protocol && result->instance_name && !strcmp(name->host, result->instance_name)) { break; @@ -3602,7 +3603,7 @@ void mdns_parse_packet(mdns_rx_packet_t * packet) if (search_result->type == MDNS_TYPE_PTR) { result = search_result->result; while (result) { - if (packet->tcpip_if == result->tcpip_if + if (_mdns_get_esp_netif(packet->tcpip_if) == result->esp_netif && packet->ip_protocol == result->ip_protocol && result->instance_name && !strcmp(name->host, result->instance_name)) { break; @@ -3810,7 +3811,7 @@ void _mdns_disable_pcb(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) _mdns_clear_pcb_tx_queue_head(tcpip_if, ip_protocol); _mdns_pcb_deinit(tcpip_if, ip_protocol); mdns_if_t other_if = _mdns_get_other_if (tcpip_if); - if (other_if != MDNS_IF_MAX && _mdns_server->interfaces[other_if].pcbs[ip_protocol].state == PCB_DUP) { + if (other_if != MDNS_MAX_INTERFACES && _mdns_server->interfaces[other_if].pcbs[ip_protocol].state == PCB_DUP) { _mdns_server->interfaces[other_if].pcbs[ip_protocol].state = PCB_OFF; _mdns_enable_pcb(other_if, ip_protocol); } @@ -3823,7 +3824,7 @@ void _mdns_disable_pcb(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) */ static void perform_event_action(mdns_if_t mdns_if, mdns_event_actions_t action) { - if (!_mdns_server || mdns_if > MDNS_IF_MAX) { + if (!_mdns_server || mdns_if > MDNS_MAX_INTERFACES) { return; } if (action & MDNS_EVENT_ENABLE_IP4) { @@ -3871,7 +3872,6 @@ void mdns_preset_if_handle_system_event(void *arg, esp_event_base_t event_base, return; } - esp_netif_dhcp_status_t dcst; if (event_base == WIFI_EVENT) { switch(event_id) { @@ -3931,7 +3931,7 @@ void mdns_preset_if_handle_system_event(void *arg, esp_event_base_t event_base, { 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_IF_MAX) { + 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); } @@ -4120,7 +4120,7 @@ static void _mdns_search_result_add_ip(mdns_search_once_t * search, const char * || search->type == MDNS_TYPE_ANY) { r = search->result; while (r) { - if (r->tcpip_if == tcpip_if && r->ip_protocol == ip_protocol) { + if (r->esp_netif == _mdns_get_esp_netif(tcpip_if) && r->ip_protocol == ip_protocol) { _mdns_result_add_ip(r, ip); _mdns_result_update_ttl(r, ttl); return; @@ -4144,7 +4144,7 @@ static void _mdns_search_result_add_ip(mdns_search_once_t * search, const char * a->next = r->addr; r->hostname = strdup(hostname); r->addr = a; - r->tcpip_if = tcpip_if; + r->esp_netif = _mdns_get_esp_netif(tcpip_if); r->ip_protocol = ip_protocol; r->next = search->result; r->ttl = ttl; @@ -4154,7 +4154,7 @@ static void _mdns_search_result_add_ip(mdns_search_once_t * search, const char * } else if (search->type == MDNS_TYPE_PTR || search->type == MDNS_TYPE_SRV) { r = search->result; while (r) { - if (r->tcpip_if == tcpip_if && r->ip_protocol == ip_protocol && !_str_null_or_empty(r->hostname) && !strcasecmp(hostname, r->hostname)) { + if (r->esp_netif == _mdns_get_esp_netif(tcpip_if) && r->ip_protocol == ip_protocol && !_str_null_or_empty(r->hostname) && !strcasecmp(hostname, r->hostname)) { _mdns_result_add_ip(r, ip); _mdns_result_update_ttl(r, ttl); break; @@ -4173,7 +4173,7 @@ static mdns_result_t * _mdns_search_result_add_ptr(mdns_search_once_t * search, { mdns_result_t * r = search->result; while (r) { - if (r->tcpip_if == tcpip_if && r->ip_protocol == ip_protocol && !_str_null_or_empty(r->instance_name) && !strcasecmp(instance, r->instance_name)) { + if (r->esp_netif == _mdns_get_esp_netif(tcpip_if) && r->ip_protocol == ip_protocol && !_str_null_or_empty(r->instance_name) && !strcasecmp(instance, r->instance_name)) { _mdns_result_update_ttl(r, ttl); return r; } @@ -4195,7 +4195,7 @@ static mdns_result_t * _mdns_search_result_add_ptr(mdns_search_once_t * search, return NULL; } - r->tcpip_if = tcpip_if; + r->esp_netif = _mdns_get_esp_netif(tcpip_if); r->ip_protocol = ip_protocol; r->ttl = ttl; r->next = search->result; @@ -4214,7 +4214,7 @@ static void _mdns_search_result_add_srv(mdns_search_once_t *search, const char * { mdns_result_t * r = search->result; while (r) { - if (r->tcpip_if == tcpip_if && r->ip_protocol == ip_protocol && !_str_null_or_empty(r->hostname) && !strcasecmp(hostname, r->hostname)) { + if (r->esp_netif == _mdns_get_esp_netif(tcpip_if) && r->ip_protocol == ip_protocol && !_str_null_or_empty(r->hostname) && !strcasecmp(hostname, r->hostname)) { _mdns_result_update_ttl(r, ttl); return; } @@ -4239,7 +4239,7 @@ static void _mdns_search_result_add_srv(mdns_search_once_t *search, const char * r->service_type = strdup(search->service); r->proto = strdup(search->proto); r->port = port; - r->tcpip_if = tcpip_if; + r->esp_netif = _mdns_get_esp_netif(tcpip_if); r->ip_protocol = ip_protocol; r->ttl = ttl; r->next = search->result; @@ -4257,7 +4257,7 @@ static void _mdns_search_result_add_txt(mdns_search_once_t *search, mdns_txt_ite { mdns_result_t * r = search->result; while (r) { - if (r->tcpip_if == tcpip_if && r->ip_protocol == ip_protocol) { + if (r->esp_netif == _mdns_get_esp_netif(tcpip_if) && r->ip_protocol == ip_protocol) { if (r->txt) { goto free_txt; } @@ -4280,7 +4280,7 @@ static void _mdns_search_result_add_txt(mdns_search_once_t *search, mdns_txt_ite r->txt = txt; r->txt_value_len = txt_value_len; r->txt_count = txt_count; - r->tcpip_if = tcpip_if; + r->esp_netif = _mdns_get_esp_netif(tcpip_if); r->ip_protocol = ip_protocol; r->ttl = ttl; r->next = search->result; @@ -4325,7 +4325,7 @@ static mdns_search_once_t * _mdns_search_find_from(mdns_search_once_t * s, mdns_ } r = s->result; while (r) { - if (r->tcpip_if == tcpip_if && r->ip_protocol == ip_protocol && !_str_null_or_empty(r->hostname) && !strcasecmp(name->host, r->hostname)) { + if (r->esp_netif == _mdns_get_esp_netif(tcpip_if) && r->ip_protocol == ip_protocol && !_str_null_or_empty(r->hostname) && !strcasecmp(name->host, r->hostname)) { return s; } r = r->next; @@ -4398,7 +4398,7 @@ static mdns_tx_packet_t * _mdns_create_search_packet(mdns_search_once_t * search r = search->result; while (r) { //full record on the same interface is available - if (r->tcpip_if != tcpip_if || r->ip_protocol != ip_protocol || r->instance_name == NULL || r->hostname == NULL || r->addr == NULL) { + if (r->esp_netif != _mdns_get_esp_netif(tcpip_if) || r->ip_protocol != ip_protocol || r->instance_name == NULL || r->hostname == NULL || r->addr == NULL) { r = r->next; continue; } @@ -4462,7 +4462,7 @@ static void _mdns_search_send(mdns_search_once_t * search) } uint8_t i, j; - for (i=0; i= MDNS_IF_MAX) { + if (!_mdns_server || mdns_if >= MDNS_MAX_INTERFACES) { return ESP_FAIL; } @@ -5048,21 +5048,35 @@ static esp_err_t mdns_post_custom_action_tcpip_if(mdns_if_t mdns_if, mdns_event_ static inline void set_default_duplicated_interfaces(void) { - mdns_if_t wifi_sta_if = MDNS_IF_MAX, eth_if = MDNS_IF_MAX; - for (mdns_if_t i=0; ilock = xSemaphoreCreateMutex(); if (!_mdns_server->lock) { @@ -5100,13 +5116,18 @@ esp_err_t mdns_init(void) err = ESP_ERR_NO_MEM; goto free_lock; } + +#if CONFIG_MDNS_PREDEF_NETIF_STA || CONFIG_MDNS_PREDEF_NETIF_AP if ((err = esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, mdns_preset_if_handle_system_event, NULL)) != ESP_OK) { goto free_event_handlers; } +#endif +#if CONFIG_MDNS_PREDEF_NETIF_STA || CONFIG_MDNS_PREDEF_NETIF_AP || CONFIG_MDNS_PREDEF_NETIF_ETH if ((err = esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, mdns_preset_if_handle_system_event, NULL)) != ESP_OK) { goto free_event_handlers; } -#if CONFIG_ETH_ENABLED +#endif +#if defined(CONFIG_ETH_ENABLED) && CONFIG_MDNS_PREDEF_NETIF_ETH if ((err = esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, mdns_preset_if_handle_system_event, NULL)) != ESP_OK) { goto free_event_handlers; } @@ -5120,7 +5141,7 @@ esp_err_t mdns_init(void) #endif esp_netif_ip_info_t if_ip_info; - for (i=0; iaction_queue); free_lock: vSemaphoreDelete(_mdns_server->lock); @@ -5168,16 +5185,12 @@ void mdns_free(void) } // Unregister handlers before destroying the mdns internals to avoid receiving async events while deinit - esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, mdns_preset_if_handle_system_event); - esp_event_handler_unregister(IP_EVENT, ESP_EVENT_ANY_ID, mdns_preset_if_handle_system_event); -#if CONFIG_ETH_ENABLED - esp_event_handler_unregister(ETH_EVENT, ESP_EVENT_ANY_ID, mdns_preset_if_handle_system_event); -#endif + unregister_predefined_handlers(); mdns_service_remove_all(); free_delegated_hostnames(); _mdns_service_task_stop(); - for (i=0; i #include #include "esp_console.h" #include "argtable3/argtable3.h" #include "mdns.h" -static const char * if_str[] = {"STA", "AP", "ETH", "MAX"}; static const char * ip_protocol_str[] = {"V4", "V6", "MAX"}; +static const char * if_str(esp_netif_t *netif) +{ + return esp_netif_get_ifkey(netif); +} + static void mdns_print_results(mdns_result_t * results) { mdns_result_t * r = results; mdns_ip_addr_t * a = NULL; int i = 1; while (r) { - printf("%d: Interface: %s, Type: %s\n", i++, if_str[r->tcpip_if], ip_protocol_str[r->ip_protocol]); + printf("%d: Interface: %s, Type: %s\n", i++, if_str(r->esp_netif), ip_protocol_str[r->ip_protocol]); if (r->instance_name) { printf(" PTR : %s\n", r->instance_name); } diff --git a/components/mdns/mdns_networking_lwip.c b/components/mdns/mdns_networking_lwip.c index 5f7a6673f..370661d12 100644 --- a/components/mdns/mdns_networking_lwip.c +++ b/components/mdns/mdns_networking_lwip.c @@ -134,7 +134,7 @@ static void _udp_recv(void *arg, struct udp_pcb *upcb, struct pbuf *pb, const ip continue; } - packet->tcpip_if = MDNS_IF_MAX; + packet->tcpip_if = MDNS_MAX_INTERFACES; packet->pb = this_pb; packet->src_port = rport; #if CONFIG_LWIP_IPV6 @@ -164,7 +164,7 @@ static void _udp_recv(void *arg, struct udp_pcb *upcb, struct pbuf *pb, const ip //lwip does not return the proper pcb if you have more than one for the same multicast address (but different interfaces) struct netif * netif = NULL; struct udp_pcb * pcb = NULL; - for (i=0; iinterfaces[i].pcbs[packet->ip_protocol].pcb; netif = esp_netif_get_netif_impl(_mdns_get_esp_netif(i)); if (pcb && netif && netif == ip_current_input_netif ()) { @@ -198,7 +198,7 @@ static void _udp_recv(void *arg, struct udp_pcb *upcb, struct pbuf *pb, const ip */ static bool _udp_pcb_is_in_use(void){ int i, p; - for (i=0; iinterfaces[i].pcbs[p].pcb){ return true; diff --git a/components/mdns/mdns_networking_socket.c b/components/mdns/mdns_networking_socket.c index 45ad83dec..f4341d4f6 100644 --- a/components/mdns/mdns_networking_socket.c +++ b/components/mdns/mdns_networking_socket.c @@ -101,7 +101,7 @@ esp_err_t _mdns_pcb_deinit(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol) } } - for (int i=0; iinterfaces[i].pcbs[j].pcb) // If any of the interfaces/protocol initialized @@ -247,7 +247,7 @@ void sock_recv_task(void* arg) fd_set rfds; FD_ZERO(&rfds); int max_sock = -1; - for (int i=0; iinterfaces[i].pcbs[j].pcb); if (sock >= 0) { @@ -267,7 +267,7 @@ void sock_recv_task(void* arg) ESP_LOGE(TAG, "Select failed. errno=%d: %s", errno, strerror(errno)); break; } else if (s > 0) { - for (int tcpip_if=0; tcpip_ifinterfaces[tcpip_if].pcbs[MDNS_IP_PROTOCOL_V4].pcb); if (sock < 0) { diff --git a/components/mdns/private_include/mdns_private.h b/components/mdns/private_include/mdns_private.h index e766146a7..bf1d6d8bf 100644 --- a/components/mdns/private_include/mdns_private.h +++ b/components/mdns/private_include/mdns_private.h @@ -41,6 +41,27 @@ * any item in question field */ #define MDNS_REPEAT_QUERY_IN_RESPONSE 1 #endif + +/** Number of predefined interfaces */ +#ifndef CONFIG_MDNS_PREDEF_NETIF_STA +#define CONFIG_MDNS_PREDEF_NETIF_STA 0 +#endif +#ifndef CONFIG_MDNS_PREDEF_NETIF_AP +#define CONFIG_MDNS_PREDEF_NETIF_AP 0 +#endif +#ifndef CONFIG_MDNS_PREDEF_NETIF_ETH +#define CONFIG_MDNS_PREDEF_NETIF_ETH 0 +#endif +#define MDNS_MAX_PREDEF_INTERFACES (CONFIG_MDNS_PREDEF_NETIF_STA + CONFIG_MDNS_PREDEF_NETIF_AP + CONFIG_MDNS_PREDEF_NETIF_ETH) + +/** Number of configured interfaces */ +#if MDNS_MAX_PREDEF_INTERFACES > CONFIG_MDNS_MAX_INTERFACES +#warning Number of configured interfaces is less then number of predefined interfaces. Please update CONFIG_MDNS_MAX_INTERFACES. +#define MDNS_MAX_INTERFACES (MDNS_MAX_PREDEF_INTERFACES) +#else +#define MDNS_MAX_INTERFACES (CONFIG_MDNS_MAX_INTERFACES) +#endif + /** The maximum number of services */ #define MDNS_MAX_SERVICES CONFIG_MDNS_MAX_SERVICES @@ -150,6 +171,8 @@ #define HOOK_MALLOC_FAILED ESP_LOGE(TAG, "Cannot allocate memory (line: %d, free heap: %d bytes)", __LINE__, esp_get_free_heap_size()); #endif +typedef size_t mdns_if_t; + typedef enum { PCB_OFF, PCB_DUP, PCB_INIT, PCB_PROBE_1, PCB_PROBE_2, PCB_PROBE_3, @@ -384,7 +407,7 @@ typedef struct mdns_search_once_s { typedef struct mdns_server_s { struct { mdns_pcb_t pcbs[MDNS_IP_PROTOCOL_MAX]; - } interfaces[MDNS_IF_MAX]; + } interfaces[MDNS_MAX_INTERFACES]; const char * hostname; const char * instance; mdns_srv_item_t * services; diff --git a/examples/protocols/mdns/main/mdns_example_main.c b/examples/protocols/mdns/main/mdns_example_main.c index c1db88ba0..5a90b5165 100644 --- a/examples/protocols/mdns/main/mdns_example_main.c +++ b/examples/protocols/mdns/main/mdns_example_main.c @@ -83,9 +83,6 @@ static void initialise_mdns(void) free(hostname); } -/* these strings match tcpip_adapter_if_t enumeration */ -static const char * if_str[] = {"STA", "AP", "ETH", "MAX"}; - /* these strings match mdns_ip_protocol_t enumeration */ static const char * ip_protocol_str[] = {"V4", "V6", "MAX"}; @@ -95,7 +92,7 @@ static void mdns_print_results(mdns_result_t *results) mdns_ip_addr_t *a = NULL; int i = 1, t; while (r) { - printf("%d: Interface: %s, Type: %s, TTL: %u\n", i++, if_str[r->tcpip_if], ip_protocol_str[r->ip_protocol], + printf("%d: Interface: %s, Type: %s, TTL: %u\n", i++, esp_netif_get_ifkey(r->esp_netif), ip_protocol_str[r->ip_protocol], r->ttl); if (r->instance_name) { printf(" PTR : %s.%s.%s\n", r->instance_name, r->service_type, r->proto);