Skip to content

Commit 747c172

Browse files
committed
Merge branch 'feat/dhcps_support_hostname_per_client' into 'master'
[lwip/dhcps]: Add support for reporting clients hostname Closes IDFGH-9326 See merge request espressif/esp-idf!42094
2 parents bb3055b + 75e0d62 commit 747c172

File tree

6 files changed

+173
-7
lines changed

6 files changed

+173
-7
lines changed

components/esp_netif/include/esp_netif_types.h

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,31 @@ typedef struct {
152152
bool preferred; /*!< The default preference of the address */
153153
} ip_event_add_ip6_t;
154154

155-
/** Event structure for IP_EVENT_ASSIGNED_IP_TO_CLIENT event */
155+
/** Event structure for IP_EVENT_ASSIGNED_IP_TO_CLIENT event
156+
*
157+
* This event is posted when a local DHCP server (e.g., SoftAP) assigns an IPv4
158+
* address to a client. The structure carries the assigned IPv4 address and the
159+
* client's MAC address. If DHCP server support is disabled via Kconfig
160+
* (CONFIG_LWIP_DHCPS=n), the \c ip field is not populated.
161+
*
162+
* If enabled by Kconfig (CONFIG_LWIP_DHCPS_REPORT_CLIENT_HOSTNAME=y), the
163+
* optional DHCP client hostname (option 12) is also included in the \c hostname
164+
* field. The hostname is a null-terminated UTF-8 string and may be empty if the
165+
* client did not provide one. Its maximum length (including the terminator) is
166+
* bounded by CONFIG_LWIP_DHCPS_MAX_HOSTNAME_LEN; longer hostnames are truncated.
167+
* The value is sanitized to contain only letters, digits, dot, dash, and
168+
* underscore. Applications should treat this field as untrusted input.
169+
*/
156170
typedef struct {
157171
esp_netif_t *esp_netif; /*!< Pointer to the associated netif handle */
158172
esp_ip4_addr_t ip; /*!< IP address which was assigned to the station */
159173
uint8_t mac[6]; /*!< MAC address of the connected client */
174+
/* Client hostname as provided via DHCP option 12 (if available). */
175+
#ifndef CONFIG_LWIP_DHCPS_MAX_HOSTNAME_LEN
176+
#define CONFIG_LWIP_DHCPS_MAX_HOSTNAME_LEN 64
177+
#endif
178+
#define ESP_NETIF_HOSTNAME_MAX_LEN CONFIG_LWIP_DHCPS_MAX_HOSTNAME_LEN
179+
char hostname[ESP_NETIF_HOSTNAME_MAX_LEN]; /*!< Optional DHCP client hostname (may be empty string) */
160180
} ip_event_assigned_ip_to_client_t;
161181

