From b5d13b9837736c44f78991d953ba62cf44f3a54f Mon Sep 17 00:00:00 2001 From: David Cermak Date: Tue, 19 Jul 2022 10:21:44 +0200 Subject: [PATCH] lwip/dhcpserver: Support for adding extra opts This enables users appending an extra, user defined options in dhcp server messages. Example of adding captive_portal option (160) to dhcp offer message is provided: * Add idf-lwip hook file (project makefile): idf_component_get_property(lwip lwip COMPONENT_LIB) target_compile_options(${lwip} PRIVATE "-I${PROJECT_DIR}/main") target_compile_definitions(${lwip} PRIVATE "-DESP_IDF_LWIP_HOOK_FILENAME=\"add_captive_portal.h\"") * Implement appending (add_captive_portal.h): #pragma once #define LWIP_HOOK_DHCPS_POST_APPEND_OPTS(netif, dhcp, state, pp_opts) \ if ((state)==DHCPOFFER) { *(pp_opts) = append_captive_portal_uri(*(pp_opts)); } static inline uint8_t *append_captive_portal_uri(uint8_t *optptr) { const static uint8_t DHCP_OPTION_CAPTIVE_PORTAL=160; const static char CAPTIVE_PORTAL_URI[]="my_uri"; int size = sizeof(CAPTIVE_PORTAL_URI) - 1; *optptr++ = DHCP_OPTION_CAPTIVE_PORTAL; *optptr++ = size; for(int i = 0; i < size; ++i) { *optptr++ = CAPTIVE_PORTAL_URI[i]; } return optptr; } Merges https://github.com/espressif/esp-idf/pull/3308 --- components/lwip/apps/dhcpserver/dhcpserver.c | 11 +++++++++++ docs/en/api-guides/lwip.rst | 12 ++++++++++++ 2 files changed, 23 insertions(+) diff --git a/components/lwip/apps/dhcpserver/dhcpserver.c b/components/lwip/apps/dhcpserver/dhcpserver.c index 289d12495e..4a65f8685a 100644 --- a/components/lwip/apps/dhcpserver/dhcpserver.c +++ b/components/lwip/apps/dhcpserver/dhcpserver.c @@ -19,6 +19,14 @@ #if ESP_DHCPS +#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 + #define BOOTP_BROADCAST 0x8000 #define DHCP_REQUEST 1 @@ -538,6 +546,7 @@ static void send_offer(dhcps_t *dhcps, struct dhcps_msg *m, u16_t len) end = add_msg_type(&m->options[4], DHCPOFFER); end = add_offer_options(dhcps, end); + LWIP_HOOK_DHCPS_POST_APPEND_OPTS(dhcps->netif, dhcps, DHCPOFFER, &end) end = add_end(end); p = dhcps_pbuf_alloc(len); @@ -615,6 +624,7 @@ static void send_nak(dhcps_t *dhcps, struct dhcps_msg *m, u16_t len) create_msg(dhcps, m); end = add_msg_type(&m->options[4], DHCPNAK); + LWIP_HOOK_DHCPS_POST_APPEND_OPTS(dhcps->netif, dhcps, DHCPNAK, &end) end = add_end(end); p = dhcps_pbuf_alloc(len); @@ -691,6 +701,7 @@ static void send_ack(dhcps_t *dhcps, struct dhcps_msg *m, u16_t len) end = add_msg_type(&m->options[4], DHCPACK); end = add_offer_options(dhcps, end); + LWIP_HOOK_DHCPS_POST_APPEND_OPTS(dhcps->netif, dhcps, DHCPACK, &end) end = add_end(end); p = dhcps_pbuf_alloc(len); diff --git a/docs/en/api-guides/lwip.rst b/docs/en/api-guides/lwip.rst index c3c9ba8d14..caf900f188 100644 --- a/docs/en/api-guides/lwip.rst +++ b/docs/en/api-guides/lwip.rst @@ -382,6 +382,18 @@ IP layer features - IPV4 mapped IPV6 addresses are supported. +Customized lwIP hooks ++++++++++++++++++++++ + +The original lwIP supports implementing custom compile-time modifications via ``LWIP_HOOK_FILENAME``. This file is already used by the IDF port layer, but IDF users could still include and implement any custom additions via a header file defined by the macro ``ESP_IDF_LWIP_HOOK_FILENAME``. Here is an exmaple of adding a custom hook file to the build process (the hook is called ``my_hook.h`` and located in the project's ``main`` folder): + +.. code-block:: cmake + + idf_component_get_property(lwip lwip COMPONENT_LIB) + target_compile_options(${lwip} PRIVATE "-I${PROJECT_DIR}/main") + target_compile_definitions(${lwip} PRIVATE "-DESP_IDF_LWIP_HOOK_FILENAME=\"my_hook.h\"") + + Limitations ^^^^^^^^^^^ Calling ``send()`` or ``sendto()`` repeatedly on a UDP socket may eventually fail with ``errno`` equal to ``ENOMEM``. This is a limitation of buffer sizes in the lower layer network interface drivers. If all driver transmit buffers are full then UDP transmission will fail. Applications sending a high volume of UDP datagrams who don't wish for any to be dropped by the sender should check for this error code and re-send the datagram after a short delay.