Skip to content

Commit bc8ac4c

Browse files
committed
fix(examples): Make multi-netif example working with DNS_PER_DEFAULT_NETIF feature
1 parent b710dbd commit bc8ac4c

File tree

7 files changed

+47
-14
lines changed

7 files changed

+47
-14
lines changed

examples/esp_netif/multiple_netifs/README.md

+8
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ This example demonstrates working with multiple different interfaces with differ
1919
* It tries to reconfigure DNS server if host name resolution fails
2020
* It tries to manually change the default interface if connection fails
2121

22+
### Handling DNS server across interfaces
23+
24+
This example also demonstrates how DNS servers could be handled on network interface level, as lwIP used global DNS server information.
25+
26+
All network interfaces store their DNS info upon acquiring an IP in the internal structure (in the application code) and the DNS servers are restored if host name resolution fails.
27+
28+
This functionality is handled in IDF (supported from v5.3) automatically if `CONFIG_ESP_NETIF_SET_DNS_PER_DEFAULT_NETIF` is enabled, the DNS server info keeps updating per network interface in IDF layers. This examples uses the IDF functionality if `CONFIG_ESP_NETIF_SET_DNS_PER_DEFAULT_NETIF=1`.
29+
2230
### Hardware Required
2331

2432
To run this example, it's recommended that you have an official ESP32 Ethernet development board - [ESP32-Ethernet-Kit](https://docs.espressif.com/projects/esp-idf/en/latest/hw-reference/get-started-ethernet-kit.html).

examples/esp_netif/multiple_netifs/main/Kconfig.projbuild

+10
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,14 @@ menu "Example Configuration"
5454
bool "Using simple UART-PPP driver"
5555
endchoice
5656

57+
config EXAMPLE_DEMONSTRATE_DNS_CLEAR_CACHE
58+
bool "Run DNS clear cache"
59+
default n
60+
help
61+
This example will cleanup the DNS cache
62+
every iteration of the demo network operation.
63+
This forces the TCP/IP stack to always resolve DNS names,
64+
thus exercising potentially invalid DNS configuration.
65+
Set this to "y" for testing, but keep as "n" for production.
66+
5767
endmenu

examples/esp_netif/multiple_netifs/main/ethernet_connect.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ static void eth_destroy(iface_info_t *info)
9595
free(eth_info);
9696
}
9797

98-
iface_info_t *eth_init(int prio)
98+
iface_info_t *example_eth_init(int prio)
9999
{
100100
struct eth_info_t *eth_info = malloc(sizeof(struct eth_info_t));
101101
assert(eth_info);
@@ -124,7 +124,7 @@ iface_info_t *eth_init(int prio)
124124
eth_info->parent.netif = esp_netif_new(&cfg);
125125
eth_info->glue = esp_eth_new_netif_glue(eth_info->eth_handle);
126126
// Attach Ethernet driver to TCP/IP stack
127-
ESP_ERROR_CHECK(esp_netif_attach(eth_info->parent.netif, eth_info->glue ));
127+
ESP_ERROR_CHECK(esp_netif_attach(eth_info->parent.netif, eth_info->glue));
128128

129129
// Register user defined event handers
130130
ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, eth_info));

examples/esp_netif/multiple_netifs/main/multi_netif_main.c

+19-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Unlicense OR CC0-1.0
55
*/
@@ -23,9 +23,9 @@
2323
#include "nvs_flash.h"
2424
#include "iface_info.h"
2525

26-
iface_info_t *eth_init(int prio);
27-
iface_info_t *wifi_init(int prio);
28-
iface_info_t *ppp_init(int prio);
26+
iface_info_t *example_eth_init(int prio);
27+
iface_info_t *example_wifi_init(int prio);
28+
iface_info_t *example_ppp_init(int prio);
2929
esp_err_t check_connectivity(const char *host);
3030

3131
#define HOST "www.espressif.com"
@@ -69,14 +69,18 @@ void app_main(void)
6969

