[net][dhcp] 限制地址池耗尽后的客户端分配 (#11947)

在地址池遍历完成后退出分配,避免持续分配客户端对象。
This commit is contained in:
usualtreee authored and GitHub committed 2026-11-05 11:17:07 +08:00
1 parent 0aae54ae97
commit c4d85d8628
1 file changed
+18 -5
+18 -5
View File
@@ -48,7 +48,8 @@
#include <lwip/init.h>
#if (LWIP_VERSION) < 0x02000000U
#error "not support old LWIP"
// cppcheck-suppress preprocessorErrorDirective
#error "not support old LWIP"
#endif
#if !LWIP_IPV4
@@ -232,6 +233,7 @@ dhcp_client_alloc(struct dhcp_server *dhcpserver, struct dhcp_msg *msg,
{
u8_t *opt;
u32_t ipaddr;
u32_t first_candidate;
struct dhcp_client_node *node;
node = dhcp_client_find_by_mac(dhcpserver, msg->chaddr, msg->hlen);
@@ -250,18 +252,29 @@ dhcp_client_alloc(struct dhcp_server *dhcpserver, struct dhcp_msg *msg,
}
}
dhcp_alloc_again:
node = dhcp_client_find_by_ip(dhcpserver, &dhcpserver->current);
if (node != NULL)
/* Check each pool address at most once. */
first_candidate = dhcpserver->current.addr;
do
{
node = dhcp_client_find_by_ip(dhcpserver, &dhcpserver->current);
if (node == NULL)
{
break;
}
ipaddr = (ntohl(dhcpserver->current.addr) + 1);
if (ipaddr > ntohl(dhcpserver->end.addr))
{
ipaddr = ntohl(dhcpserver->start.addr);
}
dhcpserver->current.addr = htonl(ipaddr);
goto dhcp_alloc_again;
} while (dhcpserver->current.addr != first_candidate);
if (node != NULL)
{
return NULL;
}
node = (struct dhcp_client_node *)mem_malloc(sizeof(struct dhcp_client_node));
if (node == NULL)
{