Skip to content

Commit c956b6f

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

File tree

6 files changed

+26
-10
lines changed

6 files changed

+26
-10
lines changed

examples/esp_netif/multiple_netifs/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ 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+
This example demonstrate how DNS servers could be handled on network interface level, as lwIP used global DNS server information. All network interfaces store the DNS info upon acquiring an IP in the internal structure and the DNS servers are restored if host name resolution fails.
23+
If `CONFIG_ESP_NETIF_SET_DNS_PER_DEFAULT_NETIF` is supported and enabled, the DNS server update per network interface is handled automatically in IDF.
24+
2225
### Hardware Required
2326

2427
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/ethernet_connect.c

Lines changed: 1 addition & 1 deletion
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);

examples/esp_netif/multiple_netifs/main/multi_netif_main.c

Lines changed: 17 additions & 7 deletions
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,13 +69,15 @@ 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+
// For demonstration purposes we clear DNS table every iteration to exercise
80+
// a condition of DNS servers being misconfigured
7981
dns_clear_cache();
8082
vTaskDelay(pdMS_TO_TICKS(2000));
8183
ssize_t i = get_default(ifaces, num_of_ifaces);
@@ -91,7 +93,9 @@ void app_main(void)
9193
continue;
9294
}
9395
if (connect_status == ESP_ERR_NOT_FOUND) {
96+
#ifndef CONFIG_ESP_NETIF_SET_DNS_PER_DEFAULT_NETIF
9497
// set the default DNS info to global DNS server list
98+
// manually if DNS_PER_DEFAULT_NETIF if OFF or not-supported
9599
for (int j = 0; j < 2; ++j) {
96100
esp_netif_dns_info_t dns_info;
97101
esp_netif_get_dns_info(ifaces[i]->netif, j, &dns_info);
@@ -102,6 +106,12 @@ void app_main(void)
102106
ESP_LOGI(TAG, "Reconfigured DNS%i=" IPSTR, j, IP2STR(&ifaces[i]->dns[j].ip.u_addr.ip4));
103107
}
104108
}
109+
#else
110+
// simulate that the (default) netif is brought UP
111+
// this is only needed, since we explicitly clear DNS servers every iteration using dns_clear_cache()
112+
// (for demonstration purpose only, won't be needed in your project, unless you delete DNS info for some reasons)
113+
esp_netif_action_connected(ifaces[i]->netif, NULL, 0, NULL);
114+
#endif
105115
}
106116
if (connect_status == ESP_FAIL) {
107117
ESP_LOGE(TAG, "No connection via the default netif!");

examples/esp_netif/multiple_netifs/main/ppp_connect.c

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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);
Lines changed: 3 additions & 0 deletions
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)