7070
// all interfaces
7171
iface_info_t *ifaces[] = {
72-
eth_init(ETH_PRIO),
73-
wifi_init(WIFI_PRIO),
74-
ppp_init(PPP_PRIO),
72+
example_eth_init(ETH_PRIO),
73+
example_wifi_init(WIFI_PRIO),
74+
example_ppp_init(PPP_PRIO),
7575
};
7676
size_t num_of_ifaces = sizeof(ifaces) / sizeof(ifaces[0]);
7777

7878
while (true) {
79+
#ifdef CONFIG_EXAMPLE_DEMONSTRATE_DNS_CLEAR_CACHE
80+
// For demonstration purposes we clear DNS table every iteration to exercise
81+
// a condition of DNS servers being misconfigured
7982
dns_clear_cache();
83+
#endif
8084
vTaskDelay(pdMS_TO_TICKS(2000));
8185
ssize_t i = get_default(ifaces, num_of_ifaces);
8286
if (i == -1) { // default netif is NULL, probably all interfaces are down -> retry
@@ -91,7 +95,9 @@ void app_main(void)
9195
continue;
9296
}
9397
if (connect_status == ESP_ERR_NOT_FOUND) {
98+
#ifndef CONFIG_ESP_NETIF_SET_DNS_PER_DEFAULT_NETIF
9499
// set the default DNS info to global DNS server list
100+
// manually if DNS_PER_DEFAULT_NETIF if OFF or not-supported
95101
for (int j = 0; j < 2; ++j) {
96102
esp_netif_dns_info_t dns_info;
97103
esp_netif_get_dns_info(ifaces[i]->netif, j, &dns_info);
@@ -102,6 +108,12 @@ void app_main(void)
102108
ESP_LOGI(TAG, "Reconfigured DNS%i=" IPSTR, j, IP2STR(&ifaces[i]->dns[j].ip.u_addr.ip4));
103109
}
104110
}
111+
#else
112+
// simulate that the (default) netif is brought UP
113+
// this is only needed, since we explicitly clear DNS servers every iteration using dns_clear_cache()
114+
// (for demonstration purpose only, won't be needed in your project, unless you delete DNS info for some reasons)
115+
esp_netif_action_connected(ifaces[i]->netif, NULL, 0, NULL);
116+
#endif
105117
}
106118
if (connect_status == ESP_FAIL) {
107119
ESP_LOGE(TAG, "No connection via the default netif!");

examples/esp_netif/multiple_netifs/main/ppp_connect.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ static void ppp_destroy(iface_info_t *info)
8585
free(info);
8686
}
8787

88-
iface_info_t *init_ppp(int prio)
88+
iface_info_t *example_ppp_init(int prio)
8989
{
9090
struct ppp_info_t *ppp_info = calloc(1, sizeof(struct ppp_info_t));
9191
assert(ppp_info);

examples/esp_netif/multiple_netifs/main/wifi_connect.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ static void wifi_destroy(iface_info_t *info)
7575
free(info);
7676
}
7777

78-
iface_info_t *wifi_init(int prio)
78+
iface_info_t *example_wifi_init(int prio)
7979
{
8080
struct iface_info_t *wifi_info = malloc(sizeof(iface_info_t));
8181
assert(wifi_info);
@@ -100,9 +100,9 @@ iface_info_t *wifi_init(int prio)
100100
.password = CONFIG_ESP_WIFI_PASSWORD,
101101
},
102102
};
103-
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
104-
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config) );
105-
ESP_ERROR_CHECK(esp_wifi_start() );
103+
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
104+
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
105+
ESP_ERROR_CHECK(esp_wifi_start());
106106

107107
ESP_LOGI(TAG, "wifi_init_sta finished.");
108108

Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1+
# You can use CONFIG_ESP_NETIF_SET_DNS_PER_DEFAULT_NETIF
2+
# to perform DNS server updates automatically in esp_netif layers
3+
# instead of manually as it is demonstrated in this example
14
CONFIG_LWIP_PPP_SUPPORT=y
25
CONFIG_LWIP_PPP_NOTIFY_PHASE_SUPPORT=y

0 commit comments

Comments
 (0)