From bab051f450b6efaff00d4b9a3974acf62fcd4802 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Wed, 19 Jan 2022 17:15:40 +0100 Subject: [PATCH 01/10] lwip/dhcps: Cleanup internal lwip and esp-netif dependency --- .../esp_netif/include/esp_netif_net_stack.h | 18 ++---- components/lwip/apps/dhcpserver/dhcpserver.c | 62 ++++++++++--------- tools/ci/check_copyright_ignore.txt | 1 - 3 files changed, 38 insertions(+), 43 deletions(-) diff --git a/components/esp_netif/include/esp_netif_net_stack.h b/components/esp_netif/include/esp_netif_net_stack.h index ab6d6a07fd..32942b3449 100644 --- a/components/esp_netif/include/esp_netif_net_stack.h +++ b/components/esp_netif/include/esp_netif_net_stack.h @@ -1,16 +1,8 @@ -// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at - -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/* + * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ #ifndef _ESP_NETIF_NET_STACK_H_ #define _ESP_NETIF_NET_STACK_H_ diff --git a/components/lwip/apps/dhcpserver/dhcpserver.c b/components/lwip/apps/dhcpserver/dhcpserver.c index 89ccec31fc..ce8289d6f5 100644 --- a/components/lwip/apps/dhcpserver/dhcpserver.c +++ b/components/lwip/apps/dhcpserver/dhcpserver.c @@ -12,7 +12,6 @@ #include "lwip/udp.h" #include "lwip/mem.h" #include "lwip/ip_addr.h" -#include "esp_netif.h" #include "dhcpserver/dhcpserver.h" #include "dhcpserver/dhcpserver_options.h" @@ -69,11 +68,16 @@ typedef struct _list_node { struct _list_node *pnext; } list_node; +typedef struct { + ip4_addr_t ip; + ip4_addr_t netmask; + ip4_addr_t gw; +} ip_info_t; //////////////////////////////////////////////////////////////////////////////////// static const u32_t magic_cookie = 0x63538263; -static struct netif *dhcps_netif = NULL; +static struct netif *dhcps_netif = NULL; static ip4_addr_t broadcast_dhcps; static ip4_addr_t server_address; static ip4_addr_t dns_server = {0}; @@ -95,6 +99,16 @@ static dhcps_time_t dhcps_lease_time = DHCPS_LEASE_TIME_DEF; //minute static dhcps_offer_t dhcps_offer = 0xFF; static dhcps_offer_t dhcps_dns = 0x00; static dhcps_cb_t dhcps_cb; +static struct udp_pcb *dhcps_pcb = NULL; + +static void get_ip_info(struct netif * netif, ip_info_t *ip_info) +{ + if (netif != NULL && netif_is_up(netif)) { + ip4_addr_set(&ip_info->ip, ip_2_ip4(&netif->ip_addr)); + ip4_addr_set(&ip_info->netmask, ip_2_ip4(&netif->netmask)); + ip4_addr_set(&ip_info->gw, ip_2_ip4(&netif->gw)); + } +} /****************************************************************************** * FunctionName : dhcps_option_info @@ -328,9 +342,8 @@ static u8_t *add_offer_options(u8_t *optptr) *optptr++ = ip4_addr4(&ipadd); if (dhcps_router_enabled(dhcps_offer)) { - esp_netif_ip_info_t if_ip; - memset(&if_ip , 0x00, sizeof(esp_netif_ip_info_t)); - esp_netif_get_ip_info(dhcps_netif->state, &if_ip); + ip_info_t if_ip = { 0 }; + get_ip_info(dhcps_netif, &if_ip); ip4_addr_t* gw_ip = (ip4_addr_t*)&if_ip.gw; @@ -520,12 +533,11 @@ static void send_offer(struct dhcps_msg *m, u16_t len) ip_addr_t ip_temp = IPADDR4_INIT(0x0); ip4_addr_set(ip_2_ip4(&ip_temp), &broadcast_dhcps); - struct udp_pcb *pcb_dhcps = dhcps_netif->dhcps_pcb; #if DHCPS_DEBUG SendOffer_err_t = udp_sendto(pcb_dhcps, p, &ip_temp, DHCPS_CLIENT_PORT); DHCPS_LOG("dhcps: send_offer>>udp_sendto result %x\n", SendOffer_err_t); #else - udp_sendto(pcb_dhcps, p, &ip_temp, DHCPS_CLIENT_PORT); + udp_sendto(dhcps_pcb, p, &ip_temp, DHCPS_CLIENT_PORT); #endif if (p->ref != 0) { @@ -598,12 +610,11 @@ static void send_nak(struct dhcps_msg *m, u16_t len) ip_addr_t ip_temp = IPADDR4_INIT(0x0); ip4_addr_set(ip_2_ip4(&ip_temp), &broadcast_dhcps); - struct udp_pcb *pcb_dhcps = dhcps_netif->dhcps_pcb; #if DHCPS_DEBUG SendNak_err_t = udp_sendto(pcb_dhcps, p, &ip_temp, DHCPS_CLIENT_PORT); DHCPS_LOG("dhcps: send_nak>>udp_sendto result %x\n", SendNak_err_t); #else - udp_sendto(pcb_dhcps, p, &ip_temp, DHCPS_CLIENT_PORT); + udp_sendto(dhcps_pcb, p, &ip_temp, DHCPS_CLIENT_PORT); #endif if (p->ref != 0) { @@ -675,8 +686,7 @@ static void send_ack(struct dhcps_msg *m, u16_t len) ip_addr_t ip_temp = IPADDR4_INIT(0x0); ip4_addr_set(ip_2_ip4(&ip_temp), &broadcast_dhcps); - struct udp_pcb *pcb_dhcps = dhcps_netif->dhcps_pcb; - SendAck_err_t = udp_sendto(pcb_dhcps, p, &ip_temp, DHCPS_CLIENT_PORT); + SendAck_err_t = udp_sendto(dhcps_pcb, p, &ip_temp, DHCPS_CLIENT_PORT); #if DHCPS_DEBUG DHCPS_LOG("dhcps: send_ack>>udp_sendto result %x\n", SendAck_err_t); #endif @@ -1140,20 +1150,16 @@ void dhcps_set_new_lease_cb(dhcps_cb_t cb) void dhcps_start(struct netif *netif, ip4_addr_t ip) { dhcps_netif = netif; - - if (dhcps_netif->dhcps_pcb != NULL) { - udp_remove(dhcps_netif->dhcps_pcb); + if (dhcps_pcb != NULL) { + udp_remove(dhcps_pcb); } - dhcps_netif->dhcps_pcb = udp_new(); - struct udp_pcb *pcb_dhcps = dhcps_netif->dhcps_pcb; + dhcps_pcb = udp_new(); - if (pcb_dhcps == NULL || ip4_addr_isany_val(ip)) { + if (dhcps_pcb == NULL || ip4_addr_isany_val(ip)) { printf("dhcps_start(): could not obtain pcb\n"); } - dhcps_netif->dhcps_pcb = pcb_dhcps; - IP4_ADDR(&broadcast_dhcps, 255, 255, 255, 255); server_address.addr = ip.addr; @@ -1161,8 +1167,8 @@ void dhcps_start(struct netif *netif, ip4_addr_t ip) client_address_plus.addr = dhcps_poll.start_ip.addr; - udp_bind(pcb_dhcps, &netif->ip_addr, DHCPS_SERVER_PORT); - udp_recv(pcb_dhcps, handle_dhcp, NULL); + udp_bind(dhcps_pcb, &netif->ip_addr, DHCPS_SERVER_PORT); + udp_recv(dhcps_pcb, handle_dhcp, NULL); #if DHCPS_DEBUG DHCPS_LOG("dhcps:dhcps_start->udp_recv function Set a receive callback handle_dhcp for UDP_PCB pcb_dhcps\n"); #endif @@ -1177,17 +1183,15 @@ void dhcps_start(struct netif *netif, ip4_addr_t ip) *******************************************************************************/ void dhcps_stop(struct netif *netif) { - struct netif *apnetif = netif; - - if (apnetif == NULL) { - printf("dhcps_stop: apnetif == NULL\n"); + if (netif == NULL || dhcps_netif != netif->state) { + printf("dhcps_stop: netif is NULL or invalid\n"); return; } - if (apnetif->dhcps_pcb != NULL) { - udp_disconnect(apnetif->dhcps_pcb); - udp_remove(apnetif->dhcps_pcb); - apnetif->dhcps_pcb = NULL; + if (dhcps_pcb != NULL) { + udp_disconnect(dhcps_pcb); + udp_remove(dhcps_pcb); + dhcps_pcb = NULL; } list_node *pnode = NULL; diff --git a/tools/ci/check_copyright_ignore.txt b/tools/ci/check_copyright_ignore.txt index c49a163ee7..48b0124d1e 100644 --- a/tools/ci/check_copyright_ignore.txt +++ b/tools/ci/check_copyright_ignore.txt @@ -482,7 +482,6 @@ components/esp_local_ctrl/src/esp_local_ctrl_handler.c components/esp_local_ctrl/src/esp_local_ctrl_priv.h components/esp_local_ctrl/src/esp_local_ctrl_transport_ble.c components/esp_local_ctrl/src/esp_local_ctrl_transport_httpd.c -components/esp_netif/include/esp_netif_net_stack.h components/esp_netif/include/esp_netif_ppp.h components/esp_netif/include/esp_netif_slip.h components/esp_netif/include/esp_netif_sta_list.h From c005b04d1c2b2a6ff0688a9495d98f51982bebed Mon Sep 17 00:00:00 2001 From: David Cermak Date: Wed, 19 Jan 2022 19:50:49 +0100 Subject: [PATCH 02/10] lwip/dhcps: Support for dynamic dhcp server instances --- .../esp_netif/include/esp_netif_sta_list.h | 20 +- components/esp_netif/lwip/esp_netif_lwip.c | 36 +- .../esp_netif/lwip/esp_netif_lwip_internal.h | 5 +- .../esp_netif/lwip/esp_netif_sta_list.c | 27 +- components/lwip/apps/dhcpserver/dhcpserver.c | 359 ++++++++++-------- .../lwip/include/apps/dhcpserver/dhcpserver.h | 21 +- components/lwip/port/esp32/include/lwipopts.h | 2 +- tools/ci/check_copyright_ignore.txt | 2 - 8 files changed, 264 insertions(+), 208 deletions(-) diff --git a/components/esp_netif/include/esp_netif_sta_list.h b/components/esp_netif/include/esp_netif_sta_list.h index d464132ace..b1a9ff3cc4 100644 --- a/components/esp_netif/include/esp_netif_sta_list.h +++ b/components/esp_netif/include/esp_netif_sta_list.h @@ -1,16 +1,8 @@ -// Copyright 2019 Espressif Systems (Shanghai) PTE LTD -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at - -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/* + * SPDX-FileCopyrightText: 2019-2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ #ifndef _ESP_NETIF_STA_LIST_H_ #define _ESP_NETIF_STA_LIST_H_ @@ -50,6 +42,8 @@ typedef struct { /** * @brief Get IP information for stations connected to the Wi-Fi AP interface * + * @warning This API works only for the default Wi-Fi AP interface, i.e. esp-netif with key="WIFI_AP_DEF" + * * @param[in] wifi_sta_list Wi-Fi station info list, returned from esp_wifi_ap_get_sta_list() * @param[out] netif_sta_list IP layer station info list, corresponding to MAC addresses provided in wifi_sta_list * diff --git a/components/esp_netif/lwip/esp_netif_lwip.c b/components/esp_netif/lwip/esp_netif_lwip.c index 392cd14949..d04e5711ea 100644 --- a/components/esp_netif/lwip/esp_netif_lwip.c +++ b/components/esp_netif/lwip/esp_netif_lwip.c @@ -548,6 +548,13 @@ esp_netif_t *esp_netif_new(const esp_netif_config_t *esp_netif_config) esp_netif_add_to_list(esp_netif); +#if ESP_DHCPS + // Create DHCP server structure + if (esp_netif_config->base->flags & ESP_NETIF_DHCP_SERVER) { + esp_netif->dhcps = dhcps_new(); + } +#endif + // Configure the created object with provided configuration esp_err_t ret = esp_netif_init_configuration(esp_netif, esp_netif_config); if (ret != ESP_OK) { @@ -627,6 +634,9 @@ void esp_netif_destroy(esp_netif_t *esp_netif) vSemaphoreDelete(esp_netif->transmit_mutex); #endif // CONFIG_ESP_NETIF_L2_TAP esp_netif_update_default_netif(esp_netif, ESP_NETIF_STOPPED); +#if ESP_DHCPS + dhcps_delete(esp_netif->dhcps); +#endif free(esp_netif); } } @@ -856,9 +866,9 @@ static esp_err_t esp_netif_start_api(esp_netif_api_msg_t *msg) ip4_addr_t lwip_netmask; memcpy(&lwip_ip, &default_ip->ip, sizeof(struct ip4_addr)); memcpy(&lwip_netmask, &default_ip->netmask, sizeof(struct ip4_addr)); - dhcps_set_new_lease_cb(esp_netif_dhcps_cb); - dhcps_set_option_info(SUBNET_MASK, (void*)&lwip_netmask, sizeof(lwip_netmask)); - dhcps_start(p_netif, lwip_ip); + dhcps_set_new_lease_cb(esp_netif->dhcps, esp_netif_dhcps_cb); + dhcps_set_option_info(esp_netif->dhcps, SUBNET_MASK, (void*)&lwip_netmask, sizeof(lwip_netmask)); + dhcps_start(esp_netif->dhcps, p_netif, lwip_ip); 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); @@ -928,7 +938,7 @@ 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(lwip_netif); // TODO(IDF-1099): dhcps checks status by its self + 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) { esp_netif->dhcps_status = ESP_NETIF_DHCP_INIT; } @@ -1303,9 +1313,9 @@ static esp_err_t esp_netif_dhcps_start_api(esp_netif_api_msg_t *msg) ip4_addr_t lwip_netmask; memcpy(&lwip_ip, &default_ip->ip, sizeof(struct ip4_addr)); memcpy(&lwip_netmask, &default_ip->netmask, sizeof(struct ip4_addr)); - dhcps_set_new_lease_cb(esp_netif_dhcps_cb); - dhcps_set_option_info(SUBNET_MASK, (void*)&lwip_netmask, sizeof(lwip_netmask)); - dhcps_start(p_netif, lwip_ip); + dhcps_set_new_lease_cb(esp_netif->dhcps, esp_netif_dhcps_cb); + dhcps_set_option_info(esp_netif->dhcps, SUBNET_MASK, (void*)&lwip_netmask, sizeof(lwip_netmask)); + dhcps_start(esp_netif->dhcps, p_netif, lwip_ip); esp_netif->dhcps_status = ESP_NETIF_DHCP_STARTED; ESP_LOGD(TAG, "DHCP server started successfully"); return ESP_OK; @@ -1331,7 +1341,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(p_netif); + dhcps_stop(esp_netif->dhcps, p_netif); } else { ESP_LOGD(TAG, "dhcp server if not ready"); return ESP_ERR_ESP_NETIF_IF_NOT_READY; @@ -1648,7 +1658,7 @@ static esp_err_t esp_netif_set_dns_info_api(esp_netif_api_msg_t *msg) ESP_LOGD(TAG, "set dns invalid type"); return ESP_ERR_ESP_NETIF_INVALID_PARAMS; } else { - dhcps_dns_setserver(lwip_ip); + dhcps_dns_setserver(esp_netif->dhcps, lwip_ip); } #else LOG_NETIF_DISABLED_AND_DO("DHCP Server", return ESP_ERR_NOT_SUPPORTED); @@ -1688,7 +1698,7 @@ 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(); + ip4_addr_t dns_ip = dhcps_dns_getserver(esp_netif->dhcps); 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); @@ -1894,10 +1904,10 @@ int32_t esp_netif_get_event_id(esp_netif_t *esp_netif, esp_netif_ip_event_type_t esp_err_t esp_netif_dhcps_option(esp_netif_t *esp_netif, esp_netif_dhcp_option_mode_t opt_op, esp_netif_dhcp_option_id_t opt_id, void *opt_val, uint32_t opt_len) { - void *opt_info = dhcps_option_info(opt_id, opt_len); - if (esp_netif == NULL) { + if (esp_netif == NULL || esp_netif->dhcps == NULL) { return ESP_ERR_ESP_NETIF_IF_NOT_READY; } + void *opt_info = dhcps_option_info(esp_netif->dhcps, opt_id, opt_len); esp_netif_dhcp_status_t dhcps_status = esp_netif->dhcps_status; if (opt_info == NULL || opt_val == NULL) { @@ -2011,7 +2021,7 @@ esp_err_t esp_netif_dhcps_option(esp_netif_t *esp_netif, esp_netif_dhcp_option_m default: break; } - dhcps_set_option_info(opt_id, opt_info, opt_len); + dhcps_set_option_info(esp_netif->dhcps, opt_id, opt_info, opt_len); } else { return ESP_ERR_ESP_NETIF_INVALID_PARAMS; } diff --git a/components/esp_netif/lwip/esp_netif_lwip_internal.h b/components/esp_netif/lwip/esp_netif_lwip_internal.h index 8ce7a154d4..3ef6702ab1 100644 --- a/components/esp_netif/lwip/esp_netif_lwip_internal.h +++ b/components/esp_netif/lwip/esp_netif_lwip_internal.h @@ -10,6 +10,7 @@ #include "esp_netif_ppp.h" #include "esp_netif_slip.h" #include "lwip/netif.h" +#include "dhcpserver/dhcpserver.h" #ifdef CONFIG_ESP_NETIF_TCPIP_LWIP @@ -101,7 +102,9 @@ struct esp_netif_obj { void (*lwip_input_fn)(void *input_netif_handle, void *buffer, size_t len, void *eb); void * netif_handle; // netif impl context (either vanilla lwip-netif or ppp_pcb) netif_related_data_t *related_data; // holds additional data for specific netifs - +#if ESP_DHCPS + dhcps_t *dhcps; +#endif // io driver related void* driver_handle; esp_err_t (*driver_transmit)(void *h, void *buffer, size_t len); diff --git a/components/esp_netif/lwip/esp_netif_sta_list.c b/components/esp_netif/lwip/esp_netif_sta_list.c index 0bbdcaadad..4ef84fff06 100644 --- a/components/esp_netif/lwip/esp_netif_sta_list.c +++ b/components/esp_netif/lwip/esp_netif_sta_list.c @@ -1,19 +1,11 @@ -// Copyright 2019 Espressif Systems (Shanghai) PTE LTD -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at - -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/* + * SPDX-FileCopyrightText: 2019-2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ #include -#include "esp_netif.h" +#include "esp_netif_lwip_internal.h" #include "esp_netif_sta_list.h" #include "dhcpserver/dhcpserver.h" #include "esp_log.h" @@ -29,12 +21,15 @@ esp_err_t esp_netif_get_sta_list(const wifi_sta_list_t *wifi_sta_list, esp_netif if ((wifi_sta_list == NULL) || (netif_sta_list == NULL)) { return ESP_ERR_ESP_NETIF_INVALID_PARAMS; } - + esp_netif_t *ap = esp_netif_get_handle_from_ifkey("WIFI_AP_DEF"); + if (ap == NULL) { + return ESP_ERR_ESP_NETIF_IF_NOT_READY; + } memset(netif_sta_list, 0, sizeof(esp_netif_sta_list_t)); netif_sta_list->num = wifi_sta_list->num; for (int i = 0; i < wifi_sta_list->num; i++) { memcpy(netif_sta_list->sta[i].mac, wifi_sta_list->sta[i].mac, 6); - dhcp_search_ip_on_mac(netif_sta_list->sta[i].mac, (ip4_addr_t*)&netif_sta_list->sta[i].ip); + dhcp_search_ip_on_mac(ap->dhcps, netif_sta_list->sta[i].mac, (ip4_addr_t*)&netif_sta_list->sta[i].ip); } return ESP_OK; diff --git a/components/lwip/apps/dhcpserver/dhcpserver.c b/components/lwip/apps/dhcpserver/dhcpserver.c index ce8289d6f5..6cc7f24ea9 100644 --- a/components/lwip/apps/dhcpserver/dhcpserver.c +++ b/components/lwip/apps/dhcpserver/dhcpserver.c @@ -6,12 +6,14 @@ //#include "esp_common.h" #include #include +#include #include "lwip/inet.h" #include "lwip/err.h" #include "lwip/pbuf.h" #include "lwip/udp.h" #include "lwip/mem.h" #include "lwip/ip_addr.h" +#include "lwip/timeouts.h" #include "dhcpserver/dhcpserver.h" #include "dhcpserver/dhcpserver_options.h" @@ -63,9 +65,16 @@ #define DHCPS_STATE_IDLE 5 #define DHCPS_STATE_RELEASE 6 -typedef struct _list_node { +typedef enum { + DHCPS_HANDLE_CREATED = 0, + DHCPS_HANDLE_STARTED, + DHCPS_HANDLE_STOPPED, + DHCPS_HANDLE_DELETE_PENDING, +} dhcps_handle_state; + +typedef struct list_node { void *pnode; - struct _list_node *pnext; + struct list_node *pnext; } list_node; typedef struct { @@ -77,29 +86,57 @@ typedef struct { static const u32_t magic_cookie = 0x63538263; -static struct netif *dhcps_netif = NULL; -static ip4_addr_t broadcast_dhcps; -static ip4_addr_t server_address; -static ip4_addr_t dns_server = {0}; -static ip4_addr_t client_address; //added -static ip4_addr_t client_address_plus; -static ip4_addr_t s_dhcps_mask = { +struct dhcps_t { + struct netif *dhcps_netif; + ip4_addr_t broadcast_dhcps; + ip4_addr_t server_address; + ip4_addr_t dns_server; + ip4_addr_t client_address; + ip4_addr_t client_address_plus; + ip4_addr_t dhcps_mask; + list_node *plist; + bool renew; + dhcps_lease_t dhcps_poll; + dhcps_time_t dhcps_lease_time; + dhcps_offer_t dhcps_offer; + dhcps_offer_t dhcps_dns; + dhcps_cb_t dhcps_cb; + struct udp_pcb *dhcps_pcb; + dhcps_handle_state state; +}; + + +static void dhcps_tmr(void* arg); + + +dhcps_t *dhcps_new(void) +{ + dhcps_t *dhcps = calloc(1, sizeof(dhcps_t)); + if (dhcps == NULL) { + return NULL; + } + dhcps->dhcps_netif = NULL; + dhcps->dns_server.addr = 0; #ifdef USE_CLASS_B_NET - .addr = PP_HTONL(LWIP_MAKEU32(255, 240, 0, 0)) + dhcps->dhcps_mask.addr = PP_HTONL(LWIP_MAKEU32(255, 240, 0, 0)); #else - .addr = PP_HTONL(LWIP_MAKEU32(255, 255, 255, 0)) + dhcps->dhcps_mask.addr = PP_HTONL(LWIP_MAKEU32(255, 255, 255, 0)); #endif - }; + dhcps->plist = NULL; + dhcps->renew = false; + dhcps->dhcps_lease_time = DHCPS_LEASE_TIME_DEF; //minute + dhcps->dhcps_offer = 0xFF; + dhcps->dhcps_dns = 0x00; + dhcps->dhcps_pcb = NULL; + dhcps->state = DHCPS_HANDLE_CREATED; + sys_timeout(DHCP_COARSE_TIMER_MSECS, dhcps_tmr, dhcps); + return dhcps; +} -static list_node *plist = NULL; -static bool renew = false; - -static dhcps_lease_t dhcps_poll; -static dhcps_time_t dhcps_lease_time = DHCPS_LEASE_TIME_DEF; //minute -static dhcps_offer_t dhcps_offer = 0xFF; -static dhcps_offer_t dhcps_dns = 0x00; -static dhcps_cb_t dhcps_cb; -static struct udp_pcb *dhcps_pcb = NULL; +void dhcps_delete(dhcps_t *dhcps) +{ + dhcps->state = DHCPS_HANDLE_DELETE_PENDING; +} static void get_ip_info(struct netif * netif, ip_info_t *ip_info) { @@ -117,41 +154,41 @@ static void get_ip_info(struct netif * netif, ip_info_t *ip_info) * opt_len -- DHCP message option length * Returns : DHCP message option addr *******************************************************************************/ -void *dhcps_option_info(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; switch (op_id) { case IP_ADDRESS_LEASE_TIME: if (opt_len == sizeof(dhcps_time_t)) { - option_arg = &dhcps_lease_time; + option_arg = &dhcps->dhcps_lease_time; } break; case REQUESTED_IP_ADDRESS: if (opt_len == sizeof(dhcps_lease_t)) { - option_arg = &dhcps_poll; + option_arg = &dhcps->dhcps_poll; } break; case ROUTER_SOLICITATION_ADDRESS: if (opt_len == sizeof(dhcps_offer_t)) { - option_arg = &dhcps_offer; + option_arg = &dhcps->dhcps_offer; } break; case DOMAIN_NAME_SERVER: if (opt_len == sizeof(dhcps_offer_t)) { - option_arg = &dhcps_dns; + option_arg = &dhcps->dhcps_dns; } break; case SUBNET_MASK: - if (opt_len == sizeof(s_dhcps_mask)) { - option_arg = &s_dhcps_mask; + if (opt_len == sizeof(dhcps->dhcps_mask)) { + option_arg = &dhcps->dhcps_mask; } break; @@ -170,7 +207,7 @@ void *dhcps_option_info(u8_t op_id, u32_t opt_len) * opt_len -- DHCP message option length * Returns : none *******************************************************************************/ -void dhcps_set_option_info(u8_t op_id, void *opt_info, u32_t opt_len) +void dhcps_set_option_info(dhcps_t *dhcps, u8_t op_id, void *opt_info, u32_t opt_len) { if (opt_info == NULL) { return; @@ -178,34 +215,34 @@ void dhcps_set_option_info(u8_t op_id, void *opt_info, u32_t opt_len) switch (op_id) { case IP_ADDRESS_LEASE_TIME: if (opt_len == sizeof(dhcps_time_t)) { - dhcps_lease_time = *(dhcps_time_t *)opt_info; + dhcps->dhcps_lease_time = *(dhcps_time_t *)opt_info; } break; case REQUESTED_IP_ADDRESS: if (opt_len == sizeof(dhcps_lease_t)) { - dhcps_poll = *(dhcps_lease_t *)opt_info; + dhcps->dhcps_poll = *(dhcps_lease_t *)opt_info; } break; case ROUTER_SOLICITATION_ADDRESS: if (opt_len == sizeof(dhcps_offer_t)) { - dhcps_offer = *(dhcps_offer_t *)opt_info; + dhcps->dhcps_offer = *(dhcps_offer_t *)opt_info; } break; case DOMAIN_NAME_SERVER: if (opt_len == sizeof(dhcps_offer_t)) { - dhcps_dns = *(dhcps_offer_t *)opt_info; + dhcps->dhcps_dns = *(dhcps_offer_t *)opt_info; } break; case SUBNET_MASK: - if (opt_len == sizeof(s_dhcps_mask)) { - s_dhcps_mask = *(ip4_addr_t *)opt_info; + if (opt_len == sizeof(dhcps->dhcps_mask)) { + dhcps->dhcps_mask = *(ip4_addr_t *)opt_info; } @@ -314,25 +351,25 @@ static u8_t *add_msg_type(u8_t *optptr, u8_t type) * Parameters : optptr -- the addr of DHCP message option * Returns : the addr of DHCP message option *******************************************************************************/ -static u8_t *add_offer_options(u8_t *optptr) +static u8_t *add_offer_options(dhcps_t *dhcps, u8_t *optptr) { ip4_addr_t ipadd; - ipadd.addr = *((u32_t *) &server_address); + ipadd.addr = *((u32_t *) &dhcps->server_address); *optptr++ = DHCP_OPTION_SUBNET_MASK; *optptr++ = 4; - *optptr++ = ip4_addr1(&s_dhcps_mask); - *optptr++ = ip4_addr2(&s_dhcps_mask); - *optptr++ = ip4_addr3(&s_dhcps_mask); - *optptr++ = ip4_addr4(&s_dhcps_mask); + *optptr++ = ip4_addr1(&dhcps->dhcps_mask); + *optptr++ = ip4_addr2(&dhcps->dhcps_mask); + *optptr++ = ip4_addr3(&dhcps->dhcps_mask); + *optptr++ = ip4_addr4(&dhcps->dhcps_mask); *optptr++ = DHCP_OPTION_LEASE_TIME; *optptr++ = 4; - *optptr++ = ((dhcps_lease_time * DHCPS_LEASE_UNIT) >> 24) & 0xFF; - *optptr++ = ((dhcps_lease_time * DHCPS_LEASE_UNIT) >> 16) & 0xFF; - *optptr++ = ((dhcps_lease_time * DHCPS_LEASE_UNIT) >> 8) & 0xFF; - *optptr++ = ((dhcps_lease_time * DHCPS_LEASE_UNIT) >> 0) & 0xFF; + *optptr++ = ((dhcps->dhcps_lease_time * DHCPS_LEASE_UNIT) >> 24) & 0xFF; + *optptr++ = ((dhcps->dhcps_lease_time * DHCPS_LEASE_UNIT) >> 16) & 0xFF; + *optptr++ = ((dhcps->dhcps_lease_time * DHCPS_LEASE_UNIT) >> 8) & 0xFF; + *optptr++ = ((dhcps->dhcps_lease_time * DHCPS_LEASE_UNIT) >> 0) & 0xFF; *optptr++ = DHCP_OPTION_SERVER_ID; *optptr++ = 4; @@ -341,9 +378,9 @@ static u8_t *add_offer_options(u8_t *optptr) *optptr++ = ip4_addr3(&ipadd); *optptr++ = ip4_addr4(&ipadd); - if (dhcps_router_enabled(dhcps_offer)) { + if (dhcps_router_enabled(dhcps->dhcps_offer)) { ip_info_t if_ip = { 0 }; - get_ip_info(dhcps_netif, &if_ip); + get_ip_info(dhcps->dhcps_netif, &if_ip); ip4_addr_t* gw_ip = (ip4_addr_t*)&if_ip.gw; @@ -359,11 +396,11 @@ static u8_t *add_offer_options(u8_t *optptr) *optptr++ = DHCP_OPTION_DNS_SERVER; *optptr++ = 4; - if (dhcps_dns_enabled(dhcps_dns)) { - *optptr++ = ip4_addr1(&dns_server); - *optptr++ = ip4_addr2(&dns_server); - *optptr++ = ip4_addr3(&dns_server); - *optptr++ = ip4_addr4(&dns_server); + if (dhcps_dns_enabled(dhcps->dhcps_dns)) { + *optptr++ = ip4_addr1(&dhcps->dns_server); + *optptr++ = ip4_addr2(&dhcps->dns_server); + *optptr++ = ip4_addr3(&dhcps->dns_server); + *optptr++ = ip4_addr4(&dhcps->dns_server); }else { *optptr++ = ip4_addr1(&ipadd); *optptr++ = ip4_addr2(&ipadd); @@ -371,7 +408,7 @@ static u8_t *add_offer_options(u8_t *optptr) *optptr++ = ip4_addr4(&ipadd); } - ip4_addr_t broadcast_addr = { .addr = (ipadd.addr & s_dhcps_mask.addr) | ~s_dhcps_mask.addr }; + ip4_addr_t broadcast_addr = { .addr = (ipadd.addr & dhcps->dhcps_mask.addr) | ~dhcps->dhcps_mask.addr }; *optptr++ = DHCP_OPTION_BROADCAST_ADDRESS; *optptr++ = 4; *optptr++ = ip4_addr1(&broadcast_addr); @@ -419,12 +456,12 @@ static u8_t *add_end(u8_t *optptr) * Parameters : m -- DHCP message info * Returns : none *******************************************************************************/ -static void create_msg(struct dhcps_msg *m) +static void create_msg(dhcps_t *dhcps, struct dhcps_msg *m) { ip4_addr_t client; - client.addr = *((uint32_t *) &client_address); + client.addr = *((uint32_t *) &dhcps->client_address); m->op = DHCP_REPLY; @@ -476,7 +513,7 @@ struct pbuf * dhcps_pbuf_alloc(u16_t len) * Parameters : m -- DHCP message info * Returns : none *******************************************************************************/ -static void send_offer(struct dhcps_msg *m, u16_t len) +static void send_offer(dhcps_t *dhcps, struct dhcps_msg *m, u16_t len) { u8_t *end; struct pbuf *p, *q; @@ -486,10 +523,10 @@ static void send_offer(struct dhcps_msg *m, u16_t len) #if DHCPS_DEBUG err_t SendOffer_err_t; #endif - create_msg(m); + create_msg(dhcps, m); end = add_msg_type(&m->options[4], DHCPOFFER); - end = add_offer_options(end); + end = add_offer_options(dhcps, end); end = add_end(end); p = dhcps_pbuf_alloc(len); @@ -532,12 +569,12 @@ static void send_offer(struct dhcps_msg *m, u16_t len) } ip_addr_t ip_temp = IPADDR4_INIT(0x0); - ip4_addr_set(ip_2_ip4(&ip_temp), &broadcast_dhcps); + ip4_addr_set(ip_2_ip4(&ip_temp), &dhcps->broadcast_dhcps); #if DHCPS_DEBUG SendOffer_err_t = udp_sendto(pcb_dhcps, p, &ip_temp, DHCPS_CLIENT_PORT); DHCPS_LOG("dhcps: send_offer>>udp_sendto result %x\n", SendOffer_err_t); #else - udp_sendto(dhcps_pcb, p, &ip_temp, DHCPS_CLIENT_PORT); + udp_sendto(dhcps->dhcps_pcb, p, &ip_temp, DHCPS_CLIENT_PORT); #endif if (p->ref != 0) { @@ -554,7 +591,7 @@ static void send_offer(struct dhcps_msg *m, u16_t len) * Parameters : m -- DHCP message info * Returns : none *******************************************************************************/ -static void send_nak(struct dhcps_msg *m, u16_t len) +static void send_nak(dhcps_t *dhcps, struct dhcps_msg *m, u16_t len) { u8_t *end; struct pbuf *p, *q; @@ -564,7 +601,7 @@ static void send_nak(struct dhcps_msg *m, u16_t len) #if DHCPS_DEBUG err_t SendNak_err_t; #endif - create_msg(m); + create_msg(dhcps, m); end = add_msg_type(&m->options[4], DHCPNAK); end = add_end(end); @@ -609,12 +646,12 @@ static void send_nak(struct dhcps_msg *m, u16_t len) } ip_addr_t ip_temp = IPADDR4_INIT(0x0); - ip4_addr_set(ip_2_ip4(&ip_temp), &broadcast_dhcps); + ip4_addr_set(ip_2_ip4(&ip_temp), &dhcps->broadcast_dhcps); #if DHCPS_DEBUG SendNak_err_t = udp_sendto(pcb_dhcps, p, &ip_temp, DHCPS_CLIENT_PORT); DHCPS_LOG("dhcps: send_nak>>udp_sendto result %x\n", SendNak_err_t); #else - udp_sendto(dhcps_pcb, p, &ip_temp, DHCPS_CLIENT_PORT); + udp_sendto(dhcps->dhcps_pcb, p, &ip_temp, DHCPS_CLIENT_PORT); #endif if (p->ref != 0) { @@ -631,7 +668,7 @@ static void send_nak(struct dhcps_msg *m, u16_t len) * Parameters : m -- DHCP message info * Returns : none *******************************************************************************/ -static void send_ack(struct dhcps_msg *m, u16_t len) +static void send_ack(dhcps_t *dhcps, struct dhcps_msg *m, u16_t len) { u8_t *end; struct pbuf *p, *q; @@ -639,10 +676,10 @@ static void send_ack(struct dhcps_msg *m, u16_t len) u16_t cnt = 0; u16_t i; err_t SendAck_err_t; - create_msg(m); + create_msg(dhcps, m); end = add_msg_type(&m->options[4], DHCPACK); - end = add_offer_options(end); + end = add_offer_options(dhcps, end); end = add_end(end); p = dhcps_pbuf_alloc(len); @@ -685,14 +722,14 @@ static void send_ack(struct dhcps_msg *m, u16_t len) } ip_addr_t ip_temp = IPADDR4_INIT(0x0); - ip4_addr_set(ip_2_ip4(&ip_temp), &broadcast_dhcps); - SendAck_err_t = udp_sendto(dhcps_pcb, p, &ip_temp, DHCPS_CLIENT_PORT); + ip4_addr_set(ip_2_ip4(&ip_temp), &dhcps->broadcast_dhcps); + SendAck_err_t = udp_sendto(dhcps->dhcps_pcb, p, &ip_temp, DHCPS_CLIENT_PORT); #if DHCPS_DEBUG DHCPS_LOG("dhcps: send_ack>>udp_sendto result %x\n", SendAck_err_t); #endif if (SendAck_err_t == ERR_OK) { - dhcps_cb(m->yiaddr, m->chaddr); + dhcps->dhcps_cb(m->yiaddr, m->chaddr); } if (p->ref != 0) { @@ -710,13 +747,13 @@ static void send_ack(struct dhcps_msg *m, u16_t len) * len -- DHCP message option length * Returns : none *******************************************************************************/ -static u8_t parse_options(u8_t *optptr, s16_t len) +static u8_t parse_options(dhcps_t *dhcps, u8_t *optptr, s16_t len) { ip4_addr_t client; bool is_dhcp_parse_end = false; struct dhcps_state s; - client.addr = *((uint32_t *) &client_address); + client.addr = *((uint32_t *) &dhcps->client_address); u8_t *end = optptr + len; u16_t type = 0; @@ -773,7 +810,7 @@ static u8_t parse_options(u8_t *optptr, s16_t len) case DHCPREQUEST://3 if (!(s.state == DHCPS_STATE_ACK || s.state == DHCPS_STATE_NAK)) { - if (renew == true) { + if (dhcps->renew == true) { s.state = DHCPS_STATE_ACK; } else { s.state = DHCPS_STATE_NAK; @@ -814,9 +851,9 @@ static u8_t parse_options(u8_t *optptr, s16_t len) * len -- DHCP message length * Returns : DHCP message type *******************************************************************************/ -static s16_t parse_msg(struct dhcps_msg *m, u16_t len) +static s16_t parse_msg(dhcps_t *dhcps, struct dhcps_msg *m, u16_t len) { - u32_t lease_timer = (dhcps_lease_time * DHCPS_LEASE_UNIT)/DHCPS_COARSE_TIMER_SECS; + u32_t lease_timer = (dhcps->dhcps_lease_time * DHCPS_LEASE_UNIT)/DHCPS_COARSE_TIMER_SECS; if (memcmp((char *)m->options, &magic_cookie, sizeof(magic_cookie)) == 0) { #if DHCPS_DEBUG @@ -830,28 +867,28 @@ static s16_t parse_msg(struct dhcps_msg *m, u16_t len) ip4_addr_t first_address; bool flag = false; - first_address.addr = dhcps_poll.start_ip.addr; - client_address.addr = client_address_plus.addr; - renew = false; + first_address.addr = dhcps->dhcps_poll.start_ip.addr; + dhcps->client_address.addr = dhcps->client_address_plus.addr; + dhcps->renew = false; - if (plist != NULL) { - for (pback_node = plist; pback_node != NULL; pback_node = pback_node->pnext) { + if (dhcps->plist != NULL) { + for (pback_node = dhcps->plist; pback_node != NULL; pback_node = pback_node->pnext) { pdhcps_pool = pback_node->pnode; if (memcmp(pdhcps_pool->mac, m->chaddr, sizeof(pdhcps_pool->mac)) == 0) { if (memcmp(&pdhcps_pool->ip.addr, m->ciaddr, sizeof(pdhcps_pool->ip.addr)) == 0) { - renew = true; + dhcps->renew = true; } - client_address.addr = pdhcps_pool->ip.addr; + dhcps->client_address.addr = pdhcps_pool->ip.addr; pdhcps_pool->lease_timer = lease_timer; pnode = pback_node; goto POOL_CHECK; - } else if (pdhcps_pool->ip.addr == client_address_plus.addr) { - addr_tmp.addr = htonl(client_address_plus.addr); + } else if (pdhcps_pool->ip.addr == dhcps->client_address_plus.addr) { + addr_tmp.addr = htonl(dhcps->client_address_plus.addr); addr_tmp.addr++; - client_address_plus.addr = htonl(addr_tmp.addr); - client_address.addr = client_address_plus.addr; + dhcps->client_address_plus.addr = htonl(addr_tmp.addr); + dhcps->client_address.addr = dhcps->client_address_plus.addr; } if (flag == false) { // search the fisrt unused ip @@ -865,22 +902,22 @@ static s16_t parse_msg(struct dhcps_msg *m, u16_t len) } } } else { - client_address.addr = dhcps_poll.start_ip.addr; + dhcps->client_address.addr = dhcps->dhcps_poll.start_ip.addr; } - if (client_address_plus.addr > dhcps_poll.end_ip.addr) { - client_address.addr = first_address.addr; + if (dhcps->client_address_plus.addr > dhcps->dhcps_poll.end_ip.addr) { + dhcps->client_address.addr = first_address.addr; } - if (client_address.addr > dhcps_poll.end_ip.addr) { - client_address_plus.addr = dhcps_poll.start_ip.addr; + if (dhcps->client_address.addr > dhcps->dhcps_poll.end_ip.addr) { + dhcps->client_address_plus.addr = dhcps->dhcps_poll.start_ip.addr; pdhcps_pool = NULL; pnode = NULL; } else { pdhcps_pool = (struct dhcps_pool *)mem_malloc(sizeof(struct dhcps_pool)); memset(pdhcps_pool , 0x00 , sizeof(struct dhcps_pool)); - pdhcps_pool->ip.addr = client_address.addr; + pdhcps_pool->ip.addr = dhcps->client_address.addr; memcpy(pdhcps_pool->mac, m->chaddr, sizeof(pdhcps_pool->mac)); pdhcps_pool->lease_timer = lease_timer; pnode = (list_node *)mem_malloc(sizeof(list_node)); @@ -888,22 +925,22 @@ static s16_t parse_msg(struct dhcps_msg *m, u16_t len) pnode->pnode = pdhcps_pool; pnode->pnext = NULL; - node_insert_to_list(&plist, pnode); + node_insert_to_list(&dhcps->plist, pnode); - if (client_address.addr == dhcps_poll.end_ip.addr) { - client_address_plus.addr = dhcps_poll.start_ip.addr; + if (dhcps->client_address.addr == dhcps->dhcps_poll.end_ip.addr) { + dhcps->client_address_plus.addr = dhcps->dhcps_poll.start_ip.addr; } else { - addr_tmp.addr = htonl(client_address.addr); + addr_tmp.addr = htonl(dhcps->client_address.addr); addr_tmp.addr++; - client_address_plus.addr = htonl(addr_tmp.addr); + dhcps->client_address_plus.addr = htonl(addr_tmp.addr); } } POOL_CHECK: - if ((client_address.addr > dhcps_poll.end_ip.addr) || (ip4_addr_isany(&client_address))) { + if ((dhcps->client_address.addr > dhcps->dhcps_poll.end_ip.addr) || (ip4_addr_isany(&dhcps->client_address))) { if (pnode != NULL) { - node_remove_from_list(&plist, pnode); + node_remove_from_list(&dhcps->plist, pnode); free(pnode); pnode = NULL; } @@ -916,11 +953,11 @@ POOL_CHECK: return 4; } - s16_t ret = parse_options(&m->options[4], len);; + s16_t ret = parse_options(dhcps, &m->options[4], len);; if (ret == DHCPS_STATE_RELEASE || ret == DHCPS_STATE_NAK) { if (pnode != NULL) { - node_remove_from_list(&plist, pnode); + node_remove_from_list(&dhcps->plist, pnode); free(pnode); pnode = NULL; } @@ -930,7 +967,7 @@ POOL_CHECK: pdhcps_pool = NULL; } - memset(&client_address, 0x0, sizeof(client_address)); + memset(&dhcps->client_address, 0x0, sizeof(dhcps->client_address)); } #if DHCPS_DEBUG @@ -959,6 +996,7 @@ static void handle_dhcp(void *arg, const ip_addr_t *addr, u16_t port) { + struct dhcps_t *dhcps = arg; struct dhcps_msg *pmsg_dhcps = NULL; s16_t tlen, malloc_len; u16_t i; @@ -1037,7 +1075,7 @@ static void handle_dhcp(void *arg, DHCPS_LOG("dhcps: handle_dhcp-> parse_msg(p)\n"); #endif - state = parse_msg(pmsg_dhcps, tlen - 240); + state = parse_msg(dhcps, pmsg_dhcps, tlen - 240); #ifdef LWIP_HOOK_DHCPS_POST_STATE state = LWIP_HOOK_DHCPS_POST_STATE(pmsg_dhcps, malloc_len, state); #endif /* LWIP_HOOK_DHCPS_POST_STATE */ @@ -1047,21 +1085,21 @@ static void handle_dhcp(void *arg, #if DHCPS_DEBUG DHCPS_LOG("dhcps: handle_dhcp-> DHCPD_STATE_OFFER\n"); #endif - send_offer(pmsg_dhcps, malloc_len); + send_offer(dhcps, pmsg_dhcps, malloc_len); break; case DHCPS_STATE_ACK://3 #if DHCPS_DEBUG DHCPS_LOG("dhcps: handle_dhcp-> DHCPD_STATE_ACK\n"); #endif - send_ack(pmsg_dhcps, malloc_len); + send_ack(dhcps, pmsg_dhcps, malloc_len); break; case DHCPS_STATE_NAK://4 #if DHCPS_DEBUG DHCPS_LOG("dhcps: handle_dhcp-> DHCPD_STATE_NAK\n"); #endif - send_nak(pmsg_dhcps, malloc_len); + send_nak(dhcps, pmsg_dhcps, malloc_len); break; default : @@ -1082,32 +1120,32 @@ static void handle_dhcp(void *arg, * Parameters : ip -- The current ip addr * Returns : none *******************************************************************************/ -static void dhcps_poll_set(u32_t ip) +static void dhcps_poll_set(dhcps_t *dhcps, u32_t ip) { u32_t softap_ip = 0, local_ip = 0; u32_t start_ip = 0; u32_t end_ip = 0; - - if (dhcps_poll.enable == true) { + dhcps_lease_t *dhcps_poll = &dhcps->dhcps_poll; + if (dhcps_poll->enable == true) { softap_ip = htonl(ip); - start_ip = htonl(dhcps_poll.start_ip.addr); - end_ip = htonl(dhcps_poll.end_ip.addr); + start_ip = htonl(dhcps_poll->start_ip.addr); + end_ip = htonl(dhcps_poll->end_ip.addr); /*config ip information can't contain local ip*/ if ((start_ip <= softap_ip) && (softap_ip <= end_ip)) { - dhcps_poll.enable = false; + dhcps_poll->enable = false; } else { /*config ip information must be in the same segment as the local ip*/ softap_ip >>= 8; if (((start_ip >> 8 != softap_ip) || (end_ip >> 8 != softap_ip)) || (end_ip - start_ip > DHCPS_MAX_LEASE)) { - dhcps_poll.enable = false; + dhcps_poll->enable = false; } } } - if (dhcps_poll.enable == false) { + if (dhcps_poll->enable == false) { local_ip = softap_ip = htonl(ip); softap_ip &= 0xFFFFFF00; local_ip &= 0xFF; @@ -1118,11 +1156,11 @@ static void dhcps_poll_set(u32_t ip) local_ip ++; } - bzero(&dhcps_poll, sizeof(dhcps_poll)); - dhcps_poll.start_ip.addr = softap_ip | local_ip; - dhcps_poll.end_ip.addr = softap_ip | (local_ip + DHCPS_MAX_LEASE - 1); - dhcps_poll.start_ip.addr = htonl(dhcps_poll.start_ip.addr); - dhcps_poll.end_ip.addr = htonl(dhcps_poll.end_ip.addr); + bzero(dhcps_poll, sizeof(*dhcps_poll)); + dhcps_poll->start_ip.addr = softap_ip | local_ip; + dhcps_poll->end_ip.addr = softap_ip | (local_ip + DHCPS_MAX_LEASE - 1); + dhcps_poll->start_ip.addr = htonl(dhcps_poll->start_ip.addr); + dhcps_poll->end_ip.addr = htonl(dhcps_poll->end_ip.addr); } } @@ -1135,9 +1173,9 @@ static void dhcps_poll_set(u32_t ip) * Parameters : cb -- callback for dhcp server * Returns : none *******************************************************************************/ -void dhcps_set_new_lease_cb(dhcps_cb_t cb) +void dhcps_set_new_lease_cb(dhcps_t *dhcps, dhcps_cb_t cb) { - dhcps_cb = cb; + dhcps->dhcps_cb = cb; } /****************************************************************************** @@ -1147,28 +1185,29 @@ void dhcps_set_new_lease_cb(dhcps_cb_t cb) * : info -- The current ip info * Returns : none *******************************************************************************/ -void dhcps_start(struct netif *netif, ip4_addr_t ip) +void dhcps_start(dhcps_t *dhcps, struct netif *netif, ip4_addr_t ip) { - dhcps_netif = netif; - if (dhcps_pcb != NULL) { - udp_remove(dhcps_pcb); + dhcps->dhcps_netif = netif; + if (dhcps->dhcps_pcb != NULL) { + udp_remove(dhcps->dhcps_pcb); } - dhcps_pcb = udp_new(); + dhcps->dhcps_pcb = udp_new(); - if (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"); } - IP4_ADDR(&broadcast_dhcps, 255, 255, 255, 255); + IP4_ADDR(&dhcps->broadcast_dhcps, 255, 255, 255, 255); - server_address.addr = ip.addr; - dhcps_poll_set(server_address.addr); + dhcps->server_address.addr = ip.addr; + dhcps_poll_set(dhcps, dhcps->server_address.addr); - client_address_plus.addr = dhcps_poll.start_ip.addr; + dhcps->client_address_plus.addr = dhcps->dhcps_poll.start_ip.addr; - udp_bind(dhcps_pcb, &netif->ip_addr, DHCPS_SERVER_PORT); - udp_recv(dhcps_pcb, handle_dhcp, NULL); + udp_bind(dhcps->dhcps_pcb, &netif->ip_addr, DHCPS_SERVER_PORT); + udp_recv(dhcps->dhcps_pcb, handle_dhcp, dhcps); + dhcps->state = DHCPS_HANDLE_STARTED; #if DHCPS_DEBUG DHCPS_LOG("dhcps:dhcps_start->udp_recv function Set a receive callback handle_dhcp for UDP_PCB pcb_dhcps\n"); #endif @@ -1181,32 +1220,33 @@ void dhcps_start(struct netif *netif, ip4_addr_t ip) * Parameters : netif -- The current netif addr * Returns : none *******************************************************************************/ -void dhcps_stop(struct netif *netif) +void dhcps_stop(dhcps_t *dhcps, struct netif *netif) { - if (netif == NULL || dhcps_netif != netif->state) { + if (netif == NULL || dhcps->dhcps_netif != netif) { printf("dhcps_stop: netif is NULL or invalid\n"); return; } - if (dhcps_pcb != NULL) { - udp_disconnect(dhcps_pcb); - udp_remove(dhcps_pcb); - dhcps_pcb = NULL; + if (dhcps->dhcps_pcb != NULL) { + udp_disconnect(dhcps->dhcps_pcb); + udp_remove(dhcps->dhcps_pcb); + dhcps->dhcps_pcb = NULL; } list_node *pnode = NULL; list_node *pback_node = NULL; - pnode = plist; + pnode = dhcps->plist; while (pnode != NULL) { pback_node = pnode; pnode = pback_node->pnext; - node_remove_from_list(&plist, pback_node); + node_remove_from_list(&dhcps->plist, pback_node); free(pback_node->pnode); pback_node->pnode = NULL; free(pback_node); pback_node = NULL; } + dhcps->state = DHCPS_HANDLE_STOPPED; } /****************************************************************************** @@ -1215,12 +1255,12 @@ void dhcps_stop(struct netif *netif) * Parameters : none * Returns : none *******************************************************************************/ -static void kill_oldest_dhcps_pool(void) +static void kill_oldest_dhcps_pool(dhcps_t *dhcps) { list_node *pre = NULL, *p = NULL; list_node *minpre = NULL, *minp = NULL; struct dhcps_pool *pdhcps_pool = NULL, *pmin_pool = NULL; - pre = plist; + pre = dhcps->plist; assert(pre != NULL && pre->pnext != NULL); // Expect the list to have at least 2 nodes p = pre->pnext; minpre = pre; @@ -1252,13 +1292,24 @@ static void kill_oldest_dhcps_pool(void) * Parameters : none * Returns : none *******************************************************************************/ -void dhcps_coarse_tmr(void) +static void dhcps_tmr(void *arg) { + dhcps_t *dhcps = arg; + dhcps_handle_state state = dhcps->state; + if (state == DHCPS_HANDLE_DELETE_PENDING) { + free(dhcps); + return; + } + + sys_timeout(DHCP_COARSE_TIMER_MSECS, dhcps_tmr, dhcps); + if (state != DHCPS_HANDLE_STARTED) { + return; + } u8_t num_dhcps_pool = 0; list_node *pback_node = NULL; list_node *pnode = NULL; struct dhcps_pool *pdhcps_pool = NULL; - pnode = plist; + pnode = dhcps->plist; while (pnode != NULL) { pdhcps_pool = pnode->pnode; @@ -1267,7 +1318,7 @@ void dhcps_coarse_tmr(void) if (pdhcps_pool->lease_timer == 0) { pback_node = pnode; pnode = pback_node->pnext; - node_remove_from_list(&plist, pback_node); + node_remove_from_list(&dhcps->plist, pback_node); free(pback_node->pnode); pback_node->pnode = NULL; free(pback_node); @@ -1279,7 +1330,7 @@ void dhcps_coarse_tmr(void) } if (num_dhcps_pool > MAX_STATION_NUM) { - kill_oldest_dhcps_pool(); + kill_oldest_dhcps_pool(dhcps); } } @@ -1290,13 +1341,13 @@ void dhcps_coarse_tmr(void) * ip -- The IP info * Returns : true or false *******************************************************************************/ -bool dhcp_search_ip_on_mac(u8_t *mac, ip4_addr_t *ip) +bool dhcp_search_ip_on_mac(dhcps_t *dhcps, u8_t *mac, ip4_addr_t *ip) { struct dhcps_pool *pdhcps_pool = NULL; list_node *pback_node = NULL; bool ret = false; - for (pback_node = 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; if (memcmp(pdhcps_pool->mac, mac, sizeof(pdhcps_pool->mac)) == 0) { @@ -1316,12 +1367,12 @@ bool dhcp_search_ip_on_mac(u8_t *mac, ip4_addr_t *ip) * Returns : none *******************************************************************************/ void -dhcps_dns_setserver(const ip_addr_t *dnsserver) +dhcps_dns_setserver(dhcps_t *dhcps, const ip_addr_t *dnsserver) { if (dnsserver != NULL) { - dns_server = *(ip_2_ip4(dnsserver)); + dhcps->dns_server = *(ip_2_ip4(dnsserver)); } else { - dns_server = *(ip_2_ip4(IP_ADDR_ANY)); + dhcps->dns_server = *(ip_2_ip4(IP_ADDR_ANY)); } } @@ -1332,8 +1383,8 @@ dhcps_dns_setserver(const ip_addr_t *dnsserver) * Returns : ip4_addr_t *******************************************************************************/ ip4_addr_t -dhcps_dns_getserver(void) +dhcps_dns_getserver(dhcps_t *dhcps) { - return dns_server; + return dhcps->dns_server; } #endif // ESP_DHCP diff --git a/components/lwip/include/apps/dhcpserver/dhcpserver.h b/components/lwip/include/apps/dhcpserver/dhcpserver.h index 7a95eb499a..bf952cbb35 100644 --- a/components/lwip/include/apps/dhcpserver/dhcpserver.h +++ b/components/lwip/include/apps/dhcpserver/dhcpserver.h @@ -93,14 +93,19 @@ static inline bool dhcps_dns_enabled (dhcps_offer_t offer) return (offer & OFFER_DNS) != 0; } -void dhcps_start(struct netif *netif, ip4_addr_t ip); -void dhcps_stop(struct netif *netif); -void *dhcps_option_info(u8_t op_id, u32_t opt_len); -void dhcps_set_option_info(u8_t op_id, void *opt_info, u32_t opt_len); -bool dhcp_search_ip_on_mac(u8_t *mac, ip4_addr_t *ip); -void dhcps_dns_setserver(const ip_addr_t *dnsserver); -ip4_addr_t dhcps_dns_getserver(void); -void dhcps_set_new_lease_cb(dhcps_cb_t cb); +typedef struct dhcps_t dhcps_t; + +dhcps_t *dhcps_new(void); +void dhcps_delete(dhcps_t *dhcps); + +void dhcps_start(dhcps_t *dhcps, struct netif *netif, ip4_addr_t ip); +void dhcps_stop(dhcps_t *dhcps, struct netif *netif); +void *dhcps_option_info(dhcps_t *dhcps, u8_t op_id, u32_t opt_len); +void dhcps_set_option_info(dhcps_t *dhcps, u8_t op_id, void *opt_info, u32_t opt_len); +bool dhcp_search_ip_on_mac(dhcps_t *dhcps, u8_t *mac, ip4_addr_t *ip); +void dhcps_dns_setserver(dhcps_t *dhcps, const ip_addr_t *dnsserver); +ip4_addr_t dhcps_dns_getserver(dhcps_t *dhcps); +void dhcps_set_new_lease_cb(dhcps_t *dhcps, dhcps_cb_t cb); #ifdef __cplusplus } diff --git a/components/lwip/port/esp32/include/lwipopts.h b/components/lwip/port/esp32/include/lwipopts.h index 2416cfc93f..97de8a492a 100644 --- a/components/lwip/port/esp32/include/lwipopts.h +++ b/components/lwip/port/esp32/include/lwipopts.h @@ -1344,7 +1344,7 @@ */ #ifdef CONFIG_LWIP_DHCPS #define ESP_DHCPS 1 -#define ESP_DHCPS_TIMER 1 +#define ESP_DHCPS_TIMER 0 #else #define ESP_DHCPS 0 #define ESP_DHCPS_TIMER 0 diff --git a/tools/ci/check_copyright_ignore.txt b/tools/ci/check_copyright_ignore.txt index 48b0124d1e..3441c89e48 100644 --- a/tools/ci/check_copyright_ignore.txt +++ b/tools/ci/check_copyright_ignore.txt @@ -484,13 +484,11 @@ components/esp_local_ctrl/src/esp_local_ctrl_transport_ble.c components/esp_local_ctrl/src/esp_local_ctrl_transport_httpd.c components/esp_netif/include/esp_netif_ppp.h components/esp_netif/include/esp_netif_slip.h -components/esp_netif/include/esp_netif_sta_list.h components/esp_netif/loopback/esp_netif_loopback.c components/esp_netif/lwip/esp_netif_lwip_defaults.c components/esp_netif/lwip/esp_netif_lwip_ppp.h components/esp_netif/lwip/esp_netif_lwip_slip.c components/esp_netif/lwip/esp_netif_lwip_slip.h -components/esp_netif/lwip/esp_netif_sta_list.c components/esp_netif/private_include/esp_netif_private.h components/esp_netif/test/test_esp_netif.c components/esp_netif/test_apps/component_ut_test.py From adc00d1813e646b30c286a6e7d2b17ade75c3dd7 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Mon, 24 Jan 2022 09:58:06 +0100 Subject: [PATCH 03/10] lwip: Fix fuzzer layers after dhcps update --- components/lwip/test_afl_host/esp_attr.h | 1 + components/lwip/test_afl_host/network_mock.c | 7 +++++-- components/lwip/test_afl_host/test_dhcp_server.c | 9 ++++++--- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/components/lwip/test_afl_host/esp_attr.h b/components/lwip/test_afl_host/esp_attr.h index 3ed81f776d..3044f875be 100644 --- a/components/lwip/test_afl_host/esp_attr.h +++ b/components/lwip/test_afl_host/esp_attr.h @@ -7,3 +7,4 @@ #define SSIZE_MAX INT_MAX #undef assert #define assert(x) +#define sys_prot_t int diff --git a/components/lwip/test_afl_host/network_mock.c b/components/lwip/test_afl_host/network_mock.c index d5a990dd03..8f77a6af6e 100644 --- a/components/lwip/test_afl_host/network_mock.c +++ b/components/lwip/test_afl_host/network_mock.c @@ -1,11 +1,10 @@ #include "no_warn_host.h" #include "lwip/opt.h" -#include "lwip/def.h" #include "lwip/pbuf.h" #include "lwip/udp.h" #include "esp_netif.h" +#include "lwip/timeouts.h" #include -#include const ip_addr_t ip_addr_any; const ip_addr_t ip_addr_broadcast; @@ -234,3 +233,7 @@ void mem_free(void *rmem) { free(rmem); } + +void sys_timeout(u32_t msecs, sys_timeout_handler handler, void *arg) +{ +} diff --git a/components/lwip/test_afl_host/test_dhcp_server.c b/components/lwip/test_afl_host/test_dhcp_server.c index deb340f548..e788310d46 100644 --- a/components/lwip/test_afl_host/test_dhcp_server.c +++ b/components/lwip/test_afl_host/test_dhcp_server.c @@ -29,8 +29,9 @@ int main(int argc, char** argv) dhcp_test_init_di(); IP4_ADDR(&server_ip, 192,168,4,1); - dhcps_set_new_lease_cb(dhcp_test_dhcps_cb); - dhcps_start(&mynetif, server_ip); + dhcps_t *dhcps = dhcps_new(); + dhcps_set_new_lease_cb(dhcps, dhcp_test_dhcps_cb); + dhcps_start(dhcps, &mynetif, server_ip); #ifdef INSTR_IS_OFF p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL); @@ -62,7 +63,9 @@ int main(int argc, char** argv) p->tot_len = len; p->next = NULL; - dhcp_test_handle_dhcp(NULL, NULL, p, &ip_addr_any, 0); + dhcp_test_handle_dhcp(dhcps, NULL, p, &ip_addr_any, 0); } + dhcps_stop(dhcps, &mynetif); + dhcps_delete(dhcps); return 0; } From afe6bc0c88c4a50f411ffe44a4f0f15c7680f460 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Mon, 24 Jan 2022 17:41:06 +0100 Subject: [PATCH 04/10] lwip/dhcp_server: Fix mem-leaks caught by UT's --- components/lwip/apps/dhcpserver/dhcpserver.c | 17 +++++++++++++---- components/lwip/test_afl_host/network_mock.c | 4 ++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/components/lwip/apps/dhcpserver/dhcpserver.c b/components/lwip/apps/dhcpserver/dhcpserver.c index 6cc7f24ea9..71a16ff91d 100644 --- a/components/lwip/apps/dhcpserver/dhcpserver.c +++ b/components/lwip/apps/dhcpserver/dhcpserver.c @@ -129,13 +129,20 @@ dhcps_t *dhcps_new(void) dhcps->dhcps_dns = 0x00; dhcps->dhcps_pcb = NULL; dhcps->state = DHCPS_HANDLE_CREATED; - sys_timeout(DHCP_COARSE_TIMER_MSECS, dhcps_tmr, dhcps); return dhcps; } void dhcps_delete(dhcps_t *dhcps) { - dhcps->state = DHCPS_HANDLE_DELETE_PENDING; + if (dhcps) { + if (dhcps->state == DHCPS_HANDLE_STARTED) { + // if the dhcp-server has started already, we have to postpone the deletion + dhcps->state = DHCPS_HANDLE_DELETE_PENDING; + } else { + // otherwise, we're free to delete the handle immediately + free(dhcps); + } + } } static void get_ip_info(struct netif * netif, ip_info_t *ip_info) @@ -1207,10 +1214,11 @@ void dhcps_start(dhcps_t *dhcps, struct netif *netif, ip4_addr_t ip) udp_bind(dhcps->dhcps_pcb, &netif->ip_addr, DHCPS_SERVER_PORT); udp_recv(dhcps->dhcps_pcb, handle_dhcp, dhcps); - dhcps->state = DHCPS_HANDLE_STARTED; #if DHCPS_DEBUG DHCPS_LOG("dhcps:dhcps_start->udp_recv function Set a receive callback handle_dhcp for UDP_PCB pcb_dhcps\n"); #endif + dhcps->state = DHCPS_HANDLE_STARTED; + sys_timeout(DHCP_COARSE_TIMER_MSECS, dhcps_tmr, dhcps); } @@ -1246,6 +1254,7 @@ void dhcps_stop(dhcps_t *dhcps, struct netif *netif) free(pback_node); pback_node = NULL; } + sys_untimeout(dhcps_tmr, dhcps); dhcps->state = DHCPS_HANDLE_STOPPED; } @@ -1301,10 +1310,10 @@ static void dhcps_tmr(void *arg) return; } - sys_timeout(DHCP_COARSE_TIMER_MSECS, dhcps_tmr, dhcps); if (state != DHCPS_HANDLE_STARTED) { return; } + sys_timeout(DHCP_COARSE_TIMER_MSECS, dhcps_tmr, dhcps); u8_t num_dhcps_pool = 0; list_node *pback_node = NULL; list_node *pnode = NULL; diff --git a/components/lwip/test_afl_host/network_mock.c b/components/lwip/test_afl_host/network_mock.c index 8f77a6af6e..5daca4cb8d 100644 --- a/components/lwip/test_afl_host/network_mock.c +++ b/components/lwip/test_afl_host/network_mock.c @@ -237,3 +237,7 @@ void mem_free(void *rmem) void sys_timeout(u32_t msecs, sys_timeout_handler handler, void *arg) { } + +void sys_untimeout(sys_timeout_handler handler, void *arg) +{ +} From 76e9d3cad084304877741d6e663b19d59ba2cf45 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Wed, 26 Jan 2022 13:43:57 +0100 Subject: [PATCH 05/10] lwip/dhcpserver: Add doxy documentation --- .../lwip/include/apps/dhcpserver/dhcpserver.h | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/components/lwip/include/apps/dhcpserver/dhcpserver.h b/components/lwip/include/apps/dhcpserver/dhcpserver.h index bf952cbb35..df82bde480 100644 --- a/components/lwip/include/apps/dhcpserver/dhcpserver.h +++ b/components/lwip/include/apps/dhcpserver/dhcpserver.h @@ -95,16 +95,85 @@ static inline bool dhcps_dns_enabled (dhcps_offer_t offer) typedef struct dhcps_t dhcps_t; +/** + * @brief Creates new DHCP server object + * + * @return Pointer to the DHCP server handle on success, NULL on error + */ dhcps_t *dhcps_new(void); + +/** + * @brief Deletes supplied DHPC server object + * + * @warning This may not delete the handle immediately if the server wasn't + * stopped properly, but mark for deleting once the timer callback occurs + * + * @param dhcps Pointer to the DHCP handle + */ void dhcps_delete(dhcps_t *dhcps); +/** + * @brief Starts the DHCP server on the specified network interface + * + * @param dhcps Pointer to the DHCP handle + * @param netif Pointer to the lwIP's network interface struct + * @param ip DHCP server's address + */ void 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 + */ void dhcps_stop(dhcps_t *dhcps, struct netif *netif); + +/** + * @brief Gets the DHCP server option info + * @param dhcps Pointer to the DHCP handle + * @param op_id DHCP message option id + * @param opt_len DHCP message option length + * @return DHCP message option addr + */ void *dhcps_option_info(dhcps_t *dhcps, u8_t op_id, u32_t opt_len); + +/** + * @brief Sets the DHCP server option info + * @param dhcps Pointer to the DHCP handle + * @param op_id DHCP message option id + * @param opt_info DHCP message option info + * @param opt_len DHCP message option length + */ void 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 + * @param dhcps Pointer to the DHCP handle + * @param mac Supplied MAC address + * @param ip Pointer to the resultant IP address + * @return True if the IP address has been found + */ 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 + */ void dhcps_dns_setserver(dhcps_t *dhcps, const ip_addr_t *dnsserver); + +/** + * @brief Returns DNS server associated with this DHCP server + * @param dhcps Pointer to the DHCP handle + * @return Address of the DNS server + */ ip4_addr_t dhcps_dns_getserver(dhcps_t *dhcps); + +/** + * @brief Sets callback on assigning an IP to the connected client + * @param dhcps Pointer to the DHCP handle + * @param cb Callback for dhcp server + */ void dhcps_set_new_lease_cb(dhcps_t *dhcps, dhcps_cb_t cb); #ifdef __cplusplus From 5f135741a144c8b4dee31f89b5c4e85378169a1d Mon Sep 17 00:00:00 2001 From: David Cermak Date: Wed, 2 Feb 2022 15:54:49 +0100 Subject: [PATCH 06/10] 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 } From 775c3a6253cee996897b1564c2598ba154ffc96f Mon Sep 17 00:00:00 2001 From: David Cermak Date: Wed, 2 Feb 2022 16:10:57 +0100 Subject: [PATCH 07/10] lwip/dhcps: Add simple start/stop unit cases --- components/lwip/test/test_lwip_apps.c | 34 +++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/components/lwip/test/test_lwip_apps.c b/components/lwip/test/test_lwip_apps.c index bf55b5f38a..df5fdbc248 100644 --- a/components/lwip/test/test_lwip_apps.c +++ b/components/lwip/test/test_lwip_apps.c @@ -1,3 +1,8 @@ +/* + * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ #include "freertos/FreeRTOS.h" #include "freertos/event_groups.h" #include "test_utils.h" @@ -6,6 +11,7 @@ #include "lwip/netdb.h" #include "lwip/sockets.h" #include "ping/ping_sock.h" +#include "dhcpserver/dhcpserver.h" #define ETH_PING_END_BIT BIT(1) #define ETH_PING_DURATION_MS (5000) @@ -104,3 +110,31 @@ TEST_CASE("localhost ping test", "[lwip]") vEventGroupDelete(eth_event_group); } + +TEST_CASE("dhcp server init/deinit", "[lwip][leaks=0]") +{ + dhcps_t *dhcps = dhcps_new(); + ip4_addr_t ip = { .addr = IPADDR_ANY }; + TEST_ASSERT(dhcps_start(dhcps, NULL, ip) == ERR_ARG); + TEST_ASSERT(dhcps_stop(dhcps, NULL) == ERR_ARG); + dhcps_delete(dhcps); +} + +TEST_CASE("dhcp server start/stop on localhost", "[lwip]") +{ + test_case_uses_tcpip(); + dhcps_t *dhcps = dhcps_new(); + struct netif *netif; + + NETIF_FOREACH(netif) { + if (netif->name[0] == 'l' && netif->name[1] == 'o') { + break; + } + } + TEST_ASSERT_NOT_NULL(netif); + + ip4_addr_t ip = { .addr = 0x7f0001 }; + TEST_ASSERT(dhcps_start(dhcps, netif, ip) == ERR_OK); + TEST_ASSERT(dhcps_stop(dhcps, netif) == ERR_OK); + dhcps_delete(dhcps); +} From 3d1c05aefb1645532c719e930298e2f51ba62c52 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Wed, 2 Feb 2022 17:17:39 +0100 Subject: [PATCH 08/10] lwip/dhcps: Add dhcps callback argument for associated netif --- components/esp_netif/include/esp_netif_types.h | 1 + components/esp_netif/lwip/esp_netif_lwip.c | 12 +++++++----- components/lwip/apps/dhcpserver/dhcpserver.c | 6 ++++-- components/lwip/include/apps/dhcpserver/dhcpserver.h | 5 +++-- 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/components/esp_netif/include/esp_netif_types.h b/components/esp_netif/include/esp_netif_types.h index 640606a6e9..e00d4eb063 100644 --- a/components/esp_netif/include/esp_netif_types.h +++ b/components/esp_netif/include/esp_netif_types.h @@ -135,6 +135,7 @@ typedef struct { /** Event structure for IP_EVENT_AP_STAIPASSIGNED event */ typedef struct { + esp_netif_t *esp_netif; /*!< Pointer to the associated netif handle */ esp_ip4_addr_t ip; /*!< IP address which was assigned to the station */ uint8_t mac[6]; /*!< MAC address of the connected client */ } ip_event_ap_staipassigned_t; diff --git a/components/esp_netif/lwip/esp_netif_lwip.c b/components/esp_netif/lwip/esp_netif_lwip.c index 464bce971d..22a3d60e15 100644 --- a/components/esp_netif/lwip/esp_netif_lwip.c +++ b/components/esp_netif/lwip/esp_netif_lwip.c @@ -793,12 +793,14 @@ esp_err_t esp_netif_recv_hook_detach(esp_netif_t *esp_netif) #endif // CONFIG_ESP_NETIF_L2_TAP #if ESP_DHCPS -static void esp_netif_dhcps_cb(uint8_t ip[4], uint8_t mac[6]) +static void esp_netif_dhcps_cb(void* arg, uint8_t ip[4], uint8_t mac[6]) { - ip_event_ap_staipassigned_t evt = { 0 }; + esp_netif_t *esp_netif = arg; + ESP_LOGD(TAG, "%s esp_netif:%p", __func__, esp_netif); + ip_event_ap_staipassigned_t evt = { .esp_netif = esp_netif }; memcpy((char *)&evt.ip.addr, (char *)ip, sizeof(evt.ip.addr)); memcpy((char *)&evt.mac, mac, sizeof(evt.mac)); - ESP_LOGI(TAG, "DHCP server assigned IP to a station, IP is: " IPSTR, IP2STR(&evt.ip)); + ESP_LOGI(TAG, "DHCP server assigned IP to a client, IP is: " IPSTR, IP2STR(&evt.ip)); ESP_LOGD(TAG, "Client's MAC: %x:%x:%x:%x:%x:%x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); int ret = esp_event_post(IP_EVENT, IP_EVENT_AP_STAIPASSIGNED, &evt, sizeof(evt), 0); @@ -866,7 +868,7 @@ static esp_err_t esp_netif_start_api(esp_netif_api_msg_t *msg) ip4_addr_t lwip_netmask; memcpy(&lwip_ip, &default_ip->ip, sizeof(struct ip4_addr)); memcpy(&lwip_netmask, &default_ip->netmask, sizeof(struct ip4_addr)); - dhcps_set_new_lease_cb(esp_netif->dhcps, esp_netif_dhcps_cb); + 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); esp_netif->dhcps_status = ESP_NETIF_DHCP_STARTED; @@ -1313,7 +1315,7 @@ static esp_err_t esp_netif_dhcps_start_api(esp_netif_api_msg_t *msg) ip4_addr_t lwip_netmask; memcpy(&lwip_ip, &default_ip->ip, sizeof(struct ip4_addr)); memcpy(&lwip_netmask, &default_ip->netmask, sizeof(struct ip4_addr)); - dhcps_set_new_lease_cb(esp_netif->dhcps, esp_netif_dhcps_cb); + 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); esp_netif->dhcps_status = ESP_NETIF_DHCP_STARTED; diff --git a/components/lwip/apps/dhcpserver/dhcpserver.c b/components/lwip/apps/dhcpserver/dhcpserver.c index 2107db15b3..9ed3a011d9 100644 --- a/components/lwip/apps/dhcpserver/dhcpserver.c +++ b/components/lwip/apps/dhcpserver/dhcpserver.c @@ -101,6 +101,7 @@ struct dhcps_t { dhcps_offer_t dhcps_offer; dhcps_offer_t dhcps_dns; dhcps_cb_t dhcps_cb; + void* dhcps_cb_arg; struct udp_pcb *dhcps_pcb; dhcps_handle_state state; }; @@ -741,7 +742,7 @@ static void send_ack(dhcps_t *dhcps, struct dhcps_msg *m, u16_t len) #endif if (SendAck_err_t == ERR_OK) { - dhcps->dhcps_cb(m->yiaddr, m->chaddr); + dhcps->dhcps_cb(dhcps->dhcps_cb_arg, m->yiaddr, m->chaddr); } if (p->ref != 0) { @@ -1185,12 +1186,13 @@ static void dhcps_poll_set(dhcps_t *dhcps, u32_t ip) * Parameters : cb -- callback for dhcp server * Returns : ERR_OK on success *******************************************************************************/ -err_t 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, void* cb_arg) { if (dhcps == NULL) { return ERR_ARG; } dhcps->dhcps_cb = cb; + dhcps->dhcps_cb_arg = cb_arg; return ERR_OK; } diff --git a/components/lwip/include/apps/dhcpserver/dhcpserver.h b/components/lwip/include/apps/dhcpserver/dhcpserver.h index 17d9a3c496..ca97cea276 100644 --- a/components/lwip/include/apps/dhcpserver/dhcpserver.h +++ b/components/lwip/include/apps/dhcpserver/dhcpserver.h @@ -82,7 +82,7 @@ typedef struct { dhcps_lease_t dhcps_poll; } dhcps_options_t; -typedef void (*dhcps_cb_t)(u8_t client_ip[4], u8_t client_mac[6]); +typedef void (*dhcps_cb_t)(void* cb_arg, u8_t client_ip[4], u8_t client_mac[6]); static inline bool dhcps_router_enabled (dhcps_offer_t offer) { @@ -179,9 +179,10 @@ 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 + * @param cb_arg Context pointer to be added to the callback * @return ERR_ARG if invalid handle, ERR_OK on success */ -err_t 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, void* cb_arg); #ifdef __cplusplus } From 1b49cf373f1b4a426fd8d4e96a0f289e5e1e948b Mon Sep 17 00:00:00 2001 From: David Cermak Date: Wed, 2 Feb 2022 17:30:20 +0100 Subject: [PATCH 09/10] lwip/dhcps: Fix fuzzer compilation --- components/lwip/apps/dhcpserver/dhcpserver.c | 3 +-- components/lwip/test_afl_host/network_mock.c | 5 +++++ components/lwip/test_afl_host/test_dhcp_server.c | 4 ++-- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/components/lwip/apps/dhcpserver/dhcpserver.c b/components/lwip/apps/dhcpserver/dhcpserver.c index 9ed3a011d9..e3db845d3f 100644 --- a/components/lwip/apps/dhcpserver/dhcpserver.c +++ b/components/lwip/apps/dhcpserver/dhcpserver.c @@ -6,8 +6,7 @@ //#include "esp_common.h" #include #include -#include -#include "lwip/inet.h" +#include "lwip/dhcp.h" #include "lwip/err.h" #include "lwip/pbuf.h" #include "lwip/udp.h" diff --git a/components/lwip/test_afl_host/network_mock.c b/components/lwip/test_afl_host/network_mock.c index 5daca4cb8d..7b77197c92 100644 --- a/components/lwip/test_afl_host/network_mock.c +++ b/components/lwip/test_afl_host/network_mock.c @@ -229,6 +229,11 @@ void * mem_malloc(mem_size_t size) return malloc(size); } +void * mem_calloc(size_t nr, mem_size_t size) +{ + return calloc(nr, size); +} + void mem_free(void *rmem) { free(rmem); diff --git a/components/lwip/test_afl_host/test_dhcp_server.c b/components/lwip/test_afl_host/test_dhcp_server.c index e788310d46..54084ee55a 100644 --- a/components/lwip/test_afl_host/test_dhcp_server.c +++ b/components/lwip/test_afl_host/test_dhcp_server.c @@ -10,7 +10,7 @@ ip4_addr_t server_ip; struct netif mynetif; // dhcps callback -void dhcp_test_dhcps_cb (u8_t client_ip[4]) {} +void dhcp_test_dhcps_cb (void* cb_arg, u8_t client_ip[4], u8_t client_mac[6]) {} // Dependency injected static function to pass the packet into parser void dhcp_test_handle_dhcp(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port); @@ -30,7 +30,7 @@ int main(int argc, char** argv) IP4_ADDR(&server_ip, 192,168,4,1); dhcps_t *dhcps = dhcps_new(); - dhcps_set_new_lease_cb(dhcps, dhcp_test_dhcps_cb); + dhcps_set_new_lease_cb(dhcps, dhcp_test_dhcps_cb, NULL); dhcps_start(dhcps, &mynetif, server_ip); #ifdef INSTR_IS_OFF From 27375c7917f17458760e96ba316001b8610b5cc5 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Mon, 7 Mar 2022 10:41:28 +0100 Subject: [PATCH 10/10] esp_netif: Cleanup dhcp-server allocations --- components/esp_netif/lwip/esp_netif_lwip.c | 5 +++++ components/lwip/apps/dhcpserver/dhcpserver.c | 14 +++++--------- components/lwip/test/test_lwip_apps.c | 1 + 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/components/esp_netif/lwip/esp_netif_lwip.c b/components/esp_netif/lwip/esp_netif_lwip.c index 22a3d60e15..1497460363 100644 --- a/components/esp_netif/lwip/esp_netif_lwip.c +++ b/components/esp_netif/lwip/esp_netif_lwip.c @@ -552,6 +552,11 @@ esp_netif_t *esp_netif_new(const esp_netif_config_t *esp_netif_config) // Create DHCP server structure if (esp_netif_config->base->flags & ESP_NETIF_DHCP_SERVER) { esp_netif->dhcps = dhcps_new(); + if (esp_netif->dhcps == NULL) { + ESP_LOGE(TAG, "Failed to create dhcp server handle"); + esp_netif_destroy(esp_netif); + return NULL; + } } #endif diff --git a/components/lwip/apps/dhcpserver/dhcpserver.c b/components/lwip/apps/dhcpserver/dhcpserver.c index e3db845d3f..0806104872 100644 --- a/components/lwip/apps/dhcpserver/dhcpserver.c +++ b/components/lwip/apps/dhcpserver/dhcpserver.c @@ -111,8 +111,7 @@ static void dhcps_tmr(void* arg); dhcps_t *dhcps_new(void) { - dhcps_t *dhcps = mem_malloc(sizeof(dhcps_t)); - memset(dhcps , 0x00 , sizeof(dhcps_t)); + dhcps_t *dhcps = mem_calloc(1, sizeof(dhcps_t)); if (dhcps == NULL) { return NULL; @@ -126,7 +125,7 @@ dhcps_t *dhcps_new(void) #endif dhcps->plist = NULL; dhcps->renew = false; - dhcps->dhcps_lease_time = DHCPS_LEASE_TIME_DEF; //minute + dhcps->dhcps_lease_time = DHCPS_LEASE_TIME_DEF; dhcps->dhcps_offer = 0xFF; dhcps->dhcps_dns = 0x00; dhcps->dhcps_pcb = NULL; @@ -926,14 +925,12 @@ static s16_t parse_msg(dhcps_t *dhcps, struct dhcps_msg *m, u16_t len) pdhcps_pool = NULL; pnode = NULL; } else { - pdhcps_pool = (struct dhcps_pool *)mem_malloc(sizeof(struct dhcps_pool)); - memset(pdhcps_pool , 0x00 , sizeof(struct dhcps_pool)); + pdhcps_pool = (struct dhcps_pool *)mem_calloc(1, sizeof(struct dhcps_pool)); pdhcps_pool->ip.addr = dhcps->client_address.addr; memcpy(pdhcps_pool->mac, m->chaddr, sizeof(pdhcps_pool->mac)); pdhcps_pool->lease_timer = lease_timer; - pnode = (list_node *)mem_malloc(sizeof(list_node)); - memset(pnode , 0x00 , sizeof(list_node)); + pnode = (list_node *)mem_calloc(1, sizeof(list_node)); pnode->pnode = pdhcps_pool; pnode->pnext = NULL; @@ -1033,13 +1030,12 @@ static void handle_dhcp(void *arg, malloc_len = p->tot_len; } - pmsg_dhcps = (struct dhcps_msg *)mem_malloc(malloc_len); + pmsg_dhcps = (struct dhcps_msg *)mem_calloc(1, malloc_len); if (NULL == pmsg_dhcps) { pbuf_free(p); return; } - memset(pmsg_dhcps , 0x00 , malloc_len); p_dhcps_msg = (u8_t *)pmsg_dhcps; tlen = p->tot_len; data = p->payload; diff --git a/components/lwip/test/test_lwip_apps.c b/components/lwip/test/test_lwip_apps.c index df5fdbc248..a1141ed07d 100644 --- a/components/lwip/test/test_lwip_apps.c +++ b/components/lwip/test/test_lwip_apps.c @@ -114,6 +114,7 @@ TEST_CASE("localhost ping test", "[lwip]") TEST_CASE("dhcp server init/deinit", "[lwip][leaks=0]") { dhcps_t *dhcps = dhcps_new(); + TEST_ASSERT_NOT_NULL(dhcps); ip4_addr_t ip = { .addr = IPADDR_ANY }; TEST_ASSERT(dhcps_start(dhcps, NULL, ip) == ERR_ARG); TEST_ASSERT(dhcps_stop(dhcps, NULL) == ERR_ARG);