From 89bb1fdec1da8df853827d847cb1bdb887b748fe Mon Sep 17 00:00:00 2001 From: David Cermak Date: Mon, 27 Jun 2022 09:12:18 +0200 Subject: [PATCH] esp_netif: Add error checks to dhcp server state transitions --- components/esp_common/src/esp_err_to_name.c | 3 +++ .../esp_netif/include/esp_netif_types.h | 2 ++ components/esp_netif/lwip/esp_netif_lwip.c | 20 ++++++++++++------- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/components/esp_common/src/esp_err_to_name.c b/components/esp_common/src/esp_err_to_name.c index 19c1ec5823..2001b036d2 100644 --- a/components/esp_common/src/esp_err_to_name.c +++ b/components/esp_common/src/esp_err_to_name.c @@ -560,6 +560,9 @@ static const esp_err_msg_t esp_err_msg_table[] = { # endif # ifdef ESP_ERR_ESP_NETIF_IP6_ADDR_FAILED ERR_TBL_IT(ESP_ERR_ESP_NETIF_IP6_ADDR_FAILED), /* 20492 0x500c */ +# endif +# ifdef ESP_ERR_ESP_NETIF_DHCPS_START_FAILED + ERR_TBL_IT(ESP_ERR_ESP_NETIF_DHCPS_START_FAILED), /* 20493 0x500d */ # endif // components/esp_common/include/esp_err.h # ifdef ESP_ERR_FLASH_BASE diff --git a/components/esp_netif/include/esp_netif_types.h b/components/esp_netif/include/esp_netif_types.h index 7ec7683007..962fd62048 100644 --- a/components/esp_netif/include/esp_netif_types.h +++ b/components/esp_netif/include/esp_netif_types.h @@ -33,6 +33,8 @@ extern "C" { #define ESP_ERR_ESP_NETIF_DNS_NOT_CONFIGURED ESP_ERR_ESP_NETIF_BASE + 0x0A #define ESP_ERR_ESP_NETIF_MLD6_FAILED ESP_ERR_ESP_NETIF_BASE + 0x0B #define ESP_ERR_ESP_NETIF_IP6_ADDR_FAILED ESP_ERR_ESP_NETIF_BASE + 0x0C +#define ESP_ERR_ESP_NETIF_DHCPS_START_FAILED ESP_ERR_ESP_NETIF_BASE + 0x0D + /** * @brief Definition of ESP-NETIF bridge controll diff --git a/components/esp_netif/lwip/esp_netif_lwip.c b/components/esp_netif/lwip/esp_netif_lwip.c index 0d86b9413a..a70d40d1e2 100644 --- a/components/esp_netif/lwip/esp_netif_lwip.c +++ b/components/esp_netif/lwip/esp_netif_lwip.c @@ -946,7 +946,11 @@ static esp_err_t esp_netif_start_api(esp_netif_api_msg_t *msg) memcpy(&lwip_netmask, &default_ip->netmask, sizeof(struct ip4_addr)); dhcps_set_new_lease_cb(esp_netif->dhcps, esp_netif_dhcps_cb, esp_netif); dhcps_set_option_info(esp_netif->dhcps, SUBNET_MASK, (void*)&lwip_netmask, sizeof(lwip_netmask)); - dhcps_start(esp_netif->dhcps, p_netif, lwip_ip); + if (dhcps_start(esp_netif->dhcps, p_netif, lwip_ip) != ERR_OK) { + ESP_LOGE(TAG, "DHCP server cannot be started"); + esp_netif->dhcps_status = ESP_NETIF_DHCP_INIT; + return ESP_ERR_ESP_NETIF_DHCPS_START_FAILED; + } esp_netif->dhcps_status = ESP_NETIF_DHCP_STARTED; ESP_LOGD(TAG, "DHCP server started successfully"); esp_netif_update_default_netif(esp_netif, ESP_NETIF_STARTED); @@ -1016,8 +1020,8 @@ static esp_err_t esp_netif_stop_api(esp_netif_api_msg_t *msg) if (esp_netif->flags & ESP_NETIF_DHCP_SERVER) { #if ESP_DHCPS - dhcps_stop(esp_netif->dhcps, lwip_netif); // TODO(IDF-1099): dhcps checks status by its self - if (ESP_NETIF_DHCP_STOPPED != esp_netif->dhcps_status) { + if (dhcps_stop(esp_netif->dhcps, lwip_netif) != ERR_OK || + esp_netif->dhcps_status != ESP_NETIF_DHCP_STOPPED) { esp_netif->dhcps_status = ESP_NETIF_DHCP_INIT; } #else @@ -1360,7 +1364,11 @@ static esp_err_t esp_netif_dhcps_start_api(esp_netif_api_msg_t *msg) memcpy(&lwip_netmask, &default_ip->netmask, sizeof(struct ip4_addr)); dhcps_set_new_lease_cb(esp_netif->dhcps, esp_netif_dhcps_cb, esp_netif); dhcps_set_option_info(esp_netif->dhcps, SUBNET_MASK, (void*)&lwip_netmask, sizeof(lwip_netmask)); - dhcps_start(esp_netif->dhcps, p_netif, lwip_ip); + if (dhcps_start(esp_netif->dhcps, p_netif, lwip_ip) != ERR_OK) { + ESP_LOGE(TAG, "DHCP server cannot be started"); + esp_netif->dhcps_status = ESP_NETIF_DHCP_INIT; + return ESP_ERR_ESP_NETIF_DHCPS_START_FAILED; + } esp_netif->dhcps_status = ESP_NETIF_DHCP_STARTED; ESP_LOGD(TAG, "DHCP server started successfully"); return ESP_OK; @@ -1385,9 +1393,7 @@ static esp_err_t esp_netif_dhcps_stop_api(esp_netif_api_msg_t *msg) struct netif *p_netif = esp_netif->lwip_netif; if (esp_netif->dhcps_status == ESP_NETIF_DHCP_STARTED) { - if (p_netif != NULL) { - dhcps_stop(esp_netif->dhcps, p_netif); - } else { + if (dhcps_stop(esp_netif->dhcps, p_netif) != ERR_OK) { ESP_LOGD(TAG, "dhcp server if not ready"); return ESP_ERR_ESP_NETIF_IF_NOT_READY; }