162182
/** Compatibility event structure for ip_event_ap_staipassigned_t event

components/esp_netif/lwip/esp_netif_lwip.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,6 +1129,18 @@ static void esp_netif_dhcps_cb(void* arg, uint8_t ip[4], uint8_t mac[6])
11291129
ESP_LOGI(TAG, "DHCP server assigned IP to a client, IP is: " IPSTR, IP2STR(&evt.ip));
11301130
ESP_LOGD(TAG, "Client's MAC: %x:%x:%x:%x:%x:%x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
11311131

1132+
#if CONFIG_LWIP_DHCPS_REPORT_CLIENT_HOSTNAME
1133+
/* Try to fetch hostname for this MAC if available */
1134+
if (esp_netif && esp_netif->dhcps) {
1135+
/* Ensure zero-terminated even if not found */
1136+
if (!dhcps_get_hostname_on_mac(esp_netif->dhcps, mac, evt.hostname, sizeof(evt.hostname))) {
1137+
if (sizeof(evt.hostname) > 0) {
1138+
evt.hostname[0] = '\0';
1139+
}
1140+
}
1141+
}
1142+
#endif
1143+
11321144
int ret = esp_event_post(IP_EVENT, IP_EVENT_ASSIGNED_IP_TO_CLIENT, &evt, sizeof(evt), 0);
11331145
if (ESP_OK != ret) {
11341146
ESP_LOGE(TAG, "dhcps cb: failed to post IP_EVENT_ASSIGNED_IP_TO_CLIENT (%x)", ret);

components/lwip/Kconfig

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,15 @@ menu "LWIP"
405405
Enabling this option allows the device to run the DHCP server
406406
(to dynamically assign IPv4 addresses to clients).
407407

408+
config LWIP_DHCPS_REPORT_CLIENT_HOSTNAME
409+
bool "DHCPS: Report client hostname in assigned-IP event"
410+
default y
411+
depends on LWIP_DHCPS
412+
help
413+
When enabled, the DHCP server parses client hostname (DHCP option 12)
414+
and makes it available via IP_EVENT_ASSIGNED_IP_TO_CLIENT. This adds
415+
a small amount of RAM usage per lease to store the hostname.
416+
408417
config LWIP_DHCPS_LEASE_UNIT
409418
int "Multiplier for lease time, in seconds"
410419
range 1 3600
@@ -425,6 +434,16 @@ menu "LWIP"
425434
After this number is exceeded, DHCP server removes of the oldest device
426435
from it's address pool, without notification.
427436

437+
config LWIP_DHCPS_MAX_HOSTNAME_LEN
438+
int "Maximum client hostname length stored by DHCPS"
439+
range 1 255
440+
default 64
441+
depends on LWIP_DHCPS_REPORT_CLIENT_HOSTNAME
442+
help
443+
Maximum number of bytes stored per-client for DHCP option 12 (hostname),
444+
including the terminating null when used in esp-netif events. Longer hostnames
445+
will be truncated.
446+
428447
config LWIP_DHCPS_STATIC_ENTRIES
429448
bool "Enable ARP static entries"
430449
default y

components/lwip/apps/dhcpserver/dhcpserver.c

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#define DHCPRELEASE 7
5151

5252
#define DHCP_OPTION_SUBNET_MASK 1
53+
#define DHCP_OPTION_HOST_NAME 12
5354
#define DHCP_OPTION_ROUTER 3
5455
#define DHCP_OPTION_DNS_SERVER 6
5556
#define DHCP_OPTION_REQ_IPADDR 50
@@ -142,6 +143,11 @@ struct dhcps_t {
142143
struct udp_pcb *dhcps_pcb;
143144
dhcps_handle_state state;
144145
bool has_declined_ip;
146+
#if CONFIG_LWIP_DHCPS_REPORT_CLIENT_HOSTNAME
147+
/* Temporary storage for option 12 parsed from the current packet */
148+
char opt_hostname[CONFIG_LWIP_DHCPS_MAX_HOSTNAME_LEN];
149+
bool opt_hostname_present;
150+
#endif
145151
};
146152

147153

@@ -923,6 +929,33 @@ static u8_t parse_options(dhcps_t *dhcps, u8_t *optptr, s16_t len)
923929
type = *(optptr + 2);
924930
break;
925931

932+
#if CONFIG_LWIP_DHCPS_REPORT_CLIENT_HOSTNAME
933+
case DHCP_OPTION_HOST_NAME: {
934+
/* option format: code(1) len(1) value(len) */
935+
u8_t olen = *(optptr + 1);
936+
const u8_t *oval = optptr + 2;
937+
if (olen > 0) {
938+
/* clamp to configured max, keep room for NUL */
939+
size_t copy_len = olen;
940+
if (copy_len >= CONFIG_LWIP_DHCPS_MAX_HOSTNAME_LEN) {
941+
copy_len = CONFIG_LWIP_DHCPS_MAX_HOSTNAME_LEN - 1;
942+
}
943+
size_t j = 0;
944+
for (; j < copy_len; ++j) {
945+
char c = (char)oval[j];
946+
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '.' || c == '-' || c == '_') {
947+
dhcps->opt_hostname[j] = c;
948+
} else {
949+
dhcps->opt_hostname[j] = '-';
950+
}
951+
}
952+
dhcps->opt_hostname[j] = '\0';
953+
dhcps->opt_hostname_present = true;
954+
}
955+
break;
956+
}
957+
#endif
958+
926959
case DHCP_OPTION_REQ_IPADDR://50
927960
if (memcmp((char *) &client.addr, (char *) optptr + 2, 4) == 0) {
928961
#if DHCPS_DEBUG
@@ -1111,7 +1144,7 @@ static s16_t parse_msg(dhcps_t *dhcps, struct dhcps_msg *m, u16_t len)
11111144
return 4;
11121145
}
11131146

