From 2ec527f426348d22796ad30704d206f81a2c3305 Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Fri, 27 May 2022 11:11:55 +0300 Subject: [PATCH 01/16] works --- cores/esp8266/LwipDhcpServer.cpp | 24 ++++++++++++++++++- cores/esp8266/LwipDhcpServer.h | 16 +++++++++++++ .../examples/CustomOffer/CustomOffer.ino | 16 +++++++++++++ 3 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 libraries/ESP8266WiFi/examples/CustomOffer/CustomOffer.ino diff --git a/cores/esp8266/LwipDhcpServer.cpp b/cores/esp8266/LwipDhcpServer.cpp index 07270bb6c2..6f31c9062a 100644 --- a/cores/esp8266/LwipDhcpServer.cpp +++ b/cores/esp8266/LwipDhcpServer.cpp @@ -534,8 +534,9 @@ void DhcpServer::send_offer(struct dhcps_msg* m) u16_t i; create_msg(m); - end = add_msg_type(&m->options[4], DHCPOFFER); + end = add_msg_type(&m->options[4], DHCPOFFER); // 3 end = add_offer_options(end); + end = add_custom_offer_options(end, std::end(m->options) - 1); end = add_end(end); p = pbuf_alloc(PBUF_TRANSPORT, sizeof(struct dhcps_msg), PBUF_RAM); @@ -658,6 +659,7 @@ void DhcpServer::send_ack(struct dhcps_msg* m) end = add_msg_type(&m->options[4], DHCPACK); end = add_offer_options(end); + end = add_custom_offer_options(end, std::end(m->options) - 1); end = add_end(end); p = pbuf_alloc(PBUF_TRANSPORT, sizeof(struct dhcps_msg), PBUF_RAM); @@ -1591,3 +1593,23 @@ uint32 DhcpServer::dhcps_client_update(u8* bssid, struct ipv4_addr* ip) return pdhcps_pool->ip.addr; } + +uint8_t* DhcpServer::add_custom_offer_options(uint8_t* optptr, uint8_t* end) { + for (const auto& option : custom_options) { + if (option.data.size() > UINT8_MAX) { + break; + } + + if ((end - optptr) < (2 + (option.data.end() - option.data.begin()))) { + break; + } + + *optptr++ = option.code; + *optptr++ = option.data.size(); + for (auto it = option.data.begin(); it != option.data.end(); ++it) { + *optptr++ = *it; + } + } + + return optptr; +} diff --git a/cores/esp8266/LwipDhcpServer.h b/cores/esp8266/LwipDhcpServer.h index d5eb6410ef..bc662c91ee 100644 --- a/cores/esp8266/LwipDhcpServer.h +++ b/cores/esp8266/LwipDhcpServer.h @@ -33,9 +33,17 @@ #include // LWIP_VERSION +#include +#include + class DhcpServer { public: + struct Option { + uint8_t code; + std::vector data; + }; + DhcpServer(netif* netif); ~DhcpServer(); @@ -63,6 +71,12 @@ class DhcpServer void dhcps_set_dns(int num, const ipv4_addr_t* dns); + template + void add_custom_offer_option(T&& option) { + custom_options.push_back(std::forward(option)); + } + + void offers(); protected: // legacy C structure and API to eventually turn into C++ @@ -76,6 +90,7 @@ class DhcpServer void node_remove_from_list(list_node** phead, list_node* pdelete); uint8_t* add_msg_type(uint8_t* optptr, uint8_t type); uint8_t* add_offer_options(uint8_t* optptr); + uint8_t* add_custom_offer_options(uint8_t* optptr, uint8_t* end); uint8_t* add_end(uint8_t* optptr); void create_msg(struct dhcps_msg* m); void send_offer(struct dhcps_msg* m); @@ -105,6 +120,7 @@ class DhcpServer uint8 offer; bool renew; + std::vector