2021-09-12 02:47:14 -04:00
|
|
|
/*
|
2024-05-21 05:02:55 -04:00
|
|
|
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
|
2021-09-12 02:47:14 -04:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2016-08-17 11:08:22 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2022-07-22 04:14:37 -04:00
|
|
|
#include <assert.h>
|
2022-02-02 11:30:20 -05:00
|
|
|
#include "lwip/dhcp.h"
|
2016-08-17 11:08:22 -04:00
|
|
|
#include "lwip/err.h"
|
|
|
|
#include "lwip/pbuf.h"
|
|
|
|
#include "lwip/udp.h"
|
|
|
|
#include "lwip/mem.h"
|
|
|
|
#include "lwip/ip_addr.h"
|
2022-01-19 13:50:49 -05:00
|
|
|
#include "lwip/timeouts.h"
|
2022-10-20 05:28:28 -04:00
|
|
|
#include "lwip/etharp.h"
|
|
|
|
#include "lwip/prot/ethernet.h"
|
2016-08-17 11:08:22 -04:00
|
|
|
|
2018-09-06 07:43:08 -04:00
|
|
|
#include "dhcpserver/dhcpserver.h"
|
|
|
|
#include "dhcpserver/dhcpserver_options.h"
|
2016-08-17 11:08:22 -04:00
|
|
|
|
2022-03-25 13:58:44 -04:00
|
|
|
#if ESP_DHCPS
|
2016-08-17 11:08:22 -04:00
|
|
|
|
2022-07-19 04:21:44 -04:00
|
|
|
#ifdef LWIP_HOOK_FILENAME
|
|
|
|
#include LWIP_HOOK_FILENAME
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef LWIP_HOOK_DHCPS_POST_APPEND_OPTS
|
|
|
|
#define LWIP_HOOK_DHCPS_POST_APPEND_OPTS(netif, dhcps, state, pp_opts)
|
|
|
|
#endif
|
|
|
|
|
2016-08-17 11:08:22 -04:00
|
|
|
#define BOOTP_BROADCAST 0x8000
|
2022-10-20 05:28:28 -04:00
|
|
|
#define BROADCAST_BIT_IS_SET(flag) (flag & BOOTP_BROADCAST)
|
2016-08-17 11:08:22 -04:00
|
|
|
|
|
|
|
#define DHCP_REQUEST 1
|
|
|
|
#define DHCP_REPLY 2
|
|
|
|
#define DHCP_HTYPE_ETHERNET 1
|
|
|
|
#define DHCP_HLEN_ETHERNET 6
|
|
|
|
#define DHCP_MSG_LEN 236
|
|
|
|
|
|
|
|
#define DHCPS_SERVER_PORT 67
|
|
|
|
#define DHCPS_CLIENT_PORT 68
|
|
|
|
|
|
|
|
#define DHCPDISCOVER 1
|
|
|
|
#define DHCPOFFER 2
|
|
|
|
#define DHCPREQUEST 3
|
|
|
|
#define DHCPDECLINE 4
|
|
|
|
#define DHCPACK 5
|
|
|
|
#define DHCPNAK 6
|
|
|
|
#define DHCPRELEASE 7
|
|
|
|
|
|
|
|
#define DHCP_OPTION_SUBNET_MASK 1
|
|
|
|
#define DHCP_OPTION_ROUTER 3
|
|
|
|
#define DHCP_OPTION_DNS_SERVER 6
|
|
|
|
#define DHCP_OPTION_REQ_IPADDR 50
|
|
|
|
#define DHCP_OPTION_LEASE_TIME 51
|
|
|
|
#define DHCP_OPTION_MSG_TYPE 53
|
|
|
|
#define DHCP_OPTION_SERVER_ID 54
|
|
|
|
#define DHCP_OPTION_INTERFACE_MTU 26
|
|
|
|
#define DHCP_OPTION_PERFORM_ROUTER_DISCOVERY 31
|
|
|
|
#define DHCP_OPTION_BROADCAST_ADDRESS 28
|
|
|
|
#define DHCP_OPTION_REQ_LIST 55
|
|
|
|
#define DHCP_OPTION_END 255
|
|
|
|
|
|
|
|
//#define USE_CLASS_B_NET 1
|
|
|
|
#define DHCPS_DEBUG 0
|
|
|
|
#define DHCPS_LOG printf
|
|
|
|
|
2022-10-27 06:25:00 -04:00
|
|
|
#define IS_INVALID_SUBNET_MASK(x) (((x-1) | x) != 0xFFFFFFFF)
|
|
|
|
/* Notes:
|
2023-02-21 21:46:41 -05:00
|
|
|
* CIDR eliminates the traditional Class A, Class B and Class C addresses.
|
2022-10-27 06:25:00 -04:00
|
|
|
*/
|
|
|
|
#define IP_CLASS_HOST_NUM(mask) (0xffffffff & ~mask)
|
2023-02-21 21:46:41 -05:00
|
|
|
#define DHCP_CHECK_SUBNET_MASK_IP(mask) \
|
2022-10-27 06:25:00 -04:00
|
|
|
do { \
|
|
|
|
if (IS_INVALID_SUBNET_MASK(mask)) { \
|
|
|
|
DHCPS_LOG("dhcps: Illegal subnet mask.\n"); \
|
|
|
|
return ERR_ARG; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
2023-07-14 08:07:39 -04:00
|
|
|
#define DHCP_CHECK_IP_MATCH_SUBNET_MASK(mask, ip) \
|
|
|
|
u32_t start_ip = 0; \
|
|
|
|
u32_t end_ip = 0; \
|
|
|
|
do { \
|
|
|
|
start_ip = ip & mask; \
|
|
|
|
end_ip = start_ip | ~mask; \
|
|
|
|
if (ip == end_ip || ip == start_ip) { \
|
|
|
|
DHCPS_LOG("dhcps: ip address and subnet mask do not match.\n"); \
|
|
|
|
return ERR_ARG; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
2017-10-31 14:13:04 -04:00
|
|
|
#define MAX_STATION_NUM CONFIG_LWIP_DHCPS_MAX_STATION_NUM
|
2016-08-17 11:08:22 -04:00
|
|
|
|
|
|
|
#define DHCPS_STATE_OFFER 1
|
|
|
|
#define DHCPS_STATE_DECLINE 2
|
|
|
|
#define DHCPS_STATE_ACK 3
|
|
|
|
#define DHCPS_STATE_NAK 4
|
|
|
|
#define DHCPS_STATE_IDLE 5
|
|
|
|
#define DHCPS_STATE_RELEASE 6
|
|
|
|
|
2022-01-19 13:50:49 -05:00
|
|
|
typedef enum {
|
|
|
|
DHCPS_HANDLE_CREATED = 0,
|
|
|
|
DHCPS_HANDLE_STARTED,
|
|
|
|
DHCPS_HANDLE_STOPPED,
|
|
|
|
DHCPS_HANDLE_DELETE_PENDING,
|
|
|
|
} dhcps_handle_state;
|
|
|
|
|
|
|
|
typedef struct list_node {
|
2017-09-05 03:11:11 -04:00
|
|
|
void *pnode;
|
2022-01-19 13:50:49 -05:00
|
|
|
struct list_node *pnext;
|
2017-09-05 03:11:11 -04:00
|
|
|
} list_node;
|
|
|
|
|
2022-01-19 11:15:40 -05:00
|
|
|
typedef struct {
|
|
|
|
ip4_addr_t ip;
|
|
|
|
ip4_addr_t netmask;
|
|
|
|
ip4_addr_t gw;
|
|
|
|
} ip_info_t;
|
2016-08-17 11:08:22 -04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-08-26 02:15:14 -04:00
|
|
|
static const u32_t magic_cookie = 0x63538263;
|
2016-08-17 11:08:22 -04:00
|
|
|
|
2022-01-19 13:50:49 -05:00
|
|
|
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;
|
2022-02-02 11:17:39 -05:00
|
|
|
void* dhcps_cb_arg;
|
2022-01-19 13:50:49 -05:00
|
|
|
struct udp_pcb *dhcps_pcb;
|
|
|
|
dhcps_handle_state state;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static void dhcps_tmr(void* arg);
|
|
|
|
|
|
|
|
|
|
|
|
dhcps_t *dhcps_new(void)
|
|
|
|
{
|
2022-03-07 04:41:28 -05:00
|
|
|
dhcps_t *dhcps = mem_calloc(1, sizeof(dhcps_t));
|
2022-02-02 09:54:49 -05:00
|
|
|
|
2022-01-19 13:50:49 -05:00
|
|
|
if (dhcps == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
dhcps->dhcps_netif = NULL;
|
|
|
|
dhcps->dns_server.addr = 0;
|
2020-04-02 08:38:09 -04:00
|
|
|
#ifdef USE_CLASS_B_NET
|
2022-01-19 13:50:49 -05:00
|
|
|
dhcps->dhcps_mask.addr = PP_HTONL(LWIP_MAKEU32(255, 240, 0, 0));
|
2020-04-02 08:38:09 -04:00
|
|
|
#else
|
2022-01-19 13:50:49 -05:00
|
|
|
dhcps->dhcps_mask.addr = PP_HTONL(LWIP_MAKEU32(255, 255, 255, 0));
|
2020-04-02 08:38:09 -04:00
|
|
|
#endif
|
2022-01-19 13:50:49 -05:00
|
|
|
dhcps->plist = NULL;
|
|
|
|
dhcps->renew = false;
|
2022-03-07 04:41:28 -05:00
|
|
|
dhcps->dhcps_lease_time = DHCPS_LEASE_TIME_DEF;
|
2022-01-19 13:50:49 -05:00
|
|
|
dhcps->dhcps_offer = 0xFF;
|
|
|
|
dhcps->dhcps_dns = 0x00;
|
|
|
|
dhcps->dhcps_pcb = NULL;
|
|
|
|
dhcps->state = DHCPS_HANDLE_CREATED;
|
|
|
|
return dhcps;
|
|
|
|
}
|
2016-08-17 11:08:22 -04:00
|
|
|
|
2022-01-19 13:50:49 -05:00
|
|
|
void dhcps_delete(dhcps_t *dhcps)
|
|
|
|
{
|
2022-01-24 11:41:06 -05:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2022-01-19 13:50:49 -05:00
|
|
|
}
|
2022-01-19 11:15:40 -05:00
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
2016-08-17 11:08:22 -04:00
|
|
|
|
2016-08-26 05:46:55 -04:00
|
|
|
/******************************************************************************
|
|
|
|
* FunctionName : dhcps_option_info
|
|
|
|
* Description : get the DHCP message option info
|
|
|
|
* Parameters : op_id -- DHCP message option id
|
|
|
|
* opt_len -- DHCP message option length
|
|
|
|
* Returns : DHCP message option addr
|
|
|
|
*******************************************************************************/
|
2022-01-19 13:50:49 -05:00
|
|
|
void *dhcps_option_info(dhcps_t *dhcps, u8_t op_id, u32_t opt_len)
|
2016-08-17 11:08:22 -04:00
|
|
|
{
|
2016-09-01 07:05:50 -04:00
|
|
|
void *option_arg = NULL;
|
2022-02-02 09:54:49 -05:00
|
|
|
if (dhcps == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-09-01 07:05:50 -04:00
|
|
|
|
|
|
|
switch (op_id) {
|
|
|
|
case IP_ADDRESS_LEASE_TIME:
|
|
|
|
if (opt_len == sizeof(dhcps_time_t)) {
|
2022-01-19 13:50:49 -05:00
|
|
|
option_arg = &dhcps->dhcps_lease_time;
|
2016-09-01 07:05:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case REQUESTED_IP_ADDRESS:
|
|
|
|
if (opt_len == sizeof(dhcps_lease_t)) {
|
2022-01-19 13:50:49 -05:00
|
|
|
option_arg = &dhcps->dhcps_poll;
|
2016-09-01 07:05:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ROUTER_SOLICITATION_ADDRESS:
|
|
|
|
if (opt_len == sizeof(dhcps_offer_t)) {
|
2022-01-19 13:50:49 -05:00
|
|
|
option_arg = &dhcps->dhcps_offer;
|
2016-09-01 07:05:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2017-06-07 09:22:53 -04:00
|
|
|
case DOMAIN_NAME_SERVER:
|
|
|
|
if (opt_len == sizeof(dhcps_offer_t)) {
|
2022-01-19 13:50:49 -05:00
|
|
|
option_arg = &dhcps->dhcps_dns;
|
2017-06-07 09:22:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2020-04-02 08:38:09 -04:00
|
|
|
case SUBNET_MASK:
|
2022-01-19 13:50:49 -05:00
|
|
|
if (opt_len == sizeof(dhcps->dhcps_mask)) {
|
|
|
|
option_arg = &dhcps->dhcps_mask;
|
2020-04-02 08:38:09 -04:00
|
|
|
}
|
2017-06-07 09:22:53 -04:00
|
|
|
|
2020-04-02 08:38:09 -04:00
|
|
|
break;
|
2016-09-01 07:05:50 -04:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return option_arg;
|
2016-08-17 11:08:22 -04:00
|
|
|
}
|
2016-08-26 02:15:14 -04:00
|
|
|
|
2017-06-07 09:22:53 -04:00
|
|
|
/******************************************************************************
|
|
|
|
* FunctionName : dhcps_set_option_info
|
|
|
|
* Description : set the DHCP message option info
|
|
|
|
* Parameters : op_id -- DHCP message option id
|
|
|
|
* opt_info -- DHCP message option info
|
|
|
|
* opt_len -- DHCP message option length
|
|
|
|
* Returns : none
|
|
|
|
*******************************************************************************/
|
2022-02-02 09:54:49 -05:00
|
|
|
err_t dhcps_set_option_info(dhcps_t *dhcps, u8_t op_id, void *opt_info, u32_t opt_len)
|
2017-06-07 09:22:53 -04:00
|
|
|
{
|
2022-02-02 09:54:49 -05:00
|
|
|
if (dhcps == NULL || opt_info == NULL) {
|
|
|
|
return ERR_ARG;
|
2017-06-07 09:22:53 -04:00
|
|
|
}
|
|
|
|
switch (op_id) {
|
|
|
|
case IP_ADDRESS_LEASE_TIME:
|
|
|
|
if (opt_len == sizeof(dhcps_time_t)) {
|
2022-01-19 13:50:49 -05:00
|
|
|
dhcps->dhcps_lease_time = *(dhcps_time_t *)opt_info;
|
2017-06-07 09:22:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case REQUESTED_IP_ADDRESS:
|
|
|
|
if (opt_len == sizeof(dhcps_lease_t)) {
|
2022-01-19 13:50:49 -05:00
|
|
|
dhcps->dhcps_poll = *(dhcps_lease_t *)opt_info;
|
2017-06-07 09:22:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ROUTER_SOLICITATION_ADDRESS:
|
|
|
|
if (opt_len == sizeof(dhcps_offer_t)) {
|
2022-01-19 13:50:49 -05:00
|
|
|
dhcps->dhcps_offer = *(dhcps_offer_t *)opt_info;
|
2017-06-07 09:22:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DOMAIN_NAME_SERVER:
|
|
|
|
if (opt_len == sizeof(dhcps_offer_t)) {
|
2022-01-19 13:50:49 -05:00
|
|
|
dhcps->dhcps_dns = *(dhcps_offer_t *)opt_info;
|
2017-06-07 09:22:53 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2020-04-02 08:38:09 -04:00
|
|
|
case SUBNET_MASK:
|
2022-01-19 13:50:49 -05:00
|
|
|
if (opt_len == sizeof(dhcps->dhcps_mask)) {
|
|
|
|
dhcps->dhcps_mask = *(ip4_addr_t *)opt_info;
|
2020-04-02 08:38:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-06-07 09:22:53 -04:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2022-02-02 09:54:49 -05:00
|
|
|
return ERR_OK;
|
2017-06-07 09:22:53 -04:00
|
|
|
}
|
|
|
|
|
2016-08-17 11:08:22 -04:00
|
|
|
/******************************************************************************
|
|
|
|
* FunctionName : node_insert_to_list
|
|
|
|
* Description : insert the node to the list
|
2016-08-26 05:46:55 -04:00
|
|
|
* Parameters : phead -- the head node of the list
|
|
|
|
* pinsert -- the insert node of the list
|
2016-08-17 11:08:22 -04:00
|
|
|
* Returns : none
|
|
|
|
*******************************************************************************/
|
2017-09-05 03:11:11 -04:00
|
|
|
static void node_insert_to_list(list_node **phead, list_node *pinsert)
|
2016-08-17 11:08:22 -04:00
|
|
|
{
|
2016-09-01 07:05:50 -04:00
|
|
|
list_node *plist = NULL;
|
|
|
|
struct dhcps_pool *pdhcps_pool = NULL;
|
|
|
|
struct dhcps_pool *pdhcps_node = NULL;
|
|
|
|
|
|
|
|
if (*phead == NULL) {
|
|
|
|
*phead = pinsert;
|
|
|
|
} else {
|
|
|
|
plist = *phead;
|
|
|
|
pdhcps_node = pinsert->pnode;
|
|
|
|
pdhcps_pool = plist->pnode;
|
|
|
|
|
|
|
|
if (pdhcps_node->ip.addr < pdhcps_pool->ip.addr) {
|
|
|
|
pinsert->pnext = plist;
|
|
|
|
*phead = pinsert;
|
|
|
|
} else {
|
2016-08-17 11:08:22 -04:00
|
|
|
while (plist->pnext != NULL) {
|
|
|
|
pdhcps_pool = plist->pnext->pnode;
|
2016-09-01 07:05:50 -04:00
|
|
|
|
2016-08-17 11:08:22 -04:00
|
|
|
if (pdhcps_node->ip.addr < pdhcps_pool->ip.addr) {
|
|
|
|
pinsert->pnext = plist->pnext;
|
|
|
|
plist->pnext = pinsert;
|
|
|
|
break;
|
|
|
|
}
|
2016-09-01 07:05:50 -04:00
|
|
|
|
2016-08-17 11:08:22 -04:00
|
|
|
plist = plist->pnext;
|
|
|
|
}
|
|
|
|
|
2016-09-01 07:05:50 -04:00
|
|
|
if (plist->pnext == NULL) {
|
2016-08-17 11:08:22 -04:00
|
|
|
plist->pnext = pinsert;
|
|
|
|
}
|
2016-09-01 07:05:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-17 11:08:22 -04:00
|
|
|
// pinsert->pnext = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* FunctionName : node_delete_from_list
|
|
|
|
* Description : remove the node from list
|
2016-08-26 05:46:55 -04:00
|
|
|
* Parameters : phead -- the head node of the list
|
|
|
|
* pdelete -- the remove node of the list
|
2016-08-17 11:08:22 -04:00
|
|
|
* Returns : none
|
|
|
|
*******************************************************************************/
|
2016-09-01 07:05:50 -04:00
|
|
|
void node_remove_from_list(list_node **phead, list_node *pdelete)
|
2016-08-17 11:08:22 -04:00
|
|
|
{
|
2016-09-01 07:05:50 -04:00
|
|
|
list_node *plist = NULL;
|
|
|
|
|
|
|
|
plist = *phead;
|
|
|
|
|
|
|
|
if (plist == NULL) {
|
|
|
|
*phead = NULL;
|
|
|
|
} else {
|
|
|
|
if (plist == pdelete) {
|
2020-01-15 10:09:18 -05:00
|
|
|
// Note: Ignoring the "use after free" warnings, as it could only happen
|
|
|
|
// if the linked list contains loops
|
|
|
|
*phead = plist->pnext; // NOLINT(clang-analyzer-unix.Malloc)
|
2016-09-01 07:05:50 -04:00
|
|
|
pdelete->pnext = NULL;
|
|
|
|
} else {
|
|
|
|
while (plist != NULL) {
|
2020-01-15 10:09:18 -05:00
|
|
|
if (plist->pnext == pdelete) { // NOLINT(clang-analyzer-unix.Malloc)
|
2016-09-01 07:05:50 -04:00
|
|
|
plist->pnext = pdelete->pnext;
|
|
|
|
pdelete->pnext = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
plist = plist->pnext;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-17 11:08:22 -04:00
|
|
|
}
|
2016-08-26 05:46:55 -04:00
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* FunctionName : add_msg_type
|
|
|
|
* Description : add TYPE option of DHCP message
|
|
|
|
* Parameters : optptr -- the addr of DHCP message option
|
|
|
|
* Returns : the addr of DHCP message option
|
|
|
|
*******************************************************************************/
|
2016-09-01 07:05:50 -04:00
|
|
|
static u8_t *add_msg_type(u8_t *optptr, u8_t type)
|
2016-08-17 11:08:22 -04:00
|
|
|
{
|
2016-09-01 07:05:50 -04:00
|
|
|
*optptr++ = DHCP_OPTION_MSG_TYPE;
|
|
|
|
*optptr++ = 1;
|
|
|
|
*optptr++ = type;
|
|
|
|
return optptr;
|
2016-08-17 11:08:22 -04:00
|
|
|
}
|
2016-08-26 05:46:55 -04:00
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* FunctionName : add_offer_options
|
|
|
|
* Description : add OFFER option of DHCP message
|
|
|
|
* Parameters : optptr -- the addr of DHCP message option
|
|
|
|
* Returns : the addr of DHCP message option
|
|
|
|
*******************************************************************************/
|
2022-01-19 13:50:49 -05:00
|
|
|
static u8_t *add_offer_options(dhcps_t *dhcps, u8_t *optptr)
|
2016-08-17 11:08:22 -04:00
|
|
|
{
|
2016-09-01 07:05:50 -04:00
|
|
|
ip4_addr_t ipadd;
|
2016-08-17 11:08:22 -04:00
|
|
|
|
2022-01-19 13:50:49 -05:00
|
|
|
ipadd.addr = *((u32_t *) &dhcps->server_address);
|
2016-08-17 11:08:22 -04:00
|
|
|
|
2016-09-01 07:05:50 -04:00
|
|
|
*optptr++ = DHCP_OPTION_SUBNET_MASK;
|
|
|
|
*optptr++ = 4;
|
2022-01-19 13:50:49 -05:00
|
|
|
*optptr++ = ip4_addr1(&dhcps->dhcps_mask);
|
|
|
|
*optptr++ = ip4_addr2(&dhcps->dhcps_mask);
|
|
|
|
*optptr++ = ip4_addr3(&dhcps->dhcps_mask);
|
|
|
|
*optptr++ = ip4_addr4(&dhcps->dhcps_mask);
|
2016-08-17 11:08:22 -04:00
|
|
|
|
2016-09-01 07:05:50 -04:00
|
|
|
*optptr++ = DHCP_OPTION_LEASE_TIME;
|
|
|
|
*optptr++ = 4;
|
2022-01-19 13:50:49 -05:00
|
|
|
*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;
|
2016-09-01 07:05:50 -04:00
|
|
|
|
|
|
|
*optptr++ = DHCP_OPTION_SERVER_ID;
|
|
|
|
*optptr++ = 4;
|
|
|
|
*optptr++ = ip4_addr1(&ipadd);
|
|
|
|
*optptr++ = ip4_addr2(&ipadd);
|
|
|
|
*optptr++ = ip4_addr3(&ipadd);
|
|
|
|
*optptr++ = ip4_addr4(&ipadd);
|
|
|
|
|
2022-01-19 13:50:49 -05:00
|
|
|
if (dhcps_router_enabled(dhcps->dhcps_offer)) {
|
2022-01-19 11:15:40 -05:00
|
|
|
ip_info_t if_ip = { 0 };
|
2022-01-19 13:50:49 -05:00
|
|
|
get_ip_info(dhcps->dhcps_netif, &if_ip);
|
2016-09-01 07:05:50 -04:00
|
|
|
|
2019-06-28 10:47:34 -04:00
|
|
|
ip4_addr_t* gw_ip = (ip4_addr_t*)&if_ip.gw;
|
2016-09-01 07:05:50 -04:00
|
|
|
|
2019-06-28 10:47:34 -04:00
|
|
|
if (!ip4_addr_isany_val(*gw_ip)) {
|
2016-12-14 14:30:40 -05:00
|
|
|
*optptr++ = DHCP_OPTION_ROUTER;
|
|
|
|
*optptr++ = 4;
|
2019-06-28 10:47:34 -04:00
|
|
|
*optptr++ = ip4_addr1(gw_ip);
|
|
|
|
*optptr++ = ip4_addr2(gw_ip);
|
|
|
|
*optptr++ = ip4_addr3(gw_ip);
|
|
|
|
*optptr++ = ip4_addr4(gw_ip);
|
2016-12-14 14:30:40 -05:00
|
|
|
}
|
2016-09-01 07:05:50 -04:00
|
|
|
}
|
2016-08-17 11:08:22 -04:00
|
|
|
|
2016-09-01 07:05:50 -04:00
|
|
|
*optptr++ = DHCP_OPTION_DNS_SERVER;
|
|
|
|
*optptr++ = 4;
|
2022-01-19 13:50:49 -05:00
|
|
|
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);
|
2017-06-07 09:22:53 -04:00
|
|
|
}else {
|
|
|
|
*optptr++ = ip4_addr1(&ipadd);
|
|
|
|
*optptr++ = ip4_addr2(&ipadd);
|
|
|
|
*optptr++ = ip4_addr3(&ipadd);
|
|
|
|
*optptr++ = ip4_addr4(&ipadd);
|
|
|
|
}
|
2016-08-17 11:08:22 -04:00
|
|
|
|
2022-01-19 13:50:49 -05:00
|
|
|
ip4_addr_t broadcast_addr = { .addr = (ipadd.addr & dhcps->dhcps_mask.addr) | ~dhcps->dhcps_mask.addr };
|
2016-09-01 07:05:50 -04:00
|
|
|
*optptr++ = DHCP_OPTION_BROADCAST_ADDRESS;
|
|
|
|
*optptr++ = 4;
|
2020-04-02 08:38:09 -04:00
|
|
|
*optptr++ = ip4_addr1(&broadcast_addr);
|
|
|
|
*optptr++ = ip4_addr2(&broadcast_addr);
|
|
|
|
*optptr++ = ip4_addr3(&broadcast_addr);
|
|
|
|
*optptr++ = ip4_addr4(&broadcast_addr);
|
2016-08-17 11:08:22 -04:00
|
|
|
|
2016-09-01 07:05:50 -04:00
|
|
|
*optptr++ = DHCP_OPTION_INTERFACE_MTU;
|
|
|
|
*optptr++ = 2;
|
|
|
|
*optptr++ = 0x05;
|
|
|
|
*optptr++ = 0xdc;
|
2016-08-17 11:08:22 -04:00
|
|
|
|
2016-09-01 07:05:50 -04:00
|
|
|
*optptr++ = DHCP_OPTION_PERFORM_ROUTER_DISCOVERY;
|
|
|
|
*optptr++ = 1;
|
|
|
|
*optptr++ = 0x00;
|
2016-08-17 11:08:22 -04:00
|
|
|
|
2016-09-01 07:05:50 -04:00
|
|
|
*optptr++ = 43;
|
|
|
|
*optptr++ = 6;
|
2016-08-17 11:08:22 -04:00
|
|
|
|
2016-09-01 07:05:50 -04:00
|
|
|
*optptr++ = 0x01;
|
|
|
|
*optptr++ = 4;
|
|
|
|
*optptr++ = 0x00;
|
|
|
|
*optptr++ = 0x00;
|
|
|
|
*optptr++ = 0x00;
|
|
|
|
*optptr++ = 0x02;
|
2016-08-17 11:08:22 -04:00
|
|
|
|
2016-09-01 07:05:50 -04:00
|
|
|
return optptr;
|
2016-08-17 11:08:22 -04:00
|
|
|
}
|
2016-08-26 05:46:55 -04:00
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* FunctionName : add_end
|
|
|
|
* Description : add end option of DHCP message
|
|
|
|
* Parameters : optptr -- the addr of DHCP message option
|
|
|
|
* Returns : the addr of DHCP message option
|
|
|
|
*******************************************************************************/
|
2016-09-01 07:05:50 -04:00
|
|
|
static u8_t *add_end(u8_t *optptr)
|
2016-08-17 11:08:22 -04:00
|
|
|
{
|
2016-09-01 07:05:50 -04:00
|
|
|
*optptr++ = DHCP_OPTION_END;
|
|
|
|
return optptr;
|
2016-08-17 11:08:22 -04:00
|
|
|
}
|
2016-08-26 05:46:55 -04:00
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* FunctionName : create_msg
|
|
|
|
* Description : create response message
|
|
|
|
* Parameters : m -- DHCP message info
|
|
|
|
* Returns : none
|
|
|
|
*******************************************************************************/
|
2022-01-19 13:50:49 -05:00
|
|
|
static void create_msg(dhcps_t *dhcps, struct dhcps_msg *m)
|
2016-08-17 11:08:22 -04:00
|
|
|
{
|
2016-09-01 07:05:50 -04:00
|
|
|
ip4_addr_t client;
|
2016-08-17 11:08:22 -04:00
|
|
|
|
|
|
|
|
2022-01-19 13:50:49 -05:00
|
|
|
client.addr = *((uint32_t *) &dhcps->client_address);
|
2016-08-17 11:08:22 -04:00
|
|
|
|
2016-09-01 07:05:50 -04:00
|
|
|
m->op = DHCP_REPLY;
|
|
|
|
m->htype = DHCP_HTYPE_ETHERNET;
|
|
|
|
m->hlen = 6;
|
2016-08-17 11:08:22 -04:00
|
|
|
|
2022-10-20 05:28:28 -04:00
|
|
|
#if !ETHARP_SUPPORT_STATIC_ENTRIES
|
|
|
|
/* If the DHCP server does not support sending unicast message to the client,
|
|
|
|
* need to set the 'flags' field to broadcast */
|
2016-09-01 07:05:50 -04:00
|
|
|
m->flags = htons(BOOTP_BROADCAST);
|
2022-10-20 05:28:28 -04:00
|
|
|
#endif
|
2016-09-01 07:05:50 -04:00
|
|
|
|
|
|
|
memcpy((char *) m->yiaddr, (char *) &client.addr, sizeof(m->yiaddr));
|
2022-10-20 05:28:28 -04:00
|
|
|
memcpy((char *) m->options, &magic_cookie, sizeof(magic_cookie));
|
|
|
|
}
|
2016-08-17 11:08:22 -04:00
|
|
|
|
2022-10-20 05:28:28 -04:00
|
|
|
/******************************************************************************
|
|
|
|
* FunctionName : dhcps_response_ip_set
|
|
|
|
* Description : set the ip address for sending to the DHCP client
|
|
|
|
* Parameters : m -- DHCP message info
|
|
|
|
* ip4_out -- ip address for sending
|
|
|
|
* Returns : none
|
|
|
|
*******************************************************************************/
|
|
|
|
static void dhcps_response_ip_set(dhcps_t *dhcps, struct dhcps_msg *m, ip4_addr_t *ip4_out)
|
|
|
|
{
|
|
|
|
#if ETHARP_SUPPORT_STATIC_ENTRIES
|
|
|
|
ip4_addr_t ip4_giaddr;
|
|
|
|
ip4_addr_t ip4_ciaddr;
|
|
|
|
ip4_addr_t ip4_yiaddr;
|
|
|
|
|
|
|
|
struct eth_addr chaddr;
|
|
|
|
memcpy(chaddr.addr, m->chaddr, sizeof(chaddr.addr));
|
|
|
|
memcpy((char *)&ip4_giaddr.addr, (char *)m->giaddr, sizeof(m->giaddr));
|
|
|
|
memcpy((char *)&ip4_ciaddr.addr, (char *)m->ciaddr, sizeof(m->ciaddr));
|
|
|
|
memcpy((char *)&ip4_yiaddr.addr, (char *)m->yiaddr, sizeof(m->yiaddr));
|
|
|
|
|
|
|
|
if (!ip4_addr_isany_val(ip4_giaddr)) {
|
|
|
|
/* If the 'giaddr' field is non-zero, send return message to the address in 'giaddr'. (RFC 2131)*/
|
|
|
|
ip4_addr_set(ip4_out, &ip4_giaddr);
|
|
|
|
/* add the IP<->MAC as static entry into the arp table. */
|
|
|
|
etharp_add_static_entry(&ip4_giaddr, &chaddr);
|
|
|
|
} else {
|
|
|
|
if (!ip4_addr_isany_val(ip4_ciaddr)) {
|
|
|
|
/* If the 'giaddr' field is zero and the 'ciaddr' is nonzero,
|
|
|
|
* the server unicasts DHCPOFFER and DHCPACK message to the address in 'ciaddr'*/
|
|
|
|
ip4_addr_set(ip4_out, &ip4_ciaddr);
|
|
|
|
etharp_add_static_entry(&ip4_ciaddr, &chaddr);
|
|
|
|
} else if (!BROADCAST_BIT_IS_SET(htons(m->flags))) {
|
|
|
|
/* If the 'giaddr' is zero and 'ciaddr' is zero, and the broadcast bit is not set,
|
|
|
|
* the server unicasts DHCPOFFER and DHCPACK message to the client's hardware address and
|
|
|
|
* 'yiaddr' address. */
|
|
|
|
ip4_addr_set(ip4_out, &ip4_yiaddr);
|
|
|
|
etharp_add_static_entry(&ip4_yiaddr, &chaddr);
|
|
|
|
} else {
|
|
|
|
/* The server broadcast DHCPOFFER and DHCPACK message to 0xffffffff*/
|
|
|
|
ip4_addr_set(ip4_out, &dhcps->broadcast_dhcps);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
ip4_addr_set(ip4_out, &dhcps->broadcast_dhcps);
|
|
|
|
#endif
|
2016-08-17 11:08:22 -04:00
|
|
|
}
|
2016-08-26 05:46:55 -04:00
|
|
|
|
2022-10-20 05:28:28 -04:00
|
|
|
|
2017-05-31 02:48:21 -04:00
|
|
|
struct pbuf * dhcps_pbuf_alloc(u16_t len)
|
|
|
|
{
|
|
|
|
u16_t mlen = sizeof(struct dhcps_msg);
|
|
|
|
|
|
|
|
if (len > mlen) {
|
|
|
|
#if DHCPS_DEBUG
|
|
|
|
DHCPS_LOG("dhcps: len=%d mlen=%d", len, mlen);
|
|
|
|
#endif
|
|
|
|
mlen = len;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pbuf_alloc(PBUF_TRANSPORT, mlen, PBUF_RAM);
|
|
|
|
}
|
|
|
|
|
2016-08-26 05:46:55 -04:00
|
|
|
/******************************************************************************
|
|
|
|
* FunctionName : send_offer
|
|
|
|
* Description : DHCP message OFFER Response
|
|
|
|
* Parameters : m -- DHCP message info
|
|
|
|
* Returns : none
|
|
|
|
*******************************************************************************/
|
2022-01-19 13:50:49 -05:00
|
|
|
static void send_offer(dhcps_t *dhcps, struct dhcps_msg *m, u16_t len)
|
2016-08-17 11:08:22 -04:00
|
|
|
{
|
2016-09-01 07:05:50 -04:00
|
|
|
u8_t *end;
|
|
|
|
struct pbuf *p, *q;
|
|
|
|
u8_t *data;
|
|
|
|
u16_t cnt = 0;
|
|
|
|
u16_t i;
|
2019-03-10 23:03:48 -04:00
|
|
|
#if DHCPS_DEBUG
|
2016-09-01 07:05:50 -04:00
|
|
|
err_t SendOffer_err_t;
|
2019-03-10 23:03:48 -04:00
|
|
|
#endif
|
2022-01-19 13:50:49 -05:00
|
|
|
create_msg(dhcps, m);
|
2016-09-01 07:05:50 -04:00
|
|
|
|
|
|
|
end = add_msg_type(&m->options[4], DHCPOFFER);
|
2022-01-19 13:50:49 -05:00
|
|
|
end = add_offer_options(dhcps, end);
|
2023-05-10 07:52:40 -04:00
|
|
|
LWIP_HOOK_DHCPS_POST_APPEND_OPTS(dhcps->dhcps_netif, dhcps, DHCPOFFER, &end)
|
2016-09-01 07:05:50 -04:00
|
|
|
end = add_end(end);
|
|
|
|
|
2017-05-31 02:48:21 -04:00
|
|
|
p = dhcps_pbuf_alloc(len);
|
2016-08-17 11:08:22 -04:00
|
|
|
#if DHCPS_DEBUG
|
2016-09-01 07:05:50 -04:00
|
|
|
DHCPS_LOG("udhcp: send_offer>>p->ref = %d\n", p->ref);
|
2016-08-17 11:08:22 -04:00
|
|
|
#endif
|
2016-09-01 07:05:50 -04:00
|
|
|
|
|
|
|
if (p != NULL) {
|
|
|
|
|
2016-08-17 11:08:22 -04:00
|
|
|
#if DHCPS_DEBUG
|
2016-09-01 07:05:50 -04:00
|
|
|
DHCPS_LOG("dhcps: send_offer>>pbuf_alloc succeed\n");
|
|
|
|
DHCPS_LOG("dhcps: send_offer>>p->tot_len = %d\n", p->tot_len);
|
|
|
|
DHCPS_LOG("dhcps: send_offer>>p->len = %d\n", p->len);
|
2016-08-17 11:08:22 -04:00
|
|
|
#endif
|
2016-09-01 07:05:50 -04:00
|
|
|
q = p;
|
|
|
|
|
|
|
|
while (q != NULL) {
|
|
|
|
data = (u8_t *)q->payload;
|
|
|
|
|
|
|
|
for (i = 0; i < q->len; i++) {
|
|
|
|
data[i] = ((u8_t *) m)[cnt++];
|
2016-08-17 11:08:22 -04:00
|
|
|
#if DHCPS_DEBUG
|
2016-09-01 07:05:50 -04:00
|
|
|
DHCPS_LOG("%02x ", data[i]);
|
|
|
|
|
|
|
|
if ((i + 1) % 16 == 0) {
|
|
|
|
DHCPS_LOG("\n");
|
|
|
|
}
|
|
|
|
|
2016-08-17 11:08:22 -04:00
|
|
|
#endif
|
2016-09-01 07:05:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
q = q->next;
|
|
|
|
}
|
|
|
|
} else {
|
2016-08-17 11:08:22 -04:00
|
|
|
|
|
|
|
#if DHCPS_DEBUG
|
2016-09-01 07:05:50 -04:00
|
|
|
DHCPS_LOG("dhcps: send_offer>>pbuf_alloc failed\n");
|
2016-08-17 11:08:22 -04:00
|
|
|
#endif
|
2016-09-01 07:05:50 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ip_addr_t ip_temp = IPADDR4_INIT(0x0);
|
2022-10-20 05:28:28 -04:00
|
|
|
dhcps_response_ip_set(dhcps, m, ip_2_ip4(&ip_temp));
|
2019-03-10 23:03:48 -04:00
|
|
|
#if DHCPS_DEBUG
|
2023-05-24 09:56:24 -04:00
|
|
|
SendOffer_err_t = udp_sendto(dhcps->dhcps_pcb, p, &ip_temp, DHCPS_CLIENT_PORT);
|
2016-09-01 07:05:50 -04:00
|
|
|
DHCPS_LOG("dhcps: send_offer>>udp_sendto result %x\n", SendOffer_err_t);
|
2019-03-10 23:03:48 -04:00
|
|
|
#else
|
2022-01-19 13:50:49 -05:00
|
|
|
udp_sendto(dhcps->dhcps_pcb, p, &ip_temp, DHCPS_CLIENT_PORT);
|
2019-03-10 23:03:48 -04:00
|
|
|
#endif
|
2016-09-01 07:05:50 -04:00
|
|
|
|
2022-10-20 05:28:28 -04:00
|
|
|
#if ETHARP_SUPPORT_STATIC_ENTRIES
|
|
|
|
/* remove the IP<->MAC from the arp table. */
|
|
|
|
etharp_remove_static_entry(ip_2_ip4(&ip_temp));
|
|
|
|
#endif
|
|
|
|
|
2016-09-01 07:05:50 -04:00
|
|
|
if (p->ref != 0) {
|
2016-08-17 11:08:22 -04:00
|
|
|
#if DHCPS_DEBUG
|
2016-09-01 07:05:50 -04:00
|
|
|
DHCPS_LOG("udhcp: send_offer>>free pbuf\n");
|
2016-08-17 11:08:22 -04:00
|
|
|
#endif
|
2016-09-01 07:05:50 -04:00
|
|
|
pbuf_free(p);
|
|
|
|
}
|
2016-08-17 11:08:22 -04:00
|
|
|
}
|
2016-08-26 05:46:55 -04:00
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* FunctionName : send_nak
|
|
|
|
* Description : DHCP message NACK Response
|
|
|
|
* Parameters : m -- DHCP message info
|
|
|
|
* Returns : none
|
|
|
|
*******************************************************************************/
|
2022-01-19 13:50:49 -05:00
|
|
|
static void send_nak(dhcps_t *dhcps, struct dhcps_msg *m, u16_t len)
|
2016-08-17 11:08:22 -04:00
|
|
|
{
|
2016-09-01 07:05:50 -04:00
|
|
|
u8_t *end;
|
|
|
|
struct pbuf *p, *q;
|
|
|
|
u8_t *data;
|
|
|
|
u16_t cnt = 0;
|
|
|
|
u16_t i;
|
2019-03-13 23:47:21 -04:00
|
|
|
#if DHCPS_DEBUG
|
2016-09-01 07:05:50 -04:00
|
|
|
err_t SendNak_err_t;
|
2019-03-13 23:47:21 -04:00
|
|
|
#endif
|
2022-01-19 13:50:49 -05:00
|
|
|
create_msg(dhcps, m);
|
2016-09-01 07:05:50 -04:00
|
|
|
|
|
|
|
end = add_msg_type(&m->options[4], DHCPNAK);
|
2023-05-10 07:52:40 -04:00
|
|
|
LWIP_HOOK_DHCPS_POST_APPEND_OPTS(dhcps->dhcps_netif, dhcps, DHCPNAK, &end)
|
2016-09-01 07:05:50 -04:00
|
|
|
end = add_end(end);
|
|
|
|
|
2017-05-31 02:48:21 -04:00
|
|
|
p = dhcps_pbuf_alloc(len);
|
2016-08-17 11:08:22 -04:00
|
|
|
#if DHCPS_DEBUG
|
2016-09-01 07:05:50 -04:00
|
|
|
DHCPS_LOG("udhcp: send_nak>>p->ref = %d\n", p->ref);
|
2016-08-17 11:08:22 -04:00
|
|
|
#endif
|
2016-09-01 07:05:50 -04:00
|
|
|
|
|
|
|
if (p != NULL) {
|
|
|
|
|
2016-08-17 11:08:22 -04:00
|
|
|
#if DHCPS_DEBUG
|
2016-09-01 07:05:50 -04:00
|
|
|
DHCPS_LOG("dhcps: send_nak>>pbuf_alloc succeed\n");
|
|
|
|
DHCPS_LOG("dhcps: send_nak>>p->tot_len = %d\n", p->tot_len);
|
|
|
|
DHCPS_LOG("dhcps: send_nak>>p->len = %d\n", p->len);
|
2016-08-17 11:08:22 -04:00
|
|
|
#endif
|
2016-09-01 07:05:50 -04:00
|
|
|
q = p;
|
|
|
|
|
|
|
|
while (q != NULL) {
|
|
|
|
data = (u8_t *)q->payload;
|
|
|
|
|
|
|
|
for (i = 0; i < q->len; i++) {
|
|
|
|
data[i] = ((u8_t *) m)[cnt++];
|
|
|
|
#if DHCPS_DEBUG
|
|
|
|
DHCPS_LOG("%02x ", data[i]);
|
|
|
|
|
|
|
|
if ((i + 1) % 16 == 0) {
|
|
|
|
DHCPS_LOG("\n");
|
|
|
|
}
|
|
|
|
|
2016-08-17 11:08:22 -04:00
|
|
|
#endif
|
2016-09-01 07:05:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
q = q->next;
|
|
|
|
}
|
|
|
|
} else {
|
2016-08-17 11:08:22 -04:00
|
|
|
|
|
|
|
#if DHCPS_DEBUG
|
2016-09-01 07:05:50 -04:00
|
|
|
DHCPS_LOG("dhcps: send_nak>>pbuf_alloc failed\n");
|
2016-08-17 11:08:22 -04:00
|
|
|
#endif
|
2016-09-01 07:05:50 -04:00
|
|
|
return;
|
|
|
|
}
|
2016-08-17 11:08:22 -04:00
|
|
|
|
2016-09-01 07:05:50 -04:00
|
|
|
ip_addr_t ip_temp = IPADDR4_INIT(0x0);
|
2022-10-20 05:28:28 -04:00
|
|
|
|
|
|
|
#if ETHARP_SUPPORT_STATIC_ENTRIES
|
|
|
|
ip4_addr_t ip4_giaddr;
|
|
|
|
struct eth_addr chaddr;
|
|
|
|
memcpy(chaddr.addr, m->chaddr, sizeof(chaddr.addr));
|
|
|
|
memcpy((char *)&ip4_giaddr.addr, (char *)m->giaddr, sizeof(m->giaddr));
|
|
|
|
|
|
|
|
if (!ip4_addr_isany_val(ip4_giaddr)) {
|
|
|
|
ip4_addr_set(ip_2_ip4(&ip_temp), &ip4_giaddr);
|
|
|
|
/* add the IP<->MAC as static entry into the arp table. */
|
|
|
|
etharp_add_static_entry(&ip4_giaddr, &chaddr);
|
|
|
|
} else {
|
|
|
|
/* when 'giaddr' is zero, the server broadcasts any DHCPNAK message to 0xffffffff. (RFC 2131)*/
|
|
|
|
ip4_addr_set(ip_2_ip4(&ip_temp), &dhcps->broadcast_dhcps);
|
|
|
|
}
|
|
|
|
#else
|
2022-01-19 13:50:49 -05:00
|
|
|
ip4_addr_set(ip_2_ip4(&ip_temp), &dhcps->broadcast_dhcps);
|
2022-10-20 05:28:28 -04:00
|
|
|
#endif
|
|
|
|
|
2019-03-13 23:47:21 -04:00
|
|
|
#if DHCPS_DEBUG
|
2023-05-24 09:56:24 -04:00
|
|
|
SendNak_err_t = udp_sendto(dhcps->dhcps_pcb, p, &ip_temp, DHCPS_CLIENT_PORT);
|
2016-09-01 07:05:50 -04:00
|
|
|
DHCPS_LOG("dhcps: send_nak>>udp_sendto result %x\n", SendNak_err_t);
|
2019-03-13 23:47:21 -04:00
|
|
|
#else
|
2022-01-19 13:50:49 -05:00
|
|
|
udp_sendto(dhcps->dhcps_pcb, p, &ip_temp, DHCPS_CLIENT_PORT);
|
2019-03-13 23:47:21 -04:00
|
|
|
#endif
|
2016-09-01 07:05:50 -04:00
|
|
|
|
2022-10-20 05:28:28 -04:00
|
|
|
#if ETHARP_SUPPORT_STATIC_ENTRIES
|
|
|
|
/* remove the IP<->MAC from the arp table. */
|
|
|
|
etharp_remove_static_entry(ip_2_ip4(&ip_temp));
|
|
|
|
#endif
|
|
|
|
|
2016-09-01 07:05:50 -04:00
|
|
|
if (p->ref != 0) {
|
|
|
|
#if DHCPS_DEBUG
|
|
|
|
DHCPS_LOG("udhcp: send_nak>>free pbuf\n");
|
2016-08-17 11:08:22 -04:00
|
|
|
#endif
|
2016-09-01 07:05:50 -04:00
|
|
|
pbuf_free(p);
|
|
|
|
}
|
2016-08-17 11:08:22 -04:00
|
|
|
}
|
2016-08-26 05:46:55 -04:00
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* FunctionName : send_ack
|
|
|
|
* Description : DHCP message ACK Response
|
|
|
|
* Parameters : m -- DHCP message info
|
|
|
|
* Returns : none
|
|
|
|
*******************************************************************************/
|
2022-01-19 13:50:49 -05:00
|
|
|
static void send_ack(dhcps_t *dhcps, struct dhcps_msg *m, u16_t len)
|
2016-08-17 11:08:22 -04:00
|
|
|
{
|
2016-09-01 07:05:50 -04:00
|
|
|
u8_t *end;
|
|
|
|
struct pbuf *p, *q;
|
|
|
|
u8_t *data;
|
|
|
|
u16_t cnt = 0;
|
|
|
|
u16_t i;
|
|
|
|
err_t SendAck_err_t;
|
2022-01-19 13:50:49 -05:00
|
|
|
create_msg(dhcps, m);
|
2016-09-01 07:05:50 -04:00
|
|
|
|
|
|
|
end = add_msg_type(&m->options[4], DHCPACK);
|
2022-01-19 13:50:49 -05:00
|
|
|
end = add_offer_options(dhcps, end);
|
2023-05-10 07:52:40 -04:00
|
|
|
LWIP_HOOK_DHCPS_POST_APPEND_OPTS(dhcps->dhcps_netif, dhcps, DHCPACK, &end)
|
2016-09-01 07:05:50 -04:00
|
|
|
end = add_end(end);
|
|
|
|
|
2017-05-31 02:48:21 -04:00
|
|
|
p = dhcps_pbuf_alloc(len);
|
2016-08-17 11:08:22 -04:00
|
|
|
#if DHCPS_DEBUG
|
2016-09-01 07:05:50 -04:00
|
|
|
DHCPS_LOG("udhcp: send_ack>>p->ref = %d\n", p->ref);
|
2016-08-17 11:08:22 -04:00
|
|
|
#endif
|
2016-09-01 07:05:50 -04:00
|
|
|
|
|
|
|
if (p != NULL) {
|
|
|
|
|
2016-08-17 11:08:22 -04:00
|
|
|
#if DHCPS_DEBUG
|
2016-09-01 07:05:50 -04:00
|
|
|
DHCPS_LOG("dhcps: send_ack>>pbuf_alloc succeed\n");
|
|
|
|
DHCPS_LOG("dhcps: send_ack>>p->tot_len = %d\n", p->tot_len);
|
|
|
|
DHCPS_LOG("dhcps: send_ack>>p->len = %d\n", p->len);
|
2016-08-17 11:08:22 -04:00
|
|
|
#endif
|
2016-09-01 07:05:50 -04:00
|
|
|
q = p;
|
|
|
|
|
|
|
|
while (q != NULL) {
|
|
|
|
data = (u8_t *)q->payload;
|
|
|
|
|
|
|
|
for (i = 0; i < q->len; i++) {
|
|
|
|
data[i] = ((u8_t *) m)[cnt++];
|
|
|
|
#if DHCPS_DEBUG
|
|
|
|
DHCPS_LOG("%02x ", data[i]);
|
|
|
|
|
|
|
|
if ((i + 1) % 16 == 0) {
|
|
|
|
DHCPS_LOG("\n");
|
|
|
|
}
|
|
|
|
|
2016-08-17 11:08:22 -04:00
|
|
|
#endif
|
2016-09-01 07:05:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
q = q->next;
|
|
|
|
}
|
|
|
|
} else {
|
2016-08-17 11:08:22 -04:00
|
|
|
|
|
|
|
#if DHCPS_DEBUG
|
2016-09-01 07:05:50 -04:00
|
|
|
DHCPS_LOG("dhcps: send_ack>>pbuf_alloc failed\n");
|
2016-08-17 11:08:22 -04:00
|
|
|
#endif
|
2016-09-01 07:05:50 -04:00
|
|
|
return;
|
|
|
|
}
|
2016-08-17 11:08:22 -04:00
|
|
|
|
2016-09-01 07:05:50 -04:00
|
|
|
ip_addr_t ip_temp = IPADDR4_INIT(0x0);
|
2022-10-20 05:28:28 -04:00
|
|
|
dhcps_response_ip_set(dhcps, m, ip_2_ip4(&ip_temp));
|
2022-01-19 13:50:49 -05:00
|
|
|
SendAck_err_t = udp_sendto(dhcps->dhcps_pcb, p, &ip_temp, DHCPS_CLIENT_PORT);
|
2016-08-17 11:08:22 -04:00
|
|
|
#if DHCPS_DEBUG
|
2016-09-01 07:05:50 -04:00
|
|
|
DHCPS_LOG("dhcps: send_ack>>udp_sendto result %x\n", SendAck_err_t);
|
2016-08-17 11:08:22 -04:00
|
|
|
#endif
|
2016-09-01 07:05:50 -04:00
|
|
|
|
2022-10-20 05:28:28 -04:00
|
|
|
#if ETHARP_SUPPORT_STATIC_ENTRIES
|
|
|
|
/* remove the IP<->MAC from the arp table. */
|
|
|
|
etharp_remove_static_entry(ip_2_ip4(&ip_temp));
|
|
|
|
#endif
|
|
|
|
|
2018-04-08 23:01:44 -04:00
|
|
|
if (SendAck_err_t == ERR_OK) {
|
2022-02-02 11:17:39 -05:00
|
|
|
dhcps->dhcps_cb(dhcps->dhcps_cb_arg, m->yiaddr, m->chaddr);
|
2018-04-08 23:01:44 -04:00
|
|
|
}
|
|
|
|
|
2016-09-01 07:05:50 -04:00
|
|
|
if (p->ref != 0) {
|
2016-08-17 11:08:22 -04:00
|
|
|
#if DHCPS_DEBUG
|
2016-09-01 07:05:50 -04:00
|
|
|
DHCPS_LOG("udhcp: send_ack>>free pbuf\n");
|
2016-08-17 11:08:22 -04:00
|
|
|
#endif
|
2016-09-01 07:05:50 -04:00
|
|
|
pbuf_free(p);
|
|
|
|
}
|
2016-08-17 11:08:22 -04:00
|
|
|
}
|
2016-08-26 05:46:55 -04:00
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* FunctionName : parse_options
|
|
|
|
* Description : parse DHCP message options
|
|
|
|
* Parameters : optptr -- DHCP message option info
|
|
|
|
* len -- DHCP message option length
|
|
|
|
* Returns : none
|
|
|
|
*******************************************************************************/
|
2022-01-19 13:50:49 -05:00
|
|
|
static u8_t parse_options(dhcps_t *dhcps, u8_t *optptr, s16_t len)
|
2016-08-17 11:08:22 -04:00
|
|
|
{
|
2016-09-01 07:05:50 -04:00
|
|
|
ip4_addr_t client;
|
|
|
|
bool is_dhcp_parse_end = false;
|
|
|
|
struct dhcps_state s;
|
2016-08-17 11:08:22 -04:00
|
|
|
|
2022-01-19 13:50:49 -05:00
|
|
|
client.addr = *((uint32_t *) &dhcps->client_address);
|
2016-08-17 11:08:22 -04:00
|
|
|
|
2016-09-01 07:05:50 -04:00
|
|
|
u8_t *end = optptr + len;
|
|
|
|
u16_t type = 0;
|
2016-08-17 11:08:22 -04:00
|
|
|
|
2016-09-01 07:05:50 -04:00
|
|
|
s.state = DHCPS_STATE_IDLE;
|
2016-08-17 11:08:22 -04:00
|
|
|
|
2016-09-01 07:05:50 -04:00
|
|
|
while (optptr < end) {
|
2016-08-17 11:08:22 -04:00
|
|
|
#if DHCPS_DEBUG
|
2016-09-01 07:05:50 -04:00
|
|
|
DHCPS_LOG("dhcps: (s16_t)*optptr = %d\n", (s16_t)*optptr);
|
2016-08-17 11:08:22 -04:00
|
|
|
#endif
|
|
|
|
|
2016-09-01 07:05:50 -04:00
|
|
|
switch ((s16_t) *optptr) {
|
|
|
|
|
|
|
|
case DHCP_OPTION_MSG_TYPE: //53
|
|
|
|
type = *(optptr + 2);
|
|
|
|
break;
|
2016-08-17 11:08:22 -04:00
|
|
|
|
2016-09-01 07:05:50 -04:00
|
|
|
case DHCP_OPTION_REQ_IPADDR://50
|
|
|
|
if (memcmp((char *) &client.addr, (char *) optptr + 2, 4) == 0) {
|
2016-08-17 11:08:22 -04:00
|
|
|
#if DHCPS_DEBUG
|
2016-09-01 07:05:50 -04:00
|
|
|
DHCPS_LOG("dhcps: DHCP_OPTION_REQ_IPADDR = 0 ok\n");
|
2016-08-17 11:08:22 -04:00
|
|
|
#endif
|
2016-09-01 07:05:50 -04:00
|
|
|
s.state = DHCPS_STATE_ACK;
|
|
|
|
} else {
|
2016-08-17 11:08:22 -04:00
|
|
|
#if DHCPS_DEBUG
|
2016-09-01 07:05:50 -04:00
|
|
|
DHCPS_LOG("dhcps: DHCP_OPTION_REQ_IPADDR != 0 err\n");
|
2016-08-17 11:08:22 -04:00
|
|
|
#endif
|
2016-09-01 07:05:50 -04:00
|
|
|
s.state = DHCPS_STATE_NAK;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2016-08-17 11:08:22 -04:00
|
|
|
|
2016-09-01 07:05:50 -04:00
|
|
|
case DHCP_OPTION_END: {
|
|
|
|
is_dhcp_parse_end = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2016-08-17 11:08:22 -04:00
|
|
|
|
2016-09-01 07:05:50 -04:00
|
|
|
if (is_dhcp_parse_end) {
|
|
|
|
break;
|
2016-08-17 11:08:22 -04:00
|
|
|
}
|
|
|
|
|
2016-09-01 07:05:50 -04:00
|
|
|
optptr += optptr[1] + 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
|
|
|
|
case DHCPDISCOVER://1
|
|
|
|
s.state = DHCPS_STATE_OFFER;
|
2016-08-17 11:08:22 -04:00
|
|
|
#if DHCPS_DEBUG
|
2016-09-01 07:05:50 -04:00
|
|
|
DHCPS_LOG("dhcps: DHCPD_STATE_OFFER\n");
|
2016-08-17 11:08:22 -04:00
|
|
|
#endif
|
2016-09-01 07:05:50 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DHCPREQUEST://3
|
|
|
|
if (!(s.state == DHCPS_STATE_ACK || s.state == DHCPS_STATE_NAK)) {
|
2022-01-19 13:50:49 -05:00
|
|
|
if (dhcps->renew == true) {
|
2016-09-01 07:05:50 -04:00
|
|
|
s.state = DHCPS_STATE_ACK;
|
|
|
|
} else {
|
|
|
|
s.state = DHCPS_STATE_NAK;
|
|
|
|
}
|
2016-08-17 11:08:22 -04:00
|
|
|
|
|
|
|
#if DHCPS_DEBUG
|
2016-09-01 07:05:50 -04:00
|
|
|
DHCPS_LOG("dhcps: DHCPD_STATE_NAK\n");
|
2016-08-17 11:08:22 -04:00
|
|
|
#endif
|
2016-09-01 07:05:50 -04:00
|
|
|
}
|
2016-08-17 11:08:22 -04:00
|
|
|
|
2016-09-01 07:05:50 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DHCPDECLINE://4
|
|
|
|
s.state = DHCPS_STATE_IDLE;
|
2016-08-17 11:08:22 -04:00
|
|
|
#if DHCPS_DEBUG
|
2016-09-01 07:05:50 -04:00
|
|
|
DHCPS_LOG("dhcps: DHCPD_STATE_IDLE\n");
|
2016-08-17 11:08:22 -04:00
|
|
|
#endif
|
2016-09-01 07:05:50 -04:00
|
|
|
break;
|
2016-08-17 11:08:22 -04:00
|
|
|
|
2016-09-01 07:05:50 -04:00
|
|
|
case DHCPRELEASE://7
|
|
|
|
s.state = DHCPS_STATE_RELEASE;
|
2016-08-17 11:08:22 -04:00
|
|
|
#if DHCPS_DEBUG
|
2016-09-01 07:05:50 -04:00
|
|
|
DHCPS_LOG("dhcps: DHCPD_STATE_IDLE\n");
|
2016-08-17 11:08:22 -04:00
|
|
|
#endif
|
2016-09-01 07:05:50 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-08-17 11:08:22 -04:00
|
|
|
#if DHCPS_DEBUG
|
2016-09-01 07:05:50 -04:00
|
|
|
DHCPS_LOG("dhcps: return s.state = %d\n", s.state);
|
2016-08-17 11:08:22 -04:00
|
|
|
#endif
|
2016-09-01 07:05:50 -04:00
|
|
|
return s.state;
|
2016-08-17 11:08:22 -04:00
|
|
|
}
|
2016-08-26 05:46:55 -04:00
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* FunctionName : parse_msg
|
|
|
|
* Description : parse DHCP message from netif
|
|
|
|
* Parameters : m -- DHCP message info
|
|
|
|
* len -- DHCP message length
|
|
|
|
* Returns : DHCP message type
|
|
|
|
*******************************************************************************/
|
2022-01-19 13:50:49 -05:00
|
|
|
static s16_t parse_msg(dhcps_t *dhcps, struct dhcps_msg *m, u16_t len)
|
2016-08-17 11:08:22 -04:00
|
|
|
{
|
2022-01-19 13:50:49 -05:00
|
|
|
u32_t lease_timer = (dhcps->dhcps_lease_time * DHCPS_LEASE_UNIT)/DHCPS_COARSE_TIMER_SECS;
|
2017-10-31 14:13:04 -04:00
|
|
|
|
2016-09-01 07:05:50 -04:00
|
|
|
if (memcmp((char *)m->options, &magic_cookie, sizeof(magic_cookie)) == 0) {
|
2016-08-17 11:08:22 -04:00
|
|
|
#if DHCPS_DEBUG
|
2016-09-01 07:05:50 -04:00
|
|
|
DHCPS_LOG("dhcps: len = %d\n", len);
|
2016-08-17 11:08:22 -04:00
|
|
|
#endif
|
2016-09-01 07:05:50 -04:00
|
|
|
ip4_addr_t addr_tmp;
|
|
|
|
|
|
|
|
struct dhcps_pool *pdhcps_pool = NULL;
|
|
|
|
list_node *pnode = NULL;
|
|
|
|
list_node *pback_node = NULL;
|
|
|
|
ip4_addr_t first_address;
|
|
|
|
bool flag = false;
|
|
|
|
|
2022-01-19 13:50:49 -05:00
|
|
|
first_address.addr = dhcps->dhcps_poll.start_ip.addr;
|
|
|
|
dhcps->client_address.addr = dhcps->client_address_plus.addr;
|
|
|
|
dhcps->renew = false;
|
2016-09-01 07:05:50 -04:00
|
|
|
|
2022-01-19 13:50:49 -05:00
|
|
|
if (dhcps->plist != NULL) {
|
|
|
|
for (pback_node = dhcps->plist; pback_node != NULL; pback_node = pback_node->pnext) {
|
2016-09-01 07:05:50 -04:00
|
|
|
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) {
|
2022-01-19 13:50:49 -05:00
|
|
|
dhcps->renew = true;
|
2016-09-01 07:05:50 -04:00
|
|
|
}
|
|
|
|
|
2022-01-19 13:50:49 -05:00
|
|
|
dhcps->client_address.addr = pdhcps_pool->ip.addr;
|
2017-04-19 04:18:07 -04:00
|
|
|
pdhcps_pool->lease_timer = lease_timer;
|
2016-09-01 07:05:50 -04:00
|
|
|
pnode = pback_node;
|
|
|
|
goto POOL_CHECK;
|
2022-01-19 13:50:49 -05:00
|
|
|
} else if (pdhcps_pool->ip.addr == dhcps->client_address_plus.addr) {
|
|
|
|
addr_tmp.addr = htonl(dhcps->client_address_plus.addr);
|
2016-09-01 07:05:50 -04:00
|
|
|
addr_tmp.addr++;
|
2022-01-19 13:50:49 -05:00
|
|
|
dhcps->client_address_plus.addr = htonl(addr_tmp.addr);
|
|
|
|
dhcps->client_address.addr = dhcps->client_address_plus.addr;
|
2016-09-01 07:05:50 -04:00
|
|
|
}
|
|
|
|
|
2024-05-21 05:02:55 -04:00
|
|
|
if (flag == false) { // search the first unused ip
|
2016-09-01 07:05:50 -04:00
|
|
|
if (first_address.addr < pdhcps_pool->ip.addr) {
|
|
|
|
flag = true;
|
|
|
|
} else {
|
|
|
|
addr_tmp.addr = htonl(first_address.addr);
|
|
|
|
addr_tmp.addr++;
|
|
|
|
first_address.addr = htonl(addr_tmp.addr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2022-01-19 13:50:49 -05:00
|
|
|
dhcps->client_address.addr = dhcps->dhcps_poll.start_ip.addr;
|
2016-09-01 07:05:50 -04:00
|
|
|
}
|
|
|
|
|
2022-01-19 13:50:49 -05:00
|
|
|
if (dhcps->client_address_plus.addr > dhcps->dhcps_poll.end_ip.addr) {
|
|
|
|
dhcps->client_address.addr = first_address.addr;
|
2016-09-01 07:05:50 -04:00
|
|
|
}
|
|
|
|
|
2022-01-19 13:50:49 -05:00
|
|
|
if (dhcps->client_address.addr > dhcps->dhcps_poll.end_ip.addr) {
|
|
|
|
dhcps->client_address_plus.addr = dhcps->dhcps_poll.start_ip.addr;
|
2016-09-01 07:05:50 -04:00
|
|
|
pdhcps_pool = NULL;
|
|
|
|
pnode = NULL;
|
|
|
|
} else {
|
2022-03-07 04:41:28 -05:00
|
|
|
pdhcps_pool = (struct dhcps_pool *)mem_calloc(1, sizeof(struct dhcps_pool));
|
2016-09-01 07:05:50 -04:00
|
|
|
|
2022-01-19 13:50:49 -05:00
|
|
|
pdhcps_pool->ip.addr = dhcps->client_address.addr;
|
2016-09-01 07:05:50 -04:00
|
|
|
memcpy(pdhcps_pool->mac, m->chaddr, sizeof(pdhcps_pool->mac));
|
2017-04-19 04:18:07 -04:00
|
|
|
pdhcps_pool->lease_timer = lease_timer;
|
2022-03-07 04:41:28 -05:00
|
|
|
pnode = (list_node *)mem_calloc(1, sizeof(list_node));
|
2016-09-01 07:05:50 -04:00
|
|
|
|
|
|
|
pnode->pnode = pdhcps_pool;
|
|
|
|
pnode->pnext = NULL;
|
2022-01-19 13:50:49 -05:00
|
|
|
node_insert_to_list(&dhcps->plist, pnode);
|
2016-09-01 07:05:50 -04:00
|
|
|
|
2022-01-19 13:50:49 -05:00
|
|
|
if (dhcps->client_address.addr == dhcps->dhcps_poll.end_ip.addr) {
|
|
|
|
dhcps->client_address_plus.addr = dhcps->dhcps_poll.start_ip.addr;
|
2016-09-01 07:05:50 -04:00
|
|
|
} else {
|
2022-01-19 13:50:49 -05:00
|
|
|
addr_tmp.addr = htonl(dhcps->client_address.addr);
|
2016-09-01 07:05:50 -04:00
|
|
|
addr_tmp.addr++;
|
2022-01-19 13:50:49 -05:00
|
|
|
dhcps->client_address_plus.addr = htonl(addr_tmp.addr);
|
2016-09-01 07:05:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
POOL_CHECK:
|
|
|
|
|
2022-01-19 13:50:49 -05:00
|
|
|
if ((dhcps->client_address.addr > dhcps->dhcps_poll.end_ip.addr) || (ip4_addr_isany(&dhcps->client_address))) {
|
2016-09-01 07:05:50 -04:00
|
|
|
if (pnode != NULL) {
|
2022-01-19 13:50:49 -05:00
|
|
|
node_remove_from_list(&dhcps->plist, pnode);
|
2016-09-01 07:05:50 -04:00
|
|
|
free(pnode);
|
|
|
|
pnode = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pdhcps_pool != NULL) {
|
|
|
|
free(pdhcps_pool);
|
|
|
|
pdhcps_pool = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
|
2022-01-19 13:50:49 -05:00
|
|
|
s16_t ret = parse_options(dhcps, &m->options[4], len);;
|
2016-09-01 07:05:50 -04:00
|
|
|
|
2021-01-22 03:29:21 -05:00
|
|
|
if (ret == DHCPS_STATE_RELEASE || ret == DHCPS_STATE_NAK) {
|
2016-09-01 07:05:50 -04:00
|
|
|
if (pnode != NULL) {
|
2022-01-19 13:50:49 -05:00
|
|
|
node_remove_from_list(&dhcps->plist, pnode);
|
2016-09-01 07:05:50 -04:00
|
|
|
free(pnode);
|
|
|
|
pnode = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pdhcps_pool != NULL) {
|
|
|
|
free(pdhcps_pool);
|
|
|
|
pdhcps_pool = NULL;
|
|
|
|
}
|
|
|
|
|
2022-01-19 13:50:49 -05:00
|
|
|
memset(&dhcps->client_address, 0x0, sizeof(dhcps->client_address));
|
2016-09-01 07:05:50 -04:00
|
|
|
}
|
2016-08-17 11:08:22 -04:00
|
|
|
|
|
|
|
#if DHCPS_DEBUG
|
2016-09-01 07:05:50 -04:00
|
|
|
DHCPS_LOG("dhcps: xid changed\n");
|
2023-05-24 09:56:24 -04:00
|
|
|
DHCPS_LOG("dhcps: client_address.addr = %x\n", dhcps->client_address.addr);
|
2016-08-17 11:08:22 -04:00
|
|
|
#endif
|
2016-09-01 07:05:50 -04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2016-08-17 11:08:22 -04:00
|
|
|
}
|
|
|
|
|
2016-08-26 05:46:55 -04:00
|
|
|
/******************************************************************************
|
|
|
|
* FunctionName : handle_dhcp
|
|
|
|
* Description : If an incoming DHCP message is in response to us, then trigger the state machine
|
|
|
|
* Parameters : arg -- arg user supplied argument (udp_pcb.recv_arg)
|
|
|
|
* pcb -- the udp_pcb which received data
|
|
|
|
* p -- the packet buffer that was received
|
|
|
|
* addr -- the remote IP address from which the packet was received
|
|
|
|
* port -- the remote port from which the packet was received
|
|
|
|
* Returns : none
|
|
|
|
*******************************************************************************/
|
2016-08-17 11:08:22 -04:00
|
|
|
static void handle_dhcp(void *arg,
|
2016-09-01 07:05:50 -04:00
|
|
|
struct udp_pcb *pcb,
|
|
|
|
struct pbuf *p,
|
|
|
|
const ip_addr_t *addr,
|
|
|
|
u16_t port)
|
2016-08-17 11:08:22 -04:00
|
|
|
{
|
2022-01-19 13:50:49 -05:00
|
|
|
struct dhcps_t *dhcps = arg;
|
2016-09-01 07:05:50 -04:00
|
|
|
struct dhcps_msg *pmsg_dhcps = NULL;
|
2017-05-31 02:48:21 -04:00
|
|
|
s16_t tlen, malloc_len;
|
2016-09-01 07:05:50 -04:00
|
|
|
u16_t i;
|
|
|
|
u16_t dhcps_msg_cnt = 0;
|
|
|
|
u8_t *p_dhcps_msg = NULL;
|
|
|
|
u8_t *data;
|
2022-01-04 10:44:26 -05:00
|
|
|
s16_t state;
|
2016-08-17 11:08:22 -04:00
|
|
|
|
|
|
|
#if DHCPS_DEBUG
|
2016-09-01 07:05:50 -04:00
|
|
|
DHCPS_LOG("dhcps: handle_dhcp-> receive a packet\n");
|
2016-08-17 11:08:22 -04:00
|
|
|
#endif
|
2016-09-01 07:05:50 -04:00
|
|
|
|
|
|
|
if (p == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-31 02:48:21 -04:00
|
|
|
malloc_len = sizeof(struct dhcps_msg);
|
|
|
|
#if DHCPS_DEBUG
|
|
|
|
DHCPS_LOG("dhcps: handle_dhcp malloc_len=%d rx_len=%d", malloc_len, p->tot_len);
|
|
|
|
#endif
|
|
|
|
if (malloc_len < p->tot_len) {
|
|
|
|
malloc_len = p->tot_len;
|
|
|
|
}
|
2016-09-01 07:05:50 -04:00
|
|
|
|
2022-03-07 04:41:28 -05:00
|
|
|
pmsg_dhcps = (struct dhcps_msg *)mem_calloc(1, malloc_len);
|
2016-09-01 07:05:50 -04:00
|
|
|
if (NULL == pmsg_dhcps) {
|
|
|
|
pbuf_free(p);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
p_dhcps_msg = (u8_t *)pmsg_dhcps;
|
|
|
|
tlen = p->tot_len;
|
|
|
|
data = p->payload;
|
2016-08-17 11:08:22 -04:00
|
|
|
|
|
|
|
#if DHCPS_DEBUG
|
2016-09-01 07:05:50 -04:00
|
|
|
DHCPS_LOG("dhcps: handle_dhcp-> p->tot_len = %d\n", tlen);
|
|
|
|
DHCPS_LOG("dhcps: handle_dhcp-> p->len = %d\n", p->len);
|
2016-08-17 11:08:22 -04:00
|
|
|
#endif
|
2016-09-01 07:05:50 -04:00
|
|
|
|
|
|
|
for (i = 0; i < p->len; i++) {
|
|
|
|
p_dhcps_msg[dhcps_msg_cnt++] = data[i];
|
2016-08-17 11:08:22 -04:00
|
|
|
#if DHCPS_DEBUG
|
2016-09-01 07:05:50 -04:00
|
|
|
DHCPS_LOG("%02x ", data[i]);
|
|
|
|
|
|
|
|
if ((i + 1) % 16 == 0) {
|
|
|
|
DHCPS_LOG("\n");
|
|
|
|
}
|
|
|
|
|
2016-08-17 11:08:22 -04:00
|
|
|
#endif
|
2016-09-01 07:05:50 -04:00
|
|
|
}
|
2016-08-17 11:08:22 -04:00
|
|
|
|
2016-09-01 07:05:50 -04:00
|
|
|
if (p->next != NULL) {
|
2016-08-17 11:08:22 -04:00
|
|
|
#if DHCPS_DEBUG
|
2016-09-01 07:05:50 -04:00
|
|
|
DHCPS_LOG("dhcps: handle_dhcp-> p->next != NULL\n");
|
|
|
|
DHCPS_LOG("dhcps: handle_dhcp-> p->next->tot_len = %d\n", p->next->tot_len);
|
|
|
|
DHCPS_LOG("dhcps: handle_dhcp-> p->next->len = %d\n", p->next->len);
|
2016-08-17 11:08:22 -04:00
|
|
|
#endif
|
2016-09-01 07:05:50 -04:00
|
|
|
|
|
|
|
data = p->next->payload;
|
|
|
|
|
|
|
|
for (i = 0; i < p->next->len; i++) {
|
|
|
|
p_dhcps_msg[dhcps_msg_cnt++] = data[i];
|
2016-08-17 11:08:22 -04:00
|
|
|
#if DHCPS_DEBUG
|
2016-09-01 07:05:50 -04:00
|
|
|
DHCPS_LOG("%02x ", data[i]);
|
|
|
|
|
|
|
|
if ((i + 1) % 16 == 0) {
|
|
|
|
DHCPS_LOG("\n");
|
|
|
|
}
|
|
|
|
|
2016-08-17 11:08:22 -04:00
|
|
|
#endif
|
|
|
|
}
|
2016-09-01 07:05:50 -04:00
|
|
|
}
|
|
|
|
|
2016-08-17 11:08:22 -04:00
|
|
|
#if DHCPS_DEBUG
|
2016-09-01 07:05:50 -04:00
|
|
|
DHCPS_LOG("dhcps: handle_dhcp-> parse_msg(p)\n");
|
2016-08-17 11:08:22 -04:00
|
|
|
#endif
|
2016-09-01 07:05:50 -04:00
|
|
|
|
2022-01-19 13:50:49 -05:00
|
|
|
state = parse_msg(dhcps, pmsg_dhcps, tlen - 240);
|
2022-01-04 10:44:26 -05:00
|
|
|
#ifdef LWIP_HOOK_DHCPS_POST_STATE
|
|
|
|
state = LWIP_HOOK_DHCPS_POST_STATE(pmsg_dhcps, malloc_len, state);
|
|
|
|
#endif /* LWIP_HOOK_DHCPS_POST_STATE */
|
|
|
|
|
|
|
|
switch (state) {
|
2016-09-01 07:05:50 -04:00
|
|
|
case DHCPS_STATE_OFFER://1
|
|
|
|
#if DHCPS_DEBUG
|
|
|
|
DHCPS_LOG("dhcps: handle_dhcp-> DHCPD_STATE_OFFER\n");
|
|
|
|
#endif
|
2022-01-19 13:50:49 -05:00
|
|
|
send_offer(dhcps, pmsg_dhcps, malloc_len);
|
2016-09-01 07:05:50 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DHCPS_STATE_ACK://3
|
|
|
|
#if DHCPS_DEBUG
|
|
|
|
DHCPS_LOG("dhcps: handle_dhcp-> DHCPD_STATE_ACK\n");
|
|
|
|
#endif
|
2022-01-19 13:50:49 -05:00
|
|
|
send_ack(dhcps, pmsg_dhcps, malloc_len);
|
2016-09-01 07:05:50 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DHCPS_STATE_NAK://4
|
|
|
|
#if DHCPS_DEBUG
|
|
|
|
DHCPS_LOG("dhcps: handle_dhcp-> DHCPD_STATE_NAK\n");
|
|
|
|
#endif
|
2022-01-19 13:50:49 -05:00
|
|
|
send_nak(dhcps, pmsg_dhcps, malloc_len);
|
2016-09-01 07:05:50 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default :
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if DHCPS_DEBUG
|
|
|
|
DHCPS_LOG("dhcps: handle_dhcp-> pbuf_free(p)\n");
|
|
|
|
#endif
|
|
|
|
pbuf_free(p);
|
|
|
|
free(pmsg_dhcps);
|
|
|
|
pmsg_dhcps = NULL;
|
2016-08-17 11:08:22 -04:00
|
|
|
}
|
2016-08-26 05:46:55 -04:00
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* FunctionName : dhcps_poll_set
|
|
|
|
* Description : set ip poll from start to end for station
|
|
|
|
* Parameters : ip -- The current ip addr
|
|
|
|
* Returns : none
|
|
|
|
*******************************************************************************/
|
2022-01-19 13:50:49 -05:00
|
|
|
static void dhcps_poll_set(dhcps_t *dhcps, u32_t ip)
|
2016-08-17 11:08:22 -04:00
|
|
|
{
|
2023-07-14 08:07:39 -04:00
|
|
|
u32_t server_ip = 0;
|
2016-09-01 07:05:50 -04:00
|
|
|
u32_t start_ip = 0;
|
|
|
|
u32_t end_ip = 0;
|
2023-07-14 08:07:39 -04:00
|
|
|
u32_t range_start_ip = 0;
|
|
|
|
u32_t range_end_ip = 0;
|
2022-01-19 13:50:49 -05:00
|
|
|
dhcps_lease_t *dhcps_poll = &dhcps->dhcps_poll;
|
|
|
|
if (dhcps_poll->enable == true) {
|
2022-10-27 06:25:00 -04:00
|
|
|
server_ip = htonl(ip);
|
2022-01-19 13:50:49 -05:00
|
|
|
start_ip = htonl(dhcps_poll->start_ip.addr);
|
|
|
|
end_ip = htonl(dhcps_poll->end_ip.addr);
|
2016-09-01 07:05:50 -04:00
|
|
|
|
|
|
|
/*config ip information can't contain local ip*/
|
2023-08-29 04:01:40 -04:00
|
|
|
if ((server_ip >= start_ip) && (server_ip <= end_ip)) {
|
2022-01-19 13:50:49 -05:00
|
|
|
dhcps_poll->enable = false;
|
2016-09-01 07:05:50 -04:00
|
|
|
} else {
|
|
|
|
/*config ip information must be in the same segment as the local ip*/
|
|
|
|
|
2023-07-14 08:07:39 -04:00
|
|
|
if (!ip4_addr_netcmp(&dhcps_poll->start_ip, &dhcps->server_address, &dhcps->dhcps_mask)
|
|
|
|
|| !ip4_addr_netcmp(&dhcps_poll->end_ip, &dhcps->server_address, &dhcps->dhcps_mask)
|
|
|
|
|| (end_ip - start_ip + 1 > DHCPS_MAX_LEASE)) {
|
2022-01-19 13:50:49 -05:00
|
|
|
dhcps_poll->enable = false;
|
2016-09-01 07:05:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-19 13:50:49 -05:00
|
|
|
if (dhcps_poll->enable == false) {
|
2023-07-14 08:07:39 -04:00
|
|
|
server_ip = htonl(ip);
|
|
|
|
range_start_ip = server_ip & htonl(dhcps->dhcps_mask.addr);
|
|
|
|
range_end_ip = range_start_ip | ~htonl(dhcps->dhcps_mask.addr);
|
2016-09-01 07:05:50 -04:00
|
|
|
|
2023-07-14 08:07:39 -04:00
|
|
|
if (server_ip - range_start_ip > range_end_ip - server_ip) {
|
|
|
|
range_start_ip = range_start_ip + 1;
|
|
|
|
range_end_ip = server_ip - 1;
|
2016-09-01 07:05:50 -04:00
|
|
|
} else {
|
2023-07-14 08:07:39 -04:00
|
|
|
range_start_ip = server_ip + 1;
|
|
|
|
range_end_ip = range_end_ip - 1;
|
2016-09-01 07:05:50 -04:00
|
|
|
}
|
2023-07-14 08:07:39 -04:00
|
|
|
if (range_end_ip - range_start_ip + 1 > DHCPS_MAX_LEASE) {
|
|
|
|
range_end_ip = range_start_ip + DHCPS_MAX_LEASE - 1;
|
2022-10-27 06:25:00 -04:00
|
|
|
}
|
2023-07-14 08:07:39 -04:00
|
|
|
bzero(dhcps_poll, sizeof(*dhcps_poll));
|
|
|
|
dhcps_poll->start_ip.addr = range_start_ip;
|
|
|
|
dhcps_poll->end_ip.addr = range_end_ip;
|
2022-01-19 13:50:49 -05:00
|
|
|
dhcps_poll->start_ip.addr = htonl(dhcps_poll->start_ip.addr);
|
|
|
|
dhcps_poll->end_ip.addr = htonl(dhcps_poll->end_ip.addr);
|
2023-08-29 04:01:40 -04:00
|
|
|
dhcps_poll->enable = true;
|
2016-09-01 07:05:50 -04:00
|
|
|
}
|
2016-08-17 11:08:22 -04:00
|
|
|
|
2016-08-26 05:46:55 -04:00
|
|
|
}
|
2016-08-17 11:08:22 -04:00
|
|
|
|
2018-04-08 23:01:44 -04:00
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* FunctionName : dhcps_set_new_lease_cb
|
2020-11-10 02:40:01 -05:00
|
|
|
* Description : set callback for dhcp server when it assign an IP
|
2018-04-08 23:01:44 -04:00
|
|
|
* to the connected dhcp client
|
|
|
|
* Parameters : cb -- callback for dhcp server
|
2022-02-02 09:54:49 -05:00
|
|
|
* Returns : ERR_OK on success
|
2018-04-08 23:01:44 -04:00
|
|
|
*******************************************************************************/
|
2022-02-02 11:17:39 -05:00
|
|
|
err_t dhcps_set_new_lease_cb(dhcps_t *dhcps, dhcps_cb_t cb, void* cb_arg)
|
2018-04-08 23:01:44 -04:00
|
|
|
{
|
2022-02-02 09:54:49 -05:00
|
|
|
if (dhcps == NULL) {
|
|
|
|
return ERR_ARG;
|
|
|
|
}
|
2022-01-19 13:50:49 -05:00
|
|
|
dhcps->dhcps_cb = cb;
|
2022-02-02 11:17:39 -05:00
|
|
|
dhcps->dhcps_cb_arg = cb_arg;
|
2022-02-02 09:54:49 -05:00
|
|
|
return ERR_OK;
|
2018-04-08 23:01:44 -04:00
|
|
|
}
|
|
|
|
|
2016-08-26 05:46:55 -04:00
|
|
|
/******************************************************************************
|
|
|
|
* FunctionName : dhcps_start
|
|
|
|
* Description : start dhcp server function
|
|
|
|
* Parameters : netif -- The current netif addr
|
|
|
|
* : info -- The current ip info
|
|
|
|
* Returns : none
|
|
|
|
*******************************************************************************/
|
2022-02-02 09:54:49 -05:00
|
|
|
err_t dhcps_start(dhcps_t *dhcps, struct netif *netif, ip4_addr_t ip)
|
2016-08-17 11:08:22 -04:00
|
|
|
{
|
2022-02-02 09:54:49 -05:00
|
|
|
if (dhcps == NULL || netif == NULL) {
|
|
|
|
return ERR_ARG;
|
|
|
|
}
|
2022-01-19 13:50:49 -05:00
|
|
|
dhcps->dhcps_netif = netif;
|
|
|
|
if (dhcps->dhcps_pcb != NULL) {
|
|
|
|
udp_remove(dhcps->dhcps_pcb);
|
2016-09-01 07:05:50 -04:00
|
|
|
}
|
|
|
|
|
2022-01-19 13:50:49 -05:00
|
|
|
dhcps->dhcps_pcb = udp_new();
|
2016-09-01 07:05:50 -04:00
|
|
|
|
2022-01-19 13:50:49 -05:00
|
|
|
if (dhcps->dhcps_pcb == NULL || ip4_addr_isany_val(ip)) {
|
2023-07-14 08:07:39 -04:00
|
|
|
DHCPS_LOG("dhcps_start(): could not obtain pcb\n");
|
2022-02-02 09:54:49 -05:00
|
|
|
return ERR_ARG;
|
2016-09-01 07:05:50 -04:00
|
|
|
}
|
|
|
|
|
2022-01-19 13:50:49 -05:00
|
|
|
IP4_ADDR(&dhcps->broadcast_dhcps, 255, 255, 255, 255);
|
2016-09-01 07:05:50 -04:00
|
|
|
|
2022-01-19 13:50:49 -05:00
|
|
|
dhcps->server_address.addr = ip.addr;
|
2023-02-21 21:46:41 -05:00
|
|
|
DHCP_CHECK_SUBNET_MASK_IP(htonl(dhcps->dhcps_mask.addr));
|
2023-07-14 08:07:39 -04:00
|
|
|
DHCP_CHECK_IP_MATCH_SUBNET_MASK(htonl(dhcps->dhcps_mask.addr), htonl(ip.addr));
|
2022-01-19 13:50:49 -05:00
|
|
|
dhcps_poll_set(dhcps, dhcps->server_address.addr);
|
2016-09-01 07:05:50 -04:00
|
|
|
|
2022-01-19 13:50:49 -05:00
|
|
|
dhcps->client_address_plus.addr = dhcps->dhcps_poll.start_ip.addr;
|
2016-09-01 07:05:50 -04:00
|
|
|
|
2022-01-19 13:50:49 -05:00
|
|
|
udp_bind(dhcps->dhcps_pcb, &netif->ip_addr, DHCPS_SERVER_PORT);
|
|
|
|
udp_recv(dhcps->dhcps_pcb, handle_dhcp, dhcps);
|
2016-08-17 11:08:22 -04:00
|
|
|
#if DHCPS_DEBUG
|
2016-09-01 07:05:50 -04:00
|
|
|
DHCPS_LOG("dhcps:dhcps_start->udp_recv function Set a receive callback handle_dhcp for UDP_PCB pcb_dhcps\n");
|
2016-08-17 11:08:22 -04:00
|
|
|
#endif
|
2022-01-24 11:41:06 -05:00
|
|
|
dhcps->state = DHCPS_HANDLE_STARTED;
|
|
|
|
sys_timeout(DHCP_COARSE_TIMER_MSECS, dhcps_tmr, dhcps);
|
2022-02-02 09:54:49 -05:00
|
|
|
return ERR_OK;
|
2016-08-17 11:08:22 -04:00
|
|
|
}
|
|
|
|
|
2016-08-26 05:46:55 -04:00
|
|
|
/******************************************************************************
|
|
|
|
* FunctionName : dhcps_stop
|
|
|
|
* Description : stop dhcp server function
|
|
|
|
* Parameters : netif -- The current netif addr
|
|
|
|
* Returns : none
|
|
|
|
*******************************************************************************/
|
2022-02-02 09:54:49 -05:00
|
|
|
err_t dhcps_stop(dhcps_t *dhcps, struct netif *netif)
|
2016-08-17 11:08:22 -04:00
|
|
|
{
|
2022-02-02 09:54:49 -05:00
|
|
|
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;
|
2016-08-17 11:08:22 -04:00
|
|
|
}
|
2016-09-01 07:05:50 -04:00
|
|
|
|
2022-01-19 13:50:49 -05:00
|
|
|
if (dhcps->dhcps_pcb != NULL) {
|
|
|
|
udp_disconnect(dhcps->dhcps_pcb);
|
|
|
|
udp_remove(dhcps->dhcps_pcb);
|
|
|
|
dhcps->dhcps_pcb = NULL;
|
2016-08-17 11:08:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
list_node *pnode = NULL;
|
|
|
|
list_node *pback_node = NULL;
|
2022-01-19 13:50:49 -05:00
|
|
|
pnode = dhcps->plist;
|
2016-09-01 07:05:50 -04:00
|
|
|
|
2016-08-17 11:08:22 -04:00
|
|
|
while (pnode != NULL) {
|
|
|
|
pback_node = pnode;
|
|
|
|
pnode = pback_node->pnext;
|
2022-01-19 13:50:49 -05:00
|
|
|
node_remove_from_list(&dhcps->plist, pback_node);
|
2016-08-17 11:08:22 -04:00
|
|
|
free(pback_node->pnode);
|
|
|
|
pback_node->pnode = NULL;
|
|
|
|
free(pback_node);
|
|
|
|
pback_node = NULL;
|
|
|
|
}
|
2022-01-24 11:41:06 -05:00
|
|
|
sys_untimeout(dhcps_tmr, dhcps);
|
2022-01-19 13:50:49 -05:00
|
|
|
dhcps->state = DHCPS_HANDLE_STOPPED;
|
2022-02-02 09:54:49 -05:00
|
|
|
return ERR_OK;
|
2016-08-17 11:08:22 -04:00
|
|
|
}
|
|
|
|
|
2016-08-26 05:46:55 -04:00
|
|
|
/******************************************************************************
|
|
|
|
* FunctionName : kill_oldest_dhcps_pool
|
|
|
|
* Description : remove the oldest node from list
|
|
|
|
* Parameters : none
|
|
|
|
* Returns : none
|
|
|
|
*******************************************************************************/
|
2022-01-19 13:50:49 -05:00
|
|
|
static void kill_oldest_dhcps_pool(dhcps_t *dhcps)
|
2016-08-17 11:08:22 -04:00
|
|
|
{
|
2016-09-01 07:05:50 -04:00
|
|
|
list_node *pre = NULL, *p = NULL;
|
|
|
|
list_node *minpre = NULL, *minp = NULL;
|
|
|
|
struct dhcps_pool *pdhcps_pool = NULL, *pmin_pool = NULL;
|
2022-01-19 13:50:49 -05:00
|
|
|
pre = dhcps->plist;
|
2020-01-15 10:09:18 -05:00
|
|
|
assert(pre != NULL && pre->pnext != NULL); // Expect the list to have at least 2 nodes
|
2016-09-01 07:05:50 -04:00
|
|
|
p = pre->pnext;
|
|
|
|
minpre = pre;
|
2024-05-21 05:02:55 -04:00
|
|
|
minp = pre;
|
2016-09-01 07:05:50 -04:00
|
|
|
|
|
|
|
while (p != NULL) {
|
|
|
|
pdhcps_pool = p->pnode;
|
|
|
|
pmin_pool = minp->pnode;
|
|
|
|
|
|
|
|
if (pdhcps_pool->lease_timer < pmin_pool->lease_timer) {
|
|
|
|
minp = p;
|
|
|
|
minpre = pre;
|
|
|
|
}
|
|
|
|
|
|
|
|
pre = p;
|
|
|
|
p = p->pnext;
|
|
|
|
}
|
2024-05-21 05:02:55 -04:00
|
|
|
if (minp == dhcps->plist) {
|
|
|
|
dhcps->plist = minp->pnext;
|
|
|
|
} else {
|
|
|
|
minpre->pnext = minp->pnext;
|
|
|
|
}
|
2016-09-01 07:05:50 -04:00
|
|
|
free(minp->pnode);
|
|
|
|
minp->pnode = NULL;
|
|
|
|
free(minp);
|
|
|
|
minp = NULL;
|
2016-08-17 11:08:22 -04:00
|
|
|
}
|
|
|
|
|
2016-08-26 05:46:55 -04:00
|
|
|
/******************************************************************************
|
|
|
|
* FunctionName : dhcps_coarse_tmr
|
|
|
|
* Description : the lease time count
|
|
|
|
* Parameters : none
|
|
|
|
* Returns : none
|
|
|
|
*******************************************************************************/
|
2022-01-19 13:50:49 -05:00
|
|
|
static void dhcps_tmr(void *arg)
|
2016-08-17 11:08:22 -04:00
|
|
|
{
|
2022-01-19 13:50:49 -05:00
|
|
|
dhcps_t *dhcps = arg;
|
|
|
|
dhcps_handle_state state = dhcps->state;
|
|
|
|
if (state == DHCPS_HANDLE_DELETE_PENDING) {
|
|
|
|
free(dhcps);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state != DHCPS_HANDLE_STARTED) {
|
|
|
|
return;
|
|
|
|
}
|
2022-01-24 11:41:06 -05:00
|
|
|
sys_timeout(DHCP_COARSE_TIMER_MSECS, dhcps_tmr, dhcps);
|
2016-09-01 07:05:50 -04:00
|
|
|
u8_t num_dhcps_pool = 0;
|
|
|
|
list_node *pback_node = NULL;
|
|
|
|
list_node *pnode = NULL;
|
|
|
|
struct dhcps_pool *pdhcps_pool = NULL;
|
2022-01-19 13:50:49 -05:00
|
|
|
pnode = dhcps->plist;
|
2016-09-01 07:05:50 -04:00
|
|
|
|
|
|
|
while (pnode != NULL) {
|
|
|
|
pdhcps_pool = pnode->pnode;
|
|
|
|
pdhcps_pool->lease_timer --;
|
|
|
|
|
|
|
|
if (pdhcps_pool->lease_timer == 0) {
|
|
|
|
pback_node = pnode;
|
|
|
|
pnode = pback_node->pnext;
|
2022-01-19 13:50:49 -05:00
|
|
|
node_remove_from_list(&dhcps->plist, pback_node);
|
2016-09-01 07:05:50 -04:00
|
|
|
free(pback_node->pnode);
|
|
|
|
pback_node->pnode = NULL;
|
|
|
|
free(pback_node);
|
|
|
|
pback_node = NULL;
|
|
|
|
} else {
|
|
|
|
pnode = pnode ->pnext;
|
|
|
|
num_dhcps_pool ++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-24 07:51:52 -04:00
|
|
|
if (num_dhcps_pool > MAX_STATION_NUM) {
|
2022-01-19 13:50:49 -05:00
|
|
|
kill_oldest_dhcps_pool(dhcps);
|
2016-09-01 07:05:50 -04:00
|
|
|
}
|
2016-08-17 11:08:22 -04:00
|
|
|
}
|
|
|
|
|
2016-08-26 05:46:55 -04:00
|
|
|
/******************************************************************************
|
|
|
|
* FunctionName : dhcp_search_ip_on_mac
|
|
|
|
* Description : Search ip address based on mac address
|
|
|
|
* Parameters : mac -- The MAC addr
|
|
|
|
* ip -- The IP info
|
|
|
|
* Returns : true or false
|
|
|
|
*******************************************************************************/
|
2022-01-19 13:50:49 -05:00
|
|
|
bool dhcp_search_ip_on_mac(dhcps_t *dhcps, u8_t *mac, ip4_addr_t *ip)
|
2016-08-22 23:22:58 -04:00
|
|
|
{
|
|
|
|
struct dhcps_pool *pdhcps_pool = NULL;
|
|
|
|
list_node *pback_node = NULL;
|
|
|
|
bool ret = false;
|
2016-09-01 07:05:50 -04:00
|
|
|
|
2022-02-02 09:54:49 -05:00
|
|
|
if (dhcps == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-01-19 13:50:49 -05:00
|
|
|
for (pback_node = dhcps->plist; pback_node != NULL; pback_node = pback_node->pnext) {
|
2016-08-22 23:22:58 -04:00
|
|
|
pdhcps_pool = pback_node->pnode;
|
2016-09-01 07:05:50 -04:00
|
|
|
|
|
|
|
if (memcmp(pdhcps_pool->mac, mac, sizeof(pdhcps_pool->mac)) == 0) {
|
2016-08-22 23:22:58 -04:00
|
|
|
memcpy(&ip->addr, &pdhcps_pool->ip.addr, sizeof(pdhcps_pool->ip.addr));
|
|
|
|
ret = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-09-01 07:05:50 -04:00
|
|
|
|
2016-08-22 23:22:58 -04:00
|
|
|
return ret;
|
|
|
|
}
|
2017-06-07 09:22:53 -04:00
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* FunctionName : dhcps_dns_setserver
|
|
|
|
* Description : set DNS server address for dhcpserver
|
|
|
|
* Parameters : dnsserver -- The DNS server address
|
2022-02-02 09:54:49 -05:00
|
|
|
* Returns : ERR_ARG if invalid handle, ERR_OK on success
|
2017-06-07 09:22:53 -04:00
|
|
|
*******************************************************************************/
|
2022-02-02 09:54:49 -05:00
|
|
|
err_t dhcps_dns_setserver(dhcps_t *dhcps, const ip_addr_t *dnsserver)
|
2017-06-07 09:22:53 -04:00
|
|
|
{
|
2022-02-02 09:54:49 -05:00
|
|
|
if (dhcps == NULL) {
|
|
|
|
return ERR_ARG;
|
|
|
|
}
|
2017-06-07 09:22:53 -04:00
|
|
|
if (dnsserver != NULL) {
|
2022-01-19 13:50:49 -05:00
|
|
|
dhcps->dns_server = *(ip_2_ip4(dnsserver));
|
2017-06-07 09:22:53 -04:00
|
|
|
} else {
|
2022-01-19 13:50:49 -05:00
|
|
|
dhcps->dns_server = *(ip_2_ip4(IP_ADDR_ANY));
|
2020-11-10 02:40:01 -05:00
|
|
|
}
|
2022-02-02 09:54:49 -05:00
|
|
|
return ERR_OK;
|
2017-06-07 09:22:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* FunctionName : dhcps_dns_getserver
|
|
|
|
* Description : get DNS server address for dhcpserver
|
|
|
|
* Parameters : none
|
|
|
|
* Returns : ip4_addr_t
|
|
|
|
*******************************************************************************/
|
2022-02-02 09:54:49 -05:00
|
|
|
err_t dhcps_dns_getserver(dhcps_t *dhcps, ip4_addr_t *dnsserver)
|
2017-06-07 09:22:53 -04:00
|
|
|
{
|
2022-02-02 09:54:49 -05:00
|
|
|
if (dhcps) {
|
|
|
|
*dnsserver = dhcps->dns_server;
|
|
|
|
return ERR_OK;
|
|
|
|
}
|
|
|
|
return ERR_ARG;
|
2017-06-07 09:22:53 -04:00
|
|
|
}
|
2022-03-25 13:58:44 -04:00
|
|
|
#endif // ESP_DHCPS
|