dhcpserver.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #ifndef __DHCPS_H__
  7. #define __DHCPS_H__
  8. #include "sdkconfig.h"
  9. #include <stdbool.h>
  10. #include "lwip/ip_addr.h"
  11. #include "lwip/err.h"
  12. #ifdef __cplusplus
  13. extern "C" {
  14. #endif
  15. typedef struct dhcps_state{
  16. s16_t state;
  17. } dhcps_state;
  18. typedef struct dhcps_msg {
  19. u8_t op, htype, hlen, hops;
  20. u8_t xid[4];
  21. u16_t secs, flags;
  22. u8_t ciaddr[4];
  23. u8_t yiaddr[4];
  24. u8_t siaddr[4];
  25. u8_t giaddr[4];
  26. u8_t chaddr[16];
  27. u8_t sname[64];
  28. u8_t file[128];
  29. u8_t options[312];
  30. }dhcps_msg;
  31. /* Defined in esp_misc.h */
  32. typedef struct {
  33. bool enable;
  34. ip4_addr_t start_ip;
  35. ip4_addr_t end_ip;
  36. } dhcps_lease_t;
  37. enum dhcps_offer_option{
  38. OFFER_START = 0x00,
  39. OFFER_ROUTER = 0x01,
  40. OFFER_DNS = 0x02,
  41. OFFER_END
  42. };
  43. /** @brief DHCP server's description of compile time configuration values in dhcpserver.c
  44. *
  45. * - DHCPS_DEBUG: Prints very detailed debug messages if set to 1, hardcoded to 0
  46. * - USE_CLASS_B_NET: Use class B network mask if enabled, not-defined (could be enabled as CC_FLAGS)
  47. * - MAX_STATION_NUM: Maximum number of clients, set to Kconfig value CONFIG_LWIP_DHCPS_MAX_STATION_NUM
  48. * - LWIP_HOOK_DHCPS_POST_STATE: Used to inject user code after parsing DHCP message, not defined
  49. * - could be enabled in lwipopts.h or via CC_FLAGS
  50. * - basic usage of the hook to print hex representation of the entire option field is below:
  51. * #define LWIP_HOOK_DHCPS_POST_STATE(msg, len, state) \
  52. * ({ s16_t ret = state; if (state == DHCPS_STATE_ACK) { ESP_LOG_BUFFER_HEXDUMP("DHCPS",msg->options, 312, ESP_LOG_INFO);} ret; })
  53. */
  54. /**
  55. * @brief Definitions related to lease time, units and limits
  56. */
  57. #define DHCPS_COARSE_TIMER_SECS 1
  58. #define DHCPS_MAX_LEASE 0x64
  59. #define DHCPS_LEASE_TIME_DEF (120)
  60. #define DHCPS_LEASE_UNIT CONFIG_LWIP_DHCPS_LEASE_UNIT
  61. struct dhcps_pool{
  62. ip4_addr_t ip;
  63. u8_t mac[6];
  64. u32_t lease_timer;
  65. };
  66. typedef u32_t dhcps_time_t;
  67. typedef u8_t dhcps_offer_t;
  68. typedef struct {
  69. dhcps_offer_t dhcps_offer;
  70. dhcps_offer_t dhcps_dns;
  71. dhcps_time_t dhcps_time;
  72. dhcps_lease_t dhcps_poll;
  73. } dhcps_options_t;
  74. typedef void (*dhcps_cb_t)(void* cb_arg, u8_t client_ip[4], u8_t client_mac[6]);
  75. static inline bool dhcps_router_enabled (dhcps_offer_t offer)
  76. {
  77. return (offer & OFFER_ROUTER) != 0;
  78. }
  79. static inline bool dhcps_dns_enabled (dhcps_offer_t offer)
  80. {
  81. return (offer & OFFER_DNS) != 0;
  82. }
  83. typedef struct dhcps_t dhcps_t;
  84. /**
  85. * @brief Creates new DHCP server object
  86. *
  87. * @return Pointer to the DHCP server handle on success, NULL on error
  88. */
  89. dhcps_t *dhcps_new(void);
  90. /**
  91. * @brief Deletes supplied DHPC server object
  92. *
  93. * @warning This may not delete the handle immediately if the server wasn't
  94. * stopped properly, but mark for deleting once the timer callback occurs
  95. *
  96. * @param dhcps Pointer to the DHCP handle
  97. */
  98. void dhcps_delete(dhcps_t *dhcps);
  99. /**
  100. * @brief Starts the DHCP server on the specified network interface
  101. *
  102. * @param dhcps Pointer to the DHCP handle
  103. * @param netif Pointer to the lwIP's network interface struct
  104. * @param ip DHCP server's address
  105. * @return ERR_ARG if invalid args, ERR_OK on success
  106. */
  107. err_t dhcps_start(dhcps_t *dhcps, struct netif *netif, ip4_addr_t ip);
  108. /**
  109. * @brief Stops the DHCP server on the specified netif
  110. * @param dhcps Pointer to the DHCP handle
  111. * @param netif Pointer to the lwIP's network interface struct
  112. * @return ERR_ARG if invalid args, ERR_OK on success
  113. */
  114. err_t dhcps_stop(dhcps_t *dhcps, struct netif *netif);
  115. /**
  116. * @brief Gets the DHCP server option info
  117. * @param dhcps Pointer to the DHCP handle
  118. * @param op_id DHCP message option id
  119. * @param opt_len DHCP message option length
  120. * @return DHCP message option addr
  121. */
  122. void *dhcps_option_info(dhcps_t *dhcps, u8_t op_id, u32_t opt_len);
  123. /**
  124. * @brief Sets the DHCP server option info
  125. * @param dhcps Pointer to the DHCP handle
  126. * @param op_id DHCP message option id
  127. * @param opt_info DHCP message option info
  128. * @param opt_len DHCP message option length
  129. * @return ERR_ARG if invalid args, ERR_OK on success
  130. */
  131. err_t dhcps_set_option_info(dhcps_t *dhcps, u8_t op_id, void *opt_info, u32_t opt_len);
  132. /**
  133. * @brief Tries to find IP address corresponding to the supplied MAC
  134. * @param dhcps Pointer to the DHCP handle
  135. * @param mac Supplied MAC address
  136. * @param ip Pointer to the resultant IP address
  137. * @return True if the IP address has been found
  138. */
  139. bool dhcp_search_ip_on_mac(dhcps_t *dhcps, u8_t *mac, ip4_addr_t *ip);
  140. /**
  141. * @brief Sets DNS server address for the DHCP server
  142. * @param dhcps Pointer to the DHCP handle
  143. * @param dnsserver Address of the DNS server
  144. * @return ERR_ARG if invalid handle, ERR_OK on success
  145. */
  146. err_t dhcps_dns_setserver(dhcps_t *dhcps, const ip_addr_t *dnsserver);
  147. /**
  148. * @brief Gets DNS server associated with this DHCP server
  149. * @param dhcps Pointer to the DHCP handle
  150. * @param dnsserver Address of the DNS server
  151. * @return ERR_ARG if invalid handle, ERR_OK on success
  152. */
  153. err_t dhcps_dns_getserver(dhcps_t *dhcps, ip4_addr_t *dnsserver);
  154. /**
  155. * @brief Sets callback on assigning an IP to the connected client
  156. * @param dhcps Pointer to the DHCP handle
  157. * @param cb Callback for dhcp server
  158. * @param cb_arg Context pointer to be added to the callback
  159. * @return ERR_ARG if invalid handle, ERR_OK on success
  160. */
  161. err_t dhcps_set_new_lease_cb(dhcps_t *dhcps, dhcps_cb_t cb, void* cb_arg);
  162. #ifdef __cplusplus
  163. }
  164. #endif
  165. #endif /* __DHCPS_H__ */