mirror of
https://github.com/espressif/esp-idf.git
synced 2024-10-05 20:47:46 -04:00
lwip/dhcps: Support for dynamic dhcp server instances
This commit is contained in:
parent
bab051f450
commit
c005b04d1c
@ -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
|
||||
*
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -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 <string.h>
|
||||
#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;
|
||||
|
@ -6,12 +6,14 @@
|
||||
//#include "esp_common.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <lwip/dhcp.h>
|
||||
#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
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user