Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[examples]: Make multi-netif example working with DNS_PER_DEFAULT_NETIF #609

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions examples/esp_netif/multiple_netifs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ This example demonstrates working with multiple different interfaces with differ
* It tries to reconfigure DNS server if host name resolution fails
* It tries to manually change the default interface if connection fails

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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be worth to add a subsection for the DNS part to improve reading.

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.

### Hardware Required

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).
Expand Down
2 changes: 1 addition & 1 deletion examples/esp_netif/multiple_netifs/main/ethernet_connect.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ static void eth_destroy(iface_info_t *info)
free(eth_info);
}

iface_info_t *eth_init(int prio)
iface_info_t *example_eth_init(int prio)
{
struct eth_info_t *eth_info = malloc(sizeof(struct eth_info_t));
assert(eth_info);
Expand Down
24 changes: 17 additions & 7 deletions examples/esp_netif/multiple_netifs/main/multi_netif_main.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
Expand All @@ -23,9 +23,9 @@
#include "nvs_flash.h"
#include "iface_info.h"

iface_info_t *eth_init(int prio);
iface_info_t *wifi_init(int prio);
iface_info_t *ppp_init(int prio);
iface_info_t *example_eth_init(int prio);
iface_info_t *example_wifi_init(int prio);
iface_info_t *example_ppp_init(int prio);
esp_err_t check_connectivity(const char *host);

#define HOST "www.espressif.com"
Expand Down Expand Up @@ -69,13 +69,15 @@ void app_main(void)

// all interfaces
iface_info_t *ifaces[] = {
eth_init(ETH_PRIO),
wifi_init(WIFI_PRIO),
ppp_init(PPP_PRIO),
example_eth_init(ETH_PRIO),
example_wifi_init(WIFI_PRIO),
example_ppp_init(PPP_PRIO),
};
size_t num_of_ifaces = sizeof(ifaces) / sizeof(ifaces[0]);

while (true) {
// For demonstration purposes we clear DNS table every iteration to exercise
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we try to move this out of a loop and have it set twice only? One with the regular expected way users would do and after that we clean and do as demo? We could consider keep the loop as is as long as we have once the expected way clearly stated.

// a condition of DNS servers being misconfigured
dns_clear_cache();
vTaskDelay(pdMS_TO_TICKS(2000));
ssize_t i = get_default(ifaces, num_of_ifaces);
Expand All @@ -91,7 +93,9 @@ void app_main(void)
continue;
}
if (connect_status == ESP_ERR_NOT_FOUND) {
#ifndef CONFIG_ESP_NETIF_SET_DNS_PER_DEFAULT_NETIF
// set the default DNS info to global DNS server list
// manually if DNS_PER_DEFAULT_NETIF if OFF or not-supported
for (int j = 0; j < 2; ++j) {
esp_netif_dns_info_t dns_info;
esp_netif_get_dns_info(ifaces[i]->netif, j, &dns_info);
Expand All @@ -102,6 +106,12 @@ void app_main(void)
ESP_LOGI(TAG, "Reconfigured DNS%i=" IPSTR, j, IP2STR(&ifaces[i]->dns[j].ip.u_addr.ip4));
}
}
#else
// simulate that the (default) netif is brought UP
// this is only needed, since we explicitly clear DNS servers every iteration using dns_clear_cache()
// (for demonstration purpose only, won't be needed in your project, unless you delete DNS info for some reasons)
esp_netif_action_connected(ifaces[i]->netif, NULL, 0, NULL);
#endif
}
if (connect_status == ESP_FAIL) {
ESP_LOGE(TAG, "No connection via the default netif!");
Expand Down
2 changes: 1 addition & 1 deletion examples/esp_netif/multiple_netifs/main/ppp_connect.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ static void ppp_destroy(iface_info_t *info)
free(info);
}

iface_info_t *init_ppp(int prio)
iface_info_t *example_ppp_init(int prio)
{
struct ppp_info_t *ppp_info = calloc(1, sizeof(struct ppp_info_t));
assert(ppp_info);
Expand Down
2 changes: 1 addition & 1 deletion examples/esp_netif/multiple_netifs/main/wifi_connect.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ static void wifi_destroy(iface_info_t *info)
free(info);
}

iface_info_t *wifi_init(int prio)
iface_info_t *example_wifi_init(int prio)
{
struct iface_info_t *wifi_info = malloc(sizeof(iface_info_t));
assert(wifi_info);
Expand Down
3 changes: 3 additions & 0 deletions examples/esp_netif/multiple_netifs/sdkconfig.defaults
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# You can use CONFIG_ESP_NETIF_SET_DNS_PER_DEFAULT_NETIF
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we expect this to be the default for users? If so, we should enable it here and point in README how to disable.

# to perform DNS server updates automatically in esp_netif layers
# instead of manually as it is demonstrated in this example
CONFIG_LWIP_PPP_SUPPORT=y
CONFIG_LWIP_PPP_NOTIFY_PHASE_SUPPORT=y
Loading