Skip to content

Commit 52419ec

Browse files
committed
Merge branch 'feature/add_dhcp_option' into 'release/v3.3'
feat(dhcp): Add DHCP option12, option60 and option61 in DHCP discovery and request state See merge request sdk/ESP8266_RTOS_SDK!1432
2 parents 98f0ad9 + a1a4712 commit 52419ec

File tree

4 files changed

+115
-0
lines changed

4 files changed

+115
-0
lines changed

components/lwip/Kconfig

+6
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,12 @@ config LWIP_DHCP_DISCOVER_RETRANSMISSION_INTERVAL
260260
help
261261
Set time interval of retransmissions of DHCP discover.
262262

263+
config LWIP_ESP_DHCP_OPTION
264+
bool "Enable DHCP option60 and option61 in discovery and request state"
265+
default n
266+
help
267+
Maybe some routers need add those option to get IP address.
268+
Enable this config to add option60 and option61 in discovery and request state
263269
endmenu #DHCP
264270

265271
menuconfig LWIP_AUTOIP

components/lwip/lwip/src/core/ipv4/dhcp.c

+105
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,12 @@ static void dhcp_option_hostname(struct dhcp *dhcp, struct netif *netif);
209209
/* always add the DHCP options trailer to end and pad */
210210
static void dhcp_option_trailer(struct dhcp *dhcp);
211211

