lwip/dhcpserver: Add return value to API that could fail

This commit is contained in:
David Cermak 2022-02-02 15:54:49 +01:00
parent 76e9d3cad0
commit 5f135741a1
3 changed files with 62 additions and 28 deletions

View File

@ -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_netif->flags & ESP_NETIF_DHCP_SERVER) {
#if ESP_DHCPS #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)); memcpy(&dns->ip.u_addr.ip4, &dns_ip, sizeof(ip4_addr_t));
#else #else
LOG_NETIF_DISABLED_AND_DO("DHCP Server", return ESP_ERR_NOT_SUPPORTED); LOG_NETIF_DISABLED_AND_DO("DHCP Server", return ESP_ERR_NOT_SUPPORTED);

View File

@ -111,7 +111,9 @@ static void dhcps_tmr(void* arg);
dhcps_t *dhcps_new(void) 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) { if (dhcps == NULL) {
return 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 *dhcps_option_info(dhcps_t *dhcps, u8_t op_id, u32_t opt_len)
{ {
void *option_arg = NULL; void *option_arg = NULL;
if (dhcps == NULL) {
return NULL;
}
switch (op_id) { switch (op_id) {
case IP_ADDRESS_LEASE_TIME: 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 * opt_len -- DHCP message option length
* Returns : none * 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) { if (dhcps == NULL || opt_info == NULL) {
return; return ERR_ARG;
} }
switch (op_id) { switch (op_id) {
case IP_ADDRESS_LEASE_TIME: 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: default:
break; 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 * Description : set callback for dhcp server when it assign an IP
* to the connected dhcp client * to the connected dhcp client
* Parameters : cb -- callback for dhcp server * 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; 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 * : info -- The current ip info
* Returns : none * 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; dhcps->dhcps_netif = netif;
if (dhcps->dhcps_pcb != NULL) { if (dhcps->dhcps_pcb != NULL) {
udp_remove(dhcps->dhcps_pcb); 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)) { if (dhcps->dhcps_pcb == NULL || ip4_addr_isany_val(ip)) {
printf("dhcps_start(): could not obtain pcb\n"); printf("dhcps_start(): could not obtain pcb\n");
return ERR_ARG;
} }
IP4_ADDR(&dhcps->broadcast_dhcps, 255, 255, 255, 255); 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 #endif
dhcps->state = DHCPS_HANDLE_STARTED; dhcps->state = DHCPS_HANDLE_STARTED;
sys_timeout(DHCP_COARSE_TIMER_MSECS, dhcps_tmr, dhcps); 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 * Parameters : netif -- The current netif addr
* Returns : none * 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) { if (dhcps == NULL || netif == NULL || dhcps->dhcps_netif != netif) {
printf("dhcps_stop: netif is NULL or invalid\n"); #if DHCPS_DEBUG
return; DHCPS_LOG("dhcps_stop: netif is NULL or invalid\n");
#endif
return ERR_ARG;
} }
if (dhcps->dhcps_pcb != NULL) { if (dhcps->dhcps_pcb != NULL) {
@ -1256,6 +1271,7 @@ void dhcps_stop(dhcps_t *dhcps, struct netif *netif)
} }
sys_untimeout(dhcps_tmr, dhcps); sys_untimeout(dhcps_tmr, dhcps);
dhcps->state = DHCPS_HANDLE_STOPPED; 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; list_node *pback_node = NULL;
bool ret = false; bool ret = false;
if (dhcps == NULL) {
return false;
}
for (pback_node = dhcps->plist; pback_node != NULL; pback_node = pback_node->pnext) { for (pback_node = dhcps->plist; pback_node != NULL; pback_node = pback_node->pnext) {
pdhcps_pool = pback_node->pnode; 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 * FunctionName : dhcps_dns_setserver
* Description : set DNS server address for dhcpserver * Description : set DNS server address for dhcpserver
* Parameters : dnsserver -- The DNS server address * Parameters : dnsserver -- The DNS server address
* Returns : none * Returns : ERR_ARG if invalid handle, ERR_OK on success
*******************************************************************************/ *******************************************************************************/
void err_t dhcps_dns_setserver(dhcps_t *dhcps, const ip_addr_t *dnsserver)
dhcps_dns_setserver(dhcps_t *dhcps, const ip_addr_t *dnsserver)
{ {
if (dhcps == NULL) {
return ERR_ARG;
}
if (dnsserver != NULL) { if (dnsserver != NULL) {
dhcps->dns_server = *(ip_2_ip4(dnsserver)); dhcps->dns_server = *(ip_2_ip4(dnsserver));
} else { } else {
dhcps->dns_server = *(ip_2_ip4(IP_ADDR_ANY)); 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 * Parameters : none
* Returns : ip4_addr_t * Returns : ip4_addr_t
*******************************************************************************/ *******************************************************************************/
ip4_addr_t err_t dhcps_dns_getserver(dhcps_t *dhcps, ip4_addr_t *dnsserver)
dhcps_dns_getserver(dhcps_t *dhcps)
{ {
return dhcps->dns_server; if (dhcps) {
*dnsserver = dhcps->dns_server;
return ERR_OK;
}
return ERR_ARG;
} }
#endif // ESP_DHCP #endif // ESP_DHCP

View File

@ -8,6 +8,7 @@
#include "sdkconfig.h" #include "sdkconfig.h"
#include "lwip/ip_addr.h" #include "lwip/ip_addr.h"
#include "lwip/err.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@ -118,15 +119,17 @@ void dhcps_delete(dhcps_t *dhcps);
* @param dhcps Pointer to the DHCP handle * @param dhcps Pointer to the DHCP handle
* @param netif Pointer to the lwIP's network interface struct * @param netif Pointer to the lwIP's network interface struct
* @param ip DHCP server's address * @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 * @brief Stops the DHCP server on the specified netif
* @param dhcps Pointer to the DHCP handle * @param dhcps Pointer to the DHCP handle
* @param netif Pointer to the lwIP's network interface struct * @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 * @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 op_id DHCP message option id
* @param opt_info DHCP message option info * @param opt_info DHCP message option info
* @param opt_len DHCP message option length * @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 * @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 * @brief Sets DNS server address for the DHCP server
* @param dhcps Pointer to the DHCP handle * @param dhcps Pointer to the DHCP handle
* @param dnsserver Address of the DNS server * @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 * @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 * @brief Sets callback on assigning an IP to the connected client
* @param dhcps Pointer to the DHCP handle * @param dhcps Pointer to the DHCP handle
* @param cb Callback for dhcp server * @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 #ifdef __cplusplus
} }