Skip to content

Commit ddc58e8

Browse files
mdns: Indicate interface using esp_netif in search results
* Original commit: espressif/esp-idf@f8495f1
1 parent fa951bf commit ddc58e8

File tree

8 files changed

+156
-101
lines changed

8 files changed

+156
-101
lines changed

components/mdns/Kconfig

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
menu "mDNS"
22

3+
config MDNS_MAX_INTERFACES
4+
int "Max number of interfaces"
5+
range 1 9
6+
default 3
7+
help
8+
Number of network interfaces to be served by the mdns library.
9+
Lowering this number helps to reduce some static RAM usage.
10+
311
config MDNS_MAX_SERVICES
412
int "Max number of services"
513
range 1 64
@@ -90,4 +98,29 @@ menu "mDNS"
9098
help
9199
Enables adding multiple service instances under the same service type.
92100

101+
menu "MDNS Predefined interfaces"
102+
103+
config MDNS_PREDEF_NETIF_STA
104+
bool "Use predefined interface for WiFi Station"
105+
default y
106+
help
107+
Set up mdns for the default WiFi station.
108+
Disable this option if you do not need mDNS on default WiFi STA.
109+
110+
config MDNS_PREDEF_NETIF_AP
111+
bool "Use predefined interface for WiFi Access Point"
112+
default y
113+
help
114+
Set up mdns for the default WiFi Access Point.
115+
Disable this option if you do not need mDNS on default WiFi AP.
116+
117+
config MDNS_PREDEF_NETIF_ETH
118+
bool "Use predefined interface for Ethernet"
119+
default y
120+
help
121+
Set up mdns for the default Ethernet interface.
122+
Disable this option if you do not need mDNS on default Ethernet.
123+
124+
endmenu # MDNS Predefined interfaces
125+
93126
endmenu