212+
#if ESP_DHCP_OPTION
213+
/* set dhcp option61 */
214+
static void dhcp_option_clientid(struct dhcp *dhcp, struct netif *netif);
215+
/* set dhcp option60 */
216+
static void dhcp_option_vendor(struct dhcp *dhcp, struct netif *netif);
217+
#endif
212218
/** Ensure DHCP PCB is allocated and bound */
213219
static err_t
214220
dhcp_inc_pcb_refcount(void)
@@ -362,6 +368,10 @@ dhcp_select(struct netif *netif)
362368
/* create and initialize the DHCP message header */
363369
result = dhcp_create_msg(netif, dhcp, DHCP_REQUEST);
364370
if (result == ERR_OK) {
371+
#if ESP_DHCP_OPTION
372+
dhcp_option_clientid(dhcp, netif);
373+
dhcp_option_vendor(dhcp, netif);
374+
#endif
365375
dhcp_option(dhcp, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
366376
dhcp_option_short(dhcp, DHCP_MAX_MSG_LEN(netif));
367377

@@ -980,13 +990,22 @@ dhcp_discover(struct netif *netif)
980990
if (result == ERR_OK) {
981991
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_discover: making request\n"));
982992

993+
#if ESP_DHCP_OPTION
994+
dhcp_option_clientid(dhcp, netif);
995+
dhcp_option_vendor(dhcp, netif);
996+
#endif
983997
dhcp_option(dhcp, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
984998
dhcp_option_short(dhcp, DHCP_MAX_MSG_LEN(netif));
985999

9861000
dhcp_option(dhcp, DHCP_OPTION_PARAMETER_REQUEST_LIST, LWIP_ARRAYSIZE(dhcp_discover_request_options));
9871001
for (i = 0; i < LWIP_ARRAYSIZE(dhcp_discover_request_options); i++) {
9881002
dhcp_option_byte(dhcp, dhcp_discover_request_options[i]);
9891003
}
1004+
1005+
#if LWIP_NETIF_HOSTNAME
1006+
dhcp_option_hostname(dhcp, netif);
1007+
#endif /* LWIP_NETIF_HOSTNAME */
1008+
9901009
dhcp_option_trailer(dhcp);
9911010

9921011
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_discover: realloc()ing\n"));
@@ -1153,6 +1172,10 @@ dhcp_renew(struct netif *netif)
11531172
/* create and initialize the DHCP message header */
11541173
result = dhcp_create_msg(netif, dhcp, DHCP_REQUEST);
11551174
if (result == ERR_OK) {
1175+
#if ESP_DHCP_OPTION
1176+
dhcp_option_clientid(dhcp, netif);
1177+
dhcp_option_vendor(dhcp, netif);
1178+
#endif
11561179
dhcp_option(dhcp, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
11571180
dhcp_option_short(dhcp, DHCP_MAX_MSG_LEN(netif));
11581181

@@ -1205,6 +1228,10 @@ dhcp_rebind(struct netif *netif)
12051228
/* create and initialize the DHCP message header */
12061229
result = dhcp_create_msg(netif, dhcp, DHCP_REQUEST);
12071230
if (result == ERR_OK) {
1231+
#if ESP_DHCP_OPTION
1232+
dhcp_option_clientid(dhcp, netif);
1233+
dhcp_option_vendor(dhcp, netif);
1234+
#endif
12081235
dhcp_option(dhcp, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
12091236
dhcp_option_short(dhcp, DHCP_MAX_MSG_LEN(netif));
12101237

@@ -1255,6 +1282,10 @@ dhcp_reboot(struct netif *netif)
12551282
/* create and initialize the DHCP message header */
12561283
result = dhcp_create_msg(netif, dhcp, DHCP_REQUEST);
12571284
if (result == ERR_OK) {
1285+
#if ESP_DHCP_OPTION
1286+
dhcp_option_clientid(dhcp, netif);
1287+
dhcp_option_vendor(dhcp, netif);
1288+
#endif
12581289
dhcp_option(dhcp, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
12591290
dhcp_option_short(dhcp, DHCP_MAX_MSG_LEN_MIN_REQUIRED);
12601291

@@ -1266,6 +1297,10 @@ dhcp_reboot(struct netif *netif)
12661297
dhcp_option_byte(dhcp, dhcp_discover_request_options[i]);
12671298
}
12681299

1300+
#if LWIP_NETIF_HOSTNAME
1301+
dhcp_option_hostname(dhcp, netif);
1302+
#endif /* LWIP_NETIF_HOSTNAME */
1303+
12691304
dhcp_option_trailer(dhcp);
12701305

12711306
pbuf_realloc(dhcp->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
@@ -1463,6 +1498,76 @@ dhcp_option_hostname(struct dhcp *dhcp, struct netif *netif)
14631498
}
14641499
#endif /* LWIP_NETIF_HOSTNAME */
14651500

1501+
#if ESP_DHCP_OPTION
1502+
static void dhcp_option_clientid(struct dhcp *dhcp, struct netif *netif)
1503+
{
1504+
if (netif) {
1505+
u8_t id_len = NETIF_MAX_HWADDR_LEN + 1;
1506+
dhcp_option(dhcp, DHCP_OPTION_CLIENT_ID, id_len);
1507+
dhcp_option_byte(dhcp, DHCP_HTYPE_ETH);
1508+
for (u8_t i = 0; i < NETIF_MAX_HWADDR_LEN; i++) {
1509+
dhcp_option_byte(dhcp, netif->hwaddr[i]);
1510+
}
1511+
}
1512+
}
1513+
1514+
static u8_t vendor_class_len = 0;
1515+
static char *vendor_class_buf = NULL;
1516+
err_t dhcp_set_vendor_class_identifier(u8_t len, char *str)
1517+
{
1518+
if (len == 0 || str == NULL) {
1519+
return ERR_ARG;
1520+
}
1521+
1522+
if (vendor_class_buf) {
1523+
mem_free(vendor_class_buf);
1524+
vendor_class_buf = NULL;
1525+
}
1526+
1527+
vendor_class_buf = (char *)mem_malloc(len + 1);
1528+
1529+
if (vendor_class_buf == NULL) {
1530+
return ERR_MEM;
1531+
}
1532+
1533+
vendor_class_len = len;
1534+
memcpy(vendor_class_buf, str, len);
1535+
return ERR_OK;
1536+
}
1537+
1538+
static void dhcp_option_vendor(struct dhcp *dhcp, struct netif *netif)
1539+
{
1540+
const char *p = NULL;
1541+
u8_t len = 0;
1542+
/* Shrink len to available bytes (need 2 bytes for OPTION_HOSTNAME
1543+
and 1 byte for trailer) */
1544+
size_t available = DHCP_OPTIONS_LEN - dhcp->options_out_len - 3;
1545+
if (vendor_class_buf && vendor_class_len) {
1546+
p = vendor_class_buf;
1547+
LWIP_ASSERT("DHCP: vendor_class_len is too long!", vendor_class_len <= available);
1548+
len = vendor_class_len;
1549+
} else { //use hostname as vendor
1550+
#if LWIP_NETIF_HOSTNAME
1551+
if (netif->hostname != NULL) {
1552+
size_t namelen = strlen(netif->hostname);
1553+
if ((namelen > 0) && (namelen < 0xFF)) {
1554+
p = netif->hostname;
1555+
LWIP_ASSERT("DHCP: hostname is too long!", namelen <= available);
1556+
len = (u8_t) namelen;
1557+
}
1558+
}
1559+
#endif
1560+
}
1561+
1562+
if (p) {
1563+
dhcp_option(dhcp, DHCP_OPTION_US, (u8_t)len);
1564+
while (len--) {
1565+
dhcp_option_byte(dhcp, *p++);
1566+
}
1567+
}
1568+
}
1569+
#endif
1570+
14661571
/**
14671572
* Extract the DHCP message and the DHCP options.
14681573
*

components/lwip/lwip/src/include/lwip/prot/dhcp.h

+3
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,9 @@ typedef enum {
175175
#define DHCP_OVERLOAD_SNAME 2
176176
#define DHCP_OVERLOAD_SNAME_FILE 3
177177

178+
#if ESP_DHCP_OPTION
179+
err_t dhcp_set_vendor_class_identifier(u8_t len, char *str);
180+
#endif
178181

179182
#ifdef __cplusplus
180183
}

components/lwip/port/esp8266/include/lwipopts.h

+1
Original file line numberDiff line numberDiff line change
@@ -2314,4 +2314,5 @@ void tcp_print_status(int status, void* p, uint32_t tmp1, uint32_t tmp2, uint32_
23142314
#define ESP_GRATUITOUS_ARP CONFIG_LWIP_ESP_GRATUITOUS_ARP
23152315
#define ESP_PING 1
23162316

2317+
#define ESP_DHCP_OPTION CONFIG_LWIP_ESP_DHCP_OPTION
23172318
#endif /* __LWIP_HDR_LWIPOPTS_H__ */

0 commit comments

Comments
 (0)