From 5f135741a144c8b4dee31f89b5c4e85378169a1d Mon Sep 17 00:00:00 2001 From: David Cermak Date: Wed, 2 Feb 2022 15:54:49 +0100 Subject: [PATCH] lwip/dhcpserver: Add return value to API that could fail --- components/esp_netif/lwip/esp_netif_lwip.c | 3 +- components/lwip/apps/dhcpserver/dhcpserver.c | 64 +++++++++++++------ .../lwip/include/apps/dhcpserver/dhcpserver.h | 23 ++++--- 3 files changed, 62 insertions(+), 28 deletions(-) diff --git a/components/esp_netif/lwip/esp_netif_lwip.c b/components/esp_netif/lwip/esp_netif_lwip.c index d04e5711ea..464bce971d 100644 --- a/components/esp_netif/lwip/esp_netif_lwip.c +++ b/components/esp_netif/lwip/esp_netif_lwip.c @@ -1698,7 +1698,8 @@ static esp_err_t esp_netif_get_dns_info_api(esp_netif_api_msg_t *msg) if (esp_netif->flags & ESP_NETIF_DHCP_SERVER) { #if ESP_DHCPS - ip4_addr_t dns_ip = dhcps_dns_getserver(esp_netif->dhcps); + ip4_addr_t dns_ip; + dhcps_dns_getserver(esp_netif->dhcps, &dns_ip); memcpy(&dns->ip.u_addr.ip4, &dns_ip, sizeof(ip4_addr_t)); #else LOG_NETIF_DISABLED_AND_DO("DHCP Server", return ESP_ERR_NOT_SUPPORTED); diff --git a/components/lwip/apps/dhcpserver/dhcpserver.c b/components/lwip/apps/dhcpserver/dhcpserver.c index 71a16ff91d..2107db15b3 100644 --- a/components/lwip/apps/dhcpserver/dhcpserver.c +++ b/components/lwip/apps/dhcpserver/dhcpserver.c @@ -111,7 +111,9 @@ static void dhcps_tmr(void* arg); dhcps_t *dhcps_new(void) { - dhcps_t *dhcps = calloc(1, sizeof(dhcps_t)); + dhcps_t *dhcps = mem_malloc(sizeof(dhcps_t)); + memset(dhcps , 0x00 , sizeof(dhcps_t)); + if (dhcps == NULL) { return NULL; } @@ -164,6 +166,9 @@ static void get_ip_info(struct netif * netif, ip_info_t *ip_info) void *dhcps_option_info(dhcps_t *dhcps, u8_t op_id, u32_t opt_len) { void *option_arg = NULL; + if (dhcps == NULL) { + return NULL; + } switch (op_id) { case IP_ADDRESS_LEASE_TIME: @@ -214,10 +219,10 @@ void *dhcps_option_info(dhcps_t *dhcps, u8_t op_id, u32_t opt_len) * opt_len -- DHCP message option length * Returns : none *******************************************************************************/ -void dhcps_set_option_info(dhcps_t *dhcps, u8_t op_id, void *opt_info, u32_t opt_len) +err_t dhcps_set_option_info(dhcps_t *dhcps, u8_t op_id, void *opt_info, u32_t opt_len) { - if (opt_info == NULL) { - return; + if (dhcps == NULL || opt_info == NULL) { + return ERR_ARG; } switch (op_id) { case IP_ADDRESS_LEASE_TIME: @@ -256,7 +261,7 @@ void dhcps_set_option_info(dhcps_t *dhcps, u8_t op_id, void *opt_info, u32_t opt default: break; } - return; + return ERR_OK; } /****************************************************************************** @@ -1178,11 +1183,15 @@ static void dhcps_poll_set(dhcps_t *dhcps, u32_t ip) * Description : set callback for dhcp server when it assign an IP * to the connected dhcp client * Parameters : cb -- callback for dhcp server - * Returns : none + * Returns : ERR_OK on success *******************************************************************************/ -void dhcps_set_new_lease_cb(dhcps_t *dhcps, dhcps_cb_t cb) +err_t dhcps_set_new_lease_cb(dhcps_t *dhcps, dhcps_cb_t cb) { + if (dhcps == NULL) { + return ERR_ARG; + } dhcps->dhcps_cb = cb; + return ERR_OK; } /****************************************************************************** @@ -1192,8 +1201,11 @@ void dhcps_set_new_lease_cb(dhcps_t *dhcps, dhcps_cb_t cb) * : info -- The current ip info * Returns : none *******************************************************************************/ -void dhcps_start(dhcps_t *dhcps, struct netif *netif, ip4_addr_t ip) +err_t dhcps_start(dhcps_t *dhcps, struct netif *netif, ip4_addr_t ip) { + if (dhcps == NULL || netif == NULL) { + return ERR_ARG; + } dhcps->dhcps_netif = netif; if (dhcps->dhcps_pcb != NULL) { udp_remove(dhcps->dhcps_pcb); @@ -1203,6 +1215,7 @@ void dhcps_start(dhcps_t *dhcps, struct netif *netif, ip4_addr_t ip) if (dhcps->dhcps_pcb == NULL || ip4_addr_isany_val(ip)) { printf("dhcps_start(): could not obtain pcb\n"); + return ERR_ARG; } IP4_ADDR(&dhcps->broadcast_dhcps, 255, 255, 255, 255); @@ -1219,7 +1232,7 @@ void dhcps_start(dhcps_t *dhcps, struct netif *netif, ip4_addr_t ip) #endif dhcps->state = DHCPS_HANDLE_STARTED; sys_timeout(DHCP_COARSE_TIMER_MSECS, dhcps_tmr, dhcps); - + return ERR_OK; } /****************************************************************************** @@ -1228,11 +1241,13 @@ void dhcps_start(dhcps_t *dhcps, struct netif *netif, ip4_addr_t ip) * Parameters : netif -- The current netif addr * Returns : none *******************************************************************************/ -void dhcps_stop(dhcps_t *dhcps, struct netif *netif) +err_t dhcps_stop(dhcps_t *dhcps, struct netif *netif) { - if (netif == NULL || dhcps->dhcps_netif != netif) { - printf("dhcps_stop: netif is NULL or invalid\n"); - return; + if (dhcps == NULL || netif == NULL || dhcps->dhcps_netif != netif) { +#if DHCPS_DEBUG + DHCPS_LOG("dhcps_stop: netif is NULL or invalid\n"); +#endif + return ERR_ARG; } if (dhcps->dhcps_pcb != NULL) { @@ -1256,6 +1271,7 @@ void dhcps_stop(dhcps_t *dhcps, struct netif *netif) } sys_untimeout(dhcps_tmr, dhcps); dhcps->state = DHCPS_HANDLE_STOPPED; + return ERR_OK; } /****************************************************************************** @@ -1356,6 +1372,10 @@ bool dhcp_search_ip_on_mac(dhcps_t *dhcps, u8_t *mac, ip4_addr_t *ip) list_node *pback_node = NULL; bool ret = false; + if (dhcps == NULL) { + return false; + } + for (pback_node = dhcps->plist; pback_node != NULL; pback_node = pback_node->pnext) { pdhcps_pool = pback_node->pnode; @@ -1373,16 +1393,19 @@ bool dhcp_search_ip_on_mac(dhcps_t *dhcps, u8_t *mac, ip4_addr_t *ip) * FunctionName : dhcps_dns_setserver * Description : set DNS server address for dhcpserver * Parameters : dnsserver -- The DNS server address - * Returns : none + * Returns : ERR_ARG if invalid handle, ERR_OK on success *******************************************************************************/ -void -dhcps_dns_setserver(dhcps_t *dhcps, const ip_addr_t *dnsserver) +err_t dhcps_dns_setserver(dhcps_t *dhcps, const ip_addr_t *dnsserver) { + if (dhcps == NULL) { + return ERR_ARG; + } if (dnsserver != NULL) { dhcps->dns_server = *(ip_2_ip4(dnsserver)); } else { dhcps->dns_server = *(ip_2_ip4(IP_ADDR_ANY)); } + return ERR_OK; } /****************************************************************************** @@ -1391,9 +1414,12 @@ dhcps_dns_setserver(dhcps_t *dhcps, const ip_addr_t *dnsserver) * Parameters : none * Returns : ip4_addr_t *******************************************************************************/ -ip4_addr_t -dhcps_dns_getserver(dhcps_t *dhcps) +err_t dhcps_dns_getserver(dhcps_t *dhcps, ip4_addr_t *dnsserver) { - return dhcps->dns_server; + if (dhcps) { + *dnsserver = dhcps->dns_server; + return ERR_OK; + } + return ERR_ARG; } #endif // ESP_DHCP diff --git a/components/lwip/include/apps/dhcpserver/dhcpserver.h b/components/lwip/include/apps/dhcpserver/dhcpserver.h index df82bde480..17d9a3c496 100644 --- a/components/lwip/include/apps/dhcpserver/dhcpserver.h +++ b/components/lwip/include/apps/dhcpserver/dhcpserver.h @@ -8,6 +8,7 @@ #include "sdkconfig.h" #include "lwip/ip_addr.h" +#include "lwip/err.h" #ifdef __cplusplus extern "C" { @@ -118,15 +119,17 @@ void dhcps_delete(dhcps_t *dhcps); * @param dhcps Pointer to the DHCP handle * @param netif Pointer to the lwIP's network interface struct * @param ip DHCP server's address + * @return ERR_ARG if invalid args, ERR_OK on success */ -void dhcps_start(dhcps_t *dhcps, struct netif *netif, ip4_addr_t ip); +err_t dhcps_start(dhcps_t *dhcps, struct netif *netif, ip4_addr_t ip); /** * @brief Stops the DHCP server on the specified netif * @param dhcps Pointer to the DHCP handle * @param netif Pointer to the lwIP's network interface struct + * @return ERR_ARG if invalid args, ERR_OK on success */ -void dhcps_stop(dhcps_t *dhcps, struct netif *netif); +err_t dhcps_stop(dhcps_t *dhcps, struct netif *netif); /** * @brief Gets the DHCP server option info @@ -143,8 +146,9 @@ void *dhcps_option_info(dhcps_t *dhcps, u8_t op_id, u32_t opt_len); * @param op_id DHCP message option id * @param opt_info DHCP message option info * @param opt_len DHCP message option length + * @return ERR_ARG if invalid args, ERR_OK on success */ -void dhcps_set_option_info(dhcps_t *dhcps, u8_t op_id, void *opt_info, u32_t opt_len); +err_t dhcps_set_option_info(dhcps_t *dhcps, u8_t op_id, void *opt_info, u32_t opt_len); /** * @brief Tries to find IP address corresponding to the supplied MAC @@ -159,22 +163,25 @@ bool dhcp_search_ip_on_mac(dhcps_t *dhcps, u8_t *mac, ip4_addr_t *ip); * @brief Sets DNS server address for the DHCP server * @param dhcps Pointer to the DHCP handle * @param dnsserver Address of the DNS server + * @return ERR_ARG if invalid handle, ERR_OK on success */ -void dhcps_dns_setserver(dhcps_t *dhcps, const ip_addr_t *dnsserver); +err_t dhcps_dns_setserver(dhcps_t *dhcps, const ip_addr_t *dnsserver); /** - * @brief Returns DNS server associated with this DHCP server + * @brief Gets DNS server associated with this DHCP server * @param dhcps Pointer to the DHCP handle - * @return Address of the DNS server + * @param dnsserver Address of the DNS server + * @return ERR_ARG if invalid handle, ERR_OK on success */ -ip4_addr_t dhcps_dns_getserver(dhcps_t *dhcps); +err_t dhcps_dns_getserver(dhcps_t *dhcps, ip4_addr_t *dnsserver); /** * @brief Sets callback on assigning an IP to the connected client * @param dhcps Pointer to the DHCP handle * @param cb Callback for dhcp server + * @return ERR_ARG if invalid handle, ERR_OK on success */ -void dhcps_set_new_lease_cb(dhcps_t *dhcps, dhcps_cb_t cb); +err_t dhcps_set_new_lease_cb(dhcps_t *dhcps, dhcps_cb_t cb); #ifdef __cplusplus }