mirror of
https://github.com/espressif/esp-idf.git
synced 2025-10-04 02:50:58 +02:00
fix(esp_netif): Restore DNS servers per netif when setting it default
Introducing config option `CONFIG_ESP_NETIF_SET_DNS_PER_DEFAULT_NETIF` to overcome LWIP limitation of using global DNS server info. This config option enables LWIP callbacks to collect per netif DNS server info and then restores global DNS servers of whichever network interface is selected as default. LWIP submodule update: git log --oneline aa4f6e78..3a3d1fb3 - dns: Allow storing dnsserver per netif (espressif/esp-lwip@3a3d1fb3)
This commit is contained in:
@@ -114,16 +114,6 @@ do {
|
||||
action; \
|
||||
} while(0)
|
||||
|
||||
//
|
||||
// Internal types
|
||||
//
|
||||
typedef enum esp_netif_action {
|
||||
ESP_NETIF_UNDEF,
|
||||
ESP_NETIF_STARTED,
|
||||
ESP_NETIF_STOPPED,
|
||||
ESP_NETIF_SET_DEFAULT,
|
||||
} esp_netif_action_t;
|
||||
|
||||
//
|
||||
// Internal variables for this module
|
||||
//
|
||||
@@ -307,6 +297,11 @@ static void esp_netif_set_default_netif_internal(esp_netif_t *esp_netif)
|
||||
} else {
|
||||
netif_set_default(esp_netif->lwip_netif);
|
||||
}
|
||||
#ifdef CONFIG_ESP_NETIF_SET_DNS_PER_DEFAULT_NETIF
|
||||
for (int i = 0; i < DNS_MAX_SERVERS; ++i) {
|
||||
dns_setserver(i, &esp_netif->dns[i]);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -317,7 +312,7 @@ static void esp_netif_set_default_netif_internal(esp_netif_t *esp_netif)
|
||||
static esp_err_t esp_netif_update_default_netif_lwip(esp_netif_api_msg_t *msg)
|
||||
{
|
||||
esp_netif_t *esp_netif = msg->esp_netif;
|
||||
esp_netif_action_t action = (esp_netif_action_t)msg->data;
|
||||
esp_netif_route_prio_action_t action = (esp_netif_route_prio_action_t)msg->data;
|
||||
|
||||
ESP_LOGD(TAG, "%s %p", __func__, esp_netif);
|
||||
|
||||
@@ -337,6 +332,7 @@ static esp_err_t esp_netif_update_default_netif_lwip(esp_netif_api_msg_t *msg)
|
||||
esp_netif_set_default_netif_internal(s_last_default_esp_netif);
|
||||
break;
|
||||
case ESP_NETIF_STARTED:
|
||||
case ESP_NETIF_GOT_IP:
|
||||
{
|
||||
// check if previously default interface hasn't been destroyed in the meantime
|
||||
s_last_default_esp_netif = esp_netif_is_active(s_last_default_esp_netif);
|
||||
@@ -352,6 +348,7 @@ static esp_err_t esp_netif_update_default_netif_lwip(esp_netif_api_msg_t *msg)
|
||||
|
||||
default:
|
||||
case ESP_NETIF_STOPPED:
|
||||
case ESP_NETIF_LOST_IP:
|
||||
{
|
||||
s_last_default_esp_netif = NULL;
|
||||
esp_netif_t *netif = esp_netif_next_unsafe(NULL);
|
||||
@@ -383,7 +380,7 @@ static esp_err_t esp_netif_update_default_netif_lwip(esp_netif_api_msg_t *msg)
|
||||
* @param esp_netif current interface which just updated state
|
||||
* @param action updating action (on-off)
|
||||
*/
|
||||
static esp_err_t esp_netif_update_default_netif(esp_netif_t *esp_netif, esp_netif_action_t action)
|
||||
esp_err_t esp_netif_update_default_netif(esp_netif_t *esp_netif, esp_netif_route_prio_action_t action)
|
||||
{
|
||||
return esp_netif_lwip_ipc_call(esp_netif_update_default_netif_lwip, esp_netif, (void*)action);
|
||||
}
|
||||
@@ -503,6 +500,24 @@ void* esp_netif_get_netif_impl(esp_netif_t *esp_netif)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#if CONFIG_ESP_NETIF_SET_DNS_PER_DEFAULT_NETIF
|
||||
static void store_dnsserver_info(struct netif* netif, u8_t numdns, const ip_addr_t *dnsserver)
|
||||
{
|
||||
if (netif == NULL) {
|
||||
return;
|
||||
}
|
||||
esp_netif_t *esp_netif = lwip_get_esp_netif(netif);
|
||||
if (esp_netif == NULL || !esp_netif_is_netif_listed(esp_netif)) {
|
||||
return;
|
||||
}
|
||||
if (!ip_addr_isany(dnsserver)) {
|
||||
ip_addr_copy(esp_netif->dns[numdns], *dnsserver);
|
||||
} else {
|
||||
ip_addr_copy(esp_netif->dns[numdns], *IP_ADDR_ANY);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
static void tcpip_init_done(void *arg)
|
||||
{
|
||||
sys_sem_t *init_sem = arg;
|
||||
@@ -546,6 +561,12 @@ esp_err_t esp_netif_init(void)
|
||||
sys_sem_wait(&init_sem);
|
||||
sys_sem_free(&init_sem);
|
||||
ESP_LOGD(TAG, "LwIP stack has been initialized");
|
||||
#if CONFIG_ESP_NETIF_SET_DNS_PER_DEFAULT_NETIF
|
||||
if (dns_setserver_callback(store_dnsserver_info) != ERR_OK) {
|
||||
ESP_LOGE(TAG, "Feiled to configure DNS set server callback");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#if !LWIP_TCPIP_CORE_LOCKING
|
||||
@@ -1075,11 +1096,7 @@ static esp_err_t esp_netif_start_api(esp_netif_api_msg_t *msg)
|
||||
ESP_LOGD(TAG, "%s %p", __func__, esp_netif);
|
||||
if (ESP_NETIF_IS_POINT2POINT_TYPE(esp_netif, PPP_LWIP_NETIF)) {
|
||||
#if CONFIG_PPP_SUPPORT
|
||||
esp_err_t ret = esp_netif_start_ppp(esp_netif);
|
||||
if (ret == ESP_OK) {
|
||||
esp_netif_update_default_netif(esp_netif, ESP_NETIF_STARTED);
|
||||
}
|
||||
return ret;
|
||||
return esp_netif_start_ppp(esp_netif);
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -1156,8 +1173,10 @@ static esp_err_t esp_netif_start_api(esp_netif_api_msg_t *msg)
|
||||
LOG_NETIF_DISABLED_AND_DO("IPv4's DHCP Client", return ESP_ERR_NOT_SUPPORTED);
|
||||
#endif
|
||||
}
|
||||
|
||||
esp_netif_update_default_netif(esp_netif, ESP_NETIF_STARTED);
|
||||
// For netifs with (active) DHCP client: we update the default netif after getting a valid IP
|
||||
if (!((esp_netif->flags & ESP_NETIF_DHCP_CLIENT) && esp_netif->dhcpc_status != ESP_NETIF_DHCP_STOPPED)) {
|
||||
esp_netif_update_default_netif(esp_netif, ESP_NETIF_STARTED);
|
||||
}
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
@@ -1371,7 +1390,7 @@ static void esp_netif_internal_dhcpc_cb(struct netif *netif)
|
||||
if (memcmp(ip_info, ip_info_old, sizeof(esp_netif_ip_info_t))) {
|
||||
evt.ip_changed = true;
|
||||
}
|
||||
|
||||
esp_netif_update_default_netif(esp_netif, ESP_NETIF_GOT_IP);
|
||||
memcpy(&evt.ip_info, ip_info, sizeof(esp_netif_ip_info_t));
|
||||
memcpy(ip_info_old, ip_info, sizeof(esp_netif_ip_info_t));
|
||||
ESP_LOGD(TAG, "if%p ip changed=%d", esp_netif, evt.ip_changed);
|
||||
@@ -1412,7 +1431,7 @@ static void esp_netif_ip_lost_timer(void *arg)
|
||||
.esp_netif = esp_netif,
|
||||
};
|
||||
int ret;
|
||||
|
||||
esp_netif_update_default_netif(esp_netif, ESP_NETIF_LOST_IP);
|
||||
ESP_LOGD(TAG, "if%p ip lost tmr: raise ip lost event", esp_netif);
|
||||
memset(esp_netif->ip_info_old, 0, sizeof(esp_netif_ip_info_t));
|
||||
if (esp_netif->lost_ip_event) {
|
||||
@@ -1731,7 +1750,10 @@ static esp_err_t esp_netif_up_api(esp_netif_api_msg_t *msg)
|
||||
netif_set_up(lwip_netif);
|
||||
netif_set_link_up(lwip_netif);
|
||||
|
||||
esp_netif_update_default_netif(esp_netif, ESP_NETIF_STARTED);
|
||||
// For netifs with (active) DHCP client: we update the default netif after getting a valid IP
|
||||
if (!((esp_netif->flags & ESP_NETIF_DHCP_CLIENT) && esp_netif->dhcpc_status != ESP_NETIF_DHCP_STOPPED)) {
|
||||
esp_netif_update_default_netif(esp_netif, ESP_NETIF_STARTED);
|
||||
}
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
@@ -1910,7 +1932,7 @@ static esp_err_t esp_netif_set_ip_info_api(esp_netif_api_msg_t *msg)
|
||||
if (memcmp(ip_info, esp_netif->ip_info_old, sizeof(esp_netif_ip_info_t))) {
|
||||
evt.ip_changed = true;
|
||||
}
|
||||
|
||||
esp_netif_update_default_netif(esp_netif, ESP_NETIF_GOT_IP);
|
||||
memcpy(&evt.ip_info, ip_info, sizeof(esp_netif_ip_info_t));
|
||||
memcpy(esp_netif->ip_info_old, ip_info, sizeof(esp_netif_ip_info_t));
|
||||
ret = esp_event_post(IP_EVENT, evt_id, &evt, sizeof(evt), 0);
|
||||
@@ -1971,7 +1993,7 @@ static esp_err_t esp_netif_set_dns_info_api(esp_netif_api_msg_t *msg)
|
||||
|
||||
ip_addr_t lwip_ip = {};
|
||||
ESPIP_TO_IP(&dns->ip, &lwip_ip);
|
||||
if (esp_netif->flags & ESP_NETIF_DHCP_SERVER) {
|
||||
if (esp_netif && esp_netif->flags & ESP_NETIF_DHCP_SERVER) {
|
||||
#if ESP_DHCPS
|
||||
// if DHCP server configured to set DNS in dhcps API
|
||||
if (type != ESP_NETIF_DNS_MAIN) {
|
||||
@@ -1984,7 +2006,17 @@ static esp_err_t esp_netif_set_dns_info_api(esp_netif_api_msg_t *msg)
|
||||
LOG_NETIF_DISABLED_AND_DO("DHCP Server", return ESP_ERR_NOT_SUPPORTED);
|
||||
#endif
|
||||
} else {
|
||||
#ifdef CONFIG_ESP_NETIF_SET_DNS_PER_DEFAULT_NETIF
|
||||
if (esp_netif) {
|
||||
store_dnsserver_info(esp_netif->lwip_netif, type, &lwip_ip);
|
||||
}
|
||||
if (esp_netif == s_last_default_esp_netif || // if this is the default one -> need to update global DNS servers
|
||||
esp_netif == NULL) { // if the netif ptr is set to NULL -> we explicitly require the update
|
||||
dns_setserver(type, &lwip_ip);
|
||||
}
|
||||
#else
|
||||
dns_setserver(type, &lwip_ip);
|
||||
#endif
|
||||
}
|
||||
|
||||
return ESP_OK;
|
||||
@@ -1992,9 +2024,11 @@ static esp_err_t esp_netif_set_dns_info_api(esp_netif_api_msg_t *msg)
|
||||
|
||||
esp_err_t esp_netif_set_dns_info(esp_netif_t *esp_netif, esp_netif_dns_type_t type, esp_netif_dns_info_t *dns)
|
||||
{
|
||||
#ifndef CONFIG_ESP_NETIF_SET_DNS_PER_DEFAULT_NETIF
|
||||
if (esp_netif == NULL) {
|
||||
return ESP_ERR_ESP_NETIF_INVALID_PARAMS;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (dns == NULL) {
|
||||
ESP_LOGD(TAG, "set dns null dns");
|
||||
@@ -2022,7 +2056,7 @@ static esp_err_t esp_netif_get_dns_info_api(esp_netif_api_msg_t *msg)
|
||||
|
||||
ESP_LOGD(TAG, "esp_netif_get_dns_info: esp_netif=%p type=%d", esp_netif, type);
|
||||
|
||||
if (esp_netif->flags & ESP_NETIF_DHCP_SERVER) {
|
||||
if (esp_netif && esp_netif->flags & ESP_NETIF_DHCP_SERVER) {
|
||||
#if ESP_DHCPS
|
||||
ip4_addr_t dns_ip;
|
||||
dhcps_dns_getserver(esp_netif->dhcps, &dns_ip);
|
||||
@@ -2033,7 +2067,15 @@ static esp_err_t esp_netif_get_dns_info_api(esp_netif_api_msg_t *msg)
|
||||
#endif
|
||||
} else {
|
||||
const ip_addr_t* dns_ip = NULL;
|
||||
#ifdef CONFIG_ESP_NETIF_SET_DNS_PER_DEFAULT_NETIF
|
||||
if (esp_netif == NULL) { // by setting esp_netif to NULL we require the global DNS server entry
|
||||
dns_ip = dns_getserver(type);
|
||||
} else {
|
||||
dns_ip = &esp_netif->dns[type];
|
||||
}
|
||||
#else
|
||||
dns_ip = dns_getserver(type);
|
||||
#endif
|
||||
if(dns_ip != NULL) {
|
||||
IP_TO_ESPIP(dns_ip, &dns->ip);
|
||||
}
|
||||
@@ -2044,9 +2086,11 @@ static esp_err_t esp_netif_get_dns_info_api(esp_netif_api_msg_t *msg)
|
||||
|
||||
esp_err_t esp_netif_get_dns_info(esp_netif_t *esp_netif, esp_netif_dns_type_t type, esp_netif_dns_info_t *dns)
|
||||
{
|
||||
#ifndef CONFIG_ESP_NETIF_SET_DNS_PER_DEFAULT_NETIF
|
||||
if (esp_netif == NULL) {
|
||||
return ESP_ERR_ESP_NETIF_INVALID_PARAMS;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (dns == NULL) {
|
||||
ESP_LOGE(TAG, "%s: dns_info cannot be NULL", __func__);
|
||||
@@ -2142,7 +2186,7 @@ static void esp_netif_internal_nd6_cb(struct netif *netif, uint8_t ip_index)
|
||||
ESP_LOGW(TAG,"CONFIG_LWIP_ESP_MLDV6_REPORT not enabled, but esp-netif configured with ESP_NETIF_FLAG_MLDV6_REPORT");
|
||||
#endif
|
||||
}
|
||||
|
||||
esp_netif_update_default_netif(esp_netif, ESP_NETIF_GOT_IP);
|
||||
memcpy(&evt.ip6_info, &ip6_info, sizeof(esp_netif_ip6_info_t));
|
||||
int ret = esp_event_post(IP_EVENT, IP_EVENT_GOT_IP6, &evt, sizeof(evt), 0);
|
||||
if (ESP_OK != ret) {
|
||||
@@ -2685,7 +2729,7 @@ static esp_err_t esp_netif_add_ip6_address_api(esp_netif_api_msg_t *msg)
|
||||
err_t err = netif_add_ip6_address(msg->esp_netif->lwip_netif, &ip6addr, &index);
|
||||
ESP_RETURN_ON_FALSE(err == ERR_OK && index >= 0, ESP_ERR_ESP_NETIF_IP6_ADDR_FAILED, TAG,
|
||||
"Failed to add ip6 address");
|
||||
|
||||
esp_netif_update_default_netif(msg->esp_netif, ESP_NETIF_GOT_IP);
|
||||
netif_ip6_addr_set_state(msg->esp_netif->lwip_netif, index,
|
||||
addr->preferred ? IP6_ADDR_PREFERRED : IP6_ADDR_DEPRECATED);
|
||||
ip_event_got_ip6_t evt = {.esp_netif = msg->esp_netif, .ip_index = index};
|
||||
|
Reference in New Issue
Block a user