2021-09-12 02:47:14 -04:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2016-08-17 11:08:22 -04:00
|
|
|
#ifndef __DHCPS_H__
|
|
|
|
#define __DHCPS_H__
|
|
|
|
|
2017-10-31 14:13:04 -04:00
|
|
|
#include "sdkconfig.h"
|
2016-08-17 11:08:22 -04:00
|
|
|
#include "lwip/ip_addr.h"
|
2022-02-02 09:54:49 -05:00
|
|
|
#include "lwip/err.h"
|
2016-08-17 11:08:22 -04:00
|
|
|
|
2021-08-31 04:26:55 -04:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2016-08-17 11:08:22 -04:00
|
|
|
typedef struct dhcps_state{
|
|
|
|
s16_t state;
|
|
|
|
} dhcps_state;
|
|
|
|
|
|
|
|
typedef struct dhcps_msg {
|
|
|
|
u8_t op, htype, hlen, hops;
|
|
|
|
u8_t xid[4];
|
|
|
|
u16_t secs, flags;
|
|
|
|
u8_t ciaddr[4];
|
|
|
|
u8_t yiaddr[4];
|
|
|
|
u8_t siaddr[4];
|
|
|
|
u8_t giaddr[4];
|
|
|
|
u8_t chaddr[16];
|
|
|
|
u8_t sname[64];
|
|
|
|
u8_t file[128];
|
|
|
|
u8_t options[312];
|
|
|
|
}dhcps_msg;
|
|
|
|
|
2016-09-06 01:02:02 -04:00
|
|
|
/* Defined in esp_misc.h */
|
|
|
|
typedef struct {
|
|
|
|
bool enable;
|
|
|
|
ip4_addr_t start_ip;
|
|
|
|
ip4_addr_t end_ip;
|
|
|
|
} dhcps_lease_t;
|
2016-08-17 11:08:22 -04:00
|
|
|
|
2016-08-26 02:15:14 -04:00
|
|
|
enum dhcps_offer_option{
|
|
|
|
OFFER_START = 0x00,
|
|
|
|
OFFER_ROUTER = 0x01,
|
2017-06-07 09:22:53 -04:00
|
|
|
OFFER_DNS = 0x02,
|
2016-08-26 02:15:14 -04:00
|
|
|
OFFER_END
|
2016-08-17 11:08:22 -04:00
|
|
|
};
|
|
|
|
|
2022-01-04 10:44:26 -05:00
|
|
|
/** @brief DHCP server's description of compile time configuration values in dhcpserver.c
|
|
|
|
*
|
|
|
|
* - DHCPS_DEBUG: Prints very detailed debug messages if set to 1, hardcoded to 0
|
|
|
|
* - USE_CLASS_B_NET: Use class B network mask if enabled, not-defined (could be enabled as CC_FLAGS)
|
|
|
|
* - MAX_STATION_NUM: Maximum number of clients, set to Kconfig value CONFIG_LWIP_DHCPS_MAX_STATION_NUM
|
|
|
|
* - LWIP_HOOK_DHCPS_POST_STATE: Used to inject user code after parsing DHCP message, not defined
|
|
|
|
* - could be enabled in lwipopts.h or via CC_FLAGS
|
|
|
|
* - basic usage of the hook to print hex representation of the entire option field is below:
|
|
|
|
* #define LWIP_HOOK_DHCPS_POST_STATE(msg, len, state) \
|
|
|
|
* ({ s16_t ret = state; if (state == DHCPS_STATE_ACK) { ESP_LOG_BUFFER_HEXDUMP("DHCPS",msg->options, 312, ESP_LOG_INFO);} ret; })
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Definitions related to lease time, units and limits
|
|
|
|
*/
|
2017-04-19 04:18:07 -04:00
|
|
|
#define DHCPS_COARSE_TIMER_SECS 1
|
2016-08-26 02:15:14 -04:00
|
|
|
#define DHCPS_MAX_LEASE 0x64
|
2017-10-31 14:13:04 -04:00
|
|
|
#define DHCPS_LEASE_TIME_DEF (120)
|
|
|
|
#define DHCPS_LEASE_UNIT CONFIG_LWIP_DHCPS_LEASE_UNIT
|
2016-08-17 11:08:22 -04:00
|
|
|
|
2016-08-26 02:15:14 -04:00
|
|
|
struct dhcps_pool{
|
|
|
|
ip4_addr_t ip;
|
|
|
|
u8_t mac[6];
|
|
|
|
u32_t lease_timer;
|
|
|
|
};
|
2016-08-17 11:08:22 -04:00
|
|
|
|
2016-08-26 02:15:14 -04:00
|
|
|
typedef u32_t dhcps_time_t;
|
|
|
|
typedef u8_t dhcps_offer_t;
|
2016-09-04 21:50:53 -04:00
|
|
|
|
2016-09-06 01:02:02 -04:00
|
|
|
typedef struct {
|
2017-06-07 09:22:53 -04:00
|
|
|
dhcps_offer_t dhcps_offer;
|
|
|
|
dhcps_offer_t dhcps_dns;
|
|
|
|
dhcps_time_t dhcps_time;
|
|
|
|
dhcps_lease_t dhcps_poll;
|
2016-09-06 01:02:02 -04:00
|
|
|
} dhcps_options_t;
|
2016-08-17 11:08:22 -04:00
|
|
|
|
2022-02-02 11:17:39 -05:00
|
|
|
typedef void (*dhcps_cb_t)(void* cb_arg, u8_t client_ip[4], u8_t client_mac[6]);
|
2018-04-08 23:01:44 -04:00
|
|
|
|
2020-11-10 02:40:01 -05:00
|
|
|
static inline bool dhcps_router_enabled (dhcps_offer_t offer)
|
2017-06-07 09:22:53 -04:00
|
|
|
{
|
|
|
|
return (offer & OFFER_ROUTER) != 0;
|
|
|
|
}
|
|
|
|
|
2020-11-10 02:40:01 -05:00
|
|
|
static inline bool dhcps_dns_enabled (dhcps_offer_t offer)
|
2017-06-07 09:22:53 -04:00
|
|
|
{
|
|
|
|
return (offer & OFFER_DNS) != 0;
|
|
|
|
}
|
2016-09-06 01:02:02 -04:00
|
|
|
|
2022-01-19 13:50:49 -05:00
|
|
|
typedef struct dhcps_t dhcps_t;
|
|
|
|
|
2022-01-26 07:43:57 -05:00
|
|
|
/**
|
|
|
|
* @brief Creates new DHCP server object
|
|
|
|
*
|
|
|
|
* @return Pointer to the DHCP server handle on success, NULL on error
|
|
|
|
*/
|
2022-01-19 13:50:49 -05:00
|
|
|
dhcps_t *dhcps_new(void);
|
2022-01-26 07:43:57 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Deletes supplied DHPC server object
|
|
|
|
*
|
|
|
|
* @warning This may not delete the handle immediately if the server wasn't
|
|
|
|
* stopped properly, but mark for deleting once the timer callback occurs
|
|
|
|
*
|
|
|
|
* @param dhcps Pointer to the DHCP handle
|
|
|
|
*/
|
2022-01-19 13:50:49 -05:00
|
|
|
void dhcps_delete(dhcps_t *dhcps);
|
|
|
|
|
2022-01-26 07:43:57 -05:00
|
|
|
/**
|
|
|
|
* @brief Starts the DHCP server on the specified network interface
|
|
|
|
*
|
|
|
|
* @param dhcps Pointer to the DHCP handle
|
|
|
|
* @param netif Pointer to the lwIP's network interface struct
|
|
|
|
* @param ip DHCP server's address
|
2022-02-02 09:54:49 -05:00
|
|
|
* @return ERR_ARG if invalid args, ERR_OK on success
|
2022-01-26 07:43:57 -05:00
|
|
|
*/
|
2022-02-02 09:54:49 -05:00
|
|
|
err_t dhcps_start(dhcps_t *dhcps, struct netif *netif, ip4_addr_t ip);
|
2022-01-26 07:43:57 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Stops the DHCP server on the specified netif
|
|
|
|
* @param dhcps Pointer to the DHCP handle
|
|
|
|
* @param netif Pointer to the lwIP's network interface struct
|
2022-02-02 09:54:49 -05:00
|
|
|
* @return ERR_ARG if invalid args, ERR_OK on success
|
2022-01-26 07:43:57 -05:00
|
|
|
*/
|
2022-02-02 09:54:49 -05:00
|
|
|
err_t dhcps_stop(dhcps_t *dhcps, struct netif *netif);
|
2022-01-26 07:43:57 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Gets the DHCP server option info
|
|
|
|
* @param dhcps Pointer to the DHCP handle
|
|
|
|
* @param op_id DHCP message option id
|
|
|
|
* @param opt_len DHCP message option length
|
|
|
|
* @return DHCP message option addr
|
|
|
|
*/
|
2022-01-19 13:50:49 -05:00
|
|
|
void *dhcps_option_info(dhcps_t *dhcps, u8_t op_id, u32_t opt_len);
|
2022-01-26 07:43:57 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Sets the DHCP server option info
|
|
|
|
* @param dhcps Pointer to the DHCP handle
|
|
|
|
* @param op_id DHCP message option id
|
|
|
|
* @param opt_info DHCP message option info
|
|
|
|
* @param opt_len DHCP message option length
|
2022-02-02 09:54:49 -05:00
|
|
|
* @return ERR_ARG if invalid args, ERR_OK on success
|
2022-01-26 07:43:57 -05:00
|
|
|
*/
|
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);
|
2022-01-26 07:43:57 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Tries to find IP address corresponding to the supplied MAC
|
|
|
|
* @param dhcps Pointer to the DHCP handle
|
|
|
|
* @param mac Supplied MAC address
|
|
|
|
* @param ip Pointer to the resultant IP address
|
|
|
|
* @return True if the IP address has been found
|
|
|
|
*/
|
2022-01-19 13:50:49 -05:00
|
|
|
bool dhcp_search_ip_on_mac(dhcps_t *dhcps, u8_t *mac, ip4_addr_t *ip);
|
2022-01-26 07:43:57 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Sets DNS server address for the DHCP server
|
|
|
|
* @param dhcps Pointer to the DHCP handle
|
|
|
|
* @param dnsserver Address of the DNS server
|
2022-02-02 09:54:49 -05:00
|
|
|
* @return ERR_ARG if invalid handle, ERR_OK on success
|
2022-01-26 07:43:57 -05:00
|
|
|
*/
|
2022-02-02 09:54:49 -05:00
|
|
|
err_t dhcps_dns_setserver(dhcps_t *dhcps, const ip_addr_t *dnsserver);
|
2022-01-26 07:43:57 -05:00
|
|
|
|
|
|
|
/**
|
2022-02-02 09:54:49 -05:00
|
|
|
* @brief Gets DNS server associated with this DHCP server
|
2022-01-26 07:43:57 -05:00
|
|
|
* @param dhcps Pointer to the DHCP handle
|
2022-02-02 09:54:49 -05:00
|
|
|
* @param dnsserver Address of the DNS server
|
|
|
|
* @return ERR_ARG if invalid handle, ERR_OK on success
|
2022-01-26 07:43:57 -05:00
|
|
|
*/
|
2022-02-02 09:54:49 -05:00
|
|
|
err_t dhcps_dns_getserver(dhcps_t *dhcps, ip4_addr_t *dnsserver);
|
2022-01-26 07:43:57 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Sets callback on assigning an IP to the connected client
|
|
|
|
* @param dhcps Pointer to the DHCP handle
|
|
|
|
* @param cb Callback for dhcp server
|
2022-02-02 11:17:39 -05:00
|
|
|
* @param cb_arg Context pointer to be added to the callback
|
2022-02-02 09:54:49 -05:00
|
|
|
* @return ERR_ARG if invalid handle, ERR_OK on success
|
2022-01-26 07:43:57 -05: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);
|
2016-08-17 11:08:22 -04:00
|
|
|
|
2021-08-31 04:26:55 -04:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
2016-08-17 11:08:22 -04:00
|
|
|
#endif
|
2021-08-31 04:26:55 -04:00
|
|
|
|
|
|
|
#endif /* __DHCPS_H__ */
|