From 6816bcf104d69f9e306b3fca6dd8d0210c8bd172 Mon Sep 17 00:00:00 2001 From: Andrew Cornford Date: Tue, 7 Jul 2026 11:46:30 +0100 Subject: [PATCH] Allow application to set DHCP hostname (option 12) of varying size without modifying library code --- Internet/DHCP/dhcp.c | 10 +++++++++- Internet/DHCP/dhcp.h | 14 +++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/Internet/DHCP/dhcp.c b/Internet/DHCP/dhcp.c index 3925ca8..fcfb1e9 100644 --- a/Internet/DHCP/dhcp.c +++ b/Internet/DHCP/dhcp.c @@ -213,7 +213,7 @@ uint32_t DHCP_XID; // Any number RIP_MSG* pDHCPMSG; // Buffer pointer for DHCP processing -uint8_t HOST_NAME[] = DCHP_HOST_NAME; +uint8_t HOST_NAME[DHCP_HOST_NAME_MAX_LEN + 1] = DHCP_HOST_NAME; uint8_t DHCP_CHADDR[6]; // DHCP Client MAC address. @@ -331,6 +331,14 @@ void reg_dhcp_cbfunc(void(*ip_assign)(void), void(*ip_update)(void), void(*ip_co } } +void DHCP_set_hostname(const char* hostname) { + uint8_t i; + for (i = 0; i < DHCP_HOST_NAME_MAX_LEN && hostname[i] != 0; i++) { + HOST_NAME[i] = (uint8_t)hostname[i]; + } + HOST_NAME[i] = 0; +} + /* make the common DHCP message */ void makeDHCPMSG(void) { uint8_t bk_mac[6]; diff --git a/Internet/DHCP/dhcp.h b/Internet/DHCP/dhcp.h index 7d13a32..d83d79d 100644 --- a/Internet/DHCP/dhcp.h +++ b/Internet/DHCP/dhcp.h @@ -73,7 +73,10 @@ extern "C" { #define MAGIC_COOKIE 0x63825363 ///< You should not modify it number. -#define DCHP_HOST_NAME "WIZnet\0" +#define DHCP_HOST_NAME "WIZnet\0" + +/* Maximum length (excluding null terminator) of a hostname set via @ref DHCP_set_hostname */ +#define DHCP_HOST_NAME_MAX_LEN 32 /* @brief return value of @ref DHCP_run() @@ -108,6 +111,15 @@ void DHCP_time_handler(void); */ void reg_dhcp_cbfunc(void(*ip_assign)(void), void(*ip_update)(void), void(*ip_conflict)(void)); +/* + @brief Set the hostname (DHCP option 12) sent with DISCOVER/REQUEST messages + @param hostname - null-terminated hostname string. Truncated to @ref DHCP_HOST_NAME_MAX_LEN + characters if longer. May be called at any time before/while DHCP is running; + takes effect on the next message sent. + @note If never called, defaults to @ref DHCP_HOST_NAME +*/ +void DHCP_set_hostname(const char* hostname); + /* @brief DHCP client in the main loop @return The value is as the follow \n