components/mdns/include/mdns.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ extern "C" {
2121
#define MDNS_TYPE_NSEC 0x002F
2222
#define MDNS_TYPE_ANY 0x00FF
2323

24-
#define CONFIG_MDNS_IF_MAX 4
25-
2624
/**
2725
* @brief Asynchronous query handle
2826
*/
@@ -64,11 +62,6 @@ typedef struct mdns_ip_addr_s {
6462
struct mdns_ip_addr_s * next; /*!< next IP, or NULL for the last IP in the list */
6563
} mdns_ip_addr_t;
6664

67-
typedef enum mdns_if_internal {
68-
MDNS_IF_INVALID = -1,
69-
MDNS_IF_MAX = CONFIG_MDNS_IF_MAX
70-
} mdns_if_t;
71-
7265
/**
7366
* @brief mDNS query type to be explicitly set to either Unicast or Multicast
7467
*/
@@ -83,7 +76,7 @@ typedef enum {
8376
typedef struct mdns_result_s {
8477
struct mdns_result_s * next; /*!< next result, or NULL for the last result in the list */
8578

86-
mdns_if_t tcpip_if; /*!< interface index */
79+
esp_netif_t* esp_netif; /*!< ptr to corresponding esp-netif */
8780
uint32_t ttl; /*!< time to live */
8881

8982
mdns_ip_protocol_t ip_protocol; /*!< ip_protocol type of the interface (v4/v6) */

components/mdns/mdns.c

Lines changed: 80 additions & 67 deletions
Large diffs are not rendered by default.

components/mdns/mdns_console.c

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,28 @@
1-
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
2-
//
3-
// Licensed under the Apache License, Version 2.0 (the "License");
4-
// you may not use this file except in compliance with the License.
5-
// You may obtain a copy of the License at
6-
7-
// http://www.apache.org/licenses/LICENSE-2.0
8-
//
9-
// Unless required by applicable law or agreed to in writing, software
10-
// distributed under the License is distributed on an "AS IS" BASIS,
11-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-
// See the License for the specific language governing permissions and
13-
// limitations under the License.
1+
/*
2+
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
146
#include <stdio.h>
157
#include <string.h>
168
#include "esp_console.h"
179
#include "argtable3/argtable3.h"
1810
#include "mdns.h"
1911

20-
static const char * if_str[] = {"STA", "AP", "ETH", "MAX"};
2112
static const char * ip_protocol_str[] = {"V4", "V6", "MAX"};
2213

14+
static const char * if_str(esp_netif_t *netif)
15+
{
16+
return esp_netif_get_ifkey(netif);
17+
}
18+
2319
static void mdns_print_results(mdns_result_t * results)
2420
{
2521
mdns_result_t * r = results;
2622
mdns_ip_addr_t * a = NULL;
2723
int i = 1;
2824
while (r) {
29-
printf("%d: Interface: %s, Type: %s\n", i++, if_str[r->tcpip_if], ip_protocol_str[r->ip_protocol]);
25+
printf("%d: Interface: %s, Type: %s\n", i++, if_str(r->esp_netif), ip_protocol_str[r->ip_protocol]);
3026
if (r->instance_name) {
3127
printf(" PTR : %s\n", r->instance_name);
3228
}

components/mdns/mdns_networking_lwip.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ static void _udp_recv(void *arg, struct udp_pcb *upcb, struct pbuf *pb, const ip
134134
continue;
135135
}
136136

137-
packet->tcpip_if = MDNS_IF_MAX;
137+
packet->tcpip_if = MDNS_MAX_INTERFACES;
138138
packet->pb = this_pb;
139139
packet->src_port = rport;
140140
#if CONFIG_LWIP_IPV6
@@ -164,7 +164,7 @@ static void _udp_recv(void *arg, struct udp_pcb *upcb, struct pbuf *pb, const ip
164164
//lwip does not return the proper pcb if you have more than one for the same multicast address (but different interfaces)
165165
struct netif * netif = NULL;
166166
struct udp_pcb * pcb = NULL;
167-
for (i=0; i<MDNS_IF_MAX; i++) {
167+
for (i=0; i<MDNS_MAX_INTERFACES; i++) {
168168
pcb = _mdns_server->interfaces[i].pcbs[packet->ip_protocol].pcb;
169169
netif = esp_netif_get_netif_impl(_mdns_get_esp_netif(i));
170170
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
198198
*/
199199
static bool _udp_pcb_is_in_use(void){
200200
int i, p;
201-
for (i=0; i<MDNS_IF_MAX; i++) {
201+
for (i=0; i<MDNS_MAX_INTERFACES; i++) {
202202
for (p=0; p<MDNS_IP_PROTOCOL_MAX; p++) {
203203
if(_mdns_server->interfaces[i].pcbs[p].pcb){
204204
return true;

components/mdns/mdns_networking_socket.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ esp_err_t _mdns_pcb_deinit(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol)
101101
}
102102
}
103103

104-
for (int i=0; i<MDNS_IF_MAX; i++) {
104+
for (int i=0; i<MDNS_MAX_INTERFACES; i++) {
105105
for (int j=0; j<MDNS_IP_PROTOCOL_MAX; j++) {
106106
if (_mdns_server->interfaces[i].pcbs[j].pcb)
107107
// If any of the interfaces/protocol initialized
@@ -247,7 +247,7 @@ void sock_recv_task(void* arg)
247247
fd_set rfds;
248248
FD_ZERO(&rfds);
249249
int max_sock = -1;
250-
for (int i=0; i<MDNS_IF_MAX; i++) {
250+
for (int i=0; i<MDNS_MAX_INTERFACES; i++) {
251251
for (int j=0; j<MDNS_IP_PROTOCOL_MAX; j++) {
252252
int sock = pcb_to_sock(_mdns_server->interfaces[i].pcbs[j].pcb);
253253
if (sock >= 0) {
@@ -267,7 +267,7 @@ void sock_recv_task(void* arg)
267267
ESP_LOGE(TAG, "Select failed. errno=%d: %s", errno, strerror(errno));
268268
break;
269269
} else if (s > 0) {
270-
for (int tcpip_if=0; tcpip_if<MDNS_IF_MAX; tcpip_if++) {
270+
for (int tcpip_if=0; tcpip_if<MDNS_MAX_INTERFACES; tcpip_if++) {
271271
// Both protocols share once socket
272272
int sock = pcb_to_sock(_mdns_server->interfaces[tcpip_if].pcbs[MDNS_IP_PROTOCOL_V4].pcb);
273273
if (sock < 0) {

components/mdns/private_include/mdns_private.h

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,27 @@
4141
* any item in question field */
4242
#define MDNS_REPEAT_QUERY_IN_RESPONSE 1
4343
#endif
44+
45+
/** Number of predefined interfaces */
46+
#ifndef CONFIG_MDNS_PREDEF_NETIF_STA
47+
#define CONFIG_MDNS_PREDEF_NETIF_STA 0
48+
#endif
49+
#ifndef CONFIG_MDNS_PREDEF_NETIF_AP
50+
#define CONFIG_MDNS_PREDEF_NETIF_AP 0
51+
#endif
52+
#ifndef CONFIG_MDNS_PREDEF_NETIF_ETH
53+
#define CONFIG_MDNS_PREDEF_NETIF_ETH 0
54+
#endif
55+
#define MDNS_MAX_PREDEF_INTERFACES (CONFIG_MDNS_PREDEF_NETIF_STA + CONFIG_MDNS_PREDEF_NETIF_AP + CONFIG_MDNS_PREDEF_NETIF_ETH)
56+
57+
/** Number of configured interfaces */
58+
#if MDNS_MAX_PREDEF_INTERFACES > CONFIG_MDNS_MAX_INTERFACES
59+
#warning Number of configured interfaces is less then number of predefined interfaces. Please update CONFIG_MDNS_MAX_INTERFACES.
60+
#define MDNS_MAX_INTERFACES (MDNS_MAX_PREDEF_INTERFACES)
61+
#else
62+
#define MDNS_MAX_INTERFACES (CONFIG_MDNS_MAX_INTERFACES)
63+
#endif
64+
4465
/** The maximum number of services */
4566
#define MDNS_MAX_SERVICES CONFIG_MDNS_MAX_SERVICES
4667

@@ -150,6 +171,8 @@
150171
#define HOOK_MALLOC_FAILED ESP_LOGE(TAG, "Cannot allocate memory (line: %d, free heap: %d bytes)", __LINE__, esp_get_free_heap_size());
151172
#endif
152173

174+
typedef size_t mdns_if_t;
175+
153176
typedef enum {
154177
PCB_OFF, PCB_DUP, PCB_INIT,
155178
PCB_PROBE_1, PCB_PROBE_2, PCB_PROBE_3,
@@ -384,7 +407,7 @@ typedef struct mdns_search_once_s {
384407
typedef struct mdns_server_s {
385408
struct {
386409
mdns_pcb_t pcbs[MDNS_IP_PROTOCOL_MAX];
387-
} interfaces[MDNS_IF_MAX];
410+
} interfaces[MDNS_MAX_INTERFACES];
388411
const char * hostname;
389412
const char * instance;
390413
mdns_srv_item_t * services;

examples/protocols/mdns/main/mdns_example_main.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,6 @@ static void initialise_mdns(void)
8383
free(hostname);
8484
}
8585

86-
/* these strings match tcpip_adapter_if_t enumeration */
87-
static const char * if_str[] = {"STA", "AP", "ETH", "MAX"};
88-
8986
/* these strings match mdns_ip_protocol_t enumeration */
9087
static const char * ip_protocol_str[] = {"V4", "V6", "MAX"};
9188

@@ -95,7 +92,7 @@ static void mdns_print_results(mdns_result_t *results)
9592
mdns_ip_addr_t *a = NULL;
9693
int i = 1, t;
9794
while (r) {
98-
printf("%d: Interface: %s, Type: %s, TTL: %u\n", i++, if_str[r->tcpip_if], ip_protocol_str[r->ip_protocol],
95+
printf("%d: Interface: %s, Type: %s, TTL: %u\n", i++, esp_netif_get_ifkey(r->esp_netif), ip_protocol_str[r->ip_protocol],
9996
r->ttl);
10097
if (r->instance_name) {
10198
printf(" PTR : %s.%s.%s\n", r->instance_name, r->service_type, r->proto);

0 commit comments

Comments
 (0)