1114-
s16_t ret = parse_options(dhcps, &m->options[4], len);;
1147+
s16_t ret = parse_options(dhcps, &m->options[4], len);
11151148

11161149
if (ret == DHCPS_STATE_RELEASE || ret == DHCPS_STATE_NAK || ret == DHCPS_STATE_DECLINE) {
11171150
if (pnode != NULL) {
@@ -1131,6 +1164,21 @@ static s16_t parse_msg(dhcps_t *dhcps, struct dhcps_msg *m, u16_t len)
11311164
memset(&dhcps->client_address, 0x0, sizeof(dhcps->client_address));
11321165
}
11331166

1167+
#if CONFIG_LWIP_DHCPS_REPORT_CLIENT_HOSTNAME
1168+
/* If we parsed a hostname from options and we have a lease entry, store it */
1169+
if (pdhcps_pool != NULL) {
1170+
if (dhcps->opt_hostname_present) {
1171+
size_t n = strnlen(dhcps->opt_hostname, CONFIG_LWIP_DHCPS_MAX_HOSTNAME_LEN);
1172+
if (n > 0) {
1173+
memset(pdhcps_pool->hostname, 0, sizeof(pdhcps_pool->hostname));
1174+
memcpy(pdhcps_pool->hostname, dhcps->opt_hostname, n);
1175+
} else {
1176+
pdhcps_pool->hostname[0] = '\0';
1177+
}
1178+
}
1179+
}
1180+
#endif
1181+
11341182
#if DHCPS_DEBUG
11351183
DHCPS_LOG("dhcps: xid changed\n");
11361184
DHCPS_LOG("dhcps: client_address.addr = %x\n", dhcps->client_address.addr);
@@ -1619,4 +1667,40 @@ err_t dhcps_dns_getserver(dhcps_t *dhcps, ip4_addr_t *dnsserver)
16191667
return dhcps_dns_getserver_by_type(dhcps, dnsserver, DNS_TYPE_MAIN);
16201668
}
16211669

1670+
#if CONFIG_LWIP_DHCPS_REPORT_CLIENT_HOSTNAME
1671+
bool dhcps_get_hostname_on_mac(dhcps_t *dhcps, const u8_t *mac, char *out, size_t out_len)
1672+
{
1673+
if ((dhcps == NULL) || (mac == NULL) || (out == NULL) || (out_len == 0)) {
1674+
return false;
1675+
}
1676+
list_node *pnode = dhcps->plist;
1677+
while (pnode) {
1678+
struct dhcps_pool *pool = pnode->pnode;
1679+
if (memcmp(pool->mac, mac, sizeof(pool->mac)) == 0) {
1680+
size_t maxcpy = (out_len > 0) ? out_len - 1 : 0;
1681+
size_t srclen = 0;
1682+
/* hostname may be empty string */
1683+
srclen = strnlen(pool->hostname, CONFIG_LWIP_DHCPS_MAX_HOSTNAME_LEN);
1684+
if (maxcpy > 0) {
1685+
if (srclen > maxcpy) srclen = maxcpy;
1686+
if (srclen) memcpy(out, pool->hostname, srclen);
1687+
out[srclen] = '\0';
1688+
}
1689+
return true;
1690+
}
1691+
pnode = pnode->pnext;
1692+
}
1693+
return false;
1694+
}
1695+
#else
1696+
bool dhcps_get_hostname_on_mac(dhcps_t *dhcps, const u8_t *mac, char *out, size_t out_len)
1697+
{
1698+
LWIP_UNUSED_ARG(dhcps);
1699+
LWIP_UNUSED_ARG(mac);
1700+
if (out && out_len) {
1701+
out[0] = '\0';
1702+
}
1703+
return false;
1704+
}
1705+
#endif
16221706
#endif // ESP_DHCPS

components/lwip/include/apps/dhcpserver/dhcpserver.h

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -8,6 +8,7 @@
88

99
#include "sdkconfig.h"
1010
#include <stdbool.h>
11+
#include <stddef.h>
1112
#include "lwip/ip_addr.h"
1213
#include "lwip/err.h"
1314

@@ -75,9 +76,15 @@ typedef enum
7576
#define DHCPS_LEASE_UNIT CONFIG_LWIP_DHCPS_LEASE_UNIT
7677

7778
struct dhcps_pool{
78-
ip4_addr_t ip;
79-
u8_t mac[6];
80-
u32_t lease_timer;
79+
ip4_addr_t ip;
80+
u8_t mac[6];
81+
u32_t lease_timer;
82+
/* Optional: client's hostname from DHCP option 12, if provided */
83+
#if CONFIG_LWIP_DHCPS
84+
#if CONFIG_LWIP_DHCPS_REPORT_CLIENT_HOSTNAME
85+
char hostname[CONFIG_LWIP_DHCPS_MAX_HOSTNAME_LEN];
86+
#endif
87+
#endif
8188
};
8289

8390
typedef u32_t dhcps_time_t;
@@ -167,6 +174,16 @@ err_t dhcps_set_option_info(dhcps_t *dhcps, u8_t op_id, void *opt_info, u32_t op
167174
*/
168175
bool dhcp_search_ip_on_mac(dhcps_t *dhcps, u8_t *mac, ip4_addr_t *ip);
169176

177+
/**
178+
* @brief Tries to find client hostname corresponding to the supplied MAC
179+
* @param dhcps Pointer to the DHCP handle
180+
* @param mac Supplied MAC address
181+
* @param out Output buffer to receive the hostname (null-terminated)
182+
* @param out_len Size of the output buffer
183+
* @return True if the hostname has been found and copied (may be empty string if not provided by client)
184+
*/
185+
bool dhcps_get_hostname_on_mac(dhcps_t *dhcps, const u8_t *mac, char *out, size_t out_len);
186+
170187
/**
171188
* @brief Sets the DNS server address for the DHCP server
172189
* @param dhcps Pointer to the DHCP handle

examples/wifi/softap_sta/main/softap_sta.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Unlicense OR CC0-1.0
55
*/
@@ -103,6 +103,15 @@ static void wifi_event_handler(void *arg, esp_event_base_t event_base,
103103
ESP_LOGI(TAG_STA, "Got IP:" IPSTR, IP2STR(&event->ip_info.ip));
104104
s_retry_num = 0;
105105
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
106+
} else if (event_base == IP_EVENT && event_id == IP_EVENT_ASSIGNED_IP_TO_CLIENT) {
107+
const ip_event_assigned_ip_to_client_t *e = (const ip_event_assigned_ip_to_client_t *)event_data;
108+
#if CONFIG_LWIP_DHCPS_REPORT_CLIENT_HOSTNAME
109+
ESP_LOGI(TAG_AP, "Assigned IP to client: " IPSTR ", MAC=" MACSTR ", hostname='%s'",
110+
IP2STR(&e->ip), MAC2STR(e->mac), e->hostname);
111+
#else
112+
ESP_LOGI(TAG_AP, "Assigned IP to client: " IPSTR ", MAC=" MACSTR,
113+
IP2STR(&e->ip), MAC2STR(e->mac));
114+
#endif
106115
}
107116
}
108117

@@ -203,6 +212,11 @@ void app_main(void)
203212
&wifi_event_handler,
204213
NULL,
205214
NULL));
215+
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
216+
IP_EVENT_ASSIGNED_IP_TO_CLIENT,
217+
&wifi_event_handler,
218+
NULL,
219+
NULL));
206220

207221
/*Initialize WiFi */
208222
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();

0 commit comments

Comments
